blob: b038f9e75e69523d5aadf931d14c4fa8eea013ed [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;
Adam Powell14874662013-07-18 19:42:41 -070079import android.util.SuperNotCalledException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070081import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.view.View;
83import android.view.ViewDebug;
84import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070085import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.view.Window;
87import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -070088import android.view.WindowManagerGlobal;
Jason Samsa6f338c2012-02-24 16:22:16 -080089import android.renderscript.RenderScript;
Kenny Root8b514752013-02-04 09:35:16 -080090import android.security.AndroidKeyStoreProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92import com.android.internal.os.BinderInternal;
93import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070094import com.android.internal.os.SamplingProfilerIntegration;
Dianne Hackborn8c841092013-06-24 13:46:13 -070095import com.android.internal.util.FastPrintWriter;
Kenny Root12e75222013-04-23 22:34:24 -070096import com.android.org.conscrypt.OpenSSLSocketImpl;
Jeff Sharkeydd97f422013-10-08 17:01:30 -070097import com.google.android.collect.Lists;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
Dave Allison24bafbc2013-11-13 17:15:50 -080099import dalvik.system.VMRuntime;
100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import java.io.File;
102import java.io.FileDescriptor;
103import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700104import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import java.io.PrintWriter;
106import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700107import java.net.InetAddress;
Kenny Root8b514752013-02-04 09:35:16 -0800108import java.security.Security;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import java.util.List;
111import java.util.Locale;
112import java.util.Map;
Kenny Roote6585b32013-12-13 12:00:26 -0800113import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import java.util.TimeZone;
115import java.util.regex.Pattern;
116
Geremy Condrab7faaf42012-09-19 18:07:42 -0700117import libcore.io.DropBox;
Geremy Condra69689a72012-09-11 16:57:17 -0700118import libcore.io.EventLogger;
Jeff Sharkeye861b422012-03-01 20:59:22 -0800119import libcore.io.IoUtils;
120
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800121import dalvik.system.CloseGuard;
Mathieu Chartier1e370902013-07-18 10:58:01 -0700122import dalvik.system.VMRuntime;
Bob Leee5408332009-09-04 18:31:17 -0700123
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700124final class RemoteServiceException extends AndroidRuntimeException {
125 public RemoteServiceException(String msg) {
126 super(msg);
127 }
128}
129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130/**
131 * This manages the execution of the main thread in an
132 * application process, scheduling and executing activities,
133 * broadcasts, and other operations on it as the activity
134 * manager requests.
135 *
136 * {@hide}
137 */
138public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700139 /** @hide */
140 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700141 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700142 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700143 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700144 /** @hide */
145 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700146 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700147 private static final boolean DEBUG_BACKUP = false;
Craig Mautner88c05892013-06-28 09:47:45 -0700148 public static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800149 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700150 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700151 private static final boolean DEBUG_PROVIDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
153 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
154 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
155 private static final int LOG_ON_PAUSE_CALLED = 30021;
156 private static final int LOG_ON_RESUME_CALLED = 30022;
157
Jeff Brown08988602014-03-10 21:24:37 -0700158 private ContextImpl mSystemContext;
Bob Leee5408332009-09-04 18:31:17 -0700159
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700160 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700162 final ApplicationThread mAppThread = new ApplicationThread();
163 final Looper mLooper = Looper.myLooper();
164 final H mH = new H();
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700165 final ArrayMap<IBinder, ActivityClientRecord> mActivities
166 = new ArrayMap<IBinder, ActivityClientRecord>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700167 // List of new activities (via ActivityRecord.nextIdle) that should
168 // be reported when next we idle.
169 ActivityClientRecord mNewActivities = null;
170 // Number of activities that are currently visible on-screen.
171 int mNumVisibleActivities = 0;
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700172 final ArrayMap<IBinder, Service> mServices
173 = new ArrayMap<IBinder, Service>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700174 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700175 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700176 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700177 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700178 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700179 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700180 Application mInitialApplication;
181 final ArrayList<Application> mAllApplications
182 = new ArrayList<Application>();
183 // set of instantiated backup agents, keyed by package name
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700184 final ArrayMap<String, BackupAgent> mBackupAgents = new ArrayMap<String, BackupAgent>();
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800185 /** Reference to singleton {@link ActivityThread} */
186 private static ActivityThread sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700187 Instrumentation mInstrumentation;
188 String mInstrumentationAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700189 String mInstrumentationAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700190 String mInstrumentationAppPackage = null;
191 String mInstrumentedAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700192 String mInstrumentedAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700193 boolean mSystemThread = false;
194 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700196 // These can be accessed by multiple threads; mPackages is the lock.
197 // XXX For now we keep around information about all packages we have
198 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800199 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700200 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800201 // which means this lock gets held while the activity and window managers
202 // holds their own lock. Thus you MUST NEVER call back into the activity manager
203 // or window manager or anything that depends on them while holding this lock.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700204 final ArrayMap<String, WeakReference<LoadedApk>> mPackages
205 = new ArrayMap<String, WeakReference<LoadedApk>>();
206 final ArrayMap<String, WeakReference<LoadedApk>> mResourcePackages
207 = new ArrayMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700208 final ArrayList<ActivityClientRecord> mRelaunchingActivities
209 = new ArrayList<ActivityClientRecord>();
210 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211
Craig Mautner88c05892013-06-28 09:47:45 -0700212 private final ResourcesManager mResourcesManager;
213
Jeff Sharkey6d515712012-09-20 16:06:08 -0700214 private static final class ProviderKey {
215 final String authority;
216 final int userId;
217
218 public ProviderKey(String authority, int userId) {
219 this.authority = authority;
220 this.userId = userId;
221 }
222
223 @Override
224 public boolean equals(Object o) {
225 if (o instanceof ProviderKey) {
226 final ProviderKey other = (ProviderKey) o;
Kenny Roote6585b32013-12-13 12:00:26 -0800227 return Objects.equals(authority, other.authority) && userId == other.userId;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700228 }
229 return false;
230 }
231
232 @Override
233 public int hashCode() {
234 return ((authority != null) ? authority.hashCode() : 0) ^ userId;
235 }
236 }
237
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700238 // The lock of mProviderMap protects the following variables.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700239 final ArrayMap<ProviderKey, ProviderClientRecord> mProviderMap
240 = new ArrayMap<ProviderKey, ProviderClientRecord>();
241 final ArrayMap<IBinder, ProviderRefCount> mProviderRefCountMap
242 = new ArrayMap<IBinder, ProviderRefCount>();
243 final ArrayMap<IBinder, ProviderClientRecord> mLocalProviders
244 = new ArrayMap<IBinder, ProviderClientRecord>();
245 final ArrayMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
246 = new ArrayMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700248 final ArrayMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
249 = new ArrayMap<Activity, ArrayList<OnActivityPausedListener>>();
Jeff Hamilton52d32032011-01-08 15:31:26 -0600250
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700251 final GcIdler mGcIdler = new GcIdler();
252 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700254 static Handler sMainThreadHandler; // set once in main()
255
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800256 Bundle mCoreSettings = null;
257
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400258 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700260 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 Intent intent;
262 Bundle state;
263 Activity activity;
264 Window window;
265 Activity parent;
266 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700267 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 boolean paused;
269 boolean stopped;
270 boolean hideForNow;
271 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700272 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700273 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700275 String profileFile;
276 ParcelFileDescriptor profileFd;
277 boolean autoStopProfiler;
278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400280 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700281 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282
283 List<ResultInfo> pendingResults;
284 List<Intent> pendingIntents;
285
286 boolean startsNotResumed;
287 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800288 int pendingConfigChanges;
289 boolean onlyLocalRequest;
290
291 View mPendingRemoveWindow;
292 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700294 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 parent = null;
296 embeddedID = null;
297 paused = false;
298 stopped = false;
299 hideForNow = false;
300 nextIdle = null;
301 }
302
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800303 public boolean isPreHoneycomb() {
304 if (activity != null) {
305 return activity.getApplicationInfo().targetSdkVersion
306 < android.os.Build.VERSION_CODES.HONEYCOMB;
307 }
308 return false;
309 }
310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700312 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 return "ActivityRecord{"
314 + Integer.toHexString(System.identityHashCode(this))
315 + " token=" + token + " " + (componentName == null
316 ? "no component name" : componentName.toShortString())
317 + "}";
318 }
319 }
320
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700321 final class ProviderClientRecord {
322 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 final IContentProvider mProvider;
324 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700325 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700327 ProviderClientRecord(String[] names, IContentProvider provider,
328 ContentProvider localProvider,
329 IActivityManager.ContentProviderHolder holder) {
330 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 mProvider = provider;
332 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700333 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 }
335 }
336
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400337 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 List<Intent> intents;
339 IBinder token;
340 public String toString() {
341 return "NewIntentData{intents=" + intents + " token=" + token + "}";
342 }
343 }
344
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400345 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700346 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700347 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
348 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
349 token, sendingUser);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700350 this.intent = intent;
351 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 Intent intent;
354 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400355 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 public String toString() {
357 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700358 info.packageName + " resultCode=" + getResultCode()
359 + " resultData=" + getResultData() + " resultExtras="
360 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
362 }
363
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400364 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700365 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400366 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700367 int backupMode;
368 public String toString() {
369 return "CreateBackupAgentData{appInfo=" + appInfo
370 + " backupAgent=" + appInfo.backupAgentName
371 + " mode=" + backupMode + "}";
372 }
373 }
Bob Leee5408332009-09-04 18:31:17 -0700374
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400375 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 IBinder token;
377 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400378 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 Intent intent;
380 public String toString() {
381 return "CreateServiceData{token=" + token + " className="
382 + info.name + " packageName=" + info.packageName
383 + " intent=" + intent + "}";
384 }
385 }
386
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400387 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 IBinder token;
389 Intent intent;
390 boolean rebind;
391 public String toString() {
392 return "BindServiceData{token=" + token + " intent=" + intent + "}";
393 }
394 }
395
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400396 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700398 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700400 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 Intent args;
402 public String toString() {
403 return "ServiceArgsData{token=" + token + " startId=" + startId
404 + " args=" + args + "}";
405 }
406 }
407
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400408 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700409 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 String processName;
411 ApplicationInfo appInfo;
412 List<ProviderInfo> providers;
413 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 Bundle instrumentationArgs;
415 IInstrumentationWatcher instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800416 IUiAutomationConnection instrumentationUiAutomationConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 int debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800418 boolean enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700419 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700420 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400422 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700423
424 /** Initial values for {@link Profiler}. */
425 String initProfileFile;
426 ParcelFileDescriptor initProfileFd;
427 boolean initAutoStopProfiler;
428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 public String toString() {
430 return "AppBindData{appInfo=" + appInfo + "}";
431 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700432 }
433
434 static final class Profiler {
435 String profileFile;
436 ParcelFileDescriptor profileFd;
437 boolean autoStopProfiler;
438 boolean profiling;
439 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700440 public void setProfiler(String file, ParcelFileDescriptor fd) {
441 if (profiling) {
442 if (fd != null) {
443 try {
444 fd.close();
445 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700446 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700447 }
448 }
449 return;
450 }
451 if (profileFd != null) {
452 try {
453 profileFd.close();
454 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700455 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700456 }
457 }
458 profileFile = file;
459 profileFd = fd;
460 }
461 public void startProfiling() {
462 if (profileFd == null || profiling) {
463 return;
464 }
465 try {
466 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
467 8 * 1024 * 1024, 0);
468 profiling = true;
469 } catch (RuntimeException e) {
470 Slog.w(TAG, "Profiling failed on path " + profileFile);
471 try {
472 profileFd.close();
473 profileFd = null;
474 } catch (IOException e2) {
475 Slog.w(TAG, "Failure closing profile fd", e2);
476 }
477 }
478 }
479 public void stopProfiling() {
480 if (profiling) {
481 profiling = false;
482 Debug.stopMethodTracing();
483 if (profileFd != null) {
484 try {
485 profileFd.close();
486 } catch (IOException e) {
487 }
488 }
489 profileFd = null;
490 profileFile = null;
491 }
492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 }
494
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400495 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700496 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700497 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800498 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 }
501
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400502 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 IBinder token;
504 List<ResultInfo> results;
505 public String toString() {
506 return "ResultData{token=" + token + " results" + results + "}";
507 }
508 }
509
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400510 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800511 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 String what;
513 String who;
514 }
515
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400516 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700517 String path;
518 ParcelFileDescriptor fd;
519 }
520
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400521 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700522 String path;
523 ParcelFileDescriptor fd;
524 }
525
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400526 static final class UpdateCompatibilityData {
527 String pkg;
528 CompatibilityInfo info;
529 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800530
Adam Skorydfc7fd72013-08-05 19:23:41 -0700531 static final class RequestAssistContextExtras {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800532 IBinder activityToken;
533 IBinder requestToken;
534 int requestType;
535 }
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700536
Romain Guy65b345f2011-07-27 18:51:50 -0700537 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700538
Romain Guy65b345f2011-07-27 18:51:50 -0700539 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700540 private static final String ONE_COUNT_COLUMN = "%21s %8d";
541 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700542 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700543
Dianne Hackborna413dc02013-07-12 12:02:55 -0700544 private int mLastProcessState = -1;
545
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700546 private void updatePendingConfiguration(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -0700547 synchronized (mResourcesManager) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700548 if (mPendingConfiguration == null ||
549 mPendingConfiguration.isOtherSeqNewer(config)) {
550 mPendingConfiguration = config;
551 }
552 }
553 }
554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 public final void schedulePauseActivity(IBinder token, boolean finished,
556 boolean userLeaving, int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700557 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
559 token,
560 (userLeaving ? 1 : 0),
561 configChanges);
562 }
563
564 public final void scheduleStopActivity(IBinder token, boolean showWindow,
565 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700566 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
568 token, 0, configChanges);
569 }
570
571 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700572 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
574 token);
575 }
576
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800577 public final void scheduleSleeping(IBinder token, boolean sleeping) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700578 sendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800579 }
580
Dianne Hackborna413dc02013-07-12 12:02:55 -0700581 public final void scheduleResumeActivity(IBinder token, int processState,
582 boolean isForward) {
583 updateProcessState(processState, false);
Jeff Brown9ef09972013-10-15 20:49:59 -0700584 sendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 }
586
587 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
588 ResultData res = new ResultData();
589 res.token = token;
590 res.results = results;
Jeff Brown9ef09972013-10-15 20:49:59 -0700591 sendMessage(H.SEND_RESULT, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 }
593
594 // we use token to identify this activity without having to send the
595 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700596 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700597 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700598 int procState, Bundle state, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700599 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
600 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700601
602 updateProcessState(procState, false);
603
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700604 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605
606 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700607 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 r.intent = intent;
609 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400610 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 r.state = state;
612
613 r.pendingResults = pendingResults;
614 r.pendingIntents = pendingNewIntents;
615
616 r.startsNotResumed = notResumed;
617 r.isForward = isForward;
618
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700619 r.profileFile = profileName;
620 r.profileFd = profileFd;
621 r.autoStopProfiler = autoStopProfiler;
622
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700623 updatePendingConfiguration(curConfig);
624
Jeff Brown9ef09972013-10-15 20:49:59 -0700625 sendMessage(H.LAUNCH_ACTIVITY, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 }
627
628 public final void scheduleRelaunchActivity(IBinder token,
629 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800630 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800631 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
632 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 }
634
635 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
636 NewIntentData data = new NewIntentData();
637 data.intents = intents;
638 data.token = token;
639
Jeff Brown9ef09972013-10-15 20:49:59 -0700640 sendMessage(H.NEW_INTENT, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 }
642
643 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
644 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700645 sendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 configChanges);
647 }
648
649 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400650 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700651 boolean sync, int sendingUser, int processState) {
652 updateProcessState(processState, false);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700653 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700654 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400656 r.compatInfo = compatInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700657 sendMessage(H.RECEIVER, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 }
659
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400660 public final void scheduleCreateBackupAgent(ApplicationInfo app,
661 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700662 CreateBackupAgentData d = new CreateBackupAgentData();
663 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400664 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700665 d.backupMode = backupMode;
666
Jeff Brown9ef09972013-10-15 20:49:59 -0700667 sendMessage(H.CREATE_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700668 }
669
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400670 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
671 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700672 CreateBackupAgentData d = new CreateBackupAgentData();
673 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400674 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700675
Jeff Brown9ef09972013-10-15 20:49:59 -0700676 sendMessage(H.DESTROY_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700677 }
678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 public final void scheduleCreateService(IBinder token,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700680 ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
681 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 CreateServiceData s = new CreateServiceData();
683 s.token = token;
684 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400685 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686
Jeff Brown9ef09972013-10-15 20:49:59 -0700687 sendMessage(H.CREATE_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 }
689
690 public final void scheduleBindService(IBinder token, Intent intent,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700691 boolean rebind, int processState) {
692 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 BindServiceData s = new BindServiceData();
694 s.token = token;
695 s.intent = intent;
696 s.rebind = rebind;
697
Amith Yamasani742a6712011-05-04 14:49:28 -0700698 if (DEBUG_SERVICE)
699 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
700 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
Jeff Brown9ef09972013-10-15 20:49:59 -0700701 sendMessage(H.BIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 }
703
704 public final void scheduleUnbindService(IBinder token, Intent intent) {
705 BindServiceData s = new BindServiceData();
706 s.token = token;
707 s.intent = intent;
708
Jeff Brown9ef09972013-10-15 20:49:59 -0700709 sendMessage(H.UNBIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 }
711
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700712 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700713 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 ServiceArgsData s = new ServiceArgsData();
715 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700716 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700718 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 s.args = args;
720
Jeff Brown9ef09972013-10-15 20:49:59 -0700721 sendMessage(H.SERVICE_ARGS, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 }
723
724 public final void scheduleStopService(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700725 sendMessage(H.STOP_SERVICE, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 }
727
728 public final void bindApplication(String processName,
729 ApplicationInfo appInfo, List<ProviderInfo> providers,
730 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700731 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800733 IUiAutomationConnection instrumentationUiConnection, int debugMode,
734 boolean enableOpenGlTrace, boolean isRestrictedBackupMode, boolean persistent,
735 Configuration config, CompatibilityInfo compatInfo, Map<String, IBinder> services,
736 Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737
738 if (services != null) {
739 // Setup the service cache in the ServiceManager
740 ServiceManager.initServiceCache(services);
741 }
742
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800743 setCoreSettings(coreSettings);
744
Dave Allison24bafbc2013-11-13 17:15:50 -0800745 // Tell the VMRuntime about the application.
746 VMRuntime.registerAppInfo(appInfo.dataDir, appInfo.processName);
747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 AppBindData data = new AppBindData();
749 data.processName = processName;
750 data.appInfo = appInfo;
751 data.providers = providers;
752 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 data.instrumentationArgs = instrumentationArgs;
754 data.instrumentationWatcher = instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800755 data.instrumentationUiAutomationConnection = instrumentationUiConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 data.debugMode = debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800757 data.enableOpenGlTrace = enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700758 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700759 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400761 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700762 data.initProfileFile = profileFile;
763 data.initProfileFd = profileFd;
764 data.initAutoStopProfiler = false;
Jeff Brown9ef09972013-10-15 20:49:59 -0700765 sendMessage(H.BIND_APPLICATION, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 }
767
768 public final void scheduleExit() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700769 sendMessage(H.EXIT_APPLICATION, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 }
771
Christopher Tate5e1ab332009-09-01 20:32:49 -0700772 public final void scheduleSuicide() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700773 sendMessage(H.SUICIDE, null);
Christopher Tate5e1ab332009-09-01 20:32:49 -0700774 }
775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 public void requestThumbnail(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700777 sendMessage(H.REQUEST_THUMBNAIL, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 }
779
780 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700781 updatePendingConfiguration(config);
Jeff Brown9ef09972013-10-15 20:49:59 -0700782 sendMessage(H.CONFIGURATION_CHANGED, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 }
784
785 public void updateTimeZone() {
786 TimeZone.setDefault(null);
787 }
788
Robert Greenwalt03595d02010-11-02 14:08:23 -0700789 public void clearDnsCache() {
790 // a non-standard API to get this to libcore
791 InetAddress.clearDnsCache();
792 }
793
Jason Monk602b2322013-07-03 17:04:33 -0400794 public void setHttpProxy(String host, String port, String exclList, String pacFileUrl) {
795 Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
Robert Greenwalt434203a2010-10-11 16:00:27 -0700796 }
797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 public void processInBackground() {
799 mH.removeMessages(H.GC_WHEN_IDLE);
800 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
801 }
802
803 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700804 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700805 try {
806 data.fd = ParcelFileDescriptor.dup(fd);
807 data.token = servicetoken;
808 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700809 sendMessage(H.DUMP_SERVICE, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700810 } catch (IOException e) {
811 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 }
813 }
814
815 // This function exists to make sure all receiver dispatching is
816 // correctly ordered, since these are one-way calls and the binder driver
817 // applies transaction ordering per object for such calls.
818 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700819 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700820 boolean sticky, int sendingUser, int processState) throws RemoteException {
821 updateProcessState(processState, false);
Dianne Hackborn20e80982012-08-31 19:00:44 -0700822 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
823 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 }
Bob Leee5408332009-09-04 18:31:17 -0700825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 public void scheduleLowMemory() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700827 sendMessage(H.LOW_MEMORY, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 }
829
830 public void scheduleActivityConfigurationChanged(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700831 sendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 }
833
Romain Guy7eabe552011-07-21 14:56:34 -0700834 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
835 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700836 ProfilerControlData pcd = new ProfilerControlData();
837 pcd.path = path;
838 pcd.fd = fd;
Jeff Brown9ef09972013-10-15 20:49:59 -0700839 sendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800840 }
841
Andy McFadden824c5102010-07-09 16:26:57 -0700842 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
843 DumpHeapData dhd = new DumpHeapData();
844 dhd.path = path;
845 dhd.fd = fd;
Jeff Brown9ef09972013-10-15 20:49:59 -0700846 sendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0, 0, true /*async*/);
Andy McFadden824c5102010-07-09 16:26:57 -0700847 }
848
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700849 public void setSchedulingGroup(int group) {
850 // Note: do this immediately, since going into the foreground
851 // should happen regardless of what pending work we have to do
852 // and the activity manager will wait for us to report back that
853 // we are done before sending us to the background.
854 try {
855 Process.setProcessGroup(Process.myPid(), group);
856 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800857 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700858 }
859 }
Bob Leee5408332009-09-04 18:31:17 -0700860
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700861 public void dispatchPackageBroadcast(int cmd, String[] packages) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700862 sendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700863 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700864
865 public void scheduleCrash(String msg) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700866 sendMessage(H.SCHEDULE_CRASH, msg);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700867 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700868
Dianne Hackborn30d71892010-12-11 10:37:55 -0800869 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
870 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700871 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700872 try {
873 data.fd = ParcelFileDescriptor.dup(fd);
874 data.token = activitytoken;
875 data.prefix = prefix;
876 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700877 sendMessage(H.DUMP_ACTIVITY, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700878 } catch (IOException e) {
879 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700880 }
881 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700882
Marco Nelissen18cb2872011-11-15 11:19:53 -0800883 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
884 String[] args) {
885 DumpComponentInfo data = new DumpComponentInfo();
886 try {
887 data.fd = ParcelFileDescriptor.dup(fd);
888 data.token = providertoken;
889 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700890 sendMessage(H.DUMP_PROVIDER, data, 0, 0, true /*async*/);
Marco Nelissen18cb2872011-11-15 11:19:53 -0800891 } catch (IOException e) {
892 Slog.w(TAG, "dumpProvider failed", e);
893 }
894 }
895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 @Override
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700897 public void dumpMemInfo(FileDescriptor fd, Debug.MemoryInfo mem, boolean checkin,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700898 boolean dumpFullInfo, boolean dumpDalvik, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700899 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700900 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700901 try {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700902 dumpMemInfo(pw, mem, checkin, dumpFullInfo, dumpDalvik);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700903 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700904 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700905 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700906 }
907
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700908 private void dumpMemInfo(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700909 boolean dumpFullInfo, boolean dumpDalvik) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 long nativeMax = Debug.getNativeHeapSize() / 1024;
911 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
912 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 Runtime runtime = Runtime.getRuntime();
915
916 long dalvikMax = runtime.totalMemory() / 1024;
917 long dalvikFree = runtime.freeMemory() / 1024;
918 long dalvikAllocated = dalvikMax - dalvikFree;
919 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700920 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700921 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
922 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 int globalAssetCount = AssetManager.getGlobalAssetCount();
924 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
925 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
926 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
927 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700928 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800929 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700930
Dianne Hackborne77187d2013-10-25 16:32:41 -0700931 dumpMemInfoTable(pw, memInfo, checkin, dumpFullInfo, dumpDalvik, Process.myPid(),
932 (mBoundApplication != null) ? mBoundApplication.processName : "unknown",
933 nativeMax, nativeAllocated, nativeFree,
934 dalvikMax, dalvikAllocated, dalvikFree);
935
Dianne Hackbornb437e092011-08-05 17:50:29 -0700936 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 // NOTE: if you change anything significant below, also consider changing
938 // ACTIVITY_THREAD_CHECKIN_VERSION.
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 // Object counts
941 pw.print(viewInstanceCount); pw.print(',');
942 pw.print(viewRootInstanceCount); pw.print(',');
943 pw.print(appContextInstanceCount); pw.print(',');
944 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 pw.print(globalAssetCount); pw.print(',');
947 pw.print(globalAssetManagerCount); pw.print(',');
948 pw.print(binderLocalObjectCount); pw.print(',');
949 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 pw.print(binderDeathObjectCount); pw.print(',');
952 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 // SQL
Vasu Noric3849202010-03-09 10:47:25 -0800955 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -0800956 pw.print(stats.memoryUsed / 1024); pw.print(',');
957 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700958 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800959 for (int i = 0; i < stats.dbStats.size(); i++) {
960 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700961 pw.print(','); pw.print(dbStats.dbName);
962 pw.print(','); pw.print(dbStats.pageSize);
963 pw.print(','); pw.print(dbStats.dbSize);
964 pw.print(','); pw.print(dbStats.lookaside);
965 pw.print(','); pw.print(dbStats.cache);
966 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800967 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700968 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700969
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700970 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 }
Bob Leee5408332009-09-04 18:31:17 -0700972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 pw.println(" ");
974 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -0700975 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 viewRootInstanceCount);
977
978 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
979 "Activities:", activityInstanceCount);
980
981 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
982 "AssetManagers:", globalAssetManagerCount);
983
984 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
985 "Proxy Binders:", binderProxyObjectCount);
986 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
987
988 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -0700989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 // SQLite mem info
991 pw.println(" ");
992 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -0800993 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
994 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
995 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800996 pw.println(" ");
997 int N = stats.dbStats.size();
998 if (N > 0) {
999 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001000 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1001 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001002 for (int i = 0; i < N; i++) {
1003 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001004 printRow(pw, DB_INFO_FORMAT,
1005 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1006 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1007 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1008 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001009 }
1010 }
Bob Leee5408332009-09-04 18:31:17 -07001011
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001012 // Asset details.
1013 String assetAlloc = AssetManager.getAssetAllocations();
1014 if (assetAlloc != null) {
1015 pw.println(" ");
1016 pw.println(" Asset Allocations");
1017 pw.print(assetAlloc);
1018 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001019 }
1020
1021 @Override
1022 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1023 dumpGraphicsInfo(fd);
Jeff Brown98365d72012-08-19 20:30:52 -07001024 WindowManagerGlobal.getInstance().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 }
1026
Jeff Brown6754ba22011-12-14 20:20:01 -08001027 @Override
1028 public void dumpDbInfo(FileDescriptor fd, String[] args) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07001029 PrintWriter pw = new FastPrintWriter(new FileOutputStream(fd));
Jeff Brown6754ba22011-12-14 20:20:01 -08001030 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1031 SQLiteDebug.dump(printer, args);
1032 pw.flush();
1033 }
1034
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001035 @Override
1036 public void unstableProviderDied(IBinder provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001037 sendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001038 }
1039
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001040 @Override
Adam Skorydfc7fd72013-08-05 19:23:41 -07001041 public void requestAssistContextExtras(IBinder activityToken, IBinder requestToken,
Adam Skory7140a252013-09-11 12:04:58 +01001042 int requestType) {
Adam Skorydfc7fd72013-08-05 19:23:41 -07001043 RequestAssistContextExtras cmd = new RequestAssistContextExtras();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001044 cmd.activityToken = activityToken;
1045 cmd.requestToken = requestToken;
1046 cmd.requestType = requestType;
Jeff Brown9ef09972013-10-15 20:49:59 -07001047 sendMessage(H.REQUEST_ASSIST_CONTEXT_EXTRAS, cmd);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001048 }
1049
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001050 public void setCoreSettings(Bundle coreSettings) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001051 sendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001052 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001053
1054 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1055 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1056 ucd.pkg = pkg;
1057 ucd.info = info;
Jeff Brown9ef09972013-10-15 20:49:59 -07001058 sendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001059 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001060
1061 public void scheduleTrimMemory(int level) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001062 sendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001063 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001064
Craig Mautner5eda9b32013-07-02 11:58:16 -07001065 public void scheduleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001066 sendMessage(H.TRANSLUCENT_CONVERSION_COMPLETE, token, drawComplete ? 1 : 0);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001067 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001068
1069 public void setProcessState(int state) {
1070 updateProcessState(state, true);
1071 }
1072
1073 public void updateProcessState(int processState, boolean fromIpc) {
1074 synchronized (this) {
1075 if (mLastProcessState != processState) {
1076 mLastProcessState = processState;
Mathieu Chartier1e370902013-07-18 10:58:01 -07001077 // Update Dalvik state based on ActivityManager.PROCESS_STATE_* constants.
1078 final int DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE = 0;
1079 final int DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1;
1080 int dalvikProcessState = DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE;
1081 // TODO: Tune this since things like gmail sync are important background but not jank perceptible.
1082 if (processState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
1083 dalvikProcessState = DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE;
1084 }
1085 VMRuntime.getRuntime().updateProcessState(dalvikProcessState);
Dianne Hackborna413dc02013-07-12 12:02:55 -07001086 if (false) {
1087 Slog.i(TAG, "******************* PROCESS STATE CHANGED TO: " + processState
1088 + (fromIpc ? " (from ipc": ""));
1089 }
1090 }
1091 }
1092 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001093
1094 @Override
1095 public void scheduleInstallProvider(ProviderInfo provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001096 sendMessage(H.INSTALL_PROVIDER, provider);
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 }
1099
Romain Guy65b345f2011-07-27 18:51:50 -07001100 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 public static final int LAUNCH_ACTIVITY = 100;
1102 public static final int PAUSE_ACTIVITY = 101;
1103 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1104 public static final int STOP_ACTIVITY_SHOW = 103;
1105 public static final int STOP_ACTIVITY_HIDE = 104;
1106 public static final int SHOW_WINDOW = 105;
1107 public static final int HIDE_WINDOW = 106;
1108 public static final int RESUME_ACTIVITY = 107;
1109 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001110 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 public static final int BIND_APPLICATION = 110;
1112 public static final int EXIT_APPLICATION = 111;
1113 public static final int NEW_INTENT = 112;
1114 public static final int RECEIVER = 113;
1115 public static final int CREATE_SERVICE = 114;
1116 public static final int SERVICE_ARGS = 115;
1117 public static final int STOP_SERVICE = 116;
1118 public static final int REQUEST_THUMBNAIL = 117;
1119 public static final int CONFIGURATION_CHANGED = 118;
1120 public static final int CLEAN_UP_CONTEXT = 119;
1121 public static final int GC_WHEN_IDLE = 120;
1122 public static final int BIND_SERVICE = 121;
1123 public static final int UNBIND_SERVICE = 122;
1124 public static final int DUMP_SERVICE = 123;
1125 public static final int LOW_MEMORY = 124;
1126 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1127 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001128 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001129 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001130 public static final int DESTROY_BACKUP_AGENT = 129;
1131 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001132 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001133 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001134 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001135 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001136 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001137 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001138 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001139 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001140 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001141 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001142 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001143 public static final int UNSTABLE_PROVIDER_DIED = 142;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001144 public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001145 public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001146 public static final int INSTALL_PROVIDER = 145;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001148 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 switch (code) {
1150 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1151 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1152 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1153 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1154 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1155 case SHOW_WINDOW: return "SHOW_WINDOW";
1156 case HIDE_WINDOW: return "HIDE_WINDOW";
1157 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1158 case SEND_RESULT: return "SEND_RESULT";
1159 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1160 case BIND_APPLICATION: return "BIND_APPLICATION";
1161 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1162 case NEW_INTENT: return "NEW_INTENT";
1163 case RECEIVER: return "RECEIVER";
1164 case CREATE_SERVICE: return "CREATE_SERVICE";
1165 case SERVICE_ARGS: return "SERVICE_ARGS";
1166 case STOP_SERVICE: return "STOP_SERVICE";
1167 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1168 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1169 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1170 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1171 case BIND_SERVICE: return "BIND_SERVICE";
1172 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1173 case DUMP_SERVICE: return "DUMP_SERVICE";
1174 case LOW_MEMORY: return "LOW_MEMORY";
1175 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1176 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001177 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001178 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1179 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001180 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001181 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001182 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001183 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001184 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001185 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001186 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001187 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001188 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001189 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001190 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001191 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001192 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
Adam Skorydfc7fd72013-08-05 19:23:41 -07001193 case REQUEST_ASSIST_CONTEXT_EXTRAS: return "REQUEST_ASSIST_CONTEXT_EXTRAS";
Craig Mautner5eda9b32013-07-02 11:58:16 -07001194 case TRANSLUCENT_CONVERSION_COMPLETE: return "TRANSLUCENT_CONVERSION_COMPLETE";
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001195 case INSTALL_PROVIDER: return "INSTALL_PROVIDER";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 }
1197 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001198 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 }
1200 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001201 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 switch (msg.what) {
1203 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001204 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001205 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206
1207 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001208 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001209 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001210 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 } break;
1212 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001213 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001214 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001215 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001216 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 } break;
1218 case PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001219 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001221 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001222 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 break;
1224 case PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001225 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001227 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 break;
1229 case STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001230 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001232 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 break;
1234 case STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001235 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001237 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 break;
1239 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001240 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001242 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 break;
1244 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001245 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001247 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 break;
1249 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001250 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 handleResumeActivity((IBinder)msg.obj, true,
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001252 msg.arg1 != 0, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001253 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 break;
1255 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001256 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001258 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 break;
1260 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001261 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1263 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001264 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 break;
1266 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001267 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 AppBindData data = (AppBindData)msg.obj;
1269 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001270 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 break;
1272 case EXIT_APPLICATION:
1273 if (mInitialApplication != null) {
1274 mInitialApplication.onTerminate();
1275 }
1276 Looper.myLooper().quit();
1277 break;
1278 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001279 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001281 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 break;
1283 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001284 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001286 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001287 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 break;
1289 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001290 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001292 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 break;
1294 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001295 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001297 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 break;
1299 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001300 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001302 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 break;
1304 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001305 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001307 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 break;
1309 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001310 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001312 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001313 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 break;
1315 case REQUEST_THUMBNAIL:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001316 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "requestThumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 handleRequestThumbnail((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001318 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 break;
1320 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001321 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001322 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001323 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001324 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 break;
1326 case CLEAN_UP_CONTEXT:
1327 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1328 cci.context.performFinalCleanup(cci.who, cci.what);
1329 break;
1330 case GC_WHEN_IDLE:
1331 scheduleGcIdler();
1332 break;
1333 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001334 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 break;
1336 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001337 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001339 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 break;
1341 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001342 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001344 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001346 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001347 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001348 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001349 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001350 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001351 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001352 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001353 break;
1354 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001355 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001356 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001357 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001358 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001359 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001360 Process.killProcess(Process.myPid());
1361 break;
1362 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001363 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001364 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001365 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001366 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001367 case ENABLE_JIT:
1368 ensureJitEnabled();
1369 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001370 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001371 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001372 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001373 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001374 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001375 case SCHEDULE_CRASH:
1376 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001377 case DUMP_HEAP:
1378 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1379 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001380 case DUMP_ACTIVITY:
1381 handleDumpActivity((DumpComponentInfo)msg.obj);
1382 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001383 case DUMP_PROVIDER:
1384 handleDumpProvider((DumpComponentInfo)msg.obj);
1385 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001386 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001387 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001388 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001389 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001390 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001391 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001392 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001393 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001394 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001395 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001396 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1397 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001398 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001399 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001400 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001401 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001402 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001403 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001404 case UNSTABLE_PROVIDER_DIED:
1405 handleUnstableProviderDied((IBinder)msg.obj, false);
1406 break;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001407 case REQUEST_ASSIST_CONTEXT_EXTRAS:
1408 handleRequestAssistContextExtras((RequestAssistContextExtras)msg.obj);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001409 break;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001410 case TRANSLUCENT_CONVERSION_COMPLETE:
1411 handleTranslucentConversionComplete((IBinder)msg.obj, msg.arg1 == 1);
1412 break;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001413 case INSTALL_PROVIDER:
1414 handleInstallProvider((ProviderInfo) msg.obj);
1415 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001417 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 }
Bob Leee5408332009-09-04 18:31:17 -07001419
Brian Carlstromed7e0072011-03-24 13:27:57 -07001420 private void maybeSnapshot() {
1421 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001422 // convert the *private* ActivityThread.PackageInfo to *public* known
1423 // android.content.pm.PackageInfo
1424 String packageName = mBoundApplication.info.mPackageName;
1425 android.content.pm.PackageInfo packageInfo = null;
1426 try {
1427 Context context = getSystemContext();
1428 if(context == null) {
1429 Log.e(TAG, "cannot get a valid context");
1430 return;
1431 }
1432 PackageManager pm = context.getPackageManager();
1433 if(pm == null) {
1434 Log.e(TAG, "cannot get a valid PackageManager");
1435 return;
1436 }
1437 packageInfo = pm.getPackageInfo(
1438 packageName, PackageManager.GET_ACTIVITIES);
1439 } catch (NameNotFoundException e) {
1440 Log.e(TAG, "cannot get package info for " + packageName, e);
1441 }
1442 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001443 }
1444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 }
1446
Romain Guy65b345f2011-07-27 18:51:50 -07001447 private class Idler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001448 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001450 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001451 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001452 if (mBoundApplication != null && mProfiler.profileFd != null
1453 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001454 stopProfiling = true;
1455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 if (a != null) {
1457 mNewActivities = null;
1458 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001459 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001461 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 TAG, "Reporting idle of " + a +
1463 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001464 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 if (a.activity != null && !a.activity.mFinished) {
1466 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001467 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001468 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001470 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 }
1472 }
1473 prev = a;
1474 a = a.nextIdle;
1475 prev.nextIdle = null;
1476 } while (a != null);
1477 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001478 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001479 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001480 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001481 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 return false;
1483 }
1484 }
1485
1486 final class GcIdler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001487 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 public final boolean queueIdle() {
1489 doGcIfNeeded();
1490 return false;
1491 }
1492 }
1493
Romain Guy65b345f2011-07-27 18:51:50 -07001494 public static ActivityThread currentActivityThread() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08001495 return sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497
Romain Guy65b345f2011-07-27 18:51:50 -07001498 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001499 ActivityThread am = currentActivityThread();
1500 return (am != null && am.mBoundApplication != null)
Dianne Hackbornb57a50b2013-04-11 17:29:32 -07001501 ? am.mBoundApplication.appInfo.packageName : null;
1502 }
1503
1504 public static String currentProcessName() {
1505 ActivityThread am = currentActivityThread();
1506 return (am != null && am.mBoundApplication != null)
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001507 ? am.mBoundApplication.processName : null;
1508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509
Romain Guy65b345f2011-07-27 18:51:50 -07001510 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001511 ActivityThread am = currentActivityThread();
1512 return am != null ? am.mInitialApplication : null;
1513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001515 public static IPackageManager getPackageManager() {
1516 if (sPackageManager != null) {
1517 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1518 return sPackageManager;
1519 }
1520 IBinder b = ServiceManager.getService("package");
1521 //Slog.v("PackageManager", "default service binder = " + b);
1522 sPackageManager = IPackageManager.Stub.asInterface(b);
1523 //Slog.v("PackageManager", "default service = " + sPackageManager);
1524 return sPackageManager;
1525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526
Romain Guy65b345f2011-07-27 18:51:50 -07001527 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001528 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1529 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001530 if (config == null) {
1531 return null;
1532 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001533 if (!compat.supportsScreen()) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001534 mMainThreadConfig.setTo(config);
1535 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001536 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001537 }
1538 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001541 /**
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001542 * Creates the top level resources for the given package.
1543 */
Jeff Browna492c3a2012-08-23 19:48:44 -07001544 Resources getTopLevelResources(String resDir,
1545 int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001546 LoadedApk pkgInfo) {
Craig Mautner88c05892013-06-28 09:47:45 -07001547 return mResourcesManager.getTopLevelResources(resDir, displayId, overrideConfiguration,
Craig Mautner48d0d182013-06-11 07:53:06 -07001548 pkgInfo.getCompatibilityInfo(), null);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001549 }
1550
1551 final Handler getHandler() {
1552 return mH;
1553 }
1554
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001555 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1556 int flags) {
Amith Yamasani98edc952012-09-25 14:09:27 -07001557 return getPackageInfo(packageName, compatInfo, flags, UserHandle.myUserId());
1558 }
1559
1560 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1561 int flags, int userId) {
Craig Mautner88c05892013-06-28 09:47:45 -07001562 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001563 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1565 ref = mPackages.get(packageName);
1566 } else {
1567 ref = mResourcePackages.get(packageName);
1568 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001569 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001570 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001571 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1572 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 if (packageInfo != null && (packageInfo.mResources == null
1574 || packageInfo.mResources.getAssets().isUpToDate())) {
1575 if (packageInfo.isSecurityViolation()
1576 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1577 throw new SecurityException(
1578 "Requesting code from " + packageName
1579 + " to be run in process "
1580 + mBoundApplication.processName
1581 + "/" + mBoundApplication.appInfo.uid);
1582 }
1583 return packageInfo;
1584 }
1585 }
1586
1587 ApplicationInfo ai = null;
1588 try {
1589 ai = getPackageManager().getApplicationInfo(packageName,
Amith Yamasani98edc952012-09-25 14:09:27 -07001590 PackageManager.GET_SHARED_LIBRARY_FILES, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001592 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 }
1594
1595 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001596 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 }
1598
1599 return null;
1600 }
1601
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001602 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1603 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1605 boolean securityViolation = includeCode && ai.uid != 0
1606 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001607 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001608 : true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1610 |Context.CONTEXT_IGNORE_SECURITY))
1611 == Context.CONTEXT_INCLUDE_CODE) {
1612 if (securityViolation) {
1613 String msg = "Requesting code from " + ai.packageName
1614 + " (with uid " + ai.uid + ")";
1615 if (mBoundApplication != null) {
1616 msg = msg + " to be run in process "
1617 + mBoundApplication.processName + " (with uid "
1618 + mBoundApplication.appInfo.uid + ")";
1619 }
1620 throw new SecurityException(msg);
1621 }
1622 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001623 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
1625
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001626 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1627 CompatibilityInfo compatInfo) {
1628 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 }
1630
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001631 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07001632 synchronized (mResourcesManager) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001633 WeakReference<LoadedApk> ref;
1634 if (includeCode) {
1635 ref = mPackages.get(packageName);
1636 } else {
1637 ref = mResourcePackages.get(packageName);
1638 }
1639 return ref != null ? ref.get() : null;
1640 }
1641 }
1642
Romain Guy65b345f2011-07-27 18:51:50 -07001643 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07001645 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001646 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 if (includeCode) {
1648 ref = mPackages.get(aInfo.packageName);
1649 } else {
1650 ref = mResourcePackages.get(aInfo.packageName);
1651 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001652 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 if (packageInfo == null || (packageInfo.mResources != null
1654 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001655 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 : "Loading resource-only package ") + aInfo.packageName
1657 + " (in " + (mBoundApplication != null
1658 ? mBoundApplication.processName : null)
1659 + ")");
1660 packageInfo =
Jeff Brown08988602014-03-10 21:24:37 -07001661 new LoadedApk(this, aInfo, compatInfo, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 securityViolation, includeCode &&
1663 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1664 if (includeCode) {
1665 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001666 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 } else {
1668 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001669 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 }
1671 }
1672 return packageInfo;
1673 }
1674 }
1675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 ActivityThread() {
Craig Mautner88c05892013-06-28 09:47:45 -07001677 mResourcesManager = ResourcesManager.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 }
1679
1680 public ApplicationThread getApplicationThread()
1681 {
1682 return mAppThread;
1683 }
1684
1685 public Instrumentation getInstrumentation()
1686 {
1687 return mInstrumentation;
1688 }
1689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001691 return mProfiler != null && mProfiler.profileFile != null
1692 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 }
1694
1695 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001696 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 }
1698
1699 public Looper getLooper() {
1700 return mLooper;
1701 }
1702
1703 public Application getApplication() {
1704 return mInitialApplication;
1705 }
Bob Leee5408332009-09-04 18:31:17 -07001706
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001707 public String getProcessName() {
1708 return mBoundApplication.processName;
1709 }
Bob Leee5408332009-09-04 18:31:17 -07001710
Dianne Hackborn21556372010-02-04 16:34:40 -08001711 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 synchronized (this) {
1713 if (mSystemContext == null) {
Jeff Brown08988602014-03-10 21:24:37 -07001714 mSystemContext = ContextImpl.createSystemContext(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 }
Jeff Brown08988602014-03-10 21:24:37 -07001716 return mSystemContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
1719
Mike Cleron432b7132009-09-24 15:28:29 -07001720 public void installSystemApplicationInfo(ApplicationInfo info) {
1721 synchronized (this) {
Jeff Brown08988602014-03-10 21:24:37 -07001722 getSystemContext().installSystemApplicationInfo(info);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001723
1724 // give ourselves a default profiler
1725 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001726 }
1727 }
1728
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001729 void ensureJitEnabled() {
1730 if (!mJitEnabled) {
1731 mJitEnabled = true;
1732 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1733 }
1734 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 void scheduleGcIdler() {
1737 if (!mGcIdlerScheduled) {
1738 mGcIdlerScheduled = true;
1739 Looper.myQueue().addIdleHandler(mGcIdler);
1740 }
1741 mH.removeMessages(H.GC_WHEN_IDLE);
1742 }
1743
1744 void unscheduleGcIdler() {
1745 if (mGcIdlerScheduled) {
1746 mGcIdlerScheduled = false;
1747 Looper.myQueue().removeIdleHandler(mGcIdler);
1748 }
1749 mH.removeMessages(H.GC_WHEN_IDLE);
1750 }
1751
1752 void doGcIfNeeded() {
1753 mGcIdlerScheduled = false;
1754 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001755 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 // + "m now=" + now);
1757 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001758 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 BinderInternal.forceGc("bg");
1760 }
1761 }
1762
Dianne Hackborne77187d2013-10-25 16:32:41 -07001763 private static final String HEAP_FULL_COLUMN
1764 = "%13s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s";
1765 private static final String HEAP_COLUMN
1766 = "%13s %8s %8s %8s %8s %8s %8s %8s";
1767
1768 // Formatting for checkin service - update version if row format changes
1769 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 3;
1770
1771 static void printRow(PrintWriter pw, String format, Object...objs) {
1772 pw.println(String.format(format, objs));
1773 }
1774
1775 public static void dumpMemInfoTable(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
1776 boolean dumpFullInfo, boolean dumpDalvik, int pid, String processName,
1777 long nativeMax, long nativeAllocated, long nativeFree,
1778 long dalvikMax, long dalvikAllocated, long dalvikFree) {
1779
1780 // For checkin, we print one long comma-separated list of values
1781 if (checkin) {
1782 // NOTE: if you change anything significant below, also consider changing
1783 // ACTIVITY_THREAD_CHECKIN_VERSION.
1784
1785 // Header
1786 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
1787 pw.print(pid); pw.print(',');
1788 pw.print(processName); pw.print(',');
1789
1790 // Heap info - max
1791 pw.print(nativeMax); pw.print(',');
1792 pw.print(dalvikMax); pw.print(',');
1793 pw.print("N/A,");
1794 pw.print(nativeMax + dalvikMax); pw.print(',');
1795
1796 // Heap info - allocated
1797 pw.print(nativeAllocated); pw.print(',');
1798 pw.print(dalvikAllocated); pw.print(',');
1799 pw.print("N/A,");
1800 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
1801
1802 // Heap info - free
1803 pw.print(nativeFree); pw.print(',');
1804 pw.print(dalvikFree); pw.print(',');
1805 pw.print("N/A,");
1806 pw.print(nativeFree + dalvikFree); pw.print(',');
1807
1808 // Heap info - proportional set size
1809 pw.print(memInfo.nativePss); pw.print(',');
1810 pw.print(memInfo.dalvikPss); pw.print(',');
1811 pw.print(memInfo.otherPss); pw.print(',');
1812 pw.print(memInfo.getTotalPss()); pw.print(',');
1813
1814 // Heap info - swappable set size
1815 pw.print(memInfo.nativeSwappablePss); pw.print(',');
1816 pw.print(memInfo.dalvikSwappablePss); pw.print(',');
1817 pw.print(memInfo.otherSwappablePss); pw.print(',');
1818 pw.print(memInfo.getTotalSwappablePss()); pw.print(',');
1819
1820 // Heap info - shared dirty
1821 pw.print(memInfo.nativeSharedDirty); pw.print(',');
1822 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
1823 pw.print(memInfo.otherSharedDirty); pw.print(',');
1824 pw.print(memInfo.getTotalSharedDirty()); pw.print(',');
1825
1826 // Heap info - shared clean
1827 pw.print(memInfo.nativeSharedClean); pw.print(',');
1828 pw.print(memInfo.dalvikSharedClean); pw.print(',');
1829 pw.print(memInfo.otherSharedClean); pw.print(',');
1830 pw.print(memInfo.getTotalSharedClean()); pw.print(',');
1831
1832 // Heap info - private Dirty
1833 pw.print(memInfo.nativePrivateDirty); pw.print(',');
1834 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
1835 pw.print(memInfo.otherPrivateDirty); pw.print(',');
1836 pw.print(memInfo.getTotalPrivateDirty()); pw.print(',');
1837
1838 // Heap info - private Clean
1839 pw.print(memInfo.nativePrivateClean); pw.print(',');
1840 pw.print(memInfo.dalvikPrivateClean); pw.print(',');
1841 pw.print(memInfo.otherPrivateClean); pw.print(',');
1842 pw.print(memInfo.getTotalPrivateClean()); pw.print(',');
1843
1844 // Heap info - other areas
1845 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
1846 pw.print(Debug.MemoryInfo.getOtherLabel(i)); pw.print(',');
1847 pw.print(memInfo.getOtherPss(i)); pw.print(',');
1848 pw.print(memInfo.getOtherSwappablePss(i)); pw.print(',');
1849 pw.print(memInfo.getOtherSharedDirty(i)); pw.print(',');
1850 pw.print(memInfo.getOtherSharedClean(i)); pw.print(',');
1851 pw.print(memInfo.getOtherPrivateDirty(i)); pw.print(',');
1852 pw.print(memInfo.getOtherPrivateClean(i)); pw.print(',');
1853 }
1854 return;
1855 }
1856
1857 // otherwise, show human-readable format
1858 if (dumpFullInfo) {
1859 printRow(pw, HEAP_FULL_COLUMN, "", "Pss", "Pss", "Shared", "Private",
1860 "Shared", "Private", "Swapped", "Heap", "Heap", "Heap");
1861 printRow(pw, HEAP_FULL_COLUMN, "", "Total", "Clean", "Dirty", "Dirty",
1862 "Clean", "Clean", "Dirty", "Size", "Alloc", "Free");
1863 printRow(pw, HEAP_FULL_COLUMN, "", "------", "------", "------", "------",
1864 "------", "------", "------", "------", "------", "------");
1865 printRow(pw, HEAP_FULL_COLUMN, "Native Heap", memInfo.nativePss,
1866 memInfo.nativeSwappablePss, memInfo.nativeSharedDirty,
1867 memInfo.nativePrivateDirty, memInfo.nativeSharedClean,
1868 memInfo.nativePrivateClean, memInfo.nativeSwappedOut,
1869 nativeMax, nativeAllocated, nativeFree);
1870 printRow(pw, HEAP_FULL_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
1871 memInfo.dalvikSwappablePss, memInfo.dalvikSharedDirty,
1872 memInfo.dalvikPrivateDirty, memInfo.dalvikSharedClean,
1873 memInfo.dalvikPrivateClean, memInfo.dalvikSwappedOut,
1874 dalvikMax, dalvikAllocated, dalvikFree);
1875 } else {
1876 printRow(pw, HEAP_COLUMN, "", "Pss", "Private",
1877 "Private", "Swapped", "Heap", "Heap", "Heap");
1878 printRow(pw, HEAP_COLUMN, "", "Total", "Dirty",
1879 "Clean", "Dirty", "Size", "Alloc", "Free");
1880 printRow(pw, HEAP_COLUMN, "", "------", "------", "------",
1881 "------", "------", "------", "------", "------");
1882 printRow(pw, HEAP_COLUMN, "Native Heap", memInfo.nativePss,
1883 memInfo.nativePrivateDirty,
1884 memInfo.nativePrivateClean, memInfo.nativeSwappedOut,
1885 nativeMax, nativeAllocated, nativeFree);
1886 printRow(pw, HEAP_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
1887 memInfo.dalvikPrivateDirty,
1888 memInfo.dalvikPrivateClean, memInfo.dalvikSwappedOut,
1889 dalvikMax, dalvikAllocated, dalvikFree);
1890 }
1891
1892 int otherPss = memInfo.otherPss;
1893 int otherSwappablePss = memInfo.otherSwappablePss;
1894 int otherSharedDirty = memInfo.otherSharedDirty;
1895 int otherPrivateDirty = memInfo.otherPrivateDirty;
1896 int otherSharedClean = memInfo.otherSharedClean;
1897 int otherPrivateClean = memInfo.otherPrivateClean;
1898 int otherSwappedOut = memInfo.otherSwappedOut;
1899
1900 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
1901 final int myPss = memInfo.getOtherPss(i);
1902 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
1903 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
1904 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
1905 final int mySharedClean = memInfo.getOtherSharedClean(i);
1906 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
1907 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
1908 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
1909 || mySharedClean != 0 || myPrivateClean != 0 || mySwappedOut != 0) {
1910 if (dumpFullInfo) {
1911 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
1912 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
1913 mySharedClean, myPrivateClean, mySwappedOut, "", "", "");
1914 } else {
1915 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
1916 myPss, myPrivateDirty,
1917 myPrivateClean, mySwappedOut, "", "", "");
1918 }
1919 otherPss -= myPss;
1920 otherSwappablePss -= mySwappablePss;
1921 otherSharedDirty -= mySharedDirty;
1922 otherPrivateDirty -= myPrivateDirty;
1923 otherSharedClean -= mySharedClean;
1924 otherPrivateClean -= myPrivateClean;
1925 otherSwappedOut -= mySwappedOut;
1926 }
1927 }
1928
1929 if (dumpFullInfo) {
1930 printRow(pw, HEAP_FULL_COLUMN, "Unknown", otherPss, otherSwappablePss,
1931 otherSharedDirty, otherPrivateDirty, otherSharedClean, otherPrivateClean,
1932 otherSwappedOut, "", "", "");
1933 printRow(pw, HEAP_FULL_COLUMN, "TOTAL", memInfo.getTotalPss(),
1934 memInfo.getTotalSwappablePss(),
1935 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
1936 memInfo.getTotalSharedClean(), memInfo.getTotalPrivateClean(),
1937 memInfo.getTotalSwappedOut(), nativeMax+dalvikMax,
1938 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
1939 } else {
1940 printRow(pw, HEAP_COLUMN, "Unknown", otherPss,
1941 otherPrivateDirty, otherPrivateClean, otherSwappedOut,
1942 "", "", "");
1943 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
1944 memInfo.getTotalPrivateDirty(),
1945 memInfo.getTotalPrivateClean(),
1946 memInfo.getTotalSwappedOut(),
1947 nativeMax+dalvikMax,
1948 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
1949 }
1950
1951 if (dumpDalvik) {
1952 pw.println(" ");
1953 pw.println(" Dalvik Details");
1954
1955 for (int i=Debug.MemoryInfo.NUM_OTHER_STATS;
1956 i<Debug.MemoryInfo.NUM_OTHER_STATS + Debug.MemoryInfo.NUM_DVK_STATS; i++) {
1957 final int myPss = memInfo.getOtherPss(i);
1958 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
1959 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
1960 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
1961 final int mySharedClean = memInfo.getOtherSharedClean(i);
1962 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
1963 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
1964 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
1965 || mySharedClean != 0 || myPrivateClean != 0) {
1966 if (dumpFullInfo) {
1967 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
1968 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
1969 mySharedClean, myPrivateClean, mySwappedOut, "", "", "");
1970 } else {
1971 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
1972 myPss, myPrivateDirty,
1973 myPrivateClean, mySwappedOut, "", "", "");
1974 }
1975 }
1976 }
1977 }
1978 }
1979
Jeff Hamilton52d32032011-01-08 15:31:26 -06001980 public void registerOnActivityPausedListener(Activity activity,
1981 OnActivityPausedListener listener) {
1982 synchronized (mOnPauseListeners) {
1983 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1984 if (list == null) {
1985 list = new ArrayList<OnActivityPausedListener>();
1986 mOnPauseListeners.put(activity, list);
1987 }
1988 list.add(listener);
1989 }
1990 }
1991
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001992 public void unregisterOnActivityPausedListener(Activity activity,
1993 OnActivityPausedListener listener) {
1994 synchronized (mOnPauseListeners) {
1995 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1996 if (list != null) {
1997 list.remove(listener);
1998 }
1999 }
2000 }
2001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 public final ActivityInfo resolveActivityInfo(Intent intent) {
2003 ActivityInfo aInfo = intent.resolveActivityInfo(
2004 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
2005 if (aInfo == null) {
2006 // Throw an exception.
2007 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07002008 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 }
2010 return aInfo;
2011 }
Bob Leee5408332009-09-04 18:31:17 -07002012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002015 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002016 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002018 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 r.intent = intent;
2020 r.state = state;
2021 r.parent = parent;
2022 r.embeddedID = id;
2023 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002024 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 if (localLOGV) {
2026 ComponentName compname = intent.getComponent();
2027 String name;
2028 if (compname != null) {
2029 name = compname.toShortString();
2030 } else {
2031 name = "(Intent " + intent + ").getComponent() returned null";
2032 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002033 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 + ", comp=" + name
2035 + ", token=" + token);
2036 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07002037 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 }
2039
2040 public final Activity getActivity(IBinder token) {
2041 return mActivities.get(token).activity;
2042 }
2043
2044 public final void sendActivityResult(
2045 IBinder token, String id, int requestCode,
2046 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002047 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07002048 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2050 list.add(new ResultInfo(id, requestCode, resultCode, data));
2051 mAppThread.scheduleSendResult(token, list);
2052 }
2053
Jeff Brown9ef09972013-10-15 20:49:59 -07002054 private void sendMessage(int what, Object obj) {
2055 sendMessage(what, obj, 0, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 }
2057
Jeff Brown9ef09972013-10-15 20:49:59 -07002058 private void sendMessage(int what, Object obj, int arg1) {
2059 sendMessage(what, obj, arg1, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 }
2061
Jeff Brown9ef09972013-10-15 20:49:59 -07002062 private void sendMessage(int what, Object obj, int arg1, int arg2) {
2063 sendMessage(what, obj, arg1, arg2, false);
2064 }
2065
2066 private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
2067 if (DEBUG_MESSAGES) Slog.v(
2068 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2069 + ": " + arg1 + " / " + obj);
2070 Message msg = Message.obtain();
2071 msg.what = what;
2072 msg.obj = obj;
2073 msg.arg1 = arg1;
2074 msg.arg2 = arg2;
2075 if (async) {
2076 msg.setAsynchronous(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 }
Jeff Brown9ef09972013-10-15 20:49:59 -07002078 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 }
2080
Dianne Hackborn21556372010-02-04 16:34:40 -08002081 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 String what) {
2083 ContextCleanupInfo cci = new ContextCleanupInfo();
2084 cci.context = context;
2085 cci.who = who;
2086 cci.what = what;
Jeff Brown9ef09972013-10-15 20:49:59 -07002087 sendMessage(H.CLEAN_UP_CONTEXT, cci);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
2089
Romain Guy65b345f2011-07-27 18:51:50 -07002090 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2092
2093 ActivityInfo aInfo = r.activityInfo;
2094 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002095 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 Context.CONTEXT_INCLUDE_CODE);
2097 }
Bob Leee5408332009-09-04 18:31:17 -07002098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 ComponentName component = r.intent.getComponent();
2100 if (component == null) {
2101 component = r.intent.resolveActivity(
2102 mInitialApplication.getPackageManager());
2103 r.intent.setComponent(component);
2104 }
2105
2106 if (r.activityInfo.targetActivity != null) {
2107 component = new ComponentName(r.activityInfo.packageName,
2108 r.activityInfo.targetActivity);
2109 }
2110
2111 Activity activity = null;
2112 try {
2113 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2114 activity = mInstrumentation.newActivity(
2115 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002116 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 r.intent.setExtrasClassLoader(cl);
2118 if (r.state != null) {
2119 r.state.setClassLoader(cl);
2120 }
2121 } catch (Exception e) {
2122 if (!mInstrumentation.onException(activity, e)) {
2123 throw new RuntimeException(
2124 "Unable to instantiate activity " + component
2125 + ": " + e.toString(), e);
2126 }
2127 }
2128
2129 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002130 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002131
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002132 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2133 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 TAG, r + ": app=" + app
2135 + ", appName=" + app.getPackageName()
2136 + ", pkg=" + r.packageInfo.getPackageName()
2137 + ", comp=" + r.intent.getComponent().toShortString()
2138 + ", dir=" + r.packageInfo.getAppDir());
2139
2140 if (activity != null) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002141 Context appContext = createBaseContextForActivity(r, activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002143 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002144 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002145 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002146 activity.attach(appContext, this, getInstrumentation(), r.token,
2147 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002148 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07002149
Christopher Tateb70f3df2009-04-07 16:07:59 -07002150 if (customIntent != null) {
2151 activity.mIntent = customIntent;
2152 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002153 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 activity.mStartedActivity = false;
2155 int theme = r.activityInfo.getThemeResource();
2156 if (theme != 0) {
2157 activity.setTheme(theme);
2158 }
2159
2160 activity.mCalled = false;
2161 mInstrumentation.callActivityOnCreate(activity, r.state);
2162 if (!activity.mCalled) {
2163 throw new SuperNotCalledException(
2164 "Activity " + r.intent.getComponent().toShortString() +
2165 " did not call through to super.onCreate()");
2166 }
2167 r.activity = activity;
2168 r.stopped = true;
2169 if (!r.activity.mFinished) {
2170 activity.performStart();
2171 r.stopped = false;
2172 }
2173 if (!r.activity.mFinished) {
2174 if (r.state != null) {
2175 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2176 }
2177 }
2178 if (!r.activity.mFinished) {
2179 activity.mCalled = false;
2180 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2181 if (!activity.mCalled) {
2182 throw new SuperNotCalledException(
2183 "Activity " + r.intent.getComponent().toShortString() +
2184 " did not call through to super.onPostCreate()");
2185 }
2186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 }
2188 r.paused = true;
2189
2190 mActivities.put(r.token, r);
2191
2192 } catch (SuperNotCalledException e) {
2193 throw e;
2194
2195 } catch (Exception e) {
2196 if (!mInstrumentation.onException(activity, e)) {
2197 throw new RuntimeException(
2198 "Unable to start activity " + component
2199 + ": " + e.toString(), e);
2200 }
2201 }
2202
2203 return activity;
2204 }
2205
Jeff Brownefd43bd2012-09-21 17:02:35 -07002206 private Context createBaseContextForActivity(ActivityClientRecord r,
2207 final Activity activity) {
Jeff Brown08988602014-03-10 21:24:37 -07002208 ContextImpl appContext = ContextImpl.createActivityContext(this, r.packageInfo, r.token);
Jeff Brownefd43bd2012-09-21 17:02:35 -07002209 appContext.setOuterContext(activity);
Craig Mautnere0a38842013-12-16 16:14:02 -08002210 Context baseContext = appContext;
2211
2212 final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
2213 try {
2214 IActivityContainer container =
2215 ActivityManagerNative.getDefault().getEnclosingActivityContainer(r.token);
Craig Mautnerbd503a42014-01-10 10:16:43 -08002216 final int displayId =
2217 container == null ? Display.DEFAULT_DISPLAY : container.getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -08002218 if (displayId > Display.DEFAULT_DISPLAY) {
2219 Display display = dm.getRealDisplay(displayId, r.token);
2220 baseContext = appContext.createDisplayContext(display);
2221 }
2222 } catch (RemoteException e) {
2223 }
Jeff Brownefd43bd2012-09-21 17:02:35 -07002224
2225 // For debugging purposes, if the activity's package name contains the value of
2226 // the "debug.use-second-display" system property as a substring, then show
2227 // its content on a secondary display if there is one.
Jeff Brownefd43bd2012-09-21 17:02:35 -07002228 String pkgName = SystemProperties.get("debug.second-display.pkg");
2229 if (pkgName != null && !pkgName.isEmpty()
2230 && r.packageInfo.mPackageName.contains(pkgName)) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002231 for (int displayId : dm.getDisplayIds()) {
2232 if (displayId != Display.DEFAULT_DISPLAY) {
Craig Mautner48d0d182013-06-11 07:53:06 -07002233 Display display = dm.getRealDisplay(displayId, r.token);
Jeff Brownefd43bd2012-09-21 17:02:35 -07002234 baseContext = appContext.createDisplayContext(display);
2235 break;
2236 }
2237 }
2238 }
2239 return baseContext;
2240 }
2241
Romain Guy65b345f2011-07-27 18:51:50 -07002242 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 // If we are getting ready to gc after going to the background, well
2244 // we are back active so skip it.
2245 unscheduleGcIdler();
2246
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002247 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002248 mProfiler.setProfiler(r.profileFile, r.profileFd);
2249 mProfiler.startProfiling();
2250 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002251 }
2252
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002253 // Make sure we are running with the most recent config.
2254 handleConfigurationChanged(null, null);
2255
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002256 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07002258 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259
2260 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002261 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002262 Bundle oldState = r.state;
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002263 handleResumeActivity(r.token, false, r.isForward,
2264 !r.activity.mFinished && !r.startsNotResumed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265
2266 if (!r.activity.mFinished && r.startsNotResumed) {
2267 // The activity manager actually wants this one to start out
2268 // paused, because it needs to be visible but isn't in the
2269 // foreground. We accomplish this by going through the
2270 // normal startup (because activities expect to go through
2271 // onResume() the first time they run, before their window
2272 // is displayed), and then pausing it. However, in this case
2273 // we do -not- need to do the full pause cycle (of freezing
2274 // and such) because the activity manager assumes it can just
2275 // retain the current state it has.
2276 try {
2277 r.activity.mCalled = false;
2278 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002279 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002280 // we need to be created again. But we only do this
2281 // for pre-Honeycomb apps, which always save their state
2282 // when pausing, so we can not have them save their state
2283 // when restarting from a paused state. For HC and later,
2284 // we want to (and can) let the state be saved as the normal
2285 // part of stopping the activity.
2286 if (r.isPreHoneycomb()) {
2287 r.state = oldState;
2288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 if (!r.activity.mCalled) {
2290 throw new SuperNotCalledException(
2291 "Activity " + r.intent.getComponent().toShortString() +
2292 " did not call through to super.onPause()");
2293 }
2294
2295 } catch (SuperNotCalledException e) {
2296 throw e;
2297
2298 } catch (Exception e) {
2299 if (!mInstrumentation.onException(r.activity, e)) {
2300 throw new RuntimeException(
2301 "Unable to pause activity "
2302 + r.intent.getComponent().toShortString()
2303 + ": " + e.toString(), e);
2304 }
2305 }
2306 r.paused = true;
2307 }
2308 } else {
2309 // If there was an error, for any reason, tell the activity
2310 // manager to stop us.
2311 try {
2312 ActivityManagerNative.getDefault()
2313 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2314 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002315 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 }
2317 }
2318 }
2319
Romain Guy65b345f2011-07-27 18:51:50 -07002320 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 List<Intent> intents) {
2322 final int N = intents.size();
2323 for (int i=0; i<N; i++) {
2324 Intent intent = intents.get(i);
2325 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002326 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2328 }
2329 }
2330
2331 public final void performNewIntents(IBinder token,
2332 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002333 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 if (r != null) {
2335 final boolean resumed = !r.paused;
2336 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002337 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 mInstrumentation.callActivityOnPause(r.activity);
2339 }
2340 deliverNewIntents(r, intents);
2341 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002342 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002343 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 }
2345 }
2346 }
Bob Leee5408332009-09-04 18:31:17 -07002347
Romain Guy65b345f2011-07-27 18:51:50 -07002348 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 performNewIntents(data.token, data.intents);
2350 }
2351
Adam Skorydfc7fd72013-08-05 19:23:41 -07002352 public void handleRequestAssistContextExtras(RequestAssistContextExtras cmd) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002353 Bundle data = new Bundle();
2354 ActivityClientRecord r = mActivities.get(cmd.activityToken);
2355 if (r != null) {
2356 r.activity.getApplication().dispatchOnProvideAssistData(r.activity, data);
2357 r.activity.onProvideAssistData(data);
2358 }
2359 if (data.isEmpty()) {
2360 data = null;
2361 }
2362 IActivityManager mgr = ActivityManagerNative.getDefault();
2363 try {
Adam Skory7140a252013-09-11 12:04:58 +01002364 mgr.reportAssistContextExtras(cmd.requestToken, data);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002365 } catch (RemoteException e) {
2366 }
2367 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002368
2369 public void handleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
2370 ActivityClientRecord r = mActivities.get(token);
2371 if (r != null) {
2372 r.activity.onTranslucentConversionComplete(drawComplete);
2373 }
2374 }
2375
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002376 public void handleInstallProvider(ProviderInfo info) {
2377 installContentProviders(mInitialApplication, Lists.newArrayList(info));
2378 }
2379
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002380 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2381
2382 /**
2383 * Return the Intent that's currently being handled by a
2384 * BroadcastReceiver on this thread, or null if none.
2385 * @hide
2386 */
2387 public static Intent getIntentBeingBroadcast() {
2388 return sCurrentBroadcastIntent.get();
2389 }
2390
Romain Guy65b345f2011-07-27 18:51:50 -07002391 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 // If we are getting ready to gc after going to the background, well
2393 // we are back active so skip it.
2394 unscheduleGcIdler();
2395
2396 String component = data.intent.getComponent().getClassName();
2397
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002398 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002399 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400
2401 IActivityManager mgr = ActivityManagerNative.getDefault();
2402
Romain Guy65b345f2011-07-27 18:51:50 -07002403 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 try {
2405 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2406 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002407 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2409 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002410 if (DEBUG_BROADCAST) Slog.i(TAG,
2411 "Finishing failed broadcast to " + data.intent.getComponent());
2412 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 throw new RuntimeException(
2414 "Unable to instantiate receiver " + component
2415 + ": " + e.toString(), e);
2416 }
2417
2418 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002419 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002420
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002421 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 TAG, "Performing receive of " + data.intent
2423 + ": app=" + app
2424 + ", appName=" + app.getPackageName()
2425 + ", pkg=" + packageInfo.getPackageName()
2426 + ", comp=" + data.intent.getComponent().toShortString()
2427 + ", dir=" + packageInfo.getAppDir());
2428
Dianne Hackborn21556372010-02-04 16:34:40 -08002429 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002430 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002431 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 receiver.onReceive(context.getReceiverRestrictedContext(),
2433 data.intent);
2434 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002435 if (DEBUG_BROADCAST) Slog.i(TAG,
2436 "Finishing failed broadcast to " + data.intent.getComponent());
2437 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 if (!mInstrumentation.onException(receiver, e)) {
2439 throw new RuntimeException(
2440 "Unable to start receiver " + component
2441 + ": " + e.toString(), e);
2442 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002443 } finally {
2444 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 }
2446
Dianne Hackborne829fef2010-10-26 17:44:01 -07002447 if (receiver.getPendingResult() != null) {
2448 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 }
2450 }
2451
Christopher Tate181fafa2009-05-14 11:12:14 -07002452 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002453 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002454 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002455
Christopher Tate346acb12012-10-15 19:20:25 -07002456 // Sanity check the requested target package's uid against ours
2457 try {
2458 PackageInfo requestedPackage = getPackageManager().getPackageInfo(
2459 data.appInfo.packageName, 0, UserHandle.myUserId());
2460 if (requestedPackage.applicationInfo.uid != Process.myUid()) {
2461 Slog.w(TAG, "Asked to instantiate non-matching package "
2462 + data.appInfo.packageName);
2463 return;
2464 }
2465 } catch (RemoteException e) {
2466 Slog.e(TAG, "Can't reach package manager", e);
2467 return;
2468 }
2469
Christopher Tate181fafa2009-05-14 11:12:14 -07002470 // no longer idle; we have backup work to do
2471 unscheduleGcIdler();
2472
2473 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002474 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002475 String packageName = packageInfo.mPackageName;
Christopher Tate346acb12012-10-15 19:20:25 -07002476 if (packageName == null) {
2477 Slog.d(TAG, "Asked to create backup agent for nonexistent package");
2478 return;
2479 }
2480
Christopher Tate181fafa2009-05-14 11:12:14 -07002481 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002482 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002483 + " already exists");
2484 return;
2485 }
Bob Leee5408332009-09-04 18:31:17 -07002486
Christopher Tate181fafa2009-05-14 11:12:14 -07002487 BackupAgent agent = null;
2488 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002489
Christopher Tate79ec80d2011-06-24 14:58:49 -07002490 // full backup operation but no app-supplied agent? use the default implementation
2491 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2492 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002493 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002494 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002495
Christopher Tate181fafa2009-05-14 11:12:14 -07002496 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002497 IBinder binder = null;
2498 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002499 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2500
Christopher Tated1475e02009-07-09 15:36:17 -07002501 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002502 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002503
2504 // set up the agent's context
Jeff Brown08988602014-03-10 21:24:37 -07002505 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
Christopher Tated1475e02009-07-09 15:36:17 -07002506 context.setOuterContext(agent);
2507 agent.attach(context);
2508
2509 agent.onCreate();
2510 binder = agent.onBind();
2511 mBackupAgents.put(packageName, agent);
2512 } catch (Exception e) {
2513 // If this is during restore, fail silently; otherwise go
2514 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002515 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002516 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2517 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002518 throw e;
2519 }
2520 // falling through with 'binder' still null
2521 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002522
2523 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002524 try {
2525 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2526 } catch (RemoteException e) {
2527 // nothing to do.
2528 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002529 } catch (Exception e) {
2530 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002531 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002532 }
2533 }
2534
2535 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002536 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002537 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002538
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002539 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002540 String packageName = packageInfo.mPackageName;
2541 BackupAgent agent = mBackupAgents.get(packageName);
2542 if (agent != null) {
2543 try {
2544 agent.onDestroy();
2545 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002546 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002547 e.printStackTrace();
2548 }
2549 mBackupAgents.remove(packageName);
2550 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002551 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002552 }
2553 }
2554
Romain Guy65b345f2011-07-27 18:51:50 -07002555 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 // If we are getting ready to gc after going to the background, well
2557 // we are back active so skip it.
2558 unscheduleGcIdler();
2559
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002560 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002561 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 Service service = null;
2563 try {
2564 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2565 service = (Service) cl.loadClass(data.info.name).newInstance();
2566 } catch (Exception e) {
2567 if (!mInstrumentation.onException(service, e)) {
2568 throw new RuntimeException(
2569 "Unable to instantiate service " + data.info.name
2570 + ": " + e.toString(), e);
2571 }
2572 }
2573
2574 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002575 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576
Jeff Brown08988602014-03-10 21:24:37 -07002577 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
2578 context.setOuterContext(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002580 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 service.attach(context, this, data.info.name, data.token, app,
2582 ActivityManagerNative.getDefault());
2583 service.onCreate();
2584 mServices.put(data.token, service);
2585 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002586 ActivityManagerNative.getDefault().serviceDoneExecuting(
2587 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 } catch (RemoteException e) {
2589 // nothing to do.
2590 }
2591 } catch (Exception e) {
2592 if (!mInstrumentation.onException(service, e)) {
2593 throw new RuntimeException(
2594 "Unable to create service " + data.info.name
2595 + ": " + e.toString(), e);
2596 }
2597 }
2598 }
2599
Romain Guy65b345f2011-07-27 18:51:50 -07002600 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002602 if (DEBUG_SERVICE)
2603 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 if (s != null) {
2605 try {
2606 data.intent.setExtrasClassLoader(s.getClassLoader());
2607 try {
2608 if (!data.rebind) {
2609 IBinder binder = s.onBind(data.intent);
2610 ActivityManagerNative.getDefault().publishService(
2611 data.token, data.intent, binder);
2612 } else {
2613 s.onRebind(data.intent);
2614 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002615 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002617 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 } catch (RemoteException ex) {
2619 }
2620 } catch (Exception e) {
2621 if (!mInstrumentation.onException(s, e)) {
2622 throw new RuntimeException(
2623 "Unable to bind to service " + s
2624 + " with " + data.intent + ": " + e.toString(), e);
2625 }
2626 }
2627 }
2628 }
2629
Romain Guy65b345f2011-07-27 18:51:50 -07002630 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 Service s = mServices.get(data.token);
2632 if (s != null) {
2633 try {
2634 data.intent.setExtrasClassLoader(s.getClassLoader());
2635 boolean doRebind = s.onUnbind(data.intent);
2636 try {
2637 if (doRebind) {
2638 ActivityManagerNative.getDefault().unbindFinished(
2639 data.token, data.intent, doRebind);
2640 } else {
2641 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002642 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 }
2644 } catch (RemoteException ex) {
2645 }
2646 } catch (Exception e) {
2647 if (!mInstrumentation.onException(s, e)) {
2648 throw new RuntimeException(
2649 "Unable to unbind to service " + s
2650 + " with " + data.intent + ": " + e.toString(), e);
2651 }
2652 }
2653 }
2654 }
2655
Dianne Hackborn625ac272010-09-17 18:29:22 -07002656 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002657 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2658 try {
2659 Service s = mServices.get(info.token);
2660 if (s != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002661 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2662 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002663 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2664 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002666 } finally {
2667 IoUtils.closeQuietly(info.fd);
2668 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 }
2670 }
2671
Dianne Hackborn625ac272010-09-17 18:29:22 -07002672 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002673 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2674 try {
2675 ActivityClientRecord r = mActivities.get(info.token);
2676 if (r != null && r.activity != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002677 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2678 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002679 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2680 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002681 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002682 } finally {
2683 IoUtils.closeQuietly(info.fd);
2684 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002685 }
2686 }
2687
Marco Nelissen18cb2872011-11-15 11:19:53 -08002688 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002689 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2690 try {
2691 ProviderClientRecord r = mLocalProviders.get(info.token);
2692 if (r != null && r.mLocalProvider != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002693 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2694 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002695 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2696 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002697 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002698 } finally {
2699 IoUtils.closeQuietly(info.fd);
2700 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002701 }
2702 }
2703
Romain Guy65b345f2011-07-27 18:51:50 -07002704 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 Service s = mServices.get(data.token);
2706 if (s != null) {
2707 try {
2708 if (data.args != null) {
2709 data.args.setExtrasClassLoader(s.getClassLoader());
2710 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002711 int res;
2712 if (!data.taskRemoved) {
2713 res = s.onStartCommand(data.args, data.flags, data.startId);
2714 } else {
2715 s.onTaskRemoved(data.args);
2716 res = Service.START_TASK_REMOVED_COMPLETE;
2717 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002718
2719 QueuedWork.waitToFinish();
2720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002722 ActivityManagerNative.getDefault().serviceDoneExecuting(
2723 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 } catch (RemoteException e) {
2725 // nothing to do.
2726 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002727 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 } catch (Exception e) {
2729 if (!mInstrumentation.onException(s, e)) {
2730 throw new RuntimeException(
2731 "Unable to start service " + s
2732 + " with " + data.args + ": " + e.toString(), e);
2733 }
2734 }
2735 }
2736 }
2737
Romain Guy65b345f2011-07-27 18:51:50 -07002738 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 Service s = mServices.remove(token);
2740 if (s != null) {
2741 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002742 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 s.onDestroy();
2744 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002745 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002747 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002749
2750 QueuedWork.waitToFinish();
2751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002753 ActivityManagerNative.getDefault().serviceDoneExecuting(
2754 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 } catch (RemoteException e) {
2756 // nothing to do.
2757 }
2758 } catch (Exception e) {
2759 if (!mInstrumentation.onException(s, e)) {
2760 throw new RuntimeException(
2761 "Unable to stop service " + s
2762 + ": " + e.toString(), e);
2763 }
2764 }
2765 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002766 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 }
2768
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002769 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002771 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002772 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 + " finished=" + r.activity.mFinished);
2774 if (r != null && !r.activity.mFinished) {
2775 if (clearHide) {
2776 r.hideForNow = false;
2777 r.activity.mStartedActivity = false;
2778 }
2779 try {
Dianne Hackborn689586d2012-10-01 18:23:04 -07002780 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 if (r.pendingIntents != null) {
2782 deliverNewIntents(r, r.pendingIntents);
2783 r.pendingIntents = null;
2784 }
2785 if (r.pendingResults != null) {
2786 deliverResults(r, r.pendingResults);
2787 r.pendingResults = null;
2788 }
2789 r.activity.performResume();
2790
Bob Leee5408332009-09-04 18:31:17 -07002791 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002792 UserHandle.myUserId(), r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 r.paused = false;
2795 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 r.state = null;
2797 } catch (Exception e) {
2798 if (!mInstrumentation.onException(r.activity, e)) {
2799 throw new RuntimeException(
2800 "Unable to resume activity "
2801 + r.intent.getComponent().toShortString()
2802 + ": " + e.toString(), e);
2803 }
2804 }
2805 }
2806 return r;
2807 }
2808
Romain Guya998dff2012-03-23 18:58:36 -07002809 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002810 if (r.mPendingRemoveWindow != null) {
2811 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2812 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2813 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07002814 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002815 r.activity.getClass().getName(), "Activity");
2816 }
2817 }
2818 r.mPendingRemoveWindow = null;
2819 r.mPendingRemoveWindowManager = null;
2820 }
2821
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002822 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward,
2823 boolean reallyResume) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 // If we are getting ready to gc after going to the background, well
2825 // we are back active so skip it.
2826 unscheduleGcIdler();
2827
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002828 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829
2830 if (r != null) {
2831 final Activity a = r.activity;
2832
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002833 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 TAG, "Resume " + r + " started activity: " +
2835 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2836 + ", finished: " + a.mFinished);
2837
2838 final int forwardBit = isForward ?
2839 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 // If the window hasn't yet been added to the window manager,
2842 // and this guy didn't finish itself or start another activity,
2843 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002844 boolean willBeVisible = !a.mStartedActivity;
2845 if (!willBeVisible) {
2846 try {
2847 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2848 a.getActivityToken());
2849 } catch (RemoteException e) {
2850 }
2851 }
2852 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 r.window = r.activity.getWindow();
2854 View decor = r.window.getDecorView();
2855 decor.setVisibility(View.INVISIBLE);
2856 ViewManager wm = a.getWindowManager();
2857 WindowManager.LayoutParams l = r.window.getAttributes();
2858 a.mDecor = decor;
2859 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2860 l.softInputMode |= forwardBit;
2861 if (a.mVisibleFromClient) {
2862 a.mWindowAdded = true;
2863 wm.addView(decor, l);
2864 }
2865
2866 // If the window has already been added, but during resume
2867 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002868 // window visible.
2869 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002870 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 TAG, "Launch " + r + " mStartedActivity set");
2872 r.hideForNow = true;
2873 }
2874
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002875 // Get rid of anything left hanging around.
2876 cleanUpPendingRemoveWindows(r);
2877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 // The window is now visible if it has been added, we are not
2879 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002880 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002881 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002883 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002884 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07002886 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 r.newConfig = null;
2888 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002889 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 + isForward);
2891 WindowManager.LayoutParams l = r.window.getAttributes();
2892 if ((l.softInputMode
2893 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2894 != forwardBit) {
2895 l.softInputMode = (l.softInputMode
2896 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2897 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002898 if (r.activity.mVisibleFromClient) {
2899 ViewManager wm = a.getWindowManager();
2900 View decor = r.window.getDecorView();
2901 wm.updateViewLayout(decor, l);
2902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 }
2904 r.activity.mVisibleFromServer = true;
2905 mNumVisibleActivities++;
2906 if (r.activity.mVisibleFromClient) {
2907 r.activity.makeVisible();
2908 }
2909 }
2910
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002911 if (!r.onlyLocalRequest) {
2912 r.nextIdle = mNewActivities;
2913 mNewActivities = r;
2914 if (localLOGV) Slog.v(
2915 TAG, "Scheduling idle handler for " + r);
2916 Looper.myQueue().addIdleHandler(new Idler());
2917 }
2918 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002920 // Tell the activity manager we have resumed.
2921 if (reallyResume) {
2922 try {
2923 ActivityManagerNative.getDefault().activityResumed(token);
2924 } catch (RemoteException ex) {
2925 }
2926 }
2927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 } else {
2929 // If an exception was thrown when trying to resume, then
2930 // just end this activity.
2931 try {
2932 ActivityManagerNative.getDefault()
2933 .finishActivity(token, Activity.RESULT_CANCELED, null);
2934 } catch (RemoteException ex) {
2935 }
2936 }
2937 }
2938
2939 private int mThumbnailWidth = -1;
2940 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002941 private Bitmap mAvailThumbnailBitmap = null;
2942 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943
Romain Guy65b345f2011-07-27 18:51:50 -07002944 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002945 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002947 if (thumbnail == null) {
2948 int w = mThumbnailWidth;
2949 int h;
2950 if (w < 0) {
2951 Resources res = r.activity.getResources();
2952 mThumbnailHeight = h =
2953 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002955 mThumbnailWidth = w =
2956 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2957 } else {
2958 h = mThumbnailHeight;
2959 }
2960
2961 // On platforms where we don't want thumbnails, set dims to (0,0)
2962 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07002963 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
2964 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002965 thumbnail.eraseColor(0);
2966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 }
2968
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002969 if (thumbnail != null) {
2970 Canvas cv = mThumbnailCanvas;
2971 if (cv == null) {
2972 mThumbnailCanvas = cv = new Canvas();
2973 }
2974
2975 cv.setBitmap(thumbnail);
2976 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2977 mAvailThumbnailBitmap = thumbnail;
2978 thumbnail = null;
2979 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002980 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 } catch (Exception e) {
2984 if (!mInstrumentation.onException(r.activity, e)) {
2985 throw new RuntimeException(
2986 "Unable to create thumbnail of "
2987 + r.intent.getComponent().toShortString()
2988 + ": " + e.toString(), e);
2989 }
2990 thumbnail = null;
2991 }
2992
2993 return thumbnail;
2994 }
2995
Romain Guy65b345f2011-07-27 18:51:50 -07002996 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002998 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003000 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 if (userLeaving) {
3002 performUserLeavingActivity(r);
3003 }
Bob Leee5408332009-09-04 18:31:17 -07003004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003006 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07003008 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003009 if (r.isPreHoneycomb()) {
3010 QueuedWork.waitToFinish();
3011 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 // Tell the activity manager we have paused.
3014 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003015 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 } catch (RemoteException ex) {
3017 }
3018 }
3019 }
3020
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003021 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 mInstrumentation.callActivityOnUserLeaving(r.activity);
3023 }
3024
3025 final Bundle performPauseActivity(IBinder token, boolean finished,
3026 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003027 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 return r != null ? performPauseActivity(r, finished, saveState) : null;
3029 }
3030
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003031 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 boolean saveState) {
3033 if (r.paused) {
3034 if (r.activity.mFinished) {
3035 // If we are finishing, we won't call onResume() in certain cases.
3036 // So here we likewise don't want to call onPause() if the activity
3037 // isn't resumed.
3038 return null;
3039 }
3040 RuntimeException e = new RuntimeException(
3041 "Performing pause of activity that is not resumed: "
3042 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003043 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 }
3045 Bundle state = null;
3046 if (finished) {
3047 r.activity.mFinished = true;
3048 }
3049 try {
3050 // Next have the activity save its current state and managed dialogs...
3051 if (!r.activity.mFinished && saveState) {
3052 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003053 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003054 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
3055 r.state = state;
3056 }
3057 // Now we are idle.
3058 r.activity.mCalled = false;
3059 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003060 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, UserHandle.myUserId(),
3061 r.activity.getComponentName().getClassName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 if (!r.activity.mCalled) {
3063 throw new SuperNotCalledException(
3064 "Activity " + r.intent.getComponent().toShortString() +
3065 " did not call through to super.onPause()");
3066 }
3067
3068 } catch (SuperNotCalledException e) {
3069 throw e;
3070
3071 } catch (Exception e) {
3072 if (!mInstrumentation.onException(r.activity, e)) {
3073 throw new RuntimeException(
3074 "Unable to pause activity "
3075 + r.intent.getComponent().toShortString()
3076 + ": " + e.toString(), e);
3077 }
3078 }
3079 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06003080
3081 // Notify any outstanding on paused listeners
3082 ArrayList<OnActivityPausedListener> listeners;
3083 synchronized (mOnPauseListeners) {
3084 listeners = mOnPauseListeners.remove(r.activity);
3085 }
3086 int size = (listeners != null ? listeners.size() : 0);
3087 for (int i = 0; i < size; i++) {
3088 listeners.get(i).onPaused(r.activity);
3089 }
3090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 return state;
3092 }
3093
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003094 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003095 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003096 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 }
3098
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003099 private static class StopInfo implements Runnable {
3100 ActivityClientRecord activity;
3101 Bundle state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 Bitmap thumbnail;
3103 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003104
3105 @Override public void run() {
3106 // Tell activity manager we have been stopped.
3107 try {
3108 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
3109 ActivityManagerNative.getDefault().activityStopped(
3110 activity.token, state, thumbnail, description);
3111 } catch (RemoteException ex) {
3112 }
3113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 }
3115
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003116 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003117 public final IActivityManager.ContentProviderHolder holder;
3118 public final ProviderClientRecord client;
3119 public int stableCount;
3120 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003121
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003122 // When this is set, the stable and unstable ref counts are 0 and
3123 // we have a pending operation scheduled to remove the ref count
3124 // from the activity manager. On the activity manager we are still
3125 // holding an unstable ref, though it is not reflected in the counts
3126 // here.
3127 public boolean removePending;
3128
3129 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
3130 ProviderClientRecord inClient, int sCount, int uCount) {
3131 holder = inHolder;
3132 client = inClient;
3133 stableCount = sCount;
3134 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 }
3136 }
3137
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003138 /**
3139 * Core implementation of stopping an activity. Note this is a little
3140 * tricky because the server's meaning of stop is slightly different
3141 * than our client -- for the server, stop means to save state and give
3142 * it the result when it is done, but the window may still be visible.
3143 * For the client, we want to call onStop()/onStart() to indicate when
3144 * the activity's UI visibillity changes.
3145 */
Romain Guy65b345f2011-07-27 18:51:50 -07003146 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003147 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003148 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003149 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 if (r != null) {
3151 if (!keepShown && r.stopped) {
3152 if (r.activity.mFinished) {
3153 // If we are finishing, we won't call onResume() in certain
3154 // cases. So here we likewise don't want to call onStop()
3155 // if the activity isn't resumed.
3156 return;
3157 }
3158 RuntimeException e = new RuntimeException(
3159 "Performing stop of activity that is not resumed: "
3160 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003161 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 }
3163
3164 if (info != null) {
3165 try {
3166 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003167 // For now, don't create the thumbnail here; we are
3168 // doing that by doing a screen snapshot.
3169 info.thumbnail = null; //createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 info.description = r.activity.onCreateDescription();
3171 } catch (Exception e) {
3172 if (!mInstrumentation.onException(r.activity, e)) {
3173 throw new RuntimeException(
3174 "Unable to save state of activity "
3175 + r.intent.getComponent().toShortString()
3176 + ": " + e.toString(), e);
3177 }
3178 }
3179 }
3180
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003181 // Next have the activity save its current state and managed dialogs...
3182 if (!r.activity.mFinished && saveState) {
3183 if (r.state == null) {
3184 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003185 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003186 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
3187 r.state = state;
3188 } else {
3189 state = r.state;
3190 }
3191 }
3192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 if (!keepShown) {
3194 try {
3195 // Now we are idle.
3196 r.activity.performStop();
3197 } catch (Exception e) {
3198 if (!mInstrumentation.onException(r.activity, e)) {
3199 throw new RuntimeException(
3200 "Unable to stop activity "
3201 + r.intent.getComponent().toShortString()
3202 + ": " + e.toString(), e);
3203 }
3204 }
3205 r.stopped = true;
3206 }
3207
3208 r.paused = true;
3209 }
3210 }
3211
Romain Guy65b345f2011-07-27 18:51:50 -07003212 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 View v = r.activity.mDecor;
3214 if (v != null) {
3215 if (show) {
3216 if (!r.activity.mVisibleFromServer) {
3217 r.activity.mVisibleFromServer = true;
3218 mNumVisibleActivities++;
3219 if (r.activity.mVisibleFromClient) {
3220 r.activity.makeVisible();
3221 }
3222 }
3223 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003224 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003225 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003227 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 r.newConfig = null;
3229 }
3230 } else {
3231 if (r.activity.mVisibleFromServer) {
3232 r.activity.mVisibleFromServer = false;
3233 mNumVisibleActivities--;
3234 v.setVisibility(View.INVISIBLE);
3235 }
3236 }
3237 }
3238 }
3239
Romain Guy65b345f2011-07-27 18:51:50 -07003240 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003241 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 r.activity.mConfigChangeFlags |= configChanges;
3243
3244 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003245 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003247 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 TAG, "Finishing stop of " + r + ": show=" + show
3249 + " win=" + r.window);
3250
3251 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003252
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003253 // Make sure any pending writes are now committed.
3254 if (!r.isPreHoneycomb()) {
3255 QueuedWork.waitToFinish();
3256 }
3257
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003258 // Schedule the call to tell the activity manager we have
3259 // stopped. We don't do this immediately, because we want to
3260 // have a chance for any other pending work (in particular memory
3261 // trim requests) to complete before you tell the activity
3262 // manager to proceed and allow us to go fully into the background.
3263 info.activity = r;
3264 info.state = r.state;
3265 mH.post(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 }
3267
3268 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003269 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 if (r.stopped) {
3271 r.activity.performRestart();
3272 r.stopped = false;
3273 }
3274 }
3275
Romain Guy65b345f2011-07-27 18:51:50 -07003276 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003277 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003278
3279 if (r == null) {
3280 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3281 return;
3282 }
3283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003285 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 } else if (show && r.stopped) {
3287 // If we are getting ready to gc after going to the background, well
3288 // we are back active so skip it.
3289 unscheduleGcIdler();
3290
3291 r.activity.performRestart();
3292 r.stopped = false;
3293 }
3294 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003295 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 TAG, "Handle window " + r + " visibility: " + show);
3297 updateVisibility(r, show);
3298 }
3299 }
3300
Romain Guy65b345f2011-07-27 18:51:50 -07003301 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003302 ActivityClientRecord r = mActivities.get(token);
3303
3304 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003305 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003306 return;
3307 }
3308
3309 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003310 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003311 try {
3312 // Now we are idle.
3313 r.activity.performStop();
3314 } catch (Exception e) {
3315 if (!mInstrumentation.onException(r.activity, e)) {
3316 throw new RuntimeException(
3317 "Unable to stop activity "
3318 + r.intent.getComponent().toShortString()
3319 + ": " + e.toString(), e);
3320 }
3321 }
3322 r.stopped = true;
3323 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003324
3325 // Make sure any pending writes are now committed.
3326 if (!r.isPreHoneycomb()) {
3327 QueuedWork.waitToFinish();
3328 }
3329
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003330 // Tell activity manager we slept.
3331 try {
3332 ActivityManagerNative.getDefault().activitySlept(r.token);
3333 } catch (RemoteException ex) {
3334 }
3335 } else {
3336 if (r.stopped && r.activity.mVisibleFromServer) {
3337 r.activity.performRestart();
3338 r.stopped = false;
3339 }
3340 }
3341 }
3342
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003343 private void handleSetCoreSettings(Bundle coreSettings) {
Craig Mautner88c05892013-06-28 09:47:45 -07003344 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003345 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003346 }
3347 }
3348
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003349 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3350 LoadedApk apk = peekPackageInfo(data.pkg, false);
3351 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003352 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003353 }
3354 apk = peekPackageInfo(data.pkg, true);
3355 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003356 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003357 }
3358 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07003359 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003360 }
3361
Romain Guy65b345f2011-07-27 18:51:50 -07003362 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 final int N = results.size();
3364 for (int i=0; i<N; i++) {
3365 ResultInfo ri = results.get(i);
3366 try {
3367 if (ri.mData != null) {
3368 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
3369 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003370 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003371 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 r.activity.dispatchActivityResult(ri.mResultWho,
3373 ri.mRequestCode, ri.mResultCode, ri.mData);
3374 } catch (Exception e) {
3375 if (!mInstrumentation.onException(r.activity, e)) {
3376 throw new RuntimeException(
3377 "Failure delivering result " + ri + " to activity "
3378 + r.intent.getComponent().toShortString()
3379 + ": " + e.toString(), e);
3380 }
3381 }
3382 }
3383 }
3384
Romain Guy65b345f2011-07-27 18:51:50 -07003385 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003386 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003387 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 if (r != null) {
3389 final boolean resumed = !r.paused;
3390 if (!r.activity.mFinished && r.activity.mDecor != null
3391 && r.hideForNow && resumed) {
3392 // We had hidden the activity because it started another
3393 // one... we have gotten a result back and we are not
3394 // paused, so make sure our window is visible.
3395 updateVisibility(r, true);
3396 }
3397 if (resumed) {
3398 try {
3399 // Now we are idle.
3400 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003401 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402 mInstrumentation.callActivityOnPause(r.activity);
3403 if (!r.activity.mCalled) {
3404 throw new SuperNotCalledException(
3405 "Activity " + r.intent.getComponent().toShortString()
3406 + " did not call through to super.onPause()");
3407 }
3408 } catch (SuperNotCalledException e) {
3409 throw e;
3410 } catch (Exception e) {
3411 if (!mInstrumentation.onException(r.activity, e)) {
3412 throw new RuntimeException(
3413 "Unable to pause activity "
3414 + r.intent.getComponent().toShortString()
3415 + ": " + e.toString(), e);
3416 }
3417 }
3418 }
3419 deliverResults(r, res.results);
3420 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003421 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003422 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 }
3424 }
3425 }
3426
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003427 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 return performDestroyActivity(token, finishing, 0, false);
3429 }
3430
Romain Guy65b345f2011-07-27 18:51:50 -07003431 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003433 ActivityClientRecord r = mActivities.get(token);
Craig Mautner88c05892013-06-28 09:47:45 -07003434 Class<? extends Activity> activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003435 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003437 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 r.activity.mConfigChangeFlags |= configChanges;
3439 if (finishing) {
3440 r.activity.mFinished = true;
3441 }
3442 if (!r.paused) {
3443 try {
3444 r.activity.mCalled = false;
3445 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003446 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, UserHandle.myUserId(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 r.activity.getComponentName().getClassName());
3448 if (!r.activity.mCalled) {
3449 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003450 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 + " did not call through to super.onPause()");
3452 }
3453 } catch (SuperNotCalledException e) {
3454 throw e;
3455 } catch (Exception e) {
3456 if (!mInstrumentation.onException(r.activity, e)) {
3457 throw new RuntimeException(
3458 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003459 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 + ": " + e.toString(), e);
3461 }
3462 }
3463 r.paused = true;
3464 }
3465 if (!r.stopped) {
3466 try {
3467 r.activity.performStop();
3468 } catch (SuperNotCalledException e) {
3469 throw e;
3470 } catch (Exception e) {
3471 if (!mInstrumentation.onException(r.activity, e)) {
3472 throw new RuntimeException(
3473 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003474 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003475 + ": " + e.toString(), e);
3476 }
3477 }
3478 r.stopped = true;
3479 }
3480 if (getNonConfigInstance) {
3481 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003482 r.lastNonConfigurationInstances
3483 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 } catch (Exception e) {
3485 if (!mInstrumentation.onException(r.activity, e)) {
3486 throw new RuntimeException(
3487 "Unable to retain activity "
3488 + r.intent.getComponent().toShortString()
3489 + ": " + e.toString(), e);
3490 }
3491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 }
3493 try {
3494 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003495 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 if (!r.activity.mCalled) {
3497 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003498 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 " did not call through to super.onDestroy()");
3500 }
3501 if (r.window != null) {
3502 r.window.closeAllPanels();
3503 }
3504 } catch (SuperNotCalledException e) {
3505 throw e;
3506 } catch (Exception e) {
3507 if (!mInstrumentation.onException(r.activity, e)) {
3508 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003509 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3510 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003511 }
3512 }
3513 }
3514 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003515 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003516 return r;
3517 }
3518
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003519 private static String safeToComponentShortString(Intent intent) {
3520 ComponentName component = intent.getComponent();
3521 return component == null ? "[Unknown]" : component.toShortString();
3522 }
3523
Romain Guy65b345f2011-07-27 18:51:50 -07003524 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003526 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 configChanges, getNonConfigInstance);
3528 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003529 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003530 WindowManager wm = r.activity.getWindowManager();
3531 View v = r.activity.mDecor;
3532 if (v != null) {
3533 if (r.activity.mVisibleFromServer) {
3534 mNumVisibleActivities--;
3535 }
3536 IBinder wtoken = v.getWindowToken();
3537 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003538 if (r.onlyLocalRequest) {
3539 // Hold off on removing this until the new activity's
3540 // window is being added.
3541 r.mPendingRemoveWindow = v;
3542 r.mPendingRemoveWindowManager = wm;
3543 } else {
3544 wm.removeViewImmediate(v);
3545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003546 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003547 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003548 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 r.activity.getClass().getName(), "Activity");
3550 }
3551 r.activity.mDecor = null;
3552 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003553 if (r.mPendingRemoveWindow == null) {
3554 // If we are delaying the removal of the activity window, then
3555 // we can't clean up all windows here. Note that we can't do
3556 // so later either, which means any windows that aren't closed
3557 // by the app will leak. Well we try to warning them a lot
3558 // about leaking windows, because that is a bug, so if they are
3559 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07003560 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003561 r.activity.getClass().getName(), "Activity");
3562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563
3564 // Mocked out contexts won't be participating in the normal
3565 // process lifecycle, but if we're running with a proper
3566 // ApplicationContext we need to have it tear down things
3567 // cleanly.
3568 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003569 if (c instanceof ContextImpl) {
3570 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 r.activity.getClass().getName(), "Activity");
3572 }
3573 }
3574 if (finishing) {
3575 try {
3576 ActivityManagerNative.getDefault().activityDestroyed(token);
3577 } catch (RemoteException ex) {
3578 // If the system process has died, it's game over for everyone.
3579 }
3580 }
3581 }
3582
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003583 public final void requestRelaunchActivity(IBinder token,
3584 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3585 int configChanges, boolean notResumed, Configuration config,
3586 boolean fromServer) {
3587 ActivityClientRecord target = null;
3588
Craig Mautner88c05892013-06-28 09:47:45 -07003589 synchronized (mResourcesManager) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003590 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3591 ActivityClientRecord r = mRelaunchingActivities.get(i);
3592 if (r.token == token) {
3593 target = r;
3594 if (pendingResults != null) {
3595 if (r.pendingResults != null) {
3596 r.pendingResults.addAll(pendingResults);
3597 } else {
3598 r.pendingResults = pendingResults;
3599 }
3600 }
3601 if (pendingNewIntents != null) {
3602 if (r.pendingIntents != null) {
3603 r.pendingIntents.addAll(pendingNewIntents);
3604 } else {
3605 r.pendingIntents = pendingNewIntents;
3606 }
3607 }
3608 break;
3609 }
3610 }
3611
3612 if (target == null) {
3613 target = new ActivityClientRecord();
3614 target.token = token;
3615 target.pendingResults = pendingResults;
3616 target.pendingIntents = pendingNewIntents;
3617 if (!fromServer) {
3618 ActivityClientRecord existing = mActivities.get(token);
3619 if (existing != null) {
3620 target.startsNotResumed = existing.paused;
3621 }
3622 target.onlyLocalRequest = true;
3623 }
3624 mRelaunchingActivities.add(target);
Jeff Brown9ef09972013-10-15 20:49:59 -07003625 sendMessage(H.RELAUNCH_ACTIVITY, target);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003626 }
3627
3628 if (fromServer) {
3629 target.startsNotResumed = notResumed;
3630 target.onlyLocalRequest = false;
3631 }
3632 if (config != null) {
3633 target.createdConfig = config;
3634 }
3635 target.pendingConfigChanges |= configChanges;
3636 }
3637 }
3638
Romain Guy65b345f2011-07-27 18:51:50 -07003639 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640 // If we are getting ready to gc after going to the background, well
3641 // we are back active so skip it.
3642 unscheduleGcIdler();
3643
3644 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003645 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 // First: make sure we have the most recent configuration and most
3648 // recent version of the activity, or skip it if some previous call
3649 // had taken a more recent version.
Craig Mautner88c05892013-06-28 09:47:45 -07003650 synchronized (mResourcesManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003651 int N = mRelaunchingActivities.size();
3652 IBinder token = tmp.token;
3653 tmp = null;
3654 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003655 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003656 if (r.token == token) {
3657 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003658 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 mRelaunchingActivities.remove(i);
3660 i--;
3661 N--;
3662 }
3663 }
Bob Leee5408332009-09-04 18:31:17 -07003664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003666 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 return;
3668 }
Bob Leee5408332009-09-04 18:31:17 -07003669
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003670 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3671 + tmp.token + " with configChanges=0x"
3672 + Integer.toHexString(configChanges));
3673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003674 if (mPendingConfiguration != null) {
3675 changedConfig = mPendingConfiguration;
3676 mPendingConfiguration = null;
3677 }
3678 }
Bob Leee5408332009-09-04 18:31:17 -07003679
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003680 if (tmp.createdConfig != null) {
3681 // If the activity manager is passing us its current config,
3682 // assume that is really what we want regardless of what we
3683 // may have pending.
3684 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003685 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3686 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3687 if (changedConfig == null
3688 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3689 changedConfig = tmp.createdConfig;
3690 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003691 }
3692 }
3693
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003694 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003695 + tmp.token + ": changedConfig=" + changedConfig);
3696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697 // If there was a pending configuration change, execute it first.
3698 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003699 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003700 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003701 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 }
Bob Leee5408332009-09-04 18:31:17 -07003703
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003704 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003705 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 if (r == null) {
3707 return;
3708 }
Bob Leee5408332009-09-04 18:31:17 -07003709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003711 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003712 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003713
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003714 r.activity.mChangingConfigurations = true;
3715
Dianne Hackborne2b04802010-12-09 09:24:55 -08003716 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003718 performPauseActivity(r.token, false, r.isPreHoneycomb());
3719 }
3720 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3721 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003722 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003723 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003724 }
Bob Leee5408332009-09-04 18:31:17 -07003725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 r.activity = null;
3729 r.window = null;
3730 r.hideForNow = false;
3731 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003732 // Merge any pending results and pending intents; don't just replace them
3733 if (tmp.pendingResults != null) {
3734 if (r.pendingResults == null) {
3735 r.pendingResults = tmp.pendingResults;
3736 } else {
3737 r.pendingResults.addAll(tmp.pendingResults);
3738 }
3739 }
3740 if (tmp.pendingIntents != null) {
3741 if (r.pendingIntents == null) {
3742 r.pendingIntents = tmp.pendingIntents;
3743 } else {
3744 r.pendingIntents.addAll(tmp.pendingIntents);
3745 }
3746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003748
Christopher Tateb70f3df2009-04-07 16:07:59 -07003749 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 }
3751
Romain Guy65b345f2011-07-27 18:51:50 -07003752 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003753 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003754 Bitmap thumbnail = createThumbnailBitmap(r);
3755 CharSequence description = null;
3756 try {
3757 description = r.activity.onCreateDescription();
3758 } catch (Exception e) {
3759 if (!mInstrumentation.onException(r.activity, e)) {
3760 throw new RuntimeException(
3761 "Unable to create description of activity "
3762 + r.intent.getComponent().toShortString()
3763 + ": " + e.toString(), e);
3764 }
3765 }
3766 //System.out.println("Reporting top thumbnail " + thumbnail);
3767 try {
3768 ActivityManagerNative.getDefault().reportThumbnail(
3769 token, thumbnail, description);
3770 } catch (RemoteException ex) {
3771 }
3772 }
3773
Dianne Hackborn73c14162012-09-19 15:45:06 -07003774 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003776 ArrayList<ComponentCallbacks2> callbacks
3777 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003778
Craig Mautner88c05892013-06-28 09:47:45 -07003779 synchronized (mResourcesManager) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003780 final int NAPP = mAllApplications.size();
3781 for (int i=0; i<NAPP; i++) {
Dianne Hackborn73c14162012-09-19 15:45:06 -07003782 callbacks.add(mAllApplications.get(i));
3783 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003784 final int NACT = mActivities.size();
3785 for (int i=0; i<NACT; i++) {
3786 ActivityClientRecord ar = mActivities.valueAt(i);
3787 Activity a = ar.activity;
3788 if (a != null) {
3789 Configuration thisConfig = applyConfigCompatMainThread(
3790 mCurDefaultDisplayDpi, newConfig,
3791 ar.packageInfo.getCompatibilityInfo());
3792 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
3793 // If the activity is currently resumed, its configuration
3794 // needs to change right now.
3795 callbacks.add(a);
3796 } else if (thisConfig != null) {
3797 // Otherwise, we will tell it about the change
3798 // the next time it is resumed or shown. Note that
3799 // the activity manager may, before then, decide the
3800 // activity needs to be destroyed to handle its new
3801 // configuration.
3802 if (DEBUG_CONFIGURATION) {
3803 Slog.v(TAG, "Setting activity "
3804 + ar.activityInfo.name + " newConfig=" + thisConfig);
Romain Guya998dff2012-03-23 18:58:36 -07003805 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003806 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 }
3808 }
3809 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003810 final int NSVC = mServices.size();
3811 for (int i=0; i<NSVC; i++) {
3812 callbacks.add(mServices.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003813 }
3814 }
3815 synchronized (mProviderMap) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003816 final int NPRV = mLocalProviders.size();
3817 for (int i=0; i<NPRV; i++) {
3818 callbacks.add(mLocalProviders.valueAt(i).mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003819 }
3820 }
Bob Leee5408332009-09-04 18:31:17 -07003821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822 return callbacks;
3823 }
Bob Leee5408332009-09-04 18:31:17 -07003824
Romain Guya998dff2012-03-23 18:58:36 -07003825 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003827 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003828 // we check the runtime type and act accordingly.
3829 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3830 if (activity != null) {
3831 activity.mCalled = false;
3832 }
Bob Leee5408332009-09-04 18:31:17 -07003833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 boolean shouldChangeConfig = false;
3835 if ((activity == null) || (activity.mCurrentConfig == null)) {
3836 shouldChangeConfig = true;
3837 } else {
Bob Leee5408332009-09-04 18:31:17 -07003838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 // If the new config is the same as the config this Activity
3840 // is already running with then don't bother calling
3841 // onConfigurationChanged
3842 int diff = activity.mCurrentConfig.diff(config);
3843 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 // If this activity doesn't handle any of the config changes
3845 // then don't bother calling onConfigurationChanged as we're
3846 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003847 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003848 shouldChangeConfig = true;
3849 }
3850 }
3851 }
Bob Leee5408332009-09-04 18:31:17 -07003852
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003853 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003854 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003855 if (shouldChangeConfig) {
3856 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 if (activity != null) {
3859 if (!activity.mCalled) {
3860 throw new SuperNotCalledException(
3861 "Activity " + activity.getLocalClassName() +
3862 " did not call through to super.onConfigurationChanged()");
3863 }
3864 activity.mConfigChangeFlags = 0;
3865 activity.mCurrentConfig = new Configuration(config);
3866 }
3867 }
3868 }
3869
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003870 public final void applyConfigurationToResources(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -07003871 synchronized (mResourcesManager) {
3872 mResourcesManager.applyConfigurationToResourcesLocked(config, null);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003873 }
3874 }
3875
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003876 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07003877 Configuration config = mConfiguration;
3878 if (mCompatConfiguration == null) {
3879 mCompatConfiguration = new Configuration();
3880 }
3881 mCompatConfiguration.setTo(mConfiguration);
Craig Mautner88c05892013-06-28 09:47:45 -07003882 if (mResourcesManager.applyCompatConfiguration(displayDensity, mCompatConfiguration)) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07003883 config = mCompatConfiguration;
3884 }
3885 return config;
3886 }
3887
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003888 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003889
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003890 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003891
Craig Mautner88c05892013-06-28 09:47:45 -07003892 synchronized (mResourcesManager) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003893 if (mPendingConfiguration != null) {
3894 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3895 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003896 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003897 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003898 }
3899 mPendingConfiguration = null;
3900 }
3901
3902 if (config == null) {
3903 return;
3904 }
3905
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003906 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003907 + config);
Craig Mautner88c05892013-06-28 09:47:45 -07003908
3909 mResourcesManager.applyConfigurationToResourcesLocked(config, compat);
3910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003911 if (mConfiguration == null) {
3912 mConfiguration = new Configuration();
3913 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003914 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003915 return;
3916 }
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003917 configDiff = mConfiguration.diff(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003918 mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003919 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07003921
3922 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
3923
Romain Guy65b345f2011-07-27 18:51:50 -07003924 // Cleanup hardware accelerated stuff
Jeff Brown98365d72012-08-19 20:30:52 -07003925 WindowManagerGlobal.getInstance().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003926
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003927 freeTextLayoutCachesIfNeeded(configDiff);
3928
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003929 if (callbacks != null) {
3930 final int N = callbacks.size();
3931 for (int i=0; i<N; i++) {
3932 performConfigurationChanged(callbacks.get(i), config);
3933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003934 }
3935 }
3936
Romain Guy46bfc482013-08-16 18:38:29 -07003937 static void freeTextLayoutCachesIfNeeded(int configDiff) {
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003938 if (configDiff != 0) {
3939 // Ask text layout engine to free its caches if there is a locale change
3940 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
3941 if (hasLocaleConfigChange) {
3942 Canvas.freeTextLayoutCaches();
3943 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
3944 }
3945 }
3946 }
3947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003949 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003950 if (r == null || r.activity == null) {
3951 return;
3952 }
Bob Leee5408332009-09-04 18:31:17 -07003953
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003954 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003955 + r.activityInfo.name);
3956
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003957 performConfigurationChanged(r.activity, mCompatConfiguration);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003958
3959 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 }
3961
Romain Guy7eabe552011-07-21 14:56:34 -07003962 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003963 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003964 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003965 switch (profileType) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003966 default:
3967 mProfiler.setProfiler(pcd.path, pcd.fd);
3968 mProfiler.autoStopProfiler = false;
3969 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003970 break;
3971 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003972 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003973 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003974 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003975 } finally {
3976 try {
3977 pcd.fd.close();
3978 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003979 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003980 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003981 }
3982 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003983 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07003984 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003985 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003986 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003987 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003988 }
3989 }
Bob Leee5408332009-09-04 18:31:17 -07003990
Romain Guya998dff2012-03-23 18:58:36 -07003991 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07003992 if (managed) {
3993 try {
3994 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3995 } catch (IOException e) {
3996 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3997 + " -- can the process access this path?");
3998 } finally {
3999 try {
4000 dhd.fd.close();
4001 } catch (IOException e) {
4002 Slog.w(TAG, "Failure closing profile fd", e);
4003 }
4004 }
4005 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07004006 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07004007 }
4008 }
4009
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004010 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
4011 boolean hasPkgInfo = false;
4012 if (packages != null) {
4013 for (int i=packages.length-1; i>=0; i--) {
4014 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
4015 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004016 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004017 ref = mPackages.get(packages[i]);
4018 if (ref != null && ref.get() != null) {
4019 hasPkgInfo = true;
4020 } else {
4021 ref = mResourcePackages.get(packages[i]);
4022 if (ref != null && ref.get() != null) {
4023 hasPkgInfo = true;
4024 }
4025 }
4026 }
4027 mPackages.remove(packages[i]);
4028 mResourcePackages.remove(packages[i]);
4029 }
4030 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08004031 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004032 hasPkgInfo);
4033 }
4034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004035 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004036 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07004037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004038 final int N = callbacks.size();
4039 for (int i=0; i<N; i++) {
4040 callbacks.get(i).onLowMemory();
4041 }
4042
Chris Tatece229052009-03-25 16:44:52 -07004043 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
4044 if (Process.myUid() != Process.SYSTEM_UID) {
4045 int sqliteReleased = SQLiteDatabase.releaseMemory();
4046 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
4047 }
Bob Leee5408332009-09-04 18:31:17 -07004048
Mike Reedcaf0df12009-04-27 14:32:05 -04004049 // Ask graphics to free up as much as possible (font/image caches)
4050 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004051
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004052 // Ask text layout engine to free also as much as possible
4053 Canvas.freeTextLayoutCaches();
4054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 BinderInternal.forceGc("mem");
4056 }
4057
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004058 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004059 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004060
Jeff Brown98365d72012-08-19 20:30:52 -07004061 final WindowManagerGlobal windowManager = WindowManagerGlobal.getInstance();
Romain Guy19f86e82012-04-23 15:19:07 -07004062 windowManager.startTrimMemory(level);
4063
Dianne Hackborn73c14162012-09-19 15:45:06 -07004064 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004065
4066 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07004067 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004068 callbacks.get(i).onTrimMemory(level);
4069 }
Romain Guy19f86e82012-04-23 15:19:07 -07004070
Jeff Brown98365d72012-08-19 20:30:52 -07004071 windowManager.endTrimMemory();
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004072 }
4073
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004074 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004075 if (Process.isIsolated()) {
4076 // Isolated processes aren't going to do UI.
4077 return;
4078 }
Romain Guya9582652011-11-10 14:20:10 -08004079 try {
4080 int uid = Process.myUid();
4081 String[] packages = getPackageManager().getPackagesForUid(uid);
4082
4083 // If there are several packages in this application we won't
4084 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004085 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004086 HardwareRenderer.setupDiskCache(cacheDir);
4087 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08004088 }
4089 } catch (RemoteException e) {
4090 // Ignore
4091 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07004092 }
4093
4094 private void updateDefaultDensity() {
4095 if (mCurDefaultDisplayDpi != Configuration.DENSITY_DPI_UNDEFINED
4096 && mCurDefaultDisplayDpi != DisplayMetrics.DENSITY_DEVICE
4097 && !mDensityCompatMode) {
4098 Slog.i(TAG, "Switching default density from "
4099 + DisplayMetrics.DENSITY_DEVICE + " to "
4100 + mCurDefaultDisplayDpi);
4101 DisplayMetrics.DENSITY_DEVICE = mCurDefaultDisplayDpi;
4102 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4103 }
4104 }
4105
Romain Guy65b345f2011-07-27 18:51:50 -07004106 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 mBoundApplication = data;
4108 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004109 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004111 mProfiler = new Profiler();
4112 mProfiler.profileFile = data.initProfileFile;
4113 mProfiler.profileFd = data.initProfileFd;
4114 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
4115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08004117 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004118 android.ddm.DdmHandleAppName.setAppName(data.processName,
4119 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004120
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004121 if (data.persistent) {
4122 // Persistent processes on low-memory devices do not get to
4123 // use hardware accelerated drawing, since this can add too much
4124 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07004125 if (!ActivityManager.isHighEndGfx()) {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004126 HardwareRenderer.disable(false);
4127 }
4128 }
Romain Guya9582652011-11-10 14:20:10 -08004129
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004130 if (mProfiler.profileFd != null) {
4131 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004132 }
4133
Joe Onoratod630f102011-03-17 18:42:26 -07004134 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
4135 // implementation to use the pool executor. Normally, we use the
4136 // serialized executor as the default. This has to happen in the
4137 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07004138 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07004139 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
4140 }
4141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 /*
4143 * Before spawning a new process, reset the time zone to be the system time zone.
4144 * This needs to be done because the system time zone could have changed after the
4145 * the spawning of this process. Without doing this this process would have the incorrect
4146 * system time zone.
4147 */
4148 TimeZone.setDefault(null);
4149
4150 /*
4151 * Initialize the default locale in this process for the reasons we set the time zone.
4152 */
4153 Locale.setDefault(data.config.locale);
4154
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004155 /*
4156 * Update the system configuration since its preloaded and might not
4157 * reflect configuration changes. The configuration object passed
4158 * in AppBindData can be safely assumed to be up to date
4159 */
Craig Mautner88c05892013-06-28 09:47:45 -07004160 mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004161 mCurDefaultDisplayDpi = data.config.densityDpi;
4162 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004163
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004164 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165
Dianne Hackborndde331c2012-08-03 14:01:57 -07004166 /**
4167 * Switch this process to density compatibility mode if needed.
4168 */
4169 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4170 == 0) {
4171 mDensityCompatMode = true;
4172 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4173 }
4174 updateDefaultDensity();
4175
Jeff Brown08988602014-03-10 21:24:37 -07004176 final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
Amith Yamasani11de39a2012-08-17 18:00:52 -07004177 if (!Process.isIsolated()) {
4178 final File cacheDir = appContext.getCacheDir();
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004179
Amith Yamasani92d57052012-08-23 10:07:52 -07004180 if (cacheDir != null) {
4181 // Provide a usable directory for temporary files
4182 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
4183
4184 setupGraphicsSupport(data.info, cacheDir);
4185 } else {
4186 Log.e(TAG, "Unable to setupGraphicsSupport due to missing cache directory");
4187 }
Amith Yamasani11de39a2012-08-17 18:00:52 -07004188 }
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004189 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004190 * For system applications on userdebug/eng builds, log stack
4191 * traces of disk and network access to dropbox for analysis.
4192 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004193 if ((data.appInfo.flags &
4194 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004195 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4196 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004197 }
4198
4199 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004200 * For apps targetting SDK Honeycomb or later, we don't allow
4201 * network usage on the main event loop / UI thread.
4202 *
4203 * Note to those grepping: this is what ultimately throws
4204 * NetworkOnMainThreadException ...
4205 */
4206 if (data.appInfo.targetSdkVersion > 9) {
4207 StrictMode.enableDeathOnNetwork();
4208 }
4209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4211 // XXX should have option to change the port.
4212 Debug.changeDebugPort(8100);
4213 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004214 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004215 + " is waiting for the debugger on port 8100...");
4216
4217 IActivityManager mgr = ActivityManagerNative.getDefault();
4218 try {
4219 mgr.showWaitingForDebugger(mAppThread, true);
4220 } catch (RemoteException ex) {
4221 }
4222
4223 Debug.waitForDebugger();
4224
4225 try {
4226 mgr.showWaitingForDebugger(mAppThread, false);
4227 } catch (RemoteException ex) {
4228 }
4229
4230 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004231 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232 + " can be debugged on port 8100...");
4233 }
4234 }
4235
Siva Velusamy92a8b222012-03-09 16:24:04 -08004236 // Enable OpenGL tracing if required
4237 if (data.enableOpenGlTrace) {
Siva Velusamy0c1761b2012-12-14 17:09:06 -08004238 GLUtils.setTracingLevel(1);
Siva Velusamy92a8b222012-03-09 16:24:04 -08004239 }
4240
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07004241 // Allow application-generated systrace messages if we're debuggable.
4242 boolean appTracingAllowed = (data.appInfo.flags&ApplicationInfo.FLAG_DEBUGGABLE) != 0;
4243 Trace.setAppTracingAllowed(appTracingAllowed);
4244
Robert Greenwalt434203a2010-10-11 16:00:27 -07004245 /**
4246 * Initialize the default http proxy in this process for the reasons we set the time zone.
4247 */
4248 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004249 if (b != null) {
4250 // In pre-boot mode (doing initial launch to collect password), not
4251 // all system is up. This includes the connectivity service, so don't
4252 // crash if we can't get it.
4253 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4254 try {
4255 ProxyProperties proxyProperties = service.getProxy();
4256 Proxy.setHttpProxySystemProperty(proxyProperties);
4257 } catch (RemoteException e) {}
4258 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004261 InstrumentationInfo ii = null;
4262 try {
4263 ii = appContext.getPackageManager().
4264 getInstrumentationInfo(data.instrumentationName, 0);
4265 } catch (PackageManager.NameNotFoundException e) {
4266 }
4267 if (ii == null) {
4268 throw new RuntimeException(
4269 "Unable to find instrumentation info for: "
4270 + data.instrumentationName);
4271 }
4272
4273 mInstrumentationAppDir = ii.sourceDir;
Brian Carlstromd893a892012-04-01 21:30:26 -07004274 mInstrumentationAppLibraryDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004275 mInstrumentationAppPackage = ii.packageName;
4276 mInstrumentedAppDir = data.info.getAppDir();
Brian Carlstromd893a892012-04-01 21:30:26 -07004277 mInstrumentedAppLibraryDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004278
4279 ApplicationInfo instrApp = new ApplicationInfo();
4280 instrApp.packageName = ii.packageName;
4281 instrApp.sourceDir = ii.sourceDir;
4282 instrApp.publicSourceDir = ii.publicSourceDir;
4283 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004284 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004285 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 appContext.getClassLoader(), false, true);
Jeff Brown08988602014-03-10 21:24:37 -07004287 ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288
4289 try {
4290 java.lang.ClassLoader cl = instrContext.getClassLoader();
4291 mInstrumentation = (Instrumentation)
4292 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4293 } catch (Exception e) {
4294 throw new RuntimeException(
4295 "Unable to instantiate instrumentation "
4296 + data.instrumentationName + ": " + e.toString(), e);
4297 }
4298
4299 mInstrumentation.init(this, instrContext, appContext,
Svetoslav Ganov80943d82013-01-02 10:25:37 -08004300 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher,
4301 data.instrumentationUiAutomationConnection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004302
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004303 if (mProfiler.profileFile != null && !ii.handleProfiling
4304 && mProfiler.profileFd == null) {
4305 mProfiler.handlingProfiling = true;
4306 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 file.getParentFile().mkdirs();
4308 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4309 }
4310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311 } else {
4312 mInstrumentation = new Instrumentation();
4313 }
4314
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004315 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004316 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004317 }
4318
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004319 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004320 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004321 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004322 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004324 // If the app is being launched for full backup or restore, bring it up in
4325 // a restricted environment with the base application class.
4326 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4327 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004328
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004329 // don't bring up providers in restricted mode; they may depend on the
4330 // app's custom Application class
4331 if (!data.restrictedBackupMode) {
4332 List<ProviderInfo> providers = data.providers;
4333 if (providers != null) {
4334 installContentProviders(app, providers);
4335 // For process that contains content providers, we want to
4336 // ensure that the JIT is enabled "at some point".
4337 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4338 }
4339 }
4340
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004341 // Do this after providers, since instrumentation tests generally start their
4342 // test thread at this point, and we don't want that racing.
4343 try {
4344 mInstrumentation.onCreate(data.instrumentationArgs);
4345 }
4346 catch (Exception e) {
4347 throw new RuntimeException(
4348 "Exception thrown in onCreate() of "
4349 + data.instrumentationName + ": " + e.toString(), e);
4350 }
4351
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004352 try {
4353 mInstrumentation.callApplicationOnCreate(app);
4354 } catch (Exception e) {
4355 if (!mInstrumentation.onException(app, e)) {
4356 throw new RuntimeException(
4357 "Unable to create application " + app.getClass().getName()
4358 + ": " + e.toString(), e);
4359 }
4360 }
4361 } finally {
4362 StrictMode.setThreadPolicy(savedPolicy);
4363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004364 }
4365
4366 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4367 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004368 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4369 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004370 Debug.stopMethodTracing();
4371 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004372 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004373 // + ", app thr: " + mAppThread);
4374 try {
4375 am.finishInstrumentation(mAppThread, resultCode, results);
4376 } catch (RemoteException ex) {
4377 }
4378 }
4379
Romain Guy65b345f2011-07-27 18:51:50 -07004380 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 Context context, List<ProviderInfo> providers) {
4382 final ArrayList<IActivityManager.ContentProviderHolder> results =
4383 new ArrayList<IActivityManager.ContentProviderHolder>();
4384
Romain Guya998dff2012-03-23 18:58:36 -07004385 for (ProviderInfo cpi : providers) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08004386 if (DEBUG_PROVIDER) {
4387 StringBuilder buf = new StringBuilder(128);
4388 buf.append("Pub ");
4389 buf.append(cpi.authority);
4390 buf.append(": ");
4391 buf.append(cpi.name);
4392 Log.i(TAG, buf.toString());
4393 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004394 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
4395 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
4396 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004397 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004398 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 }
4400 }
4401
4402 try {
4403 ActivityManagerNative.getDefault().publishContentProviders(
4404 getApplicationThread(), results);
4405 } catch (RemoteException ex) {
4406 }
4407 }
4408
Jeff Sharkey6d515712012-09-20 16:06:08 -07004409 public final IContentProvider acquireProvider(
4410 Context c, String auth, int userId, boolean stable) {
4411 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004412 if (provider != null) {
4413 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 }
4415
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004416 // There is a possible race here. Another thread may try to acquire
4417 // the same provider at the same time. When this happens, we want to ensure
4418 // that the first one wins.
4419 // Note that we cannot hold the lock while acquiring and installing the
4420 // provider since it might take a long time to run and it could also potentially
4421 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004422 IActivityManager.ContentProviderHolder holder = null;
4423 try {
4424 holder = ActivityManagerNative.getDefault().getContentProvider(
Jeff Sharkey6d515712012-09-20 16:06:08 -07004425 getApplicationThread(), auth, userId, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004426 } catch (RemoteException ex) {
4427 }
4428 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004429 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004430 return null;
4431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004432
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004433 // Install provider will increment the reference count for us, and break
4434 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004435 holder = installProvider(c, holder, holder.info,
4436 true /*noisy*/, holder.noReleaseNeeded, stable);
4437 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 }
4439
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004440 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
4441 if (stable) {
4442 prc.stableCount += 1;
4443 if (prc.stableCount == 1) {
4444 // We are acquiring a new stable reference on the provider.
4445 int unstableDelta;
4446 if (prc.removePending) {
4447 // We have a pending remove operation, which is holding the
4448 // last unstable reference. At this point we are converting
4449 // that unstable reference to our new stable reference.
4450 unstableDelta = -1;
4451 // Cancel the removal of the provider.
4452 if (DEBUG_PROVIDER) {
4453 Slog.v(TAG, "incProviderRef: stable "
4454 + "snatched provider from the jaws of death");
4455 }
4456 prc.removePending = false;
Guobin Zhang9e3e52662013-03-21 13:57:11 +08004457 // There is a race! It fails to remove the message, which
4458 // will be handled in completeRemoveProvider().
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004459 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4460 } else {
4461 unstableDelta = 0;
4462 }
4463 try {
4464 if (DEBUG_PROVIDER) {
4465 Slog.v(TAG, "incProviderRef Now stable - "
4466 + prc.holder.info.name + ": unstableDelta="
4467 + unstableDelta);
4468 }
4469 ActivityManagerNative.getDefault().refContentProvider(
4470 prc.holder.connection, 1, unstableDelta);
4471 } catch (RemoteException e) {
4472 //do nothing content provider object is dead any way
4473 }
4474 }
4475 } else {
4476 prc.unstableCount += 1;
4477 if (prc.unstableCount == 1) {
4478 // We are acquiring a new unstable reference on the provider.
4479 if (prc.removePending) {
4480 // Oh look, we actually have a remove pending for the
4481 // provider, which is still holding the last unstable
4482 // reference. We just need to cancel that to take new
4483 // ownership of the reference.
4484 if (DEBUG_PROVIDER) {
4485 Slog.v(TAG, "incProviderRef: unstable "
4486 + "snatched provider from the jaws of death");
4487 }
4488 prc.removePending = false;
4489 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4490 } else {
4491 // First unstable ref, increment our count in the
4492 // activity manager.
4493 try {
4494 if (DEBUG_PROVIDER) {
4495 Slog.v(TAG, "incProviderRef: Now unstable - "
4496 + prc.holder.info.name);
4497 }
4498 ActivityManagerNative.getDefault().refContentProvider(
4499 prc.holder.connection, 0, 1);
4500 } catch (RemoteException e) {
4501 //do nothing content provider object is dead any way
4502 }
4503 }
4504 }
4505 }
4506 }
4507
Jeff Sharkey6d515712012-09-20 16:06:08 -07004508 public final IContentProvider acquireExistingProvider(
4509 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004510 synchronized (mProviderMap) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004511 final ProviderKey key = new ProviderKey(auth, userId);
4512 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004513 if (pr == null) {
4514 return null;
4515 }
4516
4517 IContentProvider provider = pr.mProvider;
4518 IBinder jBinder = provider.asBinder();
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004519 if (!jBinder.isBinderAlive()) {
4520 // The hosting process of the provider has died; we can't
4521 // use this one.
4522 Log.i(TAG, "Acquiring provider " + auth + " for user " + userId
4523 + ": existing object's process dead");
4524 handleUnstableProviderDiedLocked(jBinder, true);
4525 return null;
4526 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004527
4528 // Only increment the ref count if we have one. If we don't then the
4529 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004530 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004531 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004532 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004533 }
4534 return provider;
4535 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004536 }
4537
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004538 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
4539 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004540 return false;
4541 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004543 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004544 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004545 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004546 if (prc == null) {
4547 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004548 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004549 }
4550
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004551 boolean lastRef = false;
4552 if (stable) {
4553 if (prc.stableCount == 0) {
4554 if (DEBUG_PROVIDER) Slog.v(TAG,
4555 "releaseProvider: stable ref count already 0, how?");
4556 return false;
4557 }
4558 prc.stableCount -= 1;
4559 if (prc.stableCount == 0) {
4560 // What we do at this point depends on whether there are
4561 // any unstable refs left: if there are, we just tell the
4562 // activity manager to decrement its stable count; if there
4563 // aren't, we need to enqueue this provider to be removed,
4564 // and convert to holding a single unstable ref while
4565 // doing so.
4566 lastRef = prc.unstableCount == 0;
4567 try {
4568 if (DEBUG_PROVIDER) {
4569 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
4570 + lastRef + " - " + prc.holder.info.name);
4571 }
4572 ActivityManagerNative.getDefault().refContentProvider(
4573 prc.holder.connection, -1, lastRef ? 1 : 0);
4574 } catch (RemoteException e) {
4575 //do nothing content provider object is dead any way
4576 }
4577 }
4578 } else {
4579 if (prc.unstableCount == 0) {
4580 if (DEBUG_PROVIDER) Slog.v(TAG,
4581 "releaseProvider: unstable ref count already 0, how?");
4582 return false;
4583 }
4584 prc.unstableCount -= 1;
4585 if (prc.unstableCount == 0) {
4586 // If this is the last reference, we need to enqueue
4587 // this provider to be removed instead of telling the
4588 // activity manager to remove it at this point.
4589 lastRef = prc.stableCount == 0;
4590 if (!lastRef) {
4591 try {
4592 if (DEBUG_PROVIDER) {
4593 Slog.v(TAG, "releaseProvider: No longer unstable - "
4594 + prc.holder.info.name);
4595 }
4596 ActivityManagerNative.getDefault().refContentProvider(
4597 prc.holder.connection, 0, -1);
4598 } catch (RemoteException e) {
4599 //do nothing content provider object is dead any way
4600 }
4601 }
4602 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004603 }
4604
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004605 if (lastRef) {
4606 if (!prc.removePending) {
4607 // Schedule the actual remove asynchronously, since we don't know the context
4608 // this will be called in.
4609 // TODO: it would be nice to post a delayed message, so
4610 // if we come back and need the same provider quickly
4611 // we will still have it available.
4612 if (DEBUG_PROVIDER) {
4613 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
4614 + prc.holder.info.name);
4615 }
4616 prc.removePending = true;
4617 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
4618 mH.sendMessage(msg);
4619 } else {
4620 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
4621 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004622 }
4623 return true;
4624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004625 }
4626
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004627 final void completeRemoveProvider(ProviderRefCount prc) {
4628 synchronized (mProviderMap) {
4629 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004630 // There was a race! Some other client managed to acquire
4631 // the provider before the removal was completed.
4632 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004633 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004634 + "provider still in use");
4635 return;
4636 }
4637
Guobin Zhang9e3e52662013-03-21 13:57:11 +08004638 // More complicated race!! Some client managed to acquire the
4639 // provider and release it before the removal was completed.
4640 // Continue the removal, and abort the next remove message.
4641 prc.removePending = false;
4642
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004643 final IBinder jBinder = prc.holder.provider.asBinder();
4644 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
4645 if (existingPrc == prc) {
4646 mProviderRefCountMap.remove(jBinder);
4647 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004648
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004649 for (int i=mProviderMap.size()-1; i>=0; i--) {
4650 ProviderClientRecord pr = mProviderMap.valueAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004651 IBinder myBinder = pr.mProvider.asBinder();
4652 if (myBinder == jBinder) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004653 mProviderMap.removeAt(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004654 }
4655 }
4656 }
4657
4658 try {
4659 if (DEBUG_PROVIDER) {
4660 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4661 + "removeContentProvider(" + prc.holder.info.name + ")");
4662 }
4663 ActivityManagerNative.getDefault().removeContentProvider(
4664 prc.holder.connection, false);
4665 } catch (RemoteException e) {
4666 //do nothing content provider object is dead any way
4667 }
4668 }
4669
4670 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004671 synchronized (mProviderMap) {
4672 handleUnstableProviderDiedLocked(provider, fromClient);
4673 }
4674 }
4675
4676 final void handleUnstableProviderDiedLocked(IBinder provider, boolean fromClient) {
4677 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4678 if (prc != null) {
4679 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
4680 + provider + " " + prc.holder.info.name);
4681 mProviderRefCountMap.remove(provider);
You Kimbc74de62013-10-01 00:13:26 +09004682 for (int i=mProviderMap.size()-1; i>=0; i--) {
4683 ProviderClientRecord pr = mProviderMap.valueAt(i);
4684 if (pr != null && pr.mProvider.asBinder() == provider) {
4685 Slog.i(TAG, "Removing dead content provider:" + pr.mProvider.toString());
4686 mProviderMap.removeAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004687 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004688 }
You Kimbc74de62013-10-01 00:13:26 +09004689
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004690 if (fromClient) {
4691 // We found out about this due to execution in our client
4692 // code. Tell the activity manager about it now, to ensure
4693 // that the next time we go to do anything with the provider
4694 // it knows it is dead (so we don't race with its death
4695 // notification).
4696 try {
4697 ActivityManagerNative.getDefault().unstableProviderDied(
4698 prc.holder.connection);
4699 } catch (RemoteException e) {
4700 //do nothing content provider object is dead any way
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004701 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004702 }
4703 }
4704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004705
Jeff Sharkey7aa76012013-09-30 14:26:27 -07004706 final void appNotRespondingViaProvider(IBinder provider) {
4707 synchronized (mProviderMap) {
4708 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4709 if (prc != null) {
4710 try {
4711 ActivityManagerNative.getDefault()
4712 .appNotRespondingViaProvider(prc.holder.connection);
4713 } catch (RemoteException e) {
4714 }
4715 }
4716 }
4717 }
4718
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004719 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Jeff Sharkey6d515712012-09-20 16:06:08 -07004720 ContentProvider localProvider, IActivityManager.ContentProviderHolder holder) {
4721 final String auths[] = PATTERN_SEMICOLON.split(holder.info.authority);
4722 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
4723
4724 final ProviderClientRecord pcr = new ProviderClientRecord(
4725 auths, provider, localProvider, holder);
4726 for (String auth : auths) {
4727 final ProviderKey key = new ProviderKey(auth, userId);
4728 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004729 if (existing != null) {
4730 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07004731 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004732 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004733 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004734 }
4735 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004736 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004737 }
4738
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004739 /**
4740 * Installs the provider.
4741 *
4742 * Providers that are local to the process or that come from the system server
4743 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4744 * Other remote providers are reference counted. The initial reference count
4745 * for all reference counted providers is one. Providers that are not reference
4746 * counted do not have a reference count (at all).
4747 *
4748 * This method detects when a provider has already been installed. When this happens,
4749 * it increments the reference count of the existing provider (if appropriate)
4750 * and returns the existing provider. This can happen due to concurrent
4751 * attempts to acquire the same provider.
4752 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004753 private IActivityManager.ContentProviderHolder installProvider(Context context,
4754 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
4755 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004756 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004757 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07004758 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004759 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004760 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004761 + info.name);
4762 }
4763 Context c = null;
4764 ApplicationInfo ai = info.applicationInfo;
4765 if (context.getPackageName().equals(ai.packageName)) {
4766 c = context;
4767 } else if (mInitialApplication != null &&
4768 mInitialApplication.getPackageName().equals(ai.packageName)) {
4769 c = mInitialApplication;
4770 } else {
4771 try {
4772 c = context.createPackageContext(ai.packageName,
4773 Context.CONTEXT_INCLUDE_CODE);
4774 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004775 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004776 }
4777 }
4778 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004779 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004780 ai.packageName +
4781 " while loading content provider " +
4782 info.name);
4783 return null;
4784 }
4785 try {
4786 final java.lang.ClassLoader cl = c.getClassLoader();
4787 localProvider = (ContentProvider)cl.
4788 loadClass(info.name).newInstance();
4789 provider = localProvider.getIContentProvider();
4790 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004791 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004792 info.name + " from sourceDir " +
4793 info.applicationInfo.sourceDir);
4794 return null;
4795 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004796 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004797 TAG, "Instantiating local provider " + info.name);
4798 // XXX Need to create the correct context for this provider.
4799 localProvider.attachInfo(c, info);
4800 } catch (java.lang.Exception e) {
4801 if (!mInstrumentation.onException(null, e)) {
4802 throw new RuntimeException(
4803 "Unable to get provider " + info.name
4804 + ": " + e.toString(), e);
4805 }
4806 return null;
4807 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004808 } else {
4809 provider = holder.provider;
4810 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004811 + info.name);
4812 }
4813
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004814 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004815
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004816 synchronized (mProviderMap) {
4817 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
4818 + " / " + info.name);
4819 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004820 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004821 ComponentName cname = new ComponentName(info.packageName, info.name);
4822 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004823 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004824 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004825 Slog.v(TAG, "installProvider: lost the race, "
4826 + "using existing local provider");
4827 }
4828 provider = pr.mProvider;
4829 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004830 holder = new IActivityManager.ContentProviderHolder(info);
4831 holder.provider = provider;
4832 holder.noReleaseNeeded = true;
4833 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004834 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004835 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004836 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004837 retHolder = pr.mHolder;
4838 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004839 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4840 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004841 if (DEBUG_PROVIDER) {
4842 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004843 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004844 // We need to transfer our new reference to the existing
4845 // ref count, releasing the old one... but only if
4846 // release is needed (that is, it is not running in the
4847 // system process).
4848 if (!noReleaseNeeded) {
4849 incProviderRefLocked(prc, stable);
4850 try {
4851 ActivityManagerNative.getDefault().removeContentProvider(
4852 holder.connection, stable);
4853 } catch (RemoteException e) {
4854 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004855 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004856 }
4857 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004858 ProviderClientRecord client = installProviderAuthoritiesLocked(
4859 provider, localProvider, holder);
4860 if (noReleaseNeeded) {
4861 prc = new ProviderRefCount(holder, client, 1000, 1000);
4862 } else {
4863 prc = stable
4864 ? new ProviderRefCount(holder, client, 1, 0)
4865 : new ProviderRefCount(holder, client, 0, 1);
4866 }
4867 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004868 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004869 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004870 }
4871 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004872
4873 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004874 }
4875
Romain Guy65b345f2011-07-27 18:51:50 -07004876 private void attach(boolean system) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08004877 sCurrentActivityThread = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004879 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004880 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Craig Mautner88c05892013-06-28 09:47:45 -07004881 @Override
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004882 public void run() {
4883 ensureJitEnabled();
4884 }
4885 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004886 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
4887 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004888 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4889 IActivityManager mgr = ActivityManagerNative.getDefault();
4890 try {
4891 mgr.attachApplication(mAppThread);
4892 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004893 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004894 }
4895 } else {
4896 // Don't set application object here -- if the system crashes,
4897 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004898 android.ddm.DdmHandleAppName.setAppName("system_process",
4899 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004900 try {
4901 mInstrumentation = new Instrumentation();
Jeff Brown08988602014-03-10 21:24:37 -07004902 ContextImpl context = ContextImpl.createAppContext(
4903 this, getSystemContext().mPackageInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004904 Application app = Instrumentation.newApplication(Application.class, context);
4905 mAllApplications.add(app);
4906 mInitialApplication = app;
4907 app.onCreate();
4908 } catch (Exception e) {
4909 throw new RuntimeException(
4910 "Unable to instantiate Application():" + e.toString(), e);
4911 }
4912 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07004913
4914 // add dropbox logging to libcore
4915 DropBox.setReporter(new DropBoxReporter());
4916
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004917 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Craig Mautner88c05892013-06-28 09:47:45 -07004918 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004919 public void onConfigurationChanged(Configuration newConfig) {
Craig Mautner88c05892013-06-28 09:47:45 -07004920 synchronized (mResourcesManager) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004921 // We need to apply this change to the resources
4922 // immediately, because upon returning the view
4923 // hierarchy will be informed about it.
Craig Mautner88c05892013-06-28 09:47:45 -07004924 if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004925 // This actually changed the resources! Tell
4926 // everyone about it.
4927 if (mPendingConfiguration == null ||
4928 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4929 mPendingConfiguration = newConfig;
4930
Jeff Brown9ef09972013-10-15 20:49:59 -07004931 sendMessage(H.CONFIGURATION_CHANGED, newConfig);
Dianne Hackbornae078162010-03-18 11:29:37 -07004932 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004933 }
4934 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004935 }
Craig Mautner88c05892013-06-28 09:47:45 -07004936 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004937 public void onLowMemory() {
4938 }
Craig Mautner88c05892013-06-28 09:47:45 -07004939 @Override
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004940 public void onTrimMemory(int level) {
4941 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004942 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004943 }
4944
Romain Guy5e9120d2012-01-30 12:17:22 -08004945 public static ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004946 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 ActivityThread thread = new ActivityThread();
4948 thread.attach(true);
4949 return thread;
4950 }
4951
Jeff Brown10e89712011-07-08 18:52:57 -07004952 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004953 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004954 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 }
4956 }
4957
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004958 public int getIntCoreSetting(String key, int defaultValue) {
Craig Mautner88c05892013-06-28 09:47:45 -07004959 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004960 if (mCoreSettings != null) {
4961 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004962 }
Craig Mautner88c05892013-06-28 09:47:45 -07004963 return defaultValue;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004964 }
4965 }
4966
Geremy Condra69689a72012-09-11 16:57:17 -07004967 private static class EventLoggingReporter implements EventLogger.Reporter {
4968 @Override
4969 public void report (int code, Object... list) {
4970 EventLog.writeEvent(code, list);
4971 }
4972 }
4973
Geremy Condrab7faaf42012-09-19 18:07:42 -07004974 private class DropBoxReporter implements DropBox.Reporter {
4975
4976 private DropBoxManager dropBox;
4977
4978 public DropBoxReporter() {
4979 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
4980 }
4981
4982 @Override
4983 public void addData(String tag, byte[] data, int flags) {
4984 dropBox.addData(tag, data, flags);
4985 }
4986
4987 @Override
4988 public void addText(String tag, String data) {
4989 dropBox.addText(tag, data);
4990 }
4991 }
4992
Romain Guy65b345f2011-07-27 18:51:50 -07004993 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004994 SamplingProfilerIntegration.start();
4995
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004996 // CloseGuard defaults to true and can be quite spammy. We
4997 // disable it here, but selectively enable it later (via
4998 // StrictMode) on debug builds, but using DropBox, not logs.
4999 CloseGuard.setEnabled(false);
5000
Jeff Sharkeyb049e212012-09-07 23:16:01 -07005001 Environment.initForCurrentUser();
5002
Geremy Condra69689a72012-09-11 16:57:17 -07005003 // Set the reporter for event logging in libcore
5004 EventLogger.setReporter(new EventLoggingReporter());
5005
Kenny Root8b514752013-02-04 09:35:16 -08005006 Security.addProvider(new AndroidKeyStoreProvider());
5007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005008 Process.setArgV0("<pre-initialized>");
5009
5010 Looper.prepareMainLooper();
5011
5012 ActivityThread thread = new ActivityThread();
5013 thread.attach(false);
5014
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07005015 if (sMainThreadHandler == null) {
5016 sMainThreadHandler = thread.getHandler();
5017 }
5018
Romain Guy5e9120d2012-01-30 12:17:22 -08005019 AsyncTask.init();
5020
Dianne Hackborn287952c2010-09-22 22:34:31 -07005021 if (false) {
5022 Looper.myLooper().setMessageLogging(new
5023 LogPrinter(Log.DEBUG, "ActivityThread"));
5024 }
5025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005026 Looper.loop();
5027
Jeff Brown10e89712011-07-08 18:52:57 -07005028 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005029 }
5030}