blob: d9f9d61886d794c4d59e12e72554707ef3b9a8f3 [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;
49import android.net.ProxyProperties;
Romain Guya9582652011-11-10 14:20:10 -080050import android.opengl.GLUtils;
Joe Onoratod630f102011-03-17 18:42:26 -070051import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070052import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Bundle;
54import android.os.Debug;
Geremy Condrab7faaf42012-09-19 18:07:42 -070055import android.os.DropBoxManager;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070056import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.os.Handler;
58import android.os.IBinder;
59import android.os.Looper;
60import android.os.Message;
61import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070062import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Process;
64import android.os.RemoteException;
65import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070066import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.os.SystemClock;
Jeff Brownefd43bd2012-09-21 17:02:35 -070068import android.os.SystemProperties;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070069import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070070import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.util.AndroidRuntimeException;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070072import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.util.DisplayMetrics;
74import android.util.EventLog;
75import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070076import android.util.LogPrinter;
Jeff Brown6754ba22011-12-14 20:20:01 -080077import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080078import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070080import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.view.View;
82import android.view.ViewDebug;
83import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070084import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.view.Window;
86import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -070087import android.view.WindowManagerGlobal;
Jason Samsa6f338c2012-02-24 16:22:16 -080088import android.renderscript.RenderScript;
Kenny Root8b514752013-02-04 09:35:16 -080089import android.security.AndroidKeyStoreProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91import com.android.internal.os.BinderInternal;
92import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070093import com.android.internal.os.SamplingProfilerIntegration;
Dianne Hackborn8c841092013-06-24 13:46:13 -070094import com.android.internal.util.FastPrintWriter;
Jeff Sharkey6d515712012-09-20 16:06:08 -070095import com.android.internal.util.Objects;
Kenny Root12e75222013-04-23 22:34:24 -070096import com.android.org.conscrypt.OpenSSLSocketImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097
98import java.io.File;
99import java.io.FileDescriptor;
100import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700101import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import java.io.PrintWriter;
103import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700104import java.net.InetAddress;
Kenny Root8b514752013-02-04 09:35:16 -0800105import java.security.Security;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import java.util.List;
108import java.util.Locale;
109import java.util.Map;
110import java.util.TimeZone;
111import java.util.regex.Pattern;
112
Geremy Condrab7faaf42012-09-19 18:07:42 -0700113import libcore.io.DropBox;
Geremy Condra69689a72012-09-11 16:57:17 -0700114import libcore.io.EventLogger;
Jeff Sharkeye861b422012-03-01 20:59:22 -0800115import libcore.io.IoUtils;
116
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800117import dalvik.system.CloseGuard;
Bob Leee5408332009-09-04 18:31:17 -0700118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119final class SuperNotCalledException extends AndroidRuntimeException {
120 public SuperNotCalledException(String msg) {
121 super(msg);
122 }
123}
124
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700125final class RemoteServiceException extends AndroidRuntimeException {
126 public RemoteServiceException(String msg) {
127 super(msg);
128 }
129}
130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131/**
132 * This manages the execution of the main thread in an
133 * application process, scheduling and executing activities,
134 * broadcasts, and other operations on it as the activity
135 * manager requests.
136 *
137 * {@hide}
138 */
139public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700140 /** @hide */
141 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700142 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700143 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700144 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700145 /** @hide */
146 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700147 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700148 private static final boolean DEBUG_BACKUP = false;
Craig Mautner88c05892013-06-28 09:47:45 -0700149 public static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800150 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700151 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700152 private static final boolean DEBUG_PROVIDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
154 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
155 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
156 private static final int LOG_ON_PAUSE_CALLED = 30021;
157 private static final int LOG_ON_RESUME_CALLED = 30022;
158
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700159 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700160
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700161 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700163 final ApplicationThread mAppThread = new ApplicationThread();
164 final Looper mLooper = Looper.myLooper();
165 final H mH = new H();
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700166 final ArrayMap<IBinder, ActivityClientRecord> mActivities
167 = new ArrayMap<IBinder, ActivityClientRecord>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700168 // List of new activities (via ActivityRecord.nextIdle) that should
169 // be reported when next we idle.
170 ActivityClientRecord mNewActivities = null;
171 // Number of activities that are currently visible on-screen.
172 int mNumVisibleActivities = 0;
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700173 final ArrayMap<IBinder, Service> mServices
174 = new ArrayMap<IBinder, Service>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700175 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700176 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700177 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700178 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700179 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700180 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700181 Application mInitialApplication;
182 final ArrayList<Application> mAllApplications
183 = new ArrayList<Application>();
184 // set of instantiated backup agents, keyed by package name
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700185 final ArrayMap<String, BackupAgent> mBackupAgents = new ArrayMap<String, BackupAgent>();
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800186 /** Reference to singleton {@link ActivityThread} */
187 private static ActivityThread sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700188 Instrumentation mInstrumentation;
189 String mInstrumentationAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700190 String mInstrumentationAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700191 String mInstrumentationAppPackage = null;
192 String mInstrumentedAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700193 String mInstrumentedAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700194 boolean mSystemThread = false;
195 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700197 // These can be accessed by multiple threads; mPackages is the lock.
198 // XXX For now we keep around information about all packages we have
199 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800200 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700201 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800202 // which means this lock gets held while the activity and window managers
203 // holds their own lock. Thus you MUST NEVER call back into the activity manager
204 // or window manager or anything that depends on them while holding this lock.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700205 final ArrayMap<String, WeakReference<LoadedApk>> mPackages
206 = new ArrayMap<String, WeakReference<LoadedApk>>();
207 final ArrayMap<String, WeakReference<LoadedApk>> mResourcePackages
208 = new ArrayMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700209 final ArrayList<ActivityClientRecord> mRelaunchingActivities
210 = new ArrayList<ActivityClientRecord>();
211 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212
Craig Mautner88c05892013-06-28 09:47:45 -0700213 private final ResourcesManager mResourcesManager;
214
Jeff Sharkey6d515712012-09-20 16:06:08 -0700215 private static final class ProviderKey {
216 final String authority;
217 final int userId;
218
219 public ProviderKey(String authority, int userId) {
220 this.authority = authority;
221 this.userId = userId;
222 }
223
224 @Override
225 public boolean equals(Object o) {
226 if (o instanceof ProviderKey) {
227 final ProviderKey other = (ProviderKey) o;
228 return Objects.equal(authority, other.authority) && userId == other.userId;
229 }
230 return false;
231 }
232
233 @Override
234 public int hashCode() {
235 return ((authority != null) ? authority.hashCode() : 0) ^ userId;
236 }
237 }
238
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700239 // The lock of mProviderMap protects the following variables.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700240 final ArrayMap<ProviderKey, ProviderClientRecord> mProviderMap
241 = new ArrayMap<ProviderKey, ProviderClientRecord>();
242 final ArrayMap<IBinder, ProviderRefCount> mProviderRefCountMap
243 = new ArrayMap<IBinder, ProviderRefCount>();
244 final ArrayMap<IBinder, ProviderClientRecord> mLocalProviders
245 = new ArrayMap<IBinder, ProviderClientRecord>();
246 final ArrayMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
247 = new ArrayMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700249 final ArrayMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
250 = new ArrayMap<Activity, ArrayList<OnActivityPausedListener>>();
Jeff Hamilton52d32032011-01-08 15:31:26 -0600251
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700252 final GcIdler mGcIdler = new GcIdler();
253 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700255 static Handler sMainThreadHandler; // set once in main()
256
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800257 Bundle mCoreSettings = null;
258
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400259 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700261 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 Intent intent;
263 Bundle state;
264 Activity activity;
265 Window window;
266 Activity parent;
267 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700268 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 boolean paused;
270 boolean stopped;
271 boolean hideForNow;
272 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700273 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700274 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700276 String profileFile;
277 ParcelFileDescriptor profileFd;
278 boolean autoStopProfiler;
279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400281 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700282 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283
284 List<ResultInfo> pendingResults;
285 List<Intent> pendingIntents;
286
287 boolean startsNotResumed;
288 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800289 int pendingConfigChanges;
290 boolean onlyLocalRequest;
291
292 View mPendingRemoveWindow;
293 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700295 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 parent = null;
297 embeddedID = null;
298 paused = false;
299 stopped = false;
300 hideForNow = false;
301 nextIdle = null;
302 }
303
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800304 public boolean isPreHoneycomb() {
305 if (activity != null) {
306 return activity.getApplicationInfo().targetSdkVersion
307 < android.os.Build.VERSION_CODES.HONEYCOMB;
308 }
309 return false;
310 }
311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700313 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 return "ActivityRecord{"
315 + Integer.toHexString(System.identityHashCode(this))
316 + " token=" + token + " " + (componentName == null
317 ? "no component name" : componentName.toShortString())
318 + "}";
319 }
320 }
321
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700322 final class ProviderClientRecord {
323 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 final IContentProvider mProvider;
325 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700326 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700328 ProviderClientRecord(String[] names, IContentProvider provider,
329 ContentProvider localProvider,
330 IActivityManager.ContentProviderHolder holder) {
331 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 mProvider = provider;
333 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700334 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 }
336 }
337
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400338 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 List<Intent> intents;
340 IBinder token;
341 public String toString() {
342 return "NewIntentData{intents=" + intents + " token=" + token + "}";
343 }
344 }
345
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400346 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700347 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700348 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
349 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
350 token, sendingUser);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700351 this.intent = intent;
352 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 Intent intent;
355 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400356 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 public String toString() {
358 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700359 info.packageName + " resultCode=" + getResultCode()
360 + " resultData=" + getResultData() + " resultExtras="
361 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 }
363 }
364
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400365 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700366 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400367 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700368 int backupMode;
369 public String toString() {
370 return "CreateBackupAgentData{appInfo=" + appInfo
371 + " backupAgent=" + appInfo.backupAgentName
372 + " mode=" + backupMode + "}";
373 }
374 }
Bob Leee5408332009-09-04 18:31:17 -0700375
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400376 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 IBinder token;
378 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400379 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 Intent intent;
381 public String toString() {
382 return "CreateServiceData{token=" + token + " className="
383 + info.name + " packageName=" + info.packageName
384 + " intent=" + intent + "}";
385 }
386 }
387
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400388 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 IBinder token;
390 Intent intent;
391 boolean rebind;
392 public String toString() {
393 return "BindServiceData{token=" + token + " intent=" + intent + "}";
394 }
395 }
396
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400397 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700399 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700401 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 Intent args;
403 public String toString() {
404 return "ServiceArgsData{token=" + token + " startId=" + startId
405 + " args=" + args + "}";
406 }
407 }
408
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400409 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700410 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 String processName;
412 ApplicationInfo appInfo;
413 List<ProviderInfo> providers;
414 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 Bundle instrumentationArgs;
416 IInstrumentationWatcher instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800417 IUiAutomationConnection instrumentationUiAutomationConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 int debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800419 boolean enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700420 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700421 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400423 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700424
425 /** Initial values for {@link Profiler}. */
426 String initProfileFile;
427 ParcelFileDescriptor initProfileFd;
428 boolean initAutoStopProfiler;
429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 public String toString() {
431 return "AppBindData{appInfo=" + appInfo + "}";
432 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700433 }
434
435 static final class Profiler {
436 String profileFile;
437 ParcelFileDescriptor profileFd;
438 boolean autoStopProfiler;
439 boolean profiling;
440 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700441 public void setProfiler(String file, ParcelFileDescriptor fd) {
442 if (profiling) {
443 if (fd != null) {
444 try {
445 fd.close();
446 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700447 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700448 }
449 }
450 return;
451 }
452 if (profileFd != null) {
453 try {
454 profileFd.close();
455 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700456 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700457 }
458 }
459 profileFile = file;
460 profileFd = fd;
461 }
462 public void startProfiling() {
463 if (profileFd == null || profiling) {
464 return;
465 }
466 try {
467 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
468 8 * 1024 * 1024, 0);
469 profiling = true;
470 } catch (RuntimeException e) {
471 Slog.w(TAG, "Profiling failed on path " + profileFile);
472 try {
473 profileFd.close();
474 profileFd = null;
475 } catch (IOException e2) {
476 Slog.w(TAG, "Failure closing profile fd", e2);
477 }
478 }
479 }
480 public void stopProfiling() {
481 if (profiling) {
482 profiling = false;
483 Debug.stopMethodTracing();
484 if (profileFd != null) {
485 try {
486 profileFd.close();
487 } catch (IOException e) {
488 }
489 }
490 profileFd = null;
491 profileFile = null;
492 }
493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 }
495
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400496 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700497 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700498 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800499 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 }
502
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400503 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 IBinder token;
505 List<ResultInfo> results;
506 public String toString() {
507 return "ResultData{token=" + token + " results" + results + "}";
508 }
509 }
510
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400511 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800512 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 String what;
514 String who;
515 }
516
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400517 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700518 String path;
519 ParcelFileDescriptor fd;
520 }
521
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400522 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700523 String path;
524 ParcelFileDescriptor fd;
525 }
526
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400527 static final class UpdateCompatibilityData {
528 String pkg;
529 CompatibilityInfo info;
530 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800531
532 static final class RequestActivityExtras {
533 IBinder activityToken;
534 IBinder requestToken;
535 int requestType;
536 }
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700537
Romain Guy65b345f2011-07-27 18:51:50 -0700538 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700539
Romain Guy65b345f2011-07-27 18:51:50 -0700540 private class ApplicationThread extends ApplicationThreadNative {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700541 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s %8s %8s %8s";
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700542 private static final String ONE_COUNT_COLUMN = "%21s %8d";
543 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700544 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 // Formatting for checkin service - update version if row format changes
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700547 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 3;
Bob Leee5408332009-09-04 18:31:17 -0700548
Dianne Hackborna413dc02013-07-12 12:02:55 -0700549 private int mLastProcessState = -1;
550
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700551 private void updatePendingConfiguration(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -0700552 synchronized (mResourcesManager) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700553 if (mPendingConfiguration == null ||
554 mPendingConfiguration.isOtherSeqNewer(config)) {
555 mPendingConfiguration = config;
556 }
557 }
558 }
559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 public final void schedulePauseActivity(IBinder token, boolean finished,
561 boolean userLeaving, int configChanges) {
562 queueOrSendMessage(
563 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
564 token,
565 (userLeaving ? 1 : 0),
566 configChanges);
567 }
568
569 public final void scheduleStopActivity(IBinder token, boolean showWindow,
570 int configChanges) {
571 queueOrSendMessage(
572 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
573 token, 0, configChanges);
574 }
575
576 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
577 queueOrSendMessage(
578 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
579 token);
580 }
581
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800582 public final void scheduleSleeping(IBinder token, boolean sleeping) {
583 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
584 }
585
Dianne Hackborna413dc02013-07-12 12:02:55 -0700586 public final void scheduleResumeActivity(IBinder token, int processState,
587 boolean isForward) {
588 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
590 }
591
592 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
593 ResultData res = new ResultData();
594 res.token = token;
595 res.results = results;
596 queueOrSendMessage(H.SEND_RESULT, res);
597 }
598
599 // we use token to identify this activity without having to send the
600 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700601 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700602 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700603 int procState, Bundle state, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700604 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
605 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700606
607 updateProcessState(procState, false);
608
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700609 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610
611 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700612 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 r.intent = intent;
614 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400615 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 r.state = state;
617
618 r.pendingResults = pendingResults;
619 r.pendingIntents = pendingNewIntents;
620
621 r.startsNotResumed = notResumed;
622 r.isForward = isForward;
623
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700624 r.profileFile = profileName;
625 r.profileFd = profileFd;
626 r.autoStopProfiler = autoStopProfiler;
627
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700628 updatePendingConfiguration(curConfig);
629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
631 }
632
633 public final void scheduleRelaunchActivity(IBinder token,
634 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800635 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800636 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
637 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 }
639
640 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
641 NewIntentData data = new NewIntentData();
642 data.intents = intents;
643 data.token = token;
644
645 queueOrSendMessage(H.NEW_INTENT, data);
646 }
647
648 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
649 int configChanges) {
650 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
651 configChanges);
652 }
653
654 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400655 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700656 boolean sync, int sendingUser, int processState) {
657 updateProcessState(processState, false);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700658 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700659 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400661 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 queueOrSendMessage(H.RECEIVER, r);
663 }
664
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400665 public final void scheduleCreateBackupAgent(ApplicationInfo app,
666 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700667 CreateBackupAgentData d = new CreateBackupAgentData();
668 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400669 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700670 d.backupMode = backupMode;
671
672 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
673 }
674
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400675 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
676 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700677 CreateBackupAgentData d = new CreateBackupAgentData();
678 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400679 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700680
681 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
682 }
683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 public final void scheduleCreateService(IBinder token,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700685 ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
686 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 CreateServiceData s = new CreateServiceData();
688 s.token = token;
689 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400690 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691
692 queueOrSendMessage(H.CREATE_SERVICE, s);
693 }
694
695 public final void scheduleBindService(IBinder token, Intent intent,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700696 boolean rebind, int processState) {
697 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 BindServiceData s = new BindServiceData();
699 s.token = token;
700 s.intent = intent;
701 s.rebind = rebind;
702
Amith Yamasani742a6712011-05-04 14:49:28 -0700703 if (DEBUG_SERVICE)
704 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
705 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 queueOrSendMessage(H.BIND_SERVICE, s);
707 }
708
709 public final void scheduleUnbindService(IBinder token, Intent intent) {
710 BindServiceData s = new BindServiceData();
711 s.token = token;
712 s.intent = intent;
713
714 queueOrSendMessage(H.UNBIND_SERVICE, s);
715 }
716
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700717 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700718 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 ServiceArgsData s = new ServiceArgsData();
720 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700721 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700723 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 s.args = args;
725
726 queueOrSendMessage(H.SERVICE_ARGS, s);
727 }
728
729 public final void scheduleStopService(IBinder token) {
730 queueOrSendMessage(H.STOP_SERVICE, token);
731 }
732
733 public final void bindApplication(String processName,
734 ApplicationInfo appInfo, List<ProviderInfo> providers,
735 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700736 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800738 IUiAutomationConnection instrumentationUiConnection, int debugMode,
739 boolean enableOpenGlTrace, boolean isRestrictedBackupMode, boolean persistent,
740 Configuration config, CompatibilityInfo compatInfo, Map<String, IBinder> services,
741 Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742
743 if (services != null) {
744 // Setup the service cache in the ServiceManager
745 ServiceManager.initServiceCache(services);
746 }
747
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800748 setCoreSettings(coreSettings);
749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 AppBindData data = new AppBindData();
751 data.processName = processName;
752 data.appInfo = appInfo;
753 data.providers = providers;
754 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 data.instrumentationArgs = instrumentationArgs;
756 data.instrumentationWatcher = instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800757 data.instrumentationUiAutomationConnection = instrumentationUiConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 data.debugMode = debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800759 data.enableOpenGlTrace = enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700760 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700761 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400763 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700764 data.initProfileFile = profileFile;
765 data.initProfileFd = profileFd;
766 data.initAutoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 queueOrSendMessage(H.BIND_APPLICATION, data);
768 }
769
770 public final void scheduleExit() {
771 queueOrSendMessage(H.EXIT_APPLICATION, null);
772 }
773
Christopher Tate5e1ab332009-09-01 20:32:49 -0700774 public final void scheduleSuicide() {
775 queueOrSendMessage(H.SUICIDE, null);
776 }
777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 public void requestThumbnail(IBinder token) {
779 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
780 }
781
782 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700783 updatePendingConfiguration(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
785 }
786
787 public void updateTimeZone() {
788 TimeZone.setDefault(null);
789 }
790
Robert Greenwalt03595d02010-11-02 14:08:23 -0700791 public void clearDnsCache() {
792 // a non-standard API to get this to libcore
793 InetAddress.clearDnsCache();
794 }
795
Robert Greenwalt434203a2010-10-11 16:00:27 -0700796 public void setHttpProxy(String host, String port, String exclList) {
797 Proxy.setHttpProxySystemProperty(host, port, exclList);
798 }
799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 public void processInBackground() {
801 mH.removeMessages(H.GC_WHEN_IDLE);
802 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
803 }
804
805 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700806 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700807 try {
808 data.fd = ParcelFileDescriptor.dup(fd);
809 data.token = servicetoken;
810 data.args = args;
811 queueOrSendMessage(H.DUMP_SERVICE, data);
812 } catch (IOException e) {
813 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 }
815 }
816
817 // This function exists to make sure all receiver dispatching is
818 // correctly ordered, since these are one-way calls and the binder driver
819 // applies transaction ordering per object for such calls.
820 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700821 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700822 boolean sticky, int sendingUser, int processState) throws RemoteException {
823 updateProcessState(processState, false);
Dianne Hackborn20e80982012-08-31 19:00:44 -0700824 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
825 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 }
Bob Leee5408332009-09-04 18:31:17 -0700827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 public void scheduleLowMemory() {
829 queueOrSendMessage(H.LOW_MEMORY, null);
830 }
831
832 public void scheduleActivityConfigurationChanged(IBinder token) {
833 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
834 }
835
Romain Guy7eabe552011-07-21 14:56:34 -0700836 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
837 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700838 ProfilerControlData pcd = new ProfilerControlData();
839 pcd.path = path;
840 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700841 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800842 }
843
Andy McFadden824c5102010-07-09 16:26:57 -0700844 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
845 DumpHeapData dhd = new DumpHeapData();
846 dhd.path = path;
847 dhd.fd = fd;
848 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
849 }
850
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700851 public void setSchedulingGroup(int group) {
852 // Note: do this immediately, since going into the foreground
853 // should happen regardless of what pending work we have to do
854 // and the activity manager will wait for us to report back that
855 // we are done before sending us to the background.
856 try {
857 Process.setProcessGroup(Process.myPid(), group);
858 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800859 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700860 }
861 }
Bob Leee5408332009-09-04 18:31:17 -0700862
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700863 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
864 Debug.getMemoryInfo(outInfo);
865 }
Bob Leee5408332009-09-04 18:31:17 -0700866
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700867 public void dispatchPackageBroadcast(int cmd, String[] packages) {
868 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
869 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700870
871 public void scheduleCrash(String msg) {
872 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
873 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700874
Dianne Hackborn30d71892010-12-11 10:37:55 -0800875 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
876 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700877 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700878 try {
879 data.fd = ParcelFileDescriptor.dup(fd);
880 data.token = activitytoken;
881 data.prefix = prefix;
882 data.args = args;
883 queueOrSendMessage(H.DUMP_ACTIVITY, data);
884 } catch (IOException e) {
885 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700886 }
887 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700888
Marco Nelissen18cb2872011-11-15 11:19:53 -0800889 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
890 String[] args) {
891 DumpComponentInfo data = new DumpComponentInfo();
892 try {
893 data.fd = ParcelFileDescriptor.dup(fd);
894 data.token = providertoken;
895 data.args = args;
896 queueOrSendMessage(H.DUMP_PROVIDER, data);
897 } catch (IOException e) {
898 Slog.w(TAG, "dumpProvider failed", e);
899 }
900 }
901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700903 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
Dianne Hackborn64770d12013-05-23 17:51:19 -0700904 boolean dumpInfo, boolean dumpDalvik, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700905 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700906 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700907 try {
Dianne Hackborn64770d12013-05-23 17:51:19 -0700908 return dumpMemInfo(pw, checkin, dumpInfo, dumpDalvik);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700909 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700910 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700911 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700912 }
913
Dianne Hackborn64770d12013-05-23 17:51:19 -0700914 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean dumpInfo,
915 boolean dumpDalvik) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 long nativeMax = Debug.getNativeHeapSize() / 1024;
917 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
918 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
919
920 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
921 Debug.getMemoryInfo(memInfo);
922
Dianne Hackborn64770d12013-05-23 17:51:19 -0700923 if (!dumpInfo) {
Dianne Hackbornb437e092011-08-05 17:50:29 -0700924 return memInfo;
925 }
926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 Runtime runtime = Runtime.getRuntime();
928
929 long dalvikMax = runtime.totalMemory() / 1024;
930 long dalvikFree = runtime.freeMemory() / 1024;
931 long dalvikAllocated = dalvikMax - dalvikFree;
932 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700933 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700934 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
935 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 int globalAssetCount = AssetManager.getGlobalAssetCount();
937 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
938 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
939 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
940 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700941 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800942 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700945 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 // NOTE: if you change anything significant below, also consider changing
947 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700948 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 // Header
952 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
953 pw.print(Process.myPid()); pw.print(',');
954 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 // Heap info - max
957 pw.print(nativeMax); pw.print(',');
958 pw.print(dalvikMax); pw.print(',');
959 pw.print("N/A,");
960 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 // Heap info - allocated
963 pw.print(nativeAllocated); pw.print(',');
964 pw.print(dalvikAllocated); pw.print(',');
965 pw.print("N/A,");
966 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 // Heap info - free
969 pw.print(nativeFree); pw.print(',');
970 pw.print(dalvikFree); pw.print(',');
971 pw.print("N/A,");
972 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 // Heap info - proportional set size
975 pw.print(memInfo.nativePss); pw.print(',');
976 pw.print(memInfo.dalvikPss); pw.print(',');
977 pw.print(memInfo.otherPss); pw.print(',');
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700978 pw.print(memInfo.getTotalPss()); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700979
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700980 // Heap info - swappable set size
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700981 pw.print(memInfo.nativeSwappablePss); pw.print(',');
982 pw.print(memInfo.dalvikSwappablePss); pw.print(',');
983 pw.print(memInfo.otherSwappablePss); pw.print(',');
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700984 pw.print(memInfo.getTotalSwappablePss()); pw.print(',');
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700985
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700986 // Heap info - shared dirty
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700987 pw.print(memInfo.nativeSharedDirty); pw.print(',');
988 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
989 pw.print(memInfo.otherSharedDirty); pw.print(',');
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700990 pw.print(memInfo.getTotalSharedDirty()); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700991
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700992 // Heap info - shared clean
993 pw.print(memInfo.nativeSharedClean); pw.print(',');
994 pw.print(memInfo.dalvikSharedClean); pw.print(',');
995 pw.print(memInfo.otherSharedClean); pw.print(',');
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700996 pw.print(memInfo.getTotalSharedClean()); pw.print(',');
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700997
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700998 // Heap info - private Dirty
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700999 pw.print(memInfo.nativePrivateDirty); pw.print(',');
1000 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
1001 pw.print(memInfo.otherPrivateDirty); pw.print(',');
Dianne Hackborncfc837f2013-06-27 18:32:07 -07001002 pw.print(memInfo.getTotalPrivateDirty()); pw.print(',');
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -07001003
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001004 // Heap info - private Clean
1005 pw.print(memInfo.nativePrivateClean); pw.print(',');
1006 pw.print(memInfo.dalvikPrivateClean); pw.print(',');
1007 pw.print(memInfo.otherPrivateClean); pw.print(',');
Dianne Hackborncfc837f2013-06-27 18:32:07 -07001008 pw.print(memInfo.getTotalPrivateClean()); pw.print(',');
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001009
Dianne Hackborncfc837f2013-06-27 18:32:07 -07001010 // Heap info - other areas
1011 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
1012 pw.print(Debug.MemoryInfo.getOtherLabel(i)); pw.print(',');
1013 pw.print(memInfo.getOtherPss(i)); pw.print(',');
1014 pw.print(memInfo.getOtherSwappablePss(i)); pw.print(',');
1015 pw.print(memInfo.getOtherSharedDirty(i)); pw.print(',');
1016 pw.print(memInfo.getOtherSharedClean(i)); pw.print(',');
1017 pw.print(memInfo.getOtherPrivateDirty(i)); pw.print(',');
1018 pw.print(memInfo.getOtherPrivateClean(i)); pw.print(',');
1019 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 // Object counts
1022 pw.print(viewInstanceCount); pw.print(',');
1023 pw.print(viewRootInstanceCount); pw.print(',');
1024 pw.print(appContextInstanceCount); pw.print(',');
1025 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 pw.print(globalAssetCount); pw.print(',');
1028 pw.print(globalAssetManagerCount); pw.print(',');
1029 pw.print(binderLocalObjectCount); pw.print(',');
1030 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 pw.print(binderDeathObjectCount); pw.print(',');
1033 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 // SQL
Vasu Noric3849202010-03-09 10:47:25 -08001036 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -08001037 pw.print(stats.memoryUsed / 1024); pw.print(',');
1038 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -07001039 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001040 for (int i = 0; i < stats.dbStats.size(); i++) {
1041 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -07001042 pw.print(','); pw.print(dbStats.dbName);
1043 pw.print(','); pw.print(dbStats.pageSize);
1044 pw.print(','); pw.print(dbStats.dbSize);
1045 pw.print(','); pw.print(dbStats.lookaside);
1046 pw.print(','); pw.print(dbStats.cache);
1047 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -08001048 }
Dianne Hackbornb437e092011-08-05 17:50:29 -07001049 pw.println();
Bob Leee5408332009-09-04 18:31:17 -07001050
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001051 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 }
Bob Leee5408332009-09-04 18:31:17 -07001053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 // otherwise, show human-readable format
Dianne Hackborn64770d12013-05-23 17:51:19 -07001055 printRow(pw, HEAP_COLUMN, "", "Pss", "Pss","Shared", "Private", "Shared", "Private",
1056 "Heap", "Heap", "Heap");
1057 printRow(pw, HEAP_COLUMN, "", "Total", "Clean", "Dirty", "Dirty", "Clean", "Clean",
1058 "Size", "Alloc", "Free");
1059 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
1060 "------", "------", "------", "------");
1061 printRow(pw, HEAP_COLUMN, "Native Heap", memInfo.nativePss, memInfo.nativeSwappablePss,
1062 memInfo.nativeSharedDirty,
1063 memInfo.nativePrivateDirty, memInfo.nativeSharedClean,
1064 memInfo.nativePrivateClean, nativeMax, nativeAllocated, nativeFree);
1065 printRow(pw, HEAP_COLUMN, "Dalvik Heap", memInfo.dalvikPss, memInfo.dalvikSwappablePss,
1066 memInfo.dalvikSharedDirty,
1067 memInfo.dalvikPrivateDirty, memInfo.dalvikSharedClean,
1068 memInfo.dalvikPrivateClean, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001070 int otherPss = memInfo.otherPss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001071 int otherSwappablePss = memInfo.otherSwappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001072 int otherSharedDirty = memInfo.otherSharedDirty;
1073 int otherPrivateDirty = memInfo.otherPrivateDirty;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -07001074 int otherSharedClean = memInfo.otherSharedClean;
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001075 int otherPrivateClean = memInfo.otherPrivateClean;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001077 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Dianne Hackborn64770d12013-05-23 17:51:19 -07001078 final int myPss = memInfo.getOtherPss(i);
1079 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
1080 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
1081 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
1082 final int mySharedClean = memInfo.getOtherSharedClean(i);
1083 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
1084 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
1085 || mySharedClean != 0 || myPrivateClean != 0) {
1086 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
1087 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
1088 mySharedClean, myPrivateClean, "", "", "");
1089 otherPss -= myPss;
1090 otherSwappablePss -= mySwappablePss;
1091 otherSharedDirty -= mySharedDirty;
1092 otherPrivateDirty -= myPrivateDirty;
1093 otherSharedClean -= mySharedClean;
1094 otherPrivateClean -= myPrivateClean;
1095 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001096 }
1097
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001098
1099
1100 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSwappablePss, otherSharedDirty,
1101 otherPrivateDirty, otherSharedClean, otherPrivateClean,"", "", "");
Dianne Hackborn64770d12013-05-23 17:51:19 -07001102 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
1103 memInfo.getTotalSwappablePss(),
1104 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
1105 memInfo.getTotalSharedClean(), memInfo.getTotalPrivateClean(),
1106 nativeMax+dalvikMax,
1107 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001108
Dianne Hackborn64770d12013-05-23 17:51:19 -07001109 if (dumpDalvik) {
1110 pw.println(" ");
1111 pw.println(" Dalvik Details");
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001112
Dianne Hackborn64770d12013-05-23 17:51:19 -07001113 for (int i=Debug.MemoryInfo.NUM_OTHER_STATS;
1114 i<Debug.MemoryInfo.NUM_OTHER_STATS + Debug.MemoryInfo.NUM_DVK_STATS; i++) {
1115 final int myPss = memInfo.getOtherPss(i);
1116 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
1117 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
1118 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
1119 final int mySharedClean = memInfo.getOtherSharedClean(i);
1120 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
1121 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
1122 || mySharedClean != 0 || myPrivateClean != 0) {
1123 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
1124 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
1125 mySharedClean, myPrivateClean, "", "", "");
1126 }
1127 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129
1130 pw.println(" ");
1131 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001132 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 viewRootInstanceCount);
1134
1135 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1136 "Activities:", activityInstanceCount);
1137
1138 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1139 "AssetManagers:", globalAssetManagerCount);
1140
1141 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1142 "Proxy Binders:", binderProxyObjectCount);
1143 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
1144
1145 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 // SQLite mem info
1148 pw.println(" ");
1149 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001150 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1151 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1152 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001153 pw.println(" ");
1154 int N = stats.dbStats.size();
1155 if (N > 0) {
1156 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001157 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1158 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001159 for (int i = 0; i < N; i++) {
1160 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001161 printRow(pw, DB_INFO_FORMAT,
1162 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1163 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1164 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1165 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001166 }
1167 }
Bob Leee5408332009-09-04 18:31:17 -07001168
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001169 // Asset details.
1170 String assetAlloc = AssetManager.getAssetAllocations();
1171 if (assetAlloc != null) {
1172 pw.println(" ");
1173 pw.println(" Asset Allocations");
1174 pw.print(assetAlloc);
1175 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001176
1177 return memInfo;
1178 }
1179
1180 @Override
1181 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1182 dumpGraphicsInfo(fd);
Jeff Brown98365d72012-08-19 20:30:52 -07001183 WindowManagerGlobal.getInstance().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 }
1185
Jeff Brown6754ba22011-12-14 20:20:01 -08001186 @Override
1187 public void dumpDbInfo(FileDescriptor fd, String[] args) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07001188 PrintWriter pw = new FastPrintWriter(new FileOutputStream(fd));
Jeff Brown6754ba22011-12-14 20:20:01 -08001189 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1190 SQLiteDebug.dump(printer, args);
1191 pw.flush();
1192 }
1193
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001194 @Override
1195 public void unstableProviderDied(IBinder provider) {
1196 queueOrSendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
1197 }
1198
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001199 @Override
1200 public void requestActivityExtras(IBinder activityToken, IBinder requestToken,
1201 int requestType) {
1202 RequestActivityExtras cmd = new RequestActivityExtras();
1203 cmd.activityToken = activityToken;
1204 cmd.requestToken = requestToken;
1205 cmd.requestType = requestType;
1206 queueOrSendMessage(H.REQUEST_ACTIVITY_EXTRAS, cmd);
1207 }
1208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 private void printRow(PrintWriter pw, String format, Object...objs) {
1210 pw.println(String.format(format, objs));
1211 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001212
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001213 public void setCoreSettings(Bundle coreSettings) {
1214 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001215 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001216
1217 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1218 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1219 ucd.pkg = pkg;
1220 ucd.info = info;
1221 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1222 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001223
1224 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001225 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001226 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001227
Craig Mautner5eda9b32013-07-02 11:58:16 -07001228 public void scheduleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
1229 queueOrSendMessage(H.TRANSLUCENT_CONVERSION_COMPLETE, token, drawComplete ? 1 : 0);
1230 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001231
1232 public void setProcessState(int state) {
1233 updateProcessState(state, true);
1234 }
1235
1236 public void updateProcessState(int processState, boolean fromIpc) {
1237 synchronized (this) {
1238 if (mLastProcessState != processState) {
1239 mLastProcessState = processState;
1240
1241 // Update Dalvik state here based on ActivityManager.PROCESS_STATE_* constants.
1242 if (false) {
1243 Slog.i(TAG, "******************* PROCESS STATE CHANGED TO: " + processState
1244 + (fromIpc ? " (from ipc": ""));
1245 }
1246 }
1247 }
1248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 }
1250
Romain Guy65b345f2011-07-27 18:51:50 -07001251 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 public static final int LAUNCH_ACTIVITY = 100;
1253 public static final int PAUSE_ACTIVITY = 101;
1254 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1255 public static final int STOP_ACTIVITY_SHOW = 103;
1256 public static final int STOP_ACTIVITY_HIDE = 104;
1257 public static final int SHOW_WINDOW = 105;
1258 public static final int HIDE_WINDOW = 106;
1259 public static final int RESUME_ACTIVITY = 107;
1260 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001261 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 public static final int BIND_APPLICATION = 110;
1263 public static final int EXIT_APPLICATION = 111;
1264 public static final int NEW_INTENT = 112;
1265 public static final int RECEIVER = 113;
1266 public static final int CREATE_SERVICE = 114;
1267 public static final int SERVICE_ARGS = 115;
1268 public static final int STOP_SERVICE = 116;
1269 public static final int REQUEST_THUMBNAIL = 117;
1270 public static final int CONFIGURATION_CHANGED = 118;
1271 public static final int CLEAN_UP_CONTEXT = 119;
1272 public static final int GC_WHEN_IDLE = 120;
1273 public static final int BIND_SERVICE = 121;
1274 public static final int UNBIND_SERVICE = 122;
1275 public static final int DUMP_SERVICE = 123;
1276 public static final int LOW_MEMORY = 124;
1277 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1278 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001279 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001280 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001281 public static final int DESTROY_BACKUP_AGENT = 129;
1282 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001283 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001284 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001285 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001286 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001287 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001288 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001289 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001290 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001291 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001292 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001293 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001294 public static final int UNSTABLE_PROVIDER_DIED = 142;
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001295 public static final int REQUEST_ACTIVITY_EXTRAS = 143;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001296 public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001298 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 switch (code) {
1300 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1301 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1302 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1303 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1304 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1305 case SHOW_WINDOW: return "SHOW_WINDOW";
1306 case HIDE_WINDOW: return "HIDE_WINDOW";
1307 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1308 case SEND_RESULT: return "SEND_RESULT";
1309 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1310 case BIND_APPLICATION: return "BIND_APPLICATION";
1311 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1312 case NEW_INTENT: return "NEW_INTENT";
1313 case RECEIVER: return "RECEIVER";
1314 case CREATE_SERVICE: return "CREATE_SERVICE";
1315 case SERVICE_ARGS: return "SERVICE_ARGS";
1316 case STOP_SERVICE: return "STOP_SERVICE";
1317 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1318 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1319 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1320 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1321 case BIND_SERVICE: return "BIND_SERVICE";
1322 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1323 case DUMP_SERVICE: return "DUMP_SERVICE";
1324 case LOW_MEMORY: return "LOW_MEMORY";
1325 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1326 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001327 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001328 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1329 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001330 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001331 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001332 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001333 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001334 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001335 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001336 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001337 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001338 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001339 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001340 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001341 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001342 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001343 case REQUEST_ACTIVITY_EXTRAS: return "REQUEST_ACTIVITY_EXTRAS";
Craig Mautner5eda9b32013-07-02 11:58:16 -07001344 case TRANSLUCENT_CONVERSION_COMPLETE: return "TRANSLUCENT_CONVERSION_COMPLETE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 }
1346 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001347 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 }
1349 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001350 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 switch (msg.what) {
1352 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001353 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001354 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355
1356 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001357 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001358 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001359 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 } break;
1361 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001362 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001363 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001364 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001365 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 } break;
1367 case PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001368 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001370 maybeSnapshot();
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 PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001374 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
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 STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001379 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
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 STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001384 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
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 SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001389 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001391 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 break;
1393 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001394 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001396 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 break;
1398 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001399 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 handleResumeActivity((IBinder)msg.obj, true,
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001401 msg.arg1 != 0, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001402 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 break;
1404 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001405 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001407 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 break;
1409 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001410 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1412 msg.arg2, false);
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 BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001416 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 AppBindData data = (AppBindData)msg.obj;
1418 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001419 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 break;
1421 case EXIT_APPLICATION:
1422 if (mInitialApplication != null) {
1423 mInitialApplication.onTerminate();
1424 }
1425 Looper.myLooper().quit();
1426 break;
1427 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001428 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001430 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 break;
1432 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001433 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001435 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001436 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 break;
1438 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001439 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001441 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 break;
1443 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001444 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001446 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 break;
1448 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001449 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001451 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 break;
1453 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001454 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001456 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 break;
1458 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001459 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001461 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001462 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 break;
1464 case REQUEST_THUMBNAIL:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001465 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "requestThumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 handleRequestThumbnail((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001467 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 break;
1469 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001470 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001471 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001472 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001473 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 break;
1475 case CLEAN_UP_CONTEXT:
1476 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1477 cci.context.performFinalCleanup(cci.who, cci.what);
1478 break;
1479 case GC_WHEN_IDLE:
1480 scheduleGcIdler();
1481 break;
1482 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001483 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 break;
1485 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001486 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001488 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 break;
1490 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001491 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001493 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001495 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001496 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001497 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001498 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001499 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001500 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001501 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001502 break;
1503 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001504 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001505 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001506 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001507 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001508 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001509 Process.killProcess(Process.myPid());
1510 break;
1511 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001512 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001513 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001514 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001515 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001516 case ENABLE_JIT:
1517 ensureJitEnabled();
1518 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001519 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001520 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001521 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001522 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001523 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001524 case SCHEDULE_CRASH:
1525 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001526 case DUMP_HEAP:
1527 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1528 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001529 case DUMP_ACTIVITY:
1530 handleDumpActivity((DumpComponentInfo)msg.obj);
1531 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001532 case DUMP_PROVIDER:
1533 handleDumpProvider((DumpComponentInfo)msg.obj);
1534 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001535 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001536 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001537 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001538 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001539 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001540 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001541 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001542 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001543 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001544 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001545 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1546 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001547 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001548 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001549 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001550 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001551 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001552 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001553 case UNSTABLE_PROVIDER_DIED:
1554 handleUnstableProviderDied((IBinder)msg.obj, false);
1555 break;
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001556 case REQUEST_ACTIVITY_EXTRAS:
1557 handleRequestActivityExtras((RequestActivityExtras)msg.obj);
1558 break;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001559 case TRANSLUCENT_CONVERSION_COMPLETE:
1560 handleTranslucentConversionComplete((IBinder)msg.obj, msg.arg1 == 1);
1561 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001563 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 }
Bob Leee5408332009-09-04 18:31:17 -07001565
Brian Carlstromed7e0072011-03-24 13:27:57 -07001566 private void maybeSnapshot() {
1567 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001568 // convert the *private* ActivityThread.PackageInfo to *public* known
1569 // android.content.pm.PackageInfo
1570 String packageName = mBoundApplication.info.mPackageName;
1571 android.content.pm.PackageInfo packageInfo = null;
1572 try {
1573 Context context = getSystemContext();
1574 if(context == null) {
1575 Log.e(TAG, "cannot get a valid context");
1576 return;
1577 }
1578 PackageManager pm = context.getPackageManager();
1579 if(pm == null) {
1580 Log.e(TAG, "cannot get a valid PackageManager");
1581 return;
1582 }
1583 packageInfo = pm.getPackageInfo(
1584 packageName, PackageManager.GET_ACTIVITIES);
1585 } catch (NameNotFoundException e) {
1586 Log.e(TAG, "cannot get package info for " + packageName, e);
1587 }
1588 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001589 }
1590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 }
1592
Romain Guy65b345f2011-07-27 18:51:50 -07001593 private class Idler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001594 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001596 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001597 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001598 if (mBoundApplication != null && mProfiler.profileFd != null
1599 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001600 stopProfiling = true;
1601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 if (a != null) {
1603 mNewActivities = null;
1604 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001605 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001607 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 TAG, "Reporting idle of " + a +
1609 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001610 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 if (a.activity != null && !a.activity.mFinished) {
1612 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001613 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001614 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001616 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 }
1618 }
1619 prev = a;
1620 a = a.nextIdle;
1621 prev.nextIdle = null;
1622 } while (a != null);
1623 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001624 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001625 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001626 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001627 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 return false;
1629 }
1630 }
1631
1632 final class GcIdler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001633 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 public final boolean queueIdle() {
1635 doGcIfNeeded();
1636 return false;
1637 }
1638 }
1639
Romain Guy65b345f2011-07-27 18:51:50 -07001640 public static ActivityThread currentActivityThread() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08001641 return sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643
Romain Guy65b345f2011-07-27 18:51:50 -07001644 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001645 ActivityThread am = currentActivityThread();
1646 return (am != null && am.mBoundApplication != null)
Dianne Hackbornb57a50b2013-04-11 17:29:32 -07001647 ? am.mBoundApplication.appInfo.packageName : null;
1648 }
1649
1650 public static String currentProcessName() {
1651 ActivityThread am = currentActivityThread();
1652 return (am != null && am.mBoundApplication != null)
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001653 ? am.mBoundApplication.processName : null;
1654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655
Romain Guy65b345f2011-07-27 18:51:50 -07001656 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001657 ActivityThread am = currentActivityThread();
1658 return am != null ? am.mInitialApplication : null;
1659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001661 public static IPackageManager getPackageManager() {
1662 if (sPackageManager != null) {
1663 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1664 return sPackageManager;
1665 }
1666 IBinder b = ServiceManager.getService("package");
1667 //Slog.v("PackageManager", "default service binder = " + b);
1668 sPackageManager = IPackageManager.Stub.asInterface(b);
1669 //Slog.v("PackageManager", "default service = " + sPackageManager);
1670 return sPackageManager;
1671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672
Romain Guy65b345f2011-07-27 18:51:50 -07001673 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001674 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1675 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001676 if (config == null) {
1677 return null;
1678 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001679 if (!compat.supportsScreen()) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001680 mMainThreadConfig.setTo(config);
1681 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001682 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001683 }
1684 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001687 /**
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001688 * Creates the top level resources for the given package.
1689 */
Jeff Browna492c3a2012-08-23 19:48:44 -07001690 Resources getTopLevelResources(String resDir,
1691 int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001692 LoadedApk pkgInfo) {
Craig Mautner88c05892013-06-28 09:47:45 -07001693 return mResourcesManager.getTopLevelResources(resDir, displayId, overrideConfiguration,
Craig Mautner48d0d182013-06-11 07:53:06 -07001694 pkgInfo.getCompatibilityInfo(), null);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001695 }
1696
1697 final Handler getHandler() {
1698 return mH;
1699 }
1700
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001701 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1702 int flags) {
Amith Yamasani98edc952012-09-25 14:09:27 -07001703 return getPackageInfo(packageName, compatInfo, flags, UserHandle.myUserId());
1704 }
1705
1706 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1707 int flags, int userId) {
Craig Mautner88c05892013-06-28 09:47:45 -07001708 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001709 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1711 ref = mPackages.get(packageName);
1712 } else {
1713 ref = mResourcePackages.get(packageName);
1714 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001715 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001716 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001717 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1718 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 if (packageInfo != null && (packageInfo.mResources == null
1720 || packageInfo.mResources.getAssets().isUpToDate())) {
1721 if (packageInfo.isSecurityViolation()
1722 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1723 throw new SecurityException(
1724 "Requesting code from " + packageName
1725 + " to be run in process "
1726 + mBoundApplication.processName
1727 + "/" + mBoundApplication.appInfo.uid);
1728 }
1729 return packageInfo;
1730 }
1731 }
1732
1733 ApplicationInfo ai = null;
1734 try {
1735 ai = getPackageManager().getApplicationInfo(packageName,
Amith Yamasani98edc952012-09-25 14:09:27 -07001736 PackageManager.GET_SHARED_LIBRARY_FILES, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001738 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 }
1740
1741 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001742 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 }
1744
1745 return null;
1746 }
1747
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001748 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1749 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1751 boolean securityViolation = includeCode && ai.uid != 0
1752 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001753 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001754 : true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1756 |Context.CONTEXT_IGNORE_SECURITY))
1757 == Context.CONTEXT_INCLUDE_CODE) {
1758 if (securityViolation) {
1759 String msg = "Requesting code from " + ai.packageName
1760 + " (with uid " + ai.uid + ")";
1761 if (mBoundApplication != null) {
1762 msg = msg + " to be run in process "
1763 + mBoundApplication.processName + " (with uid "
1764 + mBoundApplication.appInfo.uid + ")";
1765 }
1766 throw new SecurityException(msg);
1767 }
1768 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001769 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 }
1771
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001772 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1773 CompatibilityInfo compatInfo) {
1774 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 }
1776
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001777 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07001778 synchronized (mResourcesManager) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001779 WeakReference<LoadedApk> ref;
1780 if (includeCode) {
1781 ref = mPackages.get(packageName);
1782 } else {
1783 ref = mResourcePackages.get(packageName);
1784 }
1785 return ref != null ? ref.get() : null;
1786 }
1787 }
1788
Romain Guy65b345f2011-07-27 18:51:50 -07001789 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07001791 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001792 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 if (includeCode) {
1794 ref = mPackages.get(aInfo.packageName);
1795 } else {
1796 ref = mResourcePackages.get(aInfo.packageName);
1797 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001798 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 if (packageInfo == null || (packageInfo.mResources != null
1800 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001801 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 : "Loading resource-only package ") + aInfo.packageName
1803 + " (in " + (mBoundApplication != null
1804 ? mBoundApplication.processName : null)
1805 + ")");
1806 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001807 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 securityViolation, includeCode &&
1809 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1810 if (includeCode) {
1811 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001812 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 } else {
1814 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001815 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 }
1817 }
1818 return packageInfo;
1819 }
1820 }
1821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 ActivityThread() {
Craig Mautner88c05892013-06-28 09:47:45 -07001823 mResourcesManager = ResourcesManager.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 }
1825
1826 public ApplicationThread getApplicationThread()
1827 {
1828 return mAppThread;
1829 }
1830
1831 public Instrumentation getInstrumentation()
1832 {
1833 return mInstrumentation;
1834 }
1835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001837 return mProfiler != null && mProfiler.profileFile != null
1838 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 }
1840
1841 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001842 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 }
1844
1845 public Looper getLooper() {
1846 return mLooper;
1847 }
1848
1849 public Application getApplication() {
1850 return mInitialApplication;
1851 }
Bob Leee5408332009-09-04 18:31:17 -07001852
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001853 public String getProcessName() {
1854 return mBoundApplication.processName;
1855 }
Bob Leee5408332009-09-04 18:31:17 -07001856
Dianne Hackborn21556372010-02-04 16:34:40 -08001857 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 synchronized (this) {
1859 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001860 ContextImpl context =
1861 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001862 LoadedApk info = new LoadedApk(this, "android", context, null,
1863 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 context.init(info, null, this);
Craig Mautner88c05892013-06-28 09:47:45 -07001865 context.getResources().updateConfiguration(mResourcesManager.getConfiguration(),
1866 mResourcesManager.getDisplayMetricsLocked(Display.DEFAULT_DISPLAY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001868 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 // + ": " + context.getResources().getConfiguration());
1870 }
1871 }
1872 return mSystemContext;
1873 }
1874
Mike Cleron432b7132009-09-24 15:28:29 -07001875 public void installSystemApplicationInfo(ApplicationInfo info) {
1876 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001877 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001878 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001879 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001880
1881 // give ourselves a default profiler
1882 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001883 }
1884 }
1885
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001886 void ensureJitEnabled() {
1887 if (!mJitEnabled) {
1888 mJitEnabled = true;
1889 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1890 }
1891 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 void scheduleGcIdler() {
1894 if (!mGcIdlerScheduled) {
1895 mGcIdlerScheduled = true;
1896 Looper.myQueue().addIdleHandler(mGcIdler);
1897 }
1898 mH.removeMessages(H.GC_WHEN_IDLE);
1899 }
1900
1901 void unscheduleGcIdler() {
1902 if (mGcIdlerScheduled) {
1903 mGcIdlerScheduled = false;
1904 Looper.myQueue().removeIdleHandler(mGcIdler);
1905 }
1906 mH.removeMessages(H.GC_WHEN_IDLE);
1907 }
1908
1909 void doGcIfNeeded() {
1910 mGcIdlerScheduled = false;
1911 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001912 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 // + "m now=" + now);
1914 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001915 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 BinderInternal.forceGc("bg");
1917 }
1918 }
1919
Jeff Hamilton52d32032011-01-08 15:31:26 -06001920 public void registerOnActivityPausedListener(Activity activity,
1921 OnActivityPausedListener listener) {
1922 synchronized (mOnPauseListeners) {
1923 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1924 if (list == null) {
1925 list = new ArrayList<OnActivityPausedListener>();
1926 mOnPauseListeners.put(activity, list);
1927 }
1928 list.add(listener);
1929 }
1930 }
1931
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001932 public void unregisterOnActivityPausedListener(Activity activity,
1933 OnActivityPausedListener listener) {
1934 synchronized (mOnPauseListeners) {
1935 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1936 if (list != null) {
1937 list.remove(listener);
1938 }
1939 }
1940 }
1941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 public final ActivityInfo resolveActivityInfo(Intent intent) {
1943 ActivityInfo aInfo = intent.resolveActivityInfo(
1944 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1945 if (aInfo == null) {
1946 // Throw an exception.
1947 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07001948 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 }
1950 return aInfo;
1951 }
Bob Leee5408332009-09-04 18:31:17 -07001952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001955 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001956 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001958 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 r.intent = intent;
1960 r.state = state;
1961 r.parent = parent;
1962 r.embeddedID = id;
1963 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001964 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 if (localLOGV) {
1966 ComponentName compname = intent.getComponent();
1967 String name;
1968 if (compname != null) {
1969 name = compname.toShortString();
1970 } else {
1971 name = "(Intent " + intent + ").getComponent() returned null";
1972 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001973 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 + ", comp=" + name
1975 + ", token=" + token);
1976 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001977 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 }
1979
1980 public final Activity getActivity(IBinder token) {
1981 return mActivities.get(token).activity;
1982 }
1983
1984 public final void sendActivityResult(
1985 IBinder token, String id, int requestCode,
1986 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001987 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001988 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1990 list.add(new ResultInfo(id, requestCode, resultCode, data));
1991 mAppThread.scheduleSendResult(token, list);
1992 }
1993
1994 // if the thread hasn't started yet, we don't have the handler, so just
1995 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07001996 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 queueOrSendMessage(what, obj, 0, 0);
1998 }
1999
Romain Guy65b345f2011-07-27 18:51:50 -07002000 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 queueOrSendMessage(what, obj, arg1, 0);
2002 }
2003
Romain Guy65b345f2011-07-27 18:51:50 -07002004 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002006 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2008 + ": " + arg1 + " / " + obj);
2009 Message msg = Message.obtain();
2010 msg.what = what;
2011 msg.obj = obj;
2012 msg.arg1 = arg1;
2013 msg.arg2 = arg2;
2014 mH.sendMessage(msg);
2015 }
2016 }
2017
Dianne Hackborn21556372010-02-04 16:34:40 -08002018 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 String what) {
2020 ContextCleanupInfo cci = new ContextCleanupInfo();
2021 cci.context = context;
2022 cci.who = who;
2023 cci.what = what;
2024 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
2025 }
2026
Romain Guy65b345f2011-07-27 18:51:50 -07002027 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2029
2030 ActivityInfo aInfo = r.activityInfo;
2031 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002032 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 Context.CONTEXT_INCLUDE_CODE);
2034 }
Bob Leee5408332009-09-04 18:31:17 -07002035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 ComponentName component = r.intent.getComponent();
2037 if (component == null) {
2038 component = r.intent.resolveActivity(
2039 mInitialApplication.getPackageManager());
2040 r.intent.setComponent(component);
2041 }
2042
2043 if (r.activityInfo.targetActivity != null) {
2044 component = new ComponentName(r.activityInfo.packageName,
2045 r.activityInfo.targetActivity);
2046 }
2047
2048 Activity activity = null;
2049 try {
2050 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2051 activity = mInstrumentation.newActivity(
2052 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002053 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 r.intent.setExtrasClassLoader(cl);
2055 if (r.state != null) {
2056 r.state.setClassLoader(cl);
2057 }
2058 } catch (Exception e) {
2059 if (!mInstrumentation.onException(activity, e)) {
2060 throw new RuntimeException(
2061 "Unable to instantiate activity " + component
2062 + ": " + e.toString(), e);
2063 }
2064 }
2065
2066 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002067 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002068
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002069 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2070 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 TAG, r + ": app=" + app
2072 + ", appName=" + app.getPackageName()
2073 + ", pkg=" + r.packageInfo.getPackageName()
2074 + ", comp=" + r.intent.getComponent().toShortString()
2075 + ", dir=" + r.packageInfo.getAppDir());
2076
2077 if (activity != null) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002078 Context appContext = createBaseContextForActivity(r, activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002080 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002081 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002082 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002083 activity.attach(appContext, this, getInstrumentation(), r.token,
2084 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002085 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07002086
Christopher Tateb70f3df2009-04-07 16:07:59 -07002087 if (customIntent != null) {
2088 activity.mIntent = customIntent;
2089 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002090 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 activity.mStartedActivity = false;
2092 int theme = r.activityInfo.getThemeResource();
2093 if (theme != 0) {
2094 activity.setTheme(theme);
2095 }
2096
2097 activity.mCalled = false;
2098 mInstrumentation.callActivityOnCreate(activity, r.state);
2099 if (!activity.mCalled) {
2100 throw new SuperNotCalledException(
2101 "Activity " + r.intent.getComponent().toShortString() +
2102 " did not call through to super.onCreate()");
2103 }
2104 r.activity = activity;
2105 r.stopped = true;
2106 if (!r.activity.mFinished) {
2107 activity.performStart();
2108 r.stopped = false;
2109 }
2110 if (!r.activity.mFinished) {
2111 if (r.state != null) {
2112 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2113 }
2114 }
2115 if (!r.activity.mFinished) {
2116 activity.mCalled = false;
2117 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2118 if (!activity.mCalled) {
2119 throw new SuperNotCalledException(
2120 "Activity " + r.intent.getComponent().toShortString() +
2121 " did not call through to super.onPostCreate()");
2122 }
2123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 }
2125 r.paused = true;
2126
2127 mActivities.put(r.token, r);
2128
2129 } catch (SuperNotCalledException e) {
2130 throw e;
2131
2132 } catch (Exception e) {
2133 if (!mInstrumentation.onException(activity, e)) {
2134 throw new RuntimeException(
2135 "Unable to start activity " + component
2136 + ": " + e.toString(), e);
2137 }
2138 }
2139
2140 return activity;
2141 }
2142
Jeff Brownefd43bd2012-09-21 17:02:35 -07002143 private Context createBaseContextForActivity(ActivityClientRecord r,
2144 final Activity activity) {
2145 ContextImpl appContext = new ContextImpl();
2146 appContext.init(r.packageInfo, r.token, this);
2147 appContext.setOuterContext(activity);
2148
2149 // For debugging purposes, if the activity's package name contains the value of
2150 // the "debug.use-second-display" system property as a substring, then show
2151 // its content on a secondary display if there is one.
2152 Context baseContext = appContext;
2153 String pkgName = SystemProperties.get("debug.second-display.pkg");
2154 if (pkgName != null && !pkgName.isEmpty()
2155 && r.packageInfo.mPackageName.contains(pkgName)) {
2156 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
2157 for (int displayId : dm.getDisplayIds()) {
2158 if (displayId != Display.DEFAULT_DISPLAY) {
Craig Mautner48d0d182013-06-11 07:53:06 -07002159 Display display = dm.getRealDisplay(displayId, r.token);
Jeff Brownefd43bd2012-09-21 17:02:35 -07002160 baseContext = appContext.createDisplayContext(display);
2161 break;
2162 }
2163 }
2164 }
2165 return baseContext;
2166 }
2167
Romain Guy65b345f2011-07-27 18:51:50 -07002168 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 // If we are getting ready to gc after going to the background, well
2170 // we are back active so skip it.
2171 unscheduleGcIdler();
2172
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002173 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002174 mProfiler.setProfiler(r.profileFile, r.profileFd);
2175 mProfiler.startProfiling();
2176 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002177 }
2178
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002179 // Make sure we are running with the most recent config.
2180 handleConfigurationChanged(null, null);
2181
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002182 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07002184 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185
2186 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002187 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002188 Bundle oldState = r.state;
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002189 handleResumeActivity(r.token, false, r.isForward,
2190 !r.activity.mFinished && !r.startsNotResumed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191
2192 if (!r.activity.mFinished && r.startsNotResumed) {
2193 // The activity manager actually wants this one to start out
2194 // paused, because it needs to be visible but isn't in the
2195 // foreground. We accomplish this by going through the
2196 // normal startup (because activities expect to go through
2197 // onResume() the first time they run, before their window
2198 // is displayed), and then pausing it. However, in this case
2199 // we do -not- need to do the full pause cycle (of freezing
2200 // and such) because the activity manager assumes it can just
2201 // retain the current state it has.
2202 try {
2203 r.activity.mCalled = false;
2204 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002205 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002206 // we need to be created again. But we only do this
2207 // for pre-Honeycomb apps, which always save their state
2208 // when pausing, so we can not have them save their state
2209 // when restarting from a paused state. For HC and later,
2210 // we want to (and can) let the state be saved as the normal
2211 // part of stopping the activity.
2212 if (r.isPreHoneycomb()) {
2213 r.state = oldState;
2214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 if (!r.activity.mCalled) {
2216 throw new SuperNotCalledException(
2217 "Activity " + r.intent.getComponent().toShortString() +
2218 " did not call through to super.onPause()");
2219 }
2220
2221 } catch (SuperNotCalledException e) {
2222 throw e;
2223
2224 } catch (Exception e) {
2225 if (!mInstrumentation.onException(r.activity, e)) {
2226 throw new RuntimeException(
2227 "Unable to pause activity "
2228 + r.intent.getComponent().toShortString()
2229 + ": " + e.toString(), e);
2230 }
2231 }
2232 r.paused = true;
2233 }
2234 } else {
2235 // If there was an error, for any reason, tell the activity
2236 // manager to stop us.
2237 try {
2238 ActivityManagerNative.getDefault()
2239 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2240 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002241 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 }
2243 }
2244 }
2245
Romain Guy65b345f2011-07-27 18:51:50 -07002246 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 List<Intent> intents) {
2248 final int N = intents.size();
2249 for (int i=0; i<N; i++) {
2250 Intent intent = intents.get(i);
2251 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002252 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2254 }
2255 }
2256
2257 public final void performNewIntents(IBinder token,
2258 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002259 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 if (r != null) {
2261 final boolean resumed = !r.paused;
2262 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002263 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 mInstrumentation.callActivityOnPause(r.activity);
2265 }
2266 deliverNewIntents(r, intents);
2267 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002268 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002269 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 }
2271 }
2272 }
Bob Leee5408332009-09-04 18:31:17 -07002273
Romain Guy65b345f2011-07-27 18:51:50 -07002274 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 performNewIntents(data.token, data.intents);
2276 }
2277
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002278 public void handleRequestActivityExtras(RequestActivityExtras cmd) {
2279 Bundle data = new Bundle();
2280 ActivityClientRecord r = mActivities.get(cmd.activityToken);
2281 if (r != null) {
2282 r.activity.getApplication().dispatchOnProvideAssistData(r.activity, data);
2283 r.activity.onProvideAssistData(data);
2284 }
2285 if (data.isEmpty()) {
2286 data = null;
2287 }
2288 IActivityManager mgr = ActivityManagerNative.getDefault();
2289 try {
2290 mgr.reportTopActivityExtras(cmd.requestToken, data);
2291 } catch (RemoteException e) {
2292 }
2293 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002294
2295 public void handleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
2296 ActivityClientRecord r = mActivities.get(token);
2297 if (r != null) {
2298 r.activity.onTranslucentConversionComplete(drawComplete);
2299 }
2300 }
2301
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002302 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2303
2304 /**
2305 * Return the Intent that's currently being handled by a
2306 * BroadcastReceiver on this thread, or null if none.
2307 * @hide
2308 */
2309 public static Intent getIntentBeingBroadcast() {
2310 return sCurrentBroadcastIntent.get();
2311 }
2312
Romain Guy65b345f2011-07-27 18:51:50 -07002313 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 // If we are getting ready to gc after going to the background, well
2315 // we are back active so skip it.
2316 unscheduleGcIdler();
2317
2318 String component = data.intent.getComponent().getClassName();
2319
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002320 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002321 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322
2323 IActivityManager mgr = ActivityManagerNative.getDefault();
2324
Romain Guy65b345f2011-07-27 18:51:50 -07002325 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 try {
2327 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2328 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002329 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2331 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002332 if (DEBUG_BROADCAST) Slog.i(TAG,
2333 "Finishing failed broadcast to " + data.intent.getComponent());
2334 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 throw new RuntimeException(
2336 "Unable to instantiate receiver " + component
2337 + ": " + e.toString(), e);
2338 }
2339
2340 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002341 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002342
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002343 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 TAG, "Performing receive of " + data.intent
2345 + ": app=" + app
2346 + ", appName=" + app.getPackageName()
2347 + ", pkg=" + packageInfo.getPackageName()
2348 + ", comp=" + data.intent.getComponent().toShortString()
2349 + ", dir=" + packageInfo.getAppDir());
2350
Dianne Hackborn21556372010-02-04 16:34:40 -08002351 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002352 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002353 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 receiver.onReceive(context.getReceiverRestrictedContext(),
2355 data.intent);
2356 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002357 if (DEBUG_BROADCAST) Slog.i(TAG,
2358 "Finishing failed broadcast to " + data.intent.getComponent());
2359 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 if (!mInstrumentation.onException(receiver, e)) {
2361 throw new RuntimeException(
2362 "Unable to start receiver " + component
2363 + ": " + e.toString(), e);
2364 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002365 } finally {
2366 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 }
2368
Dianne Hackborne829fef2010-10-26 17:44:01 -07002369 if (receiver.getPendingResult() != null) {
2370 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 }
2372 }
2373
Christopher Tate181fafa2009-05-14 11:12:14 -07002374 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002375 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002376 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002377
Christopher Tate346acb12012-10-15 19:20:25 -07002378 // Sanity check the requested target package's uid against ours
2379 try {
2380 PackageInfo requestedPackage = getPackageManager().getPackageInfo(
2381 data.appInfo.packageName, 0, UserHandle.myUserId());
2382 if (requestedPackage.applicationInfo.uid != Process.myUid()) {
2383 Slog.w(TAG, "Asked to instantiate non-matching package "
2384 + data.appInfo.packageName);
2385 return;
2386 }
2387 } catch (RemoteException e) {
2388 Slog.e(TAG, "Can't reach package manager", e);
2389 return;
2390 }
2391
Christopher Tate181fafa2009-05-14 11:12:14 -07002392 // no longer idle; we have backup work to do
2393 unscheduleGcIdler();
2394
2395 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002396 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002397 String packageName = packageInfo.mPackageName;
Christopher Tate346acb12012-10-15 19:20:25 -07002398 if (packageName == null) {
2399 Slog.d(TAG, "Asked to create backup agent for nonexistent package");
2400 return;
2401 }
2402
Christopher Tate181fafa2009-05-14 11:12:14 -07002403 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002404 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002405 + " already exists");
2406 return;
2407 }
Bob Leee5408332009-09-04 18:31:17 -07002408
Christopher Tate181fafa2009-05-14 11:12:14 -07002409 BackupAgent agent = null;
2410 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002411
Christopher Tate79ec80d2011-06-24 14:58:49 -07002412 // full backup operation but no app-supplied agent? use the default implementation
2413 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2414 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002415 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002416 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002417
Christopher Tate181fafa2009-05-14 11:12:14 -07002418 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002419 IBinder binder = null;
2420 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002421 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2422
Christopher Tated1475e02009-07-09 15:36:17 -07002423 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002424 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002425
2426 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002427 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002428 context.init(packageInfo, null, this);
2429 context.setOuterContext(agent);
2430 agent.attach(context);
2431
2432 agent.onCreate();
2433 binder = agent.onBind();
2434 mBackupAgents.put(packageName, agent);
2435 } catch (Exception e) {
2436 // If this is during restore, fail silently; otherwise go
2437 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002438 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002439 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2440 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002441 throw e;
2442 }
2443 // falling through with 'binder' still null
2444 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002445
2446 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002447 try {
2448 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2449 } catch (RemoteException e) {
2450 // nothing to do.
2451 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002452 } catch (Exception e) {
2453 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002454 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002455 }
2456 }
2457
2458 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002459 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002460 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002461
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002462 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002463 String packageName = packageInfo.mPackageName;
2464 BackupAgent agent = mBackupAgents.get(packageName);
2465 if (agent != null) {
2466 try {
2467 agent.onDestroy();
2468 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002469 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002470 e.printStackTrace();
2471 }
2472 mBackupAgents.remove(packageName);
2473 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002474 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002475 }
2476 }
2477
Romain Guy65b345f2011-07-27 18:51:50 -07002478 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 // If we are getting ready to gc after going to the background, well
2480 // we are back active so skip it.
2481 unscheduleGcIdler();
2482
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002483 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002484 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 Service service = null;
2486 try {
2487 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2488 service = (Service) cl.loadClass(data.info.name).newInstance();
2489 } catch (Exception e) {
2490 if (!mInstrumentation.onException(service, e)) {
2491 throw new RuntimeException(
2492 "Unable to instantiate service " + data.info.name
2493 + ": " + e.toString(), e);
2494 }
2495 }
2496
2497 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002498 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499
Dianne Hackborn21556372010-02-04 16:34:40 -08002500 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 context.init(packageInfo, null, this);
2502
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002503 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 context.setOuterContext(service);
2505 service.attach(context, this, data.info.name, data.token, app,
2506 ActivityManagerNative.getDefault());
2507 service.onCreate();
2508 mServices.put(data.token, service);
2509 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002510 ActivityManagerNative.getDefault().serviceDoneExecuting(
2511 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 } catch (RemoteException e) {
2513 // nothing to do.
2514 }
2515 } catch (Exception e) {
2516 if (!mInstrumentation.onException(service, e)) {
2517 throw new RuntimeException(
2518 "Unable to create service " + data.info.name
2519 + ": " + e.toString(), e);
2520 }
2521 }
2522 }
2523
Romain Guy65b345f2011-07-27 18:51:50 -07002524 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002526 if (DEBUG_SERVICE)
2527 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 if (s != null) {
2529 try {
2530 data.intent.setExtrasClassLoader(s.getClassLoader());
2531 try {
2532 if (!data.rebind) {
2533 IBinder binder = s.onBind(data.intent);
2534 ActivityManagerNative.getDefault().publishService(
2535 data.token, data.intent, binder);
2536 } else {
2537 s.onRebind(data.intent);
2538 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002539 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002541 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 } catch (RemoteException ex) {
2543 }
2544 } catch (Exception e) {
2545 if (!mInstrumentation.onException(s, e)) {
2546 throw new RuntimeException(
2547 "Unable to bind to service " + s
2548 + " with " + data.intent + ": " + e.toString(), e);
2549 }
2550 }
2551 }
2552 }
2553
Romain Guy65b345f2011-07-27 18:51:50 -07002554 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 Service s = mServices.get(data.token);
2556 if (s != null) {
2557 try {
2558 data.intent.setExtrasClassLoader(s.getClassLoader());
2559 boolean doRebind = s.onUnbind(data.intent);
2560 try {
2561 if (doRebind) {
2562 ActivityManagerNative.getDefault().unbindFinished(
2563 data.token, data.intent, doRebind);
2564 } else {
2565 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002566 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 }
2568 } catch (RemoteException ex) {
2569 }
2570 } catch (Exception e) {
2571 if (!mInstrumentation.onException(s, e)) {
2572 throw new RuntimeException(
2573 "Unable to unbind to service " + s
2574 + " with " + data.intent + ": " + e.toString(), e);
2575 }
2576 }
2577 }
2578 }
2579
Dianne Hackborn625ac272010-09-17 18:29:22 -07002580 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002581 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2582 try {
2583 Service s = mServices.get(info.token);
2584 if (s != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002585 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2586 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002587 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2588 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002590 } finally {
2591 IoUtils.closeQuietly(info.fd);
2592 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 }
2594 }
2595
Dianne Hackborn625ac272010-09-17 18:29:22 -07002596 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002597 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2598 try {
2599 ActivityClientRecord r = mActivities.get(info.token);
2600 if (r != null && r.activity != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002601 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2602 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002603 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2604 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002605 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002606 } finally {
2607 IoUtils.closeQuietly(info.fd);
2608 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002609 }
2610 }
2611
Marco Nelissen18cb2872011-11-15 11:19:53 -08002612 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002613 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2614 try {
2615 ProviderClientRecord r = mLocalProviders.get(info.token);
2616 if (r != null && r.mLocalProvider != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002617 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2618 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002619 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2620 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002621 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002622 } finally {
2623 IoUtils.closeQuietly(info.fd);
2624 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002625 }
2626 }
2627
Romain Guy65b345f2011-07-27 18:51:50 -07002628 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 Service s = mServices.get(data.token);
2630 if (s != null) {
2631 try {
2632 if (data.args != null) {
2633 data.args.setExtrasClassLoader(s.getClassLoader());
2634 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002635 int res;
2636 if (!data.taskRemoved) {
2637 res = s.onStartCommand(data.args, data.flags, data.startId);
2638 } else {
2639 s.onTaskRemoved(data.args);
2640 res = Service.START_TASK_REMOVED_COMPLETE;
2641 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002642
2643 QueuedWork.waitToFinish();
2644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002646 ActivityManagerNative.getDefault().serviceDoneExecuting(
2647 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 } catch (RemoteException e) {
2649 // nothing to do.
2650 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002651 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 } catch (Exception e) {
2653 if (!mInstrumentation.onException(s, e)) {
2654 throw new RuntimeException(
2655 "Unable to start service " + s
2656 + " with " + data.args + ": " + e.toString(), e);
2657 }
2658 }
2659 }
2660 }
2661
Romain Guy65b345f2011-07-27 18:51:50 -07002662 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 Service s = mServices.remove(token);
2664 if (s != null) {
2665 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002666 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 s.onDestroy();
2668 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002669 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002671 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002673
2674 QueuedWork.waitToFinish();
2675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002677 ActivityManagerNative.getDefault().serviceDoneExecuting(
2678 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 } catch (RemoteException e) {
2680 // nothing to do.
2681 }
2682 } catch (Exception e) {
2683 if (!mInstrumentation.onException(s, e)) {
2684 throw new RuntimeException(
2685 "Unable to stop service " + s
2686 + ": " + e.toString(), e);
2687 }
2688 }
2689 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002690 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 }
2692
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002693 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002695 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002696 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 + " finished=" + r.activity.mFinished);
2698 if (r != null && !r.activity.mFinished) {
2699 if (clearHide) {
2700 r.hideForNow = false;
2701 r.activity.mStartedActivity = false;
2702 }
2703 try {
Dianne Hackborn689586d2012-10-01 18:23:04 -07002704 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 if (r.pendingIntents != null) {
2706 deliverNewIntents(r, r.pendingIntents);
2707 r.pendingIntents = null;
2708 }
2709 if (r.pendingResults != null) {
2710 deliverResults(r, r.pendingResults);
2711 r.pendingResults = null;
2712 }
2713 r.activity.performResume();
2714
Bob Leee5408332009-09-04 18:31:17 -07002715 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002716 UserHandle.myUserId(), r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 r.paused = false;
2719 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 r.state = null;
2721 } catch (Exception e) {
2722 if (!mInstrumentation.onException(r.activity, e)) {
2723 throw new RuntimeException(
2724 "Unable to resume activity "
2725 + r.intent.getComponent().toShortString()
2726 + ": " + e.toString(), e);
2727 }
2728 }
2729 }
2730 return r;
2731 }
2732
Romain Guya998dff2012-03-23 18:58:36 -07002733 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002734 if (r.mPendingRemoveWindow != null) {
2735 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2736 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2737 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07002738 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002739 r.activity.getClass().getName(), "Activity");
2740 }
2741 }
2742 r.mPendingRemoveWindow = null;
2743 r.mPendingRemoveWindowManager = null;
2744 }
2745
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002746 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward,
2747 boolean reallyResume) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 // If we are getting ready to gc after going to the background, well
2749 // we are back active so skip it.
2750 unscheduleGcIdler();
2751
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002752 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753
2754 if (r != null) {
2755 final Activity a = r.activity;
2756
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002757 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 TAG, "Resume " + r + " started activity: " +
2759 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2760 + ", finished: " + a.mFinished);
2761
2762 final int forwardBit = isForward ?
2763 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 // If the window hasn't yet been added to the window manager,
2766 // and this guy didn't finish itself or start another activity,
2767 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002768 boolean willBeVisible = !a.mStartedActivity;
2769 if (!willBeVisible) {
2770 try {
2771 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2772 a.getActivityToken());
2773 } catch (RemoteException e) {
2774 }
2775 }
2776 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 r.window = r.activity.getWindow();
2778 View decor = r.window.getDecorView();
2779 decor.setVisibility(View.INVISIBLE);
2780 ViewManager wm = a.getWindowManager();
2781 WindowManager.LayoutParams l = r.window.getAttributes();
2782 a.mDecor = decor;
2783 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2784 l.softInputMode |= forwardBit;
2785 if (a.mVisibleFromClient) {
2786 a.mWindowAdded = true;
2787 wm.addView(decor, l);
2788 }
2789
2790 // If the window has already been added, but during resume
2791 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002792 // window visible.
2793 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002794 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 TAG, "Launch " + r + " mStartedActivity set");
2796 r.hideForNow = true;
2797 }
2798
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002799 // Get rid of anything left hanging around.
2800 cleanUpPendingRemoveWindows(r);
2801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 // The window is now visible if it has been added, we are not
2803 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002804 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002805 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002807 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002808 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07002810 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 r.newConfig = null;
2812 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002813 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 + isForward);
2815 WindowManager.LayoutParams l = r.window.getAttributes();
2816 if ((l.softInputMode
2817 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2818 != forwardBit) {
2819 l.softInputMode = (l.softInputMode
2820 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2821 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002822 if (r.activity.mVisibleFromClient) {
2823 ViewManager wm = a.getWindowManager();
2824 View decor = r.window.getDecorView();
2825 wm.updateViewLayout(decor, l);
2826 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 }
2828 r.activity.mVisibleFromServer = true;
2829 mNumVisibleActivities++;
2830 if (r.activity.mVisibleFromClient) {
2831 r.activity.makeVisible();
2832 }
2833 }
2834
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002835 if (!r.onlyLocalRequest) {
2836 r.nextIdle = mNewActivities;
2837 mNewActivities = r;
2838 if (localLOGV) Slog.v(
2839 TAG, "Scheduling idle handler for " + r);
2840 Looper.myQueue().addIdleHandler(new Idler());
2841 }
2842 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002844 // Tell the activity manager we have resumed.
2845 if (reallyResume) {
2846 try {
2847 ActivityManagerNative.getDefault().activityResumed(token);
2848 } catch (RemoteException ex) {
2849 }
2850 }
2851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 } else {
2853 // If an exception was thrown when trying to resume, then
2854 // just end this activity.
2855 try {
2856 ActivityManagerNative.getDefault()
2857 .finishActivity(token, Activity.RESULT_CANCELED, null);
2858 } catch (RemoteException ex) {
2859 }
2860 }
2861 }
2862
2863 private int mThumbnailWidth = -1;
2864 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002865 private Bitmap mAvailThumbnailBitmap = null;
2866 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867
Romain Guy65b345f2011-07-27 18:51:50 -07002868 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002869 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002871 if (thumbnail == null) {
2872 int w = mThumbnailWidth;
2873 int h;
2874 if (w < 0) {
2875 Resources res = r.activity.getResources();
2876 mThumbnailHeight = h =
2877 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002879 mThumbnailWidth = w =
2880 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2881 } else {
2882 h = mThumbnailHeight;
2883 }
2884
2885 // On platforms where we don't want thumbnails, set dims to (0,0)
2886 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07002887 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
2888 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002889 thumbnail.eraseColor(0);
2890 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 }
2892
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002893 if (thumbnail != null) {
2894 Canvas cv = mThumbnailCanvas;
2895 if (cv == null) {
2896 mThumbnailCanvas = cv = new Canvas();
2897 }
2898
2899 cv.setBitmap(thumbnail);
2900 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2901 mAvailThumbnailBitmap = thumbnail;
2902 thumbnail = null;
2903 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002904 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 } catch (Exception e) {
2908 if (!mInstrumentation.onException(r.activity, e)) {
2909 throw new RuntimeException(
2910 "Unable to create thumbnail of "
2911 + r.intent.getComponent().toShortString()
2912 + ": " + e.toString(), e);
2913 }
2914 thumbnail = null;
2915 }
2916
2917 return thumbnail;
2918 }
2919
Romain Guy65b345f2011-07-27 18:51:50 -07002920 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002922 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002924 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 if (userLeaving) {
2926 performUserLeavingActivity(r);
2927 }
Bob Leee5408332009-09-04 18:31:17 -07002928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002930 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002932 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002933 if (r.isPreHoneycomb()) {
2934 QueuedWork.waitToFinish();
2935 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 // Tell the activity manager we have paused.
2938 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002939 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 } catch (RemoteException ex) {
2941 }
2942 }
2943 }
2944
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002945 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 mInstrumentation.callActivityOnUserLeaving(r.activity);
2947 }
2948
2949 final Bundle performPauseActivity(IBinder token, boolean finished,
2950 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002951 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 return r != null ? performPauseActivity(r, finished, saveState) : null;
2953 }
2954
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002955 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 boolean saveState) {
2957 if (r.paused) {
2958 if (r.activity.mFinished) {
2959 // If we are finishing, we won't call onResume() in certain cases.
2960 // So here we likewise don't want to call onPause() if the activity
2961 // isn't resumed.
2962 return null;
2963 }
2964 RuntimeException e = new RuntimeException(
2965 "Performing pause of activity that is not resumed: "
2966 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002967 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 }
2969 Bundle state = null;
2970 if (finished) {
2971 r.activity.mFinished = true;
2972 }
2973 try {
2974 // Next have the activity save its current state and managed dialogs...
2975 if (!r.activity.mFinished && saveState) {
2976 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002977 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2979 r.state = state;
2980 }
2981 // Now we are idle.
2982 r.activity.mCalled = false;
2983 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002984 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, UserHandle.myUserId(),
2985 r.activity.getComponentName().getClassName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 if (!r.activity.mCalled) {
2987 throw new SuperNotCalledException(
2988 "Activity " + r.intent.getComponent().toShortString() +
2989 " did not call through to super.onPause()");
2990 }
2991
2992 } catch (SuperNotCalledException e) {
2993 throw e;
2994
2995 } catch (Exception e) {
2996 if (!mInstrumentation.onException(r.activity, e)) {
2997 throw new RuntimeException(
2998 "Unable to pause activity "
2999 + r.intent.getComponent().toShortString()
3000 + ": " + e.toString(), e);
3001 }
3002 }
3003 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06003004
3005 // Notify any outstanding on paused listeners
3006 ArrayList<OnActivityPausedListener> listeners;
3007 synchronized (mOnPauseListeners) {
3008 listeners = mOnPauseListeners.remove(r.activity);
3009 }
3010 int size = (listeners != null ? listeners.size() : 0);
3011 for (int i = 0; i < size; i++) {
3012 listeners.get(i).onPaused(r.activity);
3013 }
3014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 return state;
3016 }
3017
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003018 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003019 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003020 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 }
3022
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003023 private static class StopInfo implements Runnable {
3024 ActivityClientRecord activity;
3025 Bundle state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 Bitmap thumbnail;
3027 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003028
3029 @Override public void run() {
3030 // Tell activity manager we have been stopped.
3031 try {
3032 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
3033 ActivityManagerNative.getDefault().activityStopped(
3034 activity.token, state, thumbnail, description);
3035 } catch (RemoteException ex) {
3036 }
3037 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038 }
3039
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003040 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003041 public final IActivityManager.ContentProviderHolder holder;
3042 public final ProviderClientRecord client;
3043 public int stableCount;
3044 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003045
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003046 // When this is set, the stable and unstable ref counts are 0 and
3047 // we have a pending operation scheduled to remove the ref count
3048 // from the activity manager. On the activity manager we are still
3049 // holding an unstable ref, though it is not reflected in the counts
3050 // here.
3051 public boolean removePending;
3052
3053 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
3054 ProviderClientRecord inClient, int sCount, int uCount) {
3055 holder = inHolder;
3056 client = inClient;
3057 stableCount = sCount;
3058 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 }
3060 }
3061
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003062 /**
3063 * Core implementation of stopping an activity. Note this is a little
3064 * tricky because the server's meaning of stop is slightly different
3065 * than our client -- for the server, stop means to save state and give
3066 * it the result when it is done, but the window may still be visible.
3067 * For the client, we want to call onStop()/onStart() to indicate when
3068 * the activity's UI visibillity changes.
3069 */
Romain Guy65b345f2011-07-27 18:51:50 -07003070 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003071 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003072 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003073 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 if (r != null) {
3075 if (!keepShown && r.stopped) {
3076 if (r.activity.mFinished) {
3077 // If we are finishing, we won't call onResume() in certain
3078 // cases. So here we likewise don't want to call onStop()
3079 // if the activity isn't resumed.
3080 return;
3081 }
3082 RuntimeException e = new RuntimeException(
3083 "Performing stop of activity that is not resumed: "
3084 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003085 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 }
3087
3088 if (info != null) {
3089 try {
3090 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003091 // For now, don't create the thumbnail here; we are
3092 // doing that by doing a screen snapshot.
3093 info.thumbnail = null; //createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 info.description = r.activity.onCreateDescription();
3095 } catch (Exception e) {
3096 if (!mInstrumentation.onException(r.activity, e)) {
3097 throw new RuntimeException(
3098 "Unable to save state of activity "
3099 + r.intent.getComponent().toShortString()
3100 + ": " + e.toString(), e);
3101 }
3102 }
3103 }
3104
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003105 // Next have the activity save its current state and managed dialogs...
3106 if (!r.activity.mFinished && saveState) {
3107 if (r.state == null) {
3108 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003109 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003110 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
3111 r.state = state;
3112 } else {
3113 state = r.state;
3114 }
3115 }
3116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 if (!keepShown) {
3118 try {
3119 // Now we are idle.
3120 r.activity.performStop();
3121 } catch (Exception e) {
3122 if (!mInstrumentation.onException(r.activity, e)) {
3123 throw new RuntimeException(
3124 "Unable to stop activity "
3125 + r.intent.getComponent().toShortString()
3126 + ": " + e.toString(), e);
3127 }
3128 }
3129 r.stopped = true;
3130 }
3131
3132 r.paused = true;
3133 }
3134 }
3135
Romain Guy65b345f2011-07-27 18:51:50 -07003136 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 View v = r.activity.mDecor;
3138 if (v != null) {
3139 if (show) {
3140 if (!r.activity.mVisibleFromServer) {
3141 r.activity.mVisibleFromServer = true;
3142 mNumVisibleActivities++;
3143 if (r.activity.mVisibleFromClient) {
3144 r.activity.makeVisible();
3145 }
3146 }
3147 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003148 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003149 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003151 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152 r.newConfig = null;
3153 }
3154 } else {
3155 if (r.activity.mVisibleFromServer) {
3156 r.activity.mVisibleFromServer = false;
3157 mNumVisibleActivities--;
3158 v.setVisibility(View.INVISIBLE);
3159 }
3160 }
3161 }
3162 }
3163
Romain Guy65b345f2011-07-27 18:51:50 -07003164 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003165 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 r.activity.mConfigChangeFlags |= configChanges;
3167
3168 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003169 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003171 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 TAG, "Finishing stop of " + r + ": show=" + show
3173 + " win=" + r.window);
3174
3175 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003176
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003177 // Make sure any pending writes are now committed.
3178 if (!r.isPreHoneycomb()) {
3179 QueuedWork.waitToFinish();
3180 }
3181
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003182 // Schedule the call to tell the activity manager we have
3183 // stopped. We don't do this immediately, because we want to
3184 // have a chance for any other pending work (in particular memory
3185 // trim requests) to complete before you tell the activity
3186 // manager to proceed and allow us to go fully into the background.
3187 info.activity = r;
3188 info.state = r.state;
3189 mH.post(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190 }
3191
3192 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003193 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 if (r.stopped) {
3195 r.activity.performRestart();
3196 r.stopped = false;
3197 }
3198 }
3199
Romain Guy65b345f2011-07-27 18:51:50 -07003200 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003201 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003202
3203 if (r == null) {
3204 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3205 return;
3206 }
3207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003209 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 } else if (show && r.stopped) {
3211 // If we are getting ready to gc after going to the background, well
3212 // we are back active so skip it.
3213 unscheduleGcIdler();
3214
3215 r.activity.performRestart();
3216 r.stopped = false;
3217 }
3218 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003219 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 TAG, "Handle window " + r + " visibility: " + show);
3221 updateVisibility(r, show);
3222 }
3223 }
3224
Romain Guy65b345f2011-07-27 18:51:50 -07003225 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003226 ActivityClientRecord r = mActivities.get(token);
3227
3228 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003229 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003230 return;
3231 }
3232
3233 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003234 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003235 try {
3236 // Now we are idle.
3237 r.activity.performStop();
3238 } catch (Exception e) {
3239 if (!mInstrumentation.onException(r.activity, e)) {
3240 throw new RuntimeException(
3241 "Unable to stop activity "
3242 + r.intent.getComponent().toShortString()
3243 + ": " + e.toString(), e);
3244 }
3245 }
3246 r.stopped = true;
3247 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003248
3249 // Make sure any pending writes are now committed.
3250 if (!r.isPreHoneycomb()) {
3251 QueuedWork.waitToFinish();
3252 }
3253
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003254 // Tell activity manager we slept.
3255 try {
3256 ActivityManagerNative.getDefault().activitySlept(r.token);
3257 } catch (RemoteException ex) {
3258 }
3259 } else {
3260 if (r.stopped && r.activity.mVisibleFromServer) {
3261 r.activity.performRestart();
3262 r.stopped = false;
3263 }
3264 }
3265 }
3266
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003267 private void handleSetCoreSettings(Bundle coreSettings) {
Craig Mautner88c05892013-06-28 09:47:45 -07003268 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003269 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003270 }
3271 }
3272
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003273 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3274 LoadedApk apk = peekPackageInfo(data.pkg, false);
3275 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003276 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003277 }
3278 apk = peekPackageInfo(data.pkg, true);
3279 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003280 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003281 }
3282 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07003283 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003284 }
3285
Romain Guy65b345f2011-07-27 18:51:50 -07003286 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 final int N = results.size();
3288 for (int i=0; i<N; i++) {
3289 ResultInfo ri = results.get(i);
3290 try {
3291 if (ri.mData != null) {
3292 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
3293 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003294 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003295 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 r.activity.dispatchActivityResult(ri.mResultWho,
3297 ri.mRequestCode, ri.mResultCode, ri.mData);
3298 } catch (Exception e) {
3299 if (!mInstrumentation.onException(r.activity, e)) {
3300 throw new RuntimeException(
3301 "Failure delivering result " + ri + " to activity "
3302 + r.intent.getComponent().toShortString()
3303 + ": " + e.toString(), e);
3304 }
3305 }
3306 }
3307 }
3308
Romain Guy65b345f2011-07-27 18:51:50 -07003309 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003310 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003311 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 if (r != null) {
3313 final boolean resumed = !r.paused;
3314 if (!r.activity.mFinished && r.activity.mDecor != null
3315 && r.hideForNow && resumed) {
3316 // We had hidden the activity because it started another
3317 // one... we have gotten a result back and we are not
3318 // paused, so make sure our window is visible.
3319 updateVisibility(r, true);
3320 }
3321 if (resumed) {
3322 try {
3323 // Now we are idle.
3324 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003325 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 mInstrumentation.callActivityOnPause(r.activity);
3327 if (!r.activity.mCalled) {
3328 throw new SuperNotCalledException(
3329 "Activity " + r.intent.getComponent().toShortString()
3330 + " did not call through to super.onPause()");
3331 }
3332 } catch (SuperNotCalledException e) {
3333 throw e;
3334 } catch (Exception e) {
3335 if (!mInstrumentation.onException(r.activity, e)) {
3336 throw new RuntimeException(
3337 "Unable to pause activity "
3338 + r.intent.getComponent().toShortString()
3339 + ": " + e.toString(), e);
3340 }
3341 }
3342 }
3343 deliverResults(r, res.results);
3344 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003345 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003346 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 }
3348 }
3349 }
3350
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003351 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 return performDestroyActivity(token, finishing, 0, false);
3353 }
3354
Romain Guy65b345f2011-07-27 18:51:50 -07003355 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003357 ActivityClientRecord r = mActivities.get(token);
Craig Mautner88c05892013-06-28 09:47:45 -07003358 Class<? extends Activity> activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003359 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003361 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 r.activity.mConfigChangeFlags |= configChanges;
3363 if (finishing) {
3364 r.activity.mFinished = true;
3365 }
3366 if (!r.paused) {
3367 try {
3368 r.activity.mCalled = false;
3369 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003370 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, UserHandle.myUserId(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 r.activity.getComponentName().getClassName());
3372 if (!r.activity.mCalled) {
3373 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003374 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003375 + " did not call through to super.onPause()");
3376 }
3377 } catch (SuperNotCalledException e) {
3378 throw e;
3379 } catch (Exception e) {
3380 if (!mInstrumentation.onException(r.activity, e)) {
3381 throw new RuntimeException(
3382 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003383 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384 + ": " + e.toString(), e);
3385 }
3386 }
3387 r.paused = true;
3388 }
3389 if (!r.stopped) {
3390 try {
3391 r.activity.performStop();
3392 } catch (SuperNotCalledException e) {
3393 throw e;
3394 } catch (Exception e) {
3395 if (!mInstrumentation.onException(r.activity, e)) {
3396 throw new RuntimeException(
3397 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003398 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 + ": " + e.toString(), e);
3400 }
3401 }
3402 r.stopped = true;
3403 }
3404 if (getNonConfigInstance) {
3405 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003406 r.lastNonConfigurationInstances
3407 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003408 } catch (Exception e) {
3409 if (!mInstrumentation.onException(r.activity, e)) {
3410 throw new RuntimeException(
3411 "Unable to retain activity "
3412 + r.intent.getComponent().toShortString()
3413 + ": " + e.toString(), e);
3414 }
3415 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 }
3417 try {
3418 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003419 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003420 if (!r.activity.mCalled) {
3421 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003422 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 " did not call through to super.onDestroy()");
3424 }
3425 if (r.window != null) {
3426 r.window.closeAllPanels();
3427 }
3428 } catch (SuperNotCalledException e) {
3429 throw e;
3430 } catch (Exception e) {
3431 if (!mInstrumentation.onException(r.activity, e)) {
3432 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003433 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3434 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 }
3436 }
3437 }
3438 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003439 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 return r;
3441 }
3442
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003443 private static String safeToComponentShortString(Intent intent) {
3444 ComponentName component = intent.getComponent();
3445 return component == null ? "[Unknown]" : component.toShortString();
3446 }
3447
Romain Guy65b345f2011-07-27 18:51:50 -07003448 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003450 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 configChanges, getNonConfigInstance);
3452 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003453 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003454 WindowManager wm = r.activity.getWindowManager();
3455 View v = r.activity.mDecor;
3456 if (v != null) {
3457 if (r.activity.mVisibleFromServer) {
3458 mNumVisibleActivities--;
3459 }
3460 IBinder wtoken = v.getWindowToken();
3461 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003462 if (r.onlyLocalRequest) {
3463 // Hold off on removing this until the new activity's
3464 // window is being added.
3465 r.mPendingRemoveWindow = v;
3466 r.mPendingRemoveWindowManager = wm;
3467 } else {
3468 wm.removeViewImmediate(v);
3469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003471 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003472 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 r.activity.getClass().getName(), "Activity");
3474 }
3475 r.activity.mDecor = null;
3476 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003477 if (r.mPendingRemoveWindow == null) {
3478 // If we are delaying the removal of the activity window, then
3479 // we can't clean up all windows here. Note that we can't do
3480 // so later either, which means any windows that aren't closed
3481 // by the app will leak. Well we try to warning them a lot
3482 // about leaking windows, because that is a bug, so if they are
3483 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07003484 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003485 r.activity.getClass().getName(), "Activity");
3486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487
3488 // Mocked out contexts won't be participating in the normal
3489 // process lifecycle, but if we're running with a proper
3490 // ApplicationContext we need to have it tear down things
3491 // cleanly.
3492 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003493 if (c instanceof ContextImpl) {
3494 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 r.activity.getClass().getName(), "Activity");
3496 }
3497 }
3498 if (finishing) {
3499 try {
3500 ActivityManagerNative.getDefault().activityDestroyed(token);
3501 } catch (RemoteException ex) {
3502 // If the system process has died, it's game over for everyone.
3503 }
3504 }
3505 }
3506
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003507 public final void requestRelaunchActivity(IBinder token,
3508 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3509 int configChanges, boolean notResumed, Configuration config,
3510 boolean fromServer) {
3511 ActivityClientRecord target = null;
3512
Craig Mautner88c05892013-06-28 09:47:45 -07003513 synchronized (mResourcesManager) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003514 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3515 ActivityClientRecord r = mRelaunchingActivities.get(i);
3516 if (r.token == token) {
3517 target = r;
3518 if (pendingResults != null) {
3519 if (r.pendingResults != null) {
3520 r.pendingResults.addAll(pendingResults);
3521 } else {
3522 r.pendingResults = pendingResults;
3523 }
3524 }
3525 if (pendingNewIntents != null) {
3526 if (r.pendingIntents != null) {
3527 r.pendingIntents.addAll(pendingNewIntents);
3528 } else {
3529 r.pendingIntents = pendingNewIntents;
3530 }
3531 }
3532 break;
3533 }
3534 }
3535
3536 if (target == null) {
3537 target = new ActivityClientRecord();
3538 target.token = token;
3539 target.pendingResults = pendingResults;
3540 target.pendingIntents = pendingNewIntents;
3541 if (!fromServer) {
3542 ActivityClientRecord existing = mActivities.get(token);
3543 if (existing != null) {
3544 target.startsNotResumed = existing.paused;
3545 }
3546 target.onlyLocalRequest = true;
3547 }
3548 mRelaunchingActivities.add(target);
3549 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3550 }
3551
3552 if (fromServer) {
3553 target.startsNotResumed = notResumed;
3554 target.onlyLocalRequest = false;
3555 }
3556 if (config != null) {
3557 target.createdConfig = config;
3558 }
3559 target.pendingConfigChanges |= configChanges;
3560 }
3561 }
3562
Romain Guy65b345f2011-07-27 18:51:50 -07003563 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003564 // If we are getting ready to gc after going to the background, well
3565 // we are back active so skip it.
3566 unscheduleGcIdler();
3567
3568 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003569 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 // First: make sure we have the most recent configuration and most
3572 // recent version of the activity, or skip it if some previous call
3573 // had taken a more recent version.
Craig Mautner88c05892013-06-28 09:47:45 -07003574 synchronized (mResourcesManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 int N = mRelaunchingActivities.size();
3576 IBinder token = tmp.token;
3577 tmp = null;
3578 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003579 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580 if (r.token == token) {
3581 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003582 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 mRelaunchingActivities.remove(i);
3584 i--;
3585 N--;
3586 }
3587 }
Bob Leee5408332009-09-04 18:31:17 -07003588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003590 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003591 return;
3592 }
Bob Leee5408332009-09-04 18:31:17 -07003593
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003594 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3595 + tmp.token + " with configChanges=0x"
3596 + Integer.toHexString(configChanges));
3597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 if (mPendingConfiguration != null) {
3599 changedConfig = mPendingConfiguration;
3600 mPendingConfiguration = null;
3601 }
3602 }
Bob Leee5408332009-09-04 18:31:17 -07003603
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003604 if (tmp.createdConfig != null) {
3605 // If the activity manager is passing us its current config,
3606 // assume that is really what we want regardless of what we
3607 // may have pending.
3608 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003609 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3610 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3611 if (changedConfig == null
3612 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3613 changedConfig = tmp.createdConfig;
3614 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003615 }
3616 }
3617
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003618 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003619 + tmp.token + ": changedConfig=" + changedConfig);
3620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003621 // If there was a pending configuration change, execute it first.
3622 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003623 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003624 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003625 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 }
Bob Leee5408332009-09-04 18:31:17 -07003627
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003628 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003629 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 if (r == null) {
3631 return;
3632 }
Bob Leee5408332009-09-04 18:31:17 -07003633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003635 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003636 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003637
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003638 r.activity.mChangingConfigurations = true;
3639
Dianne Hackborne2b04802010-12-09 09:24:55 -08003640 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003641 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003642 performPauseActivity(r.token, false, r.isPreHoneycomb());
3643 }
3644 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3645 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003646 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003647 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 }
Bob Leee5408332009-09-04 18:31:17 -07003649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003650 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652 r.activity = null;
3653 r.window = null;
3654 r.hideForNow = false;
3655 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003656 // Merge any pending results and pending intents; don't just replace them
3657 if (tmp.pendingResults != null) {
3658 if (r.pendingResults == null) {
3659 r.pendingResults = tmp.pendingResults;
3660 } else {
3661 r.pendingResults.addAll(tmp.pendingResults);
3662 }
3663 }
3664 if (tmp.pendingIntents != null) {
3665 if (r.pendingIntents == null) {
3666 r.pendingIntents = tmp.pendingIntents;
3667 } else {
3668 r.pendingIntents.addAll(tmp.pendingIntents);
3669 }
3670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003671 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003672
Christopher Tateb70f3df2009-04-07 16:07:59 -07003673 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003674 }
3675
Romain Guy65b345f2011-07-27 18:51:50 -07003676 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003677 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 Bitmap thumbnail = createThumbnailBitmap(r);
3679 CharSequence description = null;
3680 try {
3681 description = r.activity.onCreateDescription();
3682 } catch (Exception e) {
3683 if (!mInstrumentation.onException(r.activity, e)) {
3684 throw new RuntimeException(
3685 "Unable to create description of activity "
3686 + r.intent.getComponent().toShortString()
3687 + ": " + e.toString(), e);
3688 }
3689 }
3690 //System.out.println("Reporting top thumbnail " + thumbnail);
3691 try {
3692 ActivityManagerNative.getDefault().reportThumbnail(
3693 token, thumbnail, description);
3694 } catch (RemoteException ex) {
3695 }
3696 }
3697
Dianne Hackborn73c14162012-09-19 15:45:06 -07003698 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003700 ArrayList<ComponentCallbacks2> callbacks
3701 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003702
Craig Mautner88c05892013-06-28 09:47:45 -07003703 synchronized (mResourcesManager) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003704 final int NAPP = mAllApplications.size();
3705 for (int i=0; i<NAPP; i++) {
Dianne Hackborn73c14162012-09-19 15:45:06 -07003706 callbacks.add(mAllApplications.get(i));
3707 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003708 final int NACT = mActivities.size();
3709 for (int i=0; i<NACT; i++) {
3710 ActivityClientRecord ar = mActivities.valueAt(i);
3711 Activity a = ar.activity;
3712 if (a != null) {
3713 Configuration thisConfig = applyConfigCompatMainThread(
3714 mCurDefaultDisplayDpi, newConfig,
3715 ar.packageInfo.getCompatibilityInfo());
3716 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
3717 // If the activity is currently resumed, its configuration
3718 // needs to change right now.
3719 callbacks.add(a);
3720 } else if (thisConfig != null) {
3721 // Otherwise, we will tell it about the change
3722 // the next time it is resumed or shown. Note that
3723 // the activity manager may, before then, decide the
3724 // activity needs to be destroyed to handle its new
3725 // configuration.
3726 if (DEBUG_CONFIGURATION) {
3727 Slog.v(TAG, "Setting activity "
3728 + ar.activityInfo.name + " newConfig=" + thisConfig);
Romain Guya998dff2012-03-23 18:58:36 -07003729 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003730 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 }
3732 }
3733 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003734 final int NSVC = mServices.size();
3735 for (int i=0; i<NSVC; i++) {
3736 callbacks.add(mServices.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003737 }
3738 }
3739 synchronized (mProviderMap) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003740 final int NPRV = mLocalProviders.size();
3741 for (int i=0; i<NPRV; i++) {
3742 callbacks.add(mLocalProviders.valueAt(i).mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003743 }
3744 }
Bob Leee5408332009-09-04 18:31:17 -07003745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 return callbacks;
3747 }
Bob Leee5408332009-09-04 18:31:17 -07003748
Romain Guya998dff2012-03-23 18:58:36 -07003749 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003751 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003752 // we check the runtime type and act accordingly.
3753 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3754 if (activity != null) {
3755 activity.mCalled = false;
3756 }
Bob Leee5408332009-09-04 18:31:17 -07003757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 boolean shouldChangeConfig = false;
3759 if ((activity == null) || (activity.mCurrentConfig == null)) {
3760 shouldChangeConfig = true;
3761 } else {
Bob Leee5408332009-09-04 18:31:17 -07003762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 // If the new config is the same as the config this Activity
3764 // is already running with then don't bother calling
3765 // onConfigurationChanged
3766 int diff = activity.mCurrentConfig.diff(config);
3767 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003768 // If this activity doesn't handle any of the config changes
3769 // then don't bother calling onConfigurationChanged as we're
3770 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003771 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003772 shouldChangeConfig = true;
3773 }
3774 }
3775 }
Bob Leee5408332009-09-04 18:31:17 -07003776
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003777 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003778 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 if (shouldChangeConfig) {
3780 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003782 if (activity != null) {
3783 if (!activity.mCalled) {
3784 throw new SuperNotCalledException(
3785 "Activity " + activity.getLocalClassName() +
3786 " did not call through to super.onConfigurationChanged()");
3787 }
3788 activity.mConfigChangeFlags = 0;
3789 activity.mCurrentConfig = new Configuration(config);
3790 }
3791 }
3792 }
3793
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003794 public final void applyConfigurationToResources(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -07003795 synchronized (mResourcesManager) {
3796 mResourcesManager.applyConfigurationToResourcesLocked(config, null);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003797 }
3798 }
3799
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003800 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07003801 Configuration config = mConfiguration;
3802 if (mCompatConfiguration == null) {
3803 mCompatConfiguration = new Configuration();
3804 }
3805 mCompatConfiguration.setTo(mConfiguration);
Craig Mautner88c05892013-06-28 09:47:45 -07003806 if (mResourcesManager.applyCompatConfiguration(displayDensity, mCompatConfiguration)) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07003807 config = mCompatConfiguration;
3808 }
3809 return config;
3810 }
3811
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003812 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003813
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003814 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003815
Craig Mautner88c05892013-06-28 09:47:45 -07003816 synchronized (mResourcesManager) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003817 if (mPendingConfiguration != null) {
3818 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3819 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003820 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003821 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003822 }
3823 mPendingConfiguration = null;
3824 }
3825
3826 if (config == null) {
3827 return;
3828 }
3829
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003830 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003831 + config);
Craig Mautner88c05892013-06-28 09:47:45 -07003832
3833 mResourcesManager.applyConfigurationToResourcesLocked(config, compat);
3834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003835 if (mConfiguration == null) {
3836 mConfiguration = new Configuration();
3837 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003838 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003839 return;
3840 }
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003841 configDiff = mConfiguration.diff(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003842 mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003843 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07003845
3846 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
3847
Romain Guy65b345f2011-07-27 18:51:50 -07003848 // Cleanup hardware accelerated stuff
Jeff Brown98365d72012-08-19 20:30:52 -07003849 WindowManagerGlobal.getInstance().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003850
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003851 freeTextLayoutCachesIfNeeded(configDiff);
3852
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003853 if (callbacks != null) {
3854 final int N = callbacks.size();
3855 for (int i=0; i<N; i++) {
3856 performConfigurationChanged(callbacks.get(i), config);
3857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 }
3859 }
3860
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003861 final void freeTextLayoutCachesIfNeeded(int configDiff) {
3862 if (configDiff != 0) {
3863 // Ask text layout engine to free its caches if there is a locale change
3864 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
3865 if (hasLocaleConfigChange) {
3866 Canvas.freeTextLayoutCaches();
3867 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
3868 }
3869 }
3870 }
3871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003873 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003874 if (r == null || r.activity == null) {
3875 return;
3876 }
Bob Leee5408332009-09-04 18:31:17 -07003877
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003878 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003879 + r.activityInfo.name);
3880
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003881 performConfigurationChanged(r.activity, mCompatConfiguration);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003882
3883 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 }
3885
Romain Guy7eabe552011-07-21 14:56:34 -07003886 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003887 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003888 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003889 switch (profileType) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003890 default:
3891 mProfiler.setProfiler(pcd.path, pcd.fd);
3892 mProfiler.autoStopProfiler = false;
3893 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003894 break;
3895 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003896 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003897 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003898 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003899 } finally {
3900 try {
3901 pcd.fd.close();
3902 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003903 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003904 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003905 }
3906 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003907 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07003908 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003909 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003910 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003911 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003912 }
3913 }
Bob Leee5408332009-09-04 18:31:17 -07003914
Romain Guya998dff2012-03-23 18:58:36 -07003915 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07003916 if (managed) {
3917 try {
3918 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3919 } catch (IOException e) {
3920 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3921 + " -- can the process access this path?");
3922 } finally {
3923 try {
3924 dhd.fd.close();
3925 } catch (IOException e) {
3926 Slog.w(TAG, "Failure closing profile fd", e);
3927 }
3928 }
3929 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003930 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003931 }
3932 }
3933
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003934 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3935 boolean hasPkgInfo = false;
3936 if (packages != null) {
3937 for (int i=packages.length-1; i>=0; i--) {
3938 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3939 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003940 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003941 ref = mPackages.get(packages[i]);
3942 if (ref != null && ref.get() != null) {
3943 hasPkgInfo = true;
3944 } else {
3945 ref = mResourcePackages.get(packages[i]);
3946 if (ref != null && ref.get() != null) {
3947 hasPkgInfo = true;
3948 }
3949 }
3950 }
3951 mPackages.remove(packages[i]);
3952 mResourcePackages.remove(packages[i]);
3953 }
3954 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003955 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003956 hasPkgInfo);
3957 }
3958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003959 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07003960 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07003961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003962 final int N = callbacks.size();
3963 for (int i=0; i<N; i++) {
3964 callbacks.get(i).onLowMemory();
3965 }
3966
Chris Tatece229052009-03-25 16:44:52 -07003967 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3968 if (Process.myUid() != Process.SYSTEM_UID) {
3969 int sqliteReleased = SQLiteDatabase.releaseMemory();
3970 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3971 }
Bob Leee5408332009-09-04 18:31:17 -07003972
Mike Reedcaf0df12009-04-27 14:32:05 -04003973 // Ask graphics to free up as much as possible (font/image caches)
3974 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003975
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003976 // Ask text layout engine to free also as much as possible
3977 Canvas.freeTextLayoutCaches();
3978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 BinderInternal.forceGc("mem");
3980 }
3981
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003982 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003983 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003984
Jeff Brown98365d72012-08-19 20:30:52 -07003985 final WindowManagerGlobal windowManager = WindowManagerGlobal.getInstance();
Romain Guy19f86e82012-04-23 15:19:07 -07003986 windowManager.startTrimMemory(level);
3987
Dianne Hackborn73c14162012-09-19 15:45:06 -07003988 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003989
3990 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07003991 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003992 callbacks.get(i).onTrimMemory(level);
3993 }
Romain Guy19f86e82012-04-23 15:19:07 -07003994
Jeff Brown98365d72012-08-19 20:30:52 -07003995 windowManager.endTrimMemory();
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003996 }
3997
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003998 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003999 if (Process.isIsolated()) {
4000 // Isolated processes aren't going to do UI.
4001 return;
4002 }
Romain Guya9582652011-11-10 14:20:10 -08004003 try {
4004 int uid = Process.myUid();
4005 String[] packages = getPackageManager().getPackagesForUid(uid);
4006
4007 // If there are several packages in this application we won't
4008 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004009 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004010 HardwareRenderer.setupDiskCache(cacheDir);
4011 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08004012 }
4013 } catch (RemoteException e) {
4014 // Ignore
4015 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07004016 }
4017
4018 private void updateDefaultDensity() {
4019 if (mCurDefaultDisplayDpi != Configuration.DENSITY_DPI_UNDEFINED
4020 && mCurDefaultDisplayDpi != DisplayMetrics.DENSITY_DEVICE
4021 && !mDensityCompatMode) {
4022 Slog.i(TAG, "Switching default density from "
4023 + DisplayMetrics.DENSITY_DEVICE + " to "
4024 + mCurDefaultDisplayDpi);
4025 DisplayMetrics.DENSITY_DEVICE = mCurDefaultDisplayDpi;
4026 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4027 }
4028 }
4029
Romain Guy65b345f2011-07-27 18:51:50 -07004030 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004031 mBoundApplication = data;
4032 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004033 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004035 mProfiler = new Profiler();
4036 mProfiler.profileFile = data.initProfileFile;
4037 mProfiler.profileFd = data.initProfileFd;
4038 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
4039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08004041 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004042 android.ddm.DdmHandleAppName.setAppName(data.processName,
4043 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004044
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004045 if (data.persistent) {
4046 // Persistent processes on low-memory devices do not get to
4047 // use hardware accelerated drawing, since this can add too much
4048 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07004049 if (!ActivityManager.isHighEndGfx()) {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004050 HardwareRenderer.disable(false);
4051 }
4052 }
Romain Guya9582652011-11-10 14:20:10 -08004053
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004054 if (mProfiler.profileFd != null) {
4055 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004056 }
4057
Joe Onoratod630f102011-03-17 18:42:26 -07004058 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
4059 // implementation to use the pool executor. Normally, we use the
4060 // serialized executor as the default. This has to happen in the
4061 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07004062 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07004063 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
4064 }
4065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 /*
4067 * Before spawning a new process, reset the time zone to be the system time zone.
4068 * This needs to be done because the system time zone could have changed after the
4069 * the spawning of this process. Without doing this this process would have the incorrect
4070 * system time zone.
4071 */
4072 TimeZone.setDefault(null);
4073
4074 /*
4075 * Initialize the default locale in this process for the reasons we set the time zone.
4076 */
4077 Locale.setDefault(data.config.locale);
4078
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004079 /*
4080 * Update the system configuration since its preloaded and might not
4081 * reflect configuration changes. The configuration object passed
4082 * in AppBindData can be safely assumed to be up to date
4083 */
Craig Mautner88c05892013-06-28 09:47:45 -07004084 mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004085 mCurDefaultDisplayDpi = data.config.densityDpi;
4086 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004087
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004088 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089
Dianne Hackborndde331c2012-08-03 14:01:57 -07004090 /**
4091 * Switch this process to density compatibility mode if needed.
4092 */
4093 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4094 == 0) {
4095 mDensityCompatMode = true;
4096 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4097 }
4098 updateDefaultDensity();
4099
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004100 final ContextImpl appContext = new ContextImpl();
4101 appContext.init(data.info, null, this);
Amith Yamasani11de39a2012-08-17 18:00:52 -07004102 if (!Process.isIsolated()) {
4103 final File cacheDir = appContext.getCacheDir();
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004104
Amith Yamasani92d57052012-08-23 10:07:52 -07004105 if (cacheDir != null) {
4106 // Provide a usable directory for temporary files
4107 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
4108
4109 setupGraphicsSupport(data.info, cacheDir);
4110 } else {
4111 Log.e(TAG, "Unable to setupGraphicsSupport due to missing cache directory");
4112 }
Amith Yamasani11de39a2012-08-17 18:00:52 -07004113 }
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004114 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004115 * For system applications on userdebug/eng builds, log stack
4116 * traces of disk and network access to dropbox for analysis.
4117 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004118 if ((data.appInfo.flags &
4119 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004120 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4121 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004122 }
4123
4124 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004125 * For apps targetting SDK Honeycomb or later, we don't allow
4126 * network usage on the main event loop / UI thread.
4127 *
4128 * Note to those grepping: this is what ultimately throws
4129 * NetworkOnMainThreadException ...
4130 */
4131 if (data.appInfo.targetSdkVersion > 9) {
4132 StrictMode.enableDeathOnNetwork();
4133 }
4134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4136 // XXX should have option to change the port.
4137 Debug.changeDebugPort(8100);
4138 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004139 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140 + " is waiting for the debugger on port 8100...");
4141
4142 IActivityManager mgr = ActivityManagerNative.getDefault();
4143 try {
4144 mgr.showWaitingForDebugger(mAppThread, true);
4145 } catch (RemoteException ex) {
4146 }
4147
4148 Debug.waitForDebugger();
4149
4150 try {
4151 mgr.showWaitingForDebugger(mAppThread, false);
4152 } catch (RemoteException ex) {
4153 }
4154
4155 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004156 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004157 + " can be debugged on port 8100...");
4158 }
4159 }
4160
Siva Velusamy92a8b222012-03-09 16:24:04 -08004161 // Enable OpenGL tracing if required
4162 if (data.enableOpenGlTrace) {
Siva Velusamy0c1761b2012-12-14 17:09:06 -08004163 GLUtils.setTracingLevel(1);
Siva Velusamy92a8b222012-03-09 16:24:04 -08004164 }
4165
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07004166 // Allow application-generated systrace messages if we're debuggable.
4167 boolean appTracingAllowed = (data.appInfo.flags&ApplicationInfo.FLAG_DEBUGGABLE) != 0;
4168 Trace.setAppTracingAllowed(appTracingAllowed);
4169
Robert Greenwalt434203a2010-10-11 16:00:27 -07004170 /**
4171 * Initialize the default http proxy in this process for the reasons we set the time zone.
4172 */
4173 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004174 if (b != null) {
4175 // In pre-boot mode (doing initial launch to collect password), not
4176 // all system is up. This includes the connectivity service, so don't
4177 // crash if we can't get it.
4178 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4179 try {
4180 ProxyProperties proxyProperties = service.getProxy();
4181 Proxy.setHttpProxySystemProperty(proxyProperties);
4182 } catch (RemoteException e) {}
4183 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 InstrumentationInfo ii = null;
4187 try {
4188 ii = appContext.getPackageManager().
4189 getInstrumentationInfo(data.instrumentationName, 0);
4190 } catch (PackageManager.NameNotFoundException e) {
4191 }
4192 if (ii == null) {
4193 throw new RuntimeException(
4194 "Unable to find instrumentation info for: "
4195 + data.instrumentationName);
4196 }
4197
4198 mInstrumentationAppDir = ii.sourceDir;
Brian Carlstromd893a892012-04-01 21:30:26 -07004199 mInstrumentationAppLibraryDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004200 mInstrumentationAppPackage = ii.packageName;
4201 mInstrumentedAppDir = data.info.getAppDir();
Brian Carlstromd893a892012-04-01 21:30:26 -07004202 mInstrumentedAppLibraryDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004203
4204 ApplicationInfo instrApp = new ApplicationInfo();
4205 instrApp.packageName = ii.packageName;
4206 instrApp.sourceDir = ii.sourceDir;
4207 instrApp.publicSourceDir = ii.publicSourceDir;
4208 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004209 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004210 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08004212 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 instrContext.init(pi, null, this);
4214
4215 try {
4216 java.lang.ClassLoader cl = instrContext.getClassLoader();
4217 mInstrumentation = (Instrumentation)
4218 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4219 } catch (Exception e) {
4220 throw new RuntimeException(
4221 "Unable to instantiate instrumentation "
4222 + data.instrumentationName + ": " + e.toString(), e);
4223 }
4224
4225 mInstrumentation.init(this, instrContext, appContext,
Svetoslav Ganov80943d82013-01-02 10:25:37 -08004226 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher,
4227 data.instrumentationUiAutomationConnection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004228
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004229 if (mProfiler.profileFile != null && !ii.handleProfiling
4230 && mProfiler.profileFd == null) {
4231 mProfiler.handlingProfiling = true;
4232 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233 file.getParentFile().mkdirs();
4234 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4235 }
4236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 } else {
4238 mInstrumentation = new Instrumentation();
4239 }
4240
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004241 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004242 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004243 }
4244
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004245 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004246 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004247 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004248 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004249 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004250 // If the app is being launched for full backup or restore, bring it up in
4251 // a restricted environment with the base application class.
4252 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4253 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004254
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004255 // don't bring up providers in restricted mode; they may depend on the
4256 // app's custom Application class
4257 if (!data.restrictedBackupMode) {
4258 List<ProviderInfo> providers = data.providers;
4259 if (providers != null) {
4260 installContentProviders(app, providers);
4261 // For process that contains content providers, we want to
4262 // ensure that the JIT is enabled "at some point".
4263 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4264 }
4265 }
4266
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004267 // Do this after providers, since instrumentation tests generally start their
4268 // test thread at this point, and we don't want that racing.
4269 try {
4270 mInstrumentation.onCreate(data.instrumentationArgs);
4271 }
4272 catch (Exception e) {
4273 throw new RuntimeException(
4274 "Exception thrown in onCreate() of "
4275 + data.instrumentationName + ": " + e.toString(), e);
4276 }
4277
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004278 try {
4279 mInstrumentation.callApplicationOnCreate(app);
4280 } catch (Exception e) {
4281 if (!mInstrumentation.onException(app, e)) {
4282 throw new RuntimeException(
4283 "Unable to create application " + app.getClass().getName()
4284 + ": " + e.toString(), e);
4285 }
4286 }
4287 } finally {
4288 StrictMode.setThreadPolicy(savedPolicy);
4289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004290 }
4291
4292 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4293 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004294 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4295 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004296 Debug.stopMethodTracing();
4297 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004298 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004299 // + ", app thr: " + mAppThread);
4300 try {
4301 am.finishInstrumentation(mAppThread, resultCode, results);
4302 } catch (RemoteException ex) {
4303 }
4304 }
4305
Romain Guy65b345f2011-07-27 18:51:50 -07004306 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 Context context, List<ProviderInfo> providers) {
4308 final ArrayList<IActivityManager.ContentProviderHolder> results =
4309 new ArrayList<IActivityManager.ContentProviderHolder>();
4310
Romain Guya998dff2012-03-23 18:58:36 -07004311 for (ProviderInfo cpi : providers) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08004312 if (DEBUG_PROVIDER) {
4313 StringBuilder buf = new StringBuilder(128);
4314 buf.append("Pub ");
4315 buf.append(cpi.authority);
4316 buf.append(": ");
4317 buf.append(cpi.name);
4318 Log.i(TAG, buf.toString());
4319 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004320 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
4321 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
4322 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004323 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004325 }
4326 }
4327
4328 try {
4329 ActivityManagerNative.getDefault().publishContentProviders(
4330 getApplicationThread(), results);
4331 } catch (RemoteException ex) {
4332 }
4333 }
4334
Jeff Sharkey6d515712012-09-20 16:06:08 -07004335 public final IContentProvider acquireProvider(
4336 Context c, String auth, int userId, boolean stable) {
4337 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004338 if (provider != null) {
4339 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004340 }
4341
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004342 // There is a possible race here. Another thread may try to acquire
4343 // the same provider at the same time. When this happens, we want to ensure
4344 // that the first one wins.
4345 // Note that we cannot hold the lock while acquiring and installing the
4346 // provider since it might take a long time to run and it could also potentially
4347 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004348 IActivityManager.ContentProviderHolder holder = null;
4349 try {
4350 holder = ActivityManagerNative.getDefault().getContentProvider(
Jeff Sharkey6d515712012-09-20 16:06:08 -07004351 getApplicationThread(), auth, userId, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004352 } catch (RemoteException ex) {
4353 }
4354 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004355 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004356 return null;
4357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004359 // Install provider will increment the reference count for us, and break
4360 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004361 holder = installProvider(c, holder, holder.info,
4362 true /*noisy*/, holder.noReleaseNeeded, stable);
4363 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004364 }
4365
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004366 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
4367 if (stable) {
4368 prc.stableCount += 1;
4369 if (prc.stableCount == 1) {
4370 // We are acquiring a new stable reference on the provider.
4371 int unstableDelta;
4372 if (prc.removePending) {
4373 // We have a pending remove operation, which is holding the
4374 // last unstable reference. At this point we are converting
4375 // that unstable reference to our new stable reference.
4376 unstableDelta = -1;
4377 // Cancel the removal of the provider.
4378 if (DEBUG_PROVIDER) {
4379 Slog.v(TAG, "incProviderRef: stable "
4380 + "snatched provider from the jaws of death");
4381 }
4382 prc.removePending = false;
Guobin Zhang9e3e52662013-03-21 13:57:11 +08004383 // There is a race! It fails to remove the message, which
4384 // will be handled in completeRemoveProvider().
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004385 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4386 } else {
4387 unstableDelta = 0;
4388 }
4389 try {
4390 if (DEBUG_PROVIDER) {
4391 Slog.v(TAG, "incProviderRef Now stable - "
4392 + prc.holder.info.name + ": unstableDelta="
4393 + unstableDelta);
4394 }
4395 ActivityManagerNative.getDefault().refContentProvider(
4396 prc.holder.connection, 1, unstableDelta);
4397 } catch (RemoteException e) {
4398 //do nothing content provider object is dead any way
4399 }
4400 }
4401 } else {
4402 prc.unstableCount += 1;
4403 if (prc.unstableCount == 1) {
4404 // We are acquiring a new unstable reference on the provider.
4405 if (prc.removePending) {
4406 // Oh look, we actually have a remove pending for the
4407 // provider, which is still holding the last unstable
4408 // reference. We just need to cancel that to take new
4409 // ownership of the reference.
4410 if (DEBUG_PROVIDER) {
4411 Slog.v(TAG, "incProviderRef: unstable "
4412 + "snatched provider from the jaws of death");
4413 }
4414 prc.removePending = false;
4415 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4416 } else {
4417 // First unstable ref, increment our count in the
4418 // activity manager.
4419 try {
4420 if (DEBUG_PROVIDER) {
4421 Slog.v(TAG, "incProviderRef: Now unstable - "
4422 + prc.holder.info.name);
4423 }
4424 ActivityManagerNative.getDefault().refContentProvider(
4425 prc.holder.connection, 0, 1);
4426 } catch (RemoteException e) {
4427 //do nothing content provider object is dead any way
4428 }
4429 }
4430 }
4431 }
4432 }
4433
Jeff Sharkey6d515712012-09-20 16:06:08 -07004434 public final IContentProvider acquireExistingProvider(
4435 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004436 synchronized (mProviderMap) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004437 final ProviderKey key = new ProviderKey(auth, userId);
4438 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004439 if (pr == null) {
4440 return null;
4441 }
4442
4443 IContentProvider provider = pr.mProvider;
4444 IBinder jBinder = provider.asBinder();
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004445 if (!jBinder.isBinderAlive()) {
4446 // The hosting process of the provider has died; we can't
4447 // use this one.
4448 Log.i(TAG, "Acquiring provider " + auth + " for user " + userId
4449 + ": existing object's process dead");
4450 handleUnstableProviderDiedLocked(jBinder, true);
4451 return null;
4452 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004453
4454 // Only increment the ref count if we have one. If we don't then the
4455 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004456 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004457 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004458 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004459 }
4460 return provider;
4461 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004462 }
4463
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004464 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
4465 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 return false;
4467 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004470 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004471 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004472 if (prc == null) {
4473 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004474 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004475 }
4476
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004477 boolean lastRef = false;
4478 if (stable) {
4479 if (prc.stableCount == 0) {
4480 if (DEBUG_PROVIDER) Slog.v(TAG,
4481 "releaseProvider: stable ref count already 0, how?");
4482 return false;
4483 }
4484 prc.stableCount -= 1;
4485 if (prc.stableCount == 0) {
4486 // What we do at this point depends on whether there are
4487 // any unstable refs left: if there are, we just tell the
4488 // activity manager to decrement its stable count; if there
4489 // aren't, we need to enqueue this provider to be removed,
4490 // and convert to holding a single unstable ref while
4491 // doing so.
4492 lastRef = prc.unstableCount == 0;
4493 try {
4494 if (DEBUG_PROVIDER) {
4495 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
4496 + lastRef + " - " + prc.holder.info.name);
4497 }
4498 ActivityManagerNative.getDefault().refContentProvider(
4499 prc.holder.connection, -1, lastRef ? 1 : 0);
4500 } catch (RemoteException e) {
4501 //do nothing content provider object is dead any way
4502 }
4503 }
4504 } else {
4505 if (prc.unstableCount == 0) {
4506 if (DEBUG_PROVIDER) Slog.v(TAG,
4507 "releaseProvider: unstable ref count already 0, how?");
4508 return false;
4509 }
4510 prc.unstableCount -= 1;
4511 if (prc.unstableCount == 0) {
4512 // If this is the last reference, we need to enqueue
4513 // this provider to be removed instead of telling the
4514 // activity manager to remove it at this point.
4515 lastRef = prc.stableCount == 0;
4516 if (!lastRef) {
4517 try {
4518 if (DEBUG_PROVIDER) {
4519 Slog.v(TAG, "releaseProvider: No longer unstable - "
4520 + prc.holder.info.name);
4521 }
4522 ActivityManagerNative.getDefault().refContentProvider(
4523 prc.holder.connection, 0, -1);
4524 } catch (RemoteException e) {
4525 //do nothing content provider object is dead any way
4526 }
4527 }
4528 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004529 }
4530
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004531 if (lastRef) {
4532 if (!prc.removePending) {
4533 // Schedule the actual remove asynchronously, since we don't know the context
4534 // this will be called in.
4535 // TODO: it would be nice to post a delayed message, so
4536 // if we come back and need the same provider quickly
4537 // we will still have it available.
4538 if (DEBUG_PROVIDER) {
4539 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
4540 + prc.holder.info.name);
4541 }
4542 prc.removePending = true;
4543 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
4544 mH.sendMessage(msg);
4545 } else {
4546 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
4547 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004548 }
4549 return true;
4550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004551 }
4552
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004553 final void completeRemoveProvider(ProviderRefCount prc) {
4554 synchronized (mProviderMap) {
4555 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004556 // There was a race! Some other client managed to acquire
4557 // the provider before the removal was completed.
4558 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004559 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004560 + "provider still in use");
4561 return;
4562 }
4563
Guobin Zhang9e3e52662013-03-21 13:57:11 +08004564 // More complicated race!! Some client managed to acquire the
4565 // provider and release it before the removal was completed.
4566 // Continue the removal, and abort the next remove message.
4567 prc.removePending = false;
4568
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004569 final IBinder jBinder = prc.holder.provider.asBinder();
4570 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
4571 if (existingPrc == prc) {
4572 mProviderRefCountMap.remove(jBinder);
4573 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004574
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004575 for (int i=mProviderMap.size()-1; i>=0; i--) {
4576 ProviderClientRecord pr = mProviderMap.valueAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004577 IBinder myBinder = pr.mProvider.asBinder();
4578 if (myBinder == jBinder) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004579 mProviderMap.removeAt(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004580 }
4581 }
4582 }
4583
4584 try {
4585 if (DEBUG_PROVIDER) {
4586 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4587 + "removeContentProvider(" + prc.holder.info.name + ")");
4588 }
4589 ActivityManagerNative.getDefault().removeContentProvider(
4590 prc.holder.connection, false);
4591 } catch (RemoteException e) {
4592 //do nothing content provider object is dead any way
4593 }
4594 }
4595
4596 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004597 synchronized (mProviderMap) {
4598 handleUnstableProviderDiedLocked(provider, fromClient);
4599 }
4600 }
4601
4602 final void handleUnstableProviderDiedLocked(IBinder provider, boolean fromClient) {
4603 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4604 if (prc != null) {
4605 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
4606 + provider + " " + prc.holder.info.name);
4607 mProviderRefCountMap.remove(provider);
4608 if (prc.client != null && prc.client.mNames != null) {
4609 for (String name : prc.client.mNames) {
4610 ProviderClientRecord pr = mProviderMap.get(name);
4611 if (pr != null && pr.mProvider.asBinder() == provider) {
4612 Slog.i(TAG, "Removing dead content provider: " + name);
4613 mProviderMap.remove(name);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004614 }
4615 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004616 }
4617 if (fromClient) {
4618 // We found out about this due to execution in our client
4619 // code. Tell the activity manager about it now, to ensure
4620 // that the next time we go to do anything with the provider
4621 // it knows it is dead (so we don't race with its death
4622 // notification).
4623 try {
4624 ActivityManagerNative.getDefault().unstableProviderDied(
4625 prc.holder.connection);
4626 } catch (RemoteException e) {
4627 //do nothing content provider object is dead any way
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004628 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004629 }
4630 }
4631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004632
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004633 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Jeff Sharkey6d515712012-09-20 16:06:08 -07004634 ContentProvider localProvider, IActivityManager.ContentProviderHolder holder) {
4635 final String auths[] = PATTERN_SEMICOLON.split(holder.info.authority);
4636 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
4637
4638 final ProviderClientRecord pcr = new ProviderClientRecord(
4639 auths, provider, localProvider, holder);
4640 for (String auth : auths) {
4641 final ProviderKey key = new ProviderKey(auth, userId);
4642 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004643 if (existing != null) {
4644 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07004645 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004646 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004647 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 }
4649 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004650 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004651 }
4652
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004653 /**
4654 * Installs the provider.
4655 *
4656 * Providers that are local to the process or that come from the system server
4657 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4658 * Other remote providers are reference counted. The initial reference count
4659 * for all reference counted providers is one. Providers that are not reference
4660 * counted do not have a reference count (at all).
4661 *
4662 * This method detects when a provider has already been installed. When this happens,
4663 * it increments the reference count of the existing provider (if appropriate)
4664 * and returns the existing provider. This can happen due to concurrent
4665 * attempts to acquire the same provider.
4666 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004667 private IActivityManager.ContentProviderHolder installProvider(Context context,
4668 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
4669 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004670 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004671 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07004672 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004673 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004674 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004675 + info.name);
4676 }
4677 Context c = null;
4678 ApplicationInfo ai = info.applicationInfo;
4679 if (context.getPackageName().equals(ai.packageName)) {
4680 c = context;
4681 } else if (mInitialApplication != null &&
4682 mInitialApplication.getPackageName().equals(ai.packageName)) {
4683 c = mInitialApplication;
4684 } else {
4685 try {
4686 c = context.createPackageContext(ai.packageName,
4687 Context.CONTEXT_INCLUDE_CODE);
4688 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004689 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004690 }
4691 }
4692 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004693 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004694 ai.packageName +
4695 " while loading content provider " +
4696 info.name);
4697 return null;
4698 }
4699 try {
4700 final java.lang.ClassLoader cl = c.getClassLoader();
4701 localProvider = (ContentProvider)cl.
4702 loadClass(info.name).newInstance();
4703 provider = localProvider.getIContentProvider();
4704 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004705 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004706 info.name + " from sourceDir " +
4707 info.applicationInfo.sourceDir);
4708 return null;
4709 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004710 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004711 TAG, "Instantiating local provider " + info.name);
4712 // XXX Need to create the correct context for this provider.
4713 localProvider.attachInfo(c, info);
4714 } catch (java.lang.Exception e) {
4715 if (!mInstrumentation.onException(null, e)) {
4716 throw new RuntimeException(
4717 "Unable to get provider " + info.name
4718 + ": " + e.toString(), e);
4719 }
4720 return null;
4721 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004722 } else {
4723 provider = holder.provider;
4724 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004725 + info.name);
4726 }
4727
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004728 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004729
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004730 synchronized (mProviderMap) {
4731 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
4732 + " / " + info.name);
4733 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004734 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004735 ComponentName cname = new ComponentName(info.packageName, info.name);
4736 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004737 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004738 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004739 Slog.v(TAG, "installProvider: lost the race, "
4740 + "using existing local provider");
4741 }
4742 provider = pr.mProvider;
4743 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004744 holder = new IActivityManager.ContentProviderHolder(info);
4745 holder.provider = provider;
4746 holder.noReleaseNeeded = true;
4747 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004748 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004749 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004750 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004751 retHolder = pr.mHolder;
4752 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004753 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4754 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004755 if (DEBUG_PROVIDER) {
4756 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004757 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004758 // We need to transfer our new reference to the existing
4759 // ref count, releasing the old one... but only if
4760 // release is needed (that is, it is not running in the
4761 // system process).
4762 if (!noReleaseNeeded) {
4763 incProviderRefLocked(prc, stable);
4764 try {
4765 ActivityManagerNative.getDefault().removeContentProvider(
4766 holder.connection, stable);
4767 } catch (RemoteException e) {
4768 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004769 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004770 }
4771 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004772 ProviderClientRecord client = installProviderAuthoritiesLocked(
4773 provider, localProvider, holder);
4774 if (noReleaseNeeded) {
4775 prc = new ProviderRefCount(holder, client, 1000, 1000);
4776 } else {
4777 prc = stable
4778 ? new ProviderRefCount(holder, client, 1, 0)
4779 : new ProviderRefCount(holder, client, 0, 1);
4780 }
4781 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004782 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004783 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004784 }
4785 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004786
4787 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004788 }
4789
Romain Guy65b345f2011-07-27 18:51:50 -07004790 private void attach(boolean system) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08004791 sCurrentActivityThread = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004792 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004793 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004794 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Craig Mautner88c05892013-06-28 09:47:45 -07004795 @Override
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004796 public void run() {
4797 ensureJitEnabled();
4798 }
4799 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004800 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
4801 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004802 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4803 IActivityManager mgr = ActivityManagerNative.getDefault();
4804 try {
4805 mgr.attachApplication(mAppThread);
4806 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004807 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004808 }
4809 } else {
4810 // Don't set application object here -- if the system crashes,
4811 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004812 android.ddm.DdmHandleAppName.setAppName("system_process",
4813 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004814 try {
4815 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004816 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004817 context.init(getSystemContext().mPackageInfo, null, this);
4818 Application app = Instrumentation.newApplication(Application.class, context);
4819 mAllApplications.add(app);
4820 mInitialApplication = app;
4821 app.onCreate();
4822 } catch (Exception e) {
4823 throw new RuntimeException(
4824 "Unable to instantiate Application():" + e.toString(), e);
4825 }
4826 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07004827
4828 // add dropbox logging to libcore
4829 DropBox.setReporter(new DropBoxReporter());
4830
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004831 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Craig Mautner88c05892013-06-28 09:47:45 -07004832 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004833 public void onConfigurationChanged(Configuration newConfig) {
Craig Mautner88c05892013-06-28 09:47:45 -07004834 synchronized (mResourcesManager) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004835 // We need to apply this change to the resources
4836 // immediately, because upon returning the view
4837 // hierarchy will be informed about it.
Craig Mautner88c05892013-06-28 09:47:45 -07004838 if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004839 // This actually changed the resources! Tell
4840 // everyone about it.
4841 if (mPendingConfiguration == null ||
4842 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4843 mPendingConfiguration = newConfig;
4844
4845 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4846 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004847 }
4848 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004849 }
Craig Mautner88c05892013-06-28 09:47:45 -07004850 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004851 public void onLowMemory() {
4852 }
Craig Mautner88c05892013-06-28 09:47:45 -07004853 @Override
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004854 public void onTrimMemory(int level) {
4855 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004856 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004857 }
4858
Romain Guy5e9120d2012-01-30 12:17:22 -08004859 public static ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004860 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004861 ActivityThread thread = new ActivityThread();
4862 thread.attach(true);
4863 return thread;
4864 }
4865
Jeff Brown10e89712011-07-08 18:52:57 -07004866 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004867 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004868 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004869 }
4870 }
4871
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004872 public int getIntCoreSetting(String key, int defaultValue) {
Craig Mautner88c05892013-06-28 09:47:45 -07004873 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004874 if (mCoreSettings != null) {
4875 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004876 }
Craig Mautner88c05892013-06-28 09:47:45 -07004877 return defaultValue;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004878 }
4879 }
4880
Geremy Condra69689a72012-09-11 16:57:17 -07004881 private static class EventLoggingReporter implements EventLogger.Reporter {
4882 @Override
4883 public void report (int code, Object... list) {
4884 EventLog.writeEvent(code, list);
4885 }
4886 }
4887
Geremy Condrab7faaf42012-09-19 18:07:42 -07004888 private class DropBoxReporter implements DropBox.Reporter {
4889
4890 private DropBoxManager dropBox;
4891
4892 public DropBoxReporter() {
4893 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
4894 }
4895
4896 @Override
4897 public void addData(String tag, byte[] data, int flags) {
4898 dropBox.addData(tag, data, flags);
4899 }
4900
4901 @Override
4902 public void addText(String tag, String data) {
4903 dropBox.addText(tag, data);
4904 }
4905 }
4906
Romain Guy65b345f2011-07-27 18:51:50 -07004907 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004908 SamplingProfilerIntegration.start();
4909
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004910 // CloseGuard defaults to true and can be quite spammy. We
4911 // disable it here, but selectively enable it later (via
4912 // StrictMode) on debug builds, but using DropBox, not logs.
4913 CloseGuard.setEnabled(false);
4914
Jeff Sharkeyb049e212012-09-07 23:16:01 -07004915 Environment.initForCurrentUser();
4916
Geremy Condra69689a72012-09-11 16:57:17 -07004917 // Set the reporter for event logging in libcore
4918 EventLogger.setReporter(new EventLoggingReporter());
4919
Kenny Root8b514752013-02-04 09:35:16 -08004920 Security.addProvider(new AndroidKeyStoreProvider());
4921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004922 Process.setArgV0("<pre-initialized>");
4923
4924 Looper.prepareMainLooper();
4925
4926 ActivityThread thread = new ActivityThread();
4927 thread.attach(false);
4928
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07004929 if (sMainThreadHandler == null) {
4930 sMainThreadHandler = thread.getHandler();
4931 }
4932
Romain Guy5e9120d2012-01-30 12:17:22 -08004933 AsyncTask.init();
4934
Dianne Hackborn287952c2010-09-22 22:34:31 -07004935 if (false) {
4936 Looper.myLooper().setMessageLogging(new
4937 LogPrinter(Log.DEBUG, "ActivityThread"));
4938 }
4939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004940 Looper.loop();
4941
Jeff Brown10e89712011-07-08 18:52:57 -07004942 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004943 }
4944}