blob: 9b82f2a38519fb78e95ffa5aa72b5cd4efb61f78 [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;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070055import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.Handler;
57import android.os.IBinder;
58import android.os.Looper;
59import android.os.Message;
60import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070061import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.os.Process;
63import android.os.RemoteException;
64import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070065import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.os.SystemClock;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070067import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070068import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.util.AndroidRuntimeException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.util.DisplayMetrics;
71import android.util.EventLog;
72import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070073import android.util.LogPrinter;
Jeff Brown6754ba22011-12-14 20:20:01 -080074import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080075import android.util.Slog;
Jeff Brownd32460c2012-07-20 16:15:36 -070076import android.view.CompatibilityInfoHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070078import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.view.View;
80import android.view.ViewDebug;
81import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070082import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.view.Window;
84import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -070085import android.view.WindowManagerGlobal;
Jason Samsa6f338c2012-02-24 16:22:16 -080086import android.renderscript.RenderScript;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88import com.android.internal.os.BinderInternal;
89import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070090import com.android.internal.os.SamplingProfilerIntegration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl;
93
94import java.io.File;
95import java.io.FileDescriptor;
96import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070097import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import java.io.PrintWriter;
99import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700100import java.net.InetAddress;
Kenny Roote29df162012-08-10 08:28:37 -0700101import java.security.Security;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import java.util.ArrayList;
103import java.util.HashMap;
104import java.util.Iterator;
105import java.util.List;
106import java.util.Locale;
107import java.util.Map;
108import java.util.TimeZone;
109import java.util.regex.Pattern;
110
Geremy Condra69689a72012-09-11 16:57:17 -0700111import libcore.io.EventLogger;
Jeff Sharkeye861b422012-03-01 20:59:22 -0800112import libcore.io.IoUtils;
113
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800114import dalvik.system.CloseGuard;
Bob Leee5408332009-09-04 18:31:17 -0700115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116final class SuperNotCalledException extends AndroidRuntimeException {
117 public SuperNotCalledException(String msg) {
118 super(msg);
119 }
120}
121
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700122final class RemoteServiceException extends AndroidRuntimeException {
123 public RemoteServiceException(String msg) {
124 super(msg);
125 }
126}
127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128/**
129 * This manages the execution of the main thread in an
130 * application process, scheduling and executing activities,
131 * broadcasts, and other operations on it as the activity
132 * manager requests.
133 *
134 * {@hide}
135 */
136public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700137 /** @hide */
138 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700139 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700140 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700141 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700142 /** @hide */
143 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700144 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700145 private static final boolean DEBUG_BACKUP = false;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700146 private static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800147 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700148 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700149 private static final boolean DEBUG_PROVIDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
151 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
152 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
153 private static final int LOG_ON_PAUSE_CALLED = 30021;
154 private static final int LOG_ON_RESUME_CALLED = 30022;
155
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700156 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700157
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700158 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700160 final ApplicationThread mAppThread = new ApplicationThread();
161 final Looper mLooper = Looper.myLooper();
162 final H mH = new H();
163 final HashMap<IBinder, ActivityClientRecord> mActivities
164 = new HashMap<IBinder, ActivityClientRecord>();
165 // List of new activities (via ActivityRecord.nextIdle) that should
166 // be reported when next we idle.
167 ActivityClientRecord mNewActivities = null;
168 // Number of activities that are currently visible on-screen.
169 int mNumVisibleActivities = 0;
170 final HashMap<IBinder, Service> mServices
171 = new HashMap<IBinder, Service>();
172 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700173 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700174 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700175 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700176 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700177 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700178 Configuration mResConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700179 CompatibilityInfo mResCompatibilityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700180 Application mInitialApplication;
181 final ArrayList<Application> mAllApplications
182 = new ArrayList<Application>();
183 // set of instantiated backup agents, keyed by package name
184 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Romain Guy65b345f2011-07-27 18:51:50 -0700185 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700186 Instrumentation mInstrumentation;
187 String mInstrumentationAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700188 String mInstrumentationAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700189 String mInstrumentationAppPackage = null;
190 String mInstrumentedAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700191 String mInstrumentedAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700192 boolean mSystemThread = false;
193 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700195 // These can be accessed by multiple threads; mPackages is the lock.
196 // XXX For now we keep around information about all packages we have
197 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800198 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700199 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800200 // which means this lock gets held while the activity and window managers
201 // holds their own lock. Thus you MUST NEVER call back into the activity manager
202 // or window manager or anything that depends on them while holding this lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700203 final HashMap<String, WeakReference<LoadedApk>> mPackages
204 = new HashMap<String, WeakReference<LoadedApk>>();
205 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
206 = new HashMap<String, WeakReference<LoadedApk>>();
Jeff Browna492c3a2012-08-23 19:48:44 -0700207 final HashMap<CompatibilityInfo, DisplayMetrics> mDefaultDisplayMetrics
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700208 = new HashMap<CompatibilityInfo, DisplayMetrics>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700209 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
210 = new HashMap<ResourcesKey, WeakReference<Resources> >();
211 final ArrayList<ActivityClientRecord> mRelaunchingActivities
212 = new ArrayList<ActivityClientRecord>();
213 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700215 // The lock of mProviderMap protects the following variables.
216 final HashMap<String, ProviderClientRecord> mProviderMap
217 = new HashMap<String, ProviderClientRecord>();
218 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
219 = new HashMap<IBinder, ProviderRefCount>();
220 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
221 = new HashMap<IBinder, ProviderClientRecord>();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700222 final HashMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
223 = new HashMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224
Jeff Hamilton52d32032011-01-08 15:31:26 -0600225 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
226 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
227
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700228 final GcIdler mGcIdler = new GcIdler();
229 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700231 static Handler sMainThreadHandler; // set once in main()
232
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800233 Bundle mCoreSettings = null;
234
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400235 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700237 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 Intent intent;
239 Bundle state;
240 Activity activity;
241 Window window;
242 Activity parent;
243 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700244 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 boolean paused;
246 boolean stopped;
247 boolean hideForNow;
248 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700249 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700250 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700252 String profileFile;
253 ParcelFileDescriptor profileFd;
254 boolean autoStopProfiler;
255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400257 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700258 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259
260 List<ResultInfo> pendingResults;
261 List<Intent> pendingIntents;
262
263 boolean startsNotResumed;
264 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800265 int pendingConfigChanges;
266 boolean onlyLocalRequest;
267
268 View mPendingRemoveWindow;
269 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700271 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 parent = null;
273 embeddedID = null;
274 paused = false;
275 stopped = false;
276 hideForNow = false;
277 nextIdle = null;
278 }
279
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800280 public boolean isPreHoneycomb() {
281 if (activity != null) {
282 return activity.getApplicationInfo().targetSdkVersion
283 < android.os.Build.VERSION_CODES.HONEYCOMB;
284 }
285 return false;
286 }
287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700289 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 return "ActivityRecord{"
291 + Integer.toHexString(System.identityHashCode(this))
292 + " token=" + token + " " + (componentName == null
293 ? "no component name" : componentName.toShortString())
294 + "}";
295 }
296 }
297
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700298 final class ProviderClientRecord {
299 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 final IContentProvider mProvider;
301 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700302 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700304 ProviderClientRecord(String[] names, IContentProvider provider,
305 ContentProvider localProvider,
306 IActivityManager.ContentProviderHolder holder) {
307 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 mProvider = provider;
309 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700310 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 }
312 }
313
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400314 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 List<Intent> intents;
316 IBinder token;
317 public String toString() {
318 return "NewIntentData{intents=" + intents + " token=" + token + "}";
319 }
320 }
321
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400322 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700323 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700324 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
325 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
326 token, sendingUser);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700327 this.intent = intent;
328 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 Intent intent;
331 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400332 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 public String toString() {
334 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700335 info.packageName + " resultCode=" + getResultCode()
336 + " resultData=" + getResultData() + " resultExtras="
337 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 }
339 }
340
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400341 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700342 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400343 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700344 int backupMode;
345 public String toString() {
346 return "CreateBackupAgentData{appInfo=" + appInfo
347 + " backupAgent=" + appInfo.backupAgentName
348 + " mode=" + backupMode + "}";
349 }
350 }
Bob Leee5408332009-09-04 18:31:17 -0700351
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400352 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 IBinder token;
354 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400355 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 Intent intent;
357 public String toString() {
358 return "CreateServiceData{token=" + token + " className="
359 + info.name + " packageName=" + info.packageName
360 + " intent=" + intent + "}";
361 }
362 }
363
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400364 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 IBinder token;
366 Intent intent;
367 boolean rebind;
368 public String toString() {
369 return "BindServiceData{token=" + token + " intent=" + intent + "}";
370 }
371 }
372
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400373 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700375 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700377 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 Intent args;
379 public String toString() {
380 return "ServiceArgsData{token=" + token + " startId=" + startId
381 + " args=" + args + "}";
382 }
383 }
384
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400385 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700386 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 String processName;
388 ApplicationInfo appInfo;
389 List<ProviderInfo> providers;
390 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 Bundle instrumentationArgs;
392 IInstrumentationWatcher instrumentationWatcher;
393 int debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800394 boolean enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700395 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700396 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400398 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700399
400 /** Initial values for {@link Profiler}. */
401 String initProfileFile;
402 ParcelFileDescriptor initProfileFd;
403 boolean initAutoStopProfiler;
404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 public String toString() {
406 return "AppBindData{appInfo=" + appInfo + "}";
407 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700408 }
409
410 static final class Profiler {
411 String profileFile;
412 ParcelFileDescriptor profileFd;
413 boolean autoStopProfiler;
414 boolean profiling;
415 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700416 public void setProfiler(String file, ParcelFileDescriptor fd) {
417 if (profiling) {
418 if (fd != null) {
419 try {
420 fd.close();
421 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700422 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700423 }
424 }
425 return;
426 }
427 if (profileFd != null) {
428 try {
429 profileFd.close();
430 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700431 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700432 }
433 }
434 profileFile = file;
435 profileFd = fd;
436 }
437 public void startProfiling() {
438 if (profileFd == null || profiling) {
439 return;
440 }
441 try {
442 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
443 8 * 1024 * 1024, 0);
444 profiling = true;
445 } catch (RuntimeException e) {
446 Slog.w(TAG, "Profiling failed on path " + profileFile);
447 try {
448 profileFd.close();
449 profileFd = null;
450 } catch (IOException e2) {
451 Slog.w(TAG, "Failure closing profile fd", e2);
452 }
453 }
454 }
455 public void stopProfiling() {
456 if (profiling) {
457 profiling = false;
458 Debug.stopMethodTracing();
459 if (profileFd != null) {
460 try {
461 profileFd.close();
462 } catch (IOException e) {
463 }
464 }
465 profileFd = null;
466 profileFile = null;
467 }
468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 }
470
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400471 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700472 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700473 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800474 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 }
477
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400478 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 IBinder token;
480 List<ResultInfo> results;
481 public String toString() {
482 return "ResultData{token=" + token + " results" + results + "}";
483 }
484 }
485
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400486 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800487 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 String what;
489 String who;
490 }
491
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400492 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700493 String path;
494 ParcelFileDescriptor fd;
495 }
496
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400497 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700498 String path;
499 ParcelFileDescriptor fd;
500 }
501
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400502 static final class UpdateCompatibilityData {
503 String pkg;
504 CompatibilityInfo info;
505 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700506
Romain Guy65b345f2011-07-27 18:51:50 -0700507 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700508
Romain Guy65b345f2011-07-27 18:51:50 -0700509 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700510 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
511 private static final String ONE_COUNT_COLUMN = "%21s %8d";
512 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700513 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 // Formatting for checkin service - update version if row format changes
516 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700517
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700518 private void updatePendingConfiguration(Configuration config) {
519 synchronized (mPackages) {
520 if (mPendingConfiguration == null ||
521 mPendingConfiguration.isOtherSeqNewer(config)) {
522 mPendingConfiguration = config;
523 }
524 }
525 }
526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 public final void schedulePauseActivity(IBinder token, boolean finished,
528 boolean userLeaving, int configChanges) {
529 queueOrSendMessage(
530 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
531 token,
532 (userLeaving ? 1 : 0),
533 configChanges);
534 }
535
536 public final void scheduleStopActivity(IBinder token, boolean showWindow,
537 int configChanges) {
538 queueOrSendMessage(
539 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
540 token, 0, configChanges);
541 }
542
543 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
544 queueOrSendMessage(
545 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
546 token);
547 }
548
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800549 public final void scheduleSleeping(IBinder token, boolean sleeping) {
550 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
551 }
552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
554 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
555 }
556
557 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
558 ResultData res = new ResultData();
559 res.token = token;
560 res.results = results;
561 queueOrSendMessage(H.SEND_RESULT, res);
562 }
563
564 // we use token to identify this activity without having to send the
565 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700566 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700567 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
568 Bundle state, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700569 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
570 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700571 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572
573 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700574 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 r.intent = intent;
576 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400577 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 r.state = state;
579
580 r.pendingResults = pendingResults;
581 r.pendingIntents = pendingNewIntents;
582
583 r.startsNotResumed = notResumed;
584 r.isForward = isForward;
585
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700586 r.profileFile = profileName;
587 r.profileFd = profileFd;
588 r.autoStopProfiler = autoStopProfiler;
589
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700590 updatePendingConfiguration(curConfig);
591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
593 }
594
595 public final void scheduleRelaunchActivity(IBinder token,
596 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800597 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800598 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
599 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 }
601
602 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
603 NewIntentData data = new NewIntentData();
604 data.intents = intents;
605 data.token = token;
606
607 queueOrSendMessage(H.NEW_INTENT, data);
608 }
609
610 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
611 int configChanges) {
612 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
613 configChanges);
614 }
615
616 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400617 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700618 boolean sync, int sendingUser) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700619 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700620 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400622 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 queueOrSendMessage(H.RECEIVER, r);
624 }
625
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400626 public final void scheduleCreateBackupAgent(ApplicationInfo app,
627 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700628 CreateBackupAgentData d = new CreateBackupAgentData();
629 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400630 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700631 d.backupMode = backupMode;
632
633 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
634 }
635
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400636 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
637 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700638 CreateBackupAgentData d = new CreateBackupAgentData();
639 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400640 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700641
642 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
643 }
644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400646 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 CreateServiceData s = new CreateServiceData();
648 s.token = token;
649 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400650 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651
652 queueOrSendMessage(H.CREATE_SERVICE, s);
653 }
654
655 public final void scheduleBindService(IBinder token, Intent intent,
656 boolean rebind) {
657 BindServiceData s = new BindServiceData();
658 s.token = token;
659 s.intent = intent;
660 s.rebind = rebind;
661
Amith Yamasani742a6712011-05-04 14:49:28 -0700662 if (DEBUG_SERVICE)
663 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
664 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 queueOrSendMessage(H.BIND_SERVICE, s);
666 }
667
668 public final void scheduleUnbindService(IBinder token, Intent intent) {
669 BindServiceData s = new BindServiceData();
670 s.token = token;
671 s.intent = intent;
672
673 queueOrSendMessage(H.UNBIND_SERVICE, s);
674 }
675
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700676 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700677 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 ServiceArgsData s = new ServiceArgsData();
679 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700680 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700682 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 s.args = args;
684
685 queueOrSendMessage(H.SERVICE_ARGS, s);
686 }
687
688 public final void scheduleStopService(IBinder token) {
689 queueOrSendMessage(H.STOP_SERVICE, token);
690 }
691
692 public final void bindApplication(String processName,
693 ApplicationInfo appInfo, List<ProviderInfo> providers,
694 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700695 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Siva Velusamy92a8b222012-03-09 16:24:04 -0800697 int debugMode, boolean enableOpenGlTrace, boolean isRestrictedBackupMode,
698 boolean persistent, Configuration config, CompatibilityInfo compatInfo,
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700699 Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700
701 if (services != null) {
702 // Setup the service cache in the ServiceManager
703 ServiceManager.initServiceCache(services);
704 }
705
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800706 setCoreSettings(coreSettings);
707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 AppBindData data = new AppBindData();
709 data.processName = processName;
710 data.appInfo = appInfo;
711 data.providers = providers;
712 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 data.instrumentationArgs = instrumentationArgs;
714 data.instrumentationWatcher = instrumentationWatcher;
715 data.debugMode = debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800716 data.enableOpenGlTrace = enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700717 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700718 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400720 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700721 data.initProfileFile = profileFile;
722 data.initProfileFd = profileFd;
723 data.initAutoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 queueOrSendMessage(H.BIND_APPLICATION, data);
725 }
726
727 public final void scheduleExit() {
728 queueOrSendMessage(H.EXIT_APPLICATION, null);
729 }
730
Christopher Tate5e1ab332009-09-01 20:32:49 -0700731 public final void scheduleSuicide() {
732 queueOrSendMessage(H.SUICIDE, null);
733 }
734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 public void requestThumbnail(IBinder token) {
736 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
737 }
738
739 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700740 updatePendingConfiguration(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
742 }
743
744 public void updateTimeZone() {
745 TimeZone.setDefault(null);
746 }
747
Robert Greenwalt03595d02010-11-02 14:08:23 -0700748 public void clearDnsCache() {
749 // a non-standard API to get this to libcore
750 InetAddress.clearDnsCache();
751 }
752
Robert Greenwalt434203a2010-10-11 16:00:27 -0700753 public void setHttpProxy(String host, String port, String exclList) {
754 Proxy.setHttpProxySystemProperty(host, port, exclList);
755 }
756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 public void processInBackground() {
758 mH.removeMessages(H.GC_WHEN_IDLE);
759 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
760 }
761
762 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700763 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700764 try {
765 data.fd = ParcelFileDescriptor.dup(fd);
766 data.token = servicetoken;
767 data.args = args;
768 queueOrSendMessage(H.DUMP_SERVICE, data);
769 } catch (IOException e) {
770 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 }
772 }
773
774 // This function exists to make sure all receiver dispatching is
775 // correctly ordered, since these are one-way calls and the binder driver
776 // applies transaction ordering per object for such calls.
777 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700778 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700779 boolean sticky, int sendingUser) throws RemoteException {
780 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
781 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 }
Bob Leee5408332009-09-04 18:31:17 -0700783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 public void scheduleLowMemory() {
785 queueOrSendMessage(H.LOW_MEMORY, null);
786 }
787
788 public void scheduleActivityConfigurationChanged(IBinder token) {
789 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
790 }
791
Romain Guy7eabe552011-07-21 14:56:34 -0700792 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
793 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700794 ProfilerControlData pcd = new ProfilerControlData();
795 pcd.path = path;
796 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700797 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800798 }
799
Andy McFadden824c5102010-07-09 16:26:57 -0700800 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
801 DumpHeapData dhd = new DumpHeapData();
802 dhd.path = path;
803 dhd.fd = fd;
804 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
805 }
806
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700807 public void setSchedulingGroup(int group) {
808 // Note: do this immediately, since going into the foreground
809 // should happen regardless of what pending work we have to do
810 // and the activity manager will wait for us to report back that
811 // we are done before sending us to the background.
812 try {
813 Process.setProcessGroup(Process.myPid(), group);
814 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800815 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700816 }
817 }
Bob Leee5408332009-09-04 18:31:17 -0700818
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700819 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
820 Debug.getMemoryInfo(outInfo);
821 }
Bob Leee5408332009-09-04 18:31:17 -0700822
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700823 public void dispatchPackageBroadcast(int cmd, String[] packages) {
824 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
825 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700826
827 public void scheduleCrash(String msg) {
828 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
829 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700830
Dianne Hackborn30d71892010-12-11 10:37:55 -0800831 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
832 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700833 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700834 try {
835 data.fd = ParcelFileDescriptor.dup(fd);
836 data.token = activitytoken;
837 data.prefix = prefix;
838 data.args = args;
839 queueOrSendMessage(H.DUMP_ACTIVITY, data);
840 } catch (IOException e) {
841 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700842 }
843 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700844
Marco Nelissen18cb2872011-11-15 11:19:53 -0800845 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
846 String[] args) {
847 DumpComponentInfo data = new DumpComponentInfo();
848 try {
849 data.fd = ParcelFileDescriptor.dup(fd);
850 data.token = providertoken;
851 data.args = args;
852 queueOrSendMessage(H.DUMP_PROVIDER, data);
853 } catch (IOException e) {
854 Slog.w(TAG, "dumpProvider failed", e);
855 }
856 }
857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700859 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
860 boolean all, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700861 FileOutputStream fout = new FileOutputStream(fd);
862 PrintWriter pw = new PrintWriter(fout);
863 try {
Romain Guya998dff2012-03-23 18:58:36 -0700864 return dumpMemInfo(pw, checkin, all);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700865 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700866 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700867 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700868 }
869
Romain Guya998dff2012-03-23 18:58:36 -0700870 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean all) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 long nativeMax = Debug.getNativeHeapSize() / 1024;
872 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
873 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
874
875 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
876 Debug.getMemoryInfo(memInfo);
877
Dianne Hackbornb437e092011-08-05 17:50:29 -0700878 if (!all) {
879 return memInfo;
880 }
881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 Runtime runtime = Runtime.getRuntime();
883
884 long dalvikMax = runtime.totalMemory() / 1024;
885 long dalvikFree = runtime.freeMemory() / 1024;
886 long dalvikAllocated = dalvikMax - dalvikFree;
887 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700888 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700889 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
890 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 int globalAssetCount = AssetManager.getGlobalAssetCount();
892 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
893 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
894 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
895 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700896 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800897 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700900 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 // NOTE: if you change anything significant below, also consider changing
902 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700903 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 // Header
907 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
908 pw.print(Process.myPid()); pw.print(',');
909 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 // Heap info - max
912 pw.print(nativeMax); pw.print(',');
913 pw.print(dalvikMax); pw.print(',');
914 pw.print("N/A,");
915 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 // Heap info - allocated
918 pw.print(nativeAllocated); pw.print(',');
919 pw.print(dalvikAllocated); pw.print(',');
920 pw.print("N/A,");
921 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 // Heap info - free
924 pw.print(nativeFree); pw.print(',');
925 pw.print(dalvikFree); pw.print(',');
926 pw.print("N/A,");
927 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 // Heap info - proportional set size
930 pw.print(memInfo.nativePss); pw.print(',');
931 pw.print(memInfo.dalvikPss); pw.print(',');
932 pw.print(memInfo.otherPss); pw.print(',');
933 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700936 pw.print(memInfo.nativeSharedDirty); pw.print(',');
937 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
938 pw.print(memInfo.otherSharedDirty); pw.print(',');
939 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
940 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700943 pw.print(memInfo.nativePrivateDirty); pw.print(',');
944 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
945 pw.print(memInfo.otherPrivateDirty); pw.print(',');
946 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
947 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 // Object counts
950 pw.print(viewInstanceCount); pw.print(',');
951 pw.print(viewRootInstanceCount); pw.print(',');
952 pw.print(appContextInstanceCount); pw.print(',');
953 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 pw.print(globalAssetCount); pw.print(',');
956 pw.print(globalAssetManagerCount); pw.print(',');
957 pw.print(binderLocalObjectCount); pw.print(',');
958 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 pw.print(binderDeathObjectCount); pw.print(',');
961 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 // SQL
Vasu Noric3849202010-03-09 10:47:25 -0800964 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -0800965 pw.print(stats.memoryUsed / 1024); pw.print(',');
966 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700967 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800968 for (int i = 0; i < stats.dbStats.size(); i++) {
969 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700970 pw.print(','); pw.print(dbStats.dbName);
971 pw.print(','); pw.print(dbStats.pageSize);
972 pw.print(','); pw.print(dbStats.dbSize);
973 pw.print(','); pw.print(dbStats.lookaside);
974 pw.print(','); pw.print(dbStats.cache);
975 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800976 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700977 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700978
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700979 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 }
Bob Leee5408332009-09-04 18:31:17 -0700981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700983 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
984 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
985 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
986 "------");
987 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
988 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
989 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
990 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700992 int otherPss = memInfo.otherPss;
993 int otherSharedDirty = memInfo.otherSharedDirty;
994 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700996 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Romain Guy65b345f2011-07-27 18:51:50 -0700997 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700998 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
999 memInfo.getOtherPrivateDirty(i), "", "", "");
1000 otherPss -= memInfo.getOtherPss(i);
1001 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
1002 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
1003 }
1004
1005 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
1006 otherPrivateDirty, "", "", "");
1007 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
1008 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
1009 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
1010 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011
1012 pw.println(" ");
1013 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001014 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 viewRootInstanceCount);
1016
1017 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1018 "Activities:", activityInstanceCount);
1019
1020 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1021 "AssetManagers:", globalAssetManagerCount);
1022
1023 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1024 "Proxy Binders:", binderProxyObjectCount);
1025 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
1026
1027 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 // SQLite mem info
1030 pw.println(" ");
1031 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001032 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1033 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1034 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001035 pw.println(" ");
1036 int N = stats.dbStats.size();
1037 if (N > 0) {
1038 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001039 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1040 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001041 for (int i = 0; i < N; i++) {
1042 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001043 printRow(pw, DB_INFO_FORMAT,
1044 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1045 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1046 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1047 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001048 }
1049 }
Bob Leee5408332009-09-04 18:31:17 -07001050
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001051 // Asset details.
1052 String assetAlloc = AssetManager.getAssetAllocations();
1053 if (assetAlloc != null) {
1054 pw.println(" ");
1055 pw.println(" Asset Allocations");
1056 pw.print(assetAlloc);
1057 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001058
1059 return memInfo;
1060 }
1061
1062 @Override
1063 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1064 dumpGraphicsInfo(fd);
Jeff Brown98365d72012-08-19 20:30:52 -07001065 WindowManagerGlobal.getInstance().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 }
1067
Jeff Brown6754ba22011-12-14 20:20:01 -08001068 @Override
1069 public void dumpDbInfo(FileDescriptor fd, String[] args) {
1070 PrintWriter pw = new PrintWriter(new FileOutputStream(fd));
1071 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1072 SQLiteDebug.dump(printer, args);
1073 pw.flush();
1074 }
1075
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001076 @Override
1077 public void unstableProviderDied(IBinder provider) {
1078 queueOrSendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
1079 }
1080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 private void printRow(PrintWriter pw, String format, Object...objs) {
1082 pw.println(String.format(format, objs));
1083 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001084
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001085 public void setCoreSettings(Bundle coreSettings) {
1086 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001087 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001088
1089 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1090 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1091 ucd.pkg = pkg;
1092 ucd.info = info;
1093 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1094 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001095
1096 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001097 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001098 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 }
1101
Romain Guy65b345f2011-07-27 18:51:50 -07001102 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 public static final int LAUNCH_ACTIVITY = 100;
1104 public static final int PAUSE_ACTIVITY = 101;
1105 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1106 public static final int STOP_ACTIVITY_SHOW = 103;
1107 public static final int STOP_ACTIVITY_HIDE = 104;
1108 public static final int SHOW_WINDOW = 105;
1109 public static final int HIDE_WINDOW = 106;
1110 public static final int RESUME_ACTIVITY = 107;
1111 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001112 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 public static final int BIND_APPLICATION = 110;
1114 public static final int EXIT_APPLICATION = 111;
1115 public static final int NEW_INTENT = 112;
1116 public static final int RECEIVER = 113;
1117 public static final int CREATE_SERVICE = 114;
1118 public static final int SERVICE_ARGS = 115;
1119 public static final int STOP_SERVICE = 116;
1120 public static final int REQUEST_THUMBNAIL = 117;
1121 public static final int CONFIGURATION_CHANGED = 118;
1122 public static final int CLEAN_UP_CONTEXT = 119;
1123 public static final int GC_WHEN_IDLE = 120;
1124 public static final int BIND_SERVICE = 121;
1125 public static final int UNBIND_SERVICE = 122;
1126 public static final int DUMP_SERVICE = 123;
1127 public static final int LOW_MEMORY = 124;
1128 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1129 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001130 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001131 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001132 public static final int DESTROY_BACKUP_AGENT = 129;
1133 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001134 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001135 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001136 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001137 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001138 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001139 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001140 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001141 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001142 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001143 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001144 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001145 public static final int UNSTABLE_PROVIDER_DIED = 142;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001147 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 switch (code) {
1149 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1150 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1151 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1152 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1153 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1154 case SHOW_WINDOW: return "SHOW_WINDOW";
1155 case HIDE_WINDOW: return "HIDE_WINDOW";
1156 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1157 case SEND_RESULT: return "SEND_RESULT";
1158 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1159 case BIND_APPLICATION: return "BIND_APPLICATION";
1160 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1161 case NEW_INTENT: return "NEW_INTENT";
1162 case RECEIVER: return "RECEIVER";
1163 case CREATE_SERVICE: return "CREATE_SERVICE";
1164 case SERVICE_ARGS: return "SERVICE_ARGS";
1165 case STOP_SERVICE: return "STOP_SERVICE";
1166 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1167 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1168 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1169 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1170 case BIND_SERVICE: return "BIND_SERVICE";
1171 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1172 case DUMP_SERVICE: return "DUMP_SERVICE";
1173 case LOW_MEMORY: return "LOW_MEMORY";
1174 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1175 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001176 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001177 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1178 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001179 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001180 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001181 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001182 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001183 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001184 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001185 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001186 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001187 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001188 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001189 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001190 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001191 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 }
1193 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001194 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 }
1196 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001197 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 switch (msg.what) {
1199 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001200 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001201 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202
1203 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001204 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001205 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001206 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 } break;
1208 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001209 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001210 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001211 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001212 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 } break;
1214 case PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001215 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001217 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001218 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 break;
1220 case PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001221 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001223 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 break;
1225 case STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001226 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001228 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 break;
1230 case STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001231 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001233 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 break;
1235 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001236 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001238 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 break;
1240 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001241 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001243 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 break;
1245 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001246 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 handleResumeActivity((IBinder)msg.obj, true,
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001248 msg.arg1 != 0, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001249 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 break;
1251 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001252 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001254 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 break;
1256 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001257 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1259 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001260 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 break;
1262 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001263 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 AppBindData data = (AppBindData)msg.obj;
1265 handleBindApplication(data);
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 EXIT_APPLICATION:
1269 if (mInitialApplication != null) {
1270 mInitialApplication.onTerminate();
1271 }
1272 Looper.myLooper().quit();
1273 break;
1274 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001275 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 handleNewIntent((NewIntentData)msg.obj);
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 RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001280 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001282 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001283 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 break;
1285 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001286 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 handleCreateService((CreateServiceData)msg.obj);
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_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001291 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001293 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 break;
1295 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001296 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001298 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 break;
1300 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001301 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001303 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 break;
1305 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001306 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001308 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001309 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 break;
1311 case REQUEST_THUMBNAIL:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001312 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "requestThumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 handleRequestThumbnail((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001314 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 break;
1316 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001317 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001318 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001319 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001320 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 break;
1322 case CLEAN_UP_CONTEXT:
1323 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1324 cci.context.performFinalCleanup(cci.who, cci.what);
1325 break;
1326 case GC_WHEN_IDLE:
1327 scheduleGcIdler();
1328 break;
1329 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001330 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 break;
1332 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001333 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001335 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 break;
1337 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001338 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001340 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001342 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001343 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001344 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001345 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001346 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001347 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001348 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001349 break;
1350 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001351 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001352 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001353 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001354 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001355 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001356 Process.killProcess(Process.myPid());
1357 break;
1358 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001359 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001360 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001361 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001362 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001363 case ENABLE_JIT:
1364 ensureJitEnabled();
1365 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001366 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001367 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001368 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001369 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001370 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001371 case SCHEDULE_CRASH:
1372 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001373 case DUMP_HEAP:
1374 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1375 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001376 case DUMP_ACTIVITY:
1377 handleDumpActivity((DumpComponentInfo)msg.obj);
1378 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001379 case DUMP_PROVIDER:
1380 handleDumpProvider((DumpComponentInfo)msg.obj);
1381 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001382 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001383 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001384 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001385 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001386 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001387 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001388 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001389 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001390 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001391 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001392 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1393 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001394 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001395 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001396 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001397 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001398 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001399 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001400 case UNSTABLE_PROVIDER_DIED:
1401 handleUnstableProviderDied((IBinder)msg.obj, false);
1402 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001404 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 }
Bob Leee5408332009-09-04 18:31:17 -07001406
Brian Carlstromed7e0072011-03-24 13:27:57 -07001407 private void maybeSnapshot() {
1408 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001409 // convert the *private* ActivityThread.PackageInfo to *public* known
1410 // android.content.pm.PackageInfo
1411 String packageName = mBoundApplication.info.mPackageName;
1412 android.content.pm.PackageInfo packageInfo = null;
1413 try {
1414 Context context = getSystemContext();
1415 if(context == null) {
1416 Log.e(TAG, "cannot get a valid context");
1417 return;
1418 }
1419 PackageManager pm = context.getPackageManager();
1420 if(pm == null) {
1421 Log.e(TAG, "cannot get a valid PackageManager");
1422 return;
1423 }
1424 packageInfo = pm.getPackageInfo(
1425 packageName, PackageManager.GET_ACTIVITIES);
1426 } catch (NameNotFoundException e) {
1427 Log.e(TAG, "cannot get package info for " + packageName, e);
1428 }
1429 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001430 }
1431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 }
1433
Romain Guy65b345f2011-07-27 18:51:50 -07001434 private class Idler implements MessageQueue.IdleHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001436 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001437 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001438 if (mBoundApplication != null && mProfiler.profileFd != null
1439 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001440 stopProfiling = true;
1441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 if (a != null) {
1443 mNewActivities = null;
1444 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001445 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001447 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 TAG, "Reporting idle of " + a +
1449 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001450 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 if (a.activity != null && !a.activity.mFinished) {
1452 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001453 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001454 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001456 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 }
1458 }
1459 prev = a;
1460 a = a.nextIdle;
1461 prev.nextIdle = null;
1462 } while (a != null);
1463 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001464 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001465 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001466 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001467 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 return false;
1469 }
1470 }
1471
1472 final class GcIdler implements MessageQueue.IdleHandler {
1473 public final boolean queueIdle() {
1474 doGcIfNeeded();
1475 return false;
1476 }
1477 }
1478
Romain Guy65b345f2011-07-27 18:51:50 -07001479 private static class ResourcesKey {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001480 final private String mResDir;
Jeff Browna492c3a2012-08-23 19:48:44 -07001481 final private int mDisplayId;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001482 final private Configuration mOverrideConfiguration;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001483 final private float mScale;
1484 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001485
Jeff Browna492c3a2012-08-23 19:48:44 -07001486 ResourcesKey(String resDir, int displayId, Configuration overrideConfiguration, float scale) {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001487 mResDir = resDir;
Jeff Browna492c3a2012-08-23 19:48:44 -07001488 mDisplayId = displayId;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001489 if (overrideConfiguration != null) {
1490 if (Configuration.EMPTY.equals(overrideConfiguration)) {
1491 overrideConfiguration = null;
1492 }
1493 }
1494 mOverrideConfiguration = overrideConfiguration;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001495 mScale = scale;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001496 int hash = 17;
1497 hash = 31 * hash + mResDir.hashCode();
Jeff Browna492c3a2012-08-23 19:48:44 -07001498 hash = 31 * hash + mDisplayId;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001499 hash = 31 * hash + (mOverrideConfiguration != null
1500 ? mOverrideConfiguration.hashCode() : 0);
1501 hash = 31 * hash + Float.floatToIntBits(mScale);
1502 mHash = hash;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001503 }
Bob Leee5408332009-09-04 18:31:17 -07001504
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001505 @Override
1506 public int hashCode() {
1507 return mHash;
1508 }
1509
1510 @Override
1511 public boolean equals(Object obj) {
1512 if (!(obj instanceof ResourcesKey)) {
1513 return false;
1514 }
1515 ResourcesKey peer = (ResourcesKey) obj;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001516 if (!mResDir.equals(peer.mResDir)) {
1517 return false;
1518 }
Jeff Browna492c3a2012-08-23 19:48:44 -07001519 if (mDisplayId != peer.mDisplayId) {
1520 return false;
1521 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07001522 if (mOverrideConfiguration != peer.mOverrideConfiguration) {
1523 if (mOverrideConfiguration == null || peer.mOverrideConfiguration == null) {
1524 return false;
1525 }
1526 if (!mOverrideConfiguration.equals(peer.mOverrideConfiguration)) {
1527 return false;
1528 }
1529 }
1530 if (mScale != peer.mScale) {
1531 return false;
1532 }
1533 return true;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001534 }
1535 }
1536
Romain Guy65b345f2011-07-27 18:51:50 -07001537 public static ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001538 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540
Romain Guy65b345f2011-07-27 18:51:50 -07001541 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001542 ActivityThread am = currentActivityThread();
1543 return (am != null && am.mBoundApplication != null)
1544 ? am.mBoundApplication.processName : null;
1545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546
Romain Guy65b345f2011-07-27 18:51:50 -07001547 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001548 ActivityThread am = currentActivityThread();
1549 return am != null ? am.mInitialApplication : null;
1550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001552 public static IPackageManager getPackageManager() {
1553 if (sPackageManager != null) {
1554 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1555 return sPackageManager;
1556 }
1557 IBinder b = ServiceManager.getService("package");
1558 //Slog.v("PackageManager", "default service binder = " + b);
1559 sPackageManager = IPackageManager.Stub.asInterface(b);
1560 //Slog.v("PackageManager", "default service = " + sPackageManager);
1561 return sPackageManager;
1562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563
Jeff Browna492c3a2012-08-23 19:48:44 -07001564 private void flushDisplayMetricsLocked() {
1565 mDefaultDisplayMetrics.clear();
1566 }
1567
1568 DisplayMetrics getDisplayMetricsLocked(int displayId, CompatibilityInfo ci) {
1569 boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
1570 DisplayMetrics dm = isDefaultDisplay ? mDefaultDisplayMetrics.get(ci) : null;
1571 if (dm != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001572 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001573 }
Jeff Browna492c3a2012-08-23 19:48:44 -07001574 dm = new DisplayMetrics();
Jeff Brown848c2dc2012-08-19 20:18:08 -07001575
Jeff Brownbd6e1502012-08-28 03:27:37 -07001576 DisplayManagerGlobal displayManager = DisplayManagerGlobal.getInstance();
Jeff Brown848c2dc2012-08-19 20:18:08 -07001577 if (displayManager == null) {
1578 // may be null early in system startup
Jeff Brown848c2dc2012-08-19 20:18:08 -07001579 dm.setToDefaults();
1580 return dm;
1581 }
1582
Jeff Browna492c3a2012-08-23 19:48:44 -07001583 if (isDefaultDisplay) {
1584 mDefaultDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001585 }
Jeff Brown848c2dc2012-08-19 20:18:08 -07001586
Jeff Brownd32460c2012-07-20 16:15:36 -07001587 CompatibilityInfoHolder cih = new CompatibilityInfoHolder();
1588 cih.set(ci);
Jeff Browna492c3a2012-08-23 19:48:44 -07001589 Display d = displayManager.getCompatibleDisplay(displayId, cih);
Jeff Browna3909a92012-09-09 00:57:09 -07001590 if (d != null) {
1591 d.getMetrics(dm);
1592 } else {
1593 // Display no longer exists
1594 // FIXME: This would not be a problem if we kept the Display object around
1595 // instead of using the raw display id everywhere. The Display object caches
1596 // its information even after the display has been removed.
1597 dm.setToDefaults();
1598 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001599 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1600 // + metrics.heightPixels + " den=" + metrics.density
1601 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001602 return dm;
1603 }
1604
Romain Guy65b345f2011-07-27 18:51:50 -07001605 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001606 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1607 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001608 if (config == null) {
1609 return null;
1610 }
1611 if (compat != null && !compat.supportsScreen()) {
1612 mMainThreadConfig.setTo(config);
1613 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001614 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001615 }
1616 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001619 /**
1620 * Creates the top level Resources for applications with the given compatibility info.
1621 *
1622 * @param resDir the resource directory.
1623 * @param compInfo the compability info. It will use the default compatibility info when it's
1624 * null.
1625 */
Jeff Browna492c3a2012-08-23 19:48:44 -07001626 Resources getTopLevelResources(String resDir,
1627 int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001628 CompatibilityInfo compInfo) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001629 ResourcesKey key = new ResourcesKey(resDir,
1630 displayId, overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001631 compInfo.applicationScale);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001632 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001634 // Resources is app scale dependent.
1635 if (false) {
1636 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1637 + compInfo.applicationScale);
1638 }
1639 WeakReference<Resources> wr = mActiveResources.get(key);
1640 r = wr != null ? wr.get() : null;
1641 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1642 if (r != null && r.getAssets().isUpToDate()) {
1643 if (false) {
1644 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1645 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1646 }
1647 return r;
1648 }
1649 }
1650
1651 //if (r != null) {
1652 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1653 // + r + " " + resDir);
1654 //}
1655
1656 AssetManager assets = new AssetManager();
1657 if (assets.addAssetPath(resDir) == 0) {
1658 return null;
1659 }
1660
1661 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Jeff Browna492c3a2012-08-23 19:48:44 -07001662 DisplayMetrics dm = getDisplayMetricsLocked(displayId, null);
Dianne Hackborn756220b2012-08-14 16:45:30 -07001663 Configuration config;
Jeff Browna492c3a2012-08-23 19:48:44 -07001664 boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
1665 if (!isDefaultDisplay || key.mOverrideConfiguration != null) {
Dianne Hackborn756220b2012-08-14 16:45:30 -07001666 config = new Configuration(getConfiguration());
Jeff Browna492c3a2012-08-23 19:48:44 -07001667 if (!isDefaultDisplay) {
1668 applyNonDefaultDisplayMetricsToConfigurationLocked(dm, config);
1669 }
1670 if (key.mOverrideConfiguration != null) {
1671 config.updateFrom(key.mOverrideConfiguration);
1672 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07001673 } else {
1674 config = getConfiguration();
1675 }
Jeff Browna492c3a2012-08-23 19:48:44 -07001676 r = new Resources(assets, dm, config, compInfo);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001677 if (false) {
1678 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1679 + r.getConfiguration() + " appScale="
1680 + r.getCompatibilityInfo().applicationScale);
1681 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07001682
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001683 synchronized (mPackages) {
1684 WeakReference<Resources> wr = mActiveResources.get(key);
1685 Resources existing = wr != null ? wr.get() : null;
1686 if (existing != null && existing.getAssets().isUpToDate()) {
1687 // Someone else already created the resources while we were
1688 // unlocked; go ahead and use theirs.
1689 r.getAssets().close();
1690 return existing;
1691 }
1692
1693 // XXX need to remove entries when weak references go away
1694 mActiveResources.put(key, new WeakReference<Resources>(r));
1695 return r;
1696 }
1697 }
1698
1699 /**
1700 * Creates the top level resources for the given package.
1701 */
Jeff Browna492c3a2012-08-23 19:48:44 -07001702 Resources getTopLevelResources(String resDir,
1703 int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001704 LoadedApk pkgInfo) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001705 return getTopLevelResources(resDir, displayId, overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001706 pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001707 }
1708
1709 final Handler getHandler() {
1710 return mH;
1711 }
1712
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001713 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1714 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001715 synchronized (mPackages) {
1716 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1718 ref = mPackages.get(packageName);
1719 } else {
1720 ref = mResourcePackages.get(packageName);
1721 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001722 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001723 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001724 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1725 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 if (packageInfo != null && (packageInfo.mResources == null
1727 || packageInfo.mResources.getAssets().isUpToDate())) {
1728 if (packageInfo.isSecurityViolation()
1729 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1730 throw new SecurityException(
1731 "Requesting code from " + packageName
1732 + " to be run in process "
1733 + mBoundApplication.processName
1734 + "/" + mBoundApplication.appInfo.uid);
1735 }
1736 return packageInfo;
1737 }
1738 }
1739
1740 ApplicationInfo ai = null;
1741 try {
1742 ai = getPackageManager().getApplicationInfo(packageName,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001743 PackageManager.GET_SHARED_LIBRARY_FILES, UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001745 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 }
1747
1748 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001749 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 }
1751
1752 return null;
1753 }
1754
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001755 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1756 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1758 boolean securityViolation = includeCode && ai.uid != 0
1759 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001760 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001761 : true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1763 |Context.CONTEXT_IGNORE_SECURITY))
1764 == Context.CONTEXT_INCLUDE_CODE) {
1765 if (securityViolation) {
1766 String msg = "Requesting code from " + ai.packageName
1767 + " (with uid " + ai.uid + ")";
1768 if (mBoundApplication != null) {
1769 msg = msg + " to be run in process "
1770 + mBoundApplication.processName + " (with uid "
1771 + mBoundApplication.appInfo.uid + ")";
1772 }
1773 throw new SecurityException(msg);
1774 }
1775 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001776 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 }
1778
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001779 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1780 CompatibilityInfo compatInfo) {
1781 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 }
1783
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001784 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1785 synchronized (mPackages) {
1786 WeakReference<LoadedApk> ref;
1787 if (includeCode) {
1788 ref = mPackages.get(packageName);
1789 } else {
1790 ref = mResourcePackages.get(packageName);
1791 }
1792 return ref != null ? ref.get() : null;
1793 }
1794 }
1795
Romain Guy65b345f2011-07-27 18:51:50 -07001796 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1798 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001799 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 if (includeCode) {
1801 ref = mPackages.get(aInfo.packageName);
1802 } else {
1803 ref = mResourcePackages.get(aInfo.packageName);
1804 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001805 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 if (packageInfo == null || (packageInfo.mResources != null
1807 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001808 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 : "Loading resource-only package ") + aInfo.packageName
1810 + " (in " + (mBoundApplication != null
1811 ? mBoundApplication.processName : null)
1812 + ")");
1813 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001814 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 securityViolation, includeCode &&
1816 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1817 if (includeCode) {
1818 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001819 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 } else {
1821 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001822 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 }
1824 }
1825 return packageInfo;
1826 }
1827 }
1828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 ActivityThread() {
1830 }
1831
1832 public ApplicationThread getApplicationThread()
1833 {
1834 return mAppThread;
1835 }
1836
1837 public Instrumentation getInstrumentation()
1838 {
1839 return mInstrumentation;
1840 }
1841
1842 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001843 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 }
1845
1846 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001847 return mProfiler != null && mProfiler.profileFile != null
1848 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 }
1850
1851 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001852 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 }
1854
1855 public Looper getLooper() {
1856 return mLooper;
1857 }
1858
1859 public Application getApplication() {
1860 return mInitialApplication;
1861 }
Bob Leee5408332009-09-04 18:31:17 -07001862
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001863 public String getProcessName() {
1864 return mBoundApplication.processName;
1865 }
Bob Leee5408332009-09-04 18:31:17 -07001866
Dianne Hackborn21556372010-02-04 16:34:40 -08001867 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 synchronized (this) {
1869 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001870 ContextImpl context =
1871 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001872 LoadedApk info = new LoadedApk(this, "android", context, null,
1873 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 context.init(info, null, this);
1875 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001876 getConfiguration(), getDisplayMetricsLocked(
Jeff Browna492c3a2012-08-23 19:48:44 -07001877 Display.DEFAULT_DISPLAY,
1878 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001880 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 // + ": " + context.getResources().getConfiguration());
1882 }
1883 }
1884 return mSystemContext;
1885 }
1886
Mike Cleron432b7132009-09-24 15:28:29 -07001887 public void installSystemApplicationInfo(ApplicationInfo info) {
1888 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001889 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001890 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001891 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001892
1893 // give ourselves a default profiler
1894 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001895 }
1896 }
1897
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001898 void ensureJitEnabled() {
1899 if (!mJitEnabled) {
1900 mJitEnabled = true;
1901 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1902 }
1903 }
1904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 void scheduleGcIdler() {
1906 if (!mGcIdlerScheduled) {
1907 mGcIdlerScheduled = true;
1908 Looper.myQueue().addIdleHandler(mGcIdler);
1909 }
1910 mH.removeMessages(H.GC_WHEN_IDLE);
1911 }
1912
1913 void unscheduleGcIdler() {
1914 if (mGcIdlerScheduled) {
1915 mGcIdlerScheduled = false;
1916 Looper.myQueue().removeIdleHandler(mGcIdler);
1917 }
1918 mH.removeMessages(H.GC_WHEN_IDLE);
1919 }
1920
1921 void doGcIfNeeded() {
1922 mGcIdlerScheduled = false;
1923 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001924 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 // + "m now=" + now);
1926 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001927 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 BinderInternal.forceGc("bg");
1929 }
1930 }
1931
Jeff Hamilton52d32032011-01-08 15:31:26 -06001932 public void registerOnActivityPausedListener(Activity activity,
1933 OnActivityPausedListener listener) {
1934 synchronized (mOnPauseListeners) {
1935 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1936 if (list == null) {
1937 list = new ArrayList<OnActivityPausedListener>();
1938 mOnPauseListeners.put(activity, list);
1939 }
1940 list.add(listener);
1941 }
1942 }
1943
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001944 public void unregisterOnActivityPausedListener(Activity activity,
1945 OnActivityPausedListener listener) {
1946 synchronized (mOnPauseListeners) {
1947 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1948 if (list != null) {
1949 list.remove(listener);
1950 }
1951 }
1952 }
1953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 public final ActivityInfo resolveActivityInfo(Intent intent) {
1955 ActivityInfo aInfo = intent.resolveActivityInfo(
1956 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1957 if (aInfo == null) {
1958 // Throw an exception.
1959 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07001960 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
1962 return aInfo;
1963 }
Bob Leee5408332009-09-04 18:31:17 -07001964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001967 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001968 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001970 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 r.intent = intent;
1972 r.state = state;
1973 r.parent = parent;
1974 r.embeddedID = id;
1975 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001976 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 if (localLOGV) {
1978 ComponentName compname = intent.getComponent();
1979 String name;
1980 if (compname != null) {
1981 name = compname.toShortString();
1982 } else {
1983 name = "(Intent " + intent + ").getComponent() returned null";
1984 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001985 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 + ", comp=" + name
1987 + ", token=" + token);
1988 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001989 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 }
1991
1992 public final Activity getActivity(IBinder token) {
1993 return mActivities.get(token).activity;
1994 }
1995
1996 public final void sendActivityResult(
1997 IBinder token, String id, int requestCode,
1998 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001999 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07002000 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2002 list.add(new ResultInfo(id, requestCode, resultCode, data));
2003 mAppThread.scheduleSendResult(token, list);
2004 }
2005
2006 // if the thread hasn't started yet, we don't have the handler, so just
2007 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07002008 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 queueOrSendMessage(what, obj, 0, 0);
2010 }
2011
Romain Guy65b345f2011-07-27 18:51:50 -07002012 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 queueOrSendMessage(what, obj, arg1, 0);
2014 }
2015
Romain Guy65b345f2011-07-27 18:51:50 -07002016 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002018 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2020 + ": " + arg1 + " / " + obj);
2021 Message msg = Message.obtain();
2022 msg.what = what;
2023 msg.obj = obj;
2024 msg.arg1 = arg1;
2025 msg.arg2 = arg2;
2026 mH.sendMessage(msg);
2027 }
2028 }
2029
Dianne Hackborn21556372010-02-04 16:34:40 -08002030 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 String what) {
2032 ContextCleanupInfo cci = new ContextCleanupInfo();
2033 cci.context = context;
2034 cci.who = who;
2035 cci.what = what;
2036 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
2037 }
2038
Romain Guy65b345f2011-07-27 18:51:50 -07002039 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2041
2042 ActivityInfo aInfo = r.activityInfo;
2043 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002044 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 Context.CONTEXT_INCLUDE_CODE);
2046 }
Bob Leee5408332009-09-04 18:31:17 -07002047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 ComponentName component = r.intent.getComponent();
2049 if (component == null) {
2050 component = r.intent.resolveActivity(
2051 mInitialApplication.getPackageManager());
2052 r.intent.setComponent(component);
2053 }
2054
2055 if (r.activityInfo.targetActivity != null) {
2056 component = new ComponentName(r.activityInfo.packageName,
2057 r.activityInfo.targetActivity);
2058 }
2059
2060 Activity activity = null;
2061 try {
2062 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2063 activity = mInstrumentation.newActivity(
2064 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002065 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 r.intent.setExtrasClassLoader(cl);
2067 if (r.state != null) {
2068 r.state.setClassLoader(cl);
2069 }
2070 } catch (Exception e) {
2071 if (!mInstrumentation.onException(activity, e)) {
2072 throw new RuntimeException(
2073 "Unable to instantiate activity " + component
2074 + ": " + e.toString(), e);
2075 }
2076 }
2077
2078 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002079 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002080
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002081 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2082 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 TAG, r + ": app=" + app
2084 + ", appName=" + app.getPackageName()
2085 + ", pkg=" + r.packageInfo.getPackageName()
2086 + ", comp=" + r.intent.getComponent().toShortString()
2087 + ", dir=" + r.packageInfo.getAppDir());
2088
2089 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08002090 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 appContext.init(r.packageInfo, r.token, this);
2092 appContext.setOuterContext(activity);
2093 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002094 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002095 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002096 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002097 activity.attach(appContext, this, getInstrumentation(), r.token,
2098 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002099 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07002100
Christopher Tateb70f3df2009-04-07 16:07:59 -07002101 if (customIntent != null) {
2102 activity.mIntent = customIntent;
2103 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002104 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 activity.mStartedActivity = false;
2106 int theme = r.activityInfo.getThemeResource();
2107 if (theme != 0) {
2108 activity.setTheme(theme);
2109 }
2110
2111 activity.mCalled = false;
2112 mInstrumentation.callActivityOnCreate(activity, r.state);
2113 if (!activity.mCalled) {
2114 throw new SuperNotCalledException(
2115 "Activity " + r.intent.getComponent().toShortString() +
2116 " did not call through to super.onCreate()");
2117 }
2118 r.activity = activity;
2119 r.stopped = true;
2120 if (!r.activity.mFinished) {
2121 activity.performStart();
2122 r.stopped = false;
2123 }
2124 if (!r.activity.mFinished) {
2125 if (r.state != null) {
2126 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2127 }
2128 }
2129 if (!r.activity.mFinished) {
2130 activity.mCalled = false;
2131 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2132 if (!activity.mCalled) {
2133 throw new SuperNotCalledException(
2134 "Activity " + r.intent.getComponent().toShortString() +
2135 " did not call through to super.onPostCreate()");
2136 }
2137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 }
2139 r.paused = true;
2140
2141 mActivities.put(r.token, r);
2142
2143 } catch (SuperNotCalledException e) {
2144 throw e;
2145
2146 } catch (Exception e) {
2147 if (!mInstrumentation.onException(activity, e)) {
2148 throw new RuntimeException(
2149 "Unable to start activity " + component
2150 + ": " + e.toString(), e);
2151 }
2152 }
2153
2154 return activity;
2155 }
2156
Romain Guy65b345f2011-07-27 18:51:50 -07002157 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 // If we are getting ready to gc after going to the background, well
2159 // we are back active so skip it.
2160 unscheduleGcIdler();
2161
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002162 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002163 mProfiler.setProfiler(r.profileFile, r.profileFd);
2164 mProfiler.startProfiling();
2165 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002166 }
2167
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002168 // Make sure we are running with the most recent config.
2169 handleConfigurationChanged(null, null);
2170
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002171 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07002173 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174
2175 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002176 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002177 Bundle oldState = r.state;
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002178 handleResumeActivity(r.token, false, r.isForward,
2179 !r.activity.mFinished && !r.startsNotResumed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180
2181 if (!r.activity.mFinished && r.startsNotResumed) {
2182 // The activity manager actually wants this one to start out
2183 // paused, because it needs to be visible but isn't in the
2184 // foreground. We accomplish this by going through the
2185 // normal startup (because activities expect to go through
2186 // onResume() the first time they run, before their window
2187 // is displayed), and then pausing it. However, in this case
2188 // we do -not- need to do the full pause cycle (of freezing
2189 // and such) because the activity manager assumes it can just
2190 // retain the current state it has.
2191 try {
2192 r.activity.mCalled = false;
2193 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002194 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002195 // we need to be created again. But we only do this
2196 // for pre-Honeycomb apps, which always save their state
2197 // when pausing, so we can not have them save their state
2198 // when restarting from a paused state. For HC and later,
2199 // we want to (and can) let the state be saved as the normal
2200 // part of stopping the activity.
2201 if (r.isPreHoneycomb()) {
2202 r.state = oldState;
2203 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 if (!r.activity.mCalled) {
2205 throw new SuperNotCalledException(
2206 "Activity " + r.intent.getComponent().toShortString() +
2207 " did not call through to super.onPause()");
2208 }
2209
2210 } catch (SuperNotCalledException e) {
2211 throw e;
2212
2213 } catch (Exception e) {
2214 if (!mInstrumentation.onException(r.activity, e)) {
2215 throw new RuntimeException(
2216 "Unable to pause activity "
2217 + r.intent.getComponent().toShortString()
2218 + ": " + e.toString(), e);
2219 }
2220 }
2221 r.paused = true;
2222 }
2223 } else {
2224 // If there was an error, for any reason, tell the activity
2225 // manager to stop us.
2226 try {
2227 ActivityManagerNative.getDefault()
2228 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2229 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002230 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 }
2232 }
2233 }
2234
Romain Guy65b345f2011-07-27 18:51:50 -07002235 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 List<Intent> intents) {
2237 final int N = intents.size();
2238 for (int i=0; i<N; i++) {
2239 Intent intent = intents.get(i);
2240 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002241 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2243 }
2244 }
2245
2246 public final void performNewIntents(IBinder token,
2247 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002248 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 if (r != null) {
2250 final boolean resumed = !r.paused;
2251 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002252 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 mInstrumentation.callActivityOnPause(r.activity);
2254 }
2255 deliverNewIntents(r, intents);
2256 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002257 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002258 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 }
2260 }
2261 }
Bob Leee5408332009-09-04 18:31:17 -07002262
Romain Guy65b345f2011-07-27 18:51:50 -07002263 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 performNewIntents(data.token, data.intents);
2265 }
2266
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002267 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2268
2269 /**
2270 * Return the Intent that's currently being handled by a
2271 * BroadcastReceiver on this thread, or null if none.
2272 * @hide
2273 */
2274 public static Intent getIntentBeingBroadcast() {
2275 return sCurrentBroadcastIntent.get();
2276 }
2277
Romain Guy65b345f2011-07-27 18:51:50 -07002278 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 // If we are getting ready to gc after going to the background, well
2280 // we are back active so skip it.
2281 unscheduleGcIdler();
2282
2283 String component = data.intent.getComponent().getClassName();
2284
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002285 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002286 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287
2288 IActivityManager mgr = ActivityManagerNative.getDefault();
2289
Romain Guy65b345f2011-07-27 18:51:50 -07002290 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 try {
2292 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2293 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002294 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2296 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002297 if (DEBUG_BROADCAST) Slog.i(TAG,
2298 "Finishing failed broadcast to " + data.intent.getComponent());
2299 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300 throw new RuntimeException(
2301 "Unable to instantiate receiver " + component
2302 + ": " + e.toString(), e);
2303 }
2304
2305 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002306 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002307
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002308 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 TAG, "Performing receive of " + data.intent
2310 + ": app=" + app
2311 + ", appName=" + app.getPackageName()
2312 + ", pkg=" + packageInfo.getPackageName()
2313 + ", comp=" + data.intent.getComponent().toShortString()
2314 + ", dir=" + packageInfo.getAppDir());
2315
Dianne Hackborn21556372010-02-04 16:34:40 -08002316 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002317 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002318 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 receiver.onReceive(context.getReceiverRestrictedContext(),
2320 data.intent);
2321 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002322 if (DEBUG_BROADCAST) Slog.i(TAG,
2323 "Finishing failed broadcast to " + data.intent.getComponent());
2324 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 if (!mInstrumentation.onException(receiver, e)) {
2326 throw new RuntimeException(
2327 "Unable to start receiver " + component
2328 + ": " + e.toString(), e);
2329 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002330 } finally {
2331 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 }
2333
Dianne Hackborne829fef2010-10-26 17:44:01 -07002334 if (receiver.getPendingResult() != null) {
2335 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 }
2337 }
2338
Christopher Tate181fafa2009-05-14 11:12:14 -07002339 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002340 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002341 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002342
2343 // no longer idle; we have backup work to do
2344 unscheduleGcIdler();
2345
2346 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002347 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002348 String packageName = packageInfo.mPackageName;
2349 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002350 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002351 + " already exists");
2352 return;
2353 }
Bob Leee5408332009-09-04 18:31:17 -07002354
Christopher Tate181fafa2009-05-14 11:12:14 -07002355 BackupAgent agent = null;
2356 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002357
Christopher Tate79ec80d2011-06-24 14:58:49 -07002358 // full backup operation but no app-supplied agent? use the default implementation
2359 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2360 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002361 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002362 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002363
Christopher Tate181fafa2009-05-14 11:12:14 -07002364 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002365 IBinder binder = null;
2366 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002367 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2368
Christopher Tated1475e02009-07-09 15:36:17 -07002369 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002370 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002371
2372 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002373 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002374 context.init(packageInfo, null, this);
2375 context.setOuterContext(agent);
2376 agent.attach(context);
2377
2378 agent.onCreate();
2379 binder = agent.onBind();
2380 mBackupAgents.put(packageName, agent);
2381 } catch (Exception e) {
2382 // If this is during restore, fail silently; otherwise go
2383 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002384 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002385 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2386 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002387 throw e;
2388 }
2389 // falling through with 'binder' still null
2390 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002391
2392 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002393 try {
2394 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2395 } catch (RemoteException e) {
2396 // nothing to do.
2397 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002398 } catch (Exception e) {
2399 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002400 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002401 }
2402 }
2403
2404 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002405 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002406 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002407
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002408 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002409 String packageName = packageInfo.mPackageName;
2410 BackupAgent agent = mBackupAgents.get(packageName);
2411 if (agent != null) {
2412 try {
2413 agent.onDestroy();
2414 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002415 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002416 e.printStackTrace();
2417 }
2418 mBackupAgents.remove(packageName);
2419 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002420 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002421 }
2422 }
2423
Romain Guy65b345f2011-07-27 18:51:50 -07002424 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 // If we are getting ready to gc after going to the background, well
2426 // we are back active so skip it.
2427 unscheduleGcIdler();
2428
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002429 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002430 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 Service service = null;
2432 try {
2433 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2434 service = (Service) cl.loadClass(data.info.name).newInstance();
2435 } catch (Exception e) {
2436 if (!mInstrumentation.onException(service, e)) {
2437 throw new RuntimeException(
2438 "Unable to instantiate service " + data.info.name
2439 + ": " + e.toString(), e);
2440 }
2441 }
2442
2443 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002444 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445
Dianne Hackborn21556372010-02-04 16:34:40 -08002446 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 context.init(packageInfo, null, this);
2448
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002449 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 context.setOuterContext(service);
2451 service.attach(context, this, data.info.name, data.token, app,
2452 ActivityManagerNative.getDefault());
2453 service.onCreate();
2454 mServices.put(data.token, service);
2455 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002456 ActivityManagerNative.getDefault().serviceDoneExecuting(
2457 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 } catch (RemoteException e) {
2459 // nothing to do.
2460 }
2461 } catch (Exception e) {
2462 if (!mInstrumentation.onException(service, e)) {
2463 throw new RuntimeException(
2464 "Unable to create service " + data.info.name
2465 + ": " + e.toString(), e);
2466 }
2467 }
2468 }
2469
Romain Guy65b345f2011-07-27 18:51:50 -07002470 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002472 if (DEBUG_SERVICE)
2473 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 if (s != null) {
2475 try {
2476 data.intent.setExtrasClassLoader(s.getClassLoader());
2477 try {
2478 if (!data.rebind) {
2479 IBinder binder = s.onBind(data.intent);
2480 ActivityManagerNative.getDefault().publishService(
2481 data.token, data.intent, binder);
2482 } else {
2483 s.onRebind(data.intent);
2484 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002485 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002487 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 } catch (RemoteException ex) {
2489 }
2490 } catch (Exception e) {
2491 if (!mInstrumentation.onException(s, e)) {
2492 throw new RuntimeException(
2493 "Unable to bind to service " + s
2494 + " with " + data.intent + ": " + e.toString(), e);
2495 }
2496 }
2497 }
2498 }
2499
Romain Guy65b345f2011-07-27 18:51:50 -07002500 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 Service s = mServices.get(data.token);
2502 if (s != null) {
2503 try {
2504 data.intent.setExtrasClassLoader(s.getClassLoader());
2505 boolean doRebind = s.onUnbind(data.intent);
2506 try {
2507 if (doRebind) {
2508 ActivityManagerNative.getDefault().unbindFinished(
2509 data.token, data.intent, doRebind);
2510 } else {
2511 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002512 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 }
2514 } catch (RemoteException ex) {
2515 }
2516 } catch (Exception e) {
2517 if (!mInstrumentation.onException(s, e)) {
2518 throw new RuntimeException(
2519 "Unable to unbind to service " + s
2520 + " with " + data.intent + ": " + e.toString(), e);
2521 }
2522 }
2523 }
2524 }
2525
Dianne Hackborn625ac272010-09-17 18:29:22 -07002526 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002527 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2528 try {
2529 Service s = mServices.get(info.token);
2530 if (s != null) {
2531 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2532 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2533 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002535 } finally {
2536 IoUtils.closeQuietly(info.fd);
2537 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 }
2539 }
2540
Dianne Hackborn625ac272010-09-17 18:29:22 -07002541 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002542 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2543 try {
2544 ActivityClientRecord r = mActivities.get(info.token);
2545 if (r != null && r.activity != null) {
2546 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2547 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2548 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002549 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002550 } finally {
2551 IoUtils.closeQuietly(info.fd);
2552 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002553 }
2554 }
2555
Marco Nelissen18cb2872011-11-15 11:19:53 -08002556 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002557 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2558 try {
2559 ProviderClientRecord r = mLocalProviders.get(info.token);
2560 if (r != null && r.mLocalProvider != null) {
2561 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2562 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2563 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002564 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002565 } finally {
2566 IoUtils.closeQuietly(info.fd);
2567 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002568 }
2569 }
2570
Romain Guy65b345f2011-07-27 18:51:50 -07002571 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 Service s = mServices.get(data.token);
2573 if (s != null) {
2574 try {
2575 if (data.args != null) {
2576 data.args.setExtrasClassLoader(s.getClassLoader());
2577 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002578 int res;
2579 if (!data.taskRemoved) {
2580 res = s.onStartCommand(data.args, data.flags, data.startId);
2581 } else {
2582 s.onTaskRemoved(data.args);
2583 res = Service.START_TASK_REMOVED_COMPLETE;
2584 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002585
2586 QueuedWork.waitToFinish();
2587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002589 ActivityManagerNative.getDefault().serviceDoneExecuting(
2590 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 } catch (RemoteException e) {
2592 // nothing to do.
2593 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002594 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 } catch (Exception e) {
2596 if (!mInstrumentation.onException(s, e)) {
2597 throw new RuntimeException(
2598 "Unable to start service " + s
2599 + " with " + data.args + ": " + e.toString(), e);
2600 }
2601 }
2602 }
2603 }
2604
Romain Guy65b345f2011-07-27 18:51:50 -07002605 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 Service s = mServices.remove(token);
2607 if (s != null) {
2608 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002609 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 s.onDestroy();
2611 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002612 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002614 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002616
2617 QueuedWork.waitToFinish();
2618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002620 ActivityManagerNative.getDefault().serviceDoneExecuting(
2621 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 } catch (RemoteException e) {
2623 // nothing to do.
2624 }
2625 } catch (Exception e) {
2626 if (!mInstrumentation.onException(s, e)) {
2627 throw new RuntimeException(
2628 "Unable to stop service " + s
2629 + ": " + e.toString(), e);
2630 }
2631 }
2632 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002633 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 }
2635
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002636 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002638 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002639 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 + " finished=" + r.activity.mFinished);
2641 if (r != null && !r.activity.mFinished) {
2642 if (clearHide) {
2643 r.hideForNow = false;
2644 r.activity.mStartedActivity = false;
2645 }
2646 try {
2647 if (r.pendingIntents != null) {
2648 deliverNewIntents(r, r.pendingIntents);
2649 r.pendingIntents = null;
2650 }
2651 if (r.pendingResults != null) {
2652 deliverResults(r, r.pendingResults);
2653 r.pendingResults = null;
2654 }
2655 r.activity.performResume();
2656
Bob Leee5408332009-09-04 18:31:17 -07002657 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 r.paused = false;
2661 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 r.state = null;
2663 } catch (Exception e) {
2664 if (!mInstrumentation.onException(r.activity, e)) {
2665 throw new RuntimeException(
2666 "Unable to resume activity "
2667 + r.intent.getComponent().toShortString()
2668 + ": " + e.toString(), e);
2669 }
2670 }
2671 }
2672 return r;
2673 }
2674
Romain Guya998dff2012-03-23 18:58:36 -07002675 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002676 if (r.mPendingRemoveWindow != null) {
2677 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2678 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2679 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07002680 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002681 r.activity.getClass().getName(), "Activity");
2682 }
2683 }
2684 r.mPendingRemoveWindow = null;
2685 r.mPendingRemoveWindowManager = null;
2686 }
2687
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002688 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward,
2689 boolean reallyResume) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 // If we are getting ready to gc after going to the background, well
2691 // we are back active so skip it.
2692 unscheduleGcIdler();
2693
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002694 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695
2696 if (r != null) {
2697 final Activity a = r.activity;
2698
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002699 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 TAG, "Resume " + r + " started activity: " +
2701 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2702 + ", finished: " + a.mFinished);
2703
2704 final int forwardBit = isForward ?
2705 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 // If the window hasn't yet been added to the window manager,
2708 // and this guy didn't finish itself or start another activity,
2709 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002710 boolean willBeVisible = !a.mStartedActivity;
2711 if (!willBeVisible) {
2712 try {
2713 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2714 a.getActivityToken());
2715 } catch (RemoteException e) {
2716 }
2717 }
2718 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 r.window = r.activity.getWindow();
2720 View decor = r.window.getDecorView();
2721 decor.setVisibility(View.INVISIBLE);
2722 ViewManager wm = a.getWindowManager();
2723 WindowManager.LayoutParams l = r.window.getAttributes();
2724 a.mDecor = decor;
2725 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2726 l.softInputMode |= forwardBit;
2727 if (a.mVisibleFromClient) {
2728 a.mWindowAdded = true;
2729 wm.addView(decor, l);
2730 }
2731
2732 // If the window has already been added, but during resume
2733 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002734 // window visible.
2735 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002736 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 TAG, "Launch " + r + " mStartedActivity set");
2738 r.hideForNow = true;
2739 }
2740
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002741 // Get rid of anything left hanging around.
2742 cleanUpPendingRemoveWindows(r);
2743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 // The window is now visible if it has been added, we are not
2745 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002746 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002747 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002749 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002750 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07002752 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 r.newConfig = null;
2754 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002755 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 + isForward);
2757 WindowManager.LayoutParams l = r.window.getAttributes();
2758 if ((l.softInputMode
2759 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2760 != forwardBit) {
2761 l.softInputMode = (l.softInputMode
2762 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2763 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002764 if (r.activity.mVisibleFromClient) {
2765 ViewManager wm = a.getWindowManager();
2766 View decor = r.window.getDecorView();
2767 wm.updateViewLayout(decor, l);
2768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 }
2770 r.activity.mVisibleFromServer = true;
2771 mNumVisibleActivities++;
2772 if (r.activity.mVisibleFromClient) {
2773 r.activity.makeVisible();
2774 }
2775 }
2776
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002777 if (!r.onlyLocalRequest) {
2778 r.nextIdle = mNewActivities;
2779 mNewActivities = r;
2780 if (localLOGV) Slog.v(
2781 TAG, "Scheduling idle handler for " + r);
2782 Looper.myQueue().addIdleHandler(new Idler());
2783 }
2784 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002786 // Tell the activity manager we have resumed.
2787 if (reallyResume) {
2788 try {
2789 ActivityManagerNative.getDefault().activityResumed(token);
2790 } catch (RemoteException ex) {
2791 }
2792 }
2793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 } else {
2795 // If an exception was thrown when trying to resume, then
2796 // just end this activity.
2797 try {
2798 ActivityManagerNative.getDefault()
2799 .finishActivity(token, Activity.RESULT_CANCELED, null);
2800 } catch (RemoteException ex) {
2801 }
2802 }
2803 }
2804
2805 private int mThumbnailWidth = -1;
2806 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002807 private Bitmap mAvailThumbnailBitmap = null;
2808 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809
Romain Guy65b345f2011-07-27 18:51:50 -07002810 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002811 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002813 if (thumbnail == null) {
2814 int w = mThumbnailWidth;
2815 int h;
2816 if (w < 0) {
2817 Resources res = r.activity.getResources();
2818 mThumbnailHeight = h =
2819 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002821 mThumbnailWidth = w =
2822 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2823 } else {
2824 h = mThumbnailHeight;
2825 }
2826
2827 // On platforms where we don't want thumbnails, set dims to (0,0)
2828 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07002829 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
2830 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002831 thumbnail.eraseColor(0);
2832 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 }
2834
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002835 if (thumbnail != null) {
2836 Canvas cv = mThumbnailCanvas;
2837 if (cv == null) {
2838 mThumbnailCanvas = cv = new Canvas();
2839 }
2840
2841 cv.setBitmap(thumbnail);
2842 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2843 mAvailThumbnailBitmap = thumbnail;
2844 thumbnail = null;
2845 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002846 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 } catch (Exception e) {
2850 if (!mInstrumentation.onException(r.activity, e)) {
2851 throw new RuntimeException(
2852 "Unable to create thumbnail of "
2853 + r.intent.getComponent().toShortString()
2854 + ": " + e.toString(), e);
2855 }
2856 thumbnail = null;
2857 }
2858
2859 return thumbnail;
2860 }
2861
Romain Guy65b345f2011-07-27 18:51:50 -07002862 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002864 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002866 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 if (userLeaving) {
2868 performUserLeavingActivity(r);
2869 }
Bob Leee5408332009-09-04 18:31:17 -07002870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002872 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002874 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002875 if (r.isPreHoneycomb()) {
2876 QueuedWork.waitToFinish();
2877 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 // Tell the activity manager we have paused.
2880 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002881 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 } catch (RemoteException ex) {
2883 }
2884 }
2885 }
2886
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002887 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 mInstrumentation.callActivityOnUserLeaving(r.activity);
2889 }
2890
2891 final Bundle performPauseActivity(IBinder token, boolean finished,
2892 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002893 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 return r != null ? performPauseActivity(r, finished, saveState) : null;
2895 }
2896
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002897 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 boolean saveState) {
2899 if (r.paused) {
2900 if (r.activity.mFinished) {
2901 // If we are finishing, we won't call onResume() in certain cases.
2902 // So here we likewise don't want to call onPause() if the activity
2903 // isn't resumed.
2904 return null;
2905 }
2906 RuntimeException e = new RuntimeException(
2907 "Performing pause of activity that is not resumed: "
2908 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002909 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 }
2911 Bundle state = null;
2912 if (finished) {
2913 r.activity.mFinished = true;
2914 }
2915 try {
2916 // Next have the activity save its current state and managed dialogs...
2917 if (!r.activity.mFinished && saveState) {
2918 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002919 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2921 r.state = state;
2922 }
2923 // Now we are idle.
2924 r.activity.mCalled = false;
2925 mInstrumentation.callActivityOnPause(r.activity);
2926 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2927 if (!r.activity.mCalled) {
2928 throw new SuperNotCalledException(
2929 "Activity " + r.intent.getComponent().toShortString() +
2930 " did not call through to super.onPause()");
2931 }
2932
2933 } catch (SuperNotCalledException e) {
2934 throw e;
2935
2936 } catch (Exception e) {
2937 if (!mInstrumentation.onException(r.activity, e)) {
2938 throw new RuntimeException(
2939 "Unable to pause activity "
2940 + r.intent.getComponent().toShortString()
2941 + ": " + e.toString(), e);
2942 }
2943 }
2944 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002945
2946 // Notify any outstanding on paused listeners
2947 ArrayList<OnActivityPausedListener> listeners;
2948 synchronized (mOnPauseListeners) {
2949 listeners = mOnPauseListeners.remove(r.activity);
2950 }
2951 int size = (listeners != null ? listeners.size() : 0);
2952 for (int i = 0; i < size; i++) {
2953 listeners.get(i).onPaused(r.activity);
2954 }
2955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 return state;
2957 }
2958
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002959 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002960 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002961 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 }
2963
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002964 private static class StopInfo implements Runnable {
2965 ActivityClientRecord activity;
2966 Bundle state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 Bitmap thumbnail;
2968 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002969
2970 @Override public void run() {
2971 // Tell activity manager we have been stopped.
2972 try {
2973 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
2974 ActivityManagerNative.getDefault().activityStopped(
2975 activity.token, state, thumbnail, description);
2976 } catch (RemoteException ex) {
2977 }
2978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 }
2980
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002981 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002982 public final IActivityManager.ContentProviderHolder holder;
2983 public final ProviderClientRecord client;
2984 public int stableCount;
2985 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002986
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002987 // When this is set, the stable and unstable ref counts are 0 and
2988 // we have a pending operation scheduled to remove the ref count
2989 // from the activity manager. On the activity manager we are still
2990 // holding an unstable ref, though it is not reflected in the counts
2991 // here.
2992 public boolean removePending;
2993
2994 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
2995 ProviderClientRecord inClient, int sCount, int uCount) {
2996 holder = inHolder;
2997 client = inClient;
2998 stableCount = sCount;
2999 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 }
3001 }
3002
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003003 /**
3004 * Core implementation of stopping an activity. Note this is a little
3005 * tricky because the server's meaning of stop is slightly different
3006 * than our client -- for the server, stop means to save state and give
3007 * it the result when it is done, but the window may still be visible.
3008 * For the client, we want to call onStop()/onStart() to indicate when
3009 * the activity's UI visibillity changes.
3010 */
Romain Guy65b345f2011-07-27 18:51:50 -07003011 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003012 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003013 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003014 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 if (r != null) {
3016 if (!keepShown && r.stopped) {
3017 if (r.activity.mFinished) {
3018 // If we are finishing, we won't call onResume() in certain
3019 // cases. So here we likewise don't want to call onStop()
3020 // if the activity isn't resumed.
3021 return;
3022 }
3023 RuntimeException e = new RuntimeException(
3024 "Performing stop of activity that is not resumed: "
3025 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003026 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 }
3028
3029 if (info != null) {
3030 try {
3031 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003032 // For now, don't create the thumbnail here; we are
3033 // doing that by doing a screen snapshot.
3034 info.thumbnail = null; //createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 info.description = r.activity.onCreateDescription();
3036 } catch (Exception e) {
3037 if (!mInstrumentation.onException(r.activity, e)) {
3038 throw new RuntimeException(
3039 "Unable to save state of activity "
3040 + r.intent.getComponent().toShortString()
3041 + ": " + e.toString(), e);
3042 }
3043 }
3044 }
3045
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003046 // Next have the activity save its current state and managed dialogs...
3047 if (!r.activity.mFinished && saveState) {
3048 if (r.state == null) {
3049 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003050 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003051 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
3052 r.state = state;
3053 } else {
3054 state = r.state;
3055 }
3056 }
3057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 if (!keepShown) {
3059 try {
3060 // Now we are idle.
3061 r.activity.performStop();
3062 } catch (Exception e) {
3063 if (!mInstrumentation.onException(r.activity, e)) {
3064 throw new RuntimeException(
3065 "Unable to stop activity "
3066 + r.intent.getComponent().toShortString()
3067 + ": " + e.toString(), e);
3068 }
3069 }
3070 r.stopped = true;
3071 }
3072
3073 r.paused = true;
3074 }
3075 }
3076
Romain Guy65b345f2011-07-27 18:51:50 -07003077 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 View v = r.activity.mDecor;
3079 if (v != null) {
3080 if (show) {
3081 if (!r.activity.mVisibleFromServer) {
3082 r.activity.mVisibleFromServer = true;
3083 mNumVisibleActivities++;
3084 if (r.activity.mVisibleFromClient) {
3085 r.activity.makeVisible();
3086 }
3087 }
3088 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003089 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003090 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003092 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 r.newConfig = null;
3094 }
3095 } else {
3096 if (r.activity.mVisibleFromServer) {
3097 r.activity.mVisibleFromServer = false;
3098 mNumVisibleActivities--;
3099 v.setVisibility(View.INVISIBLE);
3100 }
3101 }
3102 }
3103 }
3104
Romain Guy65b345f2011-07-27 18:51:50 -07003105 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003106 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 r.activity.mConfigChangeFlags |= configChanges;
3108
3109 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003110 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003112 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 TAG, "Finishing stop of " + r + ": show=" + show
3114 + " win=" + r.window);
3115
3116 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003117
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003118 // Make sure any pending writes are now committed.
3119 if (!r.isPreHoneycomb()) {
3120 QueuedWork.waitToFinish();
3121 }
3122
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003123 // Schedule the call to tell the activity manager we have
3124 // stopped. We don't do this immediately, because we want to
3125 // have a chance for any other pending work (in particular memory
3126 // trim requests) to complete before you tell the activity
3127 // manager to proceed and allow us to go fully into the background.
3128 info.activity = r;
3129 info.state = r.state;
3130 mH.post(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003131 }
3132
3133 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003134 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 if (r.stopped) {
3136 r.activity.performRestart();
3137 r.stopped = false;
3138 }
3139 }
3140
Romain Guy65b345f2011-07-27 18:51:50 -07003141 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003142 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003143
3144 if (r == null) {
3145 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3146 return;
3147 }
3148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003150 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 } else if (show && r.stopped) {
3152 // If we are getting ready to gc after going to the background, well
3153 // we are back active so skip it.
3154 unscheduleGcIdler();
3155
3156 r.activity.performRestart();
3157 r.stopped = false;
3158 }
3159 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003160 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 TAG, "Handle window " + r + " visibility: " + show);
3162 updateVisibility(r, show);
3163 }
3164 }
3165
Romain Guy65b345f2011-07-27 18:51:50 -07003166 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003167 ActivityClientRecord r = mActivities.get(token);
3168
3169 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003170 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003171 return;
3172 }
3173
3174 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003175 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003176 try {
3177 // Now we are idle.
3178 r.activity.performStop();
3179 } catch (Exception e) {
3180 if (!mInstrumentation.onException(r.activity, e)) {
3181 throw new RuntimeException(
3182 "Unable to stop activity "
3183 + r.intent.getComponent().toShortString()
3184 + ": " + e.toString(), e);
3185 }
3186 }
3187 r.stopped = true;
3188 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003189
3190 // Make sure any pending writes are now committed.
3191 if (!r.isPreHoneycomb()) {
3192 QueuedWork.waitToFinish();
3193 }
3194
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003195 // Tell activity manager we slept.
3196 try {
3197 ActivityManagerNative.getDefault().activitySlept(r.token);
3198 } catch (RemoteException ex) {
3199 }
3200 } else {
3201 if (r.stopped && r.activity.mVisibleFromServer) {
3202 r.activity.performRestart();
3203 r.stopped = false;
3204 }
3205 }
3206 }
3207
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003208 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003209 synchronized (mPackages) {
3210 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003211 }
3212 }
3213
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003214 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3215 LoadedApk apk = peekPackageInfo(data.pkg, false);
3216 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003217 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003218 }
3219 apk = peekPackageInfo(data.pkg, true);
3220 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003221 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003222 }
3223 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07003224 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003225 }
3226
Romain Guy65b345f2011-07-27 18:51:50 -07003227 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 final int N = results.size();
3229 for (int i=0; i<N; i++) {
3230 ResultInfo ri = results.get(i);
3231 try {
3232 if (ri.mData != null) {
3233 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
3234 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003235 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003236 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 r.activity.dispatchActivityResult(ri.mResultWho,
3238 ri.mRequestCode, ri.mResultCode, ri.mData);
3239 } catch (Exception e) {
3240 if (!mInstrumentation.onException(r.activity, e)) {
3241 throw new RuntimeException(
3242 "Failure delivering result " + ri + " to activity "
3243 + r.intent.getComponent().toShortString()
3244 + ": " + e.toString(), e);
3245 }
3246 }
3247 }
3248 }
3249
Romain Guy65b345f2011-07-27 18:51:50 -07003250 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003251 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003252 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 if (r != null) {
3254 final boolean resumed = !r.paused;
3255 if (!r.activity.mFinished && r.activity.mDecor != null
3256 && r.hideForNow && resumed) {
3257 // We had hidden the activity because it started another
3258 // one... we have gotten a result back and we are not
3259 // paused, so make sure our window is visible.
3260 updateVisibility(r, true);
3261 }
3262 if (resumed) {
3263 try {
3264 // Now we are idle.
3265 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003266 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 mInstrumentation.callActivityOnPause(r.activity);
3268 if (!r.activity.mCalled) {
3269 throw new SuperNotCalledException(
3270 "Activity " + r.intent.getComponent().toShortString()
3271 + " did not call through to super.onPause()");
3272 }
3273 } catch (SuperNotCalledException e) {
3274 throw e;
3275 } catch (Exception e) {
3276 if (!mInstrumentation.onException(r.activity, e)) {
3277 throw new RuntimeException(
3278 "Unable to pause activity "
3279 + r.intent.getComponent().toShortString()
3280 + ": " + e.toString(), e);
3281 }
3282 }
3283 }
3284 deliverResults(r, res.results);
3285 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003286 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003287 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 }
3289 }
3290 }
3291
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003292 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 return performDestroyActivity(token, finishing, 0, false);
3294 }
3295
Romain Guy65b345f2011-07-27 18:51:50 -07003296 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003298 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003299 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003300 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003302 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 r.activity.mConfigChangeFlags |= configChanges;
3304 if (finishing) {
3305 r.activity.mFinished = true;
3306 }
3307 if (!r.paused) {
3308 try {
3309 r.activity.mCalled = false;
3310 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07003311 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 r.activity.getComponentName().getClassName());
3313 if (!r.activity.mCalled) {
3314 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003315 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 + " did not call through to super.onPause()");
3317 }
3318 } catch (SuperNotCalledException e) {
3319 throw e;
3320 } catch (Exception e) {
3321 if (!mInstrumentation.onException(r.activity, e)) {
3322 throw new RuntimeException(
3323 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003324 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 + ": " + e.toString(), e);
3326 }
3327 }
3328 r.paused = true;
3329 }
3330 if (!r.stopped) {
3331 try {
3332 r.activity.performStop();
3333 } catch (SuperNotCalledException e) {
3334 throw e;
3335 } catch (Exception e) {
3336 if (!mInstrumentation.onException(r.activity, e)) {
3337 throw new RuntimeException(
3338 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003339 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 + ": " + e.toString(), e);
3341 }
3342 }
3343 r.stopped = true;
3344 }
3345 if (getNonConfigInstance) {
3346 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003347 r.lastNonConfigurationInstances
3348 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 } catch (Exception e) {
3350 if (!mInstrumentation.onException(r.activity, e)) {
3351 throw new RuntimeException(
3352 "Unable to retain activity "
3353 + r.intent.getComponent().toShortString()
3354 + ": " + e.toString(), e);
3355 }
3356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 }
3358 try {
3359 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003360 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 if (!r.activity.mCalled) {
3362 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003363 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 " did not call through to super.onDestroy()");
3365 }
3366 if (r.window != null) {
3367 r.window.closeAllPanels();
3368 }
3369 } catch (SuperNotCalledException e) {
3370 throw e;
3371 } catch (Exception e) {
3372 if (!mInstrumentation.onException(r.activity, e)) {
3373 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003374 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3375 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 }
3377 }
3378 }
3379 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003380 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 return r;
3382 }
3383
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003384 private static String safeToComponentShortString(Intent intent) {
3385 ComponentName component = intent.getComponent();
3386 return component == null ? "[Unknown]" : component.toShortString();
3387 }
3388
Romain Guy65b345f2011-07-27 18:51:50 -07003389 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003391 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 configChanges, getNonConfigInstance);
3393 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003394 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003395 WindowManager wm = r.activity.getWindowManager();
3396 View v = r.activity.mDecor;
3397 if (v != null) {
3398 if (r.activity.mVisibleFromServer) {
3399 mNumVisibleActivities--;
3400 }
3401 IBinder wtoken = v.getWindowToken();
3402 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003403 if (r.onlyLocalRequest) {
3404 // Hold off on removing this until the new activity's
3405 // window is being added.
3406 r.mPendingRemoveWindow = v;
3407 r.mPendingRemoveWindowManager = wm;
3408 } else {
3409 wm.removeViewImmediate(v);
3410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003412 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003413 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414 r.activity.getClass().getName(), "Activity");
3415 }
3416 r.activity.mDecor = null;
3417 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003418 if (r.mPendingRemoveWindow == null) {
3419 // If we are delaying the removal of the activity window, then
3420 // we can't clean up all windows here. Note that we can't do
3421 // so later either, which means any windows that aren't closed
3422 // by the app will leak. Well we try to warning them a lot
3423 // about leaking windows, because that is a bug, so if they are
3424 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07003425 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003426 r.activity.getClass().getName(), "Activity");
3427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428
3429 // Mocked out contexts won't be participating in the normal
3430 // process lifecycle, but if we're running with a proper
3431 // ApplicationContext we need to have it tear down things
3432 // cleanly.
3433 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003434 if (c instanceof ContextImpl) {
3435 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 r.activity.getClass().getName(), "Activity");
3437 }
3438 }
3439 if (finishing) {
3440 try {
3441 ActivityManagerNative.getDefault().activityDestroyed(token);
3442 } catch (RemoteException ex) {
3443 // If the system process has died, it's game over for everyone.
3444 }
3445 }
3446 }
3447
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003448 public final void requestRelaunchActivity(IBinder token,
3449 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3450 int configChanges, boolean notResumed, Configuration config,
3451 boolean fromServer) {
3452 ActivityClientRecord target = null;
3453
3454 synchronized (mPackages) {
3455 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3456 ActivityClientRecord r = mRelaunchingActivities.get(i);
3457 if (r.token == token) {
3458 target = r;
3459 if (pendingResults != null) {
3460 if (r.pendingResults != null) {
3461 r.pendingResults.addAll(pendingResults);
3462 } else {
3463 r.pendingResults = pendingResults;
3464 }
3465 }
3466 if (pendingNewIntents != null) {
3467 if (r.pendingIntents != null) {
3468 r.pendingIntents.addAll(pendingNewIntents);
3469 } else {
3470 r.pendingIntents = pendingNewIntents;
3471 }
3472 }
3473 break;
3474 }
3475 }
3476
3477 if (target == null) {
3478 target = new ActivityClientRecord();
3479 target.token = token;
3480 target.pendingResults = pendingResults;
3481 target.pendingIntents = pendingNewIntents;
3482 if (!fromServer) {
3483 ActivityClientRecord existing = mActivities.get(token);
3484 if (existing != null) {
3485 target.startsNotResumed = existing.paused;
3486 }
3487 target.onlyLocalRequest = true;
3488 }
3489 mRelaunchingActivities.add(target);
3490 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3491 }
3492
3493 if (fromServer) {
3494 target.startsNotResumed = notResumed;
3495 target.onlyLocalRequest = false;
3496 }
3497 if (config != null) {
3498 target.createdConfig = config;
3499 }
3500 target.pendingConfigChanges |= configChanges;
3501 }
3502 }
3503
Romain Guy65b345f2011-07-27 18:51:50 -07003504 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003505 // If we are getting ready to gc after going to the background, well
3506 // we are back active so skip it.
3507 unscheduleGcIdler();
3508
3509 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003510 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003512 // First: make sure we have the most recent configuration and most
3513 // recent version of the activity, or skip it if some previous call
3514 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003515 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003516 int N = mRelaunchingActivities.size();
3517 IBinder token = tmp.token;
3518 tmp = null;
3519 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003520 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 if (r.token == token) {
3522 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003523 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 mRelaunchingActivities.remove(i);
3525 i--;
3526 N--;
3527 }
3528 }
Bob Leee5408332009-09-04 18:31:17 -07003529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003530 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003531 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 return;
3533 }
Bob Leee5408332009-09-04 18:31:17 -07003534
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003535 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3536 + tmp.token + " with configChanges=0x"
3537 + Integer.toHexString(configChanges));
3538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 if (mPendingConfiguration != null) {
3540 changedConfig = mPendingConfiguration;
3541 mPendingConfiguration = null;
3542 }
3543 }
Bob Leee5408332009-09-04 18:31:17 -07003544
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003545 if (tmp.createdConfig != null) {
3546 // If the activity manager is passing us its current config,
3547 // assume that is really what we want regardless of what we
3548 // may have pending.
3549 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003550 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3551 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3552 if (changedConfig == null
3553 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3554 changedConfig = tmp.createdConfig;
3555 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003556 }
3557 }
3558
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003559 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003560 + tmp.token + ": changedConfig=" + changedConfig);
3561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 // If there was a pending configuration change, execute it first.
3563 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003564 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003565 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003566 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003567 }
Bob Leee5408332009-09-04 18:31:17 -07003568
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003569 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003570 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 if (r == null) {
3572 return;
3573 }
Bob Leee5408332009-09-04 18:31:17 -07003574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003576 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003577 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003578
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003579 r.activity.mChangingConfigurations = true;
3580
Dianne Hackborne2b04802010-12-09 09:24:55 -08003581 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003582 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003583 performPauseActivity(r.token, false, r.isPreHoneycomb());
3584 }
3585 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3586 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003587 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003588 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 }
Bob Leee5408332009-09-04 18:31:17 -07003590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003591 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 r.activity = null;
3594 r.window = null;
3595 r.hideForNow = false;
3596 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003597 // Merge any pending results and pending intents; don't just replace them
3598 if (tmp.pendingResults != null) {
3599 if (r.pendingResults == null) {
3600 r.pendingResults = tmp.pendingResults;
3601 } else {
3602 r.pendingResults.addAll(tmp.pendingResults);
3603 }
3604 }
3605 if (tmp.pendingIntents != null) {
3606 if (r.pendingIntents == null) {
3607 r.pendingIntents = tmp.pendingIntents;
3608 } else {
3609 r.pendingIntents.addAll(tmp.pendingIntents);
3610 }
3611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003613
Christopher Tateb70f3df2009-04-07 16:07:59 -07003614 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003615 }
3616
Romain Guy65b345f2011-07-27 18:51:50 -07003617 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003618 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 Bitmap thumbnail = createThumbnailBitmap(r);
3620 CharSequence description = null;
3621 try {
3622 description = r.activity.onCreateDescription();
3623 } catch (Exception e) {
3624 if (!mInstrumentation.onException(r.activity, e)) {
3625 throw new RuntimeException(
3626 "Unable to create description of activity "
3627 + r.intent.getComponent().toShortString()
3628 + ": " + e.toString(), e);
3629 }
3630 }
3631 //System.out.println("Reporting top thumbnail " + thumbnail);
3632 try {
3633 ActivityManagerNative.getDefault().reportThumbnail(
3634 token, thumbnail, description);
3635 } catch (RemoteException ex) {
3636 }
3637 }
3638
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003639 ArrayList<ComponentCallbacks2> collectComponentCallbacksLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003641 ArrayList<ComponentCallbacks2> callbacks
3642 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644 if (mActivities.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003645 for (ActivityClientRecord ar : mActivities.values()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003646 Activity a = ar.activity;
3647 if (a != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003648 Configuration thisConfig = applyConfigCompatMainThread(mCurDefaultDisplayDpi,
3649 newConfig, ar.packageInfo.mCompatibilityInfo.getIfNeeded());
Romain Guya998dff2012-03-23 18:58:36 -07003650 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003651 // If the activity is currently resumed, its configuration
3652 // needs to change right now.
3653 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003654 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003655 // Otherwise, we will tell it about the change
3656 // the next time it is resumed or shown. Note that
3657 // the activity manager may, before then, decide the
3658 // activity needs to be destroyed to handle its new
3659 // configuration.
Romain Guya998dff2012-03-23 18:58:36 -07003660 if (DEBUG_CONFIGURATION) {
3661 Slog.v(TAG, "Setting activity "
3662 + ar.activityInfo.name + " newConfig=" + thisConfig);
3663 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003664 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 }
3666 }
3667 }
3668 }
3669 if (mServices.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003670 for (Service service : mServices.values()) {
3671 callbacks.add(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 }
3673 }
3674 synchronized (mProviderMap) {
3675 if (mLocalProviders.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003676 for (ProviderClientRecord providerClientRecord : mLocalProviders.values()) {
3677 callbacks.add(providerClientRecord.mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 }
3679 }
3680 }
3681 final int N = mAllApplications.size();
3682 for (int i=0; i<N; i++) {
3683 callbacks.add(mAllApplications.get(i));
3684 }
Bob Leee5408332009-09-04 18:31:17 -07003685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 return callbacks;
3687 }
Bob Leee5408332009-09-04 18:31:17 -07003688
Romain Guya998dff2012-03-23 18:58:36 -07003689 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003691 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 // we check the runtime type and act accordingly.
3693 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3694 if (activity != null) {
3695 activity.mCalled = false;
3696 }
Bob Leee5408332009-09-04 18:31:17 -07003697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003698 boolean shouldChangeConfig = false;
3699 if ((activity == null) || (activity.mCurrentConfig == null)) {
3700 shouldChangeConfig = true;
3701 } else {
Bob Leee5408332009-09-04 18:31:17 -07003702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 // If the new config is the same as the config this Activity
3704 // is already running with then don't bother calling
3705 // onConfigurationChanged
3706 int diff = activity.mCurrentConfig.diff(config);
3707 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003708 // If this activity doesn't handle any of the config changes
3709 // then don't bother calling onConfigurationChanged as we're
3710 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003711 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712 shouldChangeConfig = true;
3713 }
3714 }
3715 }
Bob Leee5408332009-09-04 18:31:17 -07003716
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003717 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003718 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 if (shouldChangeConfig) {
3720 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 if (activity != null) {
3723 if (!activity.mCalled) {
3724 throw new SuperNotCalledException(
3725 "Activity " + activity.getLocalClassName() +
3726 " did not call through to super.onConfigurationChanged()");
3727 }
3728 activity.mConfigChangeFlags = 0;
3729 activity.mCurrentConfig = new Configuration(config);
3730 }
3731 }
3732 }
3733
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003734 public final void applyConfigurationToResources(Configuration config) {
3735 synchronized (mPackages) {
3736 applyConfigurationToResourcesLocked(config, null);
3737 }
3738 }
3739
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003740 final boolean applyConfigurationToResourcesLocked(Configuration config,
3741 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003742 if (mResConfiguration == null) {
3743 mResConfiguration = new Configuration();
3744 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003745 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003746 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003747 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003748 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003749 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003750 int changes = mResConfiguration.updateFrom(config);
Jeff Browna492c3a2012-08-23 19:48:44 -07003751 flushDisplayMetricsLocked();
3752 DisplayMetrics defaultDisplayMetrics = getDisplayMetricsLocked(
3753 Display.DEFAULT_DISPLAY, null);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003754
3755 if (compat != null && (mResCompatibilityInfo == null ||
3756 !mResCompatibilityInfo.equals(compat))) {
3757 mResCompatibilityInfo = compat;
3758 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3759 | ActivityInfo.CONFIG_SCREEN_SIZE
3760 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3761 }
Bob Leee5408332009-09-04 18:31:17 -07003762
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003763 // set it for java, this also affects newly created Resources
3764 if (config.locale != null) {
3765 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003766 }
Bob Leee5408332009-09-04 18:31:17 -07003767
Jeff Browna492c3a2012-08-23 19:48:44 -07003768 Resources.updateSystemConfiguration(config, defaultDisplayMetrics, compat);
Bob Leee5408332009-09-04 18:31:17 -07003769
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003770 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003771 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborn756220b2012-08-14 16:45:30 -07003772
3773 Configuration tmpConfig = null;
3774
3775 Iterator<Map.Entry<ResourcesKey, WeakReference<Resources>>> it =
3776 mActiveResources.entrySet().iterator();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003777 while (it.hasNext()) {
Dianne Hackborn756220b2012-08-14 16:45:30 -07003778 Map.Entry<ResourcesKey, WeakReference<Resources>> entry = it.next();
3779 Resources r = entry.getValue().get();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003780 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003781 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003782 + r + " config to: " + config);
Jeff Browna492c3a2012-08-23 19:48:44 -07003783 int displayId = entry.getKey().mDisplayId;
3784 boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
3785 DisplayMetrics dm = defaultDisplayMetrics;
3786 Configuration overrideConfig = entry.getKey().mOverrideConfiguration;
3787 if (!isDefaultDisplay || overrideConfig != null) {
Dianne Hackborn756220b2012-08-14 16:45:30 -07003788 if (tmpConfig == null) {
3789 tmpConfig = new Configuration();
3790 }
3791 tmpConfig.setTo(config);
Jeff Browna492c3a2012-08-23 19:48:44 -07003792 if (!isDefaultDisplay) {
3793 dm = getDisplayMetricsLocked(displayId, null);
3794 applyNonDefaultDisplayMetricsToConfigurationLocked(dm, tmpConfig);
3795 }
3796 if (overrideConfig != null) {
3797 tmpConfig.updateFrom(overrideConfig);
3798 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07003799 r.updateConfiguration(tmpConfig, dm, compat);
3800 } else {
3801 r.updateConfiguration(config, dm, compat);
3802 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003803 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003804 // + " " + r + ": " + r.getConfiguration());
3805 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003806 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003807 it.remove();
3808 }
3809 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003810
3811 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003812 }
Dianne Hackborn836e2622011-10-04 18:32:39 -07003813
Jeff Browna492c3a2012-08-23 19:48:44 -07003814 final void applyNonDefaultDisplayMetricsToConfigurationLocked(
3815 DisplayMetrics dm, Configuration config) {
3816 config.screenLayout = Configuration.SCREENLAYOUT_SIZE_XLARGE
3817 | Configuration.SCREENLAYOUT_LONG_NO;
3818 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
3819 config.orientation = (dm.widthPixels >= dm.heightPixels) ?
3820 Configuration.ORIENTATION_LANDSCAPE : Configuration.ORIENTATION_PORTRAIT;
3821 config.densityDpi = dm.densityDpi;
3822 config.screenWidthDp = (int)(dm.widthPixels / dm.density);
3823 config.screenHeightDp = (int)(dm.heightPixels / dm.density);
3824 config.smallestScreenWidthDp = config.screenWidthDp; // assume screen does not rotate
3825 config.compatScreenWidthDp = config.screenWidthDp;
3826 config.compatScreenHeightDp = config.screenHeightDp;
3827 config.compatSmallestScreenWidthDp = config.smallestScreenWidthDp;
3828 }
3829
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003830 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07003831 Configuration config = mConfiguration;
3832 if (mCompatConfiguration == null) {
3833 mCompatConfiguration = new Configuration();
3834 }
3835 mCompatConfiguration.setTo(mConfiguration);
3836 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003837 mResCompatibilityInfo.applyToConfiguration(displayDensity, mCompatConfiguration);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003838 config = mCompatConfiguration;
3839 }
3840 return config;
3841 }
3842
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003843 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003844
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003845 ArrayList<ComponentCallbacks2> callbacks = null;
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003846 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003847
3848 synchronized (mPackages) {
3849 if (mPendingConfiguration != null) {
3850 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3851 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003852 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003853 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003854 }
3855 mPendingConfiguration = null;
3856 }
3857
3858 if (config == null) {
3859 return;
3860 }
3861
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003862 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003863 + config);
3864
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003865 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003867 if (mConfiguration == null) {
3868 mConfiguration = new Configuration();
3869 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003870 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003871 return;
3872 }
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003873 configDiff = mConfiguration.diff(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003874 mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003875 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 callbacks = collectComponentCallbacksLocked(false, config);
3877 }
Romain Guy65b345f2011-07-27 18:51:50 -07003878
3879 // Cleanup hardware accelerated stuff
Jeff Brown98365d72012-08-19 20:30:52 -07003880 WindowManagerGlobal.getInstance().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003881
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003882 freeTextLayoutCachesIfNeeded(configDiff);
3883
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003884 if (callbacks != null) {
3885 final int N = callbacks.size();
3886 for (int i=0; i<N; i++) {
3887 performConfigurationChanged(callbacks.get(i), config);
3888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 }
3890 }
3891
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003892 final void freeTextLayoutCachesIfNeeded(int configDiff) {
3893 if (configDiff != 0) {
3894 // Ask text layout engine to free its caches if there is a locale change
3895 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
3896 if (hasLocaleConfigChange) {
3897 Canvas.freeTextLayoutCaches();
3898 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
3899 }
3900 }
3901 }
3902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003903 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003904 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 if (r == null || r.activity == null) {
3906 return;
3907 }
Bob Leee5408332009-09-04 18:31:17 -07003908
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003909 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003910 + r.activityInfo.name);
3911
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003912 performConfigurationChanged(r.activity, mCompatConfiguration);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003913
3914 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 }
3916
Romain Guy7eabe552011-07-21 14:56:34 -07003917 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003918 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003919 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003920 switch (profileType) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003921 default:
3922 mProfiler.setProfiler(pcd.path, pcd.fd);
3923 mProfiler.autoStopProfiler = false;
3924 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003925 break;
3926 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003927 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003928 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003929 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003930 } finally {
3931 try {
3932 pcd.fd.close();
3933 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003934 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003935 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003936 }
3937 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003938 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07003939 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003940 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003941 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003942 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003943 }
3944 }
Bob Leee5408332009-09-04 18:31:17 -07003945
Romain Guya998dff2012-03-23 18:58:36 -07003946 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07003947 if (managed) {
3948 try {
3949 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3950 } catch (IOException e) {
3951 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3952 + " -- can the process access this path?");
3953 } finally {
3954 try {
3955 dhd.fd.close();
3956 } catch (IOException e) {
3957 Slog.w(TAG, "Failure closing profile fd", e);
3958 }
3959 }
3960 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003961 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003962 }
3963 }
3964
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003965 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3966 boolean hasPkgInfo = false;
3967 if (packages != null) {
3968 for (int i=packages.length-1; i>=0; i--) {
3969 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3970 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003971 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003972 ref = mPackages.get(packages[i]);
3973 if (ref != null && ref.get() != null) {
3974 hasPkgInfo = true;
3975 } else {
3976 ref = mResourcePackages.get(packages[i]);
3977 if (ref != null && ref.get() != null) {
3978 hasPkgInfo = true;
3979 }
3980 }
3981 }
3982 mPackages.remove(packages[i]);
3983 mResourcePackages.remove(packages[i]);
3984 }
3985 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003986 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003987 hasPkgInfo);
3988 }
3989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003990 final void handleLowMemory() {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003991 ArrayList<ComponentCallbacks2> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003992
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003993 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003994 callbacks = collectComponentCallbacksLocked(true, null);
3995 }
Bob Leee5408332009-09-04 18:31:17 -07003996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003997 final int N = callbacks.size();
3998 for (int i=0; i<N; i++) {
3999 callbacks.get(i).onLowMemory();
4000 }
4001
Chris Tatece229052009-03-25 16:44:52 -07004002 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
4003 if (Process.myUid() != Process.SYSTEM_UID) {
4004 int sqliteReleased = SQLiteDatabase.releaseMemory();
4005 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
4006 }
Bob Leee5408332009-09-04 18:31:17 -07004007
Mike Reedcaf0df12009-04-27 14:32:05 -04004008 // Ask graphics to free up as much as possible (font/image caches)
4009 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004011 // Ask text layout engine to free also as much as possible
4012 Canvas.freeTextLayoutCaches();
4013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004014 BinderInternal.forceGc("mem");
4015 }
4016
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004017 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004018 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004019
Jeff Brown98365d72012-08-19 20:30:52 -07004020 final WindowManagerGlobal windowManager = WindowManagerGlobal.getInstance();
Romain Guy19f86e82012-04-23 15:19:07 -07004021 windowManager.startTrimMemory(level);
4022
4023 ArrayList<ComponentCallbacks2> callbacks;
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004024 synchronized (mPackages) {
4025 callbacks = collectComponentCallbacksLocked(true, null);
4026 }
4027
4028 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07004029 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004030 callbacks.get(i).onTrimMemory(level);
4031 }
Romain Guy19f86e82012-04-23 15:19:07 -07004032
Jeff Brown98365d72012-08-19 20:30:52 -07004033 windowManager.endTrimMemory();
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004034 }
4035
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004036 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004037 if (Process.isIsolated()) {
4038 // Isolated processes aren't going to do UI.
4039 return;
4040 }
Romain Guya9582652011-11-10 14:20:10 -08004041 try {
4042 int uid = Process.myUid();
4043 String[] packages = getPackageManager().getPackagesForUid(uid);
4044
4045 // If there are several packages in this application we won't
4046 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004047 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004048 HardwareRenderer.setupDiskCache(cacheDir);
4049 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08004050 }
4051 } catch (RemoteException e) {
4052 // Ignore
4053 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07004054 }
4055
4056 private void updateDefaultDensity() {
4057 if (mCurDefaultDisplayDpi != Configuration.DENSITY_DPI_UNDEFINED
4058 && mCurDefaultDisplayDpi != DisplayMetrics.DENSITY_DEVICE
4059 && !mDensityCompatMode) {
4060 Slog.i(TAG, "Switching default density from "
4061 + DisplayMetrics.DENSITY_DEVICE + " to "
4062 + mCurDefaultDisplayDpi);
4063 DisplayMetrics.DENSITY_DEVICE = mCurDefaultDisplayDpi;
4064 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4065 }
4066 }
4067
Romain Guy65b345f2011-07-27 18:51:50 -07004068 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 mBoundApplication = data;
4070 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004071 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004073 mProfiler = new Profiler();
4074 mProfiler.profileFile = data.initProfileFile;
4075 mProfiler.profileFd = data.initProfileFd;
4076 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
4077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08004079 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004080 android.ddm.DdmHandleAppName.setAppName(data.processName,
4081 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004083 if (data.persistent) {
4084 // Persistent processes on low-memory devices do not get to
4085 // use hardware accelerated drawing, since this can add too much
4086 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07004087 if (!ActivityManager.isHighEndGfx()) {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004088 HardwareRenderer.disable(false);
4089 }
4090 }
Romain Guya9582652011-11-10 14:20:10 -08004091
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004092 if (mProfiler.profileFd != null) {
4093 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004094 }
4095
Joe Onoratod630f102011-03-17 18:42:26 -07004096 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
4097 // implementation to use the pool executor. Normally, we use the
4098 // serialized executor as the default. This has to happen in the
4099 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07004100 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07004101 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
4102 }
4103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004104 /*
4105 * Before spawning a new process, reset the time zone to be the system time zone.
4106 * This needs to be done because the system time zone could have changed after the
4107 * the spawning of this process. Without doing this this process would have the incorrect
4108 * system time zone.
4109 */
4110 TimeZone.setDefault(null);
4111
4112 /*
4113 * Initialize the default locale in this process for the reasons we set the time zone.
4114 */
4115 Locale.setDefault(data.config.locale);
4116
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004117 /*
4118 * Update the system configuration since its preloaded and might not
4119 * reflect configuration changes. The configuration object passed
4120 * in AppBindData can be safely assumed to be up to date
4121 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004122 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004123 mCurDefaultDisplayDpi = data.config.densityDpi;
4124 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004125
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004126 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004127
Dianne Hackborndde331c2012-08-03 14:01:57 -07004128 /**
4129 * Switch this process to density compatibility mode if needed.
4130 */
4131 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4132 == 0) {
4133 mDensityCompatMode = true;
4134 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4135 }
4136 updateDefaultDensity();
4137
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004138 final ContextImpl appContext = new ContextImpl();
4139 appContext.init(data.info, null, this);
Amith Yamasani11de39a2012-08-17 18:00:52 -07004140 if (!Process.isIsolated()) {
4141 final File cacheDir = appContext.getCacheDir();
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004142
Amith Yamasani92d57052012-08-23 10:07:52 -07004143 if (cacheDir != null) {
4144 // Provide a usable directory for temporary files
4145 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
4146
4147 setupGraphicsSupport(data.info, cacheDir);
4148 } else {
4149 Log.e(TAG, "Unable to setupGraphicsSupport due to missing cache directory");
4150 }
Amith Yamasani11de39a2012-08-17 18:00:52 -07004151 }
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004152 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004153 * For system applications on userdebug/eng builds, log stack
4154 * traces of disk and network access to dropbox for analysis.
4155 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004156 if ((data.appInfo.flags &
4157 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004158 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4159 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004160 }
4161
4162 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004163 * For apps targetting SDK Honeycomb or later, we don't allow
4164 * network usage on the main event loop / UI thread.
4165 *
4166 * Note to those grepping: this is what ultimately throws
4167 * NetworkOnMainThreadException ...
4168 */
4169 if (data.appInfo.targetSdkVersion > 9) {
4170 StrictMode.enableDeathOnNetwork();
4171 }
4172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4174 // XXX should have option to change the port.
4175 Debug.changeDebugPort(8100);
4176 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004177 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 + " is waiting for the debugger on port 8100...");
4179
4180 IActivityManager mgr = ActivityManagerNative.getDefault();
4181 try {
4182 mgr.showWaitingForDebugger(mAppThread, true);
4183 } catch (RemoteException ex) {
4184 }
4185
4186 Debug.waitForDebugger();
4187
4188 try {
4189 mgr.showWaitingForDebugger(mAppThread, false);
4190 } catch (RemoteException ex) {
4191 }
4192
4193 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004194 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 + " can be debugged on port 8100...");
4196 }
4197 }
4198
Siva Velusamy92a8b222012-03-09 16:24:04 -08004199 // Enable OpenGL tracing if required
4200 if (data.enableOpenGlTrace) {
4201 GLUtils.enableTracing();
4202 }
4203
Robert Greenwalt434203a2010-10-11 16:00:27 -07004204 /**
4205 * Initialize the default http proxy in this process for the reasons we set the time zone.
4206 */
4207 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004208 if (b != null) {
4209 // In pre-boot mode (doing initial launch to collect password), not
4210 // all system is up. This includes the connectivity service, so don't
4211 // crash if we can't get it.
4212 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4213 try {
4214 ProxyProperties proxyProperties = service.getProxy();
4215 Proxy.setHttpProxySystemProperty(proxyProperties);
4216 } catch (RemoteException e) {}
4217 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 InstrumentationInfo ii = null;
4221 try {
4222 ii = appContext.getPackageManager().
4223 getInstrumentationInfo(data.instrumentationName, 0);
4224 } catch (PackageManager.NameNotFoundException e) {
4225 }
4226 if (ii == null) {
4227 throw new RuntimeException(
4228 "Unable to find instrumentation info for: "
4229 + data.instrumentationName);
4230 }
4231
4232 mInstrumentationAppDir = ii.sourceDir;
Brian Carlstromd893a892012-04-01 21:30:26 -07004233 mInstrumentationAppLibraryDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 mInstrumentationAppPackage = ii.packageName;
4235 mInstrumentedAppDir = data.info.getAppDir();
Brian Carlstromd893a892012-04-01 21:30:26 -07004236 mInstrumentedAppLibraryDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237
4238 ApplicationInfo instrApp = new ApplicationInfo();
4239 instrApp.packageName = ii.packageName;
4240 instrApp.sourceDir = ii.sourceDir;
4241 instrApp.publicSourceDir = ii.publicSourceDir;
4242 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004243 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004244 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08004246 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004247 instrContext.init(pi, null, this);
4248
4249 try {
4250 java.lang.ClassLoader cl = instrContext.getClassLoader();
4251 mInstrumentation = (Instrumentation)
4252 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4253 } catch (Exception e) {
4254 throw new RuntimeException(
4255 "Unable to instantiate instrumentation "
4256 + data.instrumentationName + ": " + e.toString(), e);
4257 }
4258
4259 mInstrumentation.init(this, instrContext, appContext,
4260 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
4261
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004262 if (mProfiler.profileFile != null && !ii.handleProfiling
4263 && mProfiler.profileFd == null) {
4264 mProfiler.handlingProfiling = true;
4265 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004266 file.getParentFile().mkdirs();
4267 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4268 }
4269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004270 } else {
4271 mInstrumentation = new Instrumentation();
4272 }
4273
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004274 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004275 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004276 }
4277
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004278 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004279 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004280 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004281 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004282 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004283 // If the app is being launched for full backup or restore, bring it up in
4284 // a restricted environment with the base application class.
4285 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4286 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004287
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004288 // don't bring up providers in restricted mode; they may depend on the
4289 // app's custom Application class
4290 if (!data.restrictedBackupMode) {
4291 List<ProviderInfo> providers = data.providers;
4292 if (providers != null) {
4293 installContentProviders(app, providers);
4294 // For process that contains content providers, we want to
4295 // ensure that the JIT is enabled "at some point".
4296 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4297 }
4298 }
4299
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004300 // Do this after providers, since instrumentation tests generally start their
4301 // test thread at this point, and we don't want that racing.
4302 try {
4303 mInstrumentation.onCreate(data.instrumentationArgs);
4304 }
4305 catch (Exception e) {
4306 throw new RuntimeException(
4307 "Exception thrown in onCreate() of "
4308 + data.instrumentationName + ": " + e.toString(), e);
4309 }
4310
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004311 try {
4312 mInstrumentation.callApplicationOnCreate(app);
4313 } catch (Exception e) {
4314 if (!mInstrumentation.onException(app, e)) {
4315 throw new RuntimeException(
4316 "Unable to create application " + app.getClass().getName()
4317 + ": " + e.toString(), e);
4318 }
4319 }
4320 } finally {
4321 StrictMode.setThreadPolicy(savedPolicy);
4322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 }
4324
4325 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4326 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004327 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4328 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 Debug.stopMethodTracing();
4330 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004331 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004332 // + ", app thr: " + mAppThread);
4333 try {
4334 am.finishInstrumentation(mAppThread, resultCode, results);
4335 } catch (RemoteException ex) {
4336 }
4337 }
4338
Romain Guy65b345f2011-07-27 18:51:50 -07004339 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004340 Context context, List<ProviderInfo> providers) {
4341 final ArrayList<IActivityManager.ContentProviderHolder> results =
4342 new ArrayList<IActivityManager.ContentProviderHolder>();
4343
Romain Guya998dff2012-03-23 18:58:36 -07004344 for (ProviderInfo cpi : providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004345 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07004346 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004347 buf.append(cpi.authority);
4348 buf.append(": ");
4349 buf.append(cpi.name);
4350 Log.i(TAG, buf.toString());
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004351 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
4352 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
4353 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004354 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004355 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004356 }
4357 }
4358
4359 try {
4360 ActivityManagerNative.getDefault().publishContentProviders(
4361 getApplicationThread(), results);
4362 } catch (RemoteException ex) {
4363 }
4364 }
4365
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004366 public final IContentProvider acquireProvider(Context c, String name, boolean stable) {
4367 IContentProvider provider = acquireExistingProvider(c, name, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004368 if (provider != null) {
4369 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004370 }
4371
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004372 // There is a possible race here. Another thread may try to acquire
4373 // the same provider at the same time. When this happens, we want to ensure
4374 // that the first one wins.
4375 // Note that we cannot hold the lock while acquiring and installing the
4376 // provider since it might take a long time to run and it could also potentially
4377 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004378 IActivityManager.ContentProviderHolder holder = null;
4379 try {
4380 holder = ActivityManagerNative.getDefault().getContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004381 getApplicationThread(), name, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004382 } catch (RemoteException ex) {
4383 }
4384 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004385 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004386 return null;
4387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004388
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004389 // Install provider will increment the reference count for us, and break
4390 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004391 holder = installProvider(c, holder, holder.info,
4392 true /*noisy*/, holder.noReleaseNeeded, stable);
4393 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004394 }
4395
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004396 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
4397 if (stable) {
4398 prc.stableCount += 1;
4399 if (prc.stableCount == 1) {
4400 // We are acquiring a new stable reference on the provider.
4401 int unstableDelta;
4402 if (prc.removePending) {
4403 // We have a pending remove operation, which is holding the
4404 // last unstable reference. At this point we are converting
4405 // that unstable reference to our new stable reference.
4406 unstableDelta = -1;
4407 // Cancel the removal of the provider.
4408 if (DEBUG_PROVIDER) {
4409 Slog.v(TAG, "incProviderRef: stable "
4410 + "snatched provider from the jaws of death");
4411 }
4412 prc.removePending = false;
4413 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4414 } else {
4415 unstableDelta = 0;
4416 }
4417 try {
4418 if (DEBUG_PROVIDER) {
4419 Slog.v(TAG, "incProviderRef Now stable - "
4420 + prc.holder.info.name + ": unstableDelta="
4421 + unstableDelta);
4422 }
4423 ActivityManagerNative.getDefault().refContentProvider(
4424 prc.holder.connection, 1, unstableDelta);
4425 } catch (RemoteException e) {
4426 //do nothing content provider object is dead any way
4427 }
4428 }
4429 } else {
4430 prc.unstableCount += 1;
4431 if (prc.unstableCount == 1) {
4432 // We are acquiring a new unstable reference on the provider.
4433 if (prc.removePending) {
4434 // Oh look, we actually have a remove pending for the
4435 // provider, which is still holding the last unstable
4436 // reference. We just need to cancel that to take new
4437 // ownership of the reference.
4438 if (DEBUG_PROVIDER) {
4439 Slog.v(TAG, "incProviderRef: unstable "
4440 + "snatched provider from the jaws of death");
4441 }
4442 prc.removePending = false;
4443 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4444 } else {
4445 // First unstable ref, increment our count in the
4446 // activity manager.
4447 try {
4448 if (DEBUG_PROVIDER) {
4449 Slog.v(TAG, "incProviderRef: Now unstable - "
4450 + prc.holder.info.name);
4451 }
4452 ActivityManagerNative.getDefault().refContentProvider(
4453 prc.holder.connection, 0, 1);
4454 } catch (RemoteException e) {
4455 //do nothing content provider object is dead any way
4456 }
4457 }
4458 }
4459 }
4460 }
4461
4462 public final IContentProvider acquireExistingProvider(Context c, String name,
4463 boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004464 synchronized (mProviderMap) {
4465 ProviderClientRecord pr = mProviderMap.get(name);
4466 if (pr == null) {
4467 return null;
4468 }
4469
4470 IContentProvider provider = pr.mProvider;
4471 IBinder jBinder = provider.asBinder();
4472
4473 // Only increment the ref count if we have one. If we don't then the
4474 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004475 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004476 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004477 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004478 }
4479 return provider;
4480 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004481 }
4482
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004483 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
4484 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004485 return false;
4486 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004488 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004489 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004490 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004491 if (prc == null) {
4492 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004493 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004494 }
4495
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004496 boolean lastRef = false;
4497 if (stable) {
4498 if (prc.stableCount == 0) {
4499 if (DEBUG_PROVIDER) Slog.v(TAG,
4500 "releaseProvider: stable ref count already 0, how?");
4501 return false;
4502 }
4503 prc.stableCount -= 1;
4504 if (prc.stableCount == 0) {
4505 // What we do at this point depends on whether there are
4506 // any unstable refs left: if there are, we just tell the
4507 // activity manager to decrement its stable count; if there
4508 // aren't, we need to enqueue this provider to be removed,
4509 // and convert to holding a single unstable ref while
4510 // doing so.
4511 lastRef = prc.unstableCount == 0;
4512 try {
4513 if (DEBUG_PROVIDER) {
4514 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
4515 + lastRef + " - " + prc.holder.info.name);
4516 }
4517 ActivityManagerNative.getDefault().refContentProvider(
4518 prc.holder.connection, -1, lastRef ? 1 : 0);
4519 } catch (RemoteException e) {
4520 //do nothing content provider object is dead any way
4521 }
4522 }
4523 } else {
4524 if (prc.unstableCount == 0) {
4525 if (DEBUG_PROVIDER) Slog.v(TAG,
4526 "releaseProvider: unstable ref count already 0, how?");
4527 return false;
4528 }
4529 prc.unstableCount -= 1;
4530 if (prc.unstableCount == 0) {
4531 // If this is the last reference, we need to enqueue
4532 // this provider to be removed instead of telling the
4533 // activity manager to remove it at this point.
4534 lastRef = prc.stableCount == 0;
4535 if (!lastRef) {
4536 try {
4537 if (DEBUG_PROVIDER) {
4538 Slog.v(TAG, "releaseProvider: No longer unstable - "
4539 + prc.holder.info.name);
4540 }
4541 ActivityManagerNative.getDefault().refContentProvider(
4542 prc.holder.connection, 0, -1);
4543 } catch (RemoteException e) {
4544 //do nothing content provider object is dead any way
4545 }
4546 }
4547 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004548 }
4549
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004550 if (lastRef) {
4551 if (!prc.removePending) {
4552 // Schedule the actual remove asynchronously, since we don't know the context
4553 // this will be called in.
4554 // TODO: it would be nice to post a delayed message, so
4555 // if we come back and need the same provider quickly
4556 // we will still have it available.
4557 if (DEBUG_PROVIDER) {
4558 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
4559 + prc.holder.info.name);
4560 }
4561 prc.removePending = true;
4562 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
4563 mH.sendMessage(msg);
4564 } else {
4565 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
4566 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004567 }
4568 return true;
4569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004570 }
4571
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004572 final void completeRemoveProvider(ProviderRefCount prc) {
4573 synchronized (mProviderMap) {
4574 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004575 // There was a race! Some other client managed to acquire
4576 // the provider before the removal was completed.
4577 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004578 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004579 + "provider still in use");
4580 return;
4581 }
4582
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004583 final IBinder jBinder = prc.holder.provider.asBinder();
4584 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
4585 if (existingPrc == prc) {
4586 mProviderRefCountMap.remove(jBinder);
4587 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004588
4589 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
4590 while (iter.hasNext()) {
4591 ProviderClientRecord pr = iter.next();
4592 IBinder myBinder = pr.mProvider.asBinder();
4593 if (myBinder == jBinder) {
4594 iter.remove();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004595 }
4596 }
4597 }
4598
4599 try {
4600 if (DEBUG_PROVIDER) {
4601 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4602 + "removeContentProvider(" + prc.holder.info.name + ")");
4603 }
4604 ActivityManagerNative.getDefault().removeContentProvider(
4605 prc.holder.connection, false);
4606 } catch (RemoteException e) {
4607 //do nothing content provider object is dead any way
4608 }
4609 }
4610
4611 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
4612 synchronized(mProviderMap) {
4613 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4614 if (prc != null) {
4615 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
4616 + provider + " " + prc.holder.info.name);
4617 mProviderRefCountMap.remove(provider);
4618 if (prc.client != null && prc.client.mNames != null) {
4619 for (String name : prc.client.mNames) {
4620 ProviderClientRecord pr = mProviderMap.get(name);
4621 if (pr != null && pr.mProvider.asBinder() == provider) {
4622 Slog.i(TAG, "Removing dead content provider: " + name);
4623 mProviderMap.remove(name);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004624 }
4625 }
4626 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004627 if (fromClient) {
4628 // We found out about this due to execution in our client
4629 // code. Tell the activity manager about it now, to ensure
4630 // that the next time we go to do anything with the provider
4631 // it knows it is dead (so we don't race with its death
4632 // notification).
4633 try {
4634 ActivityManagerNative.getDefault().unstableProviderDied(
4635 prc.holder.connection);
4636 } catch (RemoteException e) {
4637 //do nothing content provider object is dead any way
4638 }
4639 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004640 }
4641 }
4642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004643
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004644 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
4645 ContentProvider localProvider,IActivityManager.ContentProviderHolder holder) {
4646 String names[] = PATTERN_SEMICOLON.split(holder.info.authority);
4647 ProviderClientRecord pcr = new ProviderClientRecord(names, provider,
4648 localProvider, holder);
4649 for (int i = 0; i < names.length; i++) {
4650 ProviderClientRecord existing = mProviderMap.get(names[i]);
4651 if (existing != null) {
4652 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
4653 + " already published as " + names[i]);
4654 } else {
4655 mProviderMap.put(names[i], pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004656 }
4657 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004658 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004659 }
4660
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004661 /**
4662 * Installs the provider.
4663 *
4664 * Providers that are local to the process or that come from the system server
4665 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4666 * Other remote providers are reference counted. The initial reference count
4667 * for all reference counted providers is one. Providers that are not reference
4668 * counted do not have a reference count (at all).
4669 *
4670 * This method detects when a provider has already been installed. When this happens,
4671 * it increments the reference count of the existing provider (if appropriate)
4672 * and returns the existing provider. This can happen due to concurrent
4673 * attempts to acquire the same provider.
4674 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004675 private IActivityManager.ContentProviderHolder installProvider(Context context,
4676 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
4677 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004678 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004679 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07004680 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004681 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004682 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004683 + info.name);
4684 }
4685 Context c = null;
4686 ApplicationInfo ai = info.applicationInfo;
4687 if (context.getPackageName().equals(ai.packageName)) {
4688 c = context;
4689 } else if (mInitialApplication != null &&
4690 mInitialApplication.getPackageName().equals(ai.packageName)) {
4691 c = mInitialApplication;
4692 } else {
4693 try {
4694 c = context.createPackageContext(ai.packageName,
4695 Context.CONTEXT_INCLUDE_CODE);
4696 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004697 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004698 }
4699 }
4700 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004701 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004702 ai.packageName +
4703 " while loading content provider " +
4704 info.name);
4705 return null;
4706 }
4707 try {
4708 final java.lang.ClassLoader cl = c.getClassLoader();
4709 localProvider = (ContentProvider)cl.
4710 loadClass(info.name).newInstance();
4711 provider = localProvider.getIContentProvider();
4712 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004713 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004714 info.name + " from sourceDir " +
4715 info.applicationInfo.sourceDir);
4716 return null;
4717 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004718 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004719 TAG, "Instantiating local provider " + info.name);
4720 // XXX Need to create the correct context for this provider.
4721 localProvider.attachInfo(c, info);
4722 } catch (java.lang.Exception e) {
4723 if (!mInstrumentation.onException(null, e)) {
4724 throw new RuntimeException(
4725 "Unable to get provider " + info.name
4726 + ": " + e.toString(), e);
4727 }
4728 return null;
4729 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004730 } else {
4731 provider = holder.provider;
4732 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004733 + info.name);
4734 }
4735
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004736 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004737
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004738 synchronized (mProviderMap) {
4739 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
4740 + " / " + info.name);
4741 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004742 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004743 ComponentName cname = new ComponentName(info.packageName, info.name);
4744 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004745 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004746 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004747 Slog.v(TAG, "installProvider: lost the race, "
4748 + "using existing local provider");
4749 }
4750 provider = pr.mProvider;
4751 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004752 holder = new IActivityManager.ContentProviderHolder(info);
4753 holder.provider = provider;
4754 holder.noReleaseNeeded = true;
4755 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004756 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004757 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004758 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004759 retHolder = pr.mHolder;
4760 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004761 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4762 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004763 if (DEBUG_PROVIDER) {
4764 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004765 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004766 // We need to transfer our new reference to the existing
4767 // ref count, releasing the old one... but only if
4768 // release is needed (that is, it is not running in the
4769 // system process).
4770 if (!noReleaseNeeded) {
4771 incProviderRefLocked(prc, stable);
4772 try {
4773 ActivityManagerNative.getDefault().removeContentProvider(
4774 holder.connection, stable);
4775 } catch (RemoteException e) {
4776 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004777 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004778 }
4779 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004780 ProviderClientRecord client = installProviderAuthoritiesLocked(
4781 provider, localProvider, holder);
4782 if (noReleaseNeeded) {
4783 prc = new ProviderRefCount(holder, client, 1000, 1000);
4784 } else {
4785 prc = stable
4786 ? new ProviderRefCount(holder, client, 1, 0)
4787 : new ProviderRefCount(holder, client, 0, 1);
4788 }
4789 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004790 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004791 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004792 }
4793 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004794
4795 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004796 }
4797
Romain Guy65b345f2011-07-27 18:51:50 -07004798 private void attach(boolean system) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004799 sThreadLocal.set(this);
4800 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004801 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004802 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004803 public void run() {
4804 ensureJitEnabled();
4805 }
4806 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004807 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
4808 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004809 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4810 IActivityManager mgr = ActivityManagerNative.getDefault();
4811 try {
4812 mgr.attachApplication(mAppThread);
4813 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004814 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004815 }
4816 } else {
4817 // Don't set application object here -- if the system crashes,
4818 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004819 android.ddm.DdmHandleAppName.setAppName("system_process",
4820 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 try {
4822 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004823 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004824 context.init(getSystemContext().mPackageInfo, null, this);
4825 Application app = Instrumentation.newApplication(Application.class, context);
4826 mAllApplications.add(app);
4827 mInitialApplication = app;
4828 app.onCreate();
4829 } catch (Exception e) {
4830 throw new RuntimeException(
4831 "Unable to instantiate Application():" + e.toString(), e);
4832 }
4833 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004834
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004835 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004836 public void onConfigurationChanged(Configuration newConfig) {
4837 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004838 // We need to apply this change to the resources
4839 // immediately, because upon returning the view
4840 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004841 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004842 // This actually changed the resources! Tell
4843 // everyone about it.
4844 if (mPendingConfiguration == null ||
4845 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4846 mPendingConfiguration = newConfig;
4847
4848 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4849 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004850 }
4851 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004852 }
4853 public void onLowMemory() {
4854 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004855 public void onTrimMemory(int level) {
4856 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004857 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004858 }
4859
Romain Guy5e9120d2012-01-30 12:17:22 -08004860 public static ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004861 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004862 ActivityThread thread = new ActivityThread();
4863 thread.attach(true);
4864 return thread;
4865 }
4866
Jeff Brown10e89712011-07-08 18:52:57 -07004867 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004868 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004869 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004870 }
4871 }
4872
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004873 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004874 synchronized (mPackages) {
4875 if (mCoreSettings != null) {
4876 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004877 } else {
4878 return defaultValue;
4879 }
4880 }
4881 }
4882
Geremy Condra69689a72012-09-11 16:57:17 -07004883 private static class EventLoggingReporter implements EventLogger.Reporter {
4884 @Override
4885 public void report (int code, Object... list) {
4886 EventLog.writeEvent(code, list);
4887 }
4888 }
4889
Romain Guy65b345f2011-07-27 18:51:50 -07004890 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004891 SamplingProfilerIntegration.start();
4892
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004893 // CloseGuard defaults to true and can be quite spammy. We
4894 // disable it here, but selectively enable it later (via
4895 // StrictMode) on debug builds, but using DropBox, not logs.
4896 CloseGuard.setEnabled(false);
4897
Jeff Sharkeyb049e212012-09-07 23:16:01 -07004898 Environment.initForCurrentUser();
4899
Geremy Condra69689a72012-09-11 16:57:17 -07004900 // Set the reporter for event logging in libcore
4901 EventLogger.setReporter(new EventLoggingReporter());
4902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004903 Process.setArgV0("<pre-initialized>");
4904
4905 Looper.prepareMainLooper();
4906
4907 ActivityThread thread = new ActivityThread();
4908 thread.attach(false);
4909
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07004910 if (sMainThreadHandler == null) {
4911 sMainThreadHandler = thread.getHandler();
4912 }
4913
Romain Guy5e9120d2012-01-30 12:17:22 -08004914 AsyncTask.init();
4915
Dianne Hackborn287952c2010-09-22 22:34:31 -07004916 if (false) {
4917 Looper.myLooper().setMessageLogging(new
4918 LogPrinter(Log.DEBUG, "ActivityThread"));
4919 }
4920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004921 Looper.loop();
4922
Jeff Brown10e89712011-07-08 18:52:57 -07004923 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004924 }
4925}