blob: d4b204f8528e232b4a1b1734f97fe3d44c98cd88 [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;
Jeff Brownbd6e1502012-08-28 03:27:37 -070046import android.hardware.display.DisplayManagerGlobal;
Robert Greenwalt434203a2010-10-11 16:00:27 -070047import android.net.IConnectivityManager;
48import android.net.Proxy;
49import android.net.ProxyProperties;
Romain Guya9582652011-11-10 14:20:10 -080050import android.opengl.GLUtils;
Joe Onoratod630f102011-03-17 18:42:26 -070051import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070052import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Bundle;
54import android.os.Debug;
Geremy Condrab7faaf42012-09-19 18:07:42 -070055import android.os.DropBoxManager;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070056import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.os.Handler;
58import android.os.IBinder;
59import android.os.Looper;
60import android.os.Message;
61import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070062import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Process;
64import android.os.RemoteException;
65import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070066import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.os.SystemClock;
Jeff Brownefd43bd2012-09-21 17:02:35 -070068import android.os.SystemProperties;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070069import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070070import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.util.AndroidRuntimeException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.util.DisplayMetrics;
73import android.util.EventLog;
74import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070075import android.util.LogPrinter;
Jeff Brown6754ba22011-12-14 20:20:01 -080076import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080077import android.util.Slog;
Jeff Brownd32460c2012-07-20 16:15:36 -070078import android.view.CompatibilityInfoHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070080import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.view.View;
82import android.view.ViewDebug;
83import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070084import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.view.Window;
86import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -070087import android.view.WindowManagerGlobal;
Jason Samsa6f338c2012-02-24 16:22:16 -080088import android.renderscript.RenderScript;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
90import com.android.internal.os.BinderInternal;
91import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070092import com.android.internal.os.SamplingProfilerIntegration;
Jeff Sharkey6d515712012-09-20 16:06:08 -070093import com.android.internal.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
95import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl;
96
97import java.io.File;
98import java.io.FileDescriptor;
99import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700100import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import java.io.PrintWriter;
102import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700103import java.net.InetAddress;
Kenny Roote29df162012-08-10 08:28:37 -0700104import java.security.Security;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import java.util.ArrayList;
106import java.util.HashMap;
107import java.util.Iterator;
108import java.util.List;
109import java.util.Locale;
110import java.util.Map;
111import java.util.TimeZone;
112import java.util.regex.Pattern;
113
Geremy Condrab7faaf42012-09-19 18:07:42 -0700114import libcore.io.DropBox;
Geremy Condra69689a72012-09-11 16:57:17 -0700115import libcore.io.EventLogger;
Jeff Sharkeye861b422012-03-01 20:59:22 -0800116import libcore.io.IoUtils;
117
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800118import dalvik.system.CloseGuard;
Bob Leee5408332009-09-04 18:31:17 -0700119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120final class SuperNotCalledException extends AndroidRuntimeException {
121 public SuperNotCalledException(String msg) {
122 super(msg);
123 }
124}
125
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700126final class RemoteServiceException extends AndroidRuntimeException {
127 public RemoteServiceException(String msg) {
128 super(msg);
129 }
130}
131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132/**
133 * This manages the execution of the main thread in an
134 * application process, scheduling and executing activities,
135 * broadcasts, and other operations on it as the activity
136 * manager requests.
137 *
138 * {@hide}
139 */
140public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700141 /** @hide */
142 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700143 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700144 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700145 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700146 /** @hide */
147 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700148 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700149 private static final boolean DEBUG_BACKUP = false;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700150 private static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800151 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700152 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700153 private static final boolean DEBUG_PROVIDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
155 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
156 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
157 private static final int LOG_ON_PAUSE_CALLED = 30021;
158 private static final int LOG_ON_RESUME_CALLED = 30022;
159
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700160 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700161
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700162 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700164 final ApplicationThread mAppThread = new ApplicationThread();
165 final Looper mLooper = Looper.myLooper();
166 final H mH = new H();
167 final HashMap<IBinder, ActivityClientRecord> mActivities
168 = new HashMap<IBinder, ActivityClientRecord>();
169 // List of new activities (via ActivityRecord.nextIdle) that should
170 // be reported when next we idle.
171 ActivityClientRecord mNewActivities = null;
172 // Number of activities that are currently visible on-screen.
173 int mNumVisibleActivities = 0;
174 final HashMap<IBinder, Service> mServices
175 = new HashMap<IBinder, Service>();
176 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700177 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700178 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700179 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700180 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700181 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700182 Configuration mResConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700183 CompatibilityInfo mResCompatibilityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700184 Application mInitialApplication;
185 final ArrayList<Application> mAllApplications
186 = new ArrayList<Application>();
187 // set of instantiated backup agents, keyed by package name
188 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Romain Guy65b345f2011-07-27 18:51:50 -0700189 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700190 Instrumentation mInstrumentation;
191 String mInstrumentationAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700192 String mInstrumentationAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700193 String mInstrumentationAppPackage = null;
194 String mInstrumentedAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700195 String mInstrumentedAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700196 boolean mSystemThread = false;
197 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700199 // These can be accessed by multiple threads; mPackages is the lock.
200 // XXX For now we keep around information about all packages we have
201 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800202 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700203 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800204 // which means this lock gets held while the activity and window managers
205 // holds their own lock. Thus you MUST NEVER call back into the activity manager
206 // or window manager or anything that depends on them while holding this lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700207 final HashMap<String, WeakReference<LoadedApk>> mPackages
208 = new HashMap<String, WeakReference<LoadedApk>>();
209 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
210 = new HashMap<String, WeakReference<LoadedApk>>();
Jeff Browna492c3a2012-08-23 19:48:44 -0700211 final HashMap<CompatibilityInfo, DisplayMetrics> mDefaultDisplayMetrics
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700212 = new HashMap<CompatibilityInfo, DisplayMetrics>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700213 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
214 = new HashMap<ResourcesKey, WeakReference<Resources> >();
215 final ArrayList<ActivityClientRecord> mRelaunchingActivities
216 = new ArrayList<ActivityClientRecord>();
217 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218
Jeff Sharkey6d515712012-09-20 16:06:08 -0700219 private static final class ProviderKey {
220 final String authority;
221 final int userId;
222
223 public ProviderKey(String authority, int userId) {
224 this.authority = authority;
225 this.userId = userId;
226 }
227
228 @Override
229 public boolean equals(Object o) {
230 if (o instanceof ProviderKey) {
231 final ProviderKey other = (ProviderKey) o;
232 return Objects.equal(authority, other.authority) && userId == other.userId;
233 }
234 return false;
235 }
236
237 @Override
238 public int hashCode() {
239 return ((authority != null) ? authority.hashCode() : 0) ^ userId;
240 }
241 }
242
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700243 // The lock of mProviderMap protects the following variables.
Jeff Sharkey6d515712012-09-20 16:06:08 -0700244 final HashMap<ProviderKey, ProviderClientRecord> mProviderMap
245 = new HashMap<ProviderKey, ProviderClientRecord>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700246 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
247 = new HashMap<IBinder, ProviderRefCount>();
248 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
249 = new HashMap<IBinder, ProviderClientRecord>();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700250 final HashMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
251 = new HashMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
Jeff Hamilton52d32032011-01-08 15:31:26 -0600253 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
254 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
255
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700256 final GcIdler mGcIdler = new GcIdler();
257 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700259 static Handler sMainThreadHandler; // set once in main()
260
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800261 Bundle mCoreSettings = null;
262
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400263 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700265 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 Intent intent;
267 Bundle state;
268 Activity activity;
269 Window window;
270 Activity parent;
271 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700272 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 boolean paused;
274 boolean stopped;
275 boolean hideForNow;
276 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700277 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700278 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700280 String profileFile;
281 ParcelFileDescriptor profileFd;
282 boolean autoStopProfiler;
283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400285 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700286 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287
288 List<ResultInfo> pendingResults;
289 List<Intent> pendingIntents;
290
291 boolean startsNotResumed;
292 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800293 int pendingConfigChanges;
294 boolean onlyLocalRequest;
295
296 View mPendingRemoveWindow;
297 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700299 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 parent = null;
301 embeddedID = null;
302 paused = false;
303 stopped = false;
304 hideForNow = false;
305 nextIdle = null;
306 }
307
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800308 public boolean isPreHoneycomb() {
309 if (activity != null) {
310 return activity.getApplicationInfo().targetSdkVersion
311 < android.os.Build.VERSION_CODES.HONEYCOMB;
312 }
313 return false;
314 }
315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700317 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 return "ActivityRecord{"
319 + Integer.toHexString(System.identityHashCode(this))
320 + " token=" + token + " " + (componentName == null
321 ? "no component name" : componentName.toShortString())
322 + "}";
323 }
324 }
325
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700326 final class ProviderClientRecord {
327 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 final IContentProvider mProvider;
329 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700330 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700332 ProviderClientRecord(String[] names, IContentProvider provider,
333 ContentProvider localProvider,
334 IActivityManager.ContentProviderHolder holder) {
335 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 mProvider = provider;
337 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700338 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 }
340 }
341
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400342 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 List<Intent> intents;
344 IBinder token;
345 public String toString() {
346 return "NewIntentData{intents=" + intents + " token=" + token + "}";
347 }
348 }
349
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400350 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700351 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700352 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
353 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
354 token, sendingUser);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700355 this.intent = intent;
356 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 Intent intent;
359 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400360 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 public String toString() {
362 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700363 info.packageName + " resultCode=" + getResultCode()
364 + " resultData=" + getResultData() + " resultExtras="
365 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 }
367 }
368
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400369 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700370 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400371 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700372 int backupMode;
373 public String toString() {
374 return "CreateBackupAgentData{appInfo=" + appInfo
375 + " backupAgent=" + appInfo.backupAgentName
376 + " mode=" + backupMode + "}";
377 }
378 }
Bob Leee5408332009-09-04 18:31:17 -0700379
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400380 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 IBinder token;
382 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400383 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 Intent intent;
385 public String toString() {
386 return "CreateServiceData{token=" + token + " className="
387 + info.name + " packageName=" + info.packageName
388 + " intent=" + intent + "}";
389 }
390 }
391
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400392 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 IBinder token;
394 Intent intent;
395 boolean rebind;
396 public String toString() {
397 return "BindServiceData{token=" + token + " intent=" + intent + "}";
398 }
399 }
400
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400401 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700403 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700405 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 Intent args;
407 public String toString() {
408 return "ServiceArgsData{token=" + token + " startId=" + startId
409 + " args=" + args + "}";
410 }
411 }
412
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400413 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700414 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 String processName;
416 ApplicationInfo appInfo;
417 List<ProviderInfo> providers;
418 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 Bundle instrumentationArgs;
420 IInstrumentationWatcher instrumentationWatcher;
421 int debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800422 boolean enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700423 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700424 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400426 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700427
428 /** Initial values for {@link Profiler}. */
429 String initProfileFile;
430 ParcelFileDescriptor initProfileFd;
431 boolean initAutoStopProfiler;
432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 public String toString() {
434 return "AppBindData{appInfo=" + appInfo + "}";
435 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700436 }
437
438 static final class Profiler {
439 String profileFile;
440 ParcelFileDescriptor profileFd;
441 boolean autoStopProfiler;
442 boolean profiling;
443 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700444 public void setProfiler(String file, ParcelFileDescriptor fd) {
445 if (profiling) {
446 if (fd != null) {
447 try {
448 fd.close();
449 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700450 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700451 }
452 }
453 return;
454 }
455 if (profileFd != null) {
456 try {
457 profileFd.close();
458 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700459 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700460 }
461 }
462 profileFile = file;
463 profileFd = fd;
464 }
465 public void startProfiling() {
466 if (profileFd == null || profiling) {
467 return;
468 }
469 try {
470 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
471 8 * 1024 * 1024, 0);
472 profiling = true;
473 } catch (RuntimeException e) {
474 Slog.w(TAG, "Profiling failed on path " + profileFile);
475 try {
476 profileFd.close();
477 profileFd = null;
478 } catch (IOException e2) {
479 Slog.w(TAG, "Failure closing profile fd", e2);
480 }
481 }
482 }
483 public void stopProfiling() {
484 if (profiling) {
485 profiling = false;
486 Debug.stopMethodTracing();
487 if (profileFd != null) {
488 try {
489 profileFd.close();
490 } catch (IOException e) {
491 }
492 }
493 profileFd = null;
494 profileFile = null;
495 }
496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 }
498
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400499 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700500 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700501 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800502 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 }
505
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400506 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 IBinder token;
508 List<ResultInfo> results;
509 public String toString() {
510 return "ResultData{token=" + token + " results" + results + "}";
511 }
512 }
513
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400514 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800515 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 String what;
517 String who;
518 }
519
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400520 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700521 String path;
522 ParcelFileDescriptor fd;
523 }
524
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400525 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700526 String path;
527 ParcelFileDescriptor fd;
528 }
529
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400530 static final class UpdateCompatibilityData {
531 String pkg;
532 CompatibilityInfo info;
533 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700534
Romain Guy65b345f2011-07-27 18:51:50 -0700535 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700536
Romain Guy65b345f2011-07-27 18:51:50 -0700537 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700538 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
539 private static final String ONE_COUNT_COLUMN = "%21s %8d";
540 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700541 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 // Formatting for checkin service - update version if row format changes
544 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700545
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700546 private void updatePendingConfiguration(Configuration config) {
547 synchronized (mPackages) {
548 if (mPendingConfiguration == null ||
549 mPendingConfiguration.isOtherSeqNewer(config)) {
550 mPendingConfiguration = config;
551 }
552 }
553 }
554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 public final void schedulePauseActivity(IBinder token, boolean finished,
556 boolean userLeaving, int configChanges) {
557 queueOrSendMessage(
558 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
559 token,
560 (userLeaving ? 1 : 0),
561 configChanges);
562 }
563
564 public final void scheduleStopActivity(IBinder token, boolean showWindow,
565 int configChanges) {
566 queueOrSendMessage(
567 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
568 token, 0, configChanges);
569 }
570
571 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
572 queueOrSendMessage(
573 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
574 token);
575 }
576
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800577 public final void scheduleSleeping(IBinder token, boolean sleeping) {
578 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
579 }
580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
582 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
583 }
584
585 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
586 ResultData res = new ResultData();
587 res.token = token;
588 res.results = results;
589 queueOrSendMessage(H.SEND_RESULT, res);
590 }
591
592 // we use token to identify this activity without having to send the
593 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700594 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700595 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
596 Bundle state, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700597 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
598 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700599 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600
601 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700602 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 r.intent = intent;
604 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400605 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 r.state = state;
607
608 r.pendingResults = pendingResults;
609 r.pendingIntents = pendingNewIntents;
610
611 r.startsNotResumed = notResumed;
612 r.isForward = isForward;
613
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700614 r.profileFile = profileName;
615 r.profileFd = profileFd;
616 r.autoStopProfiler = autoStopProfiler;
617
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700618 updatePendingConfiguration(curConfig);
619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
621 }
622
623 public final void scheduleRelaunchActivity(IBinder token,
624 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800625 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800626 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
627 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 }
629
630 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
631 NewIntentData data = new NewIntentData();
632 data.intents = intents;
633 data.token = token;
634
635 queueOrSendMessage(H.NEW_INTENT, data);
636 }
637
638 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
639 int configChanges) {
640 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
641 configChanges);
642 }
643
644 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400645 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700646 boolean sync, int sendingUser) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700647 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700648 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400650 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 queueOrSendMessage(H.RECEIVER, r);
652 }
653
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400654 public final void scheduleCreateBackupAgent(ApplicationInfo app,
655 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700656 CreateBackupAgentData d = new CreateBackupAgentData();
657 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400658 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700659 d.backupMode = backupMode;
660
661 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
662 }
663
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400664 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
665 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700666 CreateBackupAgentData d = new CreateBackupAgentData();
667 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400668 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700669
670 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
671 }
672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400674 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 CreateServiceData s = new CreateServiceData();
676 s.token = token;
677 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400678 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679
680 queueOrSendMessage(H.CREATE_SERVICE, s);
681 }
682
683 public final void scheduleBindService(IBinder token, Intent intent,
684 boolean rebind) {
685 BindServiceData s = new BindServiceData();
686 s.token = token;
687 s.intent = intent;
688 s.rebind = rebind;
689
Amith Yamasani742a6712011-05-04 14:49:28 -0700690 if (DEBUG_SERVICE)
691 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
692 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 queueOrSendMessage(H.BIND_SERVICE, s);
694 }
695
696 public final void scheduleUnbindService(IBinder token, Intent intent) {
697 BindServiceData s = new BindServiceData();
698 s.token = token;
699 s.intent = intent;
700
701 queueOrSendMessage(H.UNBIND_SERVICE, s);
702 }
703
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700704 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700705 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 ServiceArgsData s = new ServiceArgsData();
707 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700708 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700710 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 s.args = args;
712
713 queueOrSendMessage(H.SERVICE_ARGS, s);
714 }
715
716 public final void scheduleStopService(IBinder token) {
717 queueOrSendMessage(H.STOP_SERVICE, token);
718 }
719
720 public final void bindApplication(String processName,
721 ApplicationInfo appInfo, List<ProviderInfo> providers,
722 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700723 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Siva Velusamy92a8b222012-03-09 16:24:04 -0800725 int debugMode, boolean enableOpenGlTrace, boolean isRestrictedBackupMode,
726 boolean persistent, Configuration config, CompatibilityInfo compatInfo,
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700727 Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728
729 if (services != null) {
730 // Setup the service cache in the ServiceManager
731 ServiceManager.initServiceCache(services);
732 }
733
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800734 setCoreSettings(coreSettings);
735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 AppBindData data = new AppBindData();
737 data.processName = processName;
738 data.appInfo = appInfo;
739 data.providers = providers;
740 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 data.instrumentationArgs = instrumentationArgs;
742 data.instrumentationWatcher = instrumentationWatcher;
743 data.debugMode = debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800744 data.enableOpenGlTrace = enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700745 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700746 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400748 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700749 data.initProfileFile = profileFile;
750 data.initProfileFd = profileFd;
751 data.initAutoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 queueOrSendMessage(H.BIND_APPLICATION, data);
753 }
754
755 public final void scheduleExit() {
756 queueOrSendMessage(H.EXIT_APPLICATION, null);
757 }
758
Christopher Tate5e1ab332009-09-01 20:32:49 -0700759 public final void scheduleSuicide() {
760 queueOrSendMessage(H.SUICIDE, null);
761 }
762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 public void requestThumbnail(IBinder token) {
764 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
765 }
766
767 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700768 updatePendingConfiguration(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
770 }
771
772 public void updateTimeZone() {
773 TimeZone.setDefault(null);
774 }
775
Robert Greenwalt03595d02010-11-02 14:08:23 -0700776 public void clearDnsCache() {
777 // a non-standard API to get this to libcore
778 InetAddress.clearDnsCache();
779 }
780
Robert Greenwalt434203a2010-10-11 16:00:27 -0700781 public void setHttpProxy(String host, String port, String exclList) {
782 Proxy.setHttpProxySystemProperty(host, port, exclList);
783 }
784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 public void processInBackground() {
786 mH.removeMessages(H.GC_WHEN_IDLE);
787 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
788 }
789
790 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700791 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700792 try {
793 data.fd = ParcelFileDescriptor.dup(fd);
794 data.token = servicetoken;
795 data.args = args;
796 queueOrSendMessage(H.DUMP_SERVICE, data);
797 } catch (IOException e) {
798 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 }
800 }
801
802 // This function exists to make sure all receiver dispatching is
803 // correctly ordered, since these are one-way calls and the binder driver
804 // applies transaction ordering per object for such calls.
805 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700806 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700807 boolean sticky, int sendingUser) throws RemoteException {
808 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
809 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 }
Bob Leee5408332009-09-04 18:31:17 -0700811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 public void scheduleLowMemory() {
813 queueOrSendMessage(H.LOW_MEMORY, null);
814 }
815
816 public void scheduleActivityConfigurationChanged(IBinder token) {
817 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
818 }
819
Romain Guy7eabe552011-07-21 14:56:34 -0700820 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
821 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700822 ProfilerControlData pcd = new ProfilerControlData();
823 pcd.path = path;
824 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700825 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800826 }
827
Andy McFadden824c5102010-07-09 16:26:57 -0700828 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
829 DumpHeapData dhd = new DumpHeapData();
830 dhd.path = path;
831 dhd.fd = fd;
832 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
833 }
834
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700835 public void setSchedulingGroup(int group) {
836 // Note: do this immediately, since going into the foreground
837 // should happen regardless of what pending work we have to do
838 // and the activity manager will wait for us to report back that
839 // we are done before sending us to the background.
840 try {
841 Process.setProcessGroup(Process.myPid(), group);
842 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800843 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700844 }
845 }
Bob Leee5408332009-09-04 18:31:17 -0700846
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700847 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
848 Debug.getMemoryInfo(outInfo);
849 }
Bob Leee5408332009-09-04 18:31:17 -0700850
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700851 public void dispatchPackageBroadcast(int cmd, String[] packages) {
852 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
853 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700854
855 public void scheduleCrash(String msg) {
856 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
857 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700858
Dianne Hackborn30d71892010-12-11 10:37:55 -0800859 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
860 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700861 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700862 try {
863 data.fd = ParcelFileDescriptor.dup(fd);
864 data.token = activitytoken;
865 data.prefix = prefix;
866 data.args = args;
867 queueOrSendMessage(H.DUMP_ACTIVITY, data);
868 } catch (IOException e) {
869 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700870 }
871 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700872
Marco Nelissen18cb2872011-11-15 11:19:53 -0800873 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
874 String[] args) {
875 DumpComponentInfo data = new DumpComponentInfo();
876 try {
877 data.fd = ParcelFileDescriptor.dup(fd);
878 data.token = providertoken;
879 data.args = args;
880 queueOrSendMessage(H.DUMP_PROVIDER, data);
881 } catch (IOException e) {
882 Slog.w(TAG, "dumpProvider failed", e);
883 }
884 }
885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700887 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
888 boolean all, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700889 FileOutputStream fout = new FileOutputStream(fd);
890 PrintWriter pw = new PrintWriter(fout);
891 try {
Romain Guya998dff2012-03-23 18:58:36 -0700892 return dumpMemInfo(pw, checkin, all);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700893 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700894 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700895 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700896 }
897
Romain Guya998dff2012-03-23 18:58:36 -0700898 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean all) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 long nativeMax = Debug.getNativeHeapSize() / 1024;
900 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
901 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
902
903 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
904 Debug.getMemoryInfo(memInfo);
905
Dianne Hackbornb437e092011-08-05 17:50:29 -0700906 if (!all) {
907 return memInfo;
908 }
909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 Runtime runtime = Runtime.getRuntime();
911
912 long dalvikMax = runtime.totalMemory() / 1024;
913 long dalvikFree = runtime.freeMemory() / 1024;
914 long dalvikAllocated = dalvikMax - dalvikFree;
915 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700916 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700917 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
918 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 int globalAssetCount = AssetManager.getGlobalAssetCount();
920 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
921 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
922 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
923 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700924 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800925 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700928 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 // NOTE: if you change anything significant below, also consider changing
930 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700931 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 // Header
935 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
936 pw.print(Process.myPid()); pw.print(',');
937 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 // Heap info - max
940 pw.print(nativeMax); pw.print(',');
941 pw.print(dalvikMax); pw.print(',');
942 pw.print("N/A,");
943 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 // Heap info - allocated
946 pw.print(nativeAllocated); pw.print(',');
947 pw.print(dalvikAllocated); pw.print(',');
948 pw.print("N/A,");
949 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 // Heap info - free
952 pw.print(nativeFree); pw.print(',');
953 pw.print(dalvikFree); pw.print(',');
954 pw.print("N/A,");
955 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 // Heap info - proportional set size
958 pw.print(memInfo.nativePss); pw.print(',');
959 pw.print(memInfo.dalvikPss); pw.print(',');
960 pw.print(memInfo.otherPss); pw.print(',');
961 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700964 pw.print(memInfo.nativeSharedDirty); pw.print(',');
965 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
966 pw.print(memInfo.otherSharedDirty); pw.print(',');
967 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
968 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700971 pw.print(memInfo.nativePrivateDirty); pw.print(',');
972 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
973 pw.print(memInfo.otherPrivateDirty); pw.print(',');
974 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
975 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 // Object counts
978 pw.print(viewInstanceCount); pw.print(',');
979 pw.print(viewRootInstanceCount); pw.print(',');
980 pw.print(appContextInstanceCount); pw.print(',');
981 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 pw.print(globalAssetCount); pw.print(',');
984 pw.print(globalAssetManagerCount); pw.print(',');
985 pw.print(binderLocalObjectCount); pw.print(',');
986 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 pw.print(binderDeathObjectCount); pw.print(',');
989 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 // SQL
Vasu Noric3849202010-03-09 10:47:25 -0800992 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -0800993 pw.print(stats.memoryUsed / 1024); pw.print(',');
994 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700995 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800996 for (int i = 0; i < stats.dbStats.size(); i++) {
997 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700998 pw.print(','); pw.print(dbStats.dbName);
999 pw.print(','); pw.print(dbStats.pageSize);
1000 pw.print(','); pw.print(dbStats.dbSize);
1001 pw.print(','); pw.print(dbStats.lookaside);
1002 pw.print(','); pw.print(dbStats.cache);
1003 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -08001004 }
Dianne Hackbornb437e092011-08-05 17:50:29 -07001005 pw.println();
Bob Leee5408332009-09-04 18:31:17 -07001006
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001007 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
Bob Leee5408332009-09-04 18:31:17 -07001009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001011 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
1012 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
1013 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
1014 "------");
1015 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
1016 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
1017 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
1018 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001020 int otherPss = memInfo.otherPss;
1021 int otherSharedDirty = memInfo.otherSharedDirty;
1022 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001024 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Romain Guy65b345f2011-07-27 18:51:50 -07001025 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001026 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
1027 memInfo.getOtherPrivateDirty(i), "", "", "");
1028 otherPss -= memInfo.getOtherPss(i);
1029 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
1030 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
1031 }
1032
1033 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
1034 otherPrivateDirty, "", "", "");
1035 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
1036 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
1037 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
1038 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039
1040 pw.println(" ");
1041 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001042 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 viewRootInstanceCount);
1044
1045 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1046 "Activities:", activityInstanceCount);
1047
1048 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1049 "AssetManagers:", globalAssetManagerCount);
1050
1051 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1052 "Proxy Binders:", binderProxyObjectCount);
1053 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
1054
1055 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 // SQLite mem info
1058 pw.println(" ");
1059 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001060 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1061 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1062 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001063 pw.println(" ");
1064 int N = stats.dbStats.size();
1065 if (N > 0) {
1066 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001067 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1068 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001069 for (int i = 0; i < N; i++) {
1070 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001071 printRow(pw, DB_INFO_FORMAT,
1072 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1073 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1074 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1075 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001076 }
1077 }
Bob Leee5408332009-09-04 18:31:17 -07001078
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001079 // Asset details.
1080 String assetAlloc = AssetManager.getAssetAllocations();
1081 if (assetAlloc != null) {
1082 pw.println(" ");
1083 pw.println(" Asset Allocations");
1084 pw.print(assetAlloc);
1085 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001086
1087 return memInfo;
1088 }
1089
1090 @Override
1091 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1092 dumpGraphicsInfo(fd);
Jeff Brown98365d72012-08-19 20:30:52 -07001093 WindowManagerGlobal.getInstance().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
1095
Jeff Brown6754ba22011-12-14 20:20:01 -08001096 @Override
1097 public void dumpDbInfo(FileDescriptor fd, String[] args) {
1098 PrintWriter pw = new PrintWriter(new FileOutputStream(fd));
1099 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1100 SQLiteDebug.dump(printer, args);
1101 pw.flush();
1102 }
1103
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001104 @Override
1105 public void unstableProviderDied(IBinder provider) {
1106 queueOrSendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
1107 }
1108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 private void printRow(PrintWriter pw, String format, Object...objs) {
1110 pw.println(String.format(format, objs));
1111 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001112
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001113 public void setCoreSettings(Bundle coreSettings) {
1114 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001115 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001116
1117 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1118 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1119 ucd.pkg = pkg;
1120 ucd.info = info;
1121 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1122 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001123
1124 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001125 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001126 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 }
1129
Romain Guy65b345f2011-07-27 18:51:50 -07001130 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 public static final int LAUNCH_ACTIVITY = 100;
1132 public static final int PAUSE_ACTIVITY = 101;
1133 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1134 public static final int STOP_ACTIVITY_SHOW = 103;
1135 public static final int STOP_ACTIVITY_HIDE = 104;
1136 public static final int SHOW_WINDOW = 105;
1137 public static final int HIDE_WINDOW = 106;
1138 public static final int RESUME_ACTIVITY = 107;
1139 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001140 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 public static final int BIND_APPLICATION = 110;
1142 public static final int EXIT_APPLICATION = 111;
1143 public static final int NEW_INTENT = 112;
1144 public static final int RECEIVER = 113;
1145 public static final int CREATE_SERVICE = 114;
1146 public static final int SERVICE_ARGS = 115;
1147 public static final int STOP_SERVICE = 116;
1148 public static final int REQUEST_THUMBNAIL = 117;
1149 public static final int CONFIGURATION_CHANGED = 118;
1150 public static final int CLEAN_UP_CONTEXT = 119;
1151 public static final int GC_WHEN_IDLE = 120;
1152 public static final int BIND_SERVICE = 121;
1153 public static final int UNBIND_SERVICE = 122;
1154 public static final int DUMP_SERVICE = 123;
1155 public static final int LOW_MEMORY = 124;
1156 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1157 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001158 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001159 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001160 public static final int DESTROY_BACKUP_AGENT = 129;
1161 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001162 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001163 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001164 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001165 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001166 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001167 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001168 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001169 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001170 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001171 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001172 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001173 public static final int UNSTABLE_PROVIDER_DIED = 142;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001175 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 switch (code) {
1177 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1178 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1179 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1180 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1181 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1182 case SHOW_WINDOW: return "SHOW_WINDOW";
1183 case HIDE_WINDOW: return "HIDE_WINDOW";
1184 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1185 case SEND_RESULT: return "SEND_RESULT";
1186 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1187 case BIND_APPLICATION: return "BIND_APPLICATION";
1188 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1189 case NEW_INTENT: return "NEW_INTENT";
1190 case RECEIVER: return "RECEIVER";
1191 case CREATE_SERVICE: return "CREATE_SERVICE";
1192 case SERVICE_ARGS: return "SERVICE_ARGS";
1193 case STOP_SERVICE: return "STOP_SERVICE";
1194 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1195 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1196 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1197 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1198 case BIND_SERVICE: return "BIND_SERVICE";
1199 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1200 case DUMP_SERVICE: return "DUMP_SERVICE";
1201 case LOW_MEMORY: return "LOW_MEMORY";
1202 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1203 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001204 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001205 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1206 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001207 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001208 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001209 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001210 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001211 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001212 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001213 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001214 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001215 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001216 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001217 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001218 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001219 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 }
1221 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001222 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 }
1224 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001225 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 switch (msg.what) {
1227 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001228 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001229 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230
1231 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001232 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001233 handleLaunchActivity(r, null);
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 RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001237 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001238 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001239 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001240 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 } break;
1242 case PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001243 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001245 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001246 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 break;
1248 case PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001249 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001251 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 break;
1253 case STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001254 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
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 STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001259 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001261 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 break;
1263 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001264 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001266 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 break;
1268 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001269 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001271 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 break;
1273 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001274 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 handleResumeActivity((IBinder)msg.obj, true,
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001276 msg.arg1 != 0, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001277 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 break;
1279 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001280 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001282 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 break;
1284 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001285 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1287 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001288 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 break;
1290 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001291 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 AppBindData data = (AppBindData)msg.obj;
1293 handleBindApplication(data);
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 EXIT_APPLICATION:
1297 if (mInitialApplication != null) {
1298 mInitialApplication.onTerminate();
1299 }
1300 Looper.myLooper().quit();
1301 break;
1302 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001303 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 handleNewIntent((NewIntentData)msg.obj);
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 RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001308 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001310 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001311 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 break;
1313 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001314 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 handleCreateService((CreateServiceData)msg.obj);
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 BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001319 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001321 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 break;
1323 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001324 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001326 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 break;
1328 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001329 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 handleServiceArgs((ServiceArgsData)msg.obj);
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 STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001334 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001336 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001337 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 break;
1339 case REQUEST_THUMBNAIL:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001340 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "requestThumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 handleRequestThumbnail((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001342 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 break;
1344 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001345 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001346 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001347 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001348 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 break;
1350 case CLEAN_UP_CONTEXT:
1351 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1352 cci.context.performFinalCleanup(cci.who, cci.what);
1353 break;
1354 case GC_WHEN_IDLE:
1355 scheduleGcIdler();
1356 break;
1357 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001358 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 break;
1360 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001361 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001363 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 break;
1365 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001366 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001368 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001370 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001371 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001372 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001373 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001374 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001375 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001376 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001377 break;
1378 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001379 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001380 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001381 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001382 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001383 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001384 Process.killProcess(Process.myPid());
1385 break;
1386 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001387 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001388 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001389 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001390 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001391 case ENABLE_JIT:
1392 ensureJitEnabled();
1393 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001394 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001395 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001396 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001397 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001398 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001399 case SCHEDULE_CRASH:
1400 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001401 case DUMP_HEAP:
1402 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1403 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001404 case DUMP_ACTIVITY:
1405 handleDumpActivity((DumpComponentInfo)msg.obj);
1406 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001407 case DUMP_PROVIDER:
1408 handleDumpProvider((DumpComponentInfo)msg.obj);
1409 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001410 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001411 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001412 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001413 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001414 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001415 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001416 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001417 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001418 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001419 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001420 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1421 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001422 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001423 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001424 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001425 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001426 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001427 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001428 case UNSTABLE_PROVIDER_DIED:
1429 handleUnstableProviderDied((IBinder)msg.obj, false);
1430 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001432 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 }
Bob Leee5408332009-09-04 18:31:17 -07001434
Brian Carlstromed7e0072011-03-24 13:27:57 -07001435 private void maybeSnapshot() {
1436 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001437 // convert the *private* ActivityThread.PackageInfo to *public* known
1438 // android.content.pm.PackageInfo
1439 String packageName = mBoundApplication.info.mPackageName;
1440 android.content.pm.PackageInfo packageInfo = null;
1441 try {
1442 Context context = getSystemContext();
1443 if(context == null) {
1444 Log.e(TAG, "cannot get a valid context");
1445 return;
1446 }
1447 PackageManager pm = context.getPackageManager();
1448 if(pm == null) {
1449 Log.e(TAG, "cannot get a valid PackageManager");
1450 return;
1451 }
1452 packageInfo = pm.getPackageInfo(
1453 packageName, PackageManager.GET_ACTIVITIES);
1454 } catch (NameNotFoundException e) {
1455 Log.e(TAG, "cannot get package info for " + packageName, e);
1456 }
1457 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001458 }
1459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 }
1461
Romain Guy65b345f2011-07-27 18:51:50 -07001462 private class Idler implements MessageQueue.IdleHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001464 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001465 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001466 if (mBoundApplication != null && mProfiler.profileFd != null
1467 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001468 stopProfiling = true;
1469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 if (a != null) {
1471 mNewActivities = null;
1472 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001473 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001475 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 TAG, "Reporting idle of " + a +
1477 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001478 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 if (a.activity != null && !a.activity.mFinished) {
1480 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001481 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001482 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001484 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 }
1486 }
1487 prev = a;
1488 a = a.nextIdle;
1489 prev.nextIdle = null;
1490 } while (a != null);
1491 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001492 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001493 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001494 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001495 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 return false;
1497 }
1498 }
1499
1500 final class GcIdler implements MessageQueue.IdleHandler {
1501 public final boolean queueIdle() {
1502 doGcIfNeeded();
1503 return false;
1504 }
1505 }
1506
Romain Guy65b345f2011-07-27 18:51:50 -07001507 private static class ResourcesKey {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001508 final private String mResDir;
Jeff Browna492c3a2012-08-23 19:48:44 -07001509 final private int mDisplayId;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001510 final private Configuration mOverrideConfiguration;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001511 final private float mScale;
1512 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001513
Jeff Browna492c3a2012-08-23 19:48:44 -07001514 ResourcesKey(String resDir, int displayId, Configuration overrideConfiguration, float scale) {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001515 mResDir = resDir;
Jeff Browna492c3a2012-08-23 19:48:44 -07001516 mDisplayId = displayId;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001517 if (overrideConfiguration != null) {
1518 if (Configuration.EMPTY.equals(overrideConfiguration)) {
1519 overrideConfiguration = null;
1520 }
1521 }
1522 mOverrideConfiguration = overrideConfiguration;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001523 mScale = scale;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001524 int hash = 17;
1525 hash = 31 * hash + mResDir.hashCode();
Jeff Browna492c3a2012-08-23 19:48:44 -07001526 hash = 31 * hash + mDisplayId;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001527 hash = 31 * hash + (mOverrideConfiguration != null
1528 ? mOverrideConfiguration.hashCode() : 0);
1529 hash = 31 * hash + Float.floatToIntBits(mScale);
1530 mHash = hash;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001531 }
Bob Leee5408332009-09-04 18:31:17 -07001532
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001533 @Override
1534 public int hashCode() {
1535 return mHash;
1536 }
1537
1538 @Override
1539 public boolean equals(Object obj) {
1540 if (!(obj instanceof ResourcesKey)) {
1541 return false;
1542 }
1543 ResourcesKey peer = (ResourcesKey) obj;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001544 if (!mResDir.equals(peer.mResDir)) {
1545 return false;
1546 }
Jeff Browna492c3a2012-08-23 19:48:44 -07001547 if (mDisplayId != peer.mDisplayId) {
1548 return false;
1549 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07001550 if (mOverrideConfiguration != peer.mOverrideConfiguration) {
1551 if (mOverrideConfiguration == null || peer.mOverrideConfiguration == null) {
1552 return false;
1553 }
1554 if (!mOverrideConfiguration.equals(peer.mOverrideConfiguration)) {
1555 return false;
1556 }
1557 }
1558 if (mScale != peer.mScale) {
1559 return false;
1560 }
1561 return true;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001562 }
1563 }
1564
Romain Guy65b345f2011-07-27 18:51:50 -07001565 public static ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001566 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568
Romain Guy65b345f2011-07-27 18:51:50 -07001569 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001570 ActivityThread am = currentActivityThread();
1571 return (am != null && am.mBoundApplication != null)
1572 ? am.mBoundApplication.processName : null;
1573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574
Romain Guy65b345f2011-07-27 18:51:50 -07001575 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001576 ActivityThread am = currentActivityThread();
1577 return am != null ? am.mInitialApplication : null;
1578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001580 public static IPackageManager getPackageManager() {
1581 if (sPackageManager != null) {
1582 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1583 return sPackageManager;
1584 }
1585 IBinder b = ServiceManager.getService("package");
1586 //Slog.v("PackageManager", "default service binder = " + b);
1587 sPackageManager = IPackageManager.Stub.asInterface(b);
1588 //Slog.v("PackageManager", "default service = " + sPackageManager);
1589 return sPackageManager;
1590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591
Jeff Browna492c3a2012-08-23 19:48:44 -07001592 private void flushDisplayMetricsLocked() {
1593 mDefaultDisplayMetrics.clear();
1594 }
1595
1596 DisplayMetrics getDisplayMetricsLocked(int displayId, CompatibilityInfo ci) {
1597 boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
1598 DisplayMetrics dm = isDefaultDisplay ? mDefaultDisplayMetrics.get(ci) : null;
1599 if (dm != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001600 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001601 }
Jeff Browna492c3a2012-08-23 19:48:44 -07001602 dm = new DisplayMetrics();
Jeff Brown848c2dc2012-08-19 20:18:08 -07001603
Jeff Brownbd6e1502012-08-28 03:27:37 -07001604 DisplayManagerGlobal displayManager = DisplayManagerGlobal.getInstance();
Jeff Brown848c2dc2012-08-19 20:18:08 -07001605 if (displayManager == null) {
1606 // may be null early in system startup
Jeff Brown848c2dc2012-08-19 20:18:08 -07001607 dm.setToDefaults();
1608 return dm;
1609 }
1610
Jeff Browna492c3a2012-08-23 19:48:44 -07001611 if (isDefaultDisplay) {
1612 mDefaultDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001613 }
Jeff Brown848c2dc2012-08-19 20:18:08 -07001614
Jeff Brownd32460c2012-07-20 16:15:36 -07001615 CompatibilityInfoHolder cih = new CompatibilityInfoHolder();
1616 cih.set(ci);
Jeff Browna492c3a2012-08-23 19:48:44 -07001617 Display d = displayManager.getCompatibleDisplay(displayId, cih);
Jeff Browna3909a92012-09-09 00:57:09 -07001618 if (d != null) {
1619 d.getMetrics(dm);
1620 } else {
1621 // Display no longer exists
1622 // FIXME: This would not be a problem if we kept the Display object around
1623 // instead of using the raw display id everywhere. The Display object caches
1624 // its information even after the display has been removed.
1625 dm.setToDefaults();
1626 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001627 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1628 // + metrics.heightPixels + " den=" + metrics.density
1629 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001630 return dm;
1631 }
1632
Romain Guy65b345f2011-07-27 18:51:50 -07001633 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001634 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1635 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001636 if (config == null) {
1637 return null;
1638 }
1639 if (compat != null && !compat.supportsScreen()) {
1640 mMainThreadConfig.setTo(config);
1641 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001642 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001643 }
1644 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001647 /**
1648 * Creates the top level Resources for applications with the given compatibility info.
1649 *
1650 * @param resDir the resource directory.
1651 * @param compInfo the compability info. It will use the default compatibility info when it's
1652 * null.
1653 */
Jeff Browna492c3a2012-08-23 19:48:44 -07001654 Resources getTopLevelResources(String resDir,
1655 int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001656 CompatibilityInfo compInfo) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001657 ResourcesKey key = new ResourcesKey(resDir,
1658 displayId, overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001659 compInfo.applicationScale);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001660 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001662 // Resources is app scale dependent.
1663 if (false) {
1664 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1665 + compInfo.applicationScale);
1666 }
1667 WeakReference<Resources> wr = mActiveResources.get(key);
1668 r = wr != null ? wr.get() : null;
1669 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1670 if (r != null && r.getAssets().isUpToDate()) {
1671 if (false) {
1672 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1673 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1674 }
1675 return r;
1676 }
1677 }
1678
1679 //if (r != null) {
1680 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1681 // + r + " " + resDir);
1682 //}
1683
1684 AssetManager assets = new AssetManager();
1685 if (assets.addAssetPath(resDir) == 0) {
1686 return null;
1687 }
1688
1689 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Jeff Browna492c3a2012-08-23 19:48:44 -07001690 DisplayMetrics dm = getDisplayMetricsLocked(displayId, null);
Dianne Hackborn756220b2012-08-14 16:45:30 -07001691 Configuration config;
Jeff Browna492c3a2012-08-23 19:48:44 -07001692 boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
1693 if (!isDefaultDisplay || key.mOverrideConfiguration != null) {
Dianne Hackborn756220b2012-08-14 16:45:30 -07001694 config = new Configuration(getConfiguration());
Jeff Browna492c3a2012-08-23 19:48:44 -07001695 if (!isDefaultDisplay) {
1696 applyNonDefaultDisplayMetricsToConfigurationLocked(dm, config);
1697 }
1698 if (key.mOverrideConfiguration != null) {
1699 config.updateFrom(key.mOverrideConfiguration);
1700 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07001701 } else {
1702 config = getConfiguration();
1703 }
Jeff Browna492c3a2012-08-23 19:48:44 -07001704 r = new Resources(assets, dm, config, compInfo);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001705 if (false) {
1706 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1707 + r.getConfiguration() + " appScale="
1708 + r.getCompatibilityInfo().applicationScale);
1709 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07001710
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001711 synchronized (mPackages) {
1712 WeakReference<Resources> wr = mActiveResources.get(key);
1713 Resources existing = wr != null ? wr.get() : null;
1714 if (existing != null && existing.getAssets().isUpToDate()) {
1715 // Someone else already created the resources while we were
1716 // unlocked; go ahead and use theirs.
1717 r.getAssets().close();
1718 return existing;
1719 }
1720
1721 // XXX need to remove entries when weak references go away
1722 mActiveResources.put(key, new WeakReference<Resources>(r));
1723 return r;
1724 }
1725 }
1726
1727 /**
1728 * Creates the top level resources for the given package.
1729 */
Jeff Browna492c3a2012-08-23 19:48:44 -07001730 Resources getTopLevelResources(String resDir,
1731 int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001732 LoadedApk pkgInfo) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001733 return getTopLevelResources(resDir, displayId, overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001734 pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001735 }
1736
1737 final Handler getHandler() {
1738 return mH;
1739 }
1740
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001741 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1742 int flags) {
Amith Yamasani98edc952012-09-25 14:09:27 -07001743 return getPackageInfo(packageName, compatInfo, flags, UserHandle.myUserId());
1744 }
1745
1746 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1747 int flags, int userId) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001748 synchronized (mPackages) {
1749 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1751 ref = mPackages.get(packageName);
1752 } else {
1753 ref = mResourcePackages.get(packageName);
1754 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001755 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001756 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001757 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1758 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 if (packageInfo != null && (packageInfo.mResources == null
1760 || packageInfo.mResources.getAssets().isUpToDate())) {
1761 if (packageInfo.isSecurityViolation()
1762 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1763 throw new SecurityException(
1764 "Requesting code from " + packageName
1765 + " to be run in process "
1766 + mBoundApplication.processName
1767 + "/" + mBoundApplication.appInfo.uid);
1768 }
1769 return packageInfo;
1770 }
1771 }
1772
1773 ApplicationInfo ai = null;
1774 try {
1775 ai = getPackageManager().getApplicationInfo(packageName,
Amith Yamasani98edc952012-09-25 14:09:27 -07001776 PackageManager.GET_SHARED_LIBRARY_FILES, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001778 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 }
1780
1781 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001782 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 }
1784
1785 return null;
1786 }
1787
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001788 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1789 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1791 boolean securityViolation = includeCode && ai.uid != 0
1792 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001793 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001794 : true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1796 |Context.CONTEXT_IGNORE_SECURITY))
1797 == Context.CONTEXT_INCLUDE_CODE) {
1798 if (securityViolation) {
1799 String msg = "Requesting code from " + ai.packageName
1800 + " (with uid " + ai.uid + ")";
1801 if (mBoundApplication != null) {
1802 msg = msg + " to be run in process "
1803 + mBoundApplication.processName + " (with uid "
1804 + mBoundApplication.appInfo.uid + ")";
1805 }
1806 throw new SecurityException(msg);
1807 }
1808 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001809 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 }
1811
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001812 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1813 CompatibilityInfo compatInfo) {
1814 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 }
1816
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001817 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1818 synchronized (mPackages) {
1819 WeakReference<LoadedApk> ref;
1820 if (includeCode) {
1821 ref = mPackages.get(packageName);
1822 } else {
1823 ref = mResourcePackages.get(packageName);
1824 }
1825 return ref != null ? ref.get() : null;
1826 }
1827 }
1828
Romain Guy65b345f2011-07-27 18:51:50 -07001829 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1831 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001832 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 if (includeCode) {
1834 ref = mPackages.get(aInfo.packageName);
1835 } else {
1836 ref = mResourcePackages.get(aInfo.packageName);
1837 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001838 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 if (packageInfo == null || (packageInfo.mResources != null
1840 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001841 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 : "Loading resource-only package ") + aInfo.packageName
1843 + " (in " + (mBoundApplication != null
1844 ? mBoundApplication.processName : null)
1845 + ")");
1846 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001847 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 securityViolation, includeCode &&
1849 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1850 if (includeCode) {
1851 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001852 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 } else {
1854 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001855 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 }
1857 }
1858 return packageInfo;
1859 }
1860 }
1861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 ActivityThread() {
1863 }
1864
1865 public ApplicationThread getApplicationThread()
1866 {
1867 return mAppThread;
1868 }
1869
1870 public Instrumentation getInstrumentation()
1871 {
1872 return mInstrumentation;
1873 }
1874
1875 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001876 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 }
1878
1879 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001880 return mProfiler != null && mProfiler.profileFile != null
1881 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 }
1883
1884 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001885 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 }
1887
1888 public Looper getLooper() {
1889 return mLooper;
1890 }
1891
1892 public Application getApplication() {
1893 return mInitialApplication;
1894 }
Bob Leee5408332009-09-04 18:31:17 -07001895
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001896 public String getProcessName() {
1897 return mBoundApplication.processName;
1898 }
Bob Leee5408332009-09-04 18:31:17 -07001899
Dianne Hackborn21556372010-02-04 16:34:40 -08001900 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 synchronized (this) {
1902 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001903 ContextImpl context =
1904 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001905 LoadedApk info = new LoadedApk(this, "android", context, null,
1906 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 context.init(info, null, this);
1908 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001909 getConfiguration(), getDisplayMetricsLocked(
Jeff Browna492c3a2012-08-23 19:48:44 -07001910 Display.DEFAULT_DISPLAY,
1911 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001913 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 // + ": " + context.getResources().getConfiguration());
1915 }
1916 }
1917 return mSystemContext;
1918 }
1919
Mike Cleron432b7132009-09-24 15:28:29 -07001920 public void installSystemApplicationInfo(ApplicationInfo info) {
1921 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001922 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001923 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001924 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001925
1926 // give ourselves a default profiler
1927 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001928 }
1929 }
1930
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001931 void ensureJitEnabled() {
1932 if (!mJitEnabled) {
1933 mJitEnabled = true;
1934 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1935 }
1936 }
1937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 void scheduleGcIdler() {
1939 if (!mGcIdlerScheduled) {
1940 mGcIdlerScheduled = true;
1941 Looper.myQueue().addIdleHandler(mGcIdler);
1942 }
1943 mH.removeMessages(H.GC_WHEN_IDLE);
1944 }
1945
1946 void unscheduleGcIdler() {
1947 if (mGcIdlerScheduled) {
1948 mGcIdlerScheduled = false;
1949 Looper.myQueue().removeIdleHandler(mGcIdler);
1950 }
1951 mH.removeMessages(H.GC_WHEN_IDLE);
1952 }
1953
1954 void doGcIfNeeded() {
1955 mGcIdlerScheduled = false;
1956 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001957 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 // + "m now=" + now);
1959 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001960 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 BinderInternal.forceGc("bg");
1962 }
1963 }
1964
Jeff Hamilton52d32032011-01-08 15:31:26 -06001965 public void registerOnActivityPausedListener(Activity activity,
1966 OnActivityPausedListener listener) {
1967 synchronized (mOnPauseListeners) {
1968 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1969 if (list == null) {
1970 list = new ArrayList<OnActivityPausedListener>();
1971 mOnPauseListeners.put(activity, list);
1972 }
1973 list.add(listener);
1974 }
1975 }
1976
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001977 public void unregisterOnActivityPausedListener(Activity activity,
1978 OnActivityPausedListener listener) {
1979 synchronized (mOnPauseListeners) {
1980 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1981 if (list != null) {
1982 list.remove(listener);
1983 }
1984 }
1985 }
1986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 public final ActivityInfo resolveActivityInfo(Intent intent) {
1988 ActivityInfo aInfo = intent.resolveActivityInfo(
1989 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1990 if (aInfo == null) {
1991 // Throw an exception.
1992 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07001993 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 }
1995 return aInfo;
1996 }
Bob Leee5408332009-09-04 18:31:17 -07001997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002000 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002001 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002003 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 r.intent = intent;
2005 r.state = state;
2006 r.parent = parent;
2007 r.embeddedID = id;
2008 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002009 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 if (localLOGV) {
2011 ComponentName compname = intent.getComponent();
2012 String name;
2013 if (compname != null) {
2014 name = compname.toShortString();
2015 } else {
2016 name = "(Intent " + intent + ").getComponent() returned null";
2017 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002018 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 + ", comp=" + name
2020 + ", token=" + token);
2021 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07002022 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 }
2024
2025 public final Activity getActivity(IBinder token) {
2026 return mActivities.get(token).activity;
2027 }
2028
2029 public final void sendActivityResult(
2030 IBinder token, String id, int requestCode,
2031 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002032 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07002033 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2035 list.add(new ResultInfo(id, requestCode, resultCode, data));
2036 mAppThread.scheduleSendResult(token, list);
2037 }
2038
2039 // if the thread hasn't started yet, we don't have the handler, so just
2040 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07002041 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 queueOrSendMessage(what, obj, 0, 0);
2043 }
2044
Romain Guy65b345f2011-07-27 18:51:50 -07002045 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 queueOrSendMessage(what, obj, arg1, 0);
2047 }
2048
Romain Guy65b345f2011-07-27 18:51:50 -07002049 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002051 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2053 + ": " + arg1 + " / " + obj);
2054 Message msg = Message.obtain();
2055 msg.what = what;
2056 msg.obj = obj;
2057 msg.arg1 = arg1;
2058 msg.arg2 = arg2;
2059 mH.sendMessage(msg);
2060 }
2061 }
2062
Dianne Hackborn21556372010-02-04 16:34:40 -08002063 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 String what) {
2065 ContextCleanupInfo cci = new ContextCleanupInfo();
2066 cci.context = context;
2067 cci.who = who;
2068 cci.what = what;
2069 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
2070 }
2071
Romain Guy65b345f2011-07-27 18:51:50 -07002072 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2074
2075 ActivityInfo aInfo = r.activityInfo;
2076 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002077 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 Context.CONTEXT_INCLUDE_CODE);
2079 }
Bob Leee5408332009-09-04 18:31:17 -07002080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 ComponentName component = r.intent.getComponent();
2082 if (component == null) {
2083 component = r.intent.resolveActivity(
2084 mInitialApplication.getPackageManager());
2085 r.intent.setComponent(component);
2086 }
2087
2088 if (r.activityInfo.targetActivity != null) {
2089 component = new ComponentName(r.activityInfo.packageName,
2090 r.activityInfo.targetActivity);
2091 }
2092
2093 Activity activity = null;
2094 try {
2095 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2096 activity = mInstrumentation.newActivity(
2097 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002098 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 r.intent.setExtrasClassLoader(cl);
2100 if (r.state != null) {
2101 r.state.setClassLoader(cl);
2102 }
2103 } catch (Exception e) {
2104 if (!mInstrumentation.onException(activity, e)) {
2105 throw new RuntimeException(
2106 "Unable to instantiate activity " + component
2107 + ": " + e.toString(), e);
2108 }
2109 }
2110
2111 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002112 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002113
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002114 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2115 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 TAG, r + ": app=" + app
2117 + ", appName=" + app.getPackageName()
2118 + ", pkg=" + r.packageInfo.getPackageName()
2119 + ", comp=" + r.intent.getComponent().toShortString()
2120 + ", dir=" + r.packageInfo.getAppDir());
2121
2122 if (activity != null) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002123 Context appContext = createBaseContextForActivity(r, activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002125 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002126 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002127 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002128 activity.attach(appContext, this, getInstrumentation(), r.token,
2129 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002130 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07002131
Christopher Tateb70f3df2009-04-07 16:07:59 -07002132 if (customIntent != null) {
2133 activity.mIntent = customIntent;
2134 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002135 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 activity.mStartedActivity = false;
2137 int theme = r.activityInfo.getThemeResource();
2138 if (theme != 0) {
2139 activity.setTheme(theme);
2140 }
2141
2142 activity.mCalled = false;
2143 mInstrumentation.callActivityOnCreate(activity, r.state);
2144 if (!activity.mCalled) {
2145 throw new SuperNotCalledException(
2146 "Activity " + r.intent.getComponent().toShortString() +
2147 " did not call through to super.onCreate()");
2148 }
2149 r.activity = activity;
2150 r.stopped = true;
2151 if (!r.activity.mFinished) {
2152 activity.performStart();
2153 r.stopped = false;
2154 }
2155 if (!r.activity.mFinished) {
2156 if (r.state != null) {
2157 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2158 }
2159 }
2160 if (!r.activity.mFinished) {
2161 activity.mCalled = false;
2162 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2163 if (!activity.mCalled) {
2164 throw new SuperNotCalledException(
2165 "Activity " + r.intent.getComponent().toShortString() +
2166 " did not call through to super.onPostCreate()");
2167 }
2168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 }
2170 r.paused = true;
2171
2172 mActivities.put(r.token, r);
2173
2174 } catch (SuperNotCalledException e) {
2175 throw e;
2176
2177 } catch (Exception e) {
2178 if (!mInstrumentation.onException(activity, e)) {
2179 throw new RuntimeException(
2180 "Unable to start activity " + component
2181 + ": " + e.toString(), e);
2182 }
2183 }
2184
2185 return activity;
2186 }
2187
Jeff Brownefd43bd2012-09-21 17:02:35 -07002188 private Context createBaseContextForActivity(ActivityClientRecord r,
2189 final Activity activity) {
2190 ContextImpl appContext = new ContextImpl();
2191 appContext.init(r.packageInfo, r.token, this);
2192 appContext.setOuterContext(activity);
2193
2194 // For debugging purposes, if the activity's package name contains the value of
2195 // the "debug.use-second-display" system property as a substring, then show
2196 // its content on a secondary display if there is one.
2197 Context baseContext = appContext;
2198 String pkgName = SystemProperties.get("debug.second-display.pkg");
2199 if (pkgName != null && !pkgName.isEmpty()
2200 && r.packageInfo.mPackageName.contains(pkgName)) {
2201 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
2202 for (int displayId : dm.getDisplayIds()) {
2203 if (displayId != Display.DEFAULT_DISPLAY) {
2204 Display display = dm.getRealDisplay(displayId);
2205 baseContext = appContext.createDisplayContext(display);
2206 break;
2207 }
2208 }
2209 }
2210 return baseContext;
2211 }
2212
Romain Guy65b345f2011-07-27 18:51:50 -07002213 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 // If we are getting ready to gc after going to the background, well
2215 // we are back active so skip it.
2216 unscheduleGcIdler();
2217
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002218 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002219 mProfiler.setProfiler(r.profileFile, r.profileFd);
2220 mProfiler.startProfiling();
2221 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002222 }
2223
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002224 // Make sure we are running with the most recent config.
2225 handleConfigurationChanged(null, null);
2226
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002227 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07002229 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230
2231 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002232 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002233 Bundle oldState = r.state;
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002234 handleResumeActivity(r.token, false, r.isForward,
2235 !r.activity.mFinished && !r.startsNotResumed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236
2237 if (!r.activity.mFinished && r.startsNotResumed) {
2238 // The activity manager actually wants this one to start out
2239 // paused, because it needs to be visible but isn't in the
2240 // foreground. We accomplish this by going through the
2241 // normal startup (because activities expect to go through
2242 // onResume() the first time they run, before their window
2243 // is displayed), and then pausing it. However, in this case
2244 // we do -not- need to do the full pause cycle (of freezing
2245 // and such) because the activity manager assumes it can just
2246 // retain the current state it has.
2247 try {
2248 r.activity.mCalled = false;
2249 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002250 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002251 // we need to be created again. But we only do this
2252 // for pre-Honeycomb apps, which always save their state
2253 // when pausing, so we can not have them save their state
2254 // when restarting from a paused state. For HC and later,
2255 // we want to (and can) let the state be saved as the normal
2256 // part of stopping the activity.
2257 if (r.isPreHoneycomb()) {
2258 r.state = oldState;
2259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 if (!r.activity.mCalled) {
2261 throw new SuperNotCalledException(
2262 "Activity " + r.intent.getComponent().toShortString() +
2263 " did not call through to super.onPause()");
2264 }
2265
2266 } catch (SuperNotCalledException e) {
2267 throw e;
2268
2269 } catch (Exception e) {
2270 if (!mInstrumentation.onException(r.activity, e)) {
2271 throw new RuntimeException(
2272 "Unable to pause activity "
2273 + r.intent.getComponent().toShortString()
2274 + ": " + e.toString(), e);
2275 }
2276 }
2277 r.paused = true;
2278 }
2279 } else {
2280 // If there was an error, for any reason, tell the activity
2281 // manager to stop us.
2282 try {
2283 ActivityManagerNative.getDefault()
2284 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2285 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002286 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 }
2288 }
2289 }
2290
Romain Guy65b345f2011-07-27 18:51:50 -07002291 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 List<Intent> intents) {
2293 final int N = intents.size();
2294 for (int i=0; i<N; i++) {
2295 Intent intent = intents.get(i);
2296 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002297 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2299 }
2300 }
2301
2302 public final void performNewIntents(IBinder token,
2303 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002304 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 if (r != null) {
2306 final boolean resumed = !r.paused;
2307 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002308 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 mInstrumentation.callActivityOnPause(r.activity);
2310 }
2311 deliverNewIntents(r, intents);
2312 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002313 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002314 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 }
2316 }
2317 }
Bob Leee5408332009-09-04 18:31:17 -07002318
Romain Guy65b345f2011-07-27 18:51:50 -07002319 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 performNewIntents(data.token, data.intents);
2321 }
2322
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002323 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2324
2325 /**
2326 * Return the Intent that's currently being handled by a
2327 * BroadcastReceiver on this thread, or null if none.
2328 * @hide
2329 */
2330 public static Intent getIntentBeingBroadcast() {
2331 return sCurrentBroadcastIntent.get();
2332 }
2333
Romain Guy65b345f2011-07-27 18:51:50 -07002334 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 // If we are getting ready to gc after going to the background, well
2336 // we are back active so skip it.
2337 unscheduleGcIdler();
2338
2339 String component = data.intent.getComponent().getClassName();
2340
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002341 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002342 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343
2344 IActivityManager mgr = ActivityManagerNative.getDefault();
2345
Romain Guy65b345f2011-07-27 18:51:50 -07002346 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 try {
2348 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2349 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002350 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2352 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002353 if (DEBUG_BROADCAST) Slog.i(TAG,
2354 "Finishing failed broadcast to " + data.intent.getComponent());
2355 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 throw new RuntimeException(
2357 "Unable to instantiate receiver " + component
2358 + ": " + e.toString(), e);
2359 }
2360
2361 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002362 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002363
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002364 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 TAG, "Performing receive of " + data.intent
2366 + ": app=" + app
2367 + ", appName=" + app.getPackageName()
2368 + ", pkg=" + packageInfo.getPackageName()
2369 + ", comp=" + data.intent.getComponent().toShortString()
2370 + ", dir=" + packageInfo.getAppDir());
2371
Dianne Hackborn21556372010-02-04 16:34:40 -08002372 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002373 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002374 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 receiver.onReceive(context.getReceiverRestrictedContext(),
2376 data.intent);
2377 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002378 if (DEBUG_BROADCAST) Slog.i(TAG,
2379 "Finishing failed broadcast to " + data.intent.getComponent());
2380 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 if (!mInstrumentation.onException(receiver, e)) {
2382 throw new RuntimeException(
2383 "Unable to start receiver " + component
2384 + ": " + e.toString(), e);
2385 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002386 } finally {
2387 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 }
2389
Dianne Hackborne829fef2010-10-26 17:44:01 -07002390 if (receiver.getPendingResult() != null) {
2391 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 }
2393 }
2394
Christopher Tate181fafa2009-05-14 11:12:14 -07002395 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002396 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002397 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002398
2399 // no longer idle; we have backup work to do
2400 unscheduleGcIdler();
2401
2402 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002403 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002404 String packageName = packageInfo.mPackageName;
2405 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002406 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002407 + " already exists");
2408 return;
2409 }
Bob Leee5408332009-09-04 18:31:17 -07002410
Christopher Tate181fafa2009-05-14 11:12:14 -07002411 BackupAgent agent = null;
2412 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002413
Christopher Tate79ec80d2011-06-24 14:58:49 -07002414 // full backup operation but no app-supplied agent? use the default implementation
2415 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2416 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002417 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002418 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002419
Christopher Tate181fafa2009-05-14 11:12:14 -07002420 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002421 IBinder binder = null;
2422 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002423 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2424
Christopher Tated1475e02009-07-09 15:36:17 -07002425 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002426 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002427
2428 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002429 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002430 context.init(packageInfo, null, this);
2431 context.setOuterContext(agent);
2432 agent.attach(context);
2433
2434 agent.onCreate();
2435 binder = agent.onBind();
2436 mBackupAgents.put(packageName, agent);
2437 } catch (Exception e) {
2438 // If this is during restore, fail silently; otherwise go
2439 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002440 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002441 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2442 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002443 throw e;
2444 }
2445 // falling through with 'binder' still null
2446 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002447
2448 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002449 try {
2450 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2451 } catch (RemoteException e) {
2452 // nothing to do.
2453 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002454 } catch (Exception e) {
2455 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002456 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002457 }
2458 }
2459
2460 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002461 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002462 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002463
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002464 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002465 String packageName = packageInfo.mPackageName;
2466 BackupAgent agent = mBackupAgents.get(packageName);
2467 if (agent != null) {
2468 try {
2469 agent.onDestroy();
2470 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002471 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002472 e.printStackTrace();
2473 }
2474 mBackupAgents.remove(packageName);
2475 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002476 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002477 }
2478 }
2479
Romain Guy65b345f2011-07-27 18:51:50 -07002480 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 // If we are getting ready to gc after going to the background, well
2482 // we are back active so skip it.
2483 unscheduleGcIdler();
2484
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002485 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002486 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 Service service = null;
2488 try {
2489 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2490 service = (Service) cl.loadClass(data.info.name).newInstance();
2491 } catch (Exception e) {
2492 if (!mInstrumentation.onException(service, e)) {
2493 throw new RuntimeException(
2494 "Unable to instantiate service " + data.info.name
2495 + ": " + e.toString(), e);
2496 }
2497 }
2498
2499 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002500 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501
Dianne Hackborn21556372010-02-04 16:34:40 -08002502 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 context.init(packageInfo, null, this);
2504
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002505 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 context.setOuterContext(service);
2507 service.attach(context, this, data.info.name, data.token, app,
2508 ActivityManagerNative.getDefault());
2509 service.onCreate();
2510 mServices.put(data.token, service);
2511 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002512 ActivityManagerNative.getDefault().serviceDoneExecuting(
2513 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 } catch (RemoteException e) {
2515 // nothing to do.
2516 }
2517 } catch (Exception e) {
2518 if (!mInstrumentation.onException(service, e)) {
2519 throw new RuntimeException(
2520 "Unable to create service " + data.info.name
2521 + ": " + e.toString(), e);
2522 }
2523 }
2524 }
2525
Romain Guy65b345f2011-07-27 18:51:50 -07002526 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002528 if (DEBUG_SERVICE)
2529 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530 if (s != null) {
2531 try {
2532 data.intent.setExtrasClassLoader(s.getClassLoader());
2533 try {
2534 if (!data.rebind) {
2535 IBinder binder = s.onBind(data.intent);
2536 ActivityManagerNative.getDefault().publishService(
2537 data.token, data.intent, binder);
2538 } else {
2539 s.onRebind(data.intent);
2540 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002541 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002543 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 } catch (RemoteException ex) {
2545 }
2546 } catch (Exception e) {
2547 if (!mInstrumentation.onException(s, e)) {
2548 throw new RuntimeException(
2549 "Unable to bind to service " + s
2550 + " with " + data.intent + ": " + e.toString(), e);
2551 }
2552 }
2553 }
2554 }
2555
Romain Guy65b345f2011-07-27 18:51:50 -07002556 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 Service s = mServices.get(data.token);
2558 if (s != null) {
2559 try {
2560 data.intent.setExtrasClassLoader(s.getClassLoader());
2561 boolean doRebind = s.onUnbind(data.intent);
2562 try {
2563 if (doRebind) {
2564 ActivityManagerNative.getDefault().unbindFinished(
2565 data.token, data.intent, doRebind);
2566 } else {
2567 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002568 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 }
2570 } catch (RemoteException ex) {
2571 }
2572 } catch (Exception e) {
2573 if (!mInstrumentation.onException(s, e)) {
2574 throw new RuntimeException(
2575 "Unable to unbind to service " + s
2576 + " with " + data.intent + ": " + e.toString(), e);
2577 }
2578 }
2579 }
2580 }
2581
Dianne Hackborn625ac272010-09-17 18:29:22 -07002582 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002583 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2584 try {
2585 Service s = mServices.get(info.token);
2586 if (s != null) {
2587 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2588 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2589 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002591 } finally {
2592 IoUtils.closeQuietly(info.fd);
2593 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 }
2595 }
2596
Dianne Hackborn625ac272010-09-17 18:29:22 -07002597 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002598 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2599 try {
2600 ActivityClientRecord r = mActivities.get(info.token);
2601 if (r != null && r.activity != null) {
2602 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2603 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2604 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002605 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002606 } finally {
2607 IoUtils.closeQuietly(info.fd);
2608 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002609 }
2610 }
2611
Marco Nelissen18cb2872011-11-15 11:19:53 -08002612 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002613 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2614 try {
2615 ProviderClientRecord r = mLocalProviders.get(info.token);
2616 if (r != null && r.mLocalProvider != null) {
2617 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2618 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2619 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002620 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002621 } finally {
2622 IoUtils.closeQuietly(info.fd);
2623 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002624 }
2625 }
2626
Romain Guy65b345f2011-07-27 18:51:50 -07002627 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 Service s = mServices.get(data.token);
2629 if (s != null) {
2630 try {
2631 if (data.args != null) {
2632 data.args.setExtrasClassLoader(s.getClassLoader());
2633 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002634 int res;
2635 if (!data.taskRemoved) {
2636 res = s.onStartCommand(data.args, data.flags, data.startId);
2637 } else {
2638 s.onTaskRemoved(data.args);
2639 res = Service.START_TASK_REMOVED_COMPLETE;
2640 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002641
2642 QueuedWork.waitToFinish();
2643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002645 ActivityManagerNative.getDefault().serviceDoneExecuting(
2646 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 } catch (RemoteException e) {
2648 // nothing to do.
2649 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002650 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 } catch (Exception e) {
2652 if (!mInstrumentation.onException(s, e)) {
2653 throw new RuntimeException(
2654 "Unable to start service " + s
2655 + " with " + data.args + ": " + e.toString(), e);
2656 }
2657 }
2658 }
2659 }
2660
Romain Guy65b345f2011-07-27 18:51:50 -07002661 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 Service s = mServices.remove(token);
2663 if (s != null) {
2664 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002665 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 s.onDestroy();
2667 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002668 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002670 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002672
2673 QueuedWork.waitToFinish();
2674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002676 ActivityManagerNative.getDefault().serviceDoneExecuting(
2677 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 } catch (RemoteException e) {
2679 // nothing to do.
2680 }
2681 } catch (Exception e) {
2682 if (!mInstrumentation.onException(s, e)) {
2683 throw new RuntimeException(
2684 "Unable to stop service " + s
2685 + ": " + e.toString(), e);
2686 }
2687 }
2688 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002689 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 }
2691
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002692 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002694 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002695 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 + " finished=" + r.activity.mFinished);
2697 if (r != null && !r.activity.mFinished) {
2698 if (clearHide) {
2699 r.hideForNow = false;
2700 r.activity.mStartedActivity = false;
2701 }
2702 try {
2703 if (r.pendingIntents != null) {
2704 deliverNewIntents(r, r.pendingIntents);
2705 r.pendingIntents = null;
2706 }
2707 if (r.pendingResults != null) {
2708 deliverResults(r, r.pendingResults);
2709 r.pendingResults = null;
2710 }
2711 r.activity.performResume();
2712
Bob Leee5408332009-09-04 18:31:17 -07002713 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 r.paused = false;
2717 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 r.state = null;
2719 } catch (Exception e) {
2720 if (!mInstrumentation.onException(r.activity, e)) {
2721 throw new RuntimeException(
2722 "Unable to resume activity "
2723 + r.intent.getComponent().toShortString()
2724 + ": " + e.toString(), e);
2725 }
2726 }
2727 }
2728 return r;
2729 }
2730
Romain Guya998dff2012-03-23 18:58:36 -07002731 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002732 if (r.mPendingRemoveWindow != null) {
2733 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2734 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2735 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07002736 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002737 r.activity.getClass().getName(), "Activity");
2738 }
2739 }
2740 r.mPendingRemoveWindow = null;
2741 r.mPendingRemoveWindowManager = null;
2742 }
2743
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002744 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward,
2745 boolean reallyResume) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 // If we are getting ready to gc after going to the background, well
2747 // we are back active so skip it.
2748 unscheduleGcIdler();
2749
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002750 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751
2752 if (r != null) {
2753 final Activity a = r.activity;
2754
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002755 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 TAG, "Resume " + r + " started activity: " +
2757 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2758 + ", finished: " + a.mFinished);
2759
2760 final int forwardBit = isForward ?
2761 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 // If the window hasn't yet been added to the window manager,
2764 // and this guy didn't finish itself or start another activity,
2765 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002766 boolean willBeVisible = !a.mStartedActivity;
2767 if (!willBeVisible) {
2768 try {
2769 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2770 a.getActivityToken());
2771 } catch (RemoteException e) {
2772 }
2773 }
2774 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 r.window = r.activity.getWindow();
2776 View decor = r.window.getDecorView();
2777 decor.setVisibility(View.INVISIBLE);
2778 ViewManager wm = a.getWindowManager();
2779 WindowManager.LayoutParams l = r.window.getAttributes();
2780 a.mDecor = decor;
2781 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2782 l.softInputMode |= forwardBit;
2783 if (a.mVisibleFromClient) {
2784 a.mWindowAdded = true;
2785 wm.addView(decor, l);
2786 }
2787
2788 // If the window has already been added, but during resume
2789 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002790 // window visible.
2791 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002792 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 TAG, "Launch " + r + " mStartedActivity set");
2794 r.hideForNow = true;
2795 }
2796
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002797 // Get rid of anything left hanging around.
2798 cleanUpPendingRemoveWindows(r);
2799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 // The window is now visible if it has been added, we are not
2801 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002802 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002803 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002805 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002806 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07002808 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 r.newConfig = null;
2810 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002811 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 + isForward);
2813 WindowManager.LayoutParams l = r.window.getAttributes();
2814 if ((l.softInputMode
2815 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2816 != forwardBit) {
2817 l.softInputMode = (l.softInputMode
2818 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2819 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002820 if (r.activity.mVisibleFromClient) {
2821 ViewManager wm = a.getWindowManager();
2822 View decor = r.window.getDecorView();
2823 wm.updateViewLayout(decor, l);
2824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 }
2826 r.activity.mVisibleFromServer = true;
2827 mNumVisibleActivities++;
2828 if (r.activity.mVisibleFromClient) {
2829 r.activity.makeVisible();
2830 }
2831 }
2832
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002833 if (!r.onlyLocalRequest) {
2834 r.nextIdle = mNewActivities;
2835 mNewActivities = r;
2836 if (localLOGV) Slog.v(
2837 TAG, "Scheduling idle handler for " + r);
2838 Looper.myQueue().addIdleHandler(new Idler());
2839 }
2840 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002842 // Tell the activity manager we have resumed.
2843 if (reallyResume) {
2844 try {
2845 ActivityManagerNative.getDefault().activityResumed(token);
2846 } catch (RemoteException ex) {
2847 }
2848 }
2849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 } else {
2851 // If an exception was thrown when trying to resume, then
2852 // just end this activity.
2853 try {
2854 ActivityManagerNative.getDefault()
2855 .finishActivity(token, Activity.RESULT_CANCELED, null);
2856 } catch (RemoteException ex) {
2857 }
2858 }
2859 }
2860
2861 private int mThumbnailWidth = -1;
2862 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002863 private Bitmap mAvailThumbnailBitmap = null;
2864 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865
Romain Guy65b345f2011-07-27 18:51:50 -07002866 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002867 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002869 if (thumbnail == null) {
2870 int w = mThumbnailWidth;
2871 int h;
2872 if (w < 0) {
2873 Resources res = r.activity.getResources();
2874 mThumbnailHeight = h =
2875 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002877 mThumbnailWidth = w =
2878 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2879 } else {
2880 h = mThumbnailHeight;
2881 }
2882
2883 // On platforms where we don't want thumbnails, set dims to (0,0)
2884 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07002885 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
2886 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002887 thumbnail.eraseColor(0);
2888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 }
2890
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002891 if (thumbnail != null) {
2892 Canvas cv = mThumbnailCanvas;
2893 if (cv == null) {
2894 mThumbnailCanvas = cv = new Canvas();
2895 }
2896
2897 cv.setBitmap(thumbnail);
2898 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2899 mAvailThumbnailBitmap = thumbnail;
2900 thumbnail = null;
2901 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002902 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 } catch (Exception e) {
2906 if (!mInstrumentation.onException(r.activity, e)) {
2907 throw new RuntimeException(
2908 "Unable to create thumbnail of "
2909 + r.intent.getComponent().toShortString()
2910 + ": " + e.toString(), e);
2911 }
2912 thumbnail = null;
2913 }
2914
2915 return thumbnail;
2916 }
2917
Romain Guy65b345f2011-07-27 18:51:50 -07002918 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002920 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002922 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 if (userLeaving) {
2924 performUserLeavingActivity(r);
2925 }
Bob Leee5408332009-09-04 18:31:17 -07002926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002928 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002930 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002931 if (r.isPreHoneycomb()) {
2932 QueuedWork.waitToFinish();
2933 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 // Tell the activity manager we have paused.
2936 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002937 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 } catch (RemoteException ex) {
2939 }
2940 }
2941 }
2942
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002943 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 mInstrumentation.callActivityOnUserLeaving(r.activity);
2945 }
2946
2947 final Bundle performPauseActivity(IBinder token, boolean finished,
2948 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002949 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 return r != null ? performPauseActivity(r, finished, saveState) : null;
2951 }
2952
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002953 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 boolean saveState) {
2955 if (r.paused) {
2956 if (r.activity.mFinished) {
2957 // If we are finishing, we won't call onResume() in certain cases.
2958 // So here we likewise don't want to call onPause() if the activity
2959 // isn't resumed.
2960 return null;
2961 }
2962 RuntimeException e = new RuntimeException(
2963 "Performing pause of activity that is not resumed: "
2964 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002965 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 }
2967 Bundle state = null;
2968 if (finished) {
2969 r.activity.mFinished = true;
2970 }
2971 try {
2972 // Next have the activity save its current state and managed dialogs...
2973 if (!r.activity.mFinished && saveState) {
2974 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002975 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2977 r.state = state;
2978 }
2979 // Now we are idle.
2980 r.activity.mCalled = false;
2981 mInstrumentation.callActivityOnPause(r.activity);
2982 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2983 if (!r.activity.mCalled) {
2984 throw new SuperNotCalledException(
2985 "Activity " + r.intent.getComponent().toShortString() +
2986 " did not call through to super.onPause()");
2987 }
2988
2989 } catch (SuperNotCalledException e) {
2990 throw e;
2991
2992 } catch (Exception e) {
2993 if (!mInstrumentation.onException(r.activity, e)) {
2994 throw new RuntimeException(
2995 "Unable to pause activity "
2996 + r.intent.getComponent().toShortString()
2997 + ": " + e.toString(), e);
2998 }
2999 }
3000 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06003001
3002 // Notify any outstanding on paused listeners
3003 ArrayList<OnActivityPausedListener> listeners;
3004 synchronized (mOnPauseListeners) {
3005 listeners = mOnPauseListeners.remove(r.activity);
3006 }
3007 int size = (listeners != null ? listeners.size() : 0);
3008 for (int i = 0; i < size; i++) {
3009 listeners.get(i).onPaused(r.activity);
3010 }
3011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 return state;
3013 }
3014
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003015 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003016 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003017 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 }
3019
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003020 private static class StopInfo implements Runnable {
3021 ActivityClientRecord activity;
3022 Bundle state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 Bitmap thumbnail;
3024 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003025
3026 @Override public void run() {
3027 // Tell activity manager we have been stopped.
3028 try {
3029 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
3030 ActivityManagerNative.getDefault().activityStopped(
3031 activity.token, state, thumbnail, description);
3032 } catch (RemoteException ex) {
3033 }
3034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 }
3036
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003037 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003038 public final IActivityManager.ContentProviderHolder holder;
3039 public final ProviderClientRecord client;
3040 public int stableCount;
3041 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003042
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003043 // When this is set, the stable and unstable ref counts are 0 and
3044 // we have a pending operation scheduled to remove the ref count
3045 // from the activity manager. On the activity manager we are still
3046 // holding an unstable ref, though it is not reflected in the counts
3047 // here.
3048 public boolean removePending;
3049
3050 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
3051 ProviderClientRecord inClient, int sCount, int uCount) {
3052 holder = inHolder;
3053 client = inClient;
3054 stableCount = sCount;
3055 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 }
3057 }
3058
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003059 /**
3060 * Core implementation of stopping an activity. Note this is a little
3061 * tricky because the server's meaning of stop is slightly different
3062 * than our client -- for the server, stop means to save state and give
3063 * it the result when it is done, but the window may still be visible.
3064 * For the client, we want to call onStop()/onStart() to indicate when
3065 * the activity's UI visibillity changes.
3066 */
Romain Guy65b345f2011-07-27 18:51:50 -07003067 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003068 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003069 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003070 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 if (r != null) {
3072 if (!keepShown && r.stopped) {
3073 if (r.activity.mFinished) {
3074 // If we are finishing, we won't call onResume() in certain
3075 // cases. So here we likewise don't want to call onStop()
3076 // if the activity isn't resumed.
3077 return;
3078 }
3079 RuntimeException e = new RuntimeException(
3080 "Performing stop of activity that is not resumed: "
3081 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003082 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 }
3084
3085 if (info != null) {
3086 try {
3087 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003088 // For now, don't create the thumbnail here; we are
3089 // doing that by doing a screen snapshot.
3090 info.thumbnail = null; //createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 info.description = r.activity.onCreateDescription();
3092 } catch (Exception e) {
3093 if (!mInstrumentation.onException(r.activity, e)) {
3094 throw new RuntimeException(
3095 "Unable to save state of activity "
3096 + r.intent.getComponent().toShortString()
3097 + ": " + e.toString(), e);
3098 }
3099 }
3100 }
3101
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003102 // Next have the activity save its current state and managed dialogs...
3103 if (!r.activity.mFinished && saveState) {
3104 if (r.state == null) {
3105 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003106 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003107 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
3108 r.state = state;
3109 } else {
3110 state = r.state;
3111 }
3112 }
3113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 if (!keepShown) {
3115 try {
3116 // Now we are idle.
3117 r.activity.performStop();
3118 } catch (Exception e) {
3119 if (!mInstrumentation.onException(r.activity, e)) {
3120 throw new RuntimeException(
3121 "Unable to stop activity "
3122 + r.intent.getComponent().toShortString()
3123 + ": " + e.toString(), e);
3124 }
3125 }
3126 r.stopped = true;
3127 }
3128
3129 r.paused = true;
3130 }
3131 }
3132
Romain Guy65b345f2011-07-27 18:51:50 -07003133 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 View v = r.activity.mDecor;
3135 if (v != null) {
3136 if (show) {
3137 if (!r.activity.mVisibleFromServer) {
3138 r.activity.mVisibleFromServer = true;
3139 mNumVisibleActivities++;
3140 if (r.activity.mVisibleFromClient) {
3141 r.activity.makeVisible();
3142 }
3143 }
3144 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003145 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003146 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003148 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 r.newConfig = null;
3150 }
3151 } else {
3152 if (r.activity.mVisibleFromServer) {
3153 r.activity.mVisibleFromServer = false;
3154 mNumVisibleActivities--;
3155 v.setVisibility(View.INVISIBLE);
3156 }
3157 }
3158 }
3159 }
3160
Romain Guy65b345f2011-07-27 18:51:50 -07003161 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003162 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 r.activity.mConfigChangeFlags |= configChanges;
3164
3165 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003166 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003168 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 TAG, "Finishing stop of " + r + ": show=" + show
3170 + " win=" + r.window);
3171
3172 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003173
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003174 // Make sure any pending writes are now committed.
3175 if (!r.isPreHoneycomb()) {
3176 QueuedWork.waitToFinish();
3177 }
3178
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003179 // Schedule the call to tell the activity manager we have
3180 // stopped. We don't do this immediately, because we want to
3181 // have a chance for any other pending work (in particular memory
3182 // trim requests) to complete before you tell the activity
3183 // manager to proceed and allow us to go fully into the background.
3184 info.activity = r;
3185 info.state = r.state;
3186 mH.post(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003187 }
3188
3189 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003190 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 if (r.stopped) {
3192 r.activity.performRestart();
3193 r.stopped = false;
3194 }
3195 }
3196
Romain Guy65b345f2011-07-27 18:51:50 -07003197 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003198 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003199
3200 if (r == null) {
3201 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3202 return;
3203 }
3204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003206 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 } else if (show && r.stopped) {
3208 // If we are getting ready to gc after going to the background, well
3209 // we are back active so skip it.
3210 unscheduleGcIdler();
3211
3212 r.activity.performRestart();
3213 r.stopped = false;
3214 }
3215 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003216 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 TAG, "Handle window " + r + " visibility: " + show);
3218 updateVisibility(r, show);
3219 }
3220 }
3221
Romain Guy65b345f2011-07-27 18:51:50 -07003222 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003223 ActivityClientRecord r = mActivities.get(token);
3224
3225 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003226 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003227 return;
3228 }
3229
3230 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003231 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003232 try {
3233 // Now we are idle.
3234 r.activity.performStop();
3235 } catch (Exception e) {
3236 if (!mInstrumentation.onException(r.activity, e)) {
3237 throw new RuntimeException(
3238 "Unable to stop activity "
3239 + r.intent.getComponent().toShortString()
3240 + ": " + e.toString(), e);
3241 }
3242 }
3243 r.stopped = true;
3244 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003245
3246 // Make sure any pending writes are now committed.
3247 if (!r.isPreHoneycomb()) {
3248 QueuedWork.waitToFinish();
3249 }
3250
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003251 // Tell activity manager we slept.
3252 try {
3253 ActivityManagerNative.getDefault().activitySlept(r.token);
3254 } catch (RemoteException ex) {
3255 }
3256 } else {
3257 if (r.stopped && r.activity.mVisibleFromServer) {
3258 r.activity.performRestart();
3259 r.stopped = false;
3260 }
3261 }
3262 }
3263
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003264 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003265 synchronized (mPackages) {
3266 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003267 }
3268 }
3269
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003270 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3271 LoadedApk apk = peekPackageInfo(data.pkg, false);
3272 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003273 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003274 }
3275 apk = peekPackageInfo(data.pkg, true);
3276 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003277 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003278 }
3279 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07003280 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003281 }
3282
Romain Guy65b345f2011-07-27 18:51:50 -07003283 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 final int N = results.size();
3285 for (int i=0; i<N; i++) {
3286 ResultInfo ri = results.get(i);
3287 try {
3288 if (ri.mData != null) {
3289 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
3290 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003291 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003292 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 r.activity.dispatchActivityResult(ri.mResultWho,
3294 ri.mRequestCode, ri.mResultCode, ri.mData);
3295 } catch (Exception e) {
3296 if (!mInstrumentation.onException(r.activity, e)) {
3297 throw new RuntimeException(
3298 "Failure delivering result " + ri + " to activity "
3299 + r.intent.getComponent().toShortString()
3300 + ": " + e.toString(), e);
3301 }
3302 }
3303 }
3304 }
3305
Romain Guy65b345f2011-07-27 18:51:50 -07003306 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003307 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003308 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 if (r != null) {
3310 final boolean resumed = !r.paused;
3311 if (!r.activity.mFinished && r.activity.mDecor != null
3312 && r.hideForNow && resumed) {
3313 // We had hidden the activity because it started another
3314 // one... we have gotten a result back and we are not
3315 // paused, so make sure our window is visible.
3316 updateVisibility(r, true);
3317 }
3318 if (resumed) {
3319 try {
3320 // Now we are idle.
3321 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003322 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 mInstrumentation.callActivityOnPause(r.activity);
3324 if (!r.activity.mCalled) {
3325 throw new SuperNotCalledException(
3326 "Activity " + r.intent.getComponent().toShortString()
3327 + " did not call through to super.onPause()");
3328 }
3329 } catch (SuperNotCalledException e) {
3330 throw e;
3331 } catch (Exception e) {
3332 if (!mInstrumentation.onException(r.activity, e)) {
3333 throw new RuntimeException(
3334 "Unable to pause activity "
3335 + r.intent.getComponent().toShortString()
3336 + ": " + e.toString(), e);
3337 }
3338 }
3339 }
3340 deliverResults(r, res.results);
3341 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003342 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003343 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 }
3345 }
3346 }
3347
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003348 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 return performDestroyActivity(token, finishing, 0, false);
3350 }
3351
Romain Guy65b345f2011-07-27 18:51:50 -07003352 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003354 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003355 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003356 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003358 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 r.activity.mConfigChangeFlags |= configChanges;
3360 if (finishing) {
3361 r.activity.mFinished = true;
3362 }
3363 if (!r.paused) {
3364 try {
3365 r.activity.mCalled = false;
3366 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07003367 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003368 r.activity.getComponentName().getClassName());
3369 if (!r.activity.mCalled) {
3370 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003371 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 + " did not call through to super.onPause()");
3373 }
3374 } catch (SuperNotCalledException e) {
3375 throw e;
3376 } catch (Exception e) {
3377 if (!mInstrumentation.onException(r.activity, e)) {
3378 throw new RuntimeException(
3379 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003380 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 + ": " + e.toString(), e);
3382 }
3383 }
3384 r.paused = true;
3385 }
3386 if (!r.stopped) {
3387 try {
3388 r.activity.performStop();
3389 } catch (SuperNotCalledException e) {
3390 throw e;
3391 } catch (Exception e) {
3392 if (!mInstrumentation.onException(r.activity, e)) {
3393 throw new RuntimeException(
3394 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003395 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 + ": " + e.toString(), e);
3397 }
3398 }
3399 r.stopped = true;
3400 }
3401 if (getNonConfigInstance) {
3402 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003403 r.lastNonConfigurationInstances
3404 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405 } catch (Exception e) {
3406 if (!mInstrumentation.onException(r.activity, e)) {
3407 throw new RuntimeException(
3408 "Unable to retain activity "
3409 + r.intent.getComponent().toShortString()
3410 + ": " + e.toString(), e);
3411 }
3412 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 }
3414 try {
3415 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003416 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 if (!r.activity.mCalled) {
3418 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003419 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003420 " did not call through to super.onDestroy()");
3421 }
3422 if (r.window != null) {
3423 r.window.closeAllPanels();
3424 }
3425 } catch (SuperNotCalledException e) {
3426 throw e;
3427 } catch (Exception e) {
3428 if (!mInstrumentation.onException(r.activity, e)) {
3429 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003430 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3431 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 }
3433 }
3434 }
3435 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003436 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437 return r;
3438 }
3439
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003440 private static String safeToComponentShortString(Intent intent) {
3441 ComponentName component = intent.getComponent();
3442 return component == null ? "[Unknown]" : component.toShortString();
3443 }
3444
Romain Guy65b345f2011-07-27 18:51:50 -07003445 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003447 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 configChanges, getNonConfigInstance);
3449 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003450 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 WindowManager wm = r.activity.getWindowManager();
3452 View v = r.activity.mDecor;
3453 if (v != null) {
3454 if (r.activity.mVisibleFromServer) {
3455 mNumVisibleActivities--;
3456 }
3457 IBinder wtoken = v.getWindowToken();
3458 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003459 if (r.onlyLocalRequest) {
3460 // Hold off on removing this until the new activity's
3461 // window is being added.
3462 r.mPendingRemoveWindow = v;
3463 r.mPendingRemoveWindowManager = wm;
3464 } else {
3465 wm.removeViewImmediate(v);
3466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003468 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003469 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 r.activity.getClass().getName(), "Activity");
3471 }
3472 r.activity.mDecor = null;
3473 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003474 if (r.mPendingRemoveWindow == null) {
3475 // If we are delaying the removal of the activity window, then
3476 // we can't clean up all windows here. Note that we can't do
3477 // so later either, which means any windows that aren't closed
3478 // by the app will leak. Well we try to warning them a lot
3479 // about leaking windows, because that is a bug, so if they are
3480 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07003481 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003482 r.activity.getClass().getName(), "Activity");
3483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484
3485 // Mocked out contexts won't be participating in the normal
3486 // process lifecycle, but if we're running with a proper
3487 // ApplicationContext we need to have it tear down things
3488 // cleanly.
3489 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003490 if (c instanceof ContextImpl) {
3491 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 r.activity.getClass().getName(), "Activity");
3493 }
3494 }
3495 if (finishing) {
3496 try {
3497 ActivityManagerNative.getDefault().activityDestroyed(token);
3498 } catch (RemoteException ex) {
3499 // If the system process has died, it's game over for everyone.
3500 }
3501 }
3502 }
3503
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003504 public final void requestRelaunchActivity(IBinder token,
3505 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3506 int configChanges, boolean notResumed, Configuration config,
3507 boolean fromServer) {
3508 ActivityClientRecord target = null;
3509
3510 synchronized (mPackages) {
3511 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3512 ActivityClientRecord r = mRelaunchingActivities.get(i);
3513 if (r.token == token) {
3514 target = r;
3515 if (pendingResults != null) {
3516 if (r.pendingResults != null) {
3517 r.pendingResults.addAll(pendingResults);
3518 } else {
3519 r.pendingResults = pendingResults;
3520 }
3521 }
3522 if (pendingNewIntents != null) {
3523 if (r.pendingIntents != null) {
3524 r.pendingIntents.addAll(pendingNewIntents);
3525 } else {
3526 r.pendingIntents = pendingNewIntents;
3527 }
3528 }
3529 break;
3530 }
3531 }
3532
3533 if (target == null) {
3534 target = new ActivityClientRecord();
3535 target.token = token;
3536 target.pendingResults = pendingResults;
3537 target.pendingIntents = pendingNewIntents;
3538 if (!fromServer) {
3539 ActivityClientRecord existing = mActivities.get(token);
3540 if (existing != null) {
3541 target.startsNotResumed = existing.paused;
3542 }
3543 target.onlyLocalRequest = true;
3544 }
3545 mRelaunchingActivities.add(target);
3546 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3547 }
3548
3549 if (fromServer) {
3550 target.startsNotResumed = notResumed;
3551 target.onlyLocalRequest = false;
3552 }
3553 if (config != null) {
3554 target.createdConfig = config;
3555 }
3556 target.pendingConfigChanges |= configChanges;
3557 }
3558 }
3559
Romain Guy65b345f2011-07-27 18:51:50 -07003560 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 // If we are getting ready to gc after going to the background, well
3562 // we are back active so skip it.
3563 unscheduleGcIdler();
3564
3565 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003566 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 // First: make sure we have the most recent configuration and most
3569 // recent version of the activity, or skip it if some previous call
3570 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003571 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 int N = mRelaunchingActivities.size();
3573 IBinder token = tmp.token;
3574 tmp = null;
3575 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003576 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 if (r.token == token) {
3578 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003579 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580 mRelaunchingActivities.remove(i);
3581 i--;
3582 N--;
3583 }
3584 }
Bob Leee5408332009-09-04 18:31:17 -07003585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003586 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003587 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 return;
3589 }
Bob Leee5408332009-09-04 18:31:17 -07003590
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003591 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3592 + tmp.token + " with configChanges=0x"
3593 + Integer.toHexString(configChanges));
3594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003595 if (mPendingConfiguration != null) {
3596 changedConfig = mPendingConfiguration;
3597 mPendingConfiguration = null;
3598 }
3599 }
Bob Leee5408332009-09-04 18:31:17 -07003600
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003601 if (tmp.createdConfig != null) {
3602 // If the activity manager is passing us its current config,
3603 // assume that is really what we want regardless of what we
3604 // may have pending.
3605 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003606 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3607 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3608 if (changedConfig == null
3609 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3610 changedConfig = tmp.createdConfig;
3611 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003612 }
3613 }
3614
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003615 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003616 + tmp.token + ": changedConfig=" + changedConfig);
3617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003618 // If there was a pending configuration change, execute it first.
3619 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003620 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003621 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003622 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 }
Bob Leee5408332009-09-04 18:31:17 -07003624
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003625 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003626 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627 if (r == null) {
3628 return;
3629 }
Bob Leee5408332009-09-04 18:31:17 -07003630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003631 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003632 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003633 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003634
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003635 r.activity.mChangingConfigurations = true;
3636
Dianne Hackborne2b04802010-12-09 09:24:55 -08003637 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003638 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003639 performPauseActivity(r.token, false, r.isPreHoneycomb());
3640 }
3641 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3642 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003643 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003644 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003645 }
Bob Leee5408332009-09-04 18:31:17 -07003646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003649 r.activity = null;
3650 r.window = null;
3651 r.hideForNow = false;
3652 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003653 // Merge any pending results and pending intents; don't just replace them
3654 if (tmp.pendingResults != null) {
3655 if (r.pendingResults == null) {
3656 r.pendingResults = tmp.pendingResults;
3657 } else {
3658 r.pendingResults.addAll(tmp.pendingResults);
3659 }
3660 }
3661 if (tmp.pendingIntents != null) {
3662 if (r.pendingIntents == null) {
3663 r.pendingIntents = tmp.pendingIntents;
3664 } else {
3665 r.pendingIntents.addAll(tmp.pendingIntents);
3666 }
3667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003668 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003669
Christopher Tateb70f3df2009-04-07 16:07:59 -07003670 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003671 }
3672
Romain Guy65b345f2011-07-27 18:51:50 -07003673 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003674 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003675 Bitmap thumbnail = createThumbnailBitmap(r);
3676 CharSequence description = null;
3677 try {
3678 description = r.activity.onCreateDescription();
3679 } catch (Exception e) {
3680 if (!mInstrumentation.onException(r.activity, e)) {
3681 throw new RuntimeException(
3682 "Unable to create description of activity "
3683 + r.intent.getComponent().toShortString()
3684 + ": " + e.toString(), e);
3685 }
3686 }
3687 //System.out.println("Reporting top thumbnail " + thumbnail);
3688 try {
3689 ActivityManagerNative.getDefault().reportThumbnail(
3690 token, thumbnail, description);
3691 } catch (RemoteException ex) {
3692 }
3693 }
3694
Dianne Hackborn73c14162012-09-19 15:45:06 -07003695 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003697 ArrayList<ComponentCallbacks2> callbacks
3698 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003699
Dianne Hackborn73c14162012-09-19 15:45:06 -07003700 synchronized (mPackages) {
3701 final int N = mAllApplications.size();
3702 for (int i=0; i<N; i++) {
3703 callbacks.add(mAllApplications.get(i));
3704 }
3705 if (mActivities.size() > 0) {
3706 for (ActivityClientRecord ar : mActivities.values()) {
3707 Activity a = ar.activity;
3708 if (a != null) {
3709 Configuration thisConfig = applyConfigCompatMainThread(mCurDefaultDisplayDpi,
3710 newConfig, ar.packageInfo.mCompatibilityInfo.getIfNeeded());
3711 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
3712 // If the activity is currently resumed, its configuration
3713 // needs to change right now.
3714 callbacks.add(a);
3715 } else if (thisConfig != null) {
3716 // Otherwise, we will tell it about the change
3717 // the next time it is resumed or shown. Note that
3718 // the activity manager may, before then, decide the
3719 // activity needs to be destroyed to handle its new
3720 // configuration.
3721 if (DEBUG_CONFIGURATION) {
3722 Slog.v(TAG, "Setting activity "
3723 + ar.activityInfo.name + " newConfig=" + thisConfig);
3724 }
3725 ar.newConfig = thisConfig;
Romain Guya998dff2012-03-23 18:58:36 -07003726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 }
3728 }
3729 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07003730 if (mServices.size() > 0) {
3731 for (Service service : mServices.values()) {
3732 callbacks.add(service);
3733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003734 }
3735 }
3736 synchronized (mProviderMap) {
3737 if (mLocalProviders.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003738 for (ProviderClientRecord providerClientRecord : mLocalProviders.values()) {
3739 callbacks.add(providerClientRecord.mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 }
3741 }
3742 }
Bob Leee5408332009-09-04 18:31:17 -07003743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003744 return callbacks;
3745 }
Bob Leee5408332009-09-04 18:31:17 -07003746
Romain Guya998dff2012-03-23 18:58:36 -07003747 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003749 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 // we check the runtime type and act accordingly.
3751 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3752 if (activity != null) {
3753 activity.mCalled = false;
3754 }
Bob Leee5408332009-09-04 18:31:17 -07003755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 boolean shouldChangeConfig = false;
3757 if ((activity == null) || (activity.mCurrentConfig == null)) {
3758 shouldChangeConfig = true;
3759 } else {
Bob Leee5408332009-09-04 18:31:17 -07003760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003761 // If the new config is the same as the config this Activity
3762 // is already running with then don't bother calling
3763 // onConfigurationChanged
3764 int diff = activity.mCurrentConfig.diff(config);
3765 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003766 // If this activity doesn't handle any of the config changes
3767 // then don't bother calling onConfigurationChanged as we're
3768 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003769 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 shouldChangeConfig = true;
3771 }
3772 }
3773 }
Bob Leee5408332009-09-04 18:31:17 -07003774
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003775 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003776 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 if (shouldChangeConfig) {
3778 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003780 if (activity != null) {
3781 if (!activity.mCalled) {
3782 throw new SuperNotCalledException(
3783 "Activity " + activity.getLocalClassName() +
3784 " did not call through to super.onConfigurationChanged()");
3785 }
3786 activity.mConfigChangeFlags = 0;
3787 activity.mCurrentConfig = new Configuration(config);
3788 }
3789 }
3790 }
3791
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003792 public final void applyConfigurationToResources(Configuration config) {
3793 synchronized (mPackages) {
3794 applyConfigurationToResourcesLocked(config, null);
3795 }
3796 }
3797
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003798 final boolean applyConfigurationToResourcesLocked(Configuration config,
3799 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003800 if (mResConfiguration == null) {
3801 mResConfiguration = new Configuration();
3802 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003803 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003804 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003805 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003806 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003807 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003808 int changes = mResConfiguration.updateFrom(config);
Jeff Browna492c3a2012-08-23 19:48:44 -07003809 flushDisplayMetricsLocked();
3810 DisplayMetrics defaultDisplayMetrics = getDisplayMetricsLocked(
3811 Display.DEFAULT_DISPLAY, null);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003812
3813 if (compat != null && (mResCompatibilityInfo == null ||
3814 !mResCompatibilityInfo.equals(compat))) {
3815 mResCompatibilityInfo = compat;
3816 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3817 | ActivityInfo.CONFIG_SCREEN_SIZE
3818 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3819 }
Bob Leee5408332009-09-04 18:31:17 -07003820
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003821 // set it for java, this also affects newly created Resources
3822 if (config.locale != null) {
3823 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 }
Bob Leee5408332009-09-04 18:31:17 -07003825
Jeff Browna492c3a2012-08-23 19:48:44 -07003826 Resources.updateSystemConfiguration(config, defaultDisplayMetrics, compat);
Bob Leee5408332009-09-04 18:31:17 -07003827
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003828 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003829 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborn756220b2012-08-14 16:45:30 -07003830
3831 Configuration tmpConfig = null;
3832
3833 Iterator<Map.Entry<ResourcesKey, WeakReference<Resources>>> it =
3834 mActiveResources.entrySet().iterator();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003835 while (it.hasNext()) {
Dianne Hackborn756220b2012-08-14 16:45:30 -07003836 Map.Entry<ResourcesKey, WeakReference<Resources>> entry = it.next();
3837 Resources r = entry.getValue().get();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003838 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003839 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003840 + r + " config to: " + config);
Jeff Browna492c3a2012-08-23 19:48:44 -07003841 int displayId = entry.getKey().mDisplayId;
3842 boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
3843 DisplayMetrics dm = defaultDisplayMetrics;
3844 Configuration overrideConfig = entry.getKey().mOverrideConfiguration;
3845 if (!isDefaultDisplay || overrideConfig != null) {
Dianne Hackborn756220b2012-08-14 16:45:30 -07003846 if (tmpConfig == null) {
3847 tmpConfig = new Configuration();
3848 }
3849 tmpConfig.setTo(config);
Jeff Browna492c3a2012-08-23 19:48:44 -07003850 if (!isDefaultDisplay) {
3851 dm = getDisplayMetricsLocked(displayId, null);
3852 applyNonDefaultDisplayMetricsToConfigurationLocked(dm, tmpConfig);
3853 }
3854 if (overrideConfig != null) {
3855 tmpConfig.updateFrom(overrideConfig);
3856 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07003857 r.updateConfiguration(tmpConfig, dm, compat);
3858 } else {
3859 r.updateConfiguration(config, dm, compat);
3860 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003861 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003862 // + " " + r + ": " + r.getConfiguration());
3863 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003864 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003865 it.remove();
3866 }
3867 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003868
3869 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003870 }
Dianne Hackborn836e2622011-10-04 18:32:39 -07003871
Jeff Browna492c3a2012-08-23 19:48:44 -07003872 final void applyNonDefaultDisplayMetricsToConfigurationLocked(
3873 DisplayMetrics dm, Configuration config) {
3874 config.screenLayout = Configuration.SCREENLAYOUT_SIZE_XLARGE
3875 | Configuration.SCREENLAYOUT_LONG_NO;
3876 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
3877 config.orientation = (dm.widthPixels >= dm.heightPixels) ?
3878 Configuration.ORIENTATION_LANDSCAPE : Configuration.ORIENTATION_PORTRAIT;
3879 config.densityDpi = dm.densityDpi;
3880 config.screenWidthDp = (int)(dm.widthPixels / dm.density);
3881 config.screenHeightDp = (int)(dm.heightPixels / dm.density);
3882 config.smallestScreenWidthDp = config.screenWidthDp; // assume screen does not rotate
3883 config.compatScreenWidthDp = config.screenWidthDp;
3884 config.compatScreenHeightDp = config.screenHeightDp;
3885 config.compatSmallestScreenWidthDp = config.smallestScreenWidthDp;
3886 }
3887
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003888 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07003889 Configuration config = mConfiguration;
3890 if (mCompatConfiguration == null) {
3891 mCompatConfiguration = new Configuration();
3892 }
3893 mCompatConfiguration.setTo(mConfiguration);
3894 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003895 mResCompatibilityInfo.applyToConfiguration(displayDensity, mCompatConfiguration);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003896 config = mCompatConfiguration;
3897 }
3898 return config;
3899 }
3900
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003901 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003902
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003903 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003904
3905 synchronized (mPackages) {
3906 if (mPendingConfiguration != null) {
3907 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3908 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003909 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003910 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003911 }
3912 mPendingConfiguration = null;
3913 }
3914
3915 if (config == null) {
3916 return;
3917 }
3918
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003919 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003920 + config);
3921
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003922 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003924 if (mConfiguration == null) {
3925 mConfiguration = new Configuration();
3926 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003927 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003928 return;
3929 }
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003930 configDiff = mConfiguration.diff(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003931 mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003932 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003933 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07003934
3935 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
3936
Romain Guy65b345f2011-07-27 18:51:50 -07003937 // Cleanup hardware accelerated stuff
Jeff Brown98365d72012-08-19 20:30:52 -07003938 WindowManagerGlobal.getInstance().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003939
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003940 freeTextLayoutCachesIfNeeded(configDiff);
3941
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003942 if (callbacks != null) {
3943 final int N = callbacks.size();
3944 for (int i=0; i<N; i++) {
3945 performConfigurationChanged(callbacks.get(i), config);
3946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 }
3948 }
3949
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003950 final void freeTextLayoutCachesIfNeeded(int configDiff) {
3951 if (configDiff != 0) {
3952 // Ask text layout engine to free its caches if there is a locale change
3953 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
3954 if (hasLocaleConfigChange) {
3955 Canvas.freeTextLayoutCaches();
3956 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
3957 }
3958 }
3959 }
3960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003961 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003962 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 if (r == null || r.activity == null) {
3964 return;
3965 }
Bob Leee5408332009-09-04 18:31:17 -07003966
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003967 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003968 + r.activityInfo.name);
3969
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003970 performConfigurationChanged(r.activity, mCompatConfiguration);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003971
3972 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 }
3974
Romain Guy7eabe552011-07-21 14:56:34 -07003975 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003976 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003977 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003978 switch (profileType) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003979 default:
3980 mProfiler.setProfiler(pcd.path, pcd.fd);
3981 mProfiler.autoStopProfiler = false;
3982 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003983 break;
3984 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003985 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003986 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003987 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003988 } finally {
3989 try {
3990 pcd.fd.close();
3991 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003992 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003993 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003994 }
3995 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003996 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07003997 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003998 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003999 break;
Romain Guy7eabe552011-07-21 14:56:34 -07004000 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004001 }
4002 }
Bob Leee5408332009-09-04 18:31:17 -07004003
Romain Guya998dff2012-03-23 18:58:36 -07004004 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07004005 if (managed) {
4006 try {
4007 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
4008 } catch (IOException e) {
4009 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
4010 + " -- can the process access this path?");
4011 } finally {
4012 try {
4013 dhd.fd.close();
4014 } catch (IOException e) {
4015 Slog.w(TAG, "Failure closing profile fd", e);
4016 }
4017 }
4018 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07004019 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07004020 }
4021 }
4022
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004023 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
4024 boolean hasPkgInfo = false;
4025 if (packages != null) {
4026 for (int i=packages.length-1; i>=0; i--) {
4027 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
4028 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004029 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004030 ref = mPackages.get(packages[i]);
4031 if (ref != null && ref.get() != null) {
4032 hasPkgInfo = true;
4033 } else {
4034 ref = mResourcePackages.get(packages[i]);
4035 if (ref != null && ref.get() != null) {
4036 hasPkgInfo = true;
4037 }
4038 }
4039 }
4040 mPackages.remove(packages[i]);
4041 mResourcePackages.remove(packages[i]);
4042 }
4043 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08004044 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004045 hasPkgInfo);
4046 }
4047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004049 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07004050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004051 final int N = callbacks.size();
4052 for (int i=0; i<N; i++) {
4053 callbacks.get(i).onLowMemory();
4054 }
4055
Chris Tatece229052009-03-25 16:44:52 -07004056 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
4057 if (Process.myUid() != Process.SYSTEM_UID) {
4058 int sqliteReleased = SQLiteDatabase.releaseMemory();
4059 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
4060 }
Bob Leee5408332009-09-04 18:31:17 -07004061
Mike Reedcaf0df12009-04-27 14:32:05 -04004062 // Ask graphics to free up as much as possible (font/image caches)
4063 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004065 // Ask text layout engine to free also as much as possible
4066 Canvas.freeTextLayoutCaches();
4067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 BinderInternal.forceGc("mem");
4069 }
4070
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004071 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004072 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004073
Jeff Brown98365d72012-08-19 20:30:52 -07004074 final WindowManagerGlobal windowManager = WindowManagerGlobal.getInstance();
Romain Guy19f86e82012-04-23 15:19:07 -07004075 windowManager.startTrimMemory(level);
4076
Dianne Hackborn73c14162012-09-19 15:45:06 -07004077 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004078
4079 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07004080 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004081 callbacks.get(i).onTrimMemory(level);
4082 }
Romain Guy19f86e82012-04-23 15:19:07 -07004083
Jeff Brown98365d72012-08-19 20:30:52 -07004084 windowManager.endTrimMemory();
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004085 }
4086
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004087 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004088 if (Process.isIsolated()) {
4089 // Isolated processes aren't going to do UI.
4090 return;
4091 }
Romain Guya9582652011-11-10 14:20:10 -08004092 try {
4093 int uid = Process.myUid();
4094 String[] packages = getPackageManager().getPackagesForUid(uid);
4095
4096 // If there are several packages in this application we won't
4097 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004098 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004099 HardwareRenderer.setupDiskCache(cacheDir);
4100 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08004101 }
4102 } catch (RemoteException e) {
4103 // Ignore
4104 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07004105 }
4106
4107 private void updateDefaultDensity() {
4108 if (mCurDefaultDisplayDpi != Configuration.DENSITY_DPI_UNDEFINED
4109 && mCurDefaultDisplayDpi != DisplayMetrics.DENSITY_DEVICE
4110 && !mDensityCompatMode) {
4111 Slog.i(TAG, "Switching default density from "
4112 + DisplayMetrics.DENSITY_DEVICE + " to "
4113 + mCurDefaultDisplayDpi);
4114 DisplayMetrics.DENSITY_DEVICE = mCurDefaultDisplayDpi;
4115 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4116 }
4117 }
4118
Romain Guy65b345f2011-07-27 18:51:50 -07004119 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004120 mBoundApplication = data;
4121 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004122 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004123
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004124 mProfiler = new Profiler();
4125 mProfiler.profileFile = data.initProfileFile;
4126 mProfiler.profileFd = data.initProfileFd;
4127 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
4128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004129 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08004130 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004131 android.ddm.DdmHandleAppName.setAppName(data.processName,
4132 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004134 if (data.persistent) {
4135 // Persistent processes on low-memory devices do not get to
4136 // use hardware accelerated drawing, since this can add too much
4137 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07004138 if (!ActivityManager.isHighEndGfx()) {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004139 HardwareRenderer.disable(false);
4140 }
4141 }
Romain Guya9582652011-11-10 14:20:10 -08004142
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004143 if (mProfiler.profileFd != null) {
4144 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004145 }
4146
Joe Onoratod630f102011-03-17 18:42:26 -07004147 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
4148 // implementation to use the pool executor. Normally, we use the
4149 // serialized executor as the default. This has to happen in the
4150 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07004151 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07004152 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
4153 }
4154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004155 /*
4156 * Before spawning a new process, reset the time zone to be the system time zone.
4157 * This needs to be done because the system time zone could have changed after the
4158 * the spawning of this process. Without doing this this process would have the incorrect
4159 * system time zone.
4160 */
4161 TimeZone.setDefault(null);
4162
4163 /*
4164 * Initialize the default locale in this process for the reasons we set the time zone.
4165 */
4166 Locale.setDefault(data.config.locale);
4167
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004168 /*
4169 * Update the system configuration since its preloaded and might not
4170 * reflect configuration changes. The configuration object passed
4171 * in AppBindData can be safely assumed to be up to date
4172 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004173 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004174 mCurDefaultDisplayDpi = data.config.densityDpi;
4175 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004176
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004177 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178
Dianne Hackborndde331c2012-08-03 14:01:57 -07004179 /**
4180 * Switch this process to density compatibility mode if needed.
4181 */
4182 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4183 == 0) {
4184 mDensityCompatMode = true;
4185 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4186 }
4187 updateDefaultDensity();
4188
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004189 final ContextImpl appContext = new ContextImpl();
4190 appContext.init(data.info, null, this);
Amith Yamasani11de39a2012-08-17 18:00:52 -07004191 if (!Process.isIsolated()) {
4192 final File cacheDir = appContext.getCacheDir();
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004193
Amith Yamasani92d57052012-08-23 10:07:52 -07004194 if (cacheDir != null) {
4195 // Provide a usable directory for temporary files
4196 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
4197
4198 setupGraphicsSupport(data.info, cacheDir);
4199 } else {
4200 Log.e(TAG, "Unable to setupGraphicsSupport due to missing cache directory");
4201 }
Amith Yamasani11de39a2012-08-17 18:00:52 -07004202 }
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004203 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004204 * For system applications on userdebug/eng builds, log stack
4205 * traces of disk and network access to dropbox for analysis.
4206 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004207 if ((data.appInfo.flags &
4208 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004209 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4210 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004211 }
4212
4213 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004214 * For apps targetting SDK Honeycomb or later, we don't allow
4215 * network usage on the main event loop / UI thread.
4216 *
4217 * Note to those grepping: this is what ultimately throws
4218 * NetworkOnMainThreadException ...
4219 */
4220 if (data.appInfo.targetSdkVersion > 9) {
4221 StrictMode.enableDeathOnNetwork();
4222 }
4223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4225 // XXX should have option to change the port.
4226 Debug.changeDebugPort(8100);
4227 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004228 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 + " is waiting for the debugger on port 8100...");
4230
4231 IActivityManager mgr = ActivityManagerNative.getDefault();
4232 try {
4233 mgr.showWaitingForDebugger(mAppThread, true);
4234 } catch (RemoteException ex) {
4235 }
4236
4237 Debug.waitForDebugger();
4238
4239 try {
4240 mgr.showWaitingForDebugger(mAppThread, false);
4241 } catch (RemoteException ex) {
4242 }
4243
4244 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004245 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004246 + " can be debugged on port 8100...");
4247 }
4248 }
4249
Siva Velusamy92a8b222012-03-09 16:24:04 -08004250 // Enable OpenGL tracing if required
4251 if (data.enableOpenGlTrace) {
4252 GLUtils.enableTracing();
4253 }
4254
Robert Greenwalt434203a2010-10-11 16:00:27 -07004255 /**
4256 * Initialize the default http proxy in this process for the reasons we set the time zone.
4257 */
4258 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004259 if (b != null) {
4260 // In pre-boot mode (doing initial launch to collect password), not
4261 // all system is up. This includes the connectivity service, so don't
4262 // crash if we can't get it.
4263 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4264 try {
4265 ProxyProperties proxyProperties = service.getProxy();
4266 Proxy.setHttpProxySystemProperty(proxyProperties);
4267 } catch (RemoteException e) {}
4268 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004270 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004271 InstrumentationInfo ii = null;
4272 try {
4273 ii = appContext.getPackageManager().
4274 getInstrumentationInfo(data.instrumentationName, 0);
4275 } catch (PackageManager.NameNotFoundException e) {
4276 }
4277 if (ii == null) {
4278 throw new RuntimeException(
4279 "Unable to find instrumentation info for: "
4280 + data.instrumentationName);
4281 }
4282
4283 mInstrumentationAppDir = ii.sourceDir;
Brian Carlstromd893a892012-04-01 21:30:26 -07004284 mInstrumentationAppLibraryDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 mInstrumentationAppPackage = ii.packageName;
4286 mInstrumentedAppDir = data.info.getAppDir();
Brian Carlstromd893a892012-04-01 21:30:26 -07004287 mInstrumentedAppLibraryDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288
4289 ApplicationInfo instrApp = new ApplicationInfo();
4290 instrApp.packageName = ii.packageName;
4291 instrApp.sourceDir = ii.sourceDir;
4292 instrApp.publicSourceDir = ii.publicSourceDir;
4293 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004294 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004295 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004296 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08004297 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004298 instrContext.init(pi, null, this);
4299
4300 try {
4301 java.lang.ClassLoader cl = instrContext.getClassLoader();
4302 mInstrumentation = (Instrumentation)
4303 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4304 } catch (Exception e) {
4305 throw new RuntimeException(
4306 "Unable to instantiate instrumentation "
4307 + data.instrumentationName + ": " + e.toString(), e);
4308 }
4309
4310 mInstrumentation.init(this, instrContext, appContext,
4311 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
4312
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004313 if (mProfiler.profileFile != null && !ii.handleProfiling
4314 && mProfiler.profileFd == null) {
4315 mProfiler.handlingProfiling = true;
4316 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004317 file.getParentFile().mkdirs();
4318 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4319 }
4320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 } else {
4322 mInstrumentation = new Instrumentation();
4323 }
4324
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004325 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004326 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004327 }
4328
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004329 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004330 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004331 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004332 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004334 // If the app is being launched for full backup or restore, bring it up in
4335 // a restricted environment with the base application class.
4336 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4337 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004338
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004339 // don't bring up providers in restricted mode; they may depend on the
4340 // app's custom Application class
4341 if (!data.restrictedBackupMode) {
4342 List<ProviderInfo> providers = data.providers;
4343 if (providers != null) {
4344 installContentProviders(app, providers);
4345 // For process that contains content providers, we want to
4346 // ensure that the JIT is enabled "at some point".
4347 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4348 }
4349 }
4350
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004351 // Do this after providers, since instrumentation tests generally start their
4352 // test thread at this point, and we don't want that racing.
4353 try {
4354 mInstrumentation.onCreate(data.instrumentationArgs);
4355 }
4356 catch (Exception e) {
4357 throw new RuntimeException(
4358 "Exception thrown in onCreate() of "
4359 + data.instrumentationName + ": " + e.toString(), e);
4360 }
4361
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004362 try {
4363 mInstrumentation.callApplicationOnCreate(app);
4364 } catch (Exception e) {
4365 if (!mInstrumentation.onException(app, e)) {
4366 throw new RuntimeException(
4367 "Unable to create application " + app.getClass().getName()
4368 + ": " + e.toString(), e);
4369 }
4370 }
4371 } finally {
4372 StrictMode.setThreadPolicy(savedPolicy);
4373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 }
4375
4376 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4377 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004378 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4379 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004380 Debug.stopMethodTracing();
4381 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004382 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383 // + ", app thr: " + mAppThread);
4384 try {
4385 am.finishInstrumentation(mAppThread, resultCode, results);
4386 } catch (RemoteException ex) {
4387 }
4388 }
4389
Romain Guy65b345f2011-07-27 18:51:50 -07004390 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004391 Context context, List<ProviderInfo> providers) {
4392 final ArrayList<IActivityManager.ContentProviderHolder> results =
4393 new ArrayList<IActivityManager.ContentProviderHolder>();
4394
Romain Guya998dff2012-03-23 18:58:36 -07004395 for (ProviderInfo cpi : providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004396 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07004397 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004398 buf.append(cpi.authority);
4399 buf.append(": ");
4400 buf.append(cpi.name);
4401 Log.i(TAG, buf.toString());
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004402 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
4403 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
4404 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004405 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407 }
4408 }
4409
4410 try {
4411 ActivityManagerNative.getDefault().publishContentProviders(
4412 getApplicationThread(), results);
4413 } catch (RemoteException ex) {
4414 }
4415 }
4416
Jeff Sharkey6d515712012-09-20 16:06:08 -07004417 public final IContentProvider acquireProvider(
4418 Context c, String auth, int userId, boolean stable) {
4419 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004420 if (provider != null) {
4421 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004422 }
4423
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004424 // There is a possible race here. Another thread may try to acquire
4425 // the same provider at the same time. When this happens, we want to ensure
4426 // that the first one wins.
4427 // Note that we cannot hold the lock while acquiring and installing the
4428 // provider since it might take a long time to run and it could also potentially
4429 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004430 IActivityManager.ContentProviderHolder holder = null;
4431 try {
4432 holder = ActivityManagerNative.getDefault().getContentProvider(
Jeff Sharkey6d515712012-09-20 16:06:08 -07004433 getApplicationThread(), auth, userId, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 } catch (RemoteException ex) {
4435 }
4436 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004437 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 return null;
4439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004440
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004441 // Install provider will increment the reference count for us, and break
4442 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004443 holder = installProvider(c, holder, holder.info,
4444 true /*noisy*/, holder.noReleaseNeeded, stable);
4445 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004446 }
4447
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004448 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
4449 if (stable) {
4450 prc.stableCount += 1;
4451 if (prc.stableCount == 1) {
4452 // We are acquiring a new stable reference on the provider.
4453 int unstableDelta;
4454 if (prc.removePending) {
4455 // We have a pending remove operation, which is holding the
4456 // last unstable reference. At this point we are converting
4457 // that unstable reference to our new stable reference.
4458 unstableDelta = -1;
4459 // Cancel the removal of the provider.
4460 if (DEBUG_PROVIDER) {
4461 Slog.v(TAG, "incProviderRef: stable "
4462 + "snatched provider from the jaws of death");
4463 }
4464 prc.removePending = false;
4465 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4466 } else {
4467 unstableDelta = 0;
4468 }
4469 try {
4470 if (DEBUG_PROVIDER) {
4471 Slog.v(TAG, "incProviderRef Now stable - "
4472 + prc.holder.info.name + ": unstableDelta="
4473 + unstableDelta);
4474 }
4475 ActivityManagerNative.getDefault().refContentProvider(
4476 prc.holder.connection, 1, unstableDelta);
4477 } catch (RemoteException e) {
4478 //do nothing content provider object is dead any way
4479 }
4480 }
4481 } else {
4482 prc.unstableCount += 1;
4483 if (prc.unstableCount == 1) {
4484 // We are acquiring a new unstable reference on the provider.
4485 if (prc.removePending) {
4486 // Oh look, we actually have a remove pending for the
4487 // provider, which is still holding the last unstable
4488 // reference. We just need to cancel that to take new
4489 // ownership of the reference.
4490 if (DEBUG_PROVIDER) {
4491 Slog.v(TAG, "incProviderRef: unstable "
4492 + "snatched provider from the jaws of death");
4493 }
4494 prc.removePending = false;
4495 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4496 } else {
4497 // First unstable ref, increment our count in the
4498 // activity manager.
4499 try {
4500 if (DEBUG_PROVIDER) {
4501 Slog.v(TAG, "incProviderRef: Now unstable - "
4502 + prc.holder.info.name);
4503 }
4504 ActivityManagerNative.getDefault().refContentProvider(
4505 prc.holder.connection, 0, 1);
4506 } catch (RemoteException e) {
4507 //do nothing content provider object is dead any way
4508 }
4509 }
4510 }
4511 }
4512 }
4513
Jeff Sharkey6d515712012-09-20 16:06:08 -07004514 public final IContentProvider acquireExistingProvider(
4515 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004516 synchronized (mProviderMap) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004517 final ProviderKey key = new ProviderKey(auth, userId);
4518 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004519 if (pr == null) {
4520 return null;
4521 }
4522
4523 IContentProvider provider = pr.mProvider;
4524 IBinder jBinder = provider.asBinder();
4525
4526 // Only increment the ref count if we have one. If we don't then the
4527 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004528 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004529 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004530 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004531 }
4532 return provider;
4533 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004534 }
4535
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004536 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
4537 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 return false;
4539 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004542 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004543 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004544 if (prc == null) {
4545 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004546 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004547 }
4548
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004549 boolean lastRef = false;
4550 if (stable) {
4551 if (prc.stableCount == 0) {
4552 if (DEBUG_PROVIDER) Slog.v(TAG,
4553 "releaseProvider: stable ref count already 0, how?");
4554 return false;
4555 }
4556 prc.stableCount -= 1;
4557 if (prc.stableCount == 0) {
4558 // What we do at this point depends on whether there are
4559 // any unstable refs left: if there are, we just tell the
4560 // activity manager to decrement its stable count; if there
4561 // aren't, we need to enqueue this provider to be removed,
4562 // and convert to holding a single unstable ref while
4563 // doing so.
4564 lastRef = prc.unstableCount == 0;
4565 try {
4566 if (DEBUG_PROVIDER) {
4567 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
4568 + lastRef + " - " + prc.holder.info.name);
4569 }
4570 ActivityManagerNative.getDefault().refContentProvider(
4571 prc.holder.connection, -1, lastRef ? 1 : 0);
4572 } catch (RemoteException e) {
4573 //do nothing content provider object is dead any way
4574 }
4575 }
4576 } else {
4577 if (prc.unstableCount == 0) {
4578 if (DEBUG_PROVIDER) Slog.v(TAG,
4579 "releaseProvider: unstable ref count already 0, how?");
4580 return false;
4581 }
4582 prc.unstableCount -= 1;
4583 if (prc.unstableCount == 0) {
4584 // If this is the last reference, we need to enqueue
4585 // this provider to be removed instead of telling the
4586 // activity manager to remove it at this point.
4587 lastRef = prc.stableCount == 0;
4588 if (!lastRef) {
4589 try {
4590 if (DEBUG_PROVIDER) {
4591 Slog.v(TAG, "releaseProvider: No longer unstable - "
4592 + prc.holder.info.name);
4593 }
4594 ActivityManagerNative.getDefault().refContentProvider(
4595 prc.holder.connection, 0, -1);
4596 } catch (RemoteException e) {
4597 //do nothing content provider object is dead any way
4598 }
4599 }
4600 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004601 }
4602
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004603 if (lastRef) {
4604 if (!prc.removePending) {
4605 // Schedule the actual remove asynchronously, since we don't know the context
4606 // this will be called in.
4607 // TODO: it would be nice to post a delayed message, so
4608 // if we come back and need the same provider quickly
4609 // we will still have it available.
4610 if (DEBUG_PROVIDER) {
4611 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
4612 + prc.holder.info.name);
4613 }
4614 prc.removePending = true;
4615 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
4616 mH.sendMessage(msg);
4617 } else {
4618 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
4619 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004620 }
4621 return true;
4622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004623 }
4624
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004625 final void completeRemoveProvider(ProviderRefCount prc) {
4626 synchronized (mProviderMap) {
4627 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004628 // There was a race! Some other client managed to acquire
4629 // the provider before the removal was completed.
4630 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004631 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004632 + "provider still in use");
4633 return;
4634 }
4635
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004636 final IBinder jBinder = prc.holder.provider.asBinder();
4637 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
4638 if (existingPrc == prc) {
4639 mProviderRefCountMap.remove(jBinder);
4640 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004641
4642 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
4643 while (iter.hasNext()) {
4644 ProviderClientRecord pr = iter.next();
4645 IBinder myBinder = pr.mProvider.asBinder();
4646 if (myBinder == jBinder) {
4647 iter.remove();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004648 }
4649 }
4650 }
4651
4652 try {
4653 if (DEBUG_PROVIDER) {
4654 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4655 + "removeContentProvider(" + prc.holder.info.name + ")");
4656 }
4657 ActivityManagerNative.getDefault().removeContentProvider(
4658 prc.holder.connection, false);
4659 } catch (RemoteException e) {
4660 //do nothing content provider object is dead any way
4661 }
4662 }
4663
4664 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
4665 synchronized(mProviderMap) {
4666 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4667 if (prc != null) {
4668 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
4669 + provider + " " + prc.holder.info.name);
4670 mProviderRefCountMap.remove(provider);
4671 if (prc.client != null && prc.client.mNames != null) {
4672 for (String name : prc.client.mNames) {
4673 ProviderClientRecord pr = mProviderMap.get(name);
4674 if (pr != null && pr.mProvider.asBinder() == provider) {
4675 Slog.i(TAG, "Removing dead content provider: " + name);
4676 mProviderMap.remove(name);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004677 }
4678 }
4679 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004680 if (fromClient) {
4681 // We found out about this due to execution in our client
4682 // code. Tell the activity manager about it now, to ensure
4683 // that the next time we go to do anything with the provider
4684 // it knows it is dead (so we don't race with its death
4685 // notification).
4686 try {
4687 ActivityManagerNative.getDefault().unstableProviderDied(
4688 prc.holder.connection);
4689 } catch (RemoteException e) {
4690 //do nothing content provider object is dead any way
4691 }
4692 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004693 }
4694 }
4695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004696
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004697 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Jeff Sharkey6d515712012-09-20 16:06:08 -07004698 ContentProvider localProvider, IActivityManager.ContentProviderHolder holder) {
4699 final String auths[] = PATTERN_SEMICOLON.split(holder.info.authority);
4700 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
4701
4702 final ProviderClientRecord pcr = new ProviderClientRecord(
4703 auths, provider, localProvider, holder);
4704 for (String auth : auths) {
4705 final ProviderKey key = new ProviderKey(auth, userId);
4706 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004707 if (existing != null) {
4708 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07004709 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004710 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004711 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004712 }
4713 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004714 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004715 }
4716
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004717 /**
4718 * Installs the provider.
4719 *
4720 * Providers that are local to the process or that come from the system server
4721 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4722 * Other remote providers are reference counted. The initial reference count
4723 * for all reference counted providers is one. Providers that are not reference
4724 * counted do not have a reference count (at all).
4725 *
4726 * This method detects when a provider has already been installed. When this happens,
4727 * it increments the reference count of the existing provider (if appropriate)
4728 * and returns the existing provider. This can happen due to concurrent
4729 * attempts to acquire the same provider.
4730 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004731 private IActivityManager.ContentProviderHolder installProvider(Context context,
4732 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
4733 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004734 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004735 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07004736 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004737 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004738 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004739 + info.name);
4740 }
4741 Context c = null;
4742 ApplicationInfo ai = info.applicationInfo;
4743 if (context.getPackageName().equals(ai.packageName)) {
4744 c = context;
4745 } else if (mInitialApplication != null &&
4746 mInitialApplication.getPackageName().equals(ai.packageName)) {
4747 c = mInitialApplication;
4748 } else {
4749 try {
4750 c = context.createPackageContext(ai.packageName,
4751 Context.CONTEXT_INCLUDE_CODE);
4752 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004753 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004754 }
4755 }
4756 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004757 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758 ai.packageName +
4759 " while loading content provider " +
4760 info.name);
4761 return null;
4762 }
4763 try {
4764 final java.lang.ClassLoader cl = c.getClassLoader();
4765 localProvider = (ContentProvider)cl.
4766 loadClass(info.name).newInstance();
4767 provider = localProvider.getIContentProvider();
4768 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004769 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004770 info.name + " from sourceDir " +
4771 info.applicationInfo.sourceDir);
4772 return null;
4773 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004774 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004775 TAG, "Instantiating local provider " + info.name);
4776 // XXX Need to create the correct context for this provider.
4777 localProvider.attachInfo(c, info);
4778 } catch (java.lang.Exception e) {
4779 if (!mInstrumentation.onException(null, e)) {
4780 throw new RuntimeException(
4781 "Unable to get provider " + info.name
4782 + ": " + e.toString(), e);
4783 }
4784 return null;
4785 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004786 } else {
4787 provider = holder.provider;
4788 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004789 + info.name);
4790 }
4791
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004792 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004793
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004794 synchronized (mProviderMap) {
4795 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
4796 + " / " + info.name);
4797 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004798 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004799 ComponentName cname = new ComponentName(info.packageName, info.name);
4800 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004801 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004802 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004803 Slog.v(TAG, "installProvider: lost the race, "
4804 + "using existing local provider");
4805 }
4806 provider = pr.mProvider;
4807 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004808 holder = new IActivityManager.ContentProviderHolder(info);
4809 holder.provider = provider;
4810 holder.noReleaseNeeded = true;
4811 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004812 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004813 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004814 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004815 retHolder = pr.mHolder;
4816 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004817 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4818 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004819 if (DEBUG_PROVIDER) {
4820 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004821 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004822 // We need to transfer our new reference to the existing
4823 // ref count, releasing the old one... but only if
4824 // release is needed (that is, it is not running in the
4825 // system process).
4826 if (!noReleaseNeeded) {
4827 incProviderRefLocked(prc, stable);
4828 try {
4829 ActivityManagerNative.getDefault().removeContentProvider(
4830 holder.connection, stable);
4831 } catch (RemoteException e) {
4832 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004833 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004834 }
4835 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004836 ProviderClientRecord client = installProviderAuthoritiesLocked(
4837 provider, localProvider, holder);
4838 if (noReleaseNeeded) {
4839 prc = new ProviderRefCount(holder, client, 1000, 1000);
4840 } else {
4841 prc = stable
4842 ? new ProviderRefCount(holder, client, 1, 0)
4843 : new ProviderRefCount(holder, client, 0, 1);
4844 }
4845 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004846 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004847 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004848 }
4849 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004850
4851 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004852 }
4853
Romain Guy65b345f2011-07-27 18:51:50 -07004854 private void attach(boolean system) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004855 sThreadLocal.set(this);
4856 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004857 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004858 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004859 public void run() {
4860 ensureJitEnabled();
4861 }
4862 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004863 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
4864 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004865 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4866 IActivityManager mgr = ActivityManagerNative.getDefault();
4867 try {
4868 mgr.attachApplication(mAppThread);
4869 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004870 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004871 }
4872 } else {
4873 // Don't set application object here -- if the system crashes,
4874 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004875 android.ddm.DdmHandleAppName.setAppName("system_process",
4876 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004877 try {
4878 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004879 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004880 context.init(getSystemContext().mPackageInfo, null, this);
4881 Application app = Instrumentation.newApplication(Application.class, context);
4882 mAllApplications.add(app);
4883 mInitialApplication = app;
4884 app.onCreate();
4885 } catch (Exception e) {
4886 throw new RuntimeException(
4887 "Unable to instantiate Application():" + e.toString(), e);
4888 }
4889 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07004890
4891 // add dropbox logging to libcore
4892 DropBox.setReporter(new DropBoxReporter());
4893
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004894 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004895 public void onConfigurationChanged(Configuration newConfig) {
4896 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004897 // We need to apply this change to the resources
4898 // immediately, because upon returning the view
4899 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004900 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004901 // This actually changed the resources! Tell
4902 // everyone about it.
4903 if (mPendingConfiguration == null ||
4904 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4905 mPendingConfiguration = newConfig;
4906
4907 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4908 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004909 }
4910 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004911 }
4912 public void onLowMemory() {
4913 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004914 public void onTrimMemory(int level) {
4915 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004916 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004917 }
4918
Romain Guy5e9120d2012-01-30 12:17:22 -08004919 public static ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004920 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004921 ActivityThread thread = new ActivityThread();
4922 thread.attach(true);
4923 return thread;
4924 }
4925
Jeff Brown10e89712011-07-08 18:52:57 -07004926 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004927 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004928 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004929 }
4930 }
4931
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004932 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004933 synchronized (mPackages) {
4934 if (mCoreSettings != null) {
4935 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004936 } else {
4937 return defaultValue;
4938 }
4939 }
4940 }
4941
Geremy Condra69689a72012-09-11 16:57:17 -07004942 private static class EventLoggingReporter implements EventLogger.Reporter {
4943 @Override
4944 public void report (int code, Object... list) {
4945 EventLog.writeEvent(code, list);
4946 }
4947 }
4948
Geremy Condrab7faaf42012-09-19 18:07:42 -07004949 private class DropBoxReporter implements DropBox.Reporter {
4950
4951 private DropBoxManager dropBox;
4952
4953 public DropBoxReporter() {
4954 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
4955 }
4956
4957 @Override
4958 public void addData(String tag, byte[] data, int flags) {
4959 dropBox.addData(tag, data, flags);
4960 }
4961
4962 @Override
4963 public void addText(String tag, String data) {
4964 dropBox.addText(tag, data);
4965 }
4966 }
4967
Romain Guy65b345f2011-07-27 18:51:50 -07004968 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004969 SamplingProfilerIntegration.start();
4970
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004971 // CloseGuard defaults to true and can be quite spammy. We
4972 // disable it here, but selectively enable it later (via
4973 // StrictMode) on debug builds, but using DropBox, not logs.
4974 CloseGuard.setEnabled(false);
4975
Jeff Sharkeyb049e212012-09-07 23:16:01 -07004976 Environment.initForCurrentUser();
4977
Geremy Condra69689a72012-09-11 16:57:17 -07004978 // Set the reporter for event logging in libcore
4979 EventLogger.setReporter(new EventLoggingReporter());
4980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004981 Process.setArgV0("<pre-initialized>");
4982
4983 Looper.prepareMainLooper();
4984
4985 ActivityThread thread = new ActivityThread();
4986 thread.attach(false);
4987
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07004988 if (sMainThreadHandler == null) {
4989 sMainThreadHandler = thread.getHandler();
4990 }
4991
Romain Guy5e9120d2012-01-30 12:17:22 -08004992 AsyncTask.init();
4993
Dianne Hackborn287952c2010-09-22 22:34:31 -07004994 if (false) {
4995 Looper.myLooper().setMessageLogging(new
4996 LogPrinter(Log.DEBUG, "ActivityThread"));
4997 }
4998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004999 Looper.loop();
5000
Jeff Brown10e89712011-07-08 18:52:57 -07005001 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005002 }
5003}