blob: b8e16c5a7cc9371010f79f554b8eb270c3481735 [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;
32import android.content.pm.PackageManager;
Sen Hubde75702010-05-28 01:54:03 -070033import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.pm.ProviderInfo;
35import android.content.pm.ServiceInfo;
36import android.content.res.AssetManager;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -070037import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.res.Configuration;
39import android.content.res.Resources;
40import android.database.sqlite.SQLiteDatabase;
41import android.database.sqlite.SQLiteDebug;
Vasu Noric3849202010-03-09 10:47:25 -080042import android.database.sqlite.SQLiteDebug.DbStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.graphics.Bitmap;
44import android.graphics.Canvas;
Jeff Brown98365d72012-08-19 20:30:52 -070045import android.hardware.display.DisplayManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070046import android.net.IConnectivityManager;
47import android.net.Proxy;
48import android.net.ProxyProperties;
Romain Guya9582652011-11-10 14:20:10 -080049import android.opengl.GLUtils;
Joe Onoratod630f102011-03-17 18:42:26 -070050import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070051import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Bundle;
53import android.os.Debug;
54import android.os.Handler;
55import android.os.IBinder;
56import android.os.Looper;
57import android.os.Message;
58import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070059import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.Process;
61import android.os.RemoteException;
62import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070063import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.os.SystemClock;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070065import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070066import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.util.AndroidRuntimeException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.util.DisplayMetrics;
69import android.util.EventLog;
70import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070071import android.util.LogPrinter;
Jeff Brown6754ba22011-12-14 20:20:01 -080072import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080073import android.util.Slog;
Jeff Brownd32460c2012-07-20 16:15:36 -070074import android.view.CompatibilityInfoHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070076import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.view.View;
78import android.view.ViewDebug;
79import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070080import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.view.Window;
82import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -070083import android.view.WindowManagerGlobal;
Jason Samsa6f338c2012-02-24 16:22:16 -080084import android.renderscript.RenderScript;
Kenny Roote29df162012-08-10 08:28:37 -070085import android.security.AndroidKeyStoreProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87import com.android.internal.os.BinderInternal;
88import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070089import com.android.internal.os.SamplingProfilerIntegration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl;
92
93import java.io.File;
94import java.io.FileDescriptor;
95import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070096import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import java.io.PrintWriter;
98import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -070099import java.net.InetAddress;
Kenny Roote29df162012-08-10 08:28:37 -0700100import java.security.Security;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import java.util.ArrayList;
102import java.util.HashMap;
103import java.util.Iterator;
104import java.util.List;
105import java.util.Locale;
106import java.util.Map;
107import java.util.TimeZone;
108import java.util.regex.Pattern;
109
Jeff Sharkeye861b422012-03-01 20:59:22 -0800110import libcore.io.IoUtils;
111
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800112import dalvik.system.CloseGuard;
Bob Leee5408332009-09-04 18:31:17 -0700113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114final class SuperNotCalledException extends AndroidRuntimeException {
115 public SuperNotCalledException(String msg) {
116 super(msg);
117 }
118}
119
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700120final class RemoteServiceException extends AndroidRuntimeException {
121 public RemoteServiceException(String msg) {
122 super(msg);
123 }
124}
125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126/**
127 * This manages the execution of the main thread in an
128 * application process, scheduling and executing activities,
129 * broadcasts, and other operations on it as the activity
130 * manager requests.
131 *
132 * {@hide}
133 */
134public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700135 /** @hide */
136 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700137 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700138 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700139 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700140 /** @hide */
141 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700142 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700143 private static final boolean DEBUG_BACKUP = false;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700144 private static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800145 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700146 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700147 private static final boolean DEBUG_PROVIDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
149 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
150 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
151 private static final int LOG_ON_PAUSE_CALLED = 30021;
152 private static final int LOG_ON_RESUME_CALLED = 30022;
153
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700154 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700155
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700156 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700158 final ApplicationThread mAppThread = new ApplicationThread();
159 final Looper mLooper = Looper.myLooper();
160 final H mH = new H();
161 final HashMap<IBinder, ActivityClientRecord> mActivities
162 = new HashMap<IBinder, ActivityClientRecord>();
163 // List of new activities (via ActivityRecord.nextIdle) that should
164 // be reported when next we idle.
165 ActivityClientRecord mNewActivities = null;
166 // Number of activities that are currently visible on-screen.
167 int mNumVisibleActivities = 0;
168 final HashMap<IBinder, Service> mServices
169 = new HashMap<IBinder, Service>();
170 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700171 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700172 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700173 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700174 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700175 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700176 Configuration mResConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700177 CompatibilityInfo mResCompatibilityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700178 Application mInitialApplication;
179 final ArrayList<Application> mAllApplications
180 = new ArrayList<Application>();
181 // set of instantiated backup agents, keyed by package name
182 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Romain Guy65b345f2011-07-27 18:51:50 -0700183 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700184 Instrumentation mInstrumentation;
185 String mInstrumentationAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700186 String mInstrumentationAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700187 String mInstrumentationAppPackage = null;
188 String mInstrumentedAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700189 String mInstrumentedAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700190 boolean mSystemThread = false;
191 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700193 // These can be accessed by multiple threads; mPackages is the lock.
194 // XXX For now we keep around information about all packages we have
195 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800196 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700197 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800198 // which means this lock gets held while the activity and window managers
199 // holds their own lock. Thus you MUST NEVER call back into the activity manager
200 // or window manager or anything that depends on them while holding this lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700201 final HashMap<String, WeakReference<LoadedApk>> mPackages
202 = new HashMap<String, WeakReference<LoadedApk>>();
203 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
204 = new HashMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700205 final HashMap<CompatibilityInfo, DisplayMetrics> mDisplayMetrics
206 = new HashMap<CompatibilityInfo, DisplayMetrics>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700207 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
208 = new HashMap<ResourcesKey, WeakReference<Resources> >();
209 final ArrayList<ActivityClientRecord> mRelaunchingActivities
210 = new ArrayList<ActivityClientRecord>();
211 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700213 // The lock of mProviderMap protects the following variables.
214 final HashMap<String, ProviderClientRecord> mProviderMap
215 = new HashMap<String, ProviderClientRecord>();
216 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
217 = new HashMap<IBinder, ProviderRefCount>();
218 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
219 = new HashMap<IBinder, ProviderClientRecord>();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700220 final HashMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
221 = new HashMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222
Jeff Hamilton52d32032011-01-08 15:31:26 -0600223 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
224 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
225
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700226 final GcIdler mGcIdler = new GcIdler();
227 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700229 static Handler sMainThreadHandler; // set once in main()
230
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800231 Bundle mCoreSettings = null;
232
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400233 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700235 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 Intent intent;
237 Bundle state;
238 Activity activity;
239 Window window;
240 Activity parent;
241 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700242 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 boolean paused;
244 boolean stopped;
245 boolean hideForNow;
246 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700247 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700248 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700250 String profileFile;
251 ParcelFileDescriptor profileFd;
252 boolean autoStopProfiler;
253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400255 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700256 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257
258 List<ResultInfo> pendingResults;
259 List<Intent> pendingIntents;
260
261 boolean startsNotResumed;
262 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800263 int pendingConfigChanges;
264 boolean onlyLocalRequest;
265
266 View mPendingRemoveWindow;
267 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700269 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 parent = null;
271 embeddedID = null;
272 paused = false;
273 stopped = false;
274 hideForNow = false;
275 nextIdle = null;
276 }
277
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800278 public boolean isPreHoneycomb() {
279 if (activity != null) {
280 return activity.getApplicationInfo().targetSdkVersion
281 < android.os.Build.VERSION_CODES.HONEYCOMB;
282 }
283 return false;
284 }
285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700287 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 return "ActivityRecord{"
289 + Integer.toHexString(System.identityHashCode(this))
290 + " token=" + token + " " + (componentName == null
291 ? "no component name" : componentName.toShortString())
292 + "}";
293 }
294 }
295
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700296 final class ProviderClientRecord {
297 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 final IContentProvider mProvider;
299 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700300 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700302 ProviderClientRecord(String[] names, IContentProvider provider,
303 ContentProvider localProvider,
304 IActivityManager.ContentProviderHolder holder) {
305 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 mProvider = provider;
307 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700308 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 }
310 }
311
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400312 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 List<Intent> intents;
314 IBinder token;
315 public String toString() {
316 return "NewIntentData{intents=" + intents + " token=" + token + "}";
317 }
318 }
319
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400320 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700321 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
322 boolean ordered, boolean sticky, IBinder token) {
323 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky, token);
324 this.intent = intent;
325 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 Intent intent;
328 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400329 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 public String toString() {
331 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700332 info.packageName + " resultCode=" + getResultCode()
333 + " resultData=" + getResultData() + " resultExtras="
334 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 }
336 }
337
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400338 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700339 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400340 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700341 int backupMode;
342 public String toString() {
343 return "CreateBackupAgentData{appInfo=" + appInfo
344 + " backupAgent=" + appInfo.backupAgentName
345 + " mode=" + backupMode + "}";
346 }
347 }
Bob Leee5408332009-09-04 18:31:17 -0700348
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400349 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 IBinder token;
351 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400352 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 Intent intent;
354 public String toString() {
355 return "CreateServiceData{token=" + token + " className="
356 + info.name + " packageName=" + info.packageName
357 + " intent=" + intent + "}";
358 }
359 }
360
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400361 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 IBinder token;
363 Intent intent;
364 boolean rebind;
365 public String toString() {
366 return "BindServiceData{token=" + token + " intent=" + intent + "}";
367 }
368 }
369
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400370 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700372 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700374 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 Intent args;
376 public String toString() {
377 return "ServiceArgsData{token=" + token + " startId=" + startId
378 + " args=" + args + "}";
379 }
380 }
381
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400382 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700383 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 String processName;
385 ApplicationInfo appInfo;
386 List<ProviderInfo> providers;
387 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 Bundle instrumentationArgs;
389 IInstrumentationWatcher instrumentationWatcher;
390 int debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800391 boolean enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700392 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700393 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400395 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700396
397 /** Initial values for {@link Profiler}. */
398 String initProfileFile;
399 ParcelFileDescriptor initProfileFd;
400 boolean initAutoStopProfiler;
401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 public String toString() {
403 return "AppBindData{appInfo=" + appInfo + "}";
404 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700405 }
406
407 static final class Profiler {
408 String profileFile;
409 ParcelFileDescriptor profileFd;
410 boolean autoStopProfiler;
411 boolean profiling;
412 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700413 public void setProfiler(String file, ParcelFileDescriptor fd) {
414 if (profiling) {
415 if (fd != null) {
416 try {
417 fd.close();
418 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700419 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700420 }
421 }
422 return;
423 }
424 if (profileFd != null) {
425 try {
426 profileFd.close();
427 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700428 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700429 }
430 }
431 profileFile = file;
432 profileFd = fd;
433 }
434 public void startProfiling() {
435 if (profileFd == null || profiling) {
436 return;
437 }
438 try {
439 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
440 8 * 1024 * 1024, 0);
441 profiling = true;
442 } catch (RuntimeException e) {
443 Slog.w(TAG, "Profiling failed on path " + profileFile);
444 try {
445 profileFd.close();
446 profileFd = null;
447 } catch (IOException e2) {
448 Slog.w(TAG, "Failure closing profile fd", e2);
449 }
450 }
451 }
452 public void stopProfiling() {
453 if (profiling) {
454 profiling = false;
455 Debug.stopMethodTracing();
456 if (profileFd != null) {
457 try {
458 profileFd.close();
459 } catch (IOException e) {
460 }
461 }
462 profileFd = null;
463 profileFile = null;
464 }
465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 }
467
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400468 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700469 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700470 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800471 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 }
474
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400475 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 IBinder token;
477 List<ResultInfo> results;
478 public String toString() {
479 return "ResultData{token=" + token + " results" + results + "}";
480 }
481 }
482
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400483 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800484 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 String what;
486 String who;
487 }
488
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400489 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700490 String path;
491 ParcelFileDescriptor fd;
492 }
493
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400494 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700495 String path;
496 ParcelFileDescriptor fd;
497 }
498
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400499 static final class UpdateCompatibilityData {
500 String pkg;
501 CompatibilityInfo info;
502 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700503
Romain Guy65b345f2011-07-27 18:51:50 -0700504 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700505
Romain Guy65b345f2011-07-27 18:51:50 -0700506 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700507 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
508 private static final String ONE_COUNT_COLUMN = "%21s %8d";
509 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700510 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 // Formatting for checkin service - update version if row format changes
513 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700514
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700515 private void updatePendingConfiguration(Configuration config) {
516 synchronized (mPackages) {
517 if (mPendingConfiguration == null ||
518 mPendingConfiguration.isOtherSeqNewer(config)) {
519 mPendingConfiguration = config;
520 }
521 }
522 }
523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 public final void schedulePauseActivity(IBinder token, boolean finished,
525 boolean userLeaving, int configChanges) {
526 queueOrSendMessage(
527 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
528 token,
529 (userLeaving ? 1 : 0),
530 configChanges);
531 }
532
533 public final void scheduleStopActivity(IBinder token, boolean showWindow,
534 int configChanges) {
535 queueOrSendMessage(
536 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
537 token, 0, configChanges);
538 }
539
540 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
541 queueOrSendMessage(
542 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
543 token);
544 }
545
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800546 public final void scheduleSleeping(IBinder token, boolean sleeping) {
547 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
548 }
549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
551 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
552 }
553
554 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
555 ResultData res = new ResultData();
556 res.token = token;
557 res.results = results;
558 queueOrSendMessage(H.SEND_RESULT, res);
559 }
560
561 // we use token to identify this activity without having to send the
562 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700563 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700564 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
565 Bundle state, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700566 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
567 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700568 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569
570 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700571 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 r.intent = intent;
573 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400574 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 r.state = state;
576
577 r.pendingResults = pendingResults;
578 r.pendingIntents = pendingNewIntents;
579
580 r.startsNotResumed = notResumed;
581 r.isForward = isForward;
582
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700583 r.profileFile = profileName;
584 r.profileFd = profileFd;
585 r.autoStopProfiler = autoStopProfiler;
586
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700587 updatePendingConfiguration(curConfig);
588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
590 }
591
592 public final void scheduleRelaunchActivity(IBinder token,
593 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800594 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800595 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
596 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 }
598
599 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
600 NewIntentData data = new NewIntentData();
601 data.intents = intents;
602 data.token = token;
603
604 queueOrSendMessage(H.NEW_INTENT, data);
605 }
606
607 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
608 int configChanges) {
609 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
610 configChanges);
611 }
612
613 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400614 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
615 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700616 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
617 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400619 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 queueOrSendMessage(H.RECEIVER, r);
621 }
622
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400623 public final void scheduleCreateBackupAgent(ApplicationInfo app,
624 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700625 CreateBackupAgentData d = new CreateBackupAgentData();
626 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400627 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700628 d.backupMode = backupMode;
629
630 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
631 }
632
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400633 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
634 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700635 CreateBackupAgentData d = new CreateBackupAgentData();
636 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400637 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700638
639 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
640 }
641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400643 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 CreateServiceData s = new CreateServiceData();
645 s.token = token;
646 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400647 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648
649 queueOrSendMessage(H.CREATE_SERVICE, s);
650 }
651
652 public final void scheduleBindService(IBinder token, Intent intent,
653 boolean rebind) {
654 BindServiceData s = new BindServiceData();
655 s.token = token;
656 s.intent = intent;
657 s.rebind = rebind;
658
Amith Yamasani742a6712011-05-04 14:49:28 -0700659 if (DEBUG_SERVICE)
660 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
661 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 queueOrSendMessage(H.BIND_SERVICE, s);
663 }
664
665 public final void scheduleUnbindService(IBinder token, Intent intent) {
666 BindServiceData s = new BindServiceData();
667 s.token = token;
668 s.intent = intent;
669
670 queueOrSendMessage(H.UNBIND_SERVICE, s);
671 }
672
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700673 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700674 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 ServiceArgsData s = new ServiceArgsData();
676 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700677 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700679 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 s.args = args;
681
682 queueOrSendMessage(H.SERVICE_ARGS, s);
683 }
684
685 public final void scheduleStopService(IBinder token) {
686 queueOrSendMessage(H.STOP_SERVICE, token);
687 }
688
689 public final void bindApplication(String processName,
690 ApplicationInfo appInfo, List<ProviderInfo> providers,
691 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700692 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Siva Velusamy92a8b222012-03-09 16:24:04 -0800694 int debugMode, boolean enableOpenGlTrace, boolean isRestrictedBackupMode,
695 boolean persistent, Configuration config, CompatibilityInfo compatInfo,
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700696 Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697
698 if (services != null) {
699 // Setup the service cache in the ServiceManager
700 ServiceManager.initServiceCache(services);
701 }
702
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800703 setCoreSettings(coreSettings);
704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 AppBindData data = new AppBindData();
706 data.processName = processName;
707 data.appInfo = appInfo;
708 data.providers = providers;
709 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 data.instrumentationArgs = instrumentationArgs;
711 data.instrumentationWatcher = instrumentationWatcher;
712 data.debugMode = debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800713 data.enableOpenGlTrace = enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700714 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700715 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400717 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700718 data.initProfileFile = profileFile;
719 data.initProfileFd = profileFd;
720 data.initAutoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 queueOrSendMessage(H.BIND_APPLICATION, data);
722 }
723
724 public final void scheduleExit() {
725 queueOrSendMessage(H.EXIT_APPLICATION, null);
726 }
727
Christopher Tate5e1ab332009-09-01 20:32:49 -0700728 public final void scheduleSuicide() {
729 queueOrSendMessage(H.SUICIDE, null);
730 }
731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 public void requestThumbnail(IBinder token) {
733 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
734 }
735
736 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700737 updatePendingConfiguration(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
739 }
740
741 public void updateTimeZone() {
742 TimeZone.setDefault(null);
743 }
744
Robert Greenwalt03595d02010-11-02 14:08:23 -0700745 public void clearDnsCache() {
746 // a non-standard API to get this to libcore
747 InetAddress.clearDnsCache();
748 }
749
Robert Greenwalt434203a2010-10-11 16:00:27 -0700750 public void setHttpProxy(String host, String port, String exclList) {
751 Proxy.setHttpProxySystemProperty(host, port, exclList);
752 }
753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 public void processInBackground() {
755 mH.removeMessages(H.GC_WHEN_IDLE);
756 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
757 }
758
759 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700760 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700761 try {
762 data.fd = ParcelFileDescriptor.dup(fd);
763 data.token = servicetoken;
764 data.args = args;
765 queueOrSendMessage(H.DUMP_SERVICE, data);
766 } catch (IOException e) {
767 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
769 }
770
771 // This function exists to make sure all receiver dispatching is
772 // correctly ordered, since these are one-way calls and the binder driver
773 // applies transaction ordering per object for such calls.
774 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700775 int resultCode, String dataStr, Bundle extras, boolean ordered,
776 boolean sticky) throws RemoteException {
777 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 }
Bob Leee5408332009-09-04 18:31:17 -0700779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 public void scheduleLowMemory() {
781 queueOrSendMessage(H.LOW_MEMORY, null);
782 }
783
784 public void scheduleActivityConfigurationChanged(IBinder token) {
785 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
786 }
787
Romain Guy7eabe552011-07-21 14:56:34 -0700788 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
789 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700790 ProfilerControlData pcd = new ProfilerControlData();
791 pcd.path = path;
792 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700793 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800794 }
795
Andy McFadden824c5102010-07-09 16:26:57 -0700796 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
797 DumpHeapData dhd = new DumpHeapData();
798 dhd.path = path;
799 dhd.fd = fd;
800 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
801 }
802
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700803 public void setSchedulingGroup(int group) {
804 // Note: do this immediately, since going into the foreground
805 // should happen regardless of what pending work we have to do
806 // and the activity manager will wait for us to report back that
807 // we are done before sending us to the background.
808 try {
809 Process.setProcessGroup(Process.myPid(), group);
810 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800811 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700812 }
813 }
Bob Leee5408332009-09-04 18:31:17 -0700814
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700815 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
816 Debug.getMemoryInfo(outInfo);
817 }
Bob Leee5408332009-09-04 18:31:17 -0700818
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700819 public void dispatchPackageBroadcast(int cmd, String[] packages) {
820 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
821 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700822
823 public void scheduleCrash(String msg) {
824 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
825 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700826
Dianne Hackborn30d71892010-12-11 10:37:55 -0800827 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
828 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700829 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700830 try {
831 data.fd = ParcelFileDescriptor.dup(fd);
832 data.token = activitytoken;
833 data.prefix = prefix;
834 data.args = args;
835 queueOrSendMessage(H.DUMP_ACTIVITY, data);
836 } catch (IOException e) {
837 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700838 }
839 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700840
Marco Nelissen18cb2872011-11-15 11:19:53 -0800841 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
842 String[] args) {
843 DumpComponentInfo data = new DumpComponentInfo();
844 try {
845 data.fd = ParcelFileDescriptor.dup(fd);
846 data.token = providertoken;
847 data.args = args;
848 queueOrSendMessage(H.DUMP_PROVIDER, data);
849 } catch (IOException e) {
850 Slog.w(TAG, "dumpProvider failed", e);
851 }
852 }
853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700855 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
856 boolean all, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700857 FileOutputStream fout = new FileOutputStream(fd);
858 PrintWriter pw = new PrintWriter(fout);
859 try {
Romain Guya998dff2012-03-23 18:58:36 -0700860 return dumpMemInfo(pw, checkin, all);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700861 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700862 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700863 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700864 }
865
Romain Guya998dff2012-03-23 18:58:36 -0700866 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean all) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 long nativeMax = Debug.getNativeHeapSize() / 1024;
868 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
869 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
870
871 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
872 Debug.getMemoryInfo(memInfo);
873
Dianne Hackbornb437e092011-08-05 17:50:29 -0700874 if (!all) {
875 return memInfo;
876 }
877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 Runtime runtime = Runtime.getRuntime();
879
880 long dalvikMax = runtime.totalMemory() / 1024;
881 long dalvikFree = runtime.freeMemory() / 1024;
882 long dalvikAllocated = dalvikMax - dalvikFree;
883 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700884 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700885 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
886 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 int globalAssetCount = AssetManager.getGlobalAssetCount();
888 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
889 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
890 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
891 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700892 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800893 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700896 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 // NOTE: if you change anything significant below, also consider changing
898 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700899 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 // Header
903 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
904 pw.print(Process.myPid()); pw.print(',');
905 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 // Heap info - max
908 pw.print(nativeMax); pw.print(',');
909 pw.print(dalvikMax); pw.print(',');
910 pw.print("N/A,");
911 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 // Heap info - allocated
914 pw.print(nativeAllocated); pw.print(',');
915 pw.print(dalvikAllocated); pw.print(',');
916 pw.print("N/A,");
917 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 // Heap info - free
920 pw.print(nativeFree); pw.print(',');
921 pw.print(dalvikFree); pw.print(',');
922 pw.print("N/A,");
923 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 // Heap info - proportional set size
926 pw.print(memInfo.nativePss); pw.print(',');
927 pw.print(memInfo.dalvikPss); pw.print(',');
928 pw.print(memInfo.otherPss); pw.print(',');
929 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700932 pw.print(memInfo.nativeSharedDirty); pw.print(',');
933 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
934 pw.print(memInfo.otherSharedDirty); pw.print(',');
935 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
936 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700939 pw.print(memInfo.nativePrivateDirty); pw.print(',');
940 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
941 pw.print(memInfo.otherPrivateDirty); pw.print(',');
942 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
943 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 // Object counts
946 pw.print(viewInstanceCount); pw.print(',');
947 pw.print(viewRootInstanceCount); pw.print(',');
948 pw.print(appContextInstanceCount); pw.print(',');
949 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 pw.print(globalAssetCount); pw.print(',');
952 pw.print(globalAssetManagerCount); pw.print(',');
953 pw.print(binderLocalObjectCount); pw.print(',');
954 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 pw.print(binderDeathObjectCount); pw.print(',');
957 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 // SQL
Vasu Noric3849202010-03-09 10:47:25 -0800960 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -0800961 pw.print(stats.memoryUsed / 1024); pw.print(',');
962 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700963 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800964 for (int i = 0; i < stats.dbStats.size(); i++) {
965 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700966 pw.print(','); pw.print(dbStats.dbName);
967 pw.print(','); pw.print(dbStats.pageSize);
968 pw.print(','); pw.print(dbStats.dbSize);
969 pw.print(','); pw.print(dbStats.lookaside);
970 pw.print(','); pw.print(dbStats.cache);
971 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800972 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700973 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700974
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700975 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 }
Bob Leee5408332009-09-04 18:31:17 -0700977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700979 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
980 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
981 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
982 "------");
983 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
984 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
985 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
986 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700988 int otherPss = memInfo.otherPss;
989 int otherSharedDirty = memInfo.otherSharedDirty;
990 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700992 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Romain Guy65b345f2011-07-27 18:51:50 -0700993 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700994 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
995 memInfo.getOtherPrivateDirty(i), "", "", "");
996 otherPss -= memInfo.getOtherPss(i);
997 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
998 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
999 }
1000
1001 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
1002 otherPrivateDirty, "", "", "");
1003 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
1004 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
1005 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
1006 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007
1008 pw.println(" ");
1009 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001010 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 viewRootInstanceCount);
1012
1013 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1014 "Activities:", activityInstanceCount);
1015
1016 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1017 "AssetManagers:", globalAssetManagerCount);
1018
1019 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1020 "Proxy Binders:", binderProxyObjectCount);
1021 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
1022
1023 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 // SQLite mem info
1026 pw.println(" ");
1027 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001028 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1029 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1030 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001031 pw.println(" ");
1032 int N = stats.dbStats.size();
1033 if (N > 0) {
1034 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001035 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1036 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001037 for (int i = 0; i < N; i++) {
1038 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001039 printRow(pw, DB_INFO_FORMAT,
1040 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1041 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1042 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1043 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001044 }
1045 }
Bob Leee5408332009-09-04 18:31:17 -07001046
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001047 // Asset details.
1048 String assetAlloc = AssetManager.getAssetAllocations();
1049 if (assetAlloc != null) {
1050 pw.println(" ");
1051 pw.println(" Asset Allocations");
1052 pw.print(assetAlloc);
1053 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001054
1055 return memInfo;
1056 }
1057
1058 @Override
1059 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1060 dumpGraphicsInfo(fd);
Jeff Brown98365d72012-08-19 20:30:52 -07001061 WindowManagerGlobal.getInstance().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 }
1063
Jeff Brown6754ba22011-12-14 20:20:01 -08001064 @Override
1065 public void dumpDbInfo(FileDescriptor fd, String[] args) {
1066 PrintWriter pw = new PrintWriter(new FileOutputStream(fd));
1067 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1068 SQLiteDebug.dump(printer, args);
1069 pw.flush();
1070 }
1071
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001072 @Override
1073 public void unstableProviderDied(IBinder provider) {
1074 queueOrSendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
1075 }
1076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 private void printRow(PrintWriter pw, String format, Object...objs) {
1078 pw.println(String.format(format, objs));
1079 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001080
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001081 public void setCoreSettings(Bundle coreSettings) {
1082 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001083 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001084
1085 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1086 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1087 ucd.pkg = pkg;
1088 ucd.info = info;
1089 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1090 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001091
1092 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001093 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001094 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
1097
Romain Guy65b345f2011-07-27 18:51:50 -07001098 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 public static final int LAUNCH_ACTIVITY = 100;
1100 public static final int PAUSE_ACTIVITY = 101;
1101 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1102 public static final int STOP_ACTIVITY_SHOW = 103;
1103 public static final int STOP_ACTIVITY_HIDE = 104;
1104 public static final int SHOW_WINDOW = 105;
1105 public static final int HIDE_WINDOW = 106;
1106 public static final int RESUME_ACTIVITY = 107;
1107 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001108 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 public static final int BIND_APPLICATION = 110;
1110 public static final int EXIT_APPLICATION = 111;
1111 public static final int NEW_INTENT = 112;
1112 public static final int RECEIVER = 113;
1113 public static final int CREATE_SERVICE = 114;
1114 public static final int SERVICE_ARGS = 115;
1115 public static final int STOP_SERVICE = 116;
1116 public static final int REQUEST_THUMBNAIL = 117;
1117 public static final int CONFIGURATION_CHANGED = 118;
1118 public static final int CLEAN_UP_CONTEXT = 119;
1119 public static final int GC_WHEN_IDLE = 120;
1120 public static final int BIND_SERVICE = 121;
1121 public static final int UNBIND_SERVICE = 122;
1122 public static final int DUMP_SERVICE = 123;
1123 public static final int LOW_MEMORY = 124;
1124 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1125 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001126 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001127 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001128 public static final int DESTROY_BACKUP_AGENT = 129;
1129 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001130 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001131 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001132 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001133 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001134 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001135 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001136 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001137 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001138 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001139 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001140 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001141 public static final int UNSTABLE_PROVIDER_DIED = 142;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001143 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 switch (code) {
1145 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1146 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1147 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1148 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1149 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1150 case SHOW_WINDOW: return "SHOW_WINDOW";
1151 case HIDE_WINDOW: return "HIDE_WINDOW";
1152 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1153 case SEND_RESULT: return "SEND_RESULT";
1154 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1155 case BIND_APPLICATION: return "BIND_APPLICATION";
1156 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1157 case NEW_INTENT: return "NEW_INTENT";
1158 case RECEIVER: return "RECEIVER";
1159 case CREATE_SERVICE: return "CREATE_SERVICE";
1160 case SERVICE_ARGS: return "SERVICE_ARGS";
1161 case STOP_SERVICE: return "STOP_SERVICE";
1162 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1163 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1164 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1165 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1166 case BIND_SERVICE: return "BIND_SERVICE";
1167 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1168 case DUMP_SERVICE: return "DUMP_SERVICE";
1169 case LOW_MEMORY: return "LOW_MEMORY";
1170 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1171 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001172 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001173 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1174 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001175 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001176 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001177 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001178 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001179 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001180 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001181 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001182 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001183 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001184 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001185 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001186 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001187 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 }
1189 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001190 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001193 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 switch (msg.what) {
1195 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001196 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001197 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198
1199 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001200 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001201 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001202 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 } break;
1204 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001205 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001206 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001207 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001208 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 } break;
1210 case PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001211 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001213 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001214 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 break;
1216 case PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001217 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001219 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 break;
1221 case STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001222 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001224 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 break;
1226 case STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001227 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001229 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 break;
1231 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001232 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001234 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 break;
1236 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001237 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001239 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 break;
1241 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001242 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 handleResumeActivity((IBinder)msg.obj, true,
1244 msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001245 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 break;
1247 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001248 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001250 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 break;
1252 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001253 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1255 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001256 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 break;
1258 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001259 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 AppBindData data = (AppBindData)msg.obj;
1261 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001262 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 break;
1264 case EXIT_APPLICATION:
1265 if (mInitialApplication != null) {
1266 mInitialApplication.onTerminate();
1267 }
1268 Looper.myLooper().quit();
1269 break;
1270 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001271 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001273 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 break;
1275 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001276 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001278 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001279 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 break;
1281 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001282 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001284 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 break;
1286 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001287 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001289 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 break;
1291 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001292 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001294 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 break;
1296 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001297 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001299 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 break;
1301 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001302 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001304 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001305 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 break;
1307 case REQUEST_THUMBNAIL:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001308 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "requestThumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 handleRequestThumbnail((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001310 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 break;
1312 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001313 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001314 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001315 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001316 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 break;
1318 case CLEAN_UP_CONTEXT:
1319 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1320 cci.context.performFinalCleanup(cci.who, cci.what);
1321 break;
1322 case GC_WHEN_IDLE:
1323 scheduleGcIdler();
1324 break;
1325 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001326 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 break;
1328 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001329 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001331 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 break;
1333 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001334 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001336 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001338 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001339 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001340 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001341 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001342 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001343 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001344 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001345 break;
1346 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001347 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001348 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001349 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001350 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001351 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001352 Process.killProcess(Process.myPid());
1353 break;
1354 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001355 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001356 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001357 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001358 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001359 case ENABLE_JIT:
1360 ensureJitEnabled();
1361 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001362 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001363 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001364 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001365 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001366 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001367 case SCHEDULE_CRASH:
1368 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001369 case DUMP_HEAP:
1370 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1371 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001372 case DUMP_ACTIVITY:
1373 handleDumpActivity((DumpComponentInfo)msg.obj);
1374 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001375 case DUMP_PROVIDER:
1376 handleDumpProvider((DumpComponentInfo)msg.obj);
1377 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001378 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001379 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001380 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001381 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001382 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001383 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001384 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001385 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001386 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001387 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001388 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1389 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001390 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001391 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001392 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001393 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001394 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001395 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001396 case UNSTABLE_PROVIDER_DIED:
1397 handleUnstableProviderDied((IBinder)msg.obj, false);
1398 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001400 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 }
Bob Leee5408332009-09-04 18:31:17 -07001402
Brian Carlstromed7e0072011-03-24 13:27:57 -07001403 private void maybeSnapshot() {
1404 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001405 // convert the *private* ActivityThread.PackageInfo to *public* known
1406 // android.content.pm.PackageInfo
1407 String packageName = mBoundApplication.info.mPackageName;
1408 android.content.pm.PackageInfo packageInfo = null;
1409 try {
1410 Context context = getSystemContext();
1411 if(context == null) {
1412 Log.e(TAG, "cannot get a valid context");
1413 return;
1414 }
1415 PackageManager pm = context.getPackageManager();
1416 if(pm == null) {
1417 Log.e(TAG, "cannot get a valid PackageManager");
1418 return;
1419 }
1420 packageInfo = pm.getPackageInfo(
1421 packageName, PackageManager.GET_ACTIVITIES);
1422 } catch (NameNotFoundException e) {
1423 Log.e(TAG, "cannot get package info for " + packageName, e);
1424 }
1425 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001426 }
1427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 }
1429
Romain Guy65b345f2011-07-27 18:51:50 -07001430 private class Idler implements MessageQueue.IdleHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001432 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001433 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001434 if (mBoundApplication != null && mProfiler.profileFd != null
1435 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001436 stopProfiling = true;
1437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 if (a != null) {
1439 mNewActivities = null;
1440 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001441 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001443 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 TAG, "Reporting idle of " + a +
1445 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001446 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 if (a.activity != null && !a.activity.mFinished) {
1448 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001449 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001450 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001452 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 }
1454 }
1455 prev = a;
1456 a = a.nextIdle;
1457 prev.nextIdle = null;
1458 } while (a != null);
1459 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001460 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001461 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001462 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001463 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 return false;
1465 }
1466 }
1467
1468 final class GcIdler implements MessageQueue.IdleHandler {
1469 public final boolean queueIdle() {
1470 doGcIfNeeded();
1471 return false;
1472 }
1473 }
1474
Romain Guy65b345f2011-07-27 18:51:50 -07001475 private static class ResourcesKey {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001476 final private String mResDir;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001477 final private Configuration mOverrideConfiguration;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001478 final private float mScale;
1479 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001480
Dianne Hackborn756220b2012-08-14 16:45:30 -07001481 ResourcesKey(String resDir, Configuration overrideConfiguration, float scale) {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001482 mResDir = resDir;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001483 if (overrideConfiguration != null) {
1484 if (Configuration.EMPTY.equals(overrideConfiguration)) {
1485 overrideConfiguration = null;
1486 }
1487 }
1488 mOverrideConfiguration = overrideConfiguration;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001489 mScale = scale;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001490 int hash = 17;
1491 hash = 31 * hash + mResDir.hashCode();
1492 hash = 31 * hash + (mOverrideConfiguration != null
1493 ? mOverrideConfiguration.hashCode() : 0);
1494 hash = 31 * hash + Float.floatToIntBits(mScale);
1495 mHash = hash;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001496 }
Bob Leee5408332009-09-04 18:31:17 -07001497
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001498 @Override
1499 public int hashCode() {
1500 return mHash;
1501 }
1502
1503 @Override
1504 public boolean equals(Object obj) {
1505 if (!(obj instanceof ResourcesKey)) {
1506 return false;
1507 }
1508 ResourcesKey peer = (ResourcesKey) obj;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001509 if (!mResDir.equals(peer.mResDir)) {
1510 return false;
1511 }
1512 if (mOverrideConfiguration != peer.mOverrideConfiguration) {
1513 if (mOverrideConfiguration == null || peer.mOverrideConfiguration == null) {
1514 return false;
1515 }
1516 if (!mOverrideConfiguration.equals(peer.mOverrideConfiguration)) {
1517 return false;
1518 }
1519 }
1520 if (mScale != peer.mScale) {
1521 return false;
1522 }
1523 return true;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001524 }
1525 }
1526
Romain Guy65b345f2011-07-27 18:51:50 -07001527 public static ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001528 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530
Romain Guy65b345f2011-07-27 18:51:50 -07001531 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001532 ActivityThread am = currentActivityThread();
1533 return (am != null && am.mBoundApplication != null)
1534 ? am.mBoundApplication.processName : null;
1535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536
Romain Guy65b345f2011-07-27 18:51:50 -07001537 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001538 ActivityThread am = currentActivityThread();
1539 return am != null ? am.mInitialApplication : null;
1540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001542 public static IPackageManager getPackageManager() {
1543 if (sPackageManager != null) {
1544 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1545 return sPackageManager;
1546 }
1547 IBinder b = ServiceManager.getService("package");
1548 //Slog.v("PackageManager", "default service binder = " + b);
1549 sPackageManager = IPackageManager.Stub.asInterface(b);
1550 //Slog.v("PackageManager", "default service = " + sPackageManager);
1551 return sPackageManager;
1552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001554 DisplayMetrics getDisplayMetricsLocked(CompatibilityInfo ci, boolean forceUpdate) {
1555 DisplayMetrics dm = mDisplayMetrics.get(ci);
1556 if (dm != null && !forceUpdate) {
1557 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001558 }
Jeff Brown848c2dc2012-08-19 20:18:08 -07001559
1560 DisplayManager displayManager = DisplayManager.getInstance();
1561 if (displayManager == null) {
1562 // may be null early in system startup
1563 dm = new DisplayMetrics();
1564 dm.setToDefaults();
1565 return dm;
1566 }
1567
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001568 if (dm == null) {
1569 dm = new DisplayMetrics();
1570 mDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001571 }
Jeff Brown848c2dc2012-08-19 20:18:08 -07001572
Jeff Brownd32460c2012-07-20 16:15:36 -07001573 CompatibilityInfoHolder cih = new CompatibilityInfoHolder();
1574 cih.set(ci);
Jeff Brown98365d72012-08-19 20:30:52 -07001575 Display d = displayManager.getCompatibleDisplay(Display.DEFAULT_DISPLAY, cih);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001576 d.getMetrics(dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001577 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1578 // + metrics.heightPixels + " den=" + metrics.density
1579 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001580 return dm;
1581 }
1582
Romain Guy65b345f2011-07-27 18:51:50 -07001583 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001584 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1585 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001586 if (config == null) {
1587 return null;
1588 }
1589 if (compat != null && !compat.supportsScreen()) {
1590 mMainThreadConfig.setTo(config);
1591 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001592 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001593 }
1594 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001597 /**
1598 * Creates the top level Resources for applications with the given compatibility info.
1599 *
1600 * @param resDir the resource directory.
1601 * @param compInfo the compability info. It will use the default compatibility info when it's
1602 * null.
1603 */
Dianne Hackborn756220b2012-08-14 16:45:30 -07001604 Resources getTopLevelResources(String resDir, Configuration overrideConfiguration,
1605 CompatibilityInfo compInfo) {
1606 ResourcesKey key = new ResourcesKey(resDir, overrideConfiguration,
1607 compInfo.applicationScale);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001608 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001610 // Resources is app scale dependent.
1611 if (false) {
1612 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1613 + compInfo.applicationScale);
1614 }
1615 WeakReference<Resources> wr = mActiveResources.get(key);
1616 r = wr != null ? wr.get() : null;
1617 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1618 if (r != null && r.getAssets().isUpToDate()) {
1619 if (false) {
1620 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1621 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1622 }
1623 return r;
1624 }
1625 }
1626
1627 //if (r != null) {
1628 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1629 // + r + " " + resDir);
1630 //}
1631
1632 AssetManager assets = new AssetManager();
1633 if (assets.addAssetPath(resDir) == 0) {
1634 return null;
1635 }
1636
1637 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Dianne Hackborn836e2622011-10-04 18:32:39 -07001638 DisplayMetrics metrics = getDisplayMetricsLocked(null, false);
Dianne Hackborn756220b2012-08-14 16:45:30 -07001639 Configuration config;
1640 if (key.mOverrideConfiguration != null) {
1641 config = new Configuration(getConfiguration());
1642 config.updateFrom(key.mOverrideConfiguration);
1643 } else {
1644 config = getConfiguration();
1645 }
1646 r = new Resources(assets, metrics, config, compInfo);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001647 if (false) {
1648 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1649 + r.getConfiguration() + " appScale="
1650 + r.getCompatibilityInfo().applicationScale);
1651 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07001652
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001653 synchronized (mPackages) {
1654 WeakReference<Resources> wr = mActiveResources.get(key);
1655 Resources existing = wr != null ? wr.get() : null;
1656 if (existing != null && existing.getAssets().isUpToDate()) {
1657 // Someone else already created the resources while we were
1658 // unlocked; go ahead and use theirs.
1659 r.getAssets().close();
1660 return existing;
1661 }
1662
1663 // XXX need to remove entries when weak references go away
1664 mActiveResources.put(key, new WeakReference<Resources>(r));
1665 return r;
1666 }
1667 }
1668
1669 /**
1670 * Creates the top level resources for the given package.
1671 */
Dianne Hackborn756220b2012-08-14 16:45:30 -07001672 Resources getTopLevelResources(String resDir, Configuration overrideConfiguration,
1673 LoadedApk pkgInfo) {
1674 return getTopLevelResources(resDir, overrideConfiguration,
1675 pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001676 }
1677
1678 final Handler getHandler() {
1679 return mH;
1680 }
1681
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001682 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1683 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001684 synchronized (mPackages) {
1685 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1687 ref = mPackages.get(packageName);
1688 } else {
1689 ref = mResourcePackages.get(packageName);
1690 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001691 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001692 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001693 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1694 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 if (packageInfo != null && (packageInfo.mResources == null
1696 || packageInfo.mResources.getAssets().isUpToDate())) {
1697 if (packageInfo.isSecurityViolation()
1698 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1699 throw new SecurityException(
1700 "Requesting code from " + packageName
1701 + " to be run in process "
1702 + mBoundApplication.processName
1703 + "/" + mBoundApplication.appInfo.uid);
1704 }
1705 return packageInfo;
1706 }
1707 }
1708
1709 ApplicationInfo ai = null;
1710 try {
1711 ai = getPackageManager().getApplicationInfo(packageName,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001712 PackageManager.GET_SHARED_LIBRARY_FILES, UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001714 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 }
1716
1717 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001718 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 }
1720
1721 return null;
1722 }
1723
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001724 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1725 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1727 boolean securityViolation = includeCode && ai.uid != 0
1728 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001729 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001730 : true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1732 |Context.CONTEXT_IGNORE_SECURITY))
1733 == Context.CONTEXT_INCLUDE_CODE) {
1734 if (securityViolation) {
1735 String msg = "Requesting code from " + ai.packageName
1736 + " (with uid " + ai.uid + ")";
1737 if (mBoundApplication != null) {
1738 msg = msg + " to be run in process "
1739 + mBoundApplication.processName + " (with uid "
1740 + mBoundApplication.appInfo.uid + ")";
1741 }
1742 throw new SecurityException(msg);
1743 }
1744 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001745 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 }
1747
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001748 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1749 CompatibilityInfo compatInfo) {
1750 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 }
1752
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001753 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1754 synchronized (mPackages) {
1755 WeakReference<LoadedApk> ref;
1756 if (includeCode) {
1757 ref = mPackages.get(packageName);
1758 } else {
1759 ref = mResourcePackages.get(packageName);
1760 }
1761 return ref != null ? ref.get() : null;
1762 }
1763 }
1764
Romain Guy65b345f2011-07-27 18:51:50 -07001765 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1767 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001768 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 if (includeCode) {
1770 ref = mPackages.get(aInfo.packageName);
1771 } else {
1772 ref = mResourcePackages.get(aInfo.packageName);
1773 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001774 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 if (packageInfo == null || (packageInfo.mResources != null
1776 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001777 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 : "Loading resource-only package ") + aInfo.packageName
1779 + " (in " + (mBoundApplication != null
1780 ? mBoundApplication.processName : null)
1781 + ")");
1782 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001783 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 securityViolation, includeCode &&
1785 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1786 if (includeCode) {
1787 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001788 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 } else {
1790 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001791 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 }
1793 }
1794 return packageInfo;
1795 }
1796 }
1797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 ActivityThread() {
1799 }
1800
1801 public ApplicationThread getApplicationThread()
1802 {
1803 return mAppThread;
1804 }
1805
1806 public Instrumentation getInstrumentation()
1807 {
1808 return mInstrumentation;
1809 }
1810
1811 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001812 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 }
1814
1815 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001816 return mProfiler != null && mProfiler.profileFile != null
1817 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 }
1819
1820 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001821 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 }
1823
1824 public Looper getLooper() {
1825 return mLooper;
1826 }
1827
1828 public Application getApplication() {
1829 return mInitialApplication;
1830 }
Bob Leee5408332009-09-04 18:31:17 -07001831
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001832 public String getProcessName() {
1833 return mBoundApplication.processName;
1834 }
Bob Leee5408332009-09-04 18:31:17 -07001835
Dianne Hackborn21556372010-02-04 16:34:40 -08001836 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 synchronized (this) {
1838 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001839 ContextImpl context =
1840 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001841 LoadedApk info = new LoadedApk(this, "android", context, null,
1842 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 context.init(info, null, this);
1844 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001845 getConfiguration(), getDisplayMetricsLocked(
1846 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001848 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 // + ": " + context.getResources().getConfiguration());
1850 }
1851 }
1852 return mSystemContext;
1853 }
1854
Mike Cleron432b7132009-09-24 15:28:29 -07001855 public void installSystemApplicationInfo(ApplicationInfo info) {
1856 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001857 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001858 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001859 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001860
1861 // give ourselves a default profiler
1862 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001863 }
1864 }
1865
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001866 void ensureJitEnabled() {
1867 if (!mJitEnabled) {
1868 mJitEnabled = true;
1869 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1870 }
1871 }
1872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 void scheduleGcIdler() {
1874 if (!mGcIdlerScheduled) {
1875 mGcIdlerScheduled = true;
1876 Looper.myQueue().addIdleHandler(mGcIdler);
1877 }
1878 mH.removeMessages(H.GC_WHEN_IDLE);
1879 }
1880
1881 void unscheduleGcIdler() {
1882 if (mGcIdlerScheduled) {
1883 mGcIdlerScheduled = false;
1884 Looper.myQueue().removeIdleHandler(mGcIdler);
1885 }
1886 mH.removeMessages(H.GC_WHEN_IDLE);
1887 }
1888
1889 void doGcIfNeeded() {
1890 mGcIdlerScheduled = false;
1891 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001892 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 // + "m now=" + now);
1894 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001895 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 BinderInternal.forceGc("bg");
1897 }
1898 }
1899
Jeff Hamilton52d32032011-01-08 15:31:26 -06001900 public void registerOnActivityPausedListener(Activity activity,
1901 OnActivityPausedListener listener) {
1902 synchronized (mOnPauseListeners) {
1903 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1904 if (list == null) {
1905 list = new ArrayList<OnActivityPausedListener>();
1906 mOnPauseListeners.put(activity, list);
1907 }
1908 list.add(listener);
1909 }
1910 }
1911
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001912 public void unregisterOnActivityPausedListener(Activity activity,
1913 OnActivityPausedListener listener) {
1914 synchronized (mOnPauseListeners) {
1915 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1916 if (list != null) {
1917 list.remove(listener);
1918 }
1919 }
1920 }
1921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 public final ActivityInfo resolveActivityInfo(Intent intent) {
1923 ActivityInfo aInfo = intent.resolveActivityInfo(
1924 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1925 if (aInfo == null) {
1926 // Throw an exception.
1927 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07001928 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 }
1930 return aInfo;
1931 }
Bob Leee5408332009-09-04 18:31:17 -07001932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001935 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001936 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001938 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 r.intent = intent;
1940 r.state = state;
1941 r.parent = parent;
1942 r.embeddedID = id;
1943 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001944 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 if (localLOGV) {
1946 ComponentName compname = intent.getComponent();
1947 String name;
1948 if (compname != null) {
1949 name = compname.toShortString();
1950 } else {
1951 name = "(Intent " + intent + ").getComponent() returned null";
1952 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001953 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 + ", comp=" + name
1955 + ", token=" + token);
1956 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001957 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 }
1959
1960 public final Activity getActivity(IBinder token) {
1961 return mActivities.get(token).activity;
1962 }
1963
1964 public final void sendActivityResult(
1965 IBinder token, String id, int requestCode,
1966 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001967 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001968 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1970 list.add(new ResultInfo(id, requestCode, resultCode, data));
1971 mAppThread.scheduleSendResult(token, list);
1972 }
1973
1974 // if the thread hasn't started yet, we don't have the handler, so just
1975 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07001976 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 queueOrSendMessage(what, obj, 0, 0);
1978 }
1979
Romain Guy65b345f2011-07-27 18:51:50 -07001980 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 queueOrSendMessage(what, obj, arg1, 0);
1982 }
1983
Romain Guy65b345f2011-07-27 18:51:50 -07001984 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001986 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1988 + ": " + arg1 + " / " + obj);
1989 Message msg = Message.obtain();
1990 msg.what = what;
1991 msg.obj = obj;
1992 msg.arg1 = arg1;
1993 msg.arg2 = arg2;
1994 mH.sendMessage(msg);
1995 }
1996 }
1997
Dianne Hackborn21556372010-02-04 16:34:40 -08001998 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 String what) {
2000 ContextCleanupInfo cci = new ContextCleanupInfo();
2001 cci.context = context;
2002 cci.who = who;
2003 cci.what = what;
2004 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
2005 }
2006
Romain Guy65b345f2011-07-27 18:51:50 -07002007 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2009
2010 ActivityInfo aInfo = r.activityInfo;
2011 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002012 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 Context.CONTEXT_INCLUDE_CODE);
2014 }
Bob Leee5408332009-09-04 18:31:17 -07002015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 ComponentName component = r.intent.getComponent();
2017 if (component == null) {
2018 component = r.intent.resolveActivity(
2019 mInitialApplication.getPackageManager());
2020 r.intent.setComponent(component);
2021 }
2022
2023 if (r.activityInfo.targetActivity != null) {
2024 component = new ComponentName(r.activityInfo.packageName,
2025 r.activityInfo.targetActivity);
2026 }
2027
2028 Activity activity = null;
2029 try {
2030 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2031 activity = mInstrumentation.newActivity(
2032 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002033 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 r.intent.setExtrasClassLoader(cl);
2035 if (r.state != null) {
2036 r.state.setClassLoader(cl);
2037 }
2038 } catch (Exception e) {
2039 if (!mInstrumentation.onException(activity, e)) {
2040 throw new RuntimeException(
2041 "Unable to instantiate activity " + component
2042 + ": " + e.toString(), e);
2043 }
2044 }
2045
2046 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002047 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002048
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002049 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2050 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 TAG, r + ": app=" + app
2052 + ", appName=" + app.getPackageName()
2053 + ", pkg=" + r.packageInfo.getPackageName()
2054 + ", comp=" + r.intent.getComponent().toShortString()
2055 + ", dir=" + r.packageInfo.getAppDir());
2056
2057 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08002058 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 appContext.init(r.packageInfo, r.token, this);
2060 appContext.setOuterContext(activity);
2061 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002062 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002063 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002064 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002065 activity.attach(appContext, this, getInstrumentation(), r.token,
2066 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002067 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07002068
Christopher Tateb70f3df2009-04-07 16:07:59 -07002069 if (customIntent != null) {
2070 activity.mIntent = customIntent;
2071 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002072 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 activity.mStartedActivity = false;
2074 int theme = r.activityInfo.getThemeResource();
2075 if (theme != 0) {
2076 activity.setTheme(theme);
2077 }
2078
2079 activity.mCalled = false;
2080 mInstrumentation.callActivityOnCreate(activity, r.state);
2081 if (!activity.mCalled) {
2082 throw new SuperNotCalledException(
2083 "Activity " + r.intent.getComponent().toShortString() +
2084 " did not call through to super.onCreate()");
2085 }
2086 r.activity = activity;
2087 r.stopped = true;
2088 if (!r.activity.mFinished) {
2089 activity.performStart();
2090 r.stopped = false;
2091 }
2092 if (!r.activity.mFinished) {
2093 if (r.state != null) {
2094 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2095 }
2096 }
2097 if (!r.activity.mFinished) {
2098 activity.mCalled = false;
2099 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2100 if (!activity.mCalled) {
2101 throw new SuperNotCalledException(
2102 "Activity " + r.intent.getComponent().toShortString() +
2103 " did not call through to super.onPostCreate()");
2104 }
2105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 }
2107 r.paused = true;
2108
2109 mActivities.put(r.token, r);
2110
2111 } catch (SuperNotCalledException e) {
2112 throw e;
2113
2114 } catch (Exception e) {
2115 if (!mInstrumentation.onException(activity, e)) {
2116 throw new RuntimeException(
2117 "Unable to start activity " + component
2118 + ": " + e.toString(), e);
2119 }
2120 }
2121
2122 return activity;
2123 }
2124
Romain Guy65b345f2011-07-27 18:51:50 -07002125 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 // If we are getting ready to gc after going to the background, well
2127 // we are back active so skip it.
2128 unscheduleGcIdler();
2129
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002130 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002131 mProfiler.setProfiler(r.profileFile, r.profileFd);
2132 mProfiler.startProfiling();
2133 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002134 }
2135
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002136 // Make sure we are running with the most recent config.
2137 handleConfigurationChanged(null, null);
2138
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002139 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07002141 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142
2143 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002144 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002145 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 handleResumeActivity(r.token, false, r.isForward);
2147
2148 if (!r.activity.mFinished && r.startsNotResumed) {
2149 // The activity manager actually wants this one to start out
2150 // paused, because it needs to be visible but isn't in the
2151 // foreground. We accomplish this by going through the
2152 // normal startup (because activities expect to go through
2153 // onResume() the first time they run, before their window
2154 // is displayed), and then pausing it. However, in this case
2155 // we do -not- need to do the full pause cycle (of freezing
2156 // and such) because the activity manager assumes it can just
2157 // retain the current state it has.
2158 try {
2159 r.activity.mCalled = false;
2160 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002161 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002162 // we need to be created again. But we only do this
2163 // for pre-Honeycomb apps, which always save their state
2164 // when pausing, so we can not have them save their state
2165 // when restarting from a paused state. For HC and later,
2166 // we want to (and can) let the state be saved as the normal
2167 // part of stopping the activity.
2168 if (r.isPreHoneycomb()) {
2169 r.state = oldState;
2170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 if (!r.activity.mCalled) {
2172 throw new SuperNotCalledException(
2173 "Activity " + r.intent.getComponent().toShortString() +
2174 " did not call through to super.onPause()");
2175 }
2176
2177 } catch (SuperNotCalledException e) {
2178 throw e;
2179
2180 } catch (Exception e) {
2181 if (!mInstrumentation.onException(r.activity, e)) {
2182 throw new RuntimeException(
2183 "Unable to pause activity "
2184 + r.intent.getComponent().toShortString()
2185 + ": " + e.toString(), e);
2186 }
2187 }
2188 r.paused = true;
2189 }
2190 } else {
2191 // If there was an error, for any reason, tell the activity
2192 // manager to stop us.
2193 try {
2194 ActivityManagerNative.getDefault()
2195 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2196 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002197 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 }
2199 }
2200 }
2201
Romain Guy65b345f2011-07-27 18:51:50 -07002202 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 List<Intent> intents) {
2204 final int N = intents.size();
2205 for (int i=0; i<N; i++) {
2206 Intent intent = intents.get(i);
2207 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002208 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2210 }
2211 }
2212
2213 public final void performNewIntents(IBinder token,
2214 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002215 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 if (r != null) {
2217 final boolean resumed = !r.paused;
2218 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002219 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 mInstrumentation.callActivityOnPause(r.activity);
2221 }
2222 deliverNewIntents(r, intents);
2223 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002224 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002225 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 }
2227 }
2228 }
Bob Leee5408332009-09-04 18:31:17 -07002229
Romain Guy65b345f2011-07-27 18:51:50 -07002230 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 performNewIntents(data.token, data.intents);
2232 }
2233
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002234 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2235
2236 /**
2237 * Return the Intent that's currently being handled by a
2238 * BroadcastReceiver on this thread, or null if none.
2239 * @hide
2240 */
2241 public static Intent getIntentBeingBroadcast() {
2242 return sCurrentBroadcastIntent.get();
2243 }
2244
Romain Guy65b345f2011-07-27 18:51:50 -07002245 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 // If we are getting ready to gc after going to the background, well
2247 // we are back active so skip it.
2248 unscheduleGcIdler();
2249
2250 String component = data.intent.getComponent().getClassName();
2251
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002252 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002253 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254
2255 IActivityManager mgr = ActivityManagerNative.getDefault();
2256
Romain Guy65b345f2011-07-27 18:51:50 -07002257 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 try {
2259 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2260 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002261 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2263 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002264 if (DEBUG_BROADCAST) Slog.i(TAG,
2265 "Finishing failed broadcast to " + data.intent.getComponent());
2266 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 throw new RuntimeException(
2268 "Unable to instantiate receiver " + component
2269 + ": " + e.toString(), e);
2270 }
2271
2272 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002273 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002274
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002275 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 TAG, "Performing receive of " + data.intent
2277 + ": app=" + app
2278 + ", appName=" + app.getPackageName()
2279 + ", pkg=" + packageInfo.getPackageName()
2280 + ", comp=" + data.intent.getComponent().toShortString()
2281 + ", dir=" + packageInfo.getAppDir());
2282
Dianne Hackborn21556372010-02-04 16:34:40 -08002283 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002284 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002285 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 receiver.onReceive(context.getReceiverRestrictedContext(),
2287 data.intent);
2288 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002289 if (DEBUG_BROADCAST) Slog.i(TAG,
2290 "Finishing failed broadcast to " + data.intent.getComponent());
2291 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 if (!mInstrumentation.onException(receiver, e)) {
2293 throw new RuntimeException(
2294 "Unable to start receiver " + component
2295 + ": " + e.toString(), e);
2296 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002297 } finally {
2298 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 }
2300
Dianne Hackborne829fef2010-10-26 17:44:01 -07002301 if (receiver.getPendingResult() != null) {
2302 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 }
2304 }
2305
Christopher Tate181fafa2009-05-14 11:12:14 -07002306 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002307 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002308 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002309
2310 // no longer idle; we have backup work to do
2311 unscheduleGcIdler();
2312
2313 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002314 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002315 String packageName = packageInfo.mPackageName;
2316 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002317 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002318 + " already exists");
2319 return;
2320 }
Bob Leee5408332009-09-04 18:31:17 -07002321
Christopher Tate181fafa2009-05-14 11:12:14 -07002322 BackupAgent agent = null;
2323 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002324
Christopher Tate79ec80d2011-06-24 14:58:49 -07002325 // full backup operation but no app-supplied agent? use the default implementation
2326 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2327 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002328 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002329 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002330
Christopher Tate181fafa2009-05-14 11:12:14 -07002331 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002332 IBinder binder = null;
2333 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002334 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2335
Christopher Tated1475e02009-07-09 15:36:17 -07002336 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002337 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002338
2339 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002340 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002341 context.init(packageInfo, null, this);
2342 context.setOuterContext(agent);
2343 agent.attach(context);
2344
2345 agent.onCreate();
2346 binder = agent.onBind();
2347 mBackupAgents.put(packageName, agent);
2348 } catch (Exception e) {
2349 // If this is during restore, fail silently; otherwise go
2350 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002351 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002352 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2353 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002354 throw e;
2355 }
2356 // falling through with 'binder' still null
2357 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002358
2359 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002360 try {
2361 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2362 } catch (RemoteException e) {
2363 // nothing to do.
2364 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002365 } catch (Exception e) {
2366 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002367 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002368 }
2369 }
2370
2371 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002372 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002373 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002374
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002375 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002376 String packageName = packageInfo.mPackageName;
2377 BackupAgent agent = mBackupAgents.get(packageName);
2378 if (agent != null) {
2379 try {
2380 agent.onDestroy();
2381 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002382 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002383 e.printStackTrace();
2384 }
2385 mBackupAgents.remove(packageName);
2386 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002387 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002388 }
2389 }
2390
Romain Guy65b345f2011-07-27 18:51:50 -07002391 private void handleCreateService(CreateServiceData 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
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002396 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002397 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 Service service = null;
2399 try {
2400 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2401 service = (Service) cl.loadClass(data.info.name).newInstance();
2402 } catch (Exception e) {
2403 if (!mInstrumentation.onException(service, e)) {
2404 throw new RuntimeException(
2405 "Unable to instantiate service " + data.info.name
2406 + ": " + e.toString(), e);
2407 }
2408 }
2409
2410 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002411 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412
Dianne Hackborn21556372010-02-04 16:34:40 -08002413 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 context.init(packageInfo, null, this);
2415
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002416 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 context.setOuterContext(service);
2418 service.attach(context, this, data.info.name, data.token, app,
2419 ActivityManagerNative.getDefault());
2420 service.onCreate();
2421 mServices.put(data.token, service);
2422 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002423 ActivityManagerNative.getDefault().serviceDoneExecuting(
2424 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 } catch (RemoteException e) {
2426 // nothing to do.
2427 }
2428 } catch (Exception e) {
2429 if (!mInstrumentation.onException(service, e)) {
2430 throw new RuntimeException(
2431 "Unable to create service " + data.info.name
2432 + ": " + e.toString(), e);
2433 }
2434 }
2435 }
2436
Romain Guy65b345f2011-07-27 18:51:50 -07002437 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002439 if (DEBUG_SERVICE)
2440 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 if (s != null) {
2442 try {
2443 data.intent.setExtrasClassLoader(s.getClassLoader());
2444 try {
2445 if (!data.rebind) {
2446 IBinder binder = s.onBind(data.intent);
2447 ActivityManagerNative.getDefault().publishService(
2448 data.token, data.intent, binder);
2449 } else {
2450 s.onRebind(data.intent);
2451 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002452 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002454 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 } catch (RemoteException ex) {
2456 }
2457 } catch (Exception e) {
2458 if (!mInstrumentation.onException(s, e)) {
2459 throw new RuntimeException(
2460 "Unable to bind to service " + s
2461 + " with " + data.intent + ": " + e.toString(), e);
2462 }
2463 }
2464 }
2465 }
2466
Romain Guy65b345f2011-07-27 18:51:50 -07002467 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 Service s = mServices.get(data.token);
2469 if (s != null) {
2470 try {
2471 data.intent.setExtrasClassLoader(s.getClassLoader());
2472 boolean doRebind = s.onUnbind(data.intent);
2473 try {
2474 if (doRebind) {
2475 ActivityManagerNative.getDefault().unbindFinished(
2476 data.token, data.intent, doRebind);
2477 } else {
2478 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002479 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 }
2481 } catch (RemoteException ex) {
2482 }
2483 } catch (Exception e) {
2484 if (!mInstrumentation.onException(s, e)) {
2485 throw new RuntimeException(
2486 "Unable to unbind to service " + s
2487 + " with " + data.intent + ": " + e.toString(), e);
2488 }
2489 }
2490 }
2491 }
2492
Dianne Hackborn625ac272010-09-17 18:29:22 -07002493 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002494 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2495 try {
2496 Service s = mServices.get(info.token);
2497 if (s != null) {
2498 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2499 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2500 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002502 } finally {
2503 IoUtils.closeQuietly(info.fd);
2504 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 }
2506 }
2507
Dianne Hackborn625ac272010-09-17 18:29:22 -07002508 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002509 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2510 try {
2511 ActivityClientRecord r = mActivities.get(info.token);
2512 if (r != null && r.activity != null) {
2513 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2514 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2515 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002516 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002517 } finally {
2518 IoUtils.closeQuietly(info.fd);
2519 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002520 }
2521 }
2522
Marco Nelissen18cb2872011-11-15 11:19:53 -08002523 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002524 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2525 try {
2526 ProviderClientRecord r = mLocalProviders.get(info.token);
2527 if (r != null && r.mLocalProvider != null) {
2528 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2529 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2530 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002531 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002532 } finally {
2533 IoUtils.closeQuietly(info.fd);
2534 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002535 }
2536 }
2537
Romain Guy65b345f2011-07-27 18:51:50 -07002538 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 Service s = mServices.get(data.token);
2540 if (s != null) {
2541 try {
2542 if (data.args != null) {
2543 data.args.setExtrasClassLoader(s.getClassLoader());
2544 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002545 int res;
2546 if (!data.taskRemoved) {
2547 res = s.onStartCommand(data.args, data.flags, data.startId);
2548 } else {
2549 s.onTaskRemoved(data.args);
2550 res = Service.START_TASK_REMOVED_COMPLETE;
2551 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002552
2553 QueuedWork.waitToFinish();
2554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002556 ActivityManagerNative.getDefault().serviceDoneExecuting(
2557 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 } catch (RemoteException e) {
2559 // nothing to do.
2560 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002561 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 } catch (Exception e) {
2563 if (!mInstrumentation.onException(s, e)) {
2564 throw new RuntimeException(
2565 "Unable to start service " + s
2566 + " with " + data.args + ": " + e.toString(), e);
2567 }
2568 }
2569 }
2570 }
2571
Romain Guy65b345f2011-07-27 18:51:50 -07002572 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 Service s = mServices.remove(token);
2574 if (s != null) {
2575 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002576 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 s.onDestroy();
2578 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002579 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002581 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002583
2584 QueuedWork.waitToFinish();
2585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002587 ActivityManagerNative.getDefault().serviceDoneExecuting(
2588 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 } catch (RemoteException e) {
2590 // nothing to do.
2591 }
2592 } catch (Exception e) {
2593 if (!mInstrumentation.onException(s, e)) {
2594 throw new RuntimeException(
2595 "Unable to stop service " + s
2596 + ": " + e.toString(), e);
2597 }
2598 }
2599 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002600 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 }
2602
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002603 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002605 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002606 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 + " finished=" + r.activity.mFinished);
2608 if (r != null && !r.activity.mFinished) {
2609 if (clearHide) {
2610 r.hideForNow = false;
2611 r.activity.mStartedActivity = false;
2612 }
2613 try {
2614 if (r.pendingIntents != null) {
2615 deliverNewIntents(r, r.pendingIntents);
2616 r.pendingIntents = null;
2617 }
2618 if (r.pendingResults != null) {
2619 deliverResults(r, r.pendingResults);
2620 r.pendingResults = null;
2621 }
2622 r.activity.performResume();
2623
Bob Leee5408332009-09-04 18:31:17 -07002624 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 r.paused = false;
2628 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 r.state = null;
2630 } catch (Exception e) {
2631 if (!mInstrumentation.onException(r.activity, e)) {
2632 throw new RuntimeException(
2633 "Unable to resume activity "
2634 + r.intent.getComponent().toShortString()
2635 + ": " + e.toString(), e);
2636 }
2637 }
2638 }
2639 return r;
2640 }
2641
Romain Guya998dff2012-03-23 18:58:36 -07002642 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002643 if (r.mPendingRemoveWindow != null) {
2644 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2645 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2646 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07002647 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002648 r.activity.getClass().getName(), "Activity");
2649 }
2650 }
2651 r.mPendingRemoveWindow = null;
2652 r.mPendingRemoveWindowManager = null;
2653 }
2654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2656 // If we are getting ready to gc after going to the background, well
2657 // we are back active so skip it.
2658 unscheduleGcIdler();
2659
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002660 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661
2662 if (r != null) {
2663 final Activity a = r.activity;
2664
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002665 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 TAG, "Resume " + r + " started activity: " +
2667 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2668 + ", finished: " + a.mFinished);
2669
2670 final int forwardBit = isForward ?
2671 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 // If the window hasn't yet been added to the window manager,
2674 // and this guy didn't finish itself or start another activity,
2675 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002676 boolean willBeVisible = !a.mStartedActivity;
2677 if (!willBeVisible) {
2678 try {
2679 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2680 a.getActivityToken());
2681 } catch (RemoteException e) {
2682 }
2683 }
2684 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 r.window = r.activity.getWindow();
2686 View decor = r.window.getDecorView();
2687 decor.setVisibility(View.INVISIBLE);
2688 ViewManager wm = a.getWindowManager();
2689 WindowManager.LayoutParams l = r.window.getAttributes();
2690 a.mDecor = decor;
2691 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2692 l.softInputMode |= forwardBit;
2693 if (a.mVisibleFromClient) {
2694 a.mWindowAdded = true;
2695 wm.addView(decor, l);
2696 }
2697
2698 // If the window has already been added, but during resume
2699 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002700 // window visible.
2701 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002702 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 TAG, "Launch " + r + " mStartedActivity set");
2704 r.hideForNow = true;
2705 }
2706
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002707 // Get rid of anything left hanging around.
2708 cleanUpPendingRemoveWindows(r);
2709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 // The window is now visible if it has been added, we are not
2711 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002712 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002713 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002715 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002716 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07002718 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 r.newConfig = null;
2720 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002721 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 + isForward);
2723 WindowManager.LayoutParams l = r.window.getAttributes();
2724 if ((l.softInputMode
2725 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2726 != forwardBit) {
2727 l.softInputMode = (l.softInputMode
2728 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2729 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002730 if (r.activity.mVisibleFromClient) {
2731 ViewManager wm = a.getWindowManager();
2732 View decor = r.window.getDecorView();
2733 wm.updateViewLayout(decor, l);
2734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 }
2736 r.activity.mVisibleFromServer = true;
2737 mNumVisibleActivities++;
2738 if (r.activity.mVisibleFromClient) {
2739 r.activity.makeVisible();
2740 }
2741 }
2742
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002743 if (!r.onlyLocalRequest) {
2744 r.nextIdle = mNewActivities;
2745 mNewActivities = r;
2746 if (localLOGV) Slog.v(
2747 TAG, "Scheduling idle handler for " + r);
2748 Looper.myQueue().addIdleHandler(new Idler());
2749 }
2750 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751
2752 } else {
2753 // If an exception was thrown when trying to resume, then
2754 // just end this activity.
2755 try {
2756 ActivityManagerNative.getDefault()
2757 .finishActivity(token, Activity.RESULT_CANCELED, null);
2758 } catch (RemoteException ex) {
2759 }
2760 }
2761 }
2762
2763 private int mThumbnailWidth = -1;
2764 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002765 private Bitmap mAvailThumbnailBitmap = null;
2766 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767
Romain Guy65b345f2011-07-27 18:51:50 -07002768 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002769 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002771 if (thumbnail == null) {
2772 int w = mThumbnailWidth;
2773 int h;
2774 if (w < 0) {
2775 Resources res = r.activity.getResources();
2776 mThumbnailHeight = h =
2777 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002779 mThumbnailWidth = w =
2780 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2781 } else {
2782 h = mThumbnailHeight;
2783 }
2784
2785 // On platforms where we don't want thumbnails, set dims to (0,0)
2786 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07002787 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
2788 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002789 thumbnail.eraseColor(0);
2790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 }
2792
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002793 if (thumbnail != null) {
2794 Canvas cv = mThumbnailCanvas;
2795 if (cv == null) {
2796 mThumbnailCanvas = cv = new Canvas();
2797 }
2798
2799 cv.setBitmap(thumbnail);
2800 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2801 mAvailThumbnailBitmap = thumbnail;
2802 thumbnail = null;
2803 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002804 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 } catch (Exception e) {
2808 if (!mInstrumentation.onException(r.activity, e)) {
2809 throw new RuntimeException(
2810 "Unable to create thumbnail of "
2811 + r.intent.getComponent().toShortString()
2812 + ": " + e.toString(), e);
2813 }
2814 thumbnail = null;
2815 }
2816
2817 return thumbnail;
2818 }
2819
Romain Guy65b345f2011-07-27 18:51:50 -07002820 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002822 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002824 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 if (userLeaving) {
2826 performUserLeavingActivity(r);
2827 }
Bob Leee5408332009-09-04 18:31:17 -07002828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002830 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002832 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002833 if (r.isPreHoneycomb()) {
2834 QueuedWork.waitToFinish();
2835 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 // Tell the activity manager we have paused.
2838 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002839 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 } catch (RemoteException ex) {
2841 }
2842 }
2843 }
2844
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002845 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 mInstrumentation.callActivityOnUserLeaving(r.activity);
2847 }
2848
2849 final Bundle performPauseActivity(IBinder token, boolean finished,
2850 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002851 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 return r != null ? performPauseActivity(r, finished, saveState) : null;
2853 }
2854
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002855 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 boolean saveState) {
2857 if (r.paused) {
2858 if (r.activity.mFinished) {
2859 // If we are finishing, we won't call onResume() in certain cases.
2860 // So here we likewise don't want to call onPause() if the activity
2861 // isn't resumed.
2862 return null;
2863 }
2864 RuntimeException e = new RuntimeException(
2865 "Performing pause of activity that is not resumed: "
2866 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002867 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 }
2869 Bundle state = null;
2870 if (finished) {
2871 r.activity.mFinished = true;
2872 }
2873 try {
2874 // Next have the activity save its current state and managed dialogs...
2875 if (!r.activity.mFinished && saveState) {
2876 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002877 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2879 r.state = state;
2880 }
2881 // Now we are idle.
2882 r.activity.mCalled = false;
2883 mInstrumentation.callActivityOnPause(r.activity);
2884 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2885 if (!r.activity.mCalled) {
2886 throw new SuperNotCalledException(
2887 "Activity " + r.intent.getComponent().toShortString() +
2888 " did not call through to super.onPause()");
2889 }
2890
2891 } catch (SuperNotCalledException e) {
2892 throw e;
2893
2894 } catch (Exception e) {
2895 if (!mInstrumentation.onException(r.activity, e)) {
2896 throw new RuntimeException(
2897 "Unable to pause activity "
2898 + r.intent.getComponent().toShortString()
2899 + ": " + e.toString(), e);
2900 }
2901 }
2902 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002903
2904 // Notify any outstanding on paused listeners
2905 ArrayList<OnActivityPausedListener> listeners;
2906 synchronized (mOnPauseListeners) {
2907 listeners = mOnPauseListeners.remove(r.activity);
2908 }
2909 int size = (listeners != null ? listeners.size() : 0);
2910 for (int i = 0; i < size; i++) {
2911 listeners.get(i).onPaused(r.activity);
2912 }
2913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 return state;
2915 }
2916
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002917 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002918 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002919 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 }
2921
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002922 private static class StopInfo implements Runnable {
2923 ActivityClientRecord activity;
2924 Bundle state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 Bitmap thumbnail;
2926 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002927
2928 @Override public void run() {
2929 // Tell activity manager we have been stopped.
2930 try {
2931 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
2932 ActivityManagerNative.getDefault().activityStopped(
2933 activity.token, state, thumbnail, description);
2934 } catch (RemoteException ex) {
2935 }
2936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 }
2938
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002939 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002940 public final IActivityManager.ContentProviderHolder holder;
2941 public final ProviderClientRecord client;
2942 public int stableCount;
2943 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002944
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002945 // When this is set, the stable and unstable ref counts are 0 and
2946 // we have a pending operation scheduled to remove the ref count
2947 // from the activity manager. On the activity manager we are still
2948 // holding an unstable ref, though it is not reflected in the counts
2949 // here.
2950 public boolean removePending;
2951
2952 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
2953 ProviderClientRecord inClient, int sCount, int uCount) {
2954 holder = inHolder;
2955 client = inClient;
2956 stableCount = sCount;
2957 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 }
2959 }
2960
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002961 /**
2962 * Core implementation of stopping an activity. Note this is a little
2963 * tricky because the server's meaning of stop is slightly different
2964 * than our client -- for the server, stop means to save state and give
2965 * it the result when it is done, but the window may still be visible.
2966 * For the client, we want to call onStop()/onStart() to indicate when
2967 * the activity's UI visibillity changes.
2968 */
Romain Guy65b345f2011-07-27 18:51:50 -07002969 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002970 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002971 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002972 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 if (r != null) {
2974 if (!keepShown && r.stopped) {
2975 if (r.activity.mFinished) {
2976 // If we are finishing, we won't call onResume() in certain
2977 // cases. So here we likewise don't want to call onStop()
2978 // if the activity isn't resumed.
2979 return;
2980 }
2981 RuntimeException e = new RuntimeException(
2982 "Performing stop of activity that is not resumed: "
2983 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002984 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 }
2986
2987 if (info != null) {
2988 try {
2989 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002990 // For now, don't create the thumbnail here; we are
2991 // doing that by doing a screen snapshot.
2992 info.thumbnail = null; //createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 info.description = r.activity.onCreateDescription();
2994 } catch (Exception e) {
2995 if (!mInstrumentation.onException(r.activity, e)) {
2996 throw new RuntimeException(
2997 "Unable to save state of activity "
2998 + r.intent.getComponent().toShortString()
2999 + ": " + e.toString(), e);
3000 }
3001 }
3002 }
3003
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003004 // Next have the activity save its current state and managed dialogs...
3005 if (!r.activity.mFinished && saveState) {
3006 if (r.state == null) {
3007 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003008 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003009 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
3010 r.state = state;
3011 } else {
3012 state = r.state;
3013 }
3014 }
3015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 if (!keepShown) {
3017 try {
3018 // Now we are idle.
3019 r.activity.performStop();
3020 } catch (Exception e) {
3021 if (!mInstrumentation.onException(r.activity, e)) {
3022 throw new RuntimeException(
3023 "Unable to stop activity "
3024 + r.intent.getComponent().toShortString()
3025 + ": " + e.toString(), e);
3026 }
3027 }
3028 r.stopped = true;
3029 }
3030
3031 r.paused = true;
3032 }
3033 }
3034
Romain Guy65b345f2011-07-27 18:51:50 -07003035 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 View v = r.activity.mDecor;
3037 if (v != null) {
3038 if (show) {
3039 if (!r.activity.mVisibleFromServer) {
3040 r.activity.mVisibleFromServer = true;
3041 mNumVisibleActivities++;
3042 if (r.activity.mVisibleFromClient) {
3043 r.activity.makeVisible();
3044 }
3045 }
3046 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003047 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003048 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003050 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 r.newConfig = null;
3052 }
3053 } else {
3054 if (r.activity.mVisibleFromServer) {
3055 r.activity.mVisibleFromServer = false;
3056 mNumVisibleActivities--;
3057 v.setVisibility(View.INVISIBLE);
3058 }
3059 }
3060 }
3061 }
3062
Romain Guy65b345f2011-07-27 18:51:50 -07003063 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003064 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 r.activity.mConfigChangeFlags |= configChanges;
3066
3067 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003068 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003070 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 TAG, "Finishing stop of " + r + ": show=" + show
3072 + " win=" + r.window);
3073
3074 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003075
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003076 // Make sure any pending writes are now committed.
3077 if (!r.isPreHoneycomb()) {
3078 QueuedWork.waitToFinish();
3079 }
3080
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003081 // Schedule the call to tell the activity manager we have
3082 // stopped. We don't do this immediately, because we want to
3083 // have a chance for any other pending work (in particular memory
3084 // trim requests) to complete before you tell the activity
3085 // manager to proceed and allow us to go fully into the background.
3086 info.activity = r;
3087 info.state = r.state;
3088 mH.post(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 }
3090
3091 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003092 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 if (r.stopped) {
3094 r.activity.performRestart();
3095 r.stopped = false;
3096 }
3097 }
3098
Romain Guy65b345f2011-07-27 18:51:50 -07003099 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003100 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003101
3102 if (r == null) {
3103 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3104 return;
3105 }
3106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003108 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 } else if (show && r.stopped) {
3110 // If we are getting ready to gc after going to the background, well
3111 // we are back active so skip it.
3112 unscheduleGcIdler();
3113
3114 r.activity.performRestart();
3115 r.stopped = false;
3116 }
3117 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003118 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 TAG, "Handle window " + r + " visibility: " + show);
3120 updateVisibility(r, show);
3121 }
3122 }
3123
Romain Guy65b345f2011-07-27 18:51:50 -07003124 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003125 ActivityClientRecord r = mActivities.get(token);
3126
3127 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003128 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003129 return;
3130 }
3131
3132 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003133 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003134 try {
3135 // Now we are idle.
3136 r.activity.performStop();
3137 } catch (Exception e) {
3138 if (!mInstrumentation.onException(r.activity, e)) {
3139 throw new RuntimeException(
3140 "Unable to stop activity "
3141 + r.intent.getComponent().toShortString()
3142 + ": " + e.toString(), e);
3143 }
3144 }
3145 r.stopped = true;
3146 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003147
3148 // Make sure any pending writes are now committed.
3149 if (!r.isPreHoneycomb()) {
3150 QueuedWork.waitToFinish();
3151 }
3152
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003153 // Tell activity manager we slept.
3154 try {
3155 ActivityManagerNative.getDefault().activitySlept(r.token);
3156 } catch (RemoteException ex) {
3157 }
3158 } else {
3159 if (r.stopped && r.activity.mVisibleFromServer) {
3160 r.activity.performRestart();
3161 r.stopped = false;
3162 }
3163 }
3164 }
3165
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003166 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003167 synchronized (mPackages) {
3168 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003169 }
3170 }
3171
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003172 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3173 LoadedApk apk = peekPackageInfo(data.pkg, false);
3174 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003175 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003176 }
3177 apk = peekPackageInfo(data.pkg, true);
3178 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003179 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003180 }
3181 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07003182 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003183 }
3184
Romain Guy65b345f2011-07-27 18:51:50 -07003185 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 final int N = results.size();
3187 for (int i=0; i<N; i++) {
3188 ResultInfo ri = results.get(i);
3189 try {
3190 if (ri.mData != null) {
3191 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
3192 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003193 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003194 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195 r.activity.dispatchActivityResult(ri.mResultWho,
3196 ri.mRequestCode, ri.mResultCode, ri.mData);
3197 } catch (Exception e) {
3198 if (!mInstrumentation.onException(r.activity, e)) {
3199 throw new RuntimeException(
3200 "Failure delivering result " + ri + " to activity "
3201 + r.intent.getComponent().toShortString()
3202 + ": " + e.toString(), e);
3203 }
3204 }
3205 }
3206 }
3207
Romain Guy65b345f2011-07-27 18:51:50 -07003208 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003209 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003210 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 if (r != null) {
3212 final boolean resumed = !r.paused;
3213 if (!r.activity.mFinished && r.activity.mDecor != null
3214 && r.hideForNow && resumed) {
3215 // We had hidden the activity because it started another
3216 // one... we have gotten a result back and we are not
3217 // paused, so make sure our window is visible.
3218 updateVisibility(r, true);
3219 }
3220 if (resumed) {
3221 try {
3222 // Now we are idle.
3223 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003224 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 mInstrumentation.callActivityOnPause(r.activity);
3226 if (!r.activity.mCalled) {
3227 throw new SuperNotCalledException(
3228 "Activity " + r.intent.getComponent().toShortString()
3229 + " did not call through to super.onPause()");
3230 }
3231 } catch (SuperNotCalledException e) {
3232 throw e;
3233 } catch (Exception e) {
3234 if (!mInstrumentation.onException(r.activity, e)) {
3235 throw new RuntimeException(
3236 "Unable to pause activity "
3237 + r.intent.getComponent().toShortString()
3238 + ": " + e.toString(), e);
3239 }
3240 }
3241 }
3242 deliverResults(r, res.results);
3243 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003244 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003245 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 }
3247 }
3248 }
3249
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003250 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 return performDestroyActivity(token, finishing, 0, false);
3252 }
3253
Romain Guy65b345f2011-07-27 18:51:50 -07003254 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003256 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003257 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003258 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003260 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 r.activity.mConfigChangeFlags |= configChanges;
3262 if (finishing) {
3263 r.activity.mFinished = true;
3264 }
3265 if (!r.paused) {
3266 try {
3267 r.activity.mCalled = false;
3268 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07003269 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 r.activity.getComponentName().getClassName());
3271 if (!r.activity.mCalled) {
3272 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003273 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 + " did not call through to super.onPause()");
3275 }
3276 } catch (SuperNotCalledException e) {
3277 throw e;
3278 } catch (Exception e) {
3279 if (!mInstrumentation.onException(r.activity, e)) {
3280 throw new RuntimeException(
3281 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003282 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 + ": " + e.toString(), e);
3284 }
3285 }
3286 r.paused = true;
3287 }
3288 if (!r.stopped) {
3289 try {
3290 r.activity.performStop();
3291 } catch (SuperNotCalledException e) {
3292 throw e;
3293 } catch (Exception e) {
3294 if (!mInstrumentation.onException(r.activity, e)) {
3295 throw new RuntimeException(
3296 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003297 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 + ": " + e.toString(), e);
3299 }
3300 }
3301 r.stopped = true;
3302 }
3303 if (getNonConfigInstance) {
3304 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003305 r.lastNonConfigurationInstances
3306 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307 } catch (Exception e) {
3308 if (!mInstrumentation.onException(r.activity, e)) {
3309 throw new RuntimeException(
3310 "Unable to retain activity "
3311 + r.intent.getComponent().toShortString()
3312 + ": " + e.toString(), e);
3313 }
3314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003315 }
3316 try {
3317 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003318 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 if (!r.activity.mCalled) {
3320 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003321 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 " did not call through to super.onDestroy()");
3323 }
3324 if (r.window != null) {
3325 r.window.closeAllPanels();
3326 }
3327 } catch (SuperNotCalledException e) {
3328 throw e;
3329 } catch (Exception e) {
3330 if (!mInstrumentation.onException(r.activity, e)) {
3331 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003332 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3333 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 }
3335 }
3336 }
3337 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003338 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 return r;
3340 }
3341
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003342 private static String safeToComponentShortString(Intent intent) {
3343 ComponentName component = intent.getComponent();
3344 return component == null ? "[Unknown]" : component.toShortString();
3345 }
3346
Romain Guy65b345f2011-07-27 18:51:50 -07003347 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003349 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 configChanges, getNonConfigInstance);
3351 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003352 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 WindowManager wm = r.activity.getWindowManager();
3354 View v = r.activity.mDecor;
3355 if (v != null) {
3356 if (r.activity.mVisibleFromServer) {
3357 mNumVisibleActivities--;
3358 }
3359 IBinder wtoken = v.getWindowToken();
3360 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003361 if (r.onlyLocalRequest) {
3362 // Hold off on removing this until the new activity's
3363 // window is being added.
3364 r.mPendingRemoveWindow = v;
3365 r.mPendingRemoveWindowManager = wm;
3366 } else {
3367 wm.removeViewImmediate(v);
3368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003370 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003371 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 r.activity.getClass().getName(), "Activity");
3373 }
3374 r.activity.mDecor = null;
3375 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003376 if (r.mPendingRemoveWindow == null) {
3377 // If we are delaying the removal of the activity window, then
3378 // we can't clean up all windows here. Note that we can't do
3379 // so later either, which means any windows that aren't closed
3380 // by the app will leak. Well we try to warning them a lot
3381 // about leaking windows, because that is a bug, so if they are
3382 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07003383 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003384 r.activity.getClass().getName(), "Activity");
3385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386
3387 // Mocked out contexts won't be participating in the normal
3388 // process lifecycle, but if we're running with a proper
3389 // ApplicationContext we need to have it tear down things
3390 // cleanly.
3391 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003392 if (c instanceof ContextImpl) {
3393 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 r.activity.getClass().getName(), "Activity");
3395 }
3396 }
3397 if (finishing) {
3398 try {
3399 ActivityManagerNative.getDefault().activityDestroyed(token);
3400 } catch (RemoteException ex) {
3401 // If the system process has died, it's game over for everyone.
3402 }
3403 }
3404 }
3405
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003406 public final void requestRelaunchActivity(IBinder token,
3407 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3408 int configChanges, boolean notResumed, Configuration config,
3409 boolean fromServer) {
3410 ActivityClientRecord target = null;
3411
3412 synchronized (mPackages) {
3413 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3414 ActivityClientRecord r = mRelaunchingActivities.get(i);
3415 if (r.token == token) {
3416 target = r;
3417 if (pendingResults != null) {
3418 if (r.pendingResults != null) {
3419 r.pendingResults.addAll(pendingResults);
3420 } else {
3421 r.pendingResults = pendingResults;
3422 }
3423 }
3424 if (pendingNewIntents != null) {
3425 if (r.pendingIntents != null) {
3426 r.pendingIntents.addAll(pendingNewIntents);
3427 } else {
3428 r.pendingIntents = pendingNewIntents;
3429 }
3430 }
3431 break;
3432 }
3433 }
3434
3435 if (target == null) {
3436 target = new ActivityClientRecord();
3437 target.token = token;
3438 target.pendingResults = pendingResults;
3439 target.pendingIntents = pendingNewIntents;
3440 if (!fromServer) {
3441 ActivityClientRecord existing = mActivities.get(token);
3442 if (existing != null) {
3443 target.startsNotResumed = existing.paused;
3444 }
3445 target.onlyLocalRequest = true;
3446 }
3447 mRelaunchingActivities.add(target);
3448 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3449 }
3450
3451 if (fromServer) {
3452 target.startsNotResumed = notResumed;
3453 target.onlyLocalRequest = false;
3454 }
3455 if (config != null) {
3456 target.createdConfig = config;
3457 }
3458 target.pendingConfigChanges |= configChanges;
3459 }
3460 }
3461
Romain Guy65b345f2011-07-27 18:51:50 -07003462 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 // If we are getting ready to gc after going to the background, well
3464 // we are back active so skip it.
3465 unscheduleGcIdler();
3466
3467 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003468 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 // First: make sure we have the most recent configuration and most
3471 // recent version of the activity, or skip it if some previous call
3472 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003473 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003474 int N = mRelaunchingActivities.size();
3475 IBinder token = tmp.token;
3476 tmp = null;
3477 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003478 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 if (r.token == token) {
3480 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003481 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 mRelaunchingActivities.remove(i);
3483 i--;
3484 N--;
3485 }
3486 }
Bob Leee5408332009-09-04 18:31:17 -07003487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003489 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 return;
3491 }
Bob Leee5408332009-09-04 18:31:17 -07003492
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003493 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3494 + tmp.token + " with configChanges=0x"
3495 + Integer.toHexString(configChanges));
3496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 if (mPendingConfiguration != null) {
3498 changedConfig = mPendingConfiguration;
3499 mPendingConfiguration = null;
3500 }
3501 }
Bob Leee5408332009-09-04 18:31:17 -07003502
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003503 if (tmp.createdConfig != null) {
3504 // If the activity manager is passing us its current config,
3505 // assume that is really what we want regardless of what we
3506 // may have pending.
3507 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003508 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3509 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3510 if (changedConfig == null
3511 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3512 changedConfig = tmp.createdConfig;
3513 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003514 }
3515 }
3516
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003517 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003518 + tmp.token + ": changedConfig=" + changedConfig);
3519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 // If there was a pending configuration change, execute it first.
3521 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003522 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003523 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003524 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 }
Bob Leee5408332009-09-04 18:31:17 -07003526
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003527 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003528 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003529 if (r == null) {
3530 return;
3531 }
Bob Leee5408332009-09-04 18:31:17 -07003532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003534 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003535 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003536
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003537 r.activity.mChangingConfigurations = true;
3538
Dianne Hackborne2b04802010-12-09 09:24:55 -08003539 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003541 performPauseActivity(r.token, false, r.isPreHoneycomb());
3542 }
3543 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3544 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003545 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003546 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 }
Bob Leee5408332009-09-04 18:31:17 -07003548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 r.activity = null;
3552 r.window = null;
3553 r.hideForNow = false;
3554 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003555 // Merge any pending results and pending intents; don't just replace them
3556 if (tmp.pendingResults != null) {
3557 if (r.pendingResults == null) {
3558 r.pendingResults = tmp.pendingResults;
3559 } else {
3560 r.pendingResults.addAll(tmp.pendingResults);
3561 }
3562 }
3563 if (tmp.pendingIntents != null) {
3564 if (r.pendingIntents == null) {
3565 r.pendingIntents = tmp.pendingIntents;
3566 } else {
3567 r.pendingIntents.addAll(tmp.pendingIntents);
3568 }
3569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003571
Christopher Tateb70f3df2009-04-07 16:07:59 -07003572 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003573 }
3574
Romain Guy65b345f2011-07-27 18:51:50 -07003575 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003576 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 Bitmap thumbnail = createThumbnailBitmap(r);
3578 CharSequence description = null;
3579 try {
3580 description = r.activity.onCreateDescription();
3581 } catch (Exception e) {
3582 if (!mInstrumentation.onException(r.activity, e)) {
3583 throw new RuntimeException(
3584 "Unable to create description of activity "
3585 + r.intent.getComponent().toShortString()
3586 + ": " + e.toString(), e);
3587 }
3588 }
3589 //System.out.println("Reporting top thumbnail " + thumbnail);
3590 try {
3591 ActivityManagerNative.getDefault().reportThumbnail(
3592 token, thumbnail, description);
3593 } catch (RemoteException ex) {
3594 }
3595 }
3596
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003597 ArrayList<ComponentCallbacks2> collectComponentCallbacksLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003599 ArrayList<ComponentCallbacks2> callbacks
3600 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003602 if (mActivities.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003603 for (ActivityClientRecord ar : mActivities.values()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003604 Activity a = ar.activity;
3605 if (a != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003606 Configuration thisConfig = applyConfigCompatMainThread(mCurDefaultDisplayDpi,
3607 newConfig, ar.packageInfo.mCompatibilityInfo.getIfNeeded());
Romain Guya998dff2012-03-23 18:58:36 -07003608 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609 // If the activity is currently resumed, its configuration
3610 // needs to change right now.
3611 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003612 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003613 // Otherwise, we will tell it about the change
3614 // the next time it is resumed or shown. Note that
3615 // the activity manager may, before then, decide the
3616 // activity needs to be destroyed to handle its new
3617 // configuration.
Romain Guya998dff2012-03-23 18:58:36 -07003618 if (DEBUG_CONFIGURATION) {
3619 Slog.v(TAG, "Setting activity "
3620 + ar.activityInfo.name + " newConfig=" + thisConfig);
3621 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003622 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 }
3624 }
3625 }
3626 }
3627 if (mServices.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003628 for (Service service : mServices.values()) {
3629 callbacks.add(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 }
3631 }
3632 synchronized (mProviderMap) {
3633 if (mLocalProviders.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003634 for (ProviderClientRecord providerClientRecord : mLocalProviders.values()) {
3635 callbacks.add(providerClientRecord.mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003636 }
3637 }
3638 }
3639 final int N = mAllApplications.size();
3640 for (int i=0; i<N; i++) {
3641 callbacks.add(mAllApplications.get(i));
3642 }
Bob Leee5408332009-09-04 18:31:17 -07003643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644 return callbacks;
3645 }
Bob Leee5408332009-09-04 18:31:17 -07003646
Romain Guya998dff2012-03-23 18:58:36 -07003647 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003649 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003650 // we check the runtime type and act accordingly.
3651 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3652 if (activity != null) {
3653 activity.mCalled = false;
3654 }
Bob Leee5408332009-09-04 18:31:17 -07003655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003656 boolean shouldChangeConfig = false;
3657 if ((activity == null) || (activity.mCurrentConfig == null)) {
3658 shouldChangeConfig = true;
3659 } else {
Bob Leee5408332009-09-04 18:31:17 -07003660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661 // If the new config is the same as the config this Activity
3662 // is already running with then don't bother calling
3663 // onConfigurationChanged
3664 int diff = activity.mCurrentConfig.diff(config);
3665 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003666 // If this activity doesn't handle any of the config changes
3667 // then don't bother calling onConfigurationChanged as we're
3668 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003669 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 shouldChangeConfig = true;
3671 }
3672 }
3673 }
Bob Leee5408332009-09-04 18:31:17 -07003674
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003675 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003676 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003677 if (shouldChangeConfig) {
3678 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 if (activity != null) {
3681 if (!activity.mCalled) {
3682 throw new SuperNotCalledException(
3683 "Activity " + activity.getLocalClassName() +
3684 " did not call through to super.onConfigurationChanged()");
3685 }
3686 activity.mConfigChangeFlags = 0;
3687 activity.mCurrentConfig = new Configuration(config);
3688 }
3689 }
3690 }
3691
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003692 public final void applyConfigurationToResources(Configuration config) {
3693 synchronized (mPackages) {
3694 applyConfigurationToResourcesLocked(config, null);
3695 }
3696 }
3697
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003698 final boolean applyConfigurationToResourcesLocked(Configuration config,
3699 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003700 if (mResConfiguration == null) {
3701 mResConfiguration = new Configuration();
3702 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003703 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003704 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003705 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003706 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003707 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003708 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003709 DisplayMetrics dm = getDisplayMetricsLocked(null, true);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003710
3711 if (compat != null && (mResCompatibilityInfo == null ||
3712 !mResCompatibilityInfo.equals(compat))) {
3713 mResCompatibilityInfo = compat;
3714 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3715 | ActivityInfo.CONFIG_SCREEN_SIZE
3716 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3717 }
Bob Leee5408332009-09-04 18:31:17 -07003718
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003719 // set it for java, this also affects newly created Resources
3720 if (config.locale != null) {
3721 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 }
Bob Leee5408332009-09-04 18:31:17 -07003723
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003724 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003725
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003726 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003727 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborn756220b2012-08-14 16:45:30 -07003728
3729 Configuration tmpConfig = null;
3730
3731 Iterator<Map.Entry<ResourcesKey, WeakReference<Resources>>> it =
3732 mActiveResources.entrySet().iterator();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003733 while (it.hasNext()) {
Dianne Hackborn756220b2012-08-14 16:45:30 -07003734 Map.Entry<ResourcesKey, WeakReference<Resources>> entry = it.next();
3735 Resources r = entry.getValue().get();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003736 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003737 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003738 + r + " config to: " + config);
Dianne Hackborn756220b2012-08-14 16:45:30 -07003739 Configuration override = entry.getKey().mOverrideConfiguration;
3740 if (override != null) {
3741 if (tmpConfig == null) {
3742 tmpConfig = new Configuration();
3743 }
3744 tmpConfig.setTo(config);
3745 tmpConfig.updateFrom(override);
3746 r.updateConfiguration(tmpConfig, dm, compat);
3747 } else {
3748 r.updateConfiguration(config, dm, compat);
3749 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003750 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003751 // + " " + r + ": " + r.getConfiguration());
3752 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003753 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003754 it.remove();
3755 }
3756 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003757
3758 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003759 }
Dianne Hackborn836e2622011-10-04 18:32:39 -07003760
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003761 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07003762 Configuration config = mConfiguration;
3763 if (mCompatConfiguration == null) {
3764 mCompatConfiguration = new Configuration();
3765 }
3766 mCompatConfiguration.setTo(mConfiguration);
3767 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003768 mResCompatibilityInfo.applyToConfiguration(displayDensity, mCompatConfiguration);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003769 config = mCompatConfiguration;
3770 }
3771 return config;
3772 }
3773
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003774 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003775
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003776 ArrayList<ComponentCallbacks2> callbacks = null;
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003777 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003778
3779 synchronized (mPackages) {
3780 if (mPendingConfiguration != null) {
3781 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3782 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003783 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003784 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003785 }
3786 mPendingConfiguration = null;
3787 }
3788
3789 if (config == null) {
3790 return;
3791 }
3792
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003793 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003794 + config);
3795
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003796 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003798 if (mConfiguration == null) {
3799 mConfiguration = new Configuration();
3800 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003801 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003802 return;
3803 }
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003804 configDiff = mConfiguration.diff(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003806 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 callbacks = collectComponentCallbacksLocked(false, config);
3808 }
Romain Guy65b345f2011-07-27 18:51:50 -07003809
3810 // Cleanup hardware accelerated stuff
Jeff Brown98365d72012-08-19 20:30:52 -07003811 WindowManagerGlobal.getInstance().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003812
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003813 freeTextLayoutCachesIfNeeded(configDiff);
3814
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003815 if (callbacks != null) {
3816 final int N = callbacks.size();
3817 for (int i=0; i<N; i++) {
3818 performConfigurationChanged(callbacks.get(i), config);
3819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003820 }
3821 }
3822
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003823 final void freeTextLayoutCachesIfNeeded(int configDiff) {
3824 if (configDiff != 0) {
3825 // Ask text layout engine to free its caches if there is a locale change
3826 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
3827 if (hasLocaleConfigChange) {
3828 Canvas.freeTextLayoutCaches();
3829 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
3830 }
3831 }
3832 }
3833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003835 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 if (r == null || r.activity == null) {
3837 return;
3838 }
Bob Leee5408332009-09-04 18:31:17 -07003839
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003840 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003841 + r.activityInfo.name);
3842
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003843 performConfigurationChanged(r.activity, mCompatConfiguration);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003844
3845 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003846 }
3847
Romain Guy7eabe552011-07-21 14:56:34 -07003848 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003849 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003850 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003851 switch (profileType) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003852 default:
3853 mProfiler.setProfiler(pcd.path, pcd.fd);
3854 mProfiler.autoStopProfiler = false;
3855 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003856 break;
3857 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003858 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003859 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003860 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003861 } finally {
3862 try {
3863 pcd.fd.close();
3864 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003865 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003866 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003867 }
3868 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003869 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07003870 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003871 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003872 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003873 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003874 }
3875 }
Bob Leee5408332009-09-04 18:31:17 -07003876
Romain Guya998dff2012-03-23 18:58:36 -07003877 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07003878 if (managed) {
3879 try {
3880 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3881 } catch (IOException e) {
3882 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3883 + " -- can the process access this path?");
3884 } finally {
3885 try {
3886 dhd.fd.close();
3887 } catch (IOException e) {
3888 Slog.w(TAG, "Failure closing profile fd", e);
3889 }
3890 }
3891 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003892 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003893 }
3894 }
3895
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003896 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3897 boolean hasPkgInfo = false;
3898 if (packages != null) {
3899 for (int i=packages.length-1; i>=0; i--) {
3900 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3901 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003902 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003903 ref = mPackages.get(packages[i]);
3904 if (ref != null && ref.get() != null) {
3905 hasPkgInfo = true;
3906 } else {
3907 ref = mResourcePackages.get(packages[i]);
3908 if (ref != null && ref.get() != null) {
3909 hasPkgInfo = true;
3910 }
3911 }
3912 }
3913 mPackages.remove(packages[i]);
3914 mResourcePackages.remove(packages[i]);
3915 }
3916 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003917 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003918 hasPkgInfo);
3919 }
3920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 final void handleLowMemory() {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003922 ArrayList<ComponentCallbacks2> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003923
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003924 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003925 callbacks = collectComponentCallbacksLocked(true, null);
3926 }
Bob Leee5408332009-09-04 18:31:17 -07003927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 final int N = callbacks.size();
3929 for (int i=0; i<N; i++) {
3930 callbacks.get(i).onLowMemory();
3931 }
3932
Chris Tatece229052009-03-25 16:44:52 -07003933 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3934 if (Process.myUid() != Process.SYSTEM_UID) {
3935 int sqliteReleased = SQLiteDatabase.releaseMemory();
3936 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3937 }
Bob Leee5408332009-09-04 18:31:17 -07003938
Mike Reedcaf0df12009-04-27 14:32:05 -04003939 // Ask graphics to free up as much as possible (font/image caches)
3940 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003942 // Ask text layout engine to free also as much as possible
3943 Canvas.freeTextLayoutCaches();
3944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003945 BinderInternal.forceGc("mem");
3946 }
3947
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003948 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003949 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003950
Jeff Brown98365d72012-08-19 20:30:52 -07003951 final WindowManagerGlobal windowManager = WindowManagerGlobal.getInstance();
Romain Guy19f86e82012-04-23 15:19:07 -07003952 windowManager.startTrimMemory(level);
3953
3954 ArrayList<ComponentCallbacks2> callbacks;
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003955 synchronized (mPackages) {
3956 callbacks = collectComponentCallbacksLocked(true, null);
3957 }
3958
3959 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07003960 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003961 callbacks.get(i).onTrimMemory(level);
3962 }
Romain Guy19f86e82012-04-23 15:19:07 -07003963
Jeff Brown98365d72012-08-19 20:30:52 -07003964 windowManager.endTrimMemory();
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003965 }
3966
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003967 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003968 if (Process.isIsolated()) {
3969 // Isolated processes aren't going to do UI.
3970 return;
3971 }
Romain Guya9582652011-11-10 14:20:10 -08003972 try {
3973 int uid = Process.myUid();
3974 String[] packages = getPackageManager().getPackagesForUid(uid);
3975
3976 // If there are several packages in this application we won't
3977 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003978 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003979 HardwareRenderer.setupDiskCache(cacheDir);
3980 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08003981 }
3982 } catch (RemoteException e) {
3983 // Ignore
3984 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07003985 }
3986
3987 private void updateDefaultDensity() {
3988 if (mCurDefaultDisplayDpi != Configuration.DENSITY_DPI_UNDEFINED
3989 && mCurDefaultDisplayDpi != DisplayMetrics.DENSITY_DEVICE
3990 && !mDensityCompatMode) {
3991 Slog.i(TAG, "Switching default density from "
3992 + DisplayMetrics.DENSITY_DEVICE + " to "
3993 + mCurDefaultDisplayDpi);
3994 DisplayMetrics.DENSITY_DEVICE = mCurDefaultDisplayDpi;
3995 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3996 }
3997 }
3998
Romain Guy65b345f2011-07-27 18:51:50 -07003999 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004000 mBoundApplication = data;
4001 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004002 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004004 mProfiler = new Profiler();
4005 mProfiler.profileFile = data.initProfileFile;
4006 mProfiler.profileFd = data.initProfileFd;
4007 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
4008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08004010 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004011 android.ddm.DdmHandleAppName.setAppName(data.processName);
4012
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004013 if (data.persistent) {
4014 // Persistent processes on low-memory devices do not get to
4015 // use hardware accelerated drawing, since this can add too much
4016 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07004017 if (!ActivityManager.isHighEndGfx()) {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004018 HardwareRenderer.disable(false);
4019 }
4020 }
Romain Guya9582652011-11-10 14:20:10 -08004021
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004022 if (mProfiler.profileFd != null) {
4023 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004024 }
4025
Joe Onoratod630f102011-03-17 18:42:26 -07004026 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
4027 // implementation to use the pool executor. Normally, we use the
4028 // serialized executor as the default. This has to happen in the
4029 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07004030 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07004031 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
4032 }
4033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 /*
4035 * Before spawning a new process, reset the time zone to be the system time zone.
4036 * This needs to be done because the system time zone could have changed after the
4037 * the spawning of this process. Without doing this this process would have the incorrect
4038 * system time zone.
4039 */
4040 TimeZone.setDefault(null);
4041
4042 /*
4043 * Initialize the default locale in this process for the reasons we set the time zone.
4044 */
4045 Locale.setDefault(data.config.locale);
4046
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004047 /*
4048 * Update the system configuration since its preloaded and might not
4049 * reflect configuration changes. The configuration object passed
4050 * in AppBindData can be safely assumed to be up to date
4051 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004052 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004053 mCurDefaultDisplayDpi = data.config.densityDpi;
4054 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004055
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004056 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057
Dianne Hackborndde331c2012-08-03 14:01:57 -07004058 /**
4059 * Switch this process to density compatibility mode if needed.
4060 */
4061 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4062 == 0) {
4063 mDensityCompatMode = true;
4064 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4065 }
4066 updateDefaultDensity();
4067
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004068 final ContextImpl appContext = new ContextImpl();
4069 appContext.init(data.info, null, this);
Amith Yamasani11de39a2012-08-17 18:00:52 -07004070 if (!Process.isIsolated()) {
4071 final File cacheDir = appContext.getCacheDir();
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004072
Amith Yamasani92d57052012-08-23 10:07:52 -07004073 if (cacheDir != null) {
4074 // Provide a usable directory for temporary files
4075 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
4076
4077 setupGraphicsSupport(data.info, cacheDir);
4078 } else {
4079 Log.e(TAG, "Unable to setupGraphicsSupport due to missing cache directory");
4080 }
Amith Yamasani11de39a2012-08-17 18:00:52 -07004081 }
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004082 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004083 * For system applications on userdebug/eng builds, log stack
4084 * traces of disk and network access to dropbox for analysis.
4085 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004086 if ((data.appInfo.flags &
4087 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004088 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4089 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004090 }
4091
4092 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004093 * For apps targetting SDK Honeycomb or later, we don't allow
4094 * network usage on the main event loop / UI thread.
4095 *
4096 * Note to those grepping: this is what ultimately throws
4097 * NetworkOnMainThreadException ...
4098 */
4099 if (data.appInfo.targetSdkVersion > 9) {
4100 StrictMode.enableDeathOnNetwork();
4101 }
4102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4104 // XXX should have option to change the port.
4105 Debug.changeDebugPort(8100);
4106 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004107 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004108 + " is waiting for the debugger on port 8100...");
4109
4110 IActivityManager mgr = ActivityManagerNative.getDefault();
4111 try {
4112 mgr.showWaitingForDebugger(mAppThread, true);
4113 } catch (RemoteException ex) {
4114 }
4115
4116 Debug.waitForDebugger();
4117
4118 try {
4119 mgr.showWaitingForDebugger(mAppThread, false);
4120 } catch (RemoteException ex) {
4121 }
4122
4123 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004124 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004125 + " can be debugged on port 8100...");
4126 }
4127 }
4128
Siva Velusamy92a8b222012-03-09 16:24:04 -08004129 // Enable OpenGL tracing if required
4130 if (data.enableOpenGlTrace) {
4131 GLUtils.enableTracing();
4132 }
4133
Robert Greenwalt434203a2010-10-11 16:00:27 -07004134 /**
4135 * Initialize the default http proxy in this process for the reasons we set the time zone.
4136 */
4137 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004138 if (b != null) {
4139 // In pre-boot mode (doing initial launch to collect password), not
4140 // all system is up. This includes the connectivity service, so don't
4141 // crash if we can't get it.
4142 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4143 try {
4144 ProxyProperties proxyProperties = service.getProxy();
4145 Proxy.setHttpProxySystemProperty(proxyProperties);
4146 } catch (RemoteException e) {}
4147 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 InstrumentationInfo ii = null;
4151 try {
4152 ii = appContext.getPackageManager().
4153 getInstrumentationInfo(data.instrumentationName, 0);
4154 } catch (PackageManager.NameNotFoundException e) {
4155 }
4156 if (ii == null) {
4157 throw new RuntimeException(
4158 "Unable to find instrumentation info for: "
4159 + data.instrumentationName);
4160 }
4161
4162 mInstrumentationAppDir = ii.sourceDir;
Brian Carlstromd893a892012-04-01 21:30:26 -07004163 mInstrumentationAppLibraryDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004164 mInstrumentationAppPackage = ii.packageName;
4165 mInstrumentedAppDir = data.info.getAppDir();
Brian Carlstromd893a892012-04-01 21:30:26 -07004166 mInstrumentedAppLibraryDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004167
4168 ApplicationInfo instrApp = new ApplicationInfo();
4169 instrApp.packageName = ii.packageName;
4170 instrApp.sourceDir = ii.sourceDir;
4171 instrApp.publicSourceDir = ii.publicSourceDir;
4172 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004173 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004174 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08004176 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004177 instrContext.init(pi, null, this);
4178
4179 try {
4180 java.lang.ClassLoader cl = instrContext.getClassLoader();
4181 mInstrumentation = (Instrumentation)
4182 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4183 } catch (Exception e) {
4184 throw new RuntimeException(
4185 "Unable to instantiate instrumentation "
4186 + data.instrumentationName + ": " + e.toString(), e);
4187 }
4188
4189 mInstrumentation.init(this, instrContext, appContext,
4190 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
4191
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004192 if (mProfiler.profileFile != null && !ii.handleProfiling
4193 && mProfiler.profileFd == null) {
4194 mProfiler.handlingProfiling = true;
4195 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004196 file.getParentFile().mkdirs();
4197 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4198 }
4199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004200 } else {
4201 mInstrumentation = new Instrumentation();
4202 }
4203
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004204 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004205 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004206 }
4207
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004208 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004209 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004210 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004211 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004213 // If the app is being launched for full backup or restore, bring it up in
4214 // a restricted environment with the base application class.
4215 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4216 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004217
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004218 // don't bring up providers in restricted mode; they may depend on the
4219 // app's custom Application class
4220 if (!data.restrictedBackupMode) {
4221 List<ProviderInfo> providers = data.providers;
4222 if (providers != null) {
4223 installContentProviders(app, providers);
4224 // For process that contains content providers, we want to
4225 // ensure that the JIT is enabled "at some point".
4226 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4227 }
4228 }
4229
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004230 // Do this after providers, since instrumentation tests generally start their
4231 // test thread at this point, and we don't want that racing.
4232 try {
4233 mInstrumentation.onCreate(data.instrumentationArgs);
4234 }
4235 catch (Exception e) {
4236 throw new RuntimeException(
4237 "Exception thrown in onCreate() of "
4238 + data.instrumentationName + ": " + e.toString(), e);
4239 }
4240
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004241 try {
4242 mInstrumentation.callApplicationOnCreate(app);
4243 } catch (Exception e) {
4244 if (!mInstrumentation.onException(app, e)) {
4245 throw new RuntimeException(
4246 "Unable to create application " + app.getClass().getName()
4247 + ": " + e.toString(), e);
4248 }
4249 }
4250 } finally {
4251 StrictMode.setThreadPolicy(savedPolicy);
4252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004253 }
4254
4255 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4256 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004257 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4258 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004259 Debug.stopMethodTracing();
4260 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004261 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004262 // + ", app thr: " + mAppThread);
4263 try {
4264 am.finishInstrumentation(mAppThread, resultCode, results);
4265 } catch (RemoteException ex) {
4266 }
4267 }
4268
Romain Guy65b345f2011-07-27 18:51:50 -07004269 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004270 Context context, List<ProviderInfo> providers) {
4271 final ArrayList<IActivityManager.ContentProviderHolder> results =
4272 new ArrayList<IActivityManager.ContentProviderHolder>();
4273
Romain Guya998dff2012-03-23 18:58:36 -07004274 for (ProviderInfo cpi : providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004275 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07004276 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 buf.append(cpi.authority);
4278 buf.append(": ");
4279 buf.append(cpi.name);
4280 Log.i(TAG, buf.toString());
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004281 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
4282 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
4283 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004284 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 }
4287 }
4288
4289 try {
4290 ActivityManagerNative.getDefault().publishContentProviders(
4291 getApplicationThread(), results);
4292 } catch (RemoteException ex) {
4293 }
4294 }
4295
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004296 public final IContentProvider acquireProvider(Context c, String name, boolean stable) {
4297 IContentProvider provider = acquireExistingProvider(c, name, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004298 if (provider != null) {
4299 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004300 }
4301
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004302 // There is a possible race here. Another thread may try to acquire
4303 // the same provider at the same time. When this happens, we want to ensure
4304 // that the first one wins.
4305 // Note that we cannot hold the lock while acquiring and installing the
4306 // provider since it might take a long time to run and it could also potentially
4307 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004308 IActivityManager.ContentProviderHolder holder = null;
4309 try {
4310 holder = ActivityManagerNative.getDefault().getContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004311 getApplicationThread(), name, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312 } catch (RemoteException ex) {
4313 }
4314 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004315 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 return null;
4317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004319 // Install provider will increment the reference count for us, and break
4320 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004321 holder = installProvider(c, holder, holder.info,
4322 true /*noisy*/, holder.noReleaseNeeded, stable);
4323 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 }
4325
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004326 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
4327 if (stable) {
4328 prc.stableCount += 1;
4329 if (prc.stableCount == 1) {
4330 // We are acquiring a new stable reference on the provider.
4331 int unstableDelta;
4332 if (prc.removePending) {
4333 // We have a pending remove operation, which is holding the
4334 // last unstable reference. At this point we are converting
4335 // that unstable reference to our new stable reference.
4336 unstableDelta = -1;
4337 // Cancel the removal of the provider.
4338 if (DEBUG_PROVIDER) {
4339 Slog.v(TAG, "incProviderRef: stable "
4340 + "snatched provider from the jaws of death");
4341 }
4342 prc.removePending = false;
4343 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4344 } else {
4345 unstableDelta = 0;
4346 }
4347 try {
4348 if (DEBUG_PROVIDER) {
4349 Slog.v(TAG, "incProviderRef Now stable - "
4350 + prc.holder.info.name + ": unstableDelta="
4351 + unstableDelta);
4352 }
4353 ActivityManagerNative.getDefault().refContentProvider(
4354 prc.holder.connection, 1, unstableDelta);
4355 } catch (RemoteException e) {
4356 //do nothing content provider object is dead any way
4357 }
4358 }
4359 } else {
4360 prc.unstableCount += 1;
4361 if (prc.unstableCount == 1) {
4362 // We are acquiring a new unstable reference on the provider.
4363 if (prc.removePending) {
4364 // Oh look, we actually have a remove pending for the
4365 // provider, which is still holding the last unstable
4366 // reference. We just need to cancel that to take new
4367 // ownership of the reference.
4368 if (DEBUG_PROVIDER) {
4369 Slog.v(TAG, "incProviderRef: unstable "
4370 + "snatched provider from the jaws of death");
4371 }
4372 prc.removePending = false;
4373 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4374 } else {
4375 // First unstable ref, increment our count in the
4376 // activity manager.
4377 try {
4378 if (DEBUG_PROVIDER) {
4379 Slog.v(TAG, "incProviderRef: Now unstable - "
4380 + prc.holder.info.name);
4381 }
4382 ActivityManagerNative.getDefault().refContentProvider(
4383 prc.holder.connection, 0, 1);
4384 } catch (RemoteException e) {
4385 //do nothing content provider object is dead any way
4386 }
4387 }
4388 }
4389 }
4390 }
4391
4392 public final IContentProvider acquireExistingProvider(Context c, String name,
4393 boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004394 synchronized (mProviderMap) {
4395 ProviderClientRecord pr = mProviderMap.get(name);
4396 if (pr == null) {
4397 return null;
4398 }
4399
4400 IContentProvider provider = pr.mProvider;
4401 IBinder jBinder = provider.asBinder();
4402
4403 // Only increment the ref count if we have one. If we don't then the
4404 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004405 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004406 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004407 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004408 }
4409 return provider;
4410 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004411 }
4412
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004413 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
4414 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004415 return false;
4416 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004418 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004419 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004420 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004421 if (prc == null) {
4422 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004423 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004424 }
4425
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004426 boolean lastRef = false;
4427 if (stable) {
4428 if (prc.stableCount == 0) {
4429 if (DEBUG_PROVIDER) Slog.v(TAG,
4430 "releaseProvider: stable ref count already 0, how?");
4431 return false;
4432 }
4433 prc.stableCount -= 1;
4434 if (prc.stableCount == 0) {
4435 // What we do at this point depends on whether there are
4436 // any unstable refs left: if there are, we just tell the
4437 // activity manager to decrement its stable count; if there
4438 // aren't, we need to enqueue this provider to be removed,
4439 // and convert to holding a single unstable ref while
4440 // doing so.
4441 lastRef = prc.unstableCount == 0;
4442 try {
4443 if (DEBUG_PROVIDER) {
4444 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
4445 + lastRef + " - " + prc.holder.info.name);
4446 }
4447 ActivityManagerNative.getDefault().refContentProvider(
4448 prc.holder.connection, -1, lastRef ? 1 : 0);
4449 } catch (RemoteException e) {
4450 //do nothing content provider object is dead any way
4451 }
4452 }
4453 } else {
4454 if (prc.unstableCount == 0) {
4455 if (DEBUG_PROVIDER) Slog.v(TAG,
4456 "releaseProvider: unstable ref count already 0, how?");
4457 return false;
4458 }
4459 prc.unstableCount -= 1;
4460 if (prc.unstableCount == 0) {
4461 // If this is the last reference, we need to enqueue
4462 // this provider to be removed instead of telling the
4463 // activity manager to remove it at this point.
4464 lastRef = prc.stableCount == 0;
4465 if (!lastRef) {
4466 try {
4467 if (DEBUG_PROVIDER) {
4468 Slog.v(TAG, "releaseProvider: No longer unstable - "
4469 + prc.holder.info.name);
4470 }
4471 ActivityManagerNative.getDefault().refContentProvider(
4472 prc.holder.connection, 0, -1);
4473 } catch (RemoteException e) {
4474 //do nothing content provider object is dead any way
4475 }
4476 }
4477 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004478 }
4479
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004480 if (lastRef) {
4481 if (!prc.removePending) {
4482 // Schedule the actual remove asynchronously, since we don't know the context
4483 // this will be called in.
4484 // TODO: it would be nice to post a delayed message, so
4485 // if we come back and need the same provider quickly
4486 // we will still have it available.
4487 if (DEBUG_PROVIDER) {
4488 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
4489 + prc.holder.info.name);
4490 }
4491 prc.removePending = true;
4492 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
4493 mH.sendMessage(msg);
4494 } else {
4495 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
4496 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004497 }
4498 return true;
4499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004500 }
4501
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004502 final void completeRemoveProvider(ProviderRefCount prc) {
4503 synchronized (mProviderMap) {
4504 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004505 // There was a race! Some other client managed to acquire
4506 // the provider before the removal was completed.
4507 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004508 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004509 + "provider still in use");
4510 return;
4511 }
4512
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004513 final IBinder jBinder = prc.holder.provider.asBinder();
4514 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
4515 if (existingPrc == prc) {
4516 mProviderRefCountMap.remove(jBinder);
4517 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004518
4519 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
4520 while (iter.hasNext()) {
4521 ProviderClientRecord pr = iter.next();
4522 IBinder myBinder = pr.mProvider.asBinder();
4523 if (myBinder == jBinder) {
4524 iter.remove();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004525 }
4526 }
4527 }
4528
4529 try {
4530 if (DEBUG_PROVIDER) {
4531 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4532 + "removeContentProvider(" + prc.holder.info.name + ")");
4533 }
4534 ActivityManagerNative.getDefault().removeContentProvider(
4535 prc.holder.connection, false);
4536 } catch (RemoteException e) {
4537 //do nothing content provider object is dead any way
4538 }
4539 }
4540
4541 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
4542 synchronized(mProviderMap) {
4543 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4544 if (prc != null) {
4545 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
4546 + provider + " " + prc.holder.info.name);
4547 mProviderRefCountMap.remove(provider);
4548 if (prc.client != null && prc.client.mNames != null) {
4549 for (String name : prc.client.mNames) {
4550 ProviderClientRecord pr = mProviderMap.get(name);
4551 if (pr != null && pr.mProvider.asBinder() == provider) {
4552 Slog.i(TAG, "Removing dead content provider: " + name);
4553 mProviderMap.remove(name);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004554 }
4555 }
4556 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004557 if (fromClient) {
4558 // We found out about this due to execution in our client
4559 // code. Tell the activity manager about it now, to ensure
4560 // that the next time we go to do anything with the provider
4561 // it knows it is dead (so we don't race with its death
4562 // notification).
4563 try {
4564 ActivityManagerNative.getDefault().unstableProviderDied(
4565 prc.holder.connection);
4566 } catch (RemoteException e) {
4567 //do nothing content provider object is dead any way
4568 }
4569 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004570 }
4571 }
4572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004573
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004574 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
4575 ContentProvider localProvider,IActivityManager.ContentProviderHolder holder) {
4576 String names[] = PATTERN_SEMICOLON.split(holder.info.authority);
4577 ProviderClientRecord pcr = new ProviderClientRecord(names, provider,
4578 localProvider, holder);
4579 for (int i = 0; i < names.length; i++) {
4580 ProviderClientRecord existing = mProviderMap.get(names[i]);
4581 if (existing != null) {
4582 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
4583 + " already published as " + names[i]);
4584 } else {
4585 mProviderMap.put(names[i], pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004586 }
4587 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004588 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004589 }
4590
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004591 /**
4592 * Installs the provider.
4593 *
4594 * Providers that are local to the process or that come from the system server
4595 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4596 * Other remote providers are reference counted. The initial reference count
4597 * for all reference counted providers is one. Providers that are not reference
4598 * counted do not have a reference count (at all).
4599 *
4600 * This method detects when a provider has already been installed. When this happens,
4601 * it increments the reference count of the existing provider (if appropriate)
4602 * and returns the existing provider. This can happen due to concurrent
4603 * attempts to acquire the same provider.
4604 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004605 private IActivityManager.ContentProviderHolder installProvider(Context context,
4606 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
4607 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004608 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004609 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07004610 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004611 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004612 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004613 + info.name);
4614 }
4615 Context c = null;
4616 ApplicationInfo ai = info.applicationInfo;
4617 if (context.getPackageName().equals(ai.packageName)) {
4618 c = context;
4619 } else if (mInitialApplication != null &&
4620 mInitialApplication.getPackageName().equals(ai.packageName)) {
4621 c = mInitialApplication;
4622 } else {
4623 try {
4624 c = context.createPackageContext(ai.packageName,
4625 Context.CONTEXT_INCLUDE_CODE);
4626 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004627 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004628 }
4629 }
4630 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004631 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004632 ai.packageName +
4633 " while loading content provider " +
4634 info.name);
4635 return null;
4636 }
4637 try {
4638 final java.lang.ClassLoader cl = c.getClassLoader();
4639 localProvider = (ContentProvider)cl.
4640 loadClass(info.name).newInstance();
4641 provider = localProvider.getIContentProvider();
4642 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004643 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004644 info.name + " from sourceDir " +
4645 info.applicationInfo.sourceDir);
4646 return null;
4647 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004648 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004649 TAG, "Instantiating local provider " + info.name);
4650 // XXX Need to create the correct context for this provider.
4651 localProvider.attachInfo(c, info);
4652 } catch (java.lang.Exception e) {
4653 if (!mInstrumentation.onException(null, e)) {
4654 throw new RuntimeException(
4655 "Unable to get provider " + info.name
4656 + ": " + e.toString(), e);
4657 }
4658 return null;
4659 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004660 } else {
4661 provider = holder.provider;
4662 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004663 + info.name);
4664 }
4665
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004666 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004667
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004668 synchronized (mProviderMap) {
4669 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
4670 + " / " + info.name);
4671 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004672 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004673 ComponentName cname = new ComponentName(info.packageName, info.name);
4674 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004675 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004676 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004677 Slog.v(TAG, "installProvider: lost the race, "
4678 + "using existing local provider");
4679 }
4680 provider = pr.mProvider;
4681 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004682 holder = new IActivityManager.ContentProviderHolder(info);
4683 holder.provider = provider;
4684 holder.noReleaseNeeded = true;
4685 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004686 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004687 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004688 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004689 retHolder = pr.mHolder;
4690 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004691 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4692 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004693 if (DEBUG_PROVIDER) {
4694 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004695 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004696 // We need to transfer our new reference to the existing
4697 // ref count, releasing the old one... but only if
4698 // release is needed (that is, it is not running in the
4699 // system process).
4700 if (!noReleaseNeeded) {
4701 incProviderRefLocked(prc, stable);
4702 try {
4703 ActivityManagerNative.getDefault().removeContentProvider(
4704 holder.connection, stable);
4705 } catch (RemoteException e) {
4706 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004707 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004708 }
4709 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004710 ProviderClientRecord client = installProviderAuthoritiesLocked(
4711 provider, localProvider, holder);
4712 if (noReleaseNeeded) {
4713 prc = new ProviderRefCount(holder, client, 1000, 1000);
4714 } else {
4715 prc = stable
4716 ? new ProviderRefCount(holder, client, 1, 0)
4717 : new ProviderRefCount(holder, client, 0, 1);
4718 }
4719 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004720 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004721 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004722 }
4723 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004724
4725 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004726 }
4727
Romain Guy65b345f2011-07-27 18:51:50 -07004728 private void attach(boolean system) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004729 sThreadLocal.set(this);
4730 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004731 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004732 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004733 public void run() {
4734 ensureJitEnabled();
4735 }
4736 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004737 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4738 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4739 IActivityManager mgr = ActivityManagerNative.getDefault();
4740 try {
4741 mgr.attachApplication(mAppThread);
4742 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004743 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004744 }
4745 } else {
4746 // Don't set application object here -- if the system crashes,
4747 // we can't display an alert, we just want to die die die.
4748 android.ddm.DdmHandleAppName.setAppName("system_process");
4749 try {
4750 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004751 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004752 context.init(getSystemContext().mPackageInfo, null, this);
4753 Application app = Instrumentation.newApplication(Application.class, context);
4754 mAllApplications.add(app);
4755 mInitialApplication = app;
4756 app.onCreate();
4757 } catch (Exception e) {
4758 throw new RuntimeException(
4759 "Unable to instantiate Application():" + e.toString(), e);
4760 }
4761 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004762
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004763 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004764 public void onConfigurationChanged(Configuration newConfig) {
4765 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004766 // We need to apply this change to the resources
4767 // immediately, because upon returning the view
4768 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004769 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004770 // This actually changed the resources! Tell
4771 // everyone about it.
4772 if (mPendingConfiguration == null ||
4773 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4774 mPendingConfiguration = newConfig;
4775
4776 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4777 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004778 }
4779 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004780 }
4781 public void onLowMemory() {
4782 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004783 public void onTrimMemory(int level) {
4784 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004785 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004786 }
4787
Romain Guy5e9120d2012-01-30 12:17:22 -08004788 public static ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004789 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004790 ActivityThread thread = new ActivityThread();
4791 thread.attach(true);
4792 return thread;
4793 }
4794
Jeff Brown10e89712011-07-08 18:52:57 -07004795 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004796 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004797 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004798 }
4799 }
4800
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004801 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004802 synchronized (mPackages) {
4803 if (mCoreSettings != null) {
4804 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004805 } else {
4806 return defaultValue;
4807 }
4808 }
4809 }
4810
Romain Guy65b345f2011-07-27 18:51:50 -07004811 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004812 SamplingProfilerIntegration.start();
4813
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004814 // CloseGuard defaults to true and can be quite spammy. We
4815 // disable it here, but selectively enable it later (via
4816 // StrictMode) on debug builds, but using DropBox, not logs.
4817 CloseGuard.setEnabled(false);
4818
Kenny Roote29df162012-08-10 08:28:37 -07004819 Security.addProvider(new AndroidKeyStoreProvider());
4820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 Process.setArgV0("<pre-initialized>");
4822
4823 Looper.prepareMainLooper();
4824
4825 ActivityThread thread = new ActivityThread();
4826 thread.attach(false);
4827
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07004828 if (sMainThreadHandler == null) {
4829 sMainThreadHandler = thread.getHandler();
4830 }
4831
Romain Guy5e9120d2012-01-30 12:17:22 -08004832 AsyncTask.init();
4833
Dianne Hackborn287952c2010-09-22 22:34:31 -07004834 if (false) {
4835 Looper.myLooper().setMessageLogging(new
4836 LogPrinter(Log.DEBUG, "ActivityThread"));
4837 }
4838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004839 Looper.loop();
4840
Jeff Brown10e89712011-07-08 18:52:57 -07004841 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004842 }
4843}