blob: 3a764fa9c7976bc874862dabc31fb0474e678f2a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Christopher Tate45281862010-03-05 15:46:30 -080019import android.app.backup.BackupAgent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.BroadcastReceiver;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070021import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.ComponentName;
23import android.content.ContentProvider;
24import android.content.Context;
25import android.content.IContentProvider;
26import android.content.Intent;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070027import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.pm.ActivityInfo;
29import android.content.pm.ApplicationInfo;
30import android.content.pm.IPackageManager;
31import android.content.pm.InstrumentationInfo;
Christopher Tate346acb12012-10-15 19:20:25 -070032import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.pm.PackageManager;
Sen Hubde75702010-05-28 01:54:03 -070034import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.pm.ProviderInfo;
36import android.content.pm.ServiceInfo;
37import android.content.res.AssetManager;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -070038import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.res.Configuration;
40import android.content.res.Resources;
41import android.database.sqlite.SQLiteDatabase;
42import android.database.sqlite.SQLiteDebug;
Vasu Noric3849202010-03-09 10:47:25 -080043import android.database.sqlite.SQLiteDebug.DbStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.graphics.Bitmap;
45import android.graphics.Canvas;
Jeff Brownbd6e1502012-08-28 03:27:37 -070046import android.hardware.display.DisplayManagerGlobal;
Robert Greenwalt434203a2010-10-11 16:00:27 -070047import android.net.IConnectivityManager;
48import android.net.Proxy;
Jason Monk207900c2014-04-25 15:00:09 -040049import android.net.ProxyInfo;
Jason Monk83520b92014-05-09 15:16:06 -040050import android.net.Uri;
Romain Guya9582652011-11-10 14:20:10 -080051import android.opengl.GLUtils;
Joe Onoratod630f102011-03-17 18:42:26 -070052import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070053import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Bundle;
55import android.os.Debug;
Geremy Condrab7faaf42012-09-19 18:07:42 -070056import android.os.DropBoxManager;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070057import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.os.Handler;
59import android.os.IBinder;
60import android.os.Looper;
61import android.os.Message;
62import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070063import android.os.ParcelFileDescriptor;
Craig Mautnera0026042014-04-23 11:45:37 -070064import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.os.Process;
66import android.os.RemoteException;
67import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070068import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.os.SystemClock;
Jeff Brownefd43bd2012-09-21 17:02:35 -070070import android.os.SystemProperties;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070071import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070072import android.os.UserHandle;
Narayan Kamathccb2a082013-12-19 14:49:36 +000073import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.util.AndroidRuntimeException;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070075import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.util.DisplayMetrics;
77import android.util.EventLog;
78import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070079import android.util.LogPrinter;
Craig Mautnereb8abf72014-07-02 15:04:09 -070080import android.util.Pair;
Jeff Brown6754ba22011-12-14 20:20:01 -080081import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080082import android.util.Slog;
Adam Powell14874662013-07-18 19:42:41 -070083import android.util.SuperNotCalledException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070085import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.view.View;
87import android.view.ViewDebug;
88import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070089import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.view.Window;
91import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -070092import android.view.WindowManagerGlobal;
Jason Samsa6f338c2012-02-24 16:22:16 -080093import android.renderscript.RenderScript;
Kenny Root8b514752013-02-04 09:35:16 -080094import android.security.AndroidKeyStoreProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
Dianne Hackborn91097de2014-04-04 18:02:06 -070096import com.android.internal.app.IVoiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import com.android.internal.os.BinderInternal;
98import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070099import com.android.internal.os.SamplingProfilerIntegration;
Dianne Hackborn8c841092013-06-24 13:46:13 -0700100import com.android.internal.util.FastPrintWriter;
Kenny Root12e75222013-04-23 22:34:24 -0700101import com.android.org.conscrypt.OpenSSLSocketImpl;
Robin Lee3d076af2014-04-25 14:57:49 +0100102import com.android.org.conscrypt.TrustedCertificateStore;
Jeff Sharkeydd97f422013-10-08 17:01:30 -0700103import com.google.android.collect.Lists;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104
Dave Allison24bafbc2013-11-13 17:15:50 -0800105import dalvik.system.VMRuntime;
106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import java.io.File;
108import java.io.FileDescriptor;
109import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700110import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import java.io.PrintWriter;
112import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700113import java.net.InetAddress;
Kenny Root8b514752013-02-04 09:35:16 -0800114import java.security.Security;
Narayan Kamathccb2a082013-12-19 14:49:36 +0000115import java.text.DateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import java.util.List;
118import java.util.Locale;
119import java.util.Map;
Kenny Roote6585b32013-12-13 12:00:26 -0800120import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121import java.util.TimeZone;
122import java.util.regex.Pattern;
123
Geremy Condrab7faaf42012-09-19 18:07:42 -0700124import libcore.io.DropBox;
Geremy Condra69689a72012-09-11 16:57:17 -0700125import libcore.io.EventLogger;
Jeff Sharkeye861b422012-03-01 20:59:22 -0800126import libcore.io.IoUtils;
127
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800128import dalvik.system.CloseGuard;
Mathieu Chartier1e370902013-07-18 10:58:01 -0700129import dalvik.system.VMRuntime;
Bob Leee5408332009-09-04 18:31:17 -0700130
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700131final class RemoteServiceException extends AndroidRuntimeException {
132 public RemoteServiceException(String msg) {
133 super(msg);
134 }
135}
136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137/**
138 * This manages the execution of the main thread in an
139 * application process, scheduling and executing activities,
140 * broadcasts, and other operations on it as the activity
141 * manager requests.
142 *
143 * {@hide}
144 */
145public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700146 /** @hide */
147 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700148 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700149 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700150 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700151 /** @hide */
152 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700153 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700154 private static final boolean DEBUG_BACKUP = false;
Craig Mautner88c05892013-06-28 09:47:45 -0700155 public static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800156 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700157 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700158 private static final boolean DEBUG_PROVIDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
160 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
161 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
162 private static final int LOG_ON_PAUSE_CALLED = 30021;
163 private static final int LOG_ON_RESUME_CALLED = 30022;
164
Jeff Browndefd4a62014-03-10 21:24:37 -0700165 private ContextImpl mSystemContext;
Bob Leee5408332009-09-04 18:31:17 -0700166
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700167 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700169 final ApplicationThread mAppThread = new ApplicationThread();
170 final Looper mLooper = Looper.myLooper();
171 final H mH = new H();
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700172 final ArrayMap<IBinder, ActivityClientRecord> mActivities
173 = new ArrayMap<IBinder, ActivityClientRecord>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700174 // List of new activities (via ActivityRecord.nextIdle) that should
175 // be reported when next we idle.
176 ActivityClientRecord mNewActivities = null;
177 // Number of activities that are currently visible on-screen.
178 int mNumVisibleActivities = 0;
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700179 final ArrayMap<IBinder, Service> mServices
180 = new ArrayMap<IBinder, Service>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700181 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700182 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700183 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700184 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700185 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700186 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700187 Application mInitialApplication;
188 final ArrayList<Application> mAllApplications
189 = new ArrayList<Application>();
190 // set of instantiated backup agents, keyed by package name
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700191 final ArrayMap<String, BackupAgent> mBackupAgents = new ArrayMap<String, BackupAgent>();
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800192 /** Reference to singleton {@link ActivityThread} */
193 private static ActivityThread sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700194 Instrumentation mInstrumentation;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700195 String mInstrumentationPackageName = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700196 String mInstrumentationAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700197 String[] mInstrumentationSplitAppDirs = null;
198 String mInstrumentationLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700199 String mInstrumentedAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700200 String[] mInstrumentedSplitAppDirs = null;
201 String mInstrumentedLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700202 boolean mSystemThread = false;
203 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700205 // These can be accessed by multiple threads; mPackages is the lock.
206 // XXX For now we keep around information about all packages we have
207 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800208 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700209 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800210 // which means this lock gets held while the activity and window managers
211 // holds their own lock. Thus you MUST NEVER call back into the activity manager
212 // or window manager or anything that depends on them while holding this lock.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700213 final ArrayMap<String, WeakReference<LoadedApk>> mPackages
214 = new ArrayMap<String, WeakReference<LoadedApk>>();
215 final ArrayMap<String, WeakReference<LoadedApk>> mResourcePackages
216 = new ArrayMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700217 final ArrayList<ActivityClientRecord> mRelaunchingActivities
218 = new ArrayList<ActivityClientRecord>();
219 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220
Craig Mautner88c05892013-06-28 09:47:45 -0700221 private final ResourcesManager mResourcesManager;
222
Jeff Sharkey6d515712012-09-20 16:06:08 -0700223 private static final class ProviderKey {
224 final String authority;
225 final int userId;
226
227 public ProviderKey(String authority, int userId) {
228 this.authority = authority;
229 this.userId = userId;
230 }
231
232 @Override
233 public boolean equals(Object o) {
234 if (o instanceof ProviderKey) {
235 final ProviderKey other = (ProviderKey) o;
Kenny Roote6585b32013-12-13 12:00:26 -0800236 return Objects.equals(authority, other.authority) && userId == other.userId;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700237 }
238 return false;
239 }
240
241 @Override
242 public int hashCode() {
243 return ((authority != null) ? authority.hashCode() : 0) ^ userId;
244 }
245 }
246
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700247 // The lock of mProviderMap protects the following variables.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700248 final ArrayMap<ProviderKey, ProviderClientRecord> mProviderMap
249 = new ArrayMap<ProviderKey, ProviderClientRecord>();
250 final ArrayMap<IBinder, ProviderRefCount> mProviderRefCountMap
251 = new ArrayMap<IBinder, ProviderRefCount>();
252 final ArrayMap<IBinder, ProviderClientRecord> mLocalProviders
253 = new ArrayMap<IBinder, ProviderClientRecord>();
254 final ArrayMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
255 = new ArrayMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700257 final ArrayMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
258 = new ArrayMap<Activity, ArrayList<OnActivityPausedListener>>();
Jeff Hamilton52d32032011-01-08 15:31:26 -0600259
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700260 final GcIdler mGcIdler = new GcIdler();
261 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700263 static Handler sMainThreadHandler; // set once in main()
264
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800265 Bundle mCoreSettings = null;
266
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400267 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700269 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 Intent intent;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700271 IVoiceInteractor voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -0700273 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 Activity activity;
275 Window window;
276 Activity parent;
277 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700278 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 boolean paused;
280 boolean stopped;
281 boolean hideForNow;
282 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700283 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700284 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700286 String profileFile;
287 ParcelFileDescriptor profileFd;
288 boolean autoStopProfiler;
289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400291 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700292 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293
294 List<ResultInfo> pendingResults;
295 List<Intent> pendingIntents;
296
297 boolean startsNotResumed;
298 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800299 int pendingConfigChanges;
300 boolean onlyLocalRequest;
301
302 View mPendingRemoveWindow;
303 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700305 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 parent = null;
307 embeddedID = null;
308 paused = false;
309 stopped = false;
310 hideForNow = false;
311 nextIdle = null;
312 }
313
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800314 public boolean isPreHoneycomb() {
315 if (activity != null) {
316 return activity.getApplicationInfo().targetSdkVersion
317 < android.os.Build.VERSION_CODES.HONEYCOMB;
318 }
319 return false;
320 }
321
Craig Mautnera0026042014-04-23 11:45:37 -0700322 public boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700323 return activityInfo.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS;
Craig Mautnera0026042014-04-23 11:45:37 -0700324 }
325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700327 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 return "ActivityRecord{"
329 + Integer.toHexString(System.identityHashCode(this))
330 + " token=" + token + " " + (componentName == null
331 ? "no component name" : componentName.toShortString())
332 + "}";
333 }
334 }
335
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700336 final class ProviderClientRecord {
337 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 final IContentProvider mProvider;
339 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700340 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700342 ProviderClientRecord(String[] names, IContentProvider provider,
343 ContentProvider localProvider,
344 IActivityManager.ContentProviderHolder holder) {
345 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 mProvider = provider;
347 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700348 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 }
350 }
351
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400352 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 List<Intent> intents;
354 IBinder token;
355 public String toString() {
356 return "NewIntentData{intents=" + intents + " token=" + token + "}";
357 }
358 }
359
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400360 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700361 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700362 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
363 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
364 token, sendingUser);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700365 this.intent = intent;
366 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 Intent intent;
369 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400370 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 public String toString() {
372 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700373 info.packageName + " resultCode=" + getResultCode()
374 + " resultData=" + getResultData() + " resultExtras="
375 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 }
377 }
378
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400379 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700380 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400381 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700382 int backupMode;
383 public String toString() {
384 return "CreateBackupAgentData{appInfo=" + appInfo
385 + " backupAgent=" + appInfo.backupAgentName
386 + " mode=" + backupMode + "}";
387 }
388 }
Bob Leee5408332009-09-04 18:31:17 -0700389
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400390 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 IBinder token;
392 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400393 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 Intent intent;
395 public String toString() {
396 return "CreateServiceData{token=" + token + " className="
397 + info.name + " packageName=" + info.packageName
398 + " intent=" + intent + "}";
399 }
400 }
401
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400402 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 IBinder token;
404 Intent intent;
405 boolean rebind;
406 public String toString() {
407 return "BindServiceData{token=" + token + " intent=" + intent + "}";
408 }
409 }
410
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400411 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700413 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700415 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 Intent args;
417 public String toString() {
418 return "ServiceArgsData{token=" + token + " startId=" + startId
419 + " args=" + args + "}";
420 }
421 }
422
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400423 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700424 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 String processName;
426 ApplicationInfo appInfo;
427 List<ProviderInfo> providers;
428 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 Bundle instrumentationArgs;
430 IInstrumentationWatcher instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800431 IUiAutomationConnection instrumentationUiAutomationConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 int debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800433 boolean enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700434 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700435 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400437 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700438
439 /** Initial values for {@link Profiler}. */
440 String initProfileFile;
441 ParcelFileDescriptor initProfileFd;
442 boolean initAutoStopProfiler;
443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 public String toString() {
445 return "AppBindData{appInfo=" + appInfo + "}";
446 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700447 }
448
449 static final class Profiler {
450 String profileFile;
451 ParcelFileDescriptor profileFd;
452 boolean autoStopProfiler;
453 boolean profiling;
454 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700455 public void setProfiler(String file, ParcelFileDescriptor fd) {
456 if (profiling) {
457 if (fd != null) {
458 try {
459 fd.close();
460 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700461 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700462 }
463 }
464 return;
465 }
466 if (profileFd != null) {
467 try {
468 profileFd.close();
469 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700470 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700471 }
472 }
473 profileFile = file;
474 profileFd = fd;
475 }
476 public void startProfiling() {
477 if (profileFd == null || profiling) {
478 return;
479 }
480 try {
481 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
482 8 * 1024 * 1024, 0);
483 profiling = true;
484 } catch (RuntimeException e) {
485 Slog.w(TAG, "Profiling failed on path " + profileFile);
486 try {
487 profileFd.close();
488 profileFd = null;
489 } catch (IOException e2) {
490 Slog.w(TAG, "Failure closing profile fd", e2);
491 }
492 }
493 }
494 public void stopProfiling() {
495 if (profiling) {
496 profiling = false;
497 Debug.stopMethodTracing();
498 if (profileFd != null) {
499 try {
500 profileFd.close();
501 } catch (IOException e) {
502 }
503 }
504 profileFd = null;
505 profileFile = null;
506 }
507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 }
509
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400510 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700511 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700512 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800513 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 }
516
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400517 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 IBinder token;
519 List<ResultInfo> results;
520 public String toString() {
521 return "ResultData{token=" + token + " results" + results + "}";
522 }
523 }
524
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400525 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800526 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 String what;
528 String who;
529 }
530
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400531 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700532 String path;
533 ParcelFileDescriptor fd;
534 }
535
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400536 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700537 String path;
538 ParcelFileDescriptor fd;
539 }
540
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400541 static final class UpdateCompatibilityData {
542 String pkg;
543 CompatibilityInfo info;
544 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800545
Adam Skorydfc7fd72013-08-05 19:23:41 -0700546 static final class RequestAssistContextExtras {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800547 IBinder activityToken;
548 IBinder requestToken;
549 int requestType;
550 }
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700551
Romain Guy65b345f2011-07-27 18:51:50 -0700552 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700553
Romain Guy65b345f2011-07-27 18:51:50 -0700554 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700555 private static final String ONE_COUNT_COLUMN = "%21s %8d";
556 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700557 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700558
Dianne Hackborna413dc02013-07-12 12:02:55 -0700559 private int mLastProcessState = -1;
560
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700561 private void updatePendingConfiguration(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -0700562 synchronized (mResourcesManager) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700563 if (mPendingConfiguration == null ||
564 mPendingConfiguration.isOtherSeqNewer(config)) {
565 mPendingConfiguration = config;
566 }
567 }
568 }
569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 public final void schedulePauseActivity(IBinder token, boolean finished,
571 boolean userLeaving, int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700572 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
574 token,
575 (userLeaving ? 1 : 0),
576 configChanges);
577 }
578
579 public final void scheduleStopActivity(IBinder token, boolean showWindow,
580 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700581 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
583 token, 0, configChanges);
584 }
585
586 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700587 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
589 token);
590 }
591
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800592 public final void scheduleSleeping(IBinder token, boolean sleeping) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700593 sendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800594 }
595
Dianne Hackborna413dc02013-07-12 12:02:55 -0700596 public final void scheduleResumeActivity(IBinder token, int processState,
Adam Powellcfbe9be2013-11-06 14:58:58 -0800597 boolean isForward, Bundle resumeArgs) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700598 updateProcessState(processState, false);
Craig Mautner233ceee2014-05-09 17:05:11 -0700599 sendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 }
601
602 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
603 ResultData res = new ResultData();
604 res.token = token;
605 res.results = results;
Jeff Brown9ef09972013-10-15 20:49:59 -0700606 sendMessage(H.SEND_RESULT, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 }
608
609 // we use token to identify this activity without having to send the
610 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700611 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700612 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
Craig Mautnera0026042014-04-23 11:45:37 -0700613 IVoiceInteractor voiceInteractor, int procState, Bundle state,
614 PersistableBundle persistentState, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700615 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
Craig Mautner233ceee2014-05-09 17:05:11 -0700616 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700617
618 updateProcessState(procState, false);
619
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700620 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621
622 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700623 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 r.intent = intent;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700625 r.voiceInteractor = voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400627 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 r.state = state;
Craig Mautnera0026042014-04-23 11:45:37 -0700629 r.persistentState = persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630
631 r.pendingResults = pendingResults;
632 r.pendingIntents = pendingNewIntents;
633
634 r.startsNotResumed = notResumed;
635 r.isForward = isForward;
636
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700637 r.profileFile = profileName;
638 r.profileFd = profileFd;
639 r.autoStopProfiler = autoStopProfiler;
640
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700641 updatePendingConfiguration(curConfig);
642
Jeff Brown9ef09972013-10-15 20:49:59 -0700643 sendMessage(H.LAUNCH_ACTIVITY, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 }
645
646 public final void scheduleRelaunchActivity(IBinder token,
647 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800648 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800649 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
650 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 }
652
653 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
654 NewIntentData data = new NewIntentData();
655 data.intents = intents;
656 data.token = token;
657
Jeff Brown9ef09972013-10-15 20:49:59 -0700658 sendMessage(H.NEW_INTENT, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 }
660
661 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
662 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700663 sendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 configChanges);
665 }
666
667 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400668 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700669 boolean sync, int sendingUser, int processState) {
670 updateProcessState(processState, false);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700671 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700672 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400674 r.compatInfo = compatInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700675 sendMessage(H.RECEIVER, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 }
677
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400678 public final void scheduleCreateBackupAgent(ApplicationInfo app,
679 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700680 CreateBackupAgentData d = new CreateBackupAgentData();
681 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400682 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700683 d.backupMode = backupMode;
684
Jeff Brown9ef09972013-10-15 20:49:59 -0700685 sendMessage(H.CREATE_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700686 }
687
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400688 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
689 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700690 CreateBackupAgentData d = new CreateBackupAgentData();
691 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400692 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700693
Jeff Brown9ef09972013-10-15 20:49:59 -0700694 sendMessage(H.DESTROY_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700695 }
696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 public final void scheduleCreateService(IBinder token,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700698 ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
699 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 CreateServiceData s = new CreateServiceData();
701 s.token = token;
702 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400703 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704
Jeff Brown9ef09972013-10-15 20:49:59 -0700705 sendMessage(H.CREATE_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 }
707
708 public final void scheduleBindService(IBinder token, Intent intent,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700709 boolean rebind, int processState) {
710 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 BindServiceData s = new BindServiceData();
712 s.token = token;
713 s.intent = intent;
714 s.rebind = rebind;
715
Amith Yamasani742a6712011-05-04 14:49:28 -0700716 if (DEBUG_SERVICE)
717 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
718 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
Jeff Brown9ef09972013-10-15 20:49:59 -0700719 sendMessage(H.BIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 }
721
722 public final void scheduleUnbindService(IBinder token, Intent intent) {
723 BindServiceData s = new BindServiceData();
724 s.token = token;
725 s.intent = intent;
726
Jeff Brown9ef09972013-10-15 20:49:59 -0700727 sendMessage(H.UNBIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 }
729
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700730 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700731 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 ServiceArgsData s = new ServiceArgsData();
733 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700734 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700736 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 s.args = args;
738
Jeff Brown9ef09972013-10-15 20:49:59 -0700739 sendMessage(H.SERVICE_ARGS, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 }
741
742 public final void scheduleStopService(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700743 sendMessage(H.STOP_SERVICE, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 }
745
746 public final void bindApplication(String processName,
747 ApplicationInfo appInfo, List<ProviderInfo> providers,
748 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700749 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800751 IUiAutomationConnection instrumentationUiConnection, int debugMode,
752 boolean enableOpenGlTrace, boolean isRestrictedBackupMode, boolean persistent,
753 Configuration config, CompatibilityInfo compatInfo, Map<String, IBinder> services,
754 Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755
756 if (services != null) {
757 // Setup the service cache in the ServiceManager
758 ServiceManager.initServiceCache(services);
759 }
760
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800761 setCoreSettings(coreSettings);
762
Dave Allison0efbd9a2014-01-30 14:19:51 -0800763 /*
764 * Two possible indications that this package could be
765 * sharing its runtime with other packages:
766 *
767 * 1.) the sharedUserId attribute is set in the manifest,
768 * indicating a request to share a VM with other
769 * packages with the same sharedUserId.
770 *
771 * 2.) the application element of the manifest has an
772 * attribute specifying a non-default process name,
773 * indicating the desire to run in another packages VM.
774 *
775 * If sharing is enabled we do not have a unique application
776 * in a process and therefore cannot rely on the package
777 * name inside the runtime.
778 */
779 IPackageManager pm = getPackageManager();
780 android.content.pm.PackageInfo pi = null;
781 try {
782 pi = pm.getPackageInfo(appInfo.packageName, 0, UserHandle.myUserId());
783 } catch (RemoteException e) {
784 }
785 if (pi != null) {
786 boolean sharedUserIdSet = (pi.sharedUserId != null);
787 boolean processNameNotDefault =
788 (pi.applicationInfo != null &&
789 !appInfo.packageName.equals(pi.applicationInfo.processName));
790 boolean sharable = (sharedUserIdSet || processNameNotDefault);
791
792 // Tell the VMRuntime about the application, unless it is shared
793 // inside a process.
794 if (!sharable) {
795 VMRuntime.registerAppInfo(appInfo.packageName, appInfo.dataDir,
796 appInfo.processName);
797 }
798 }
Dave Allison24bafbc2013-11-13 17:15:50 -0800799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 AppBindData data = new AppBindData();
801 data.processName = processName;
802 data.appInfo = appInfo;
803 data.providers = providers;
804 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 data.instrumentationArgs = instrumentationArgs;
806 data.instrumentationWatcher = instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800807 data.instrumentationUiAutomationConnection = instrumentationUiConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 data.debugMode = debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800809 data.enableOpenGlTrace = enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700810 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700811 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400813 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700814 data.initProfileFile = profileFile;
815 data.initProfileFd = profileFd;
816 data.initAutoStopProfiler = false;
Jeff Brown9ef09972013-10-15 20:49:59 -0700817 sendMessage(H.BIND_APPLICATION, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 }
819
820 public final void scheduleExit() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700821 sendMessage(H.EXIT_APPLICATION, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 }
823
Christopher Tate5e1ab332009-09-01 20:32:49 -0700824 public final void scheduleSuicide() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700825 sendMessage(H.SUICIDE, null);
Christopher Tate5e1ab332009-09-01 20:32:49 -0700826 }
827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700829 updatePendingConfiguration(config);
Jeff Brown9ef09972013-10-15 20:49:59 -0700830 sendMessage(H.CONFIGURATION_CHANGED, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 }
832
833 public void updateTimeZone() {
834 TimeZone.setDefault(null);
835 }
836
Robert Greenwalt03595d02010-11-02 14:08:23 -0700837 public void clearDnsCache() {
838 // a non-standard API to get this to libcore
839 InetAddress.clearDnsCache();
840 }
841
Jason Monk83520b92014-05-09 15:16:06 -0400842 public void setHttpProxy(String host, String port, String exclList, Uri pacFileUrl) {
Jason Monk602b2322013-07-03 17:04:33 -0400843 Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
Robert Greenwalt434203a2010-10-11 16:00:27 -0700844 }
845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 public void processInBackground() {
847 mH.removeMessages(H.GC_WHEN_IDLE);
848 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
849 }
850
851 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700852 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700853 try {
854 data.fd = ParcelFileDescriptor.dup(fd);
855 data.token = servicetoken;
856 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700857 sendMessage(H.DUMP_SERVICE, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700858 } catch (IOException e) {
859 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 }
861 }
862
863 // This function exists to make sure all receiver dispatching is
864 // correctly ordered, since these are one-way calls and the binder driver
865 // applies transaction ordering per object for such calls.
866 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700867 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700868 boolean sticky, int sendingUser, int processState) throws RemoteException {
869 updateProcessState(processState, false);
Dianne Hackborn20e80982012-08-31 19:00:44 -0700870 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
871 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 }
Bob Leee5408332009-09-04 18:31:17 -0700873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 public void scheduleLowMemory() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700875 sendMessage(H.LOW_MEMORY, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 }
877
878 public void scheduleActivityConfigurationChanged(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700879 sendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 }
881
Romain Guy7eabe552011-07-21 14:56:34 -0700882 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
883 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700884 ProfilerControlData pcd = new ProfilerControlData();
885 pcd.path = path;
886 pcd.fd = fd;
Jeff Brown9ef09972013-10-15 20:49:59 -0700887 sendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800888 }
889
Andy McFadden824c5102010-07-09 16:26:57 -0700890 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
891 DumpHeapData dhd = new DumpHeapData();
892 dhd.path = path;
893 dhd.fd = fd;
Jeff Brown9ef09972013-10-15 20:49:59 -0700894 sendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0, 0, true /*async*/);
Andy McFadden824c5102010-07-09 16:26:57 -0700895 }
896
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700897 public void setSchedulingGroup(int group) {
898 // Note: do this immediately, since going into the foreground
899 // should happen regardless of what pending work we have to do
900 // and the activity manager will wait for us to report back that
901 // we are done before sending us to the background.
902 try {
903 Process.setProcessGroup(Process.myPid(), group);
904 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800905 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700906 }
907 }
Bob Leee5408332009-09-04 18:31:17 -0700908
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700909 public void dispatchPackageBroadcast(int cmd, String[] packages) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700910 sendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700911 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700912
913 public void scheduleCrash(String msg) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700914 sendMessage(H.SCHEDULE_CRASH, msg);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700915 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700916
Dianne Hackborn30d71892010-12-11 10:37:55 -0800917 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
918 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700919 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700920 try {
921 data.fd = ParcelFileDescriptor.dup(fd);
922 data.token = activitytoken;
923 data.prefix = prefix;
924 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700925 sendMessage(H.DUMP_ACTIVITY, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700926 } catch (IOException e) {
927 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700928 }
929 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700930
Marco Nelissen18cb2872011-11-15 11:19:53 -0800931 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
932 String[] args) {
933 DumpComponentInfo data = new DumpComponentInfo();
934 try {
935 data.fd = ParcelFileDescriptor.dup(fd);
936 data.token = providertoken;
937 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700938 sendMessage(H.DUMP_PROVIDER, data, 0, 0, true /*async*/);
Marco Nelissen18cb2872011-11-15 11:19:53 -0800939 } catch (IOException e) {
940 Slog.w(TAG, "dumpProvider failed", e);
941 }
942 }
943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 @Override
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700945 public void dumpMemInfo(FileDescriptor fd, Debug.MemoryInfo mem, boolean checkin,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700946 boolean dumpFullInfo, boolean dumpDalvik, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700947 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700948 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700949 try {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700950 dumpMemInfo(pw, mem, checkin, dumpFullInfo, dumpDalvik);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700951 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700952 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700953 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700954 }
955
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700956 private void dumpMemInfo(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700957 boolean dumpFullInfo, boolean dumpDalvik) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 long nativeMax = Debug.getNativeHeapSize() / 1024;
959 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
960 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 Runtime runtime = Runtime.getRuntime();
963
964 long dalvikMax = runtime.totalMemory() / 1024;
965 long dalvikFree = runtime.freeMemory() / 1024;
966 long dalvikAllocated = dalvikMax - dalvikFree;
967 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700968 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700969 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
970 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 int globalAssetCount = AssetManager.getGlobalAssetCount();
972 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
973 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
974 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
975 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700976 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800977 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700978
Dianne Hackborne77187d2013-10-25 16:32:41 -0700979 dumpMemInfoTable(pw, memInfo, checkin, dumpFullInfo, dumpDalvik, Process.myPid(),
980 (mBoundApplication != null) ? mBoundApplication.processName : "unknown",
981 nativeMax, nativeAllocated, nativeFree,
982 dalvikMax, dalvikAllocated, dalvikFree);
983
Dianne Hackbornb437e092011-08-05 17:50:29 -0700984 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 // NOTE: if you change anything significant below, also consider changing
986 // ACTIVITY_THREAD_CHECKIN_VERSION.
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 // Object counts
989 pw.print(viewInstanceCount); pw.print(',');
990 pw.print(viewRootInstanceCount); pw.print(',');
991 pw.print(appContextInstanceCount); pw.print(',');
992 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 pw.print(globalAssetCount); pw.print(',');
995 pw.print(globalAssetManagerCount); pw.print(',');
996 pw.print(binderLocalObjectCount); pw.print(',');
997 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 pw.print(binderDeathObjectCount); pw.print(',');
1000 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 // SQL
Vasu Noric3849202010-03-09 10:47:25 -08001003 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -08001004 pw.print(stats.memoryUsed / 1024); pw.print(',');
1005 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -07001006 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001007 for (int i = 0; i < stats.dbStats.size(); i++) {
1008 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -07001009 pw.print(','); pw.print(dbStats.dbName);
1010 pw.print(','); pw.print(dbStats.pageSize);
1011 pw.print(','); pw.print(dbStats.dbSize);
1012 pw.print(','); pw.print(dbStats.lookaside);
1013 pw.print(','); pw.print(dbStats.cache);
1014 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -08001015 }
Dianne Hackbornb437e092011-08-05 17:50:29 -07001016 pw.println();
Bob Leee5408332009-09-04 18:31:17 -07001017
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001018 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 }
Bob Leee5408332009-09-04 18:31:17 -07001020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 pw.println(" ");
1022 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001023 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 viewRootInstanceCount);
1025
1026 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1027 "Activities:", activityInstanceCount);
1028
1029 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1030 "AssetManagers:", globalAssetManagerCount);
1031
1032 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1033 "Proxy Binders:", binderProxyObjectCount);
1034 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
1035
1036 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 // SQLite mem info
1039 pw.println(" ");
1040 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001041 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1042 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1043 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001044 pw.println(" ");
1045 int N = stats.dbStats.size();
1046 if (N > 0) {
1047 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001048 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1049 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001050 for (int i = 0; i < N; i++) {
1051 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001052 printRow(pw, DB_INFO_FORMAT,
1053 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1054 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1055 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1056 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001057 }
1058 }
Bob Leee5408332009-09-04 18:31:17 -07001059
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001060 // Asset details.
1061 String assetAlloc = AssetManager.getAssetAllocations();
1062 if (assetAlloc != null) {
1063 pw.println(" ");
1064 pw.println(" Asset Allocations");
1065 pw.print(assetAlloc);
1066 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001067 }
1068
1069 @Override
1070 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1071 dumpGraphicsInfo(fd);
Jeff Brown98365d72012-08-19 20:30:52 -07001072 WindowManagerGlobal.getInstance().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 }
1074
Jeff Brown6754ba22011-12-14 20:20:01 -08001075 @Override
1076 public void dumpDbInfo(FileDescriptor fd, String[] args) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07001077 PrintWriter pw = new FastPrintWriter(new FileOutputStream(fd));
Jeff Brown6754ba22011-12-14 20:20:01 -08001078 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1079 SQLiteDebug.dump(printer, args);
1080 pw.flush();
1081 }
1082
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001083 @Override
1084 public void unstableProviderDied(IBinder provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001085 sendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001086 }
1087
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001088 @Override
Adam Skorydfc7fd72013-08-05 19:23:41 -07001089 public void requestAssistContextExtras(IBinder activityToken, IBinder requestToken,
Adam Skory7140a252013-09-11 12:04:58 +01001090 int requestType) {
Adam Skorydfc7fd72013-08-05 19:23:41 -07001091 RequestAssistContextExtras cmd = new RequestAssistContextExtras();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001092 cmd.activityToken = activityToken;
1093 cmd.requestToken = requestToken;
1094 cmd.requestType = requestType;
Jeff Brown9ef09972013-10-15 20:49:59 -07001095 sendMessage(H.REQUEST_ASSIST_CONTEXT_EXTRAS, cmd);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001096 }
1097
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001098 public void setCoreSettings(Bundle coreSettings) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001099 sendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001100 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001101
1102 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1103 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1104 ucd.pkg = pkg;
1105 ucd.info = info;
Jeff Brown9ef09972013-10-15 20:49:59 -07001106 sendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001107 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001108
1109 public void scheduleTrimMemory(int level) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001110 sendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001111 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001112
Craig Mautner5eda9b32013-07-02 11:58:16 -07001113 public void scheduleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001114 sendMessage(H.TRANSLUCENT_CONVERSION_COMPLETE, token, drawComplete ? 1 : 0);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001115 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001116
Craig Mautnereb8abf72014-07-02 15:04:09 -07001117 public void scheduleOnNewActivityOptions(IBinder token, ActivityOptions options) {
1118 sendMessage(H.ON_NEW_ACTIVITY_OPTIONS,
1119 new Pair<IBinder, ActivityOptions>(token, options));
1120 }
1121
Dianne Hackborna413dc02013-07-12 12:02:55 -07001122 public void setProcessState(int state) {
1123 updateProcessState(state, true);
1124 }
1125
1126 public void updateProcessState(int processState, boolean fromIpc) {
1127 synchronized (this) {
1128 if (mLastProcessState != processState) {
1129 mLastProcessState = processState;
Mathieu Chartier1e370902013-07-18 10:58:01 -07001130 // Update Dalvik state based on ActivityManager.PROCESS_STATE_* constants.
1131 final int DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE = 0;
1132 final int DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1;
1133 int dalvikProcessState = DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE;
1134 // TODO: Tune this since things like gmail sync are important background but not jank perceptible.
1135 if (processState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
1136 dalvikProcessState = DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE;
1137 }
1138 VMRuntime.getRuntime().updateProcessState(dalvikProcessState);
Dianne Hackborna413dc02013-07-12 12:02:55 -07001139 if (false) {
1140 Slog.i(TAG, "******************* PROCESS STATE CHANGED TO: " + processState
1141 + (fromIpc ? " (from ipc": ""));
1142 }
1143 }
1144 }
1145 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001146
1147 @Override
1148 public void scheduleInstallProvider(ProviderInfo provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001149 sendMessage(H.INSTALL_PROVIDER, provider);
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001150 }
Narayan Kamathccb2a082013-12-19 14:49:36 +00001151
1152 @Override
1153 public final void updateTimePrefs(boolean is24Hour) {
1154 DateFormat.set24HourTimePref(is24Hour);
1155 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07001156
1157 @Override
1158 public void scheduleStopMediaPlaying(IBinder token) {
1159 sendMessage(H.STOP_MEDIA_PLAYING, token);
1160 }
1161
1162 @Override
1163 public void scheduleBackgroundMediaPlayingChanged(IBinder token, boolean playing) {
1164 sendMessage(H.BACKGROUND_MEDIA_PLAYING_CHANGED, token, playing ? 1 : 0);
1165 }
Craig Mautner8746a472014-07-24 15:12:54 -07001166
1167 public void scheduleEnterAnimationComplete(IBinder token) {
1168 sendMessage(H.ENTER_ANIMATION_COMPLETE, token);
1169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 }
1171
Romain Guy65b345f2011-07-27 18:51:50 -07001172 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 public static final int LAUNCH_ACTIVITY = 100;
1174 public static final int PAUSE_ACTIVITY = 101;
1175 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1176 public static final int STOP_ACTIVITY_SHOW = 103;
1177 public static final int STOP_ACTIVITY_HIDE = 104;
1178 public static final int SHOW_WINDOW = 105;
1179 public static final int HIDE_WINDOW = 106;
1180 public static final int RESUME_ACTIVITY = 107;
1181 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001182 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 public static final int BIND_APPLICATION = 110;
1184 public static final int EXIT_APPLICATION = 111;
1185 public static final int NEW_INTENT = 112;
1186 public static final int RECEIVER = 113;
1187 public static final int CREATE_SERVICE = 114;
1188 public static final int SERVICE_ARGS = 115;
1189 public static final int STOP_SERVICE = 116;
Dianne Hackborn09233282014-04-30 11:33:59 -07001190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 public static final int CONFIGURATION_CHANGED = 118;
1192 public static final int CLEAN_UP_CONTEXT = 119;
1193 public static final int GC_WHEN_IDLE = 120;
1194 public static final int BIND_SERVICE = 121;
1195 public static final int UNBIND_SERVICE = 122;
1196 public static final int DUMP_SERVICE = 123;
1197 public static final int LOW_MEMORY = 124;
1198 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1199 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001200 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001201 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001202 public static final int DESTROY_BACKUP_AGENT = 129;
1203 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001204 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001205 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001206 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001207 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001208 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001209 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001210 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001211 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001212 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001213 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001214 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001215 public static final int UNSTABLE_PROVIDER_DIED = 142;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001216 public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001217 public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001218 public static final int INSTALL_PROVIDER = 145;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001219 public static final int ON_NEW_ACTIVITY_OPTIONS = 146;
Craig Mautneree2e45a2014-06-27 12:10:03 -07001220 public static final int STOP_MEDIA_PLAYING = 147;
1221 public static final int BACKGROUND_MEDIA_PLAYING_CHANGED = 148;
Craig Mautner8746a472014-07-24 15:12:54 -07001222 public static final int ENTER_ANIMATION_COMPLETE = 149;
Narayan Kamathccb2a082013-12-19 14:49:36 +00001223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001225 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 switch (code) {
1227 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1228 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1229 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1230 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1231 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1232 case SHOW_WINDOW: return "SHOW_WINDOW";
1233 case HIDE_WINDOW: return "HIDE_WINDOW";
1234 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1235 case SEND_RESULT: return "SEND_RESULT";
1236 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1237 case BIND_APPLICATION: return "BIND_APPLICATION";
1238 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1239 case NEW_INTENT: return "NEW_INTENT";
1240 case RECEIVER: return "RECEIVER";
1241 case CREATE_SERVICE: return "CREATE_SERVICE";
1242 case SERVICE_ARGS: return "SERVICE_ARGS";
1243 case STOP_SERVICE: return "STOP_SERVICE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1245 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1246 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1247 case BIND_SERVICE: return "BIND_SERVICE";
1248 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1249 case DUMP_SERVICE: return "DUMP_SERVICE";
1250 case LOW_MEMORY: return "LOW_MEMORY";
1251 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1252 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001253 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001254 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1255 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001256 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001257 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001258 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001259 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001260 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001261 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001262 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001263 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001264 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001265 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001266 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001267 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001268 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
Adam Skorydfc7fd72013-08-05 19:23:41 -07001269 case REQUEST_ASSIST_CONTEXT_EXTRAS: return "REQUEST_ASSIST_CONTEXT_EXTRAS";
Craig Mautner5eda9b32013-07-02 11:58:16 -07001270 case TRANSLUCENT_CONVERSION_COMPLETE: return "TRANSLUCENT_CONVERSION_COMPLETE";
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001271 case INSTALL_PROVIDER: return "INSTALL_PROVIDER";
Craig Mautnereb8abf72014-07-02 15:04:09 -07001272 case ON_NEW_ACTIVITY_OPTIONS: return "ON_NEW_ACTIVITY_OPTIONS";
Craig Mautneree2e45a2014-06-27 12:10:03 -07001273 case STOP_MEDIA_PLAYING: return "STOP_MEDIA_PLAYING";
1274 case BACKGROUND_MEDIA_PLAYING_CHANGED: return "BACKGROUND_MEDIA_PLAYING_CHANGED";
Craig Mautner8746a472014-07-24 15:12:54 -07001275 case ENTER_ANIMATION_COMPLETE: return "ENTER_ANIMATION_COMPLETE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 }
1277 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001278 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 }
1280 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001281 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 switch (msg.what) {
1283 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001284 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Adam Powellcfbe9be2013-11-06 14:58:58 -08001285 final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286
1287 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001288 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001289 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001290 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 } break;
1292 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001293 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001294 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001295 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001296 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 } break;
1298 case PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001299 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001301 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001302 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 break;
1304 case PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001305 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001307 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 break;
1309 case STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001310 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001312 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 break;
1314 case STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001315 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001317 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 break;
1319 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001320 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001322 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 break;
1324 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001325 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001327 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 break;
1329 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001330 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
Craig Mautner233ceee2014-05-09 17:05:11 -07001331 handleResumeActivity((IBinder) msg.obj, true, msg.arg1 != 0, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001332 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 break;
1334 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001335 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001337 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 break;
1339 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001340 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1342 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001343 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 break;
1345 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001346 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 AppBindData data = (AppBindData)msg.obj;
1348 handleBindApplication(data);
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 EXIT_APPLICATION:
1352 if (mInitialApplication != null) {
1353 mInitialApplication.onTerminate();
1354 }
1355 Looper.myLooper().quit();
1356 break;
1357 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001358 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001360 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 break;
1362 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001363 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001365 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001366 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 break;
1368 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001369 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001371 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 break;
1373 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001374 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001376 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 break;
1378 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001379 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001381 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 break;
1383 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001384 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001386 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 break;
1388 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001389 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001391 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001392 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001395 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001396 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001397 handleConfigurationChanged((Configuration)msg.obj, null);
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 CLEAN_UP_CONTEXT:
1401 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1402 cci.context.performFinalCleanup(cci.who, cci.what);
1403 break;
1404 case GC_WHEN_IDLE:
1405 scheduleGcIdler();
1406 break;
1407 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001408 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 break;
1410 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001411 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001413 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 break;
1415 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001416 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001418 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001420 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001421 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001422 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001423 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001424 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001425 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001426 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001427 break;
1428 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001429 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001430 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001431 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001432 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001433 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001434 Process.killProcess(Process.myPid());
1435 break;
1436 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001437 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001438 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001439 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001440 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001441 case ENABLE_JIT:
1442 ensureJitEnabled();
1443 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001444 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001445 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001446 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001447 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001448 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001449 case SCHEDULE_CRASH:
1450 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001451 case DUMP_HEAP:
1452 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1453 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001454 case DUMP_ACTIVITY:
1455 handleDumpActivity((DumpComponentInfo)msg.obj);
1456 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001457 case DUMP_PROVIDER:
1458 handleDumpProvider((DumpComponentInfo)msg.obj);
1459 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001460 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001461 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001462 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001463 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001464 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001465 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001466 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001467 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001468 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001469 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001470 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1471 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001472 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001473 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001474 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001475 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001476 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001477 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001478 case UNSTABLE_PROVIDER_DIED:
1479 handleUnstableProviderDied((IBinder)msg.obj, false);
1480 break;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001481 case REQUEST_ASSIST_CONTEXT_EXTRAS:
1482 handleRequestAssistContextExtras((RequestAssistContextExtras)msg.obj);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001483 break;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001484 case TRANSLUCENT_CONVERSION_COMPLETE:
1485 handleTranslucentConversionComplete((IBinder)msg.obj, msg.arg1 == 1);
1486 break;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001487 case INSTALL_PROVIDER:
1488 handleInstallProvider((ProviderInfo) msg.obj);
1489 break;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001490 case ON_NEW_ACTIVITY_OPTIONS:
1491 Pair<IBinder, ActivityOptions> pair = (Pair<IBinder, ActivityOptions>) msg.obj;
1492 onNewActivityOptions(pair.first, pair.second);
Dake Gu7ef70b02014-07-08 18:37:12 -07001493 break;
Craig Mautneree2e45a2014-06-27 12:10:03 -07001494 case STOP_MEDIA_PLAYING:
1495 handleStopMediaPlaying((IBinder) msg.obj);
1496 break;
1497 case BACKGROUND_MEDIA_PLAYING_CHANGED:
1498 handleOnBackgroundMediaPlayingChanged((IBinder) msg.obj, msg.arg1 > 0);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001499 break;
Craig Mautner8746a472014-07-24 15:12:54 -07001500 case ENTER_ANIMATION_COMPLETE:
1501 handleEnterAnimationComplete((IBinder) msg.obj);
1502 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001504 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 }
Bob Leee5408332009-09-04 18:31:17 -07001506
Brian Carlstromed7e0072011-03-24 13:27:57 -07001507 private void maybeSnapshot() {
1508 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001509 // convert the *private* ActivityThread.PackageInfo to *public* known
1510 // android.content.pm.PackageInfo
1511 String packageName = mBoundApplication.info.mPackageName;
1512 android.content.pm.PackageInfo packageInfo = null;
1513 try {
1514 Context context = getSystemContext();
1515 if(context == null) {
1516 Log.e(TAG, "cannot get a valid context");
1517 return;
1518 }
1519 PackageManager pm = context.getPackageManager();
1520 if(pm == null) {
1521 Log.e(TAG, "cannot get a valid PackageManager");
1522 return;
1523 }
1524 packageInfo = pm.getPackageInfo(
1525 packageName, PackageManager.GET_ACTIVITIES);
1526 } catch (NameNotFoundException e) {
1527 Log.e(TAG, "cannot get package info for " + packageName, e);
1528 }
1529 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001530 }
1531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 }
1533
Romain Guy65b345f2011-07-27 18:51:50 -07001534 private class Idler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001535 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001537 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001538 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001539 if (mBoundApplication != null && mProfiler.profileFd != null
1540 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001541 stopProfiling = true;
1542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 if (a != null) {
1544 mNewActivities = null;
1545 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001546 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001548 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 TAG, "Reporting idle of " + a +
1550 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001551 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 if (a.activity != null && !a.activity.mFinished) {
1553 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001554 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001555 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001557 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 }
1559 }
1560 prev = a;
1561 a = a.nextIdle;
1562 prev.nextIdle = null;
1563 } while (a != null);
1564 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001565 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001566 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001567 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001568 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 return false;
1570 }
1571 }
1572
1573 final class GcIdler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001574 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 public final boolean queueIdle() {
1576 doGcIfNeeded();
1577 return false;
1578 }
1579 }
1580
Romain Guy65b345f2011-07-27 18:51:50 -07001581 public static ActivityThread currentActivityThread() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08001582 return sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001583 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584
Romain Guy65b345f2011-07-27 18:51:50 -07001585 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001586 ActivityThread am = currentActivityThread();
1587 return (am != null && am.mBoundApplication != null)
Dianne Hackbornb57a50b2013-04-11 17:29:32 -07001588 ? am.mBoundApplication.appInfo.packageName : null;
1589 }
1590
1591 public static String currentProcessName() {
1592 ActivityThread am = currentActivityThread();
1593 return (am != null && am.mBoundApplication != null)
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001594 ? am.mBoundApplication.processName : null;
1595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596
Romain Guy65b345f2011-07-27 18:51:50 -07001597 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001598 ActivityThread am = currentActivityThread();
1599 return am != null ? am.mInitialApplication : null;
1600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001602 public static IPackageManager getPackageManager() {
1603 if (sPackageManager != null) {
1604 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1605 return sPackageManager;
1606 }
1607 IBinder b = ServiceManager.getService("package");
1608 //Slog.v("PackageManager", "default service binder = " + b);
1609 sPackageManager = IPackageManager.Stub.asInterface(b);
1610 //Slog.v("PackageManager", "default service = " + sPackageManager);
1611 return sPackageManager;
1612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613
Romain Guy65b345f2011-07-27 18:51:50 -07001614 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001615 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1616 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001617 if (config == null) {
1618 return null;
1619 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001620 if (!compat.supportsScreen()) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001621 mMainThreadConfig.setTo(config);
1622 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001623 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001624 }
1625 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001628 /**
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001629 * Creates the top level resources for the given package.
1630 */
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001631 Resources getTopLevelResources(String resDir, String[] splitResDirs, String[] overlayDirs,
1632 String[] libDirs, int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001633 LoadedApk pkgInfo) {
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001634 return mResourcesManager.getTopLevelResources(resDir, splitResDirs, overlayDirs, libDirs,
1635 displayId, overrideConfiguration, pkgInfo.getCompatibilityInfo(), null);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001636 }
1637
1638 final Handler getHandler() {
1639 return mH;
1640 }
1641
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001642 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1643 int flags) {
Amith Yamasani98edc952012-09-25 14:09:27 -07001644 return getPackageInfo(packageName, compatInfo, flags, UserHandle.myUserId());
1645 }
1646
1647 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1648 int flags, int userId) {
Craig Mautner88c05892013-06-28 09:47:45 -07001649 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001650 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1652 ref = mPackages.get(packageName);
1653 } else {
1654 ref = mResourcePackages.get(packageName);
1655 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001656 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001657 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001658 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1659 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 if (packageInfo != null && (packageInfo.mResources == null
1661 || packageInfo.mResources.getAssets().isUpToDate())) {
1662 if (packageInfo.isSecurityViolation()
1663 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1664 throw new SecurityException(
1665 "Requesting code from " + packageName
1666 + " to be run in process "
1667 + mBoundApplication.processName
1668 + "/" + mBoundApplication.appInfo.uid);
1669 }
1670 return packageInfo;
1671 }
1672 }
1673
1674 ApplicationInfo ai = null;
1675 try {
1676 ai = getPackageManager().getApplicationInfo(packageName,
Amith Yamasani98edc952012-09-25 14:09:27 -07001677 PackageManager.GET_SHARED_LIBRARY_FILES, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001679 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 }
1681
1682 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001683 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 }
1685
1686 return null;
1687 }
1688
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001689 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1690 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1692 boolean securityViolation = includeCode && ai.uid != 0
1693 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001694 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001695 : true);
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001696 boolean registerPackage = includeCode && (flags&Context.CONTEXT_REGISTER_PACKAGE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1698 |Context.CONTEXT_IGNORE_SECURITY))
1699 == Context.CONTEXT_INCLUDE_CODE) {
1700 if (securityViolation) {
1701 String msg = "Requesting code from " + ai.packageName
1702 + " (with uid " + ai.uid + ")";
1703 if (mBoundApplication != null) {
1704 msg = msg + " to be run in process "
1705 + mBoundApplication.processName + " (with uid "
1706 + mBoundApplication.appInfo.uid + ")";
1707 }
1708 throw new SecurityException(msg);
1709 }
1710 }
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001711 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode,
1712 registerPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 }
1714
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001715 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1716 CompatibilityInfo compatInfo) {
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001717 return getPackageInfo(ai, compatInfo, null, false, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
1719
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001720 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07001721 synchronized (mResourcesManager) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001722 WeakReference<LoadedApk> ref;
1723 if (includeCode) {
1724 ref = mPackages.get(packageName);
1725 } else {
1726 ref = mResourcePackages.get(packageName);
1727 }
1728 return ref != null ? ref.get() : null;
1729 }
1730 }
1731
Romain Guy65b345f2011-07-27 18:51:50 -07001732 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001733 ClassLoader baseLoader, boolean securityViolation, boolean includeCode,
1734 boolean registerPackage) {
Craig Mautner88c05892013-06-28 09:47:45 -07001735 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001736 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 if (includeCode) {
1738 ref = mPackages.get(aInfo.packageName);
1739 } else {
1740 ref = mResourcePackages.get(aInfo.packageName);
1741 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001742 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 if (packageInfo == null || (packageInfo.mResources != null
1744 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001745 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 : "Loading resource-only package ") + aInfo.packageName
1747 + " (in " + (mBoundApplication != null
1748 ? mBoundApplication.processName : null)
1749 + ")");
1750 packageInfo =
Jeff Browndefd4a62014-03-10 21:24:37 -07001751 new LoadedApk(this, aInfo, compatInfo, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 securityViolation, includeCode &&
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001753 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0, registerPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 if (includeCode) {
1755 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001756 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 } else {
1758 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001759 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 }
1761 }
1762 return packageInfo;
1763 }
1764 }
1765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 ActivityThread() {
Craig Mautner88c05892013-06-28 09:47:45 -07001767 mResourcesManager = ResourcesManager.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 }
1769
1770 public ApplicationThread getApplicationThread()
1771 {
1772 return mAppThread;
1773 }
1774
1775 public Instrumentation getInstrumentation()
1776 {
1777 return mInstrumentation;
1778 }
1779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001781 return mProfiler != null && mProfiler.profileFile != null
1782 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 }
1784
1785 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001786 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 }
1788
1789 public Looper getLooper() {
1790 return mLooper;
1791 }
1792
1793 public Application getApplication() {
1794 return mInitialApplication;
1795 }
Bob Leee5408332009-09-04 18:31:17 -07001796
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001797 public String getProcessName() {
1798 return mBoundApplication.processName;
1799 }
Bob Leee5408332009-09-04 18:31:17 -07001800
Dianne Hackborn21556372010-02-04 16:34:40 -08001801 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 synchronized (this) {
1803 if (mSystemContext == null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001804 mSystemContext = ContextImpl.createSystemContext(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 }
Jeff Browndefd4a62014-03-10 21:24:37 -07001806 return mSystemContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 }
1809
Mike Cleron432b7132009-09-24 15:28:29 -07001810 public void installSystemApplicationInfo(ApplicationInfo info) {
1811 synchronized (this) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001812 getSystemContext().installSystemApplicationInfo(info);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001813
1814 // give ourselves a default profiler
1815 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001816 }
1817 }
1818
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001819 void ensureJitEnabled() {
1820 if (!mJitEnabled) {
1821 mJitEnabled = true;
1822 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1823 }
1824 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 void scheduleGcIdler() {
1827 if (!mGcIdlerScheduled) {
1828 mGcIdlerScheduled = true;
1829 Looper.myQueue().addIdleHandler(mGcIdler);
1830 }
1831 mH.removeMessages(H.GC_WHEN_IDLE);
1832 }
1833
1834 void unscheduleGcIdler() {
1835 if (mGcIdlerScheduled) {
1836 mGcIdlerScheduled = false;
1837 Looper.myQueue().removeIdleHandler(mGcIdler);
1838 }
1839 mH.removeMessages(H.GC_WHEN_IDLE);
1840 }
1841
1842 void doGcIfNeeded() {
1843 mGcIdlerScheduled = false;
1844 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001845 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 // + "m now=" + now);
1847 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001848 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 BinderInternal.forceGc("bg");
1850 }
1851 }
1852
Dianne Hackborne77187d2013-10-25 16:32:41 -07001853 private static final String HEAP_FULL_COLUMN
1854 = "%13s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s";
1855 private static final String HEAP_COLUMN
1856 = "%13s %8s %8s %8s %8s %8s %8s %8s";
1857
1858 // Formatting for checkin service - update version if row format changes
1859 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 3;
1860
1861 static void printRow(PrintWriter pw, String format, Object...objs) {
1862 pw.println(String.format(format, objs));
1863 }
1864
1865 public static void dumpMemInfoTable(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
1866 boolean dumpFullInfo, boolean dumpDalvik, int pid, String processName,
1867 long nativeMax, long nativeAllocated, long nativeFree,
1868 long dalvikMax, long dalvikAllocated, long dalvikFree) {
1869
1870 // For checkin, we print one long comma-separated list of values
1871 if (checkin) {
1872 // NOTE: if you change anything significant below, also consider changing
1873 // ACTIVITY_THREAD_CHECKIN_VERSION.
1874
1875 // Header
1876 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
1877 pw.print(pid); pw.print(',');
1878 pw.print(processName); pw.print(',');
1879
1880 // Heap info - max
1881 pw.print(nativeMax); pw.print(',');
1882 pw.print(dalvikMax); pw.print(',');
1883 pw.print("N/A,");
1884 pw.print(nativeMax + dalvikMax); pw.print(',');
1885
1886 // Heap info - allocated
1887 pw.print(nativeAllocated); pw.print(',');
1888 pw.print(dalvikAllocated); pw.print(',');
1889 pw.print("N/A,");
1890 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
1891
1892 // Heap info - free
1893 pw.print(nativeFree); pw.print(',');
1894 pw.print(dalvikFree); pw.print(',');
1895 pw.print("N/A,");
1896 pw.print(nativeFree + dalvikFree); pw.print(',');
1897
1898 // Heap info - proportional set size
1899 pw.print(memInfo.nativePss); pw.print(',');
1900 pw.print(memInfo.dalvikPss); pw.print(',');
1901 pw.print(memInfo.otherPss); pw.print(',');
1902 pw.print(memInfo.getTotalPss()); pw.print(',');
1903
1904 // Heap info - swappable set size
1905 pw.print(memInfo.nativeSwappablePss); pw.print(',');
1906 pw.print(memInfo.dalvikSwappablePss); pw.print(',');
1907 pw.print(memInfo.otherSwappablePss); pw.print(',');
1908 pw.print(memInfo.getTotalSwappablePss()); pw.print(',');
1909
1910 // Heap info - shared dirty
1911 pw.print(memInfo.nativeSharedDirty); pw.print(',');
1912 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
1913 pw.print(memInfo.otherSharedDirty); pw.print(',');
1914 pw.print(memInfo.getTotalSharedDirty()); pw.print(',');
1915
1916 // Heap info - shared clean
1917 pw.print(memInfo.nativeSharedClean); pw.print(',');
1918 pw.print(memInfo.dalvikSharedClean); pw.print(',');
1919 pw.print(memInfo.otherSharedClean); pw.print(',');
1920 pw.print(memInfo.getTotalSharedClean()); pw.print(',');
1921
1922 // Heap info - private Dirty
1923 pw.print(memInfo.nativePrivateDirty); pw.print(',');
1924 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
1925 pw.print(memInfo.otherPrivateDirty); pw.print(',');
1926 pw.print(memInfo.getTotalPrivateDirty()); pw.print(',');
1927
1928 // Heap info - private Clean
1929 pw.print(memInfo.nativePrivateClean); pw.print(',');
1930 pw.print(memInfo.dalvikPrivateClean); pw.print(',');
1931 pw.print(memInfo.otherPrivateClean); pw.print(',');
1932 pw.print(memInfo.getTotalPrivateClean()); pw.print(',');
1933
1934 // Heap info - other areas
1935 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
1936 pw.print(Debug.MemoryInfo.getOtherLabel(i)); pw.print(',');
1937 pw.print(memInfo.getOtherPss(i)); pw.print(',');
1938 pw.print(memInfo.getOtherSwappablePss(i)); pw.print(',');
1939 pw.print(memInfo.getOtherSharedDirty(i)); pw.print(',');
1940 pw.print(memInfo.getOtherSharedClean(i)); pw.print(',');
1941 pw.print(memInfo.getOtherPrivateDirty(i)); pw.print(',');
1942 pw.print(memInfo.getOtherPrivateClean(i)); pw.print(',');
1943 }
1944 return;
1945 }
1946
1947 // otherwise, show human-readable format
1948 if (dumpFullInfo) {
1949 printRow(pw, HEAP_FULL_COLUMN, "", "Pss", "Pss", "Shared", "Private",
1950 "Shared", "Private", "Swapped", "Heap", "Heap", "Heap");
1951 printRow(pw, HEAP_FULL_COLUMN, "", "Total", "Clean", "Dirty", "Dirty",
1952 "Clean", "Clean", "Dirty", "Size", "Alloc", "Free");
1953 printRow(pw, HEAP_FULL_COLUMN, "", "------", "------", "------", "------",
1954 "------", "------", "------", "------", "------", "------");
1955 printRow(pw, HEAP_FULL_COLUMN, "Native Heap", memInfo.nativePss,
1956 memInfo.nativeSwappablePss, memInfo.nativeSharedDirty,
1957 memInfo.nativePrivateDirty, memInfo.nativeSharedClean,
1958 memInfo.nativePrivateClean, memInfo.nativeSwappedOut,
1959 nativeMax, nativeAllocated, nativeFree);
1960 printRow(pw, HEAP_FULL_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
1961 memInfo.dalvikSwappablePss, memInfo.dalvikSharedDirty,
1962 memInfo.dalvikPrivateDirty, memInfo.dalvikSharedClean,
1963 memInfo.dalvikPrivateClean, memInfo.dalvikSwappedOut,
1964 dalvikMax, dalvikAllocated, dalvikFree);
1965 } else {
1966 printRow(pw, HEAP_COLUMN, "", "Pss", "Private",
1967 "Private", "Swapped", "Heap", "Heap", "Heap");
1968 printRow(pw, HEAP_COLUMN, "", "Total", "Dirty",
1969 "Clean", "Dirty", "Size", "Alloc", "Free");
1970 printRow(pw, HEAP_COLUMN, "", "------", "------", "------",
1971 "------", "------", "------", "------", "------");
1972 printRow(pw, HEAP_COLUMN, "Native Heap", memInfo.nativePss,
1973 memInfo.nativePrivateDirty,
1974 memInfo.nativePrivateClean, memInfo.nativeSwappedOut,
1975 nativeMax, nativeAllocated, nativeFree);
1976 printRow(pw, HEAP_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
1977 memInfo.dalvikPrivateDirty,
1978 memInfo.dalvikPrivateClean, memInfo.dalvikSwappedOut,
1979 dalvikMax, dalvikAllocated, dalvikFree);
1980 }
1981
1982 int otherPss = memInfo.otherPss;
1983 int otherSwappablePss = memInfo.otherSwappablePss;
1984 int otherSharedDirty = memInfo.otherSharedDirty;
1985 int otherPrivateDirty = memInfo.otherPrivateDirty;
1986 int otherSharedClean = memInfo.otherSharedClean;
1987 int otherPrivateClean = memInfo.otherPrivateClean;
1988 int otherSwappedOut = memInfo.otherSwappedOut;
1989
1990 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
1991 final int myPss = memInfo.getOtherPss(i);
1992 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
1993 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
1994 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
1995 final int mySharedClean = memInfo.getOtherSharedClean(i);
1996 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
1997 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
1998 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
1999 || mySharedClean != 0 || myPrivateClean != 0 || mySwappedOut != 0) {
2000 if (dumpFullInfo) {
2001 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2002 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
2003 mySharedClean, myPrivateClean, mySwappedOut, "", "", "");
2004 } else {
2005 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2006 myPss, myPrivateDirty,
2007 myPrivateClean, mySwappedOut, "", "", "");
2008 }
2009 otherPss -= myPss;
2010 otherSwappablePss -= mySwappablePss;
2011 otherSharedDirty -= mySharedDirty;
2012 otherPrivateDirty -= myPrivateDirty;
2013 otherSharedClean -= mySharedClean;
2014 otherPrivateClean -= myPrivateClean;
2015 otherSwappedOut -= mySwappedOut;
2016 }
2017 }
2018
2019 if (dumpFullInfo) {
2020 printRow(pw, HEAP_FULL_COLUMN, "Unknown", otherPss, otherSwappablePss,
2021 otherSharedDirty, otherPrivateDirty, otherSharedClean, otherPrivateClean,
2022 otherSwappedOut, "", "", "");
2023 printRow(pw, HEAP_FULL_COLUMN, "TOTAL", memInfo.getTotalPss(),
2024 memInfo.getTotalSwappablePss(),
2025 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
2026 memInfo.getTotalSharedClean(), memInfo.getTotalPrivateClean(),
2027 memInfo.getTotalSwappedOut(), nativeMax+dalvikMax,
2028 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
2029 } else {
2030 printRow(pw, HEAP_COLUMN, "Unknown", otherPss,
2031 otherPrivateDirty, otherPrivateClean, otherSwappedOut,
2032 "", "", "");
2033 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
2034 memInfo.getTotalPrivateDirty(),
2035 memInfo.getTotalPrivateClean(),
2036 memInfo.getTotalSwappedOut(),
2037 nativeMax+dalvikMax,
2038 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
2039 }
2040
2041 if (dumpDalvik) {
2042 pw.println(" ");
2043 pw.println(" Dalvik Details");
2044
2045 for (int i=Debug.MemoryInfo.NUM_OTHER_STATS;
2046 i<Debug.MemoryInfo.NUM_OTHER_STATS + Debug.MemoryInfo.NUM_DVK_STATS; i++) {
2047 final int myPss = memInfo.getOtherPss(i);
2048 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2049 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2050 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2051 final int mySharedClean = memInfo.getOtherSharedClean(i);
2052 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2053 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
2054 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
2055 || mySharedClean != 0 || myPrivateClean != 0) {
2056 if (dumpFullInfo) {
2057 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2058 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
2059 mySharedClean, myPrivateClean, mySwappedOut, "", "", "");
2060 } else {
2061 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2062 myPss, myPrivateDirty,
2063 myPrivateClean, mySwappedOut, "", "", "");
2064 }
2065 }
2066 }
2067 }
2068 }
2069
Jeff Hamilton52d32032011-01-08 15:31:26 -06002070 public void registerOnActivityPausedListener(Activity activity,
2071 OnActivityPausedListener listener) {
2072 synchronized (mOnPauseListeners) {
2073 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2074 if (list == null) {
2075 list = new ArrayList<OnActivityPausedListener>();
2076 mOnPauseListeners.put(activity, list);
2077 }
2078 list.add(listener);
2079 }
2080 }
2081
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08002082 public void unregisterOnActivityPausedListener(Activity activity,
2083 OnActivityPausedListener listener) {
2084 synchronized (mOnPauseListeners) {
2085 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2086 if (list != null) {
2087 list.remove(listener);
2088 }
2089 }
2090 }
2091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 public final ActivityInfo resolveActivityInfo(Intent intent) {
2093 ActivityInfo aInfo = intent.resolveActivityInfo(
2094 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
2095 if (aInfo == null) {
2096 // Throw an exception.
2097 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07002098 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 }
2100 return aInfo;
2101 }
Bob Leee5408332009-09-04 18:31:17 -07002102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002105 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002106 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002108 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 r.intent = intent;
2110 r.state = state;
2111 r.parent = parent;
2112 r.embeddedID = id;
2113 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002114 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 if (localLOGV) {
2116 ComponentName compname = intent.getComponent();
2117 String name;
2118 if (compname != null) {
2119 name = compname.toShortString();
2120 } else {
2121 name = "(Intent " + intent + ").getComponent() returned null";
2122 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002123 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 + ", comp=" + name
2125 + ", token=" + token);
2126 }
Craig Mautner233ceee2014-05-09 17:05:11 -07002127 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 }
2129
2130 public final Activity getActivity(IBinder token) {
2131 return mActivities.get(token).activity;
2132 }
2133
2134 public final void sendActivityResult(
2135 IBinder token, String id, int requestCode,
2136 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002137 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07002138 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2140 list.add(new ResultInfo(id, requestCode, resultCode, data));
2141 mAppThread.scheduleSendResult(token, list);
2142 }
2143
Jeff Brown9ef09972013-10-15 20:49:59 -07002144 private void sendMessage(int what, Object obj) {
2145 sendMessage(what, obj, 0, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 }
2147
Jeff Brown9ef09972013-10-15 20:49:59 -07002148 private void sendMessage(int what, Object obj, int arg1) {
2149 sendMessage(what, obj, arg1, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 }
2151
Jeff Brown9ef09972013-10-15 20:49:59 -07002152 private void sendMessage(int what, Object obj, int arg1, int arg2) {
2153 sendMessage(what, obj, arg1, arg2, false);
2154 }
2155
2156 private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
2157 if (DEBUG_MESSAGES) Slog.v(
2158 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2159 + ": " + arg1 + " / " + obj);
2160 Message msg = Message.obtain();
2161 msg.what = what;
2162 msg.obj = obj;
2163 msg.arg1 = arg1;
2164 msg.arg2 = arg2;
2165 if (async) {
2166 msg.setAsynchronous(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 }
Jeff Brown9ef09972013-10-15 20:49:59 -07002168 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 }
2170
Dianne Hackborn21556372010-02-04 16:34:40 -08002171 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 String what) {
2173 ContextCleanupInfo cci = new ContextCleanupInfo();
2174 cci.context = context;
2175 cci.who = who;
2176 cci.what = what;
Jeff Brown9ef09972013-10-15 20:49:59 -07002177 sendMessage(H.CLEAN_UP_CONTEXT, cci);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 }
2179
Craig Mautner233ceee2014-05-09 17:05:11 -07002180 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2182
2183 ActivityInfo aInfo = r.activityInfo;
2184 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002185 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 Context.CONTEXT_INCLUDE_CODE);
2187 }
Bob Leee5408332009-09-04 18:31:17 -07002188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 ComponentName component = r.intent.getComponent();
2190 if (component == null) {
2191 component = r.intent.resolveActivity(
2192 mInitialApplication.getPackageManager());
2193 r.intent.setComponent(component);
2194 }
2195
2196 if (r.activityInfo.targetActivity != null) {
2197 component = new ComponentName(r.activityInfo.packageName,
2198 r.activityInfo.targetActivity);
2199 }
2200
2201 Activity activity = null;
2202 try {
2203 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2204 activity = mInstrumentation.newActivity(
2205 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002206 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 r.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002208 r.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 if (r.state != null) {
2210 r.state.setClassLoader(cl);
2211 }
2212 } catch (Exception e) {
2213 if (!mInstrumentation.onException(activity, e)) {
2214 throw new RuntimeException(
2215 "Unable to instantiate activity " + component
2216 + ": " + e.toString(), e);
2217 }
2218 }
2219
2220 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002221 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002222
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002223 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2224 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 TAG, r + ": app=" + app
2226 + ", appName=" + app.getPackageName()
2227 + ", pkg=" + r.packageInfo.getPackageName()
2228 + ", comp=" + r.intent.getComponent().toShortString()
2229 + ", dir=" + r.packageInfo.getAppDir());
2230
2231 if (activity != null) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002232 Context appContext = createBaseContextForActivity(r, activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002234 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002235 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002236 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002237 activity.attach(appContext, this, getInstrumentation(), r.token,
2238 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Craig Mautner233ceee2014-05-09 17:05:11 -07002239 r.embeddedID, r.lastNonConfigurationInstances, config,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002240 r.voiceInteractor);
Bob Leee5408332009-09-04 18:31:17 -07002241
Christopher Tateb70f3df2009-04-07 16:07:59 -07002242 if (customIntent != null) {
2243 activity.mIntent = customIntent;
2244 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002245 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 activity.mStartedActivity = false;
2247 int theme = r.activityInfo.getThemeResource();
2248 if (theme != 0) {
2249 activity.setTheme(theme);
2250 }
2251
2252 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002253 if (r.isPersistable()) {
2254 mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
2255 } else {
2256 mInstrumentation.callActivityOnCreate(activity, r.state);
2257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 if (!activity.mCalled) {
2259 throw new SuperNotCalledException(
2260 "Activity " + r.intent.getComponent().toShortString() +
2261 " did not call through to super.onCreate()");
2262 }
2263 r.activity = activity;
2264 r.stopped = true;
2265 if (!r.activity.mFinished) {
2266 activity.performStart();
2267 r.stopped = false;
2268 }
2269 if (!r.activity.mFinished) {
Craig Mautnera0026042014-04-23 11:45:37 -07002270 if (r.isPersistable()) {
2271 if (r.state != null || r.persistentState != null) {
2272 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,
2273 r.persistentState);
2274 }
2275 } else if (r.state != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2277 }
2278 }
2279 if (!r.activity.mFinished) {
2280 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002281 if (r.isPersistable()) {
2282 mInstrumentation.callActivityOnPostCreate(activity, r.state,
2283 r.persistentState);
2284 } else {
2285 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 if (!activity.mCalled) {
2288 throw new SuperNotCalledException(
2289 "Activity " + r.intent.getComponent().toShortString() +
2290 " did not call through to super.onPostCreate()");
2291 }
2292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 }
2294 r.paused = true;
2295
2296 mActivities.put(r.token, r);
2297
2298 } catch (SuperNotCalledException e) {
2299 throw e;
2300
2301 } catch (Exception e) {
2302 if (!mInstrumentation.onException(activity, e)) {
2303 throw new RuntimeException(
2304 "Unable to start activity " + component
2305 + ": " + e.toString(), e);
2306 }
2307 }
2308
2309 return activity;
2310 }
2311
Jeff Brownefd43bd2012-09-21 17:02:35 -07002312 private Context createBaseContextForActivity(ActivityClientRecord r,
2313 final Activity activity) {
Jeff Browndefd4a62014-03-10 21:24:37 -07002314 ContextImpl appContext = ContextImpl.createActivityContext(this, r.packageInfo, r.token);
Jeff Brownefd43bd2012-09-21 17:02:35 -07002315 appContext.setOuterContext(activity);
Craig Mautnere0a38842013-12-16 16:14:02 -08002316 Context baseContext = appContext;
2317
2318 final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
2319 try {
2320 IActivityContainer container =
2321 ActivityManagerNative.getDefault().getEnclosingActivityContainer(r.token);
Craig Mautnerbd503a42014-01-10 10:16:43 -08002322 final int displayId =
2323 container == null ? Display.DEFAULT_DISPLAY : container.getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -08002324 if (displayId > Display.DEFAULT_DISPLAY) {
2325 Display display = dm.getRealDisplay(displayId, r.token);
2326 baseContext = appContext.createDisplayContext(display);
2327 }
2328 } catch (RemoteException e) {
2329 }
Jeff Brownefd43bd2012-09-21 17:02:35 -07002330
2331 // For debugging purposes, if the activity's package name contains the value of
2332 // the "debug.use-second-display" system property as a substring, then show
2333 // its content on a secondary display if there is one.
Jeff Brownefd43bd2012-09-21 17:02:35 -07002334 String pkgName = SystemProperties.get("debug.second-display.pkg");
2335 if (pkgName != null && !pkgName.isEmpty()
2336 && r.packageInfo.mPackageName.contains(pkgName)) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002337 for (int displayId : dm.getDisplayIds()) {
2338 if (displayId != Display.DEFAULT_DISPLAY) {
Craig Mautner48d0d182013-06-11 07:53:06 -07002339 Display display = dm.getRealDisplay(displayId, r.token);
Jeff Brownefd43bd2012-09-21 17:02:35 -07002340 baseContext = appContext.createDisplayContext(display);
2341 break;
2342 }
2343 }
2344 }
2345 return baseContext;
2346 }
2347
Romain Guy65b345f2011-07-27 18:51:50 -07002348 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 // If we are getting ready to gc after going to the background, well
2350 // we are back active so skip it.
2351 unscheduleGcIdler();
2352
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002353 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002354 mProfiler.setProfiler(r.profileFile, r.profileFd);
2355 mProfiler.startProfiling();
2356 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002357 }
2358
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002359 // Make sure we are running with the most recent config.
2360 handleConfigurationChanged(null, null);
2361
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002362 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 TAG, "Handling launch of " + r);
Adam Powellcfbe9be2013-11-06 14:58:58 -08002364
Craig Mautner233ceee2014-05-09 17:05:11 -07002365 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366
2367 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002368 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002369 Bundle oldState = r.state;
Craig Mautner233ceee2014-05-09 17:05:11 -07002370 handleResumeActivity(r.token, false, r.isForward,
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002371 !r.activity.mFinished && !r.startsNotResumed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372
2373 if (!r.activity.mFinished && r.startsNotResumed) {
2374 // The activity manager actually wants this one to start out
2375 // paused, because it needs to be visible but isn't in the
2376 // foreground. We accomplish this by going through the
2377 // normal startup (because activities expect to go through
2378 // onResume() the first time they run, before their window
2379 // is displayed), and then pausing it. However, in this case
2380 // we do -not- need to do the full pause cycle (of freezing
2381 // and such) because the activity manager assumes it can just
2382 // retain the current state it has.
2383 try {
2384 r.activity.mCalled = false;
2385 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002386 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002387 // we need to be created again. But we only do this
2388 // for pre-Honeycomb apps, which always save their state
2389 // when pausing, so we can not have them save their state
2390 // when restarting from a paused state. For HC and later,
2391 // we want to (and can) let the state be saved as the normal
2392 // part of stopping the activity.
2393 if (r.isPreHoneycomb()) {
2394 r.state = oldState;
2395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 if (!r.activity.mCalled) {
2397 throw new SuperNotCalledException(
2398 "Activity " + r.intent.getComponent().toShortString() +
2399 " did not call through to super.onPause()");
2400 }
2401
2402 } catch (SuperNotCalledException e) {
2403 throw e;
2404
2405 } catch (Exception e) {
2406 if (!mInstrumentation.onException(r.activity, e)) {
2407 throw new RuntimeException(
2408 "Unable to pause activity "
2409 + r.intent.getComponent().toShortString()
2410 + ": " + e.toString(), e);
2411 }
2412 }
2413 r.paused = true;
2414 }
2415 } else {
2416 // If there was an error, for any reason, tell the activity
2417 // manager to stop us.
2418 try {
2419 ActivityManagerNative.getDefault()
Winson Chung3b3f4642014-04-22 10:08:18 -07002420 .finishActivity(r.token, Activity.RESULT_CANCELED, null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002422 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 }
2424 }
2425 }
2426
Romain Guy65b345f2011-07-27 18:51:50 -07002427 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 List<Intent> intents) {
2429 final int N = intents.size();
2430 for (int i=0; i<N; i++) {
2431 Intent intent = intents.get(i);
2432 intent.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002433 intent.prepareToEnterProcess();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002434 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2436 }
2437 }
2438
2439 public final void performNewIntents(IBinder token,
2440 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002441 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 if (r != null) {
2443 final boolean resumed = !r.paused;
2444 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002445 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 mInstrumentation.callActivityOnPause(r.activity);
2447 }
2448 deliverNewIntents(r, intents);
2449 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002450 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002451 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 }
2453 }
2454 }
Bob Leee5408332009-09-04 18:31:17 -07002455
Romain Guy65b345f2011-07-27 18:51:50 -07002456 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 performNewIntents(data.token, data.intents);
2458 }
2459
Adam Skorydfc7fd72013-08-05 19:23:41 -07002460 public void handleRequestAssistContextExtras(RequestAssistContextExtras cmd) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002461 Bundle data = new Bundle();
2462 ActivityClientRecord r = mActivities.get(cmd.activityToken);
2463 if (r != null) {
2464 r.activity.getApplication().dispatchOnProvideAssistData(r.activity, data);
2465 r.activity.onProvideAssistData(data);
2466 }
2467 if (data.isEmpty()) {
2468 data = null;
2469 }
2470 IActivityManager mgr = ActivityManagerNative.getDefault();
2471 try {
Adam Skory7140a252013-09-11 12:04:58 +01002472 mgr.reportAssistContextExtras(cmd.requestToken, data);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002473 } catch (RemoteException e) {
2474 }
2475 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002476
2477 public void handleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
2478 ActivityClientRecord r = mActivities.get(token);
2479 if (r != null) {
2480 r.activity.onTranslucentConversionComplete(drawComplete);
2481 }
2482 }
2483
Craig Mautnereb8abf72014-07-02 15:04:09 -07002484 public void onNewActivityOptions(IBinder token, ActivityOptions options) {
2485 ActivityClientRecord r = mActivities.get(token);
2486 if (r != null) {
2487 r.activity.onNewActivityOptions(options);
2488 }
2489 }
2490
Craig Mautneree2e45a2014-06-27 12:10:03 -07002491 public void handleStopMediaPlaying(IBinder token) {
2492 ActivityClientRecord r = mActivities.get(token);
2493 if (r != null) {
2494 final Activity activity = r.activity;
2495 if (activity.mMediaPlaying) {
2496 activity.mCalled = false;
2497 activity.onStopMediaPlaying();
2498 // Tick, tick, tick. The activity has 500 msec to return or it will be destroyed.
2499 if (!activity.mCalled) {
2500 throw new SuperNotCalledException("Activity " + activity.getLocalClassName() +
2501 " did not call through to super.onStopMediaPlayback()");
2502 }
2503 activity.mMediaPlaying = false;
2504 }
2505 }
2506 try {
2507 ActivityManagerNative.getDefault().mediaResourcesReleased(token);
2508 } catch (RemoteException e) {
2509 }
2510 }
2511
2512 public void handleOnBackgroundMediaPlayingChanged(IBinder token, boolean playing) {
2513 ActivityClientRecord r = mActivities.get(token);
2514 if (r != null) {
2515 r.activity.onBackgroundMediaPlayingChanged(playing);
2516 }
2517 }
2518
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002519 public void handleInstallProvider(ProviderInfo info) {
2520 installContentProviders(mInitialApplication, Lists.newArrayList(info));
2521 }
2522
Craig Mautner8746a472014-07-24 15:12:54 -07002523 private void handleEnterAnimationComplete(IBinder token) {
2524 ActivityClientRecord r = mActivities.get(token);
2525 if (r != null) {
2526 r.activity.onEnterAnimationComplete();
2527 }
2528 }
2529
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002530 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2531
2532 /**
2533 * Return the Intent that's currently being handled by a
2534 * BroadcastReceiver on this thread, or null if none.
2535 * @hide
2536 */
2537 public static Intent getIntentBeingBroadcast() {
2538 return sCurrentBroadcastIntent.get();
2539 }
2540
Romain Guy65b345f2011-07-27 18:51:50 -07002541 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 // If we are getting ready to gc after going to the background, well
2543 // we are back active so skip it.
2544 unscheduleGcIdler();
2545
2546 String component = data.intent.getComponent().getClassName();
2547
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002548 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002549 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550
2551 IActivityManager mgr = ActivityManagerNative.getDefault();
2552
Romain Guy65b345f2011-07-27 18:51:50 -07002553 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 try {
2555 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2556 data.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002557 data.intent.prepareToEnterProcess();
Dianne Hackborne829fef2010-10-26 17:44:01 -07002558 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2560 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002561 if (DEBUG_BROADCAST) Slog.i(TAG,
2562 "Finishing failed broadcast to " + data.intent.getComponent());
2563 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 throw new RuntimeException(
2565 "Unable to instantiate receiver " + component
2566 + ": " + e.toString(), e);
2567 }
2568
2569 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002570 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002571
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002572 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 TAG, "Performing receive of " + data.intent
2574 + ": app=" + app
2575 + ", appName=" + app.getPackageName()
2576 + ", pkg=" + packageInfo.getPackageName()
2577 + ", comp=" + data.intent.getComponent().toShortString()
2578 + ", dir=" + packageInfo.getAppDir());
2579
Dianne Hackborn21556372010-02-04 16:34:40 -08002580 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002581 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002582 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 receiver.onReceive(context.getReceiverRestrictedContext(),
2584 data.intent);
2585 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002586 if (DEBUG_BROADCAST) Slog.i(TAG,
2587 "Finishing failed broadcast to " + data.intent.getComponent());
2588 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 if (!mInstrumentation.onException(receiver, e)) {
2590 throw new RuntimeException(
2591 "Unable to start receiver " + component
2592 + ": " + e.toString(), e);
2593 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002594 } finally {
2595 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 }
2597
Dianne Hackborne829fef2010-10-26 17:44:01 -07002598 if (receiver.getPendingResult() != null) {
2599 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 }
2601 }
2602
Christopher Tate181fafa2009-05-14 11:12:14 -07002603 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002604 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002605 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002606
Christopher Tate346acb12012-10-15 19:20:25 -07002607 // Sanity check the requested target package's uid against ours
2608 try {
2609 PackageInfo requestedPackage = getPackageManager().getPackageInfo(
2610 data.appInfo.packageName, 0, UserHandle.myUserId());
2611 if (requestedPackage.applicationInfo.uid != Process.myUid()) {
2612 Slog.w(TAG, "Asked to instantiate non-matching package "
2613 + data.appInfo.packageName);
2614 return;
2615 }
2616 } catch (RemoteException e) {
2617 Slog.e(TAG, "Can't reach package manager", e);
2618 return;
2619 }
2620
Christopher Tate181fafa2009-05-14 11:12:14 -07002621 // no longer idle; we have backup work to do
2622 unscheduleGcIdler();
2623
2624 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002625 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002626 String packageName = packageInfo.mPackageName;
Christopher Tate346acb12012-10-15 19:20:25 -07002627 if (packageName == null) {
2628 Slog.d(TAG, "Asked to create backup agent for nonexistent package");
2629 return;
2630 }
2631
Christopher Tate181fafa2009-05-14 11:12:14 -07002632 String classname = data.appInfo.backupAgentName;
Christopher Tate79ec80d2011-06-24 14:58:49 -07002633 // full backup operation but no app-supplied agent? use the default implementation
2634 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2635 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002636 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002637 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002638
Christopher Tate181fafa2009-05-14 11:12:14 -07002639 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002640 IBinder binder = null;
Christopher Tate2e40d112014-07-15 12:37:38 -07002641 BackupAgent agent = mBackupAgents.get(packageName);
2642 if (agent != null) {
2643 // reusing the existing instance
2644 if (DEBUG_BACKUP) {
2645 Slog.v(TAG, "Reusing existing agent instance");
Christopher Tated1475e02009-07-09 15:36:17 -07002646 }
Christopher Tate2e40d112014-07-15 12:37:38 -07002647 binder = agent.onBind();
2648 } else {
2649 try {
2650 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2651
2652 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2653 agent = (BackupAgent) cl.loadClass(classname).newInstance();
2654
2655 // set up the agent's context
2656 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
2657 context.setOuterContext(agent);
2658 agent.attach(context);
2659
2660 agent.onCreate();
2661 binder = agent.onBind();
2662 mBackupAgents.put(packageName, agent);
2663 } catch (Exception e) {
2664 // If this is during restore, fail silently; otherwise go
2665 // ahead and let the user see the crash.
2666 Slog.e(TAG, "Agent threw during creation: " + e);
2667 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2668 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
2669 throw e;
2670 }
2671 // falling through with 'binder' still null
2672 }
Christopher Tated1475e02009-07-09 15:36:17 -07002673 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002674
2675 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002676 try {
2677 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2678 } catch (RemoteException e) {
2679 // nothing to do.
2680 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002681 } catch (Exception e) {
2682 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002683 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002684 }
2685 }
2686
2687 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002688 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002689 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002690
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002691 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002692 String packageName = packageInfo.mPackageName;
2693 BackupAgent agent = mBackupAgents.get(packageName);
2694 if (agent != null) {
2695 try {
2696 agent.onDestroy();
2697 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002698 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002699 e.printStackTrace();
2700 }
2701 mBackupAgents.remove(packageName);
2702 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002703 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002704 }
2705 }
2706
Romain Guy65b345f2011-07-27 18:51:50 -07002707 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 // If we are getting ready to gc after going to the background, well
2709 // we are back active so skip it.
2710 unscheduleGcIdler();
2711
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002712 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002713 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 Service service = null;
2715 try {
2716 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2717 service = (Service) cl.loadClass(data.info.name).newInstance();
2718 } catch (Exception e) {
2719 if (!mInstrumentation.onException(service, e)) {
2720 throw new RuntimeException(
2721 "Unable to instantiate service " + data.info.name
2722 + ": " + e.toString(), e);
2723 }
2724 }
2725
2726 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002727 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728
Jeff Browndefd4a62014-03-10 21:24:37 -07002729 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
2730 context.setOuterContext(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002732 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 service.attach(context, this, data.info.name, data.token, app,
2734 ActivityManagerNative.getDefault());
2735 service.onCreate();
2736 mServices.put(data.token, service);
2737 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002738 ActivityManagerNative.getDefault().serviceDoneExecuting(
2739 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 } catch (RemoteException e) {
2741 // nothing to do.
2742 }
2743 } catch (Exception e) {
2744 if (!mInstrumentation.onException(service, e)) {
2745 throw new RuntimeException(
2746 "Unable to create service " + data.info.name
2747 + ": " + e.toString(), e);
2748 }
2749 }
2750 }
2751
Romain Guy65b345f2011-07-27 18:51:50 -07002752 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002754 if (DEBUG_SERVICE)
2755 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 if (s != null) {
2757 try {
2758 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002759 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 try {
2761 if (!data.rebind) {
2762 IBinder binder = s.onBind(data.intent);
2763 ActivityManagerNative.getDefault().publishService(
2764 data.token, data.intent, binder);
2765 } else {
2766 s.onRebind(data.intent);
2767 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002768 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002770 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 } catch (RemoteException ex) {
2772 }
2773 } catch (Exception e) {
2774 if (!mInstrumentation.onException(s, e)) {
2775 throw new RuntimeException(
2776 "Unable to bind to service " + s
2777 + " with " + data.intent + ": " + e.toString(), e);
2778 }
2779 }
2780 }
2781 }
2782
Romain Guy65b345f2011-07-27 18:51:50 -07002783 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 Service s = mServices.get(data.token);
2785 if (s != null) {
2786 try {
2787 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002788 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 boolean doRebind = s.onUnbind(data.intent);
2790 try {
2791 if (doRebind) {
2792 ActivityManagerNative.getDefault().unbindFinished(
2793 data.token, data.intent, doRebind);
2794 } else {
2795 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002796 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 }
2798 } catch (RemoteException ex) {
2799 }
2800 } catch (Exception e) {
2801 if (!mInstrumentation.onException(s, e)) {
2802 throw new RuntimeException(
2803 "Unable to unbind to service " + s
2804 + " with " + data.intent + ": " + e.toString(), e);
2805 }
2806 }
2807 }
2808 }
2809
Dianne Hackborn625ac272010-09-17 18:29:22 -07002810 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002811 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2812 try {
2813 Service s = mServices.get(info.token);
2814 if (s != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002815 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2816 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002817 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2818 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002820 } finally {
2821 IoUtils.closeQuietly(info.fd);
2822 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 }
2824 }
2825
Dianne Hackborn625ac272010-09-17 18:29:22 -07002826 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002827 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2828 try {
2829 ActivityClientRecord r = mActivities.get(info.token);
2830 if (r != null && r.activity != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002831 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2832 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002833 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2834 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002835 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002836 } finally {
2837 IoUtils.closeQuietly(info.fd);
2838 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002839 }
2840 }
2841
Marco Nelissen18cb2872011-11-15 11:19:53 -08002842 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002843 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2844 try {
2845 ProviderClientRecord r = mLocalProviders.get(info.token);
2846 if (r != null && r.mLocalProvider != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002847 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2848 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002849 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2850 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002851 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002852 } finally {
2853 IoUtils.closeQuietly(info.fd);
2854 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002855 }
2856 }
2857
Romain Guy65b345f2011-07-27 18:51:50 -07002858 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 Service s = mServices.get(data.token);
2860 if (s != null) {
2861 try {
2862 if (data.args != null) {
2863 data.args.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002864 data.args.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002866 int res;
2867 if (!data.taskRemoved) {
2868 res = s.onStartCommand(data.args, data.flags, data.startId);
2869 } else {
2870 s.onTaskRemoved(data.args);
2871 res = Service.START_TASK_REMOVED_COMPLETE;
2872 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002873
2874 QueuedWork.waitToFinish();
2875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002877 ActivityManagerNative.getDefault().serviceDoneExecuting(
2878 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 } catch (RemoteException e) {
2880 // nothing to do.
2881 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002882 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 } catch (Exception e) {
2884 if (!mInstrumentation.onException(s, e)) {
2885 throw new RuntimeException(
2886 "Unable to start service " + s
2887 + " with " + data.args + ": " + e.toString(), e);
2888 }
2889 }
2890 }
2891 }
2892
Romain Guy65b345f2011-07-27 18:51:50 -07002893 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 Service s = mServices.remove(token);
2895 if (s != null) {
2896 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002897 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 s.onDestroy();
2899 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002900 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002902 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002904
2905 QueuedWork.waitToFinish();
2906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002908 ActivityManagerNative.getDefault().serviceDoneExecuting(
2909 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 } catch (RemoteException e) {
2911 // nothing to do.
2912 }
2913 } catch (Exception e) {
2914 if (!mInstrumentation.onException(s, e)) {
2915 throw new RuntimeException(
2916 "Unable to stop service " + s
2917 + ": " + e.toString(), e);
2918 }
2919 }
2920 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002921 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 }
2923
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002924 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002926 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002927 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 + " finished=" + r.activity.mFinished);
2929 if (r != null && !r.activity.mFinished) {
2930 if (clearHide) {
2931 r.hideForNow = false;
2932 r.activity.mStartedActivity = false;
2933 }
2934 try {
Dianne Hackborn689586d2012-10-01 18:23:04 -07002935 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 if (r.pendingIntents != null) {
2937 deliverNewIntents(r, r.pendingIntents);
2938 r.pendingIntents = null;
2939 }
2940 if (r.pendingResults != null) {
2941 deliverResults(r, r.pendingResults);
2942 r.pendingResults = null;
2943 }
2944 r.activity.performResume();
2945
Bob Leee5408332009-09-04 18:31:17 -07002946 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002947 UserHandle.myUserId(), r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 r.paused = false;
2950 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 r.state = null;
Craig Mautnera0026042014-04-23 11:45:37 -07002952 r.persistentState = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 } catch (Exception e) {
2954 if (!mInstrumentation.onException(r.activity, e)) {
2955 throw new RuntimeException(
2956 "Unable to resume activity "
2957 + r.intent.getComponent().toShortString()
2958 + ": " + e.toString(), e);
2959 }
2960 }
2961 }
2962 return r;
2963 }
2964
Romain Guya998dff2012-03-23 18:58:36 -07002965 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002966 if (r.mPendingRemoveWindow != null) {
2967 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2968 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2969 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07002970 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002971 r.activity.getClass().getName(), "Activity");
2972 }
2973 }
2974 r.mPendingRemoveWindow = null;
2975 r.mPendingRemoveWindowManager = null;
2976 }
2977
Craig Mautner233ceee2014-05-09 17:05:11 -07002978 final void handleResumeActivity(IBinder token,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002979 boolean clearHide, boolean isForward, boolean reallyResume) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 // If we are getting ready to gc after going to the background, well
2981 // we are back active so skip it.
2982 unscheduleGcIdler();
2983
Adam Powellcfbe9be2013-11-06 14:58:58 -08002984 // TODO Push resumeArgs into the activity for consideration
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002985 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986
2987 if (r != null) {
2988 final Activity a = r.activity;
2989
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002990 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 TAG, "Resume " + r + " started activity: " +
2992 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2993 + ", finished: " + a.mFinished);
2994
2995 final int forwardBit = isForward ?
2996 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 // If the window hasn't yet been added to the window manager,
2999 // and this guy didn't finish itself or start another activity,
3000 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003001 boolean willBeVisible = !a.mStartedActivity;
3002 if (!willBeVisible) {
3003 try {
3004 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
3005 a.getActivityToken());
3006 } catch (RemoteException e) {
3007 }
3008 }
3009 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 r.window = r.activity.getWindow();
3011 View decor = r.window.getDecorView();
3012 decor.setVisibility(View.INVISIBLE);
3013 ViewManager wm = a.getWindowManager();
3014 WindowManager.LayoutParams l = r.window.getAttributes();
3015 a.mDecor = decor;
3016 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
3017 l.softInputMode |= forwardBit;
3018 if (a.mVisibleFromClient) {
3019 a.mWindowAdded = true;
3020 wm.addView(decor, l);
3021 }
3022
3023 // If the window has already been added, but during resume
3024 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003025 // window visible.
3026 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003027 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 TAG, "Launch " + r + " mStartedActivity set");
3029 r.hideForNow = true;
3030 }
3031
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003032 // Get rid of anything left hanging around.
3033 cleanUpPendingRemoveWindows(r);
3034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 // The window is now visible if it has been added, we are not
3036 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003037 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003038 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003040 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003041 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003043 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 r.newConfig = null;
3045 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003046 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 + isForward);
3048 WindowManager.LayoutParams l = r.window.getAttributes();
3049 if ((l.softInputMode
3050 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
3051 != forwardBit) {
3052 l.softInputMode = (l.softInputMode
3053 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
3054 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003055 if (r.activity.mVisibleFromClient) {
3056 ViewManager wm = a.getWindowManager();
3057 View decor = r.window.getDecorView();
3058 wm.updateViewLayout(decor, l);
3059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 }
3061 r.activity.mVisibleFromServer = true;
3062 mNumVisibleActivities++;
3063 if (r.activity.mVisibleFromClient) {
3064 r.activity.makeVisible();
3065 }
3066 }
3067
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003068 if (!r.onlyLocalRequest) {
3069 r.nextIdle = mNewActivities;
3070 mNewActivities = r;
3071 if (localLOGV) Slog.v(
3072 TAG, "Scheduling idle handler for " + r);
3073 Looper.myQueue().addIdleHandler(new Idler());
3074 }
3075 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003077 // Tell the activity manager we have resumed.
3078 if (reallyResume) {
3079 try {
3080 ActivityManagerNative.getDefault().activityResumed(token);
3081 } catch (RemoteException ex) {
3082 }
3083 }
3084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 } else {
3086 // If an exception was thrown when trying to resume, then
3087 // just end this activity.
3088 try {
3089 ActivityManagerNative.getDefault()
Winson Chung3b3f4642014-04-22 10:08:18 -07003090 .finishActivity(token, Activity.RESULT_CANCELED, null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 } catch (RemoteException ex) {
3092 }
3093 }
3094 }
3095
3096 private int mThumbnailWidth = -1;
3097 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003098 private Bitmap mAvailThumbnailBitmap = null;
3099 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100
Romain Guy65b345f2011-07-27 18:51:50 -07003101 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003102 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003104 if (thumbnail == null) {
3105 int w = mThumbnailWidth;
3106 int h;
3107 if (w < 0) {
3108 Resources res = r.activity.getResources();
Winson Chung6784f1c2014-05-22 11:22:41 -07003109 int wId = com.android.internal.R.dimen.thumbnail_width;
3110 int hId = com.android.internal.R.dimen.thumbnail_height;
Winson Chungfd086222014-05-13 15:49:42 -07003111 mThumbnailWidth = w = res.getDimensionPixelSize(wId);
3112 mThumbnailHeight = h = res.getDimensionPixelSize(hId);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003113 } else {
3114 h = mThumbnailHeight;
3115 }
3116
3117 // On platforms where we don't want thumbnails, set dims to (0,0)
3118 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07003119 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
3120 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003121 thumbnail.eraseColor(0);
3122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 }
3124
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003125 if (thumbnail != null) {
3126 Canvas cv = mThumbnailCanvas;
3127 if (cv == null) {
3128 mThumbnailCanvas = cv = new Canvas();
3129 }
3130
3131 cv.setBitmap(thumbnail);
3132 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
3133 mAvailThumbnailBitmap = thumbnail;
3134 thumbnail = null;
3135 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07003136 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07003138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 } catch (Exception e) {
3140 if (!mInstrumentation.onException(r.activity, e)) {
3141 throw new RuntimeException(
3142 "Unable to create thumbnail of "
3143 + r.intent.getComponent().toShortString()
3144 + ": " + e.toString(), e);
3145 }
3146 thumbnail = null;
3147 }
3148
3149 return thumbnail;
3150 }
3151
Romain Guy65b345f2011-07-27 18:51:50 -07003152 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003154 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003156 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 if (userLeaving) {
3158 performUserLeavingActivity(r);
3159 }
Bob Leee5408332009-09-04 18:31:17 -07003160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003162 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07003164 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003165 if (r.isPreHoneycomb()) {
3166 QueuedWork.waitToFinish();
3167 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 // Tell the activity manager we have paused.
3170 try {
Craig Mautnera0026042014-04-23 11:45:37 -07003171 ActivityManagerNative.getDefault().activityPaused(token, r.persistentState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 } catch (RemoteException ex) {
3173 }
3174 }
3175 }
3176
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003177 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 mInstrumentation.callActivityOnUserLeaving(r.activity);
3179 }
3180
3181 final Bundle performPauseActivity(IBinder token, boolean finished,
3182 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003183 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 return r != null ? performPauseActivity(r, finished, saveState) : null;
3185 }
3186
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003187 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 boolean saveState) {
3189 if (r.paused) {
3190 if (r.activity.mFinished) {
3191 // If we are finishing, we won't call onResume() in certain cases.
3192 // So here we likewise don't want to call onPause() if the activity
3193 // isn't resumed.
3194 return null;
3195 }
3196 RuntimeException e = new RuntimeException(
3197 "Performing pause of activity that is not resumed: "
3198 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003199 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 if (finished) {
3202 r.activity.mFinished = true;
3203 }
3204 try {
3205 // Next have the activity save its current state and managed dialogs...
3206 if (!r.activity.mFinished && saveState) {
Craig Mautnera0026042014-04-23 11:45:37 -07003207 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 }
3209 // Now we are idle.
3210 r.activity.mCalled = false;
3211 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003212 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, UserHandle.myUserId(),
3213 r.activity.getComponentName().getClassName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 if (!r.activity.mCalled) {
3215 throw new SuperNotCalledException(
3216 "Activity " + r.intent.getComponent().toShortString() +
3217 " did not call through to super.onPause()");
3218 }
3219
3220 } catch (SuperNotCalledException e) {
3221 throw e;
3222
3223 } catch (Exception e) {
3224 if (!mInstrumentation.onException(r.activity, e)) {
3225 throw new RuntimeException(
3226 "Unable to pause activity "
3227 + r.intent.getComponent().toShortString()
3228 + ": " + e.toString(), e);
3229 }
3230 }
3231 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06003232
3233 // Notify any outstanding on paused listeners
3234 ArrayList<OnActivityPausedListener> listeners;
3235 synchronized (mOnPauseListeners) {
3236 listeners = mOnPauseListeners.remove(r.activity);
3237 }
3238 int size = (listeners != null ? listeners.size() : 0);
3239 for (int i = 0; i < size; i++) {
3240 listeners.get(i).onPaused(r.activity);
3241 }
3242
Craig Mautnera0026042014-04-23 11:45:37 -07003243 return !r.activity.mFinished && saveState ? r.state : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 }
3245
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003246 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003247 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003248 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 }
3250
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003251 private static class StopInfo implements Runnable {
3252 ActivityClientRecord activity;
3253 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -07003254 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003256
3257 @Override public void run() {
3258 // Tell activity manager we have been stopped.
3259 try {
3260 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
3261 ActivityManagerNative.getDefault().activityStopped(
Craig Mautnera0026042014-04-23 11:45:37 -07003262 activity.token, state, persistentState, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003263 } catch (RemoteException ex) {
3264 }
3265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 }
3267
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003268 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003269 public final IActivityManager.ContentProviderHolder holder;
3270 public final ProviderClientRecord client;
3271 public int stableCount;
3272 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003273
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003274 // When this is set, the stable and unstable ref counts are 0 and
3275 // we have a pending operation scheduled to remove the ref count
3276 // from the activity manager. On the activity manager we are still
3277 // holding an unstable ref, though it is not reflected in the counts
3278 // here.
3279 public boolean removePending;
3280
3281 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
3282 ProviderClientRecord inClient, int sCount, int uCount) {
3283 holder = inHolder;
3284 client = inClient;
3285 stableCount = sCount;
3286 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 }
3288 }
3289
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003290 /**
3291 * Core implementation of stopping an activity. Note this is a little
3292 * tricky because the server's meaning of stop is slightly different
3293 * than our client -- for the server, stop means to save state and give
3294 * it the result when it is done, but the window may still be visible.
3295 * For the client, we want to call onStop()/onStart() to indicate when
3296 * the activity's UI visibillity changes.
3297 */
Romain Guy65b345f2011-07-27 18:51:50 -07003298 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003299 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003300 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 if (r != null) {
3302 if (!keepShown && r.stopped) {
3303 if (r.activity.mFinished) {
3304 // If we are finishing, we won't call onResume() in certain
3305 // cases. So here we likewise don't want to call onStop()
3306 // if the activity isn't resumed.
3307 return;
3308 }
3309 RuntimeException e = new RuntimeException(
3310 "Performing stop of activity that is not resumed: "
3311 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003312 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 }
3314
3315 if (info != null) {
3316 try {
3317 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003318 // For now, don't create the thumbnail here; we are
3319 // doing that by doing a screen snapshot.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 info.description = r.activity.onCreateDescription();
3321 } catch (Exception e) {
3322 if (!mInstrumentation.onException(r.activity, e)) {
3323 throw new RuntimeException(
3324 "Unable to save state of activity "
3325 + r.intent.getComponent().toShortString()
3326 + ": " + e.toString(), e);
3327 }
3328 }
3329 }
3330
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003331 // Next have the activity save its current state and managed dialogs...
3332 if (!r.activity.mFinished && saveState) {
3333 if (r.state == null) {
Craig Mautnera0026042014-04-23 11:45:37 -07003334 callCallActivityOnSaveInstanceState(r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003335 }
3336 }
3337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 if (!keepShown) {
3339 try {
3340 // Now we are idle.
3341 r.activity.performStop();
3342 } catch (Exception e) {
3343 if (!mInstrumentation.onException(r.activity, e)) {
3344 throw new RuntimeException(
3345 "Unable to stop activity "
3346 + r.intent.getComponent().toShortString()
3347 + ": " + e.toString(), e);
3348 }
3349 }
3350 r.stopped = true;
3351 }
3352
3353 r.paused = true;
3354 }
3355 }
3356
Romain Guy65b345f2011-07-27 18:51:50 -07003357 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 View v = r.activity.mDecor;
3359 if (v != null) {
3360 if (show) {
3361 if (!r.activity.mVisibleFromServer) {
3362 r.activity.mVisibleFromServer = true;
3363 mNumVisibleActivities++;
3364 if (r.activity.mVisibleFromClient) {
3365 r.activity.makeVisible();
3366 }
3367 }
3368 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003369 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003370 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003372 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 r.newConfig = null;
3374 }
3375 } else {
3376 if (r.activity.mVisibleFromServer) {
3377 r.activity.mVisibleFromServer = false;
3378 mNumVisibleActivities--;
3379 v.setVisibility(View.INVISIBLE);
3380 }
3381 }
3382 }
3383 }
3384
Romain Guy65b345f2011-07-27 18:51:50 -07003385 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003386 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387 r.activity.mConfigChangeFlags |= configChanges;
3388
3389 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003390 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003392 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 TAG, "Finishing stop of " + r + ": show=" + show
3394 + " win=" + r.window);
3395
3396 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003397
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003398 // Make sure any pending writes are now committed.
3399 if (!r.isPreHoneycomb()) {
3400 QueuedWork.waitToFinish();
3401 }
3402
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003403 // Schedule the call to tell the activity manager we have
3404 // stopped. We don't do this immediately, because we want to
3405 // have a chance for any other pending work (in particular memory
3406 // trim requests) to complete before you tell the activity
3407 // manager to proceed and allow us to go fully into the background.
3408 info.activity = r;
3409 info.state = r.state;
Craig Mautnera0026042014-04-23 11:45:37 -07003410 info.persistentState = r.persistentState;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003411 mH.post(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412 }
3413
3414 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003415 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 if (r.stopped) {
3417 r.activity.performRestart();
3418 r.stopped = false;
3419 }
3420 }
3421
Romain Guy65b345f2011-07-27 18:51:50 -07003422 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003423 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003424
3425 if (r == null) {
3426 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3427 return;
3428 }
3429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003431 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 } else if (show && r.stopped) {
3433 // If we are getting ready to gc after going to the background, well
3434 // we are back active so skip it.
3435 unscheduleGcIdler();
3436
3437 r.activity.performRestart();
3438 r.stopped = false;
3439 }
3440 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003441 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 TAG, "Handle window " + r + " visibility: " + show);
3443 updateVisibility(r, show);
3444 }
3445 }
3446
Romain Guy65b345f2011-07-27 18:51:50 -07003447 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003448 ActivityClientRecord r = mActivities.get(token);
3449
3450 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003451 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003452 return;
3453 }
3454
3455 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003456 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003457 try {
3458 // Now we are idle.
3459 r.activity.performStop();
3460 } catch (Exception e) {
3461 if (!mInstrumentation.onException(r.activity, e)) {
3462 throw new RuntimeException(
3463 "Unable to stop activity "
3464 + r.intent.getComponent().toShortString()
3465 + ": " + e.toString(), e);
3466 }
3467 }
3468 r.stopped = true;
3469 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003470
3471 // Make sure any pending writes are now committed.
3472 if (!r.isPreHoneycomb()) {
3473 QueuedWork.waitToFinish();
3474 }
3475
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003476 // Tell activity manager we slept.
3477 try {
3478 ActivityManagerNative.getDefault().activitySlept(r.token);
3479 } catch (RemoteException ex) {
3480 }
3481 } else {
3482 if (r.stopped && r.activity.mVisibleFromServer) {
3483 r.activity.performRestart();
3484 r.stopped = false;
3485 }
3486 }
3487 }
3488
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003489 private void handleSetCoreSettings(Bundle coreSettings) {
Craig Mautner88c05892013-06-28 09:47:45 -07003490 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003491 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003492 }
3493 }
3494
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003495 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3496 LoadedApk apk = peekPackageInfo(data.pkg, false);
3497 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003498 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003499 }
3500 apk = peekPackageInfo(data.pkg, true);
3501 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003502 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003503 }
3504 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07003505 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003506 }
3507
Romain Guy65b345f2011-07-27 18:51:50 -07003508 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 final int N = results.size();
3510 for (int i=0; i<N; i++) {
3511 ResultInfo ri = results.get(i);
3512 try {
3513 if (ri.mData != null) {
3514 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003515 ri.mData.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003516 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003517 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003518 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 r.activity.dispatchActivityResult(ri.mResultWho,
3520 ri.mRequestCode, ri.mResultCode, ri.mData);
3521 } catch (Exception e) {
3522 if (!mInstrumentation.onException(r.activity, e)) {
3523 throw new RuntimeException(
3524 "Failure delivering result " + ri + " to activity "
3525 + r.intent.getComponent().toShortString()
3526 + ": " + e.toString(), e);
3527 }
3528 }
3529 }
3530 }
3531
Romain Guy65b345f2011-07-27 18:51:50 -07003532 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003533 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003534 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 if (r != null) {
3536 final boolean resumed = !r.paused;
3537 if (!r.activity.mFinished && r.activity.mDecor != null
3538 && r.hideForNow && resumed) {
3539 // We had hidden the activity because it started another
3540 // one... we have gotten a result back and we are not
3541 // paused, so make sure our window is visible.
3542 updateVisibility(r, true);
3543 }
3544 if (resumed) {
3545 try {
3546 // Now we are idle.
3547 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003548 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 mInstrumentation.callActivityOnPause(r.activity);
3550 if (!r.activity.mCalled) {
3551 throw new SuperNotCalledException(
3552 "Activity " + r.intent.getComponent().toShortString()
3553 + " did not call through to super.onPause()");
3554 }
3555 } catch (SuperNotCalledException e) {
3556 throw e;
3557 } catch (Exception e) {
3558 if (!mInstrumentation.onException(r.activity, e)) {
3559 throw new RuntimeException(
3560 "Unable to pause activity "
3561 + r.intent.getComponent().toShortString()
3562 + ": " + e.toString(), e);
3563 }
3564 }
3565 }
3566 deliverResults(r, res.results);
3567 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003568 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003569 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 }
3571 }
3572 }
3573
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003574 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 return performDestroyActivity(token, finishing, 0, false);
3576 }
3577
Romain Guy65b345f2011-07-27 18:51:50 -07003578 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003579 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003580 ActivityClientRecord r = mActivities.get(token);
Craig Mautner88c05892013-06-28 09:47:45 -07003581 Class<? extends Activity> activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003582 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003584 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003585 r.activity.mConfigChangeFlags |= configChanges;
3586 if (finishing) {
3587 r.activity.mFinished = true;
3588 }
3589 if (!r.paused) {
3590 try {
3591 r.activity.mCalled = false;
3592 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003593 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, UserHandle.myUserId(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 r.activity.getComponentName().getClassName());
3595 if (!r.activity.mCalled) {
3596 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003597 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 + " did not call through to super.onPause()");
3599 }
3600 } catch (SuperNotCalledException e) {
3601 throw e;
3602 } catch (Exception e) {
3603 if (!mInstrumentation.onException(r.activity, e)) {
3604 throw new RuntimeException(
3605 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003606 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003607 + ": " + e.toString(), e);
3608 }
3609 }
3610 r.paused = true;
3611 }
3612 if (!r.stopped) {
3613 try {
3614 r.activity.performStop();
3615 } catch (SuperNotCalledException e) {
3616 throw e;
3617 } catch (Exception e) {
3618 if (!mInstrumentation.onException(r.activity, e)) {
3619 throw new RuntimeException(
3620 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003621 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 + ": " + e.toString(), e);
3623 }
3624 }
3625 r.stopped = true;
3626 }
3627 if (getNonConfigInstance) {
3628 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003629 r.lastNonConfigurationInstances
3630 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003631 } catch (Exception e) {
3632 if (!mInstrumentation.onException(r.activity, e)) {
3633 throw new RuntimeException(
3634 "Unable to retain activity "
3635 + r.intent.getComponent().toShortString()
3636 + ": " + e.toString(), e);
3637 }
3638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 }
3640 try {
3641 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003642 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003643 if (!r.activity.mCalled) {
3644 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003645 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003646 " did not call through to super.onDestroy()");
3647 }
3648 if (r.window != null) {
3649 r.window.closeAllPanels();
3650 }
3651 } catch (SuperNotCalledException e) {
3652 throw e;
3653 } catch (Exception e) {
3654 if (!mInstrumentation.onException(r.activity, e)) {
3655 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003656 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3657 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 }
3659 }
3660 }
3661 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003662 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003663 return r;
3664 }
3665
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003666 private static String safeToComponentShortString(Intent intent) {
3667 ComponentName component = intent.getComponent();
3668 return component == null ? "[Unknown]" : component.toShortString();
3669 }
3670
Romain Guy65b345f2011-07-27 18:51:50 -07003671 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003673 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003674 configChanges, getNonConfigInstance);
3675 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003676 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003677 WindowManager wm = r.activity.getWindowManager();
3678 View v = r.activity.mDecor;
3679 if (v != null) {
3680 if (r.activity.mVisibleFromServer) {
3681 mNumVisibleActivities--;
3682 }
3683 IBinder wtoken = v.getWindowToken();
3684 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003685 if (r.onlyLocalRequest) {
3686 // Hold off on removing this until the new activity's
3687 // window is being added.
3688 r.mPendingRemoveWindow = v;
3689 r.mPendingRemoveWindowManager = wm;
3690 } else {
3691 wm.removeViewImmediate(v);
3692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003694 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003695 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 r.activity.getClass().getName(), "Activity");
3697 }
3698 r.activity.mDecor = null;
3699 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003700 if (r.mPendingRemoveWindow == null) {
3701 // If we are delaying the removal of the activity window, then
3702 // we can't clean up all windows here. Note that we can't do
3703 // so later either, which means any windows that aren't closed
3704 // by the app will leak. Well we try to warning them a lot
3705 // about leaking windows, because that is a bug, so if they are
3706 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07003707 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003708 r.activity.getClass().getName(), "Activity");
3709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710
3711 // Mocked out contexts won't be participating in the normal
3712 // process lifecycle, but if we're running with a proper
3713 // ApplicationContext we need to have it tear down things
3714 // cleanly.
3715 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003716 if (c instanceof ContextImpl) {
3717 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718 r.activity.getClass().getName(), "Activity");
3719 }
3720 }
3721 if (finishing) {
3722 try {
3723 ActivityManagerNative.getDefault().activityDestroyed(token);
3724 } catch (RemoteException ex) {
3725 // If the system process has died, it's game over for everyone.
3726 }
3727 }
3728 }
3729
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003730 public final void requestRelaunchActivity(IBinder token,
3731 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3732 int configChanges, boolean notResumed, Configuration config,
3733 boolean fromServer) {
3734 ActivityClientRecord target = null;
3735
Craig Mautner88c05892013-06-28 09:47:45 -07003736 synchronized (mResourcesManager) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003737 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3738 ActivityClientRecord r = mRelaunchingActivities.get(i);
3739 if (r.token == token) {
3740 target = r;
3741 if (pendingResults != null) {
3742 if (r.pendingResults != null) {
3743 r.pendingResults.addAll(pendingResults);
3744 } else {
3745 r.pendingResults = pendingResults;
3746 }
3747 }
3748 if (pendingNewIntents != null) {
3749 if (r.pendingIntents != null) {
3750 r.pendingIntents.addAll(pendingNewIntents);
3751 } else {
3752 r.pendingIntents = pendingNewIntents;
3753 }
3754 }
3755 break;
3756 }
3757 }
3758
3759 if (target == null) {
3760 target = new ActivityClientRecord();
3761 target.token = token;
3762 target.pendingResults = pendingResults;
3763 target.pendingIntents = pendingNewIntents;
3764 if (!fromServer) {
3765 ActivityClientRecord existing = mActivities.get(token);
3766 if (existing != null) {
3767 target.startsNotResumed = existing.paused;
3768 }
3769 target.onlyLocalRequest = true;
3770 }
3771 mRelaunchingActivities.add(target);
Jeff Brown9ef09972013-10-15 20:49:59 -07003772 sendMessage(H.RELAUNCH_ACTIVITY, target);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003773 }
3774
3775 if (fromServer) {
3776 target.startsNotResumed = notResumed;
3777 target.onlyLocalRequest = false;
3778 }
3779 if (config != null) {
3780 target.createdConfig = config;
3781 }
3782 target.pendingConfigChanges |= configChanges;
3783 }
3784 }
3785
Romain Guy65b345f2011-07-27 18:51:50 -07003786 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 // If we are getting ready to gc after going to the background, well
3788 // we are back active so skip it.
3789 unscheduleGcIdler();
3790
3791 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003792 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003794 // First: make sure we have the most recent configuration and most
3795 // recent version of the activity, or skip it if some previous call
3796 // had taken a more recent version.
Craig Mautner88c05892013-06-28 09:47:45 -07003797 synchronized (mResourcesManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003798 int N = mRelaunchingActivities.size();
3799 IBinder token = tmp.token;
3800 tmp = null;
3801 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003802 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003803 if (r.token == token) {
3804 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003805 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 mRelaunchingActivities.remove(i);
3807 i--;
3808 N--;
3809 }
3810 }
Bob Leee5408332009-09-04 18:31:17 -07003811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003812 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003813 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003814 return;
3815 }
Bob Leee5408332009-09-04 18:31:17 -07003816
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003817 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3818 + tmp.token + " with configChanges=0x"
3819 + Integer.toHexString(configChanges));
3820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 if (mPendingConfiguration != null) {
3822 changedConfig = mPendingConfiguration;
3823 mPendingConfiguration = null;
3824 }
3825 }
Bob Leee5408332009-09-04 18:31:17 -07003826
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003827 if (tmp.createdConfig != null) {
3828 // If the activity manager is passing us its current config,
3829 // assume that is really what we want regardless of what we
3830 // may have pending.
3831 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003832 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3833 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3834 if (changedConfig == null
3835 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3836 changedConfig = tmp.createdConfig;
3837 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003838 }
3839 }
3840
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003841 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003842 + tmp.token + ": changedConfig=" + changedConfig);
3843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 // If there was a pending configuration change, execute it first.
3845 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003846 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003847 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003848 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849 }
Bob Leee5408332009-09-04 18:31:17 -07003850
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003851 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003852 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853 if (r == null) {
3854 return;
3855 }
Bob Leee5408332009-09-04 18:31:17 -07003856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003857 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003858 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003859 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003860
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003861 r.activity.mChangingConfigurations = true;
3862
Dianne Hackborne2b04802010-12-09 09:24:55 -08003863 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003864 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003865 performPauseActivity(r.token, false, r.isPreHoneycomb());
3866 }
3867 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
Craig Mautnera0026042014-04-23 11:45:37 -07003868 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003869 }
Bob Leee5408332009-09-04 18:31:17 -07003870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003873 r.activity = null;
3874 r.window = null;
3875 r.hideForNow = false;
3876 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003877 // Merge any pending results and pending intents; don't just replace them
3878 if (tmp.pendingResults != null) {
3879 if (r.pendingResults == null) {
3880 r.pendingResults = tmp.pendingResults;
3881 } else {
3882 r.pendingResults.addAll(tmp.pendingResults);
3883 }
3884 }
3885 if (tmp.pendingIntents != null) {
3886 if (r.pendingIntents == null) {
3887 r.pendingIntents = tmp.pendingIntents;
3888 } else {
3889 r.pendingIntents.addAll(tmp.pendingIntents);
3890 }
3891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003893
Christopher Tateb70f3df2009-04-07 16:07:59 -07003894 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003895 }
3896
Craig Mautnera0026042014-04-23 11:45:37 -07003897 private void callCallActivityOnSaveInstanceState(ActivityClientRecord r) {
3898 r.state = new Bundle();
3899 r.state.setAllowFds(false);
3900 if (r.isPersistable()) {
3901 r.persistentState = new PersistableBundle();
3902 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state,
3903 r.persistentState);
3904 } else {
3905 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
3906 }
3907 }
3908
Dianne Hackborn73c14162012-09-19 15:45:06 -07003909 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003910 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003911 ArrayList<ComponentCallbacks2> callbacks
3912 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003913
Craig Mautner88c05892013-06-28 09:47:45 -07003914 synchronized (mResourcesManager) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003915 final int NAPP = mAllApplications.size();
3916 for (int i=0; i<NAPP; i++) {
Dianne Hackborn73c14162012-09-19 15:45:06 -07003917 callbacks.add(mAllApplications.get(i));
3918 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003919 final int NACT = mActivities.size();
3920 for (int i=0; i<NACT; i++) {
3921 ActivityClientRecord ar = mActivities.valueAt(i);
3922 Activity a = ar.activity;
3923 if (a != null) {
3924 Configuration thisConfig = applyConfigCompatMainThread(
3925 mCurDefaultDisplayDpi, newConfig,
3926 ar.packageInfo.getCompatibilityInfo());
3927 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
3928 // If the activity is currently resumed, its configuration
3929 // needs to change right now.
3930 callbacks.add(a);
3931 } else if (thisConfig != null) {
3932 // Otherwise, we will tell it about the change
3933 // the next time it is resumed or shown. Note that
3934 // the activity manager may, before then, decide the
3935 // activity needs to be destroyed to handle its new
3936 // configuration.
3937 if (DEBUG_CONFIGURATION) {
3938 Slog.v(TAG, "Setting activity "
3939 + ar.activityInfo.name + " newConfig=" + thisConfig);
Romain Guya998dff2012-03-23 18:58:36 -07003940 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003941 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003942 }
3943 }
3944 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003945 final int NSVC = mServices.size();
3946 for (int i=0; i<NSVC; i++) {
3947 callbacks.add(mServices.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 }
3949 }
3950 synchronized (mProviderMap) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003951 final int NPRV = mLocalProviders.size();
3952 for (int i=0; i<NPRV; i++) {
3953 callbacks.add(mLocalProviders.valueAt(i).mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 }
3955 }
Bob Leee5408332009-09-04 18:31:17 -07003956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003957 return callbacks;
3958 }
Bob Leee5408332009-09-04 18:31:17 -07003959
Romain Guya998dff2012-03-23 18:58:36 -07003960 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003961 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003962 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 // we check the runtime type and act accordingly.
3964 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3965 if (activity != null) {
3966 activity.mCalled = false;
3967 }
Bob Leee5408332009-09-04 18:31:17 -07003968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 boolean shouldChangeConfig = false;
3970 if ((activity == null) || (activity.mCurrentConfig == null)) {
3971 shouldChangeConfig = true;
3972 } else {
Bob Leee5408332009-09-04 18:31:17 -07003973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003974 // If the new config is the same as the config this Activity
3975 // is already running with then don't bother calling
3976 // onConfigurationChanged
3977 int diff = activity.mCurrentConfig.diff(config);
3978 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 // If this activity doesn't handle any of the config changes
3980 // then don't bother calling onConfigurationChanged as we're
3981 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003982 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003983 shouldChangeConfig = true;
3984 }
3985 }
3986 }
Bob Leee5408332009-09-04 18:31:17 -07003987
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003988 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003989 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003990 if (shouldChangeConfig) {
3991 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 if (activity != null) {
3994 if (!activity.mCalled) {
3995 throw new SuperNotCalledException(
3996 "Activity " + activity.getLocalClassName() +
3997 " did not call through to super.onConfigurationChanged()");
3998 }
3999 activity.mConfigChangeFlags = 0;
4000 activity.mCurrentConfig = new Configuration(config);
4001 }
4002 }
4003 }
4004
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004005 public final void applyConfigurationToResources(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -07004006 synchronized (mResourcesManager) {
4007 mResourcesManager.applyConfigurationToResourcesLocked(config, null);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004008 }
4009 }
4010
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004011 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004012 Configuration config = mConfiguration;
4013 if (mCompatConfiguration == null) {
4014 mCompatConfiguration = new Configuration();
4015 }
4016 mCompatConfiguration.setTo(mConfiguration);
Craig Mautner88c05892013-06-28 09:47:45 -07004017 if (mResourcesManager.applyCompatConfiguration(displayDensity, mCompatConfiguration)) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004018 config = mCompatConfiguration;
4019 }
4020 return config;
4021 }
4022
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004023 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004024
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004025 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004026
Craig Mautner88c05892013-06-28 09:47:45 -07004027 synchronized (mResourcesManager) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004028 if (mPendingConfiguration != null) {
4029 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
4030 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004031 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004032 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004033 }
4034 mPendingConfiguration = null;
4035 }
4036
4037 if (config == null) {
4038 return;
4039 }
4040
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004041 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004042 + config);
Craig Mautner88c05892013-06-28 09:47:45 -07004043
4044 mResourcesManager.applyConfigurationToResourcesLocked(config, compat);
4045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004046 if (mConfiguration == null) {
4047 mConfiguration = new Configuration();
4048 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004049 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004050 return;
4051 }
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004052 configDiff = mConfiguration.diff(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004054 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07004056
4057 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
4058
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004059 freeTextLayoutCachesIfNeeded(configDiff);
4060
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004061 if (callbacks != null) {
4062 final int N = callbacks.size();
4063 for (int i=0; i<N; i++) {
4064 performConfigurationChanged(callbacks.get(i), config);
4065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 }
4067 }
4068
Romain Guy46bfc482013-08-16 18:38:29 -07004069 static void freeTextLayoutCachesIfNeeded(int configDiff) {
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004070 if (configDiff != 0) {
4071 // Ask text layout engine to free its caches if there is a locale change
4072 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
4073 if (hasLocaleConfigChange) {
4074 Canvas.freeTextLayoutCaches();
4075 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
4076 }
4077 }
4078 }
4079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004081 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 if (r == null || r.activity == null) {
4083 return;
4084 }
Bob Leee5408332009-09-04 18:31:17 -07004085
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004086 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004087 + r.activityInfo.name);
4088
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004089 performConfigurationChanged(r.activity, mCompatConfiguration);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004090
4091 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004092 }
4093
Romain Guy7eabe552011-07-21 14:56:34 -07004094 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004095 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004096 try {
Romain Guy7eabe552011-07-21 14:56:34 -07004097 switch (profileType) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004098 default:
4099 mProfiler.setProfiler(pcd.path, pcd.fd);
4100 mProfiler.autoStopProfiler = false;
4101 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004102 break;
4103 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004104 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004105 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004106 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004107 } finally {
4108 try {
4109 pcd.fd.close();
4110 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004111 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004112 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004113 }
4114 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07004115 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07004116 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004117 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004118 break;
Romain Guy7eabe552011-07-21 14:56:34 -07004119 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004120 }
4121 }
Bob Leee5408332009-09-04 18:31:17 -07004122
Romain Guya998dff2012-03-23 18:58:36 -07004123 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07004124 if (managed) {
4125 try {
4126 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
4127 } catch (IOException e) {
4128 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
4129 + " -- can the process access this path?");
4130 } finally {
4131 try {
4132 dhd.fd.close();
4133 } catch (IOException e) {
4134 Slog.w(TAG, "Failure closing profile fd", e);
4135 }
4136 }
4137 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07004138 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07004139 }
4140 }
4141
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004142 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
4143 boolean hasPkgInfo = false;
4144 if (packages != null) {
4145 for (int i=packages.length-1; i>=0; i--) {
4146 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
4147 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004148 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004149 ref = mPackages.get(packages[i]);
4150 if (ref != null && ref.get() != null) {
4151 hasPkgInfo = true;
4152 } else {
4153 ref = mResourcePackages.get(packages[i]);
4154 if (ref != null && ref.get() != null) {
4155 hasPkgInfo = true;
4156 }
4157 }
4158 }
4159 mPackages.remove(packages[i]);
4160 mResourcePackages.remove(packages[i]);
4161 }
4162 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08004163 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004164 hasPkgInfo);
4165 }
4166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004167 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004168 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07004169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170 final int N = callbacks.size();
4171 for (int i=0; i<N; i++) {
4172 callbacks.get(i).onLowMemory();
4173 }
4174
Chris Tatece229052009-03-25 16:44:52 -07004175 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
4176 if (Process.myUid() != Process.SYSTEM_UID) {
4177 int sqliteReleased = SQLiteDatabase.releaseMemory();
4178 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
4179 }
Bob Leee5408332009-09-04 18:31:17 -07004180
Mike Reedcaf0df12009-04-27 14:32:05 -04004181 // Ask graphics to free up as much as possible (font/image caches)
4182 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004183
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004184 // Ask text layout engine to free also as much as possible
4185 Canvas.freeTextLayoutCaches();
4186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 BinderInternal.forceGc("mem");
4188 }
4189
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004190 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004191 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004192
Dianne Hackborn73c14162012-09-19 15:45:06 -07004193 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004194
4195 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07004196 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004197 callbacks.get(i).onTrimMemory(level);
4198 }
Romain Guy19f86e82012-04-23 15:19:07 -07004199
John Reckf47a5942014-06-30 16:20:04 -07004200 WindowManagerGlobal.getInstance().trimMemory(level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004201 }
4202
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004203 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004204 if (Process.isIsolated()) {
4205 // Isolated processes aren't going to do UI.
4206 return;
4207 }
Romain Guya9582652011-11-10 14:20:10 -08004208 try {
4209 int uid = Process.myUid();
4210 String[] packages = getPackageManager().getPackagesForUid(uid);
4211
4212 // If there are several packages in this application we won't
4213 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004214 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004215 HardwareRenderer.setupDiskCache(cacheDir);
4216 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08004217 }
4218 } catch (RemoteException e) {
4219 // Ignore
4220 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07004221 }
4222
4223 private void updateDefaultDensity() {
4224 if (mCurDefaultDisplayDpi != Configuration.DENSITY_DPI_UNDEFINED
4225 && mCurDefaultDisplayDpi != DisplayMetrics.DENSITY_DEVICE
4226 && !mDensityCompatMode) {
4227 Slog.i(TAG, "Switching default density from "
4228 + DisplayMetrics.DENSITY_DEVICE + " to "
4229 + mCurDefaultDisplayDpi);
4230 DisplayMetrics.DENSITY_DEVICE = mCurDefaultDisplayDpi;
4231 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4232 }
4233 }
4234
Romain Guy65b345f2011-07-27 18:51:50 -07004235 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004236 mBoundApplication = data;
4237 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004238 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004240 mProfiler = new Profiler();
4241 mProfiler.profileFile = data.initProfileFile;
4242 mProfiler.profileFd = data.initProfileFd;
4243 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
4244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08004246 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004247 android.ddm.DdmHandleAppName.setAppName(data.processName,
4248 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004249
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004250 if (data.persistent) {
4251 // Persistent processes on low-memory devices do not get to
4252 // use hardware accelerated drawing, since this can add too much
4253 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07004254 if (!ActivityManager.isHighEndGfx()) {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004255 HardwareRenderer.disable(false);
4256 }
4257 }
Romain Guya9582652011-11-10 14:20:10 -08004258
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004259 if (mProfiler.profileFd != null) {
4260 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004261 }
4262
Joe Onoratod630f102011-03-17 18:42:26 -07004263 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
4264 // implementation to use the pool executor. Normally, we use the
4265 // serialized executor as the default. This has to happen in the
4266 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07004267 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07004268 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
4269 }
4270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004271 /*
4272 * Before spawning a new process, reset the time zone to be the system time zone.
4273 * This needs to be done because the system time zone could have changed after the
4274 * the spawning of this process. Without doing this this process would have the incorrect
4275 * system time zone.
4276 */
4277 TimeZone.setDefault(null);
4278
4279 /*
4280 * Initialize the default locale in this process for the reasons we set the time zone.
4281 */
4282 Locale.setDefault(data.config.locale);
4283
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004284 /*
4285 * Update the system configuration since its preloaded and might not
4286 * reflect configuration changes. The configuration object passed
4287 * in AppBindData can be safely assumed to be up to date
4288 */
Craig Mautner88c05892013-06-28 09:47:45 -07004289 mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004290 mCurDefaultDisplayDpi = data.config.densityDpi;
4291 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004292
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004293 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004294
Dianne Hackborndde331c2012-08-03 14:01:57 -07004295 /**
4296 * Switch this process to density compatibility mode if needed.
4297 */
4298 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4299 == 0) {
4300 mDensityCompatMode = true;
4301 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4302 }
4303 updateDefaultDensity();
4304
Jeff Browndefd4a62014-03-10 21:24:37 -07004305 final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
Amith Yamasani11de39a2012-08-17 18:00:52 -07004306 if (!Process.isIsolated()) {
4307 final File cacheDir = appContext.getCacheDir();
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004308
Amith Yamasani92d57052012-08-23 10:07:52 -07004309 if (cacheDir != null) {
4310 // Provide a usable directory for temporary files
4311 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
4312
4313 setupGraphicsSupport(data.info, cacheDir);
4314 } else {
4315 Log.e(TAG, "Unable to setupGraphicsSupport due to missing cache directory");
4316 }
Amith Yamasani11de39a2012-08-17 18:00:52 -07004317 }
Narayan Kamathccb2a082013-12-19 14:49:36 +00004318
4319
4320 final boolean is24Hr = "24".equals(mCoreSettings.getString(Settings.System.TIME_12_24));
4321 DateFormat.set24HourTimePref(is24Hr);
4322
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004323 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004324 * For system applications on userdebug/eng builds, log stack
4325 * traces of disk and network access to dropbox for analysis.
4326 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004327 if ((data.appInfo.flags &
4328 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004329 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4330 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004331 }
4332
4333 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004334 * For apps targetting SDK Honeycomb or later, we don't allow
4335 * network usage on the main event loop / UI thread.
4336 *
4337 * Note to those grepping: this is what ultimately throws
4338 * NetworkOnMainThreadException ...
4339 */
4340 if (data.appInfo.targetSdkVersion > 9) {
4341 StrictMode.enableDeathOnNetwork();
4342 }
4343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004344 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4345 // XXX should have option to change the port.
4346 Debug.changeDebugPort(8100);
4347 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004348 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004349 + " is waiting for the debugger on port 8100...");
4350
4351 IActivityManager mgr = ActivityManagerNative.getDefault();
4352 try {
4353 mgr.showWaitingForDebugger(mAppThread, true);
4354 } catch (RemoteException ex) {
4355 }
4356
4357 Debug.waitForDebugger();
4358
4359 try {
4360 mgr.showWaitingForDebugger(mAppThread, false);
4361 } catch (RemoteException ex) {
4362 }
4363
4364 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004365 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004366 + " can be debugged on port 8100...");
4367 }
4368 }
4369
Siva Velusamy92a8b222012-03-09 16:24:04 -08004370 // Enable OpenGL tracing if required
4371 if (data.enableOpenGlTrace) {
Siva Velusamy0c1761b2012-12-14 17:09:06 -08004372 GLUtils.setTracingLevel(1);
Siva Velusamy92a8b222012-03-09 16:24:04 -08004373 }
4374
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07004375 // Allow application-generated systrace messages if we're debuggable.
4376 boolean appTracingAllowed = (data.appInfo.flags&ApplicationInfo.FLAG_DEBUGGABLE) != 0;
4377 Trace.setAppTracingAllowed(appTracingAllowed);
4378
Robert Greenwalt434203a2010-10-11 16:00:27 -07004379 /**
4380 * Initialize the default http proxy in this process for the reasons we set the time zone.
4381 */
4382 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004383 if (b != null) {
4384 // In pre-boot mode (doing initial launch to collect password), not
4385 // all system is up. This includes the connectivity service, so don't
4386 // crash if we can't get it.
4387 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4388 try {
Jason Monk207900c2014-04-25 15:00:09 -04004389 ProxyInfo proxyInfo = service.getProxy();
4390 Proxy.setHttpProxySystemProperty(proxyInfo);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004391 } catch (RemoteException e) {}
4392 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004394 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 InstrumentationInfo ii = null;
4396 try {
4397 ii = appContext.getPackageManager().
4398 getInstrumentationInfo(data.instrumentationName, 0);
4399 } catch (PackageManager.NameNotFoundException e) {
4400 }
4401 if (ii == null) {
4402 throw new RuntimeException(
4403 "Unable to find instrumentation info for: "
4404 + data.instrumentationName);
4405 }
4406
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004407 mInstrumentationPackageName = ii.packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 mInstrumentationAppDir = ii.sourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004409 mInstrumentationSplitAppDirs = ii.splitSourceDirs;
4410 mInstrumentationLibDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004411 mInstrumentedAppDir = data.info.getAppDir();
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004412 mInstrumentedSplitAppDirs = data.info.getSplitAppDirs();
4413 mInstrumentedLibDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414
4415 ApplicationInfo instrApp = new ApplicationInfo();
4416 instrApp.packageName = ii.packageName;
4417 instrApp.sourceDir = ii.sourceDir;
4418 instrApp.publicSourceDir = ii.publicSourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004419 instrApp.splitSourceDirs = ii.splitSourceDirs;
4420 instrApp.splitPublicSourceDirs = ii.splitPublicSourceDirs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004421 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004422 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004423 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07004424 appContext.getClassLoader(), false, true, false);
Jeff Browndefd4a62014-03-10 21:24:37 -07004425 ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004426
4427 try {
4428 java.lang.ClassLoader cl = instrContext.getClassLoader();
4429 mInstrumentation = (Instrumentation)
4430 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4431 } catch (Exception e) {
4432 throw new RuntimeException(
4433 "Unable to instantiate instrumentation "
4434 + data.instrumentationName + ": " + e.toString(), e);
4435 }
4436
4437 mInstrumentation.init(this, instrContext, appContext,
Svetoslav Ganov80943d82013-01-02 10:25:37 -08004438 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher,
4439 data.instrumentationUiAutomationConnection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004440
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004441 if (mProfiler.profileFile != null && !ii.handleProfiling
4442 && mProfiler.profileFd == null) {
4443 mProfiler.handlingProfiling = true;
4444 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004445 file.getParentFile().mkdirs();
4446 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4447 }
4448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004449 } else {
4450 mInstrumentation = new Instrumentation();
4451 }
4452
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004453 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004454 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004455 }
4456
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004457 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004458 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004459 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004460 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004461 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004462 // If the app is being launched for full backup or restore, bring it up in
4463 // a restricted environment with the base application class.
4464 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4465 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004466
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004467 // don't bring up providers in restricted mode; they may depend on the
4468 // app's custom Application class
4469 if (!data.restrictedBackupMode) {
4470 List<ProviderInfo> providers = data.providers;
4471 if (providers != null) {
4472 installContentProviders(app, providers);
4473 // For process that contains content providers, we want to
4474 // ensure that the JIT is enabled "at some point".
4475 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4476 }
4477 }
4478
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004479 // Do this after providers, since instrumentation tests generally start their
4480 // test thread at this point, and we don't want that racing.
4481 try {
4482 mInstrumentation.onCreate(data.instrumentationArgs);
4483 }
4484 catch (Exception e) {
4485 throw new RuntimeException(
4486 "Exception thrown in onCreate() of "
4487 + data.instrumentationName + ": " + e.toString(), e);
4488 }
4489
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004490 try {
4491 mInstrumentation.callApplicationOnCreate(app);
4492 } catch (Exception e) {
4493 if (!mInstrumentation.onException(app, e)) {
4494 throw new RuntimeException(
4495 "Unable to create application " + app.getClass().getName()
4496 + ": " + e.toString(), e);
4497 }
4498 }
4499 } finally {
4500 StrictMode.setThreadPolicy(savedPolicy);
4501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004502 }
4503
4504 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4505 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004506 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4507 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004508 Debug.stopMethodTracing();
4509 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004510 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004511 // + ", app thr: " + mAppThread);
4512 try {
4513 am.finishInstrumentation(mAppThread, resultCode, results);
4514 } catch (RemoteException ex) {
4515 }
4516 }
4517
Romain Guy65b345f2011-07-27 18:51:50 -07004518 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004519 Context context, List<ProviderInfo> providers) {
4520 final ArrayList<IActivityManager.ContentProviderHolder> results =
4521 new ArrayList<IActivityManager.ContentProviderHolder>();
4522
Romain Guya998dff2012-03-23 18:58:36 -07004523 for (ProviderInfo cpi : providers) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08004524 if (DEBUG_PROVIDER) {
4525 StringBuilder buf = new StringBuilder(128);
4526 buf.append("Pub ");
4527 buf.append(cpi.authority);
4528 buf.append(": ");
4529 buf.append(cpi.name);
4530 Log.i(TAG, buf.toString());
4531 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004532 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
4533 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
4534 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004535 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004536 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004537 }
4538 }
4539
4540 try {
4541 ActivityManagerNative.getDefault().publishContentProviders(
4542 getApplicationThread(), results);
4543 } catch (RemoteException ex) {
4544 }
4545 }
4546
Jeff Sharkey6d515712012-09-20 16:06:08 -07004547 public final IContentProvider acquireProvider(
4548 Context c, String auth, int userId, boolean stable) {
4549 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004550 if (provider != null) {
4551 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004552 }
4553
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004554 // There is a possible race here. Another thread may try to acquire
4555 // the same provider at the same time. When this happens, we want to ensure
4556 // that the first one wins.
4557 // Note that we cannot hold the lock while acquiring and installing the
4558 // provider since it might take a long time to run and it could also potentially
4559 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004560 IActivityManager.ContentProviderHolder holder = null;
4561 try {
4562 holder = ActivityManagerNative.getDefault().getContentProvider(
Jeff Sharkey6d515712012-09-20 16:06:08 -07004563 getApplicationThread(), auth, userId, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004564 } catch (RemoteException ex) {
4565 }
4566 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004567 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004568 return null;
4569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004570
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004571 // Install provider will increment the reference count for us, and break
4572 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004573 holder = installProvider(c, holder, holder.info,
4574 true /*noisy*/, holder.noReleaseNeeded, stable);
4575 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004576 }
4577
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004578 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
4579 if (stable) {
4580 prc.stableCount += 1;
4581 if (prc.stableCount == 1) {
4582 // We are acquiring a new stable reference on the provider.
4583 int unstableDelta;
4584 if (prc.removePending) {
4585 // We have a pending remove operation, which is holding the
4586 // last unstable reference. At this point we are converting
4587 // that unstable reference to our new stable reference.
4588 unstableDelta = -1;
4589 // Cancel the removal of the provider.
4590 if (DEBUG_PROVIDER) {
4591 Slog.v(TAG, "incProviderRef: stable "
4592 + "snatched provider from the jaws of death");
4593 }
4594 prc.removePending = false;
Guobin Zhang9e3e5262013-03-21 13:57:11 +08004595 // There is a race! It fails to remove the message, which
4596 // will be handled in completeRemoveProvider().
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004597 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4598 } else {
4599 unstableDelta = 0;
4600 }
4601 try {
4602 if (DEBUG_PROVIDER) {
4603 Slog.v(TAG, "incProviderRef Now stable - "
4604 + prc.holder.info.name + ": unstableDelta="
4605 + unstableDelta);
4606 }
4607 ActivityManagerNative.getDefault().refContentProvider(
4608 prc.holder.connection, 1, unstableDelta);
4609 } catch (RemoteException e) {
4610 //do nothing content provider object is dead any way
4611 }
4612 }
4613 } else {
4614 prc.unstableCount += 1;
4615 if (prc.unstableCount == 1) {
4616 // We are acquiring a new unstable reference on the provider.
4617 if (prc.removePending) {
4618 // Oh look, we actually have a remove pending for the
4619 // provider, which is still holding the last unstable
4620 // reference. We just need to cancel that to take new
4621 // ownership of the reference.
4622 if (DEBUG_PROVIDER) {
4623 Slog.v(TAG, "incProviderRef: unstable "
4624 + "snatched provider from the jaws of death");
4625 }
4626 prc.removePending = false;
4627 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4628 } else {
4629 // First unstable ref, increment our count in the
4630 // activity manager.
4631 try {
4632 if (DEBUG_PROVIDER) {
4633 Slog.v(TAG, "incProviderRef: Now unstable - "
4634 + prc.holder.info.name);
4635 }
4636 ActivityManagerNative.getDefault().refContentProvider(
4637 prc.holder.connection, 0, 1);
4638 } catch (RemoteException e) {
4639 //do nothing content provider object is dead any way
4640 }
4641 }
4642 }
4643 }
4644 }
4645
Jeff Sharkey6d515712012-09-20 16:06:08 -07004646 public final IContentProvider acquireExistingProvider(
4647 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004648 synchronized (mProviderMap) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004649 final ProviderKey key = new ProviderKey(auth, userId);
4650 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004651 if (pr == null) {
4652 return null;
4653 }
4654
4655 IContentProvider provider = pr.mProvider;
4656 IBinder jBinder = provider.asBinder();
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004657 if (!jBinder.isBinderAlive()) {
4658 // The hosting process of the provider has died; we can't
4659 // use this one.
4660 Log.i(TAG, "Acquiring provider " + auth + " for user " + userId
4661 + ": existing object's process dead");
4662 handleUnstableProviderDiedLocked(jBinder, true);
4663 return null;
4664 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004665
4666 // Only increment the ref count if we have one. If we don't then the
4667 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004668 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004669 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004670 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004671 }
4672 return provider;
4673 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004674 }
4675
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004676 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
4677 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004678 return false;
4679 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004681 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004682 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004683 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004684 if (prc == null) {
4685 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004686 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004687 }
4688
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004689 boolean lastRef = false;
4690 if (stable) {
4691 if (prc.stableCount == 0) {
4692 if (DEBUG_PROVIDER) Slog.v(TAG,
4693 "releaseProvider: stable ref count already 0, how?");
4694 return false;
4695 }
4696 prc.stableCount -= 1;
4697 if (prc.stableCount == 0) {
4698 // What we do at this point depends on whether there are
4699 // any unstable refs left: if there are, we just tell the
4700 // activity manager to decrement its stable count; if there
4701 // aren't, we need to enqueue this provider to be removed,
4702 // and convert to holding a single unstable ref while
4703 // doing so.
4704 lastRef = prc.unstableCount == 0;
4705 try {
4706 if (DEBUG_PROVIDER) {
4707 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
4708 + lastRef + " - " + prc.holder.info.name);
4709 }
4710 ActivityManagerNative.getDefault().refContentProvider(
4711 prc.holder.connection, -1, lastRef ? 1 : 0);
4712 } catch (RemoteException e) {
4713 //do nothing content provider object is dead any way
4714 }
4715 }
4716 } else {
4717 if (prc.unstableCount == 0) {
4718 if (DEBUG_PROVIDER) Slog.v(TAG,
4719 "releaseProvider: unstable ref count already 0, how?");
4720 return false;
4721 }
4722 prc.unstableCount -= 1;
4723 if (prc.unstableCount == 0) {
4724 // If this is the last reference, we need to enqueue
4725 // this provider to be removed instead of telling the
4726 // activity manager to remove it at this point.
4727 lastRef = prc.stableCount == 0;
4728 if (!lastRef) {
4729 try {
4730 if (DEBUG_PROVIDER) {
4731 Slog.v(TAG, "releaseProvider: No longer unstable - "
4732 + prc.holder.info.name);
4733 }
4734 ActivityManagerNative.getDefault().refContentProvider(
4735 prc.holder.connection, 0, -1);
4736 } catch (RemoteException e) {
4737 //do nothing content provider object is dead any way
4738 }
4739 }
4740 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004741 }
4742
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004743 if (lastRef) {
4744 if (!prc.removePending) {
4745 // Schedule the actual remove asynchronously, since we don't know the context
4746 // this will be called in.
4747 // TODO: it would be nice to post a delayed message, so
4748 // if we come back and need the same provider quickly
4749 // we will still have it available.
4750 if (DEBUG_PROVIDER) {
4751 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
4752 + prc.holder.info.name);
4753 }
4754 prc.removePending = true;
4755 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
4756 mH.sendMessage(msg);
4757 } else {
4758 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
4759 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004760 }
4761 return true;
4762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004763 }
4764
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004765 final void completeRemoveProvider(ProviderRefCount prc) {
4766 synchronized (mProviderMap) {
4767 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004768 // There was a race! Some other client managed to acquire
4769 // the provider before the removal was completed.
4770 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004771 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004772 + "provider still in use");
4773 return;
4774 }
4775
Guobin Zhang9e3e5262013-03-21 13:57:11 +08004776 // More complicated race!! Some client managed to acquire the
4777 // provider and release it before the removal was completed.
4778 // Continue the removal, and abort the next remove message.
4779 prc.removePending = false;
4780
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004781 final IBinder jBinder = prc.holder.provider.asBinder();
4782 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
4783 if (existingPrc == prc) {
4784 mProviderRefCountMap.remove(jBinder);
4785 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004786
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004787 for (int i=mProviderMap.size()-1; i>=0; i--) {
4788 ProviderClientRecord pr = mProviderMap.valueAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004789 IBinder myBinder = pr.mProvider.asBinder();
4790 if (myBinder == jBinder) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004791 mProviderMap.removeAt(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004792 }
4793 }
4794 }
4795
4796 try {
4797 if (DEBUG_PROVIDER) {
4798 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4799 + "removeContentProvider(" + prc.holder.info.name + ")");
4800 }
4801 ActivityManagerNative.getDefault().removeContentProvider(
4802 prc.holder.connection, false);
4803 } catch (RemoteException e) {
4804 //do nothing content provider object is dead any way
4805 }
4806 }
4807
4808 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004809 synchronized (mProviderMap) {
4810 handleUnstableProviderDiedLocked(provider, fromClient);
4811 }
4812 }
4813
4814 final void handleUnstableProviderDiedLocked(IBinder provider, boolean fromClient) {
4815 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4816 if (prc != null) {
4817 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
4818 + provider + " " + prc.holder.info.name);
4819 mProviderRefCountMap.remove(provider);
You Kimbc74de62013-10-01 00:13:26 +09004820 for (int i=mProviderMap.size()-1; i>=0; i--) {
4821 ProviderClientRecord pr = mProviderMap.valueAt(i);
4822 if (pr != null && pr.mProvider.asBinder() == provider) {
4823 Slog.i(TAG, "Removing dead content provider:" + pr.mProvider.toString());
4824 mProviderMap.removeAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004825 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004826 }
You Kimbc74de62013-10-01 00:13:26 +09004827
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004828 if (fromClient) {
4829 // We found out about this due to execution in our client
4830 // code. Tell the activity manager about it now, to ensure
4831 // that the next time we go to do anything with the provider
4832 // it knows it is dead (so we don't race with its death
4833 // notification).
4834 try {
4835 ActivityManagerNative.getDefault().unstableProviderDied(
4836 prc.holder.connection);
4837 } catch (RemoteException e) {
4838 //do nothing content provider object is dead any way
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004839 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004840 }
4841 }
4842 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004843
Jeff Sharkey7aa76012013-09-30 14:26:27 -07004844 final void appNotRespondingViaProvider(IBinder provider) {
4845 synchronized (mProviderMap) {
4846 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4847 if (prc != null) {
4848 try {
4849 ActivityManagerNative.getDefault()
4850 .appNotRespondingViaProvider(prc.holder.connection);
4851 } catch (RemoteException e) {
4852 }
4853 }
4854 }
4855 }
4856
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004857 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Jeff Sharkey6d515712012-09-20 16:06:08 -07004858 ContentProvider localProvider, IActivityManager.ContentProviderHolder holder) {
4859 final String auths[] = PATTERN_SEMICOLON.split(holder.info.authority);
4860 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
4861
4862 final ProviderClientRecord pcr = new ProviderClientRecord(
4863 auths, provider, localProvider, holder);
4864 for (String auth : auths) {
4865 final ProviderKey key = new ProviderKey(auth, userId);
4866 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004867 if (existing != null) {
4868 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07004869 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004870 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004871 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004872 }
4873 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004874 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004875 }
4876
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004877 /**
4878 * Installs the provider.
4879 *
4880 * Providers that are local to the process or that come from the system server
4881 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4882 * Other remote providers are reference counted. The initial reference count
4883 * for all reference counted providers is one. Providers that are not reference
4884 * counted do not have a reference count (at all).
4885 *
4886 * This method detects when a provider has already been installed. When this happens,
4887 * it increments the reference count of the existing provider (if appropriate)
4888 * and returns the existing provider. This can happen due to concurrent
4889 * attempts to acquire the same provider.
4890 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004891 private IActivityManager.ContentProviderHolder installProvider(Context context,
4892 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
4893 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004894 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004895 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07004896 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004897 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004898 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004899 + info.name);
4900 }
4901 Context c = null;
4902 ApplicationInfo ai = info.applicationInfo;
4903 if (context.getPackageName().equals(ai.packageName)) {
4904 c = context;
4905 } else if (mInitialApplication != null &&
4906 mInitialApplication.getPackageName().equals(ai.packageName)) {
4907 c = mInitialApplication;
4908 } else {
4909 try {
4910 c = context.createPackageContext(ai.packageName,
4911 Context.CONTEXT_INCLUDE_CODE);
4912 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004913 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004914 }
4915 }
4916 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004917 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004918 ai.packageName +
4919 " while loading content provider " +
4920 info.name);
4921 return null;
4922 }
4923 try {
4924 final java.lang.ClassLoader cl = c.getClassLoader();
4925 localProvider = (ContentProvider)cl.
4926 loadClass(info.name).newInstance();
4927 provider = localProvider.getIContentProvider();
4928 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004929 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004930 info.name + " from sourceDir " +
4931 info.applicationInfo.sourceDir);
4932 return null;
4933 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004934 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004935 TAG, "Instantiating local provider " + info.name);
4936 // XXX Need to create the correct context for this provider.
4937 localProvider.attachInfo(c, info);
4938 } catch (java.lang.Exception e) {
4939 if (!mInstrumentation.onException(null, e)) {
4940 throw new RuntimeException(
4941 "Unable to get provider " + info.name
4942 + ": " + e.toString(), e);
4943 }
4944 return null;
4945 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004946 } else {
4947 provider = holder.provider;
4948 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004949 + info.name);
4950 }
4951
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004952 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004953
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004954 synchronized (mProviderMap) {
4955 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
4956 + " / " + info.name);
4957 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004958 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004959 ComponentName cname = new ComponentName(info.packageName, info.name);
4960 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004961 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004962 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004963 Slog.v(TAG, "installProvider: lost the race, "
4964 + "using existing local provider");
4965 }
4966 provider = pr.mProvider;
4967 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004968 holder = new IActivityManager.ContentProviderHolder(info);
4969 holder.provider = provider;
4970 holder.noReleaseNeeded = true;
4971 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004972 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004973 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004974 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004975 retHolder = pr.mHolder;
4976 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004977 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4978 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004979 if (DEBUG_PROVIDER) {
4980 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004981 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004982 // We need to transfer our new reference to the existing
4983 // ref count, releasing the old one... but only if
4984 // release is needed (that is, it is not running in the
4985 // system process).
4986 if (!noReleaseNeeded) {
4987 incProviderRefLocked(prc, stable);
4988 try {
4989 ActivityManagerNative.getDefault().removeContentProvider(
4990 holder.connection, stable);
4991 } catch (RemoteException e) {
4992 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004993 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004994 }
4995 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004996 ProviderClientRecord client = installProviderAuthoritiesLocked(
4997 provider, localProvider, holder);
4998 if (noReleaseNeeded) {
4999 prc = new ProviderRefCount(holder, client, 1000, 1000);
5000 } else {
5001 prc = stable
5002 ? new ProviderRefCount(holder, client, 1, 0)
5003 : new ProviderRefCount(holder, client, 0, 1);
5004 }
5005 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005006 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005007 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005008 }
5009 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005010
5011 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005012 }
5013
Romain Guy65b345f2011-07-27 18:51:50 -07005014 private void attach(boolean system) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08005015 sCurrentActivityThread = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005016 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005017 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005018 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Craig Mautner88c05892013-06-28 09:47:45 -07005019 @Override
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08005020 public void run() {
5021 ensureJitEnabled();
5022 }
5023 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005024 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
5025 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005026 RuntimeInit.setApplicationObject(mAppThread.asBinder());
5027 IActivityManager mgr = ActivityManagerNative.getDefault();
5028 try {
5029 mgr.attachApplication(mAppThread);
5030 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07005031 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005032 }
5033 } else {
5034 // Don't set application object here -- if the system crashes,
5035 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005036 android.ddm.DdmHandleAppName.setAppName("system_process",
5037 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005038 try {
5039 mInstrumentation = new Instrumentation();
Jeff Browndefd4a62014-03-10 21:24:37 -07005040 ContextImpl context = ContextImpl.createAppContext(
5041 this, getSystemContext().mPackageInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 Application app = Instrumentation.newApplication(Application.class, context);
5043 mAllApplications.add(app);
5044 mInitialApplication = app;
5045 app.onCreate();
5046 } catch (Exception e) {
5047 throw new RuntimeException(
5048 "Unable to instantiate Application():" + e.toString(), e);
5049 }
5050 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07005051
5052 // add dropbox logging to libcore
5053 DropBox.setReporter(new DropBoxReporter());
5054
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005055 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Craig Mautner88c05892013-06-28 09:47:45 -07005056 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005057 public void onConfigurationChanged(Configuration newConfig) {
Craig Mautner88c05892013-06-28 09:47:45 -07005058 synchronized (mResourcesManager) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005059 // We need to apply this change to the resources
5060 // immediately, because upon returning the view
5061 // hierarchy will be informed about it.
Craig Mautner88c05892013-06-28 09:47:45 -07005062 if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005063 // This actually changed the resources! Tell
5064 // everyone about it.
5065 if (mPendingConfiguration == null ||
5066 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
5067 mPendingConfiguration = newConfig;
5068
Jeff Brown9ef09972013-10-15 20:49:59 -07005069 sendMessage(H.CONFIGURATION_CHANGED, newConfig);
Dianne Hackbornae078162010-03-18 11:29:37 -07005070 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005071 }
5072 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005073 }
Craig Mautner88c05892013-06-28 09:47:45 -07005074 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005075 public void onLowMemory() {
5076 }
Craig Mautner88c05892013-06-28 09:47:45 -07005077 @Override
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005078 public void onTrimMemory(int level) {
5079 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005080 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 }
5082
Romain Guy5e9120d2012-01-30 12:17:22 -08005083 public static ActivityThread systemMain() {
Alan Viverette5e1565e2014-07-29 16:14:25 -07005084 // The system process on low-memory devices do not get to use hardware
5085 // accelerated drawing, since this can add too much overhead to the
5086 // process.
5087 if (!ActivityManager.isHighEndGfx()) {
5088 HardwareRenderer.disable(true);
5089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005090 ActivityThread thread = new ActivityThread();
5091 thread.attach(true);
5092 return thread;
5093 }
5094
Jeff Brown10e89712011-07-08 18:52:57 -07005095 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07005097 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005098 }
5099 }
5100
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005101 public int getIntCoreSetting(String key, int defaultValue) {
Craig Mautner88c05892013-06-28 09:47:45 -07005102 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08005103 if (mCoreSettings != null) {
5104 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005105 }
Craig Mautner88c05892013-06-28 09:47:45 -07005106 return defaultValue;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005107 }
5108 }
5109
Geremy Condra69689a72012-09-11 16:57:17 -07005110 private static class EventLoggingReporter implements EventLogger.Reporter {
5111 @Override
5112 public void report (int code, Object... list) {
5113 EventLog.writeEvent(code, list);
5114 }
5115 }
5116
Geremy Condrab7faaf42012-09-19 18:07:42 -07005117 private class DropBoxReporter implements DropBox.Reporter {
5118
5119 private DropBoxManager dropBox;
5120
5121 public DropBoxReporter() {
5122 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
5123 }
5124
5125 @Override
5126 public void addData(String tag, byte[] data, int flags) {
5127 dropBox.addData(tag, data, flags);
5128 }
5129
5130 @Override
5131 public void addText(String tag, String data) {
5132 dropBox.addText(tag, data);
5133 }
5134 }
5135
Romain Guy65b345f2011-07-27 18:51:50 -07005136 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07005137 SamplingProfilerIntegration.start();
5138
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08005139 // CloseGuard defaults to true and can be quite spammy. We
5140 // disable it here, but selectively enable it later (via
5141 // StrictMode) on debug builds, but using DropBox, not logs.
5142 CloseGuard.setEnabled(false);
5143
Jeff Sharkeyb049e212012-09-07 23:16:01 -07005144 Environment.initForCurrentUser();
5145
Geremy Condra69689a72012-09-11 16:57:17 -07005146 // Set the reporter for event logging in libcore
5147 EventLogger.setReporter(new EventLoggingReporter());
5148
Kenny Root8b514752013-02-04 09:35:16 -08005149 Security.addProvider(new AndroidKeyStoreProvider());
5150
Robin Lee3d076af2014-04-25 14:57:49 +01005151 // Make sure TrustedCertificateStore looks in the right place for CA certificates
5152 final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
5153 TrustedCertificateStore.setDefaultUserDirectory(configDir);
5154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005155 Process.setArgV0("<pre-initialized>");
5156
5157 Looper.prepareMainLooper();
5158
5159 ActivityThread thread = new ActivityThread();
5160 thread.attach(false);
5161
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07005162 if (sMainThreadHandler == null) {
5163 sMainThreadHandler = thread.getHandler();
5164 }
5165
Romain Guy5e9120d2012-01-30 12:17:22 -08005166 AsyncTask.init();
5167
Dianne Hackborn287952c2010-09-22 22:34:31 -07005168 if (false) {
5169 Looper.myLooper().setMessageLogging(new
5170 LogPrinter(Log.DEBUG, "ActivityThread"));
5171 }
5172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005173 Looper.loop();
5174
Jeff Brown10e89712011-07-08 18:52:57 -07005175 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005176 }
5177}