blob: 5d1d4618e32ca0de35d355046902c988ac940245 [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;
Robert Greenwalt434203a2010-10-11 16:00:27 -070045import android.net.IConnectivityManager;
46import android.net.Proxy;
47import android.net.ProxyProperties;
Romain Guya9582652011-11-10 14:20:10 -080048import android.opengl.GLUtils;
Joe Onoratod630f102011-03-17 18:42:26 -070049import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070050import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.Bundle;
52import android.os.Debug;
53import android.os.Handler;
54import android.os.IBinder;
55import android.os.Looper;
56import android.os.Message;
57import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070058import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Process;
60import android.os.RemoteException;
61import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070062import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.SystemClock;
Amith Yamasani742a6712011-05-04 14:49:28 -070064import android.os.UserId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.util.AndroidRuntimeException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.util.DisplayMetrics;
67import android.util.EventLog;
68import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070069import android.util.LogPrinter;
Jeff Brown6754ba22011-12-14 20:20:01 -080070import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080071import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070073import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.view.View;
75import android.view.ViewDebug;
76import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070077import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.view.Window;
79import android.view.WindowManager;
80import android.view.WindowManagerImpl;
Jason Samsa6f338c2012-02-24 16:22:16 -080081import android.renderscript.RenderScript;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83import com.android.internal.os.BinderInternal;
84import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070085import com.android.internal.os.SamplingProfilerIntegration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl;
88
89import java.io.File;
90import java.io.FileDescriptor;
91import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070092import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import java.io.PrintWriter;
94import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -070095import java.net.InetAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import java.util.ArrayList;
97import java.util.HashMap;
98import java.util.Iterator;
99import java.util.List;
100import java.util.Locale;
101import java.util.Map;
102import java.util.TimeZone;
103import java.util.regex.Pattern;
104
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800105import dalvik.system.CloseGuard;
Bob Leee5408332009-09-04 18:31:17 -0700106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107final class SuperNotCalledException extends AndroidRuntimeException {
108 public SuperNotCalledException(String msg) {
109 super(msg);
110 }
111}
112
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700113final class RemoteServiceException extends AndroidRuntimeException {
114 public RemoteServiceException(String msg) {
115 super(msg);
116 }
117}
118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119/**
120 * This manages the execution of the main thread in an
121 * application process, scheduling and executing activities,
122 * broadcasts, and other operations on it as the activity
123 * manager requests.
124 *
125 * {@hide}
126 */
127public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700128 /** @hide */
129 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700130 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700131 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700132 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700133 /** @hide */
134 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700135 private static final boolean DEBUG_RESULTS = false;
Christopher Tate4a627c72011-04-01 14:43:32 -0700136 private static final boolean DEBUG_BACKUP = true;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700137 private static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800138 private static final boolean DEBUG_SERVICE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
140 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
141 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
142 private static final int LOG_ON_PAUSE_CALLED = 30021;
143 private static final int LOG_ON_RESUME_CALLED = 30022;
144
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700145 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700146
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700147 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700149 final ApplicationThread mAppThread = new ApplicationThread();
150 final Looper mLooper = Looper.myLooper();
151 final H mH = new H();
152 final HashMap<IBinder, ActivityClientRecord> mActivities
153 = new HashMap<IBinder, ActivityClientRecord>();
154 // List of new activities (via ActivityRecord.nextIdle) that should
155 // be reported when next we idle.
156 ActivityClientRecord mNewActivities = null;
157 // Number of activities that are currently visible on-screen.
158 int mNumVisibleActivities = 0;
159 final HashMap<IBinder, Service> mServices
160 = new HashMap<IBinder, Service>();
161 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700162 Profiler mProfiler;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700163 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700164 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700165 Configuration mResConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700166 CompatibilityInfo mResCompatibilityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700167 Application mInitialApplication;
168 final ArrayList<Application> mAllApplications
169 = new ArrayList<Application>();
170 // set of instantiated backup agents, keyed by package name
171 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Romain Guy65b345f2011-07-27 18:51:50 -0700172 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700173 Instrumentation mInstrumentation;
174 String mInstrumentationAppDir = null;
175 String mInstrumentationAppPackage = null;
176 String mInstrumentedAppDir = null;
177 boolean mSystemThread = false;
178 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700180 // These can be accessed by multiple threads; mPackages is the lock.
181 // XXX For now we keep around information about all packages we have
182 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800183 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700184 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800185 // which means this lock gets held while the activity and window managers
186 // holds their own lock. Thus you MUST NEVER call back into the activity manager
187 // or window manager or anything that depends on them while holding this lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700188 final HashMap<String, WeakReference<LoadedApk>> mPackages
189 = new HashMap<String, WeakReference<LoadedApk>>();
190 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
191 = new HashMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700192 final HashMap<CompatibilityInfo, DisplayMetrics> mDisplayMetrics
193 = new HashMap<CompatibilityInfo, DisplayMetrics>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700194 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
195 = new HashMap<ResourcesKey, WeakReference<Resources> >();
196 final ArrayList<ActivityClientRecord> mRelaunchingActivities
197 = new ArrayList<ActivityClientRecord>();
198 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700200 // The lock of mProviderMap protects the following variables.
201 final HashMap<String, ProviderClientRecord> mProviderMap
202 = new HashMap<String, ProviderClientRecord>();
203 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
204 = new HashMap<IBinder, ProviderRefCount>();
205 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
206 = new HashMap<IBinder, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207
Jeff Hamilton52d32032011-01-08 15:31:26 -0600208 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
209 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
210
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700211 final GcIdler mGcIdler = new GcIdler();
212 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700214 static Handler sMainThreadHandler; // set once in main()
215
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800216 Bundle mCoreSettings = null;
217
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400218 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700220 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 Intent intent;
222 Bundle state;
223 Activity activity;
224 Window window;
225 Activity parent;
226 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700227 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 boolean paused;
229 boolean stopped;
230 boolean hideForNow;
231 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700232 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700233 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700235 String profileFile;
236 ParcelFileDescriptor profileFd;
237 boolean autoStopProfiler;
238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400240 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700241 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242
243 List<ResultInfo> pendingResults;
244 List<Intent> pendingIntents;
245
246 boolean startsNotResumed;
247 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800248 int pendingConfigChanges;
249 boolean onlyLocalRequest;
250
251 View mPendingRemoveWindow;
252 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700254 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 parent = null;
256 embeddedID = null;
257 paused = false;
258 stopped = false;
259 hideForNow = false;
260 nextIdle = null;
261 }
262
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800263 public boolean isPreHoneycomb() {
264 if (activity != null) {
265 return activity.getApplicationInfo().targetSdkVersion
266 < android.os.Build.VERSION_CODES.HONEYCOMB;
267 }
268 return false;
269 }
270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 public String toString() {
272 ComponentName componentName = intent.getComponent();
273 return "ActivityRecord{"
274 + Integer.toHexString(System.identityHashCode(this))
275 + " token=" + token + " " + (componentName == null
276 ? "no component name" : componentName.toShortString())
277 + "}";
278 }
279 }
280
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400281 final class ProviderClientRecord implements IBinder.DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 final String mName;
283 final IContentProvider mProvider;
284 final ContentProvider mLocalProvider;
285
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700286 ProviderClientRecord(String name, IContentProvider provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 ContentProvider localProvider) {
288 mName = name;
289 mProvider = provider;
290 mLocalProvider = localProvider;
291 }
292
293 public void binderDied() {
294 removeDeadProvider(mName, mProvider);
295 }
296 }
297
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400298 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 List<Intent> intents;
300 IBinder token;
301 public String toString() {
302 return "NewIntentData{intents=" + intents + " token=" + token + "}";
303 }
304 }
305
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400306 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700307 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
308 boolean ordered, boolean sticky, IBinder token) {
309 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky, token);
310 this.intent = intent;
311 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 Intent intent;
314 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400315 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 public String toString() {
317 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700318 info.packageName + " resultCode=" + getResultCode()
319 + " resultData=" + getResultData() + " resultExtras="
320 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 }
322 }
323
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400324 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700325 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400326 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700327 int backupMode;
328 public String toString() {
329 return "CreateBackupAgentData{appInfo=" + appInfo
330 + " backupAgent=" + appInfo.backupAgentName
331 + " mode=" + backupMode + "}";
332 }
333 }
Bob Leee5408332009-09-04 18:31:17 -0700334
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400335 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 IBinder token;
337 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400338 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 Intent intent;
340 public String toString() {
341 return "CreateServiceData{token=" + token + " className="
342 + info.name + " packageName=" + info.packageName
343 + " intent=" + intent + "}";
344 }
345 }
346
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400347 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 IBinder token;
349 Intent intent;
350 boolean rebind;
351 public String toString() {
352 return "BindServiceData{token=" + token + " intent=" + intent + "}";
353 }
354 }
355
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400356 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700358 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700360 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 Intent args;
362 public String toString() {
363 return "ServiceArgsData{token=" + token + " startId=" + startId
364 + " args=" + args + "}";
365 }
366 }
367
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400368 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700369 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 String processName;
371 ApplicationInfo appInfo;
372 List<ProviderInfo> providers;
373 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 Bundle instrumentationArgs;
375 IInstrumentationWatcher instrumentationWatcher;
376 int debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700377 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700378 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400380 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700381
382 /** Initial values for {@link Profiler}. */
383 String initProfileFile;
384 ParcelFileDescriptor initProfileFd;
385 boolean initAutoStopProfiler;
386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 public String toString() {
388 return "AppBindData{appInfo=" + appInfo + "}";
389 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700390 }
391
392 static final class Profiler {
393 String profileFile;
394 ParcelFileDescriptor profileFd;
395 boolean autoStopProfiler;
396 boolean profiling;
397 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700398 public void setProfiler(String file, ParcelFileDescriptor fd) {
399 if (profiling) {
400 if (fd != null) {
401 try {
402 fd.close();
403 } catch (IOException e) {
404 }
405 }
406 return;
407 }
408 if (profileFd != null) {
409 try {
410 profileFd.close();
411 } catch (IOException e) {
412 }
413 }
414 profileFile = file;
415 profileFd = fd;
416 }
417 public void startProfiling() {
418 if (profileFd == null || profiling) {
419 return;
420 }
421 try {
422 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
423 8 * 1024 * 1024, 0);
424 profiling = true;
425 } catch (RuntimeException e) {
426 Slog.w(TAG, "Profiling failed on path " + profileFile);
427 try {
428 profileFd.close();
429 profileFd = null;
430 } catch (IOException e2) {
431 Slog.w(TAG, "Failure closing profile fd", e2);
432 }
433 }
434 }
435 public void stopProfiling() {
436 if (profiling) {
437 profiling = false;
438 Debug.stopMethodTracing();
439 if (profileFd != null) {
440 try {
441 profileFd.close();
442 } catch (IOException e) {
443 }
444 }
445 profileFd = null;
446 profileFile = null;
447 }
448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400451 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700452 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700453 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800454 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 }
457
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400458 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 IBinder token;
460 List<ResultInfo> results;
461 public String toString() {
462 return "ResultData{token=" + token + " results" + results + "}";
463 }
464 }
465
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400466 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800467 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 String what;
469 String who;
470 }
471
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400472 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700473 String path;
474 ParcelFileDescriptor fd;
475 }
476
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400477 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700478 String path;
479 ParcelFileDescriptor fd;
480 }
481
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400482 static final class UpdateCompatibilityData {
483 String pkg;
484 CompatibilityInfo info;
485 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700486
Romain Guy65b345f2011-07-27 18:51:50 -0700487 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700488
Romain Guy65b345f2011-07-27 18:51:50 -0700489 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700490 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
491 private static final String ONE_COUNT_COLUMN = "%21s %8d";
492 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700493 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 // Formatting for checkin service - update version if row format changes
496 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700497
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700498 private void updatePendingConfiguration(Configuration config) {
499 synchronized (mPackages) {
500 if (mPendingConfiguration == null ||
501 mPendingConfiguration.isOtherSeqNewer(config)) {
502 mPendingConfiguration = config;
503 }
504 }
505 }
506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 public final void schedulePauseActivity(IBinder token, boolean finished,
508 boolean userLeaving, int configChanges) {
509 queueOrSendMessage(
510 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
511 token,
512 (userLeaving ? 1 : 0),
513 configChanges);
514 }
515
516 public final void scheduleStopActivity(IBinder token, boolean showWindow,
517 int configChanges) {
518 queueOrSendMessage(
519 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
520 token, 0, configChanges);
521 }
522
523 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
524 queueOrSendMessage(
525 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
526 token);
527 }
528
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800529 public final void scheduleSleeping(IBinder token, boolean sleeping) {
530 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
531 }
532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
534 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
535 }
536
537 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
538 ResultData res = new ResultData();
539 res.token = token;
540 res.results = results;
541 queueOrSendMessage(H.SEND_RESULT, res);
542 }
543
544 // we use token to identify this activity without having to send the
545 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700546 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700547 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
548 Bundle state, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700549 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
550 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700551 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552
553 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700554 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 r.intent = intent;
556 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400557 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 r.state = state;
559
560 r.pendingResults = pendingResults;
561 r.pendingIntents = pendingNewIntents;
562
563 r.startsNotResumed = notResumed;
564 r.isForward = isForward;
565
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700566 r.profileFile = profileName;
567 r.profileFd = profileFd;
568 r.autoStopProfiler = autoStopProfiler;
569
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700570 updatePendingConfiguration(curConfig);
571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
573 }
574
575 public final void scheduleRelaunchActivity(IBinder token,
576 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800577 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800578 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
579 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 }
581
582 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
583 NewIntentData data = new NewIntentData();
584 data.intents = intents;
585 data.token = token;
586
587 queueOrSendMessage(H.NEW_INTENT, data);
588 }
589
590 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
591 int configChanges) {
592 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
593 configChanges);
594 }
595
596 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400597 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
598 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700599 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
600 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400602 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 queueOrSendMessage(H.RECEIVER, r);
604 }
605
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400606 public final void scheduleCreateBackupAgent(ApplicationInfo app,
607 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700608 CreateBackupAgentData d = new CreateBackupAgentData();
609 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400610 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700611 d.backupMode = backupMode;
612
613 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
614 }
615
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400616 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
617 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700618 CreateBackupAgentData d = new CreateBackupAgentData();
619 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400620 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700621
622 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
623 }
624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400626 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 CreateServiceData s = new CreateServiceData();
628 s.token = token;
629 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400630 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631
632 queueOrSendMessage(H.CREATE_SERVICE, s);
633 }
634
635 public final void scheduleBindService(IBinder token, Intent intent,
636 boolean rebind) {
637 BindServiceData s = new BindServiceData();
638 s.token = token;
639 s.intent = intent;
640 s.rebind = rebind;
641
Amith Yamasani742a6712011-05-04 14:49:28 -0700642 if (DEBUG_SERVICE)
643 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
644 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 queueOrSendMessage(H.BIND_SERVICE, s);
646 }
647
648 public final void scheduleUnbindService(IBinder token, Intent intent) {
649 BindServiceData s = new BindServiceData();
650 s.token = token;
651 s.intent = intent;
652
653 queueOrSendMessage(H.UNBIND_SERVICE, s);
654 }
655
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700656 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700657 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 ServiceArgsData s = new ServiceArgsData();
659 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700660 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700662 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 s.args = args;
664
665 queueOrSendMessage(H.SERVICE_ARGS, s);
666 }
667
668 public final void scheduleStopService(IBinder token) {
669 queueOrSendMessage(H.STOP_SERVICE, token);
670 }
671
672 public final void bindApplication(String processName,
673 ApplicationInfo appInfo, List<ProviderInfo> providers,
674 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700675 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700677 int debugMode, boolean isRestrictedBackupMode, boolean persistent,
678 Configuration config, CompatibilityInfo compatInfo,
679 Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680
681 if (services != null) {
682 // Setup the service cache in the ServiceManager
683 ServiceManager.initServiceCache(services);
684 }
685
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800686 setCoreSettings(coreSettings);
687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 AppBindData data = new AppBindData();
689 data.processName = processName;
690 data.appInfo = appInfo;
691 data.providers = providers;
692 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 data.instrumentationArgs = instrumentationArgs;
694 data.instrumentationWatcher = instrumentationWatcher;
695 data.debugMode = debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700696 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700697 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400699 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700700 data.initProfileFile = profileFile;
701 data.initProfileFd = profileFd;
702 data.initAutoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 queueOrSendMessage(H.BIND_APPLICATION, data);
704 }
705
706 public final void scheduleExit() {
707 queueOrSendMessage(H.EXIT_APPLICATION, null);
708 }
709
Christopher Tate5e1ab332009-09-01 20:32:49 -0700710 public final void scheduleSuicide() {
711 queueOrSendMessage(H.SUICIDE, null);
712 }
713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 public void requestThumbnail(IBinder token) {
715 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
716 }
717
718 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700719 updatePendingConfiguration(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
721 }
722
723 public void updateTimeZone() {
724 TimeZone.setDefault(null);
725 }
726
Robert Greenwalt03595d02010-11-02 14:08:23 -0700727 public void clearDnsCache() {
728 // a non-standard API to get this to libcore
729 InetAddress.clearDnsCache();
730 }
731
Robert Greenwalt434203a2010-10-11 16:00:27 -0700732 public void setHttpProxy(String host, String port, String exclList) {
733 Proxy.setHttpProxySystemProperty(host, port, exclList);
734 }
735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 public void processInBackground() {
737 mH.removeMessages(H.GC_WHEN_IDLE);
738 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
739 }
740
741 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700742 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700743 try {
744 data.fd = ParcelFileDescriptor.dup(fd);
745 data.token = servicetoken;
746 data.args = args;
747 queueOrSendMessage(H.DUMP_SERVICE, data);
748 } catch (IOException e) {
749 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 }
751 }
752
753 // This function exists to make sure all receiver dispatching is
754 // correctly ordered, since these are one-way calls and the binder driver
755 // applies transaction ordering per object for such calls.
756 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700757 int resultCode, String dataStr, Bundle extras, boolean ordered,
758 boolean sticky) throws RemoteException {
759 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 }
Bob Leee5408332009-09-04 18:31:17 -0700761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 public void scheduleLowMemory() {
763 queueOrSendMessage(H.LOW_MEMORY, null);
764 }
765
766 public void scheduleActivityConfigurationChanged(IBinder token) {
767 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
768 }
769
Romain Guy7eabe552011-07-21 14:56:34 -0700770 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
771 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700772 ProfilerControlData pcd = new ProfilerControlData();
773 pcd.path = path;
774 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700775 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800776 }
777
Andy McFadden824c5102010-07-09 16:26:57 -0700778 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
779 DumpHeapData dhd = new DumpHeapData();
780 dhd.path = path;
781 dhd.fd = fd;
782 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
783 }
784
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700785 public void setSchedulingGroup(int group) {
786 // Note: do this immediately, since going into the foreground
787 // should happen regardless of what pending work we have to do
788 // and the activity manager will wait for us to report back that
789 // we are done before sending us to the background.
790 try {
791 Process.setProcessGroup(Process.myPid(), group);
792 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800793 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700794 }
795 }
Bob Leee5408332009-09-04 18:31:17 -0700796
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700797 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
798 Debug.getMemoryInfo(outInfo);
799 }
Bob Leee5408332009-09-04 18:31:17 -0700800
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700801 public void dispatchPackageBroadcast(int cmd, String[] packages) {
802 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
803 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700804
805 public void scheduleCrash(String msg) {
806 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
807 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700808
Dianne Hackborn30d71892010-12-11 10:37:55 -0800809 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
810 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700811 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700812 try {
813 data.fd = ParcelFileDescriptor.dup(fd);
814 data.token = activitytoken;
815 data.prefix = prefix;
816 data.args = args;
817 queueOrSendMessage(H.DUMP_ACTIVITY, data);
818 } catch (IOException e) {
819 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700820 }
821 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700822
Marco Nelissen18cb2872011-11-15 11:19:53 -0800823 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
824 String[] args) {
825 DumpComponentInfo data = new DumpComponentInfo();
826 try {
827 data.fd = ParcelFileDescriptor.dup(fd);
828 data.token = providertoken;
829 data.args = args;
830 queueOrSendMessage(H.DUMP_PROVIDER, data);
831 } catch (IOException e) {
832 Slog.w(TAG, "dumpProvider failed", e);
833 }
834 }
835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700837 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
838 boolean all, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700839 FileOutputStream fout = new FileOutputStream(fd);
840 PrintWriter pw = new PrintWriter(fout);
841 try {
Dianne Hackbornb437e092011-08-05 17:50:29 -0700842 return dumpMemInfo(pw, checkin, all, args);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700843 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700844 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700845 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700846 }
847
Dianne Hackbornb437e092011-08-05 17:50:29 -0700848 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean all,
849 String[] args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 long nativeMax = Debug.getNativeHeapSize() / 1024;
851 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
852 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
853
854 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
855 Debug.getMemoryInfo(memInfo);
856
Dianne Hackbornb437e092011-08-05 17:50:29 -0700857 if (!all) {
858 return memInfo;
859 }
860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 Runtime runtime = Runtime.getRuntime();
862
863 long dalvikMax = runtime.totalMemory() / 1024;
864 long dalvikFree = runtime.freeMemory() / 1024;
865 long dalvikAllocated = dalvikMax - dalvikFree;
866 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700867 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700868 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
869 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 int globalAssetCount = AssetManager.getGlobalAssetCount();
871 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
872 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
873 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
874 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700875 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800876 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700879 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 // NOTE: if you change anything significant below, also consider changing
881 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700882 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 // Header
886 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
887 pw.print(Process.myPid()); pw.print(',');
888 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 // Heap info - max
891 pw.print(nativeMax); pw.print(',');
892 pw.print(dalvikMax); pw.print(',');
893 pw.print("N/A,");
894 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 // Heap info - allocated
897 pw.print(nativeAllocated); pw.print(',');
898 pw.print(dalvikAllocated); pw.print(',');
899 pw.print("N/A,");
900 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 // Heap info - free
903 pw.print(nativeFree); pw.print(',');
904 pw.print(dalvikFree); pw.print(',');
905 pw.print("N/A,");
906 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 // Heap info - proportional set size
909 pw.print(memInfo.nativePss); pw.print(',');
910 pw.print(memInfo.dalvikPss); pw.print(',');
911 pw.print(memInfo.otherPss); pw.print(',');
912 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700915 pw.print(memInfo.nativeSharedDirty); pw.print(',');
916 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
917 pw.print(memInfo.otherSharedDirty); pw.print(',');
918 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
919 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700922 pw.print(memInfo.nativePrivateDirty); pw.print(',');
923 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
924 pw.print(memInfo.otherPrivateDirty); pw.print(',');
925 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
926 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 // Object counts
929 pw.print(viewInstanceCount); pw.print(',');
930 pw.print(viewRootInstanceCount); pw.print(',');
931 pw.print(appContextInstanceCount); pw.print(',');
932 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 pw.print(globalAssetCount); pw.print(',');
935 pw.print(globalAssetManagerCount); pw.print(',');
936 pw.print(binderLocalObjectCount); pw.print(',');
937 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 pw.print(binderDeathObjectCount); pw.print(',');
940 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 // SQL
Vasu Noric3849202010-03-09 10:47:25 -0800943 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -0800944 pw.print(stats.memoryUsed / 1024); pw.print(',');
945 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700946 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800947 for (int i = 0; i < stats.dbStats.size(); i++) {
948 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700949 pw.print(','); pw.print(dbStats.dbName);
950 pw.print(','); pw.print(dbStats.pageSize);
951 pw.print(','); pw.print(dbStats.dbSize);
952 pw.print(','); pw.print(dbStats.lookaside);
953 pw.print(','); pw.print(dbStats.cache);
954 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800955 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700956 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700957
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700958 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 }
Bob Leee5408332009-09-04 18:31:17 -0700960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700962 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
963 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
964 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
965 "------");
966 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
967 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
968 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
969 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700971 int otherPss = memInfo.otherPss;
972 int otherSharedDirty = memInfo.otherSharedDirty;
973 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700975 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Romain Guy65b345f2011-07-27 18:51:50 -0700976 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700977 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
978 memInfo.getOtherPrivateDirty(i), "", "", "");
979 otherPss -= memInfo.getOtherPss(i);
980 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
981 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
982 }
983
984 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
985 otherPrivateDirty, "", "", "");
986 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
987 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
988 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
989 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990
991 pw.println(" ");
992 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -0700993 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 viewRootInstanceCount);
995
996 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
997 "Activities:", activityInstanceCount);
998
999 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1000 "AssetManagers:", globalAssetManagerCount);
1001
1002 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1003 "Proxy Binders:", binderProxyObjectCount);
1004 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
1005
1006 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 // SQLite mem info
1009 pw.println(" ");
1010 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001011 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1012 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1013 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001014 pw.println(" ");
1015 int N = stats.dbStats.size();
1016 if (N > 0) {
1017 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001018 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1019 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001020 for (int i = 0; i < N; i++) {
1021 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001022 printRow(pw, DB_INFO_FORMAT,
1023 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1024 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1025 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1026 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001027 }
1028 }
Bob Leee5408332009-09-04 18:31:17 -07001029
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001030 // Asset details.
1031 String assetAlloc = AssetManager.getAssetAllocations();
1032 if (assetAlloc != null) {
1033 pw.println(" ");
1034 pw.println(" Asset Allocations");
1035 pw.print(assetAlloc);
1036 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001037
1038 return memInfo;
1039 }
1040
1041 @Override
1042 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1043 dumpGraphicsInfo(fd);
Romain Guy65b345f2011-07-27 18:51:50 -07001044 WindowManagerImpl.getDefault().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 }
1046
Jeff Brown6754ba22011-12-14 20:20:01 -08001047 @Override
1048 public void dumpDbInfo(FileDescriptor fd, String[] args) {
1049 PrintWriter pw = new PrintWriter(new FileOutputStream(fd));
1050 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1051 SQLiteDebug.dump(printer, args);
1052 pw.flush();
1053 }
1054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 private void printRow(PrintWriter pw, String format, Object...objs) {
1056 pw.println(String.format(format, objs));
1057 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001058
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001059 public void setCoreSettings(Bundle coreSettings) {
1060 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001061 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001062
1063 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1064 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1065 ucd.pkg = pkg;
1066 ucd.info = info;
1067 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1068 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001069
1070 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001071 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001072 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 }
1075
Romain Guy65b345f2011-07-27 18:51:50 -07001076 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 public static final int LAUNCH_ACTIVITY = 100;
1078 public static final int PAUSE_ACTIVITY = 101;
1079 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1080 public static final int STOP_ACTIVITY_SHOW = 103;
1081 public static final int STOP_ACTIVITY_HIDE = 104;
1082 public static final int SHOW_WINDOW = 105;
1083 public static final int HIDE_WINDOW = 106;
1084 public static final int RESUME_ACTIVITY = 107;
1085 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001086 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 public static final int BIND_APPLICATION = 110;
1088 public static final int EXIT_APPLICATION = 111;
1089 public static final int NEW_INTENT = 112;
1090 public static final int RECEIVER = 113;
1091 public static final int CREATE_SERVICE = 114;
1092 public static final int SERVICE_ARGS = 115;
1093 public static final int STOP_SERVICE = 116;
1094 public static final int REQUEST_THUMBNAIL = 117;
1095 public static final int CONFIGURATION_CHANGED = 118;
1096 public static final int CLEAN_UP_CONTEXT = 119;
1097 public static final int GC_WHEN_IDLE = 120;
1098 public static final int BIND_SERVICE = 121;
1099 public static final int UNBIND_SERVICE = 122;
1100 public static final int DUMP_SERVICE = 123;
1101 public static final int LOW_MEMORY = 124;
1102 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1103 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001104 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001105 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001106 public static final int DESTROY_BACKUP_AGENT = 129;
1107 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001108 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001109 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001110 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001111 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001112 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001113 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001114 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001115 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001116 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001117 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001118 public static final int DUMP_PROVIDER = 141;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001120 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 switch (code) {
1122 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1123 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1124 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1125 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1126 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1127 case SHOW_WINDOW: return "SHOW_WINDOW";
1128 case HIDE_WINDOW: return "HIDE_WINDOW";
1129 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1130 case SEND_RESULT: return "SEND_RESULT";
1131 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1132 case BIND_APPLICATION: return "BIND_APPLICATION";
1133 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1134 case NEW_INTENT: return "NEW_INTENT";
1135 case RECEIVER: return "RECEIVER";
1136 case CREATE_SERVICE: return "CREATE_SERVICE";
1137 case SERVICE_ARGS: return "SERVICE_ARGS";
1138 case STOP_SERVICE: return "STOP_SERVICE";
1139 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1140 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1141 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1142 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1143 case BIND_SERVICE: return "BIND_SERVICE";
1144 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1145 case DUMP_SERVICE: return "DUMP_SERVICE";
1146 case LOW_MEMORY: return "LOW_MEMORY";
1147 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1148 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001149 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001150 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1151 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001152 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001153 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001154 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001155 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001156 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001157 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001158 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001159 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001160 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001161 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001162 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001163 case DUMP_PROVIDER: return "DUMP_PROVIDER";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 }
1165 }
1166 return "(unknown)";
1167 }
1168 public void handleMessage(Message msg) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001169 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 switch (msg.what) {
1171 case LAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001172 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173
1174 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001175 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001176 handleLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 } break;
1178 case RELAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001179 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001180 handleRelaunchActivity(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 } break;
1182 case PAUSE_ACTIVITY:
1183 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001184 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 break;
1186 case PAUSE_ACTIVITY_FINISHING:
1187 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
1188 break;
1189 case STOP_ACTIVITY_SHOW:
1190 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
1191 break;
1192 case STOP_ACTIVITY_HIDE:
1193 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
1194 break;
1195 case SHOW_WINDOW:
1196 handleWindowVisibility((IBinder)msg.obj, true);
1197 break;
1198 case HIDE_WINDOW:
1199 handleWindowVisibility((IBinder)msg.obj, false);
1200 break;
1201 case RESUME_ACTIVITY:
1202 handleResumeActivity((IBinder)msg.obj, true,
1203 msg.arg1 != 0);
1204 break;
1205 case SEND_RESULT:
1206 handleSendResult((ResultData)msg.obj);
1207 break;
1208 case DESTROY_ACTIVITY:
1209 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1210 msg.arg2, false);
1211 break;
1212 case BIND_APPLICATION:
1213 AppBindData data = (AppBindData)msg.obj;
1214 handleBindApplication(data);
1215 break;
1216 case EXIT_APPLICATION:
1217 if (mInitialApplication != null) {
1218 mInitialApplication.onTerminate();
1219 }
1220 Looper.myLooper().quit();
1221 break;
1222 case NEW_INTENT:
1223 handleNewIntent((NewIntentData)msg.obj);
1224 break;
1225 case RECEIVER:
1226 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001227 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 break;
1229 case CREATE_SERVICE:
1230 handleCreateService((CreateServiceData)msg.obj);
1231 break;
1232 case BIND_SERVICE:
1233 handleBindService((BindServiceData)msg.obj);
1234 break;
1235 case UNBIND_SERVICE:
1236 handleUnbindService((BindServiceData)msg.obj);
1237 break;
1238 case SERVICE_ARGS:
1239 handleServiceArgs((ServiceArgsData)msg.obj);
1240 break;
1241 case STOP_SERVICE:
1242 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001243 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 break;
1245 case REQUEST_THUMBNAIL:
1246 handleRequestThumbnail((IBinder)msg.obj);
1247 break;
1248 case CONFIGURATION_CHANGED:
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001249 handleConfigurationChanged((Configuration)msg.obj, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 break;
1251 case CLEAN_UP_CONTEXT:
1252 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1253 cci.context.performFinalCleanup(cci.who, cci.what);
1254 break;
1255 case GC_WHEN_IDLE:
1256 scheduleGcIdler();
1257 break;
1258 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001259 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 break;
1261 case LOW_MEMORY:
1262 handleLowMemory();
1263 break;
1264 case ACTIVITY_CONFIGURATION_CHANGED:
1265 handleActivityConfigurationChanged((IBinder)msg.obj);
1266 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001267 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001268 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001269 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001270 case CREATE_BACKUP_AGENT:
1271 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
1272 break;
1273 case DESTROY_BACKUP_AGENT:
1274 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
1275 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001276 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001277 Process.killProcess(Process.myPid());
1278 break;
1279 case REMOVE_PROVIDER:
1280 completeRemoveProvider((IContentProvider)msg.obj);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001281 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001282 case ENABLE_JIT:
1283 ensureJitEnabled();
1284 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001285 case DISPATCH_PACKAGE_BROADCAST:
1286 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
1287 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001288 case SCHEDULE_CRASH:
1289 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001290 case DUMP_HEAP:
1291 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1292 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001293 case DUMP_ACTIVITY:
1294 handleDumpActivity((DumpComponentInfo)msg.obj);
1295 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001296 case DUMP_PROVIDER:
1297 handleDumpProvider((DumpComponentInfo)msg.obj);
1298 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001299 case SLEEPING:
1300 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
1301 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001302 case SET_CORE_SETTINGS:
1303 handleSetCoreSettings((Bundle) msg.obj);
1304 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001305 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1306 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001307 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001308 case TRIM_MEMORY:
1309 handleTrimMemory(msg.arg1);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001310 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07001312 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 }
Bob Leee5408332009-09-04 18:31:17 -07001314
Brian Carlstromed7e0072011-03-24 13:27:57 -07001315 private void maybeSnapshot() {
1316 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001317 // convert the *private* ActivityThread.PackageInfo to *public* known
1318 // android.content.pm.PackageInfo
1319 String packageName = mBoundApplication.info.mPackageName;
1320 android.content.pm.PackageInfo packageInfo = null;
1321 try {
1322 Context context = getSystemContext();
1323 if(context == null) {
1324 Log.e(TAG, "cannot get a valid context");
1325 return;
1326 }
1327 PackageManager pm = context.getPackageManager();
1328 if(pm == null) {
1329 Log.e(TAG, "cannot get a valid PackageManager");
1330 return;
1331 }
1332 packageInfo = pm.getPackageInfo(
1333 packageName, PackageManager.GET_ACTIVITIES);
1334 } catch (NameNotFoundException e) {
1335 Log.e(TAG, "cannot get package info for " + packageName, e);
1336 }
1337 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001338 }
1339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 }
1341
Romain Guy65b345f2011-07-27 18:51:50 -07001342 private class Idler implements MessageQueue.IdleHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001344 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001345 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001346 if (mBoundApplication != null && mProfiler.profileFd != null
1347 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001348 stopProfiling = true;
1349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 if (a != null) {
1351 mNewActivities = null;
1352 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001353 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001355 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 TAG, "Reporting idle of " + a +
1357 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001358 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 if (a.activity != null && !a.activity.mFinished) {
1360 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001361 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001362 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001364 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 }
1366 }
1367 prev = a;
1368 a = a.nextIdle;
1369 prev.nextIdle = null;
1370 } while (a != null);
1371 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001372 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001373 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001374 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001375 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 return false;
1377 }
1378 }
1379
1380 final class GcIdler implements MessageQueue.IdleHandler {
1381 public final boolean queueIdle() {
1382 doGcIfNeeded();
1383 return false;
1384 }
1385 }
1386
Romain Guy65b345f2011-07-27 18:51:50 -07001387 private static class ResourcesKey {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001388 final private String mResDir;
1389 final private float mScale;
1390 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001391
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001392 ResourcesKey(String resDir, float scale) {
1393 mResDir = resDir;
1394 mScale = scale;
1395 mHash = mResDir.hashCode() << 2 + (int) (mScale * 2);
1396 }
Bob Leee5408332009-09-04 18:31:17 -07001397
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001398 @Override
1399 public int hashCode() {
1400 return mHash;
1401 }
1402
1403 @Override
1404 public boolean equals(Object obj) {
1405 if (!(obj instanceof ResourcesKey)) {
1406 return false;
1407 }
1408 ResourcesKey peer = (ResourcesKey) obj;
1409 return mResDir.equals(peer.mResDir) && mScale == peer.mScale;
1410 }
1411 }
1412
Romain Guy65b345f2011-07-27 18:51:50 -07001413 public static ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001414 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001415 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416
Romain Guy65b345f2011-07-27 18:51:50 -07001417 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001418 ActivityThread am = currentActivityThread();
1419 return (am != null && am.mBoundApplication != null)
1420 ? am.mBoundApplication.processName : null;
1421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422
Romain Guy65b345f2011-07-27 18:51:50 -07001423 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001424 ActivityThread am = currentActivityThread();
1425 return am != null ? am.mInitialApplication : null;
1426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001428 public static IPackageManager getPackageManager() {
1429 if (sPackageManager != null) {
1430 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1431 return sPackageManager;
1432 }
1433 IBinder b = ServiceManager.getService("package");
1434 //Slog.v("PackageManager", "default service binder = " + b);
1435 sPackageManager = IPackageManager.Stub.asInterface(b);
1436 //Slog.v("PackageManager", "default service = " + sPackageManager);
1437 return sPackageManager;
1438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001440 DisplayMetrics getDisplayMetricsLocked(CompatibilityInfo ci, boolean forceUpdate) {
1441 DisplayMetrics dm = mDisplayMetrics.get(ci);
1442 if (dm != null && !forceUpdate) {
1443 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001444 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001445 if (dm == null) {
1446 dm = new DisplayMetrics();
1447 mDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001448 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001449 Display d = WindowManagerImpl.getDefault(ci).getDefaultDisplay();
1450 d.getMetrics(dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001451 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1452 // + metrics.heightPixels + " den=" + metrics.density
1453 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001454 return dm;
1455 }
1456
1457 static Configuration applyConfigCompat(Configuration config, CompatibilityInfo compat) {
1458 if (config == null) {
1459 return null;
1460 }
1461 if (compat != null && !compat.supportsScreen()) {
1462 config = new Configuration(config);
1463 compat.applyToConfiguration(config);
1464 }
1465 return config;
1466 }
1467
Romain Guy65b345f2011-07-27 18:51:50 -07001468 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001469 Configuration applyConfigCompatMainThread(Configuration config, CompatibilityInfo compat) {
1470 if (config == null) {
1471 return null;
1472 }
1473 if (compat != null && !compat.supportsScreen()) {
1474 mMainThreadConfig.setTo(config);
1475 config = mMainThreadConfig;
1476 compat.applyToConfiguration(config);
1477 }
1478 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001481 /**
1482 * Creates the top level Resources for applications with the given compatibility info.
1483 *
1484 * @param resDir the resource directory.
1485 * @param compInfo the compability info. It will use the default compatibility info when it's
1486 * null.
1487 */
1488 Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) {
1489 ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale);
1490 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001492 // Resources is app scale dependent.
1493 if (false) {
1494 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1495 + compInfo.applicationScale);
1496 }
1497 WeakReference<Resources> wr = mActiveResources.get(key);
1498 r = wr != null ? wr.get() : null;
1499 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1500 if (r != null && r.getAssets().isUpToDate()) {
1501 if (false) {
1502 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1503 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1504 }
1505 return r;
1506 }
1507 }
1508
1509 //if (r != null) {
1510 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1511 // + r + " " + resDir);
1512 //}
1513
1514 AssetManager assets = new AssetManager();
1515 if (assets.addAssetPath(resDir) == 0) {
1516 return null;
1517 }
1518
1519 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Dianne Hackborn836e2622011-10-04 18:32:39 -07001520 DisplayMetrics metrics = getDisplayMetricsLocked(null, false);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001521 r = new Resources(assets, metrics, getConfiguration(), compInfo);
1522 if (false) {
1523 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1524 + r.getConfiguration() + " appScale="
1525 + r.getCompatibilityInfo().applicationScale);
1526 }
1527
1528 synchronized (mPackages) {
1529 WeakReference<Resources> wr = mActiveResources.get(key);
1530 Resources existing = wr != null ? wr.get() : null;
1531 if (existing != null && existing.getAssets().isUpToDate()) {
1532 // Someone else already created the resources while we were
1533 // unlocked; go ahead and use theirs.
1534 r.getAssets().close();
1535 return existing;
1536 }
1537
1538 // XXX need to remove entries when weak references go away
1539 mActiveResources.put(key, new WeakReference<Resources>(r));
1540 return r;
1541 }
1542 }
1543
1544 /**
1545 * Creates the top level resources for the given package.
1546 */
1547 Resources getTopLevelResources(String resDir, LoadedApk pkgInfo) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001548 return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001549 }
1550
1551 final Handler getHandler() {
1552 return mH;
1553 }
1554
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001555 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1556 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001557 synchronized (mPackages) {
1558 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1560 ref = mPackages.get(packageName);
1561 } else {
1562 ref = mResourcePackages.get(packageName);
1563 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001564 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001565 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001566 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1567 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 if (packageInfo != null && (packageInfo.mResources == null
1569 || packageInfo.mResources.getAssets().isUpToDate())) {
1570 if (packageInfo.isSecurityViolation()
1571 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1572 throw new SecurityException(
1573 "Requesting code from " + packageName
1574 + " to be run in process "
1575 + mBoundApplication.processName
1576 + "/" + mBoundApplication.appInfo.uid);
1577 }
1578 return packageInfo;
1579 }
1580 }
1581
1582 ApplicationInfo ai = null;
1583 try {
1584 ai = getPackageManager().getApplicationInfo(packageName,
1585 PackageManager.GET_SHARED_LIBRARY_FILES);
1586 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001587 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 }
1589
1590 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001591 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 }
1593
1594 return null;
1595 }
1596
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001597 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1598 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1600 boolean securityViolation = includeCode && ai.uid != 0
1601 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Amith Yamasani742a6712011-05-04 14:49:28 -07001602 ? !UserId.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
1603 : true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1605 |Context.CONTEXT_IGNORE_SECURITY))
1606 == Context.CONTEXT_INCLUDE_CODE) {
1607 if (securityViolation) {
1608 String msg = "Requesting code from " + ai.packageName
1609 + " (with uid " + ai.uid + ")";
1610 if (mBoundApplication != null) {
1611 msg = msg + " to be run in process "
1612 + mBoundApplication.processName + " (with uid "
1613 + mBoundApplication.appInfo.uid + ")";
1614 }
1615 throw new SecurityException(msg);
1616 }
1617 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001618 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 }
1620
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001621 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1622 CompatibilityInfo compatInfo) {
1623 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
1625
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001626 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1627 synchronized (mPackages) {
1628 WeakReference<LoadedApk> ref;
1629 if (includeCode) {
1630 ref = mPackages.get(packageName);
1631 } else {
1632 ref = mResourcePackages.get(packageName);
1633 }
1634 return ref != null ? ref.get() : null;
1635 }
1636 }
1637
Romain Guy65b345f2011-07-27 18:51:50 -07001638 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1640 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001641 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 if (includeCode) {
1643 ref = mPackages.get(aInfo.packageName);
1644 } else {
1645 ref = mResourcePackages.get(aInfo.packageName);
1646 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001647 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 if (packageInfo == null || (packageInfo.mResources != null
1649 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001650 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 : "Loading resource-only package ") + aInfo.packageName
1652 + " (in " + (mBoundApplication != null
1653 ? mBoundApplication.processName : null)
1654 + ")");
1655 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001656 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 securityViolation, includeCode &&
1658 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1659 if (includeCode) {
1660 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001661 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 } else {
1663 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001664 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 }
1666 }
1667 return packageInfo;
1668 }
1669 }
1670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 ActivityThread() {
1672 }
1673
1674 public ApplicationThread getApplicationThread()
1675 {
1676 return mAppThread;
1677 }
1678
1679 public Instrumentation getInstrumentation()
1680 {
1681 return mInstrumentation;
1682 }
1683
1684 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001685 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 }
1687
1688 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001689 return mProfiler != null && mProfiler.profileFile != null
1690 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 }
1692
1693 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001694 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 }
1696
1697 public Looper getLooper() {
1698 return mLooper;
1699 }
1700
1701 public Application getApplication() {
1702 return mInitialApplication;
1703 }
Bob Leee5408332009-09-04 18:31:17 -07001704
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001705 public String getProcessName() {
1706 return mBoundApplication.processName;
1707 }
Bob Leee5408332009-09-04 18:31:17 -07001708
Dianne Hackborn21556372010-02-04 16:34:40 -08001709 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 synchronized (this) {
1711 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001712 ContextImpl context =
1713 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001714 LoadedApk info = new LoadedApk(this, "android", context, null,
1715 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 context.init(info, null, this);
1717 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001718 getConfiguration(), getDisplayMetricsLocked(
1719 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001721 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 // + ": " + context.getResources().getConfiguration());
1723 }
1724 }
1725 return mSystemContext;
1726 }
1727
Mike Cleron432b7132009-09-24 15:28:29 -07001728 public void installSystemApplicationInfo(ApplicationInfo info) {
1729 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001730 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001731 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001732 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001733
1734 // give ourselves a default profiler
1735 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001736 }
1737 }
1738
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001739 void ensureJitEnabled() {
1740 if (!mJitEnabled) {
1741 mJitEnabled = true;
1742 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1743 }
1744 }
1745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 void scheduleGcIdler() {
1747 if (!mGcIdlerScheduled) {
1748 mGcIdlerScheduled = true;
1749 Looper.myQueue().addIdleHandler(mGcIdler);
1750 }
1751 mH.removeMessages(H.GC_WHEN_IDLE);
1752 }
1753
1754 void unscheduleGcIdler() {
1755 if (mGcIdlerScheduled) {
1756 mGcIdlerScheduled = false;
1757 Looper.myQueue().removeIdleHandler(mGcIdler);
1758 }
1759 mH.removeMessages(H.GC_WHEN_IDLE);
1760 }
1761
1762 void doGcIfNeeded() {
1763 mGcIdlerScheduled = false;
1764 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001765 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 // + "m now=" + now);
1767 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001768 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 BinderInternal.forceGc("bg");
1770 }
1771 }
1772
Jeff Hamilton52d32032011-01-08 15:31:26 -06001773 public void registerOnActivityPausedListener(Activity activity,
1774 OnActivityPausedListener listener) {
1775 synchronized (mOnPauseListeners) {
1776 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1777 if (list == null) {
1778 list = new ArrayList<OnActivityPausedListener>();
1779 mOnPauseListeners.put(activity, list);
1780 }
1781 list.add(listener);
1782 }
1783 }
1784
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001785 public void unregisterOnActivityPausedListener(Activity activity,
1786 OnActivityPausedListener listener) {
1787 synchronized (mOnPauseListeners) {
1788 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1789 if (list != null) {
1790 list.remove(listener);
1791 }
1792 }
1793 }
1794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 public final ActivityInfo resolveActivityInfo(Intent intent) {
1796 ActivityInfo aInfo = intent.resolveActivityInfo(
1797 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1798 if (aInfo == null) {
1799 // Throw an exception.
1800 Instrumentation.checkStartActivityResult(
1801 IActivityManager.START_CLASS_NOT_FOUND, intent);
1802 }
1803 return aInfo;
1804 }
Bob Leee5408332009-09-04 18:31:17 -07001805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001808 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001809 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001811 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 r.intent = intent;
1813 r.state = state;
1814 r.parent = parent;
1815 r.embeddedID = id;
1816 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001817 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 if (localLOGV) {
1819 ComponentName compname = intent.getComponent();
1820 String name;
1821 if (compname != null) {
1822 name = compname.toShortString();
1823 } else {
1824 name = "(Intent " + intent + ").getComponent() returned null";
1825 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001826 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 + ", comp=" + name
1828 + ", token=" + token);
1829 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001830 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 }
1832
1833 public final Activity getActivity(IBinder token) {
1834 return mActivities.get(token).activity;
1835 }
1836
1837 public final void sendActivityResult(
1838 IBinder token, String id, int requestCode,
1839 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001840 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001841 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1843 list.add(new ResultInfo(id, requestCode, resultCode, data));
1844 mAppThread.scheduleSendResult(token, list);
1845 }
1846
1847 // if the thread hasn't started yet, we don't have the handler, so just
1848 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07001849 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 queueOrSendMessage(what, obj, 0, 0);
1851 }
1852
Romain Guy65b345f2011-07-27 18:51:50 -07001853 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 queueOrSendMessage(what, obj, arg1, 0);
1855 }
1856
Romain Guy65b345f2011-07-27 18:51:50 -07001857 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001859 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1861 + ": " + arg1 + " / " + obj);
1862 Message msg = Message.obtain();
1863 msg.what = what;
1864 msg.obj = obj;
1865 msg.arg1 = arg1;
1866 msg.arg2 = arg2;
1867 mH.sendMessage(msg);
1868 }
1869 }
1870
Dianne Hackborn21556372010-02-04 16:34:40 -08001871 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 String what) {
1873 ContextCleanupInfo cci = new ContextCleanupInfo();
1874 cci.context = context;
1875 cci.who = who;
1876 cci.what = what;
1877 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1878 }
1879
Romain Guy65b345f2011-07-27 18:51:50 -07001880 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1882
1883 ActivityInfo aInfo = r.activityInfo;
1884 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001885 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 Context.CONTEXT_INCLUDE_CODE);
1887 }
Bob Leee5408332009-09-04 18:31:17 -07001888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 ComponentName component = r.intent.getComponent();
1890 if (component == null) {
1891 component = r.intent.resolveActivity(
1892 mInitialApplication.getPackageManager());
1893 r.intent.setComponent(component);
1894 }
1895
1896 if (r.activityInfo.targetActivity != null) {
1897 component = new ComponentName(r.activityInfo.packageName,
1898 r.activityInfo.targetActivity);
1899 }
1900
1901 Activity activity = null;
1902 try {
1903 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
1904 activity = mInstrumentation.newActivity(
1905 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08001906 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 r.intent.setExtrasClassLoader(cl);
1908 if (r.state != null) {
1909 r.state.setClassLoader(cl);
1910 }
1911 } catch (Exception e) {
1912 if (!mInstrumentation.onException(activity, e)) {
1913 throw new RuntimeException(
1914 "Unable to instantiate activity " + component
1915 + ": " + e.toString(), e);
1916 }
1917 }
1918
1919 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001920 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001921
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001922 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
1923 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 TAG, r + ": app=" + app
1925 + ", appName=" + app.getPackageName()
1926 + ", pkg=" + r.packageInfo.getPackageName()
1927 + ", comp=" + r.intent.getComponent().toShortString()
1928 + ", dir=" + r.packageInfo.getAppDir());
1929
1930 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001931 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 appContext.init(r.packageInfo, r.token, this);
1933 appContext.setOuterContext(activity);
1934 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001935 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001936 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07001937 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001938 activity.attach(appContext, this, getInstrumentation(), r.token,
1939 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001940 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07001941
Christopher Tateb70f3df2009-04-07 16:07:59 -07001942 if (customIntent != null) {
1943 activity.mIntent = customIntent;
1944 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001945 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 activity.mStartedActivity = false;
1947 int theme = r.activityInfo.getThemeResource();
1948 if (theme != 0) {
1949 activity.setTheme(theme);
1950 }
1951
1952 activity.mCalled = false;
1953 mInstrumentation.callActivityOnCreate(activity, r.state);
1954 if (!activity.mCalled) {
1955 throw new SuperNotCalledException(
1956 "Activity " + r.intent.getComponent().toShortString() +
1957 " did not call through to super.onCreate()");
1958 }
1959 r.activity = activity;
1960 r.stopped = true;
1961 if (!r.activity.mFinished) {
1962 activity.performStart();
1963 r.stopped = false;
1964 }
1965 if (!r.activity.mFinished) {
1966 if (r.state != null) {
1967 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
1968 }
1969 }
1970 if (!r.activity.mFinished) {
1971 activity.mCalled = false;
1972 mInstrumentation.callActivityOnPostCreate(activity, r.state);
1973 if (!activity.mCalled) {
1974 throw new SuperNotCalledException(
1975 "Activity " + r.intent.getComponent().toShortString() +
1976 " did not call through to super.onPostCreate()");
1977 }
1978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 }
1980 r.paused = true;
1981
1982 mActivities.put(r.token, r);
1983
1984 } catch (SuperNotCalledException e) {
1985 throw e;
1986
1987 } catch (Exception e) {
1988 if (!mInstrumentation.onException(activity, e)) {
1989 throw new RuntimeException(
1990 "Unable to start activity " + component
1991 + ": " + e.toString(), e);
1992 }
1993 }
1994
1995 return activity;
1996 }
1997
Romain Guy65b345f2011-07-27 18:51:50 -07001998 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 // If we are getting ready to gc after going to the background, well
2000 // we are back active so skip it.
2001 unscheduleGcIdler();
2002
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002003 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002004 mProfiler.setProfiler(r.profileFile, r.profileFd);
2005 mProfiler.startProfiling();
2006 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002007 }
2008
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002009 // Make sure we are running with the most recent config.
2010 handleConfigurationChanged(null, null);
2011
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002012 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07002014 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015
2016 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002017 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002018 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 handleResumeActivity(r.token, false, r.isForward);
2020
2021 if (!r.activity.mFinished && r.startsNotResumed) {
2022 // The activity manager actually wants this one to start out
2023 // paused, because it needs to be visible but isn't in the
2024 // foreground. We accomplish this by going through the
2025 // normal startup (because activities expect to go through
2026 // onResume() the first time they run, before their window
2027 // is displayed), and then pausing it. However, in this case
2028 // we do -not- need to do the full pause cycle (of freezing
2029 // and such) because the activity manager assumes it can just
2030 // retain the current state it has.
2031 try {
2032 r.activity.mCalled = false;
2033 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002034 // We need to keep around the original state, in case
2035 // we need to be created again.
2036 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 if (!r.activity.mCalled) {
2038 throw new SuperNotCalledException(
2039 "Activity " + r.intent.getComponent().toShortString() +
2040 " did not call through to super.onPause()");
2041 }
2042
2043 } catch (SuperNotCalledException e) {
2044 throw e;
2045
2046 } catch (Exception e) {
2047 if (!mInstrumentation.onException(r.activity, e)) {
2048 throw new RuntimeException(
2049 "Unable to pause activity "
2050 + r.intent.getComponent().toShortString()
2051 + ": " + e.toString(), e);
2052 }
2053 }
2054 r.paused = true;
2055 }
2056 } else {
2057 // If there was an error, for any reason, tell the activity
2058 // manager to stop us.
2059 try {
2060 ActivityManagerNative.getDefault()
2061 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2062 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002063 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 }
2065 }
2066 }
2067
Romain Guy65b345f2011-07-27 18:51:50 -07002068 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 List<Intent> intents) {
2070 final int N = intents.size();
2071 for (int i=0; i<N; i++) {
2072 Intent intent = intents.get(i);
2073 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002074 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2076 }
2077 }
2078
2079 public final void performNewIntents(IBinder token,
2080 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002081 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 if (r != null) {
2083 final boolean resumed = !r.paused;
2084 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002085 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 mInstrumentation.callActivityOnPause(r.activity);
2087 }
2088 deliverNewIntents(r, intents);
2089 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002090 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002091 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 }
2093 }
2094 }
Bob Leee5408332009-09-04 18:31:17 -07002095
Romain Guy65b345f2011-07-27 18:51:50 -07002096 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 performNewIntents(data.token, data.intents);
2098 }
2099
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002100 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2101
2102 /**
2103 * Return the Intent that's currently being handled by a
2104 * BroadcastReceiver on this thread, or null if none.
2105 * @hide
2106 */
2107 public static Intent getIntentBeingBroadcast() {
2108 return sCurrentBroadcastIntent.get();
2109 }
2110
Romain Guy65b345f2011-07-27 18:51:50 -07002111 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 // If we are getting ready to gc after going to the background, well
2113 // we are back active so skip it.
2114 unscheduleGcIdler();
2115
2116 String component = data.intent.getComponent().getClassName();
2117
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002118 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002119 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120
2121 IActivityManager mgr = ActivityManagerNative.getDefault();
2122
Romain Guy65b345f2011-07-27 18:51:50 -07002123 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 try {
2125 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2126 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002127 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2129 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002130 if (DEBUG_BROADCAST) Slog.i(TAG,
2131 "Finishing failed broadcast to " + data.intent.getComponent());
2132 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 throw new RuntimeException(
2134 "Unable to instantiate receiver " + component
2135 + ": " + e.toString(), e);
2136 }
2137
2138 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002139 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002140
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002141 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 TAG, "Performing receive of " + data.intent
2143 + ": app=" + app
2144 + ", appName=" + app.getPackageName()
2145 + ", pkg=" + packageInfo.getPackageName()
2146 + ", comp=" + data.intent.getComponent().toShortString()
2147 + ", dir=" + packageInfo.getAppDir());
2148
Dianne Hackborn21556372010-02-04 16:34:40 -08002149 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002150 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002151 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 receiver.onReceive(context.getReceiverRestrictedContext(),
2153 data.intent);
2154 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002155 if (DEBUG_BROADCAST) Slog.i(TAG,
2156 "Finishing failed broadcast to " + data.intent.getComponent());
2157 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 if (!mInstrumentation.onException(receiver, e)) {
2159 throw new RuntimeException(
2160 "Unable to start receiver " + component
2161 + ": " + e.toString(), e);
2162 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002163 } finally {
2164 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 }
2166
Dianne Hackborne829fef2010-10-26 17:44:01 -07002167 if (receiver.getPendingResult() != null) {
2168 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 }
2170 }
2171
Christopher Tate181fafa2009-05-14 11:12:14 -07002172 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002173 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002174 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002175
2176 // no longer idle; we have backup work to do
2177 unscheduleGcIdler();
2178
2179 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002180 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002181 String packageName = packageInfo.mPackageName;
2182 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002183 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002184 + " already exists");
2185 return;
2186 }
Bob Leee5408332009-09-04 18:31:17 -07002187
Christopher Tate181fafa2009-05-14 11:12:14 -07002188 BackupAgent agent = null;
2189 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002190
Christopher Tate79ec80d2011-06-24 14:58:49 -07002191 // full backup operation but no app-supplied agent? use the default implementation
2192 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2193 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002194 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002195 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002196
Christopher Tate181fafa2009-05-14 11:12:14 -07002197 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002198 IBinder binder = null;
2199 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002200 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2201
Christopher Tated1475e02009-07-09 15:36:17 -07002202 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002203 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002204
2205 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002206 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002207 context.init(packageInfo, null, this);
2208 context.setOuterContext(agent);
2209 agent.attach(context);
2210
2211 agent.onCreate();
2212 binder = agent.onBind();
2213 mBackupAgents.put(packageName, agent);
2214 } catch (Exception e) {
2215 // If this is during restore, fail silently; otherwise go
2216 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002217 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002218 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2219 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002220 throw e;
2221 }
2222 // falling through with 'binder' still null
2223 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002224
2225 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002226 try {
2227 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2228 } catch (RemoteException e) {
2229 // nothing to do.
2230 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002231 } catch (Exception e) {
2232 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002233 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002234 }
2235 }
2236
2237 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002238 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002239 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002240
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002241 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002242 String packageName = packageInfo.mPackageName;
2243 BackupAgent agent = mBackupAgents.get(packageName);
2244 if (agent != null) {
2245 try {
2246 agent.onDestroy();
2247 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002248 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002249 e.printStackTrace();
2250 }
2251 mBackupAgents.remove(packageName);
2252 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002253 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002254 }
2255 }
2256
Romain Guy65b345f2011-07-27 18:51:50 -07002257 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 // If we are getting ready to gc after going to the background, well
2259 // we are back active so skip it.
2260 unscheduleGcIdler();
2261
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002262 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002263 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 Service service = null;
2265 try {
2266 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2267 service = (Service) cl.loadClass(data.info.name).newInstance();
2268 } catch (Exception e) {
2269 if (!mInstrumentation.onException(service, e)) {
2270 throw new RuntimeException(
2271 "Unable to instantiate service " + data.info.name
2272 + ": " + e.toString(), e);
2273 }
2274 }
2275
2276 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002277 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278
Dianne Hackborn21556372010-02-04 16:34:40 -08002279 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 context.init(packageInfo, null, this);
2281
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002282 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 context.setOuterContext(service);
2284 service.attach(context, this, data.info.name, data.token, app,
2285 ActivityManagerNative.getDefault());
2286 service.onCreate();
2287 mServices.put(data.token, service);
2288 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002289 ActivityManagerNative.getDefault().serviceDoneExecuting(
2290 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 } catch (RemoteException e) {
2292 // nothing to do.
2293 }
2294 } catch (Exception e) {
2295 if (!mInstrumentation.onException(service, e)) {
2296 throw new RuntimeException(
2297 "Unable to create service " + data.info.name
2298 + ": " + e.toString(), e);
2299 }
2300 }
2301 }
2302
Romain Guy65b345f2011-07-27 18:51:50 -07002303 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002305 if (DEBUG_SERVICE)
2306 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 if (s != null) {
2308 try {
2309 data.intent.setExtrasClassLoader(s.getClassLoader());
2310 try {
2311 if (!data.rebind) {
2312 IBinder binder = s.onBind(data.intent);
2313 ActivityManagerNative.getDefault().publishService(
2314 data.token, data.intent, binder);
2315 } else {
2316 s.onRebind(data.intent);
2317 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002318 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002320 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 } catch (RemoteException ex) {
2322 }
2323 } catch (Exception e) {
2324 if (!mInstrumentation.onException(s, e)) {
2325 throw new RuntimeException(
2326 "Unable to bind to service " + s
2327 + " with " + data.intent + ": " + e.toString(), e);
2328 }
2329 }
2330 }
2331 }
2332
Romain Guy65b345f2011-07-27 18:51:50 -07002333 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 Service s = mServices.get(data.token);
2335 if (s != null) {
2336 try {
2337 data.intent.setExtrasClassLoader(s.getClassLoader());
2338 boolean doRebind = s.onUnbind(data.intent);
2339 try {
2340 if (doRebind) {
2341 ActivityManagerNative.getDefault().unbindFinished(
2342 data.token, data.intent, doRebind);
2343 } else {
2344 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002345 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 }
2347 } catch (RemoteException ex) {
2348 }
2349 } catch (Exception e) {
2350 if (!mInstrumentation.onException(s, e)) {
2351 throw new RuntimeException(
2352 "Unable to unbind to service " + s
2353 + " with " + data.intent + ": " + e.toString(), e);
2354 }
2355 }
2356 }
2357 }
2358
Dianne Hackborn625ac272010-09-17 18:29:22 -07002359 private void handleDumpService(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002360 Service s = mServices.get(info.token);
2361 if (s != null) {
2362 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2363 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2364 pw.flush();
2365 try {
2366 info.fd.close();
2367 } catch (IOException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 }
2369 }
2370 }
2371
Dianne Hackborn625ac272010-09-17 18:29:22 -07002372 private void handleDumpActivity(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002373 ActivityClientRecord r = mActivities.get(info.token);
2374 if (r != null && r.activity != null) {
2375 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2376 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2377 pw.flush();
2378 try {
2379 info.fd.close();
2380 } catch (IOException e) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07002381 }
2382 }
2383 }
2384
Marco Nelissen18cb2872011-11-15 11:19:53 -08002385 private void handleDumpProvider(DumpComponentInfo info) {
2386 ProviderClientRecord r = mLocalProviders.get(info.token);
2387 if (r != null && r.mLocalProvider != null) {
2388 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2389 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2390 pw.flush();
2391 try {
2392 info.fd.close();
2393 } catch (IOException e) {
2394 }
2395 }
2396 }
2397
Romain Guy65b345f2011-07-27 18:51:50 -07002398 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 Service s = mServices.get(data.token);
2400 if (s != null) {
2401 try {
2402 if (data.args != null) {
2403 data.args.setExtrasClassLoader(s.getClassLoader());
2404 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002405 int res;
2406 if (!data.taskRemoved) {
2407 res = s.onStartCommand(data.args, data.flags, data.startId);
2408 } else {
2409 s.onTaskRemoved(data.args);
2410 res = Service.START_TASK_REMOVED_COMPLETE;
2411 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002412
2413 QueuedWork.waitToFinish();
2414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002416 ActivityManagerNative.getDefault().serviceDoneExecuting(
2417 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 } catch (RemoteException e) {
2419 // nothing to do.
2420 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002421 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 } catch (Exception e) {
2423 if (!mInstrumentation.onException(s, e)) {
2424 throw new RuntimeException(
2425 "Unable to start service " + s
2426 + " with " + data.args + ": " + e.toString(), e);
2427 }
2428 }
2429 }
2430 }
2431
Romain Guy65b345f2011-07-27 18:51:50 -07002432 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 Service s = mServices.remove(token);
2434 if (s != null) {
2435 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002436 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 s.onDestroy();
2438 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002439 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002441 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002443
2444 QueuedWork.waitToFinish();
2445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002447 ActivityManagerNative.getDefault().serviceDoneExecuting(
2448 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 } catch (RemoteException e) {
2450 // nothing to do.
2451 }
2452 } catch (Exception e) {
2453 if (!mInstrumentation.onException(s, e)) {
2454 throw new RuntimeException(
2455 "Unable to stop service " + s
2456 + ": " + e.toString(), e);
2457 }
2458 }
2459 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002460 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 }
2462
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002463 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002465 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002466 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 + " finished=" + r.activity.mFinished);
2468 if (r != null && !r.activity.mFinished) {
2469 if (clearHide) {
2470 r.hideForNow = false;
2471 r.activity.mStartedActivity = false;
2472 }
2473 try {
2474 if (r.pendingIntents != null) {
2475 deliverNewIntents(r, r.pendingIntents);
2476 r.pendingIntents = null;
2477 }
2478 if (r.pendingResults != null) {
2479 deliverResults(r, r.pendingResults);
2480 r.pendingResults = null;
2481 }
2482 r.activity.performResume();
2483
Bob Leee5408332009-09-04 18:31:17 -07002484 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 r.paused = false;
2488 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 r.state = null;
2490 } catch (Exception e) {
2491 if (!mInstrumentation.onException(r.activity, e)) {
2492 throw new RuntimeException(
2493 "Unable to resume activity "
2494 + r.intent.getComponent().toShortString()
2495 + ": " + e.toString(), e);
2496 }
2497 }
2498 }
2499 return r;
2500 }
2501
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002502 final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
2503 if (r.mPendingRemoveWindow != null) {
2504 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2505 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2506 if (wtoken != null) {
2507 WindowManagerImpl.getDefault().closeAll(wtoken,
2508 r.activity.getClass().getName(), "Activity");
2509 }
2510 }
2511 r.mPendingRemoveWindow = null;
2512 r.mPendingRemoveWindowManager = null;
2513 }
2514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2516 // If we are getting ready to gc after going to the background, well
2517 // we are back active so skip it.
2518 unscheduleGcIdler();
2519
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002520 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521
2522 if (r != null) {
2523 final Activity a = r.activity;
2524
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002525 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 TAG, "Resume " + r + " started activity: " +
2527 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2528 + ", finished: " + a.mFinished);
2529
2530 final int forwardBit = isForward ?
2531 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 // If the window hasn't yet been added to the window manager,
2534 // and this guy didn't finish itself or start another activity,
2535 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002536 boolean willBeVisible = !a.mStartedActivity;
2537 if (!willBeVisible) {
2538 try {
2539 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2540 a.getActivityToken());
2541 } catch (RemoteException e) {
2542 }
2543 }
2544 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 r.window = r.activity.getWindow();
2546 View decor = r.window.getDecorView();
2547 decor.setVisibility(View.INVISIBLE);
2548 ViewManager wm = a.getWindowManager();
2549 WindowManager.LayoutParams l = r.window.getAttributes();
2550 a.mDecor = decor;
2551 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2552 l.softInputMode |= forwardBit;
2553 if (a.mVisibleFromClient) {
2554 a.mWindowAdded = true;
2555 wm.addView(decor, l);
2556 }
2557
2558 // If the window has already been added, but during resume
2559 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002560 // window visible.
2561 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002562 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 TAG, "Launch " + r + " mStartedActivity set");
2564 r.hideForNow = true;
2565 }
2566
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002567 // Get rid of anything left hanging around.
2568 cleanUpPendingRemoveWindows(r);
2569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 // The window is now visible if it has been added, we are not
2571 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002572 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002573 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002575 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002576 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 performConfigurationChanged(r.activity, r.newConfig);
2578 r.newConfig = null;
2579 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002580 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 + isForward);
2582 WindowManager.LayoutParams l = r.window.getAttributes();
2583 if ((l.softInputMode
2584 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2585 != forwardBit) {
2586 l.softInputMode = (l.softInputMode
2587 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2588 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002589 if (r.activity.mVisibleFromClient) {
2590 ViewManager wm = a.getWindowManager();
2591 View decor = r.window.getDecorView();
2592 wm.updateViewLayout(decor, l);
2593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 }
2595 r.activity.mVisibleFromServer = true;
2596 mNumVisibleActivities++;
2597 if (r.activity.mVisibleFromClient) {
2598 r.activity.makeVisible();
2599 }
2600 }
2601
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002602 if (!r.onlyLocalRequest) {
2603 r.nextIdle = mNewActivities;
2604 mNewActivities = r;
2605 if (localLOGV) Slog.v(
2606 TAG, "Scheduling idle handler for " + r);
2607 Looper.myQueue().addIdleHandler(new Idler());
2608 }
2609 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610
2611 } else {
2612 // If an exception was thrown when trying to resume, then
2613 // just end this activity.
2614 try {
2615 ActivityManagerNative.getDefault()
2616 .finishActivity(token, Activity.RESULT_CANCELED, null);
2617 } catch (RemoteException ex) {
2618 }
2619 }
2620 }
2621
2622 private int mThumbnailWidth = -1;
2623 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002624 private Bitmap mAvailThumbnailBitmap = null;
2625 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626
Romain Guy65b345f2011-07-27 18:51:50 -07002627 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002628 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002630 if (thumbnail == null) {
2631 int w = mThumbnailWidth;
2632 int h;
2633 if (w < 0) {
2634 Resources res = r.activity.getResources();
2635 mThumbnailHeight = h =
2636 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002638 mThumbnailWidth = w =
2639 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2640 } else {
2641 h = mThumbnailHeight;
2642 }
2643
2644 // On platforms where we don't want thumbnails, set dims to (0,0)
2645 if ((w > 0) && (h > 0)) {
2646 thumbnail = Bitmap.createBitmap(w, h, THUMBNAIL_FORMAT);
2647 thumbnail.eraseColor(0);
2648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 }
2650
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002651 if (thumbnail != null) {
2652 Canvas cv = mThumbnailCanvas;
2653 if (cv == null) {
2654 mThumbnailCanvas = cv = new Canvas();
2655 }
2656
2657 cv.setBitmap(thumbnail);
2658 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2659 mAvailThumbnailBitmap = thumbnail;
2660 thumbnail = null;
2661 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002662 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 } catch (Exception e) {
2666 if (!mInstrumentation.onException(r.activity, e)) {
2667 throw new RuntimeException(
2668 "Unable to create thumbnail of "
2669 + r.intent.getComponent().toShortString()
2670 + ": " + e.toString(), e);
2671 }
2672 thumbnail = null;
2673 }
2674
2675 return thumbnail;
2676 }
2677
Romain Guy65b345f2011-07-27 18:51:50 -07002678 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002680 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002682 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 if (userLeaving) {
2684 performUserLeavingActivity(r);
2685 }
Bob Leee5408332009-09-04 18:31:17 -07002686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002688 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002690 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002691 if (r.isPreHoneycomb()) {
2692 QueuedWork.waitToFinish();
2693 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 // Tell the activity manager we have paused.
2696 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002697 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 } catch (RemoteException ex) {
2699 }
2700 }
2701 }
2702
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002703 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 mInstrumentation.callActivityOnUserLeaving(r.activity);
2705 }
2706
2707 final Bundle performPauseActivity(IBinder token, boolean finished,
2708 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002709 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 return r != null ? performPauseActivity(r, finished, saveState) : null;
2711 }
2712
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002713 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 boolean saveState) {
2715 if (r.paused) {
2716 if (r.activity.mFinished) {
2717 // If we are finishing, we won't call onResume() in certain cases.
2718 // So here we likewise don't want to call onPause() if the activity
2719 // isn't resumed.
2720 return null;
2721 }
2722 RuntimeException e = new RuntimeException(
2723 "Performing pause of activity that is not resumed: "
2724 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002725 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 }
2727 Bundle state = null;
2728 if (finished) {
2729 r.activity.mFinished = true;
2730 }
2731 try {
2732 // Next have the activity save its current state and managed dialogs...
2733 if (!r.activity.mFinished && saveState) {
2734 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002735 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2737 r.state = state;
2738 }
2739 // Now we are idle.
2740 r.activity.mCalled = false;
2741 mInstrumentation.callActivityOnPause(r.activity);
2742 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2743 if (!r.activity.mCalled) {
2744 throw new SuperNotCalledException(
2745 "Activity " + r.intent.getComponent().toShortString() +
2746 " did not call through to super.onPause()");
2747 }
2748
2749 } catch (SuperNotCalledException e) {
2750 throw e;
2751
2752 } catch (Exception e) {
2753 if (!mInstrumentation.onException(r.activity, e)) {
2754 throw new RuntimeException(
2755 "Unable to pause activity "
2756 + r.intent.getComponent().toShortString()
2757 + ": " + e.toString(), e);
2758 }
2759 }
2760 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002761
2762 // Notify any outstanding on paused listeners
2763 ArrayList<OnActivityPausedListener> listeners;
2764 synchronized (mOnPauseListeners) {
2765 listeners = mOnPauseListeners.remove(r.activity);
2766 }
2767 int size = (listeners != null ? listeners.size() : 0);
2768 for (int i = 0; i < size; i++) {
2769 listeners.get(i).onPaused(r.activity);
2770 }
2771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 return state;
2773 }
2774
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002775 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002776 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002777 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 }
2779
2780 private static class StopInfo {
2781 Bitmap thumbnail;
2782 CharSequence description;
2783 }
2784
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002785 private static final class ProviderRefCount {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 public int count;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 ProviderRefCount(int pCount) {
2789 count = pCount;
2790 }
2791 }
2792
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002793 /**
2794 * Core implementation of stopping an activity. Note this is a little
2795 * tricky because the server's meaning of stop is slightly different
2796 * than our client -- for the server, stop means to save state and give
2797 * it the result when it is done, but the window may still be visible.
2798 * For the client, we want to call onStop()/onStart() to indicate when
2799 * the activity's UI visibillity changes.
2800 */
Romain Guy65b345f2011-07-27 18:51:50 -07002801 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002802 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002803 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002804 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 if (r != null) {
2806 if (!keepShown && r.stopped) {
2807 if (r.activity.mFinished) {
2808 // If we are finishing, we won't call onResume() in certain
2809 // cases. So here we likewise don't want to call onStop()
2810 // if the activity isn't resumed.
2811 return;
2812 }
2813 RuntimeException e = new RuntimeException(
2814 "Performing stop of activity that is not resumed: "
2815 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002816 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 }
2818
2819 if (info != null) {
2820 try {
2821 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002822 // For now, don't create the thumbnail here; we are
2823 // doing that by doing a screen snapshot.
2824 info.thumbnail = null; //createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 info.description = r.activity.onCreateDescription();
2826 } catch (Exception e) {
2827 if (!mInstrumentation.onException(r.activity, e)) {
2828 throw new RuntimeException(
2829 "Unable to save state of activity "
2830 + r.intent.getComponent().toShortString()
2831 + ": " + e.toString(), e);
2832 }
2833 }
2834 }
2835
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002836 // Next have the activity save its current state and managed dialogs...
2837 if (!r.activity.mFinished && saveState) {
2838 if (r.state == null) {
2839 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002840 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002841 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2842 r.state = state;
2843 } else {
2844 state = r.state;
2845 }
2846 }
2847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 if (!keepShown) {
2849 try {
2850 // Now we are idle.
2851 r.activity.performStop();
2852 } catch (Exception e) {
2853 if (!mInstrumentation.onException(r.activity, e)) {
2854 throw new RuntimeException(
2855 "Unable to stop activity "
2856 + r.intent.getComponent().toShortString()
2857 + ": " + e.toString(), e);
2858 }
2859 }
2860 r.stopped = true;
2861 }
2862
2863 r.paused = true;
2864 }
2865 }
2866
Romain Guy65b345f2011-07-27 18:51:50 -07002867 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 View v = r.activity.mDecor;
2869 if (v != null) {
2870 if (show) {
2871 if (!r.activity.mVisibleFromServer) {
2872 r.activity.mVisibleFromServer = true;
2873 mNumVisibleActivities++;
2874 if (r.activity.mVisibleFromClient) {
2875 r.activity.makeVisible();
2876 }
2877 }
2878 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002879 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002880 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 performConfigurationChanged(r.activity, r.newConfig);
2882 r.newConfig = null;
2883 }
2884 } else {
2885 if (r.activity.mVisibleFromServer) {
2886 r.activity.mVisibleFromServer = false;
2887 mNumVisibleActivities--;
2888 v.setVisibility(View.INVISIBLE);
2889 }
2890 }
2891 }
2892 }
2893
Romain Guy65b345f2011-07-27 18:51:50 -07002894 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002895 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 r.activity.mConfigChangeFlags |= configChanges;
2897
2898 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002899 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002901 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 TAG, "Finishing stop of " + r + ": show=" + show
2903 + " win=" + r.window);
2904
2905 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07002906
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002907 // Make sure any pending writes are now committed.
2908 if (!r.isPreHoneycomb()) {
2909 QueuedWork.waitToFinish();
2910 }
2911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 // Tell activity manager we have been stopped.
2913 try {
2914 ActivityManagerNative.getDefault().activityStopped(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002915 r.token, r.state, info.thumbnail, info.description);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 } catch (RemoteException ex) {
2917 }
2918 }
2919
2920 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002921 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 if (r.stopped) {
2923 r.activity.performRestart();
2924 r.stopped = false;
2925 }
2926 }
2927
Romain Guy65b345f2011-07-27 18:51:50 -07002928 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002929 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08002930
2931 if (r == null) {
2932 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
2933 return;
2934 }
2935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002937 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 } else if (show && r.stopped) {
2939 // If we are getting ready to gc after going to the background, well
2940 // we are back active so skip it.
2941 unscheduleGcIdler();
2942
2943 r.activity.performRestart();
2944 r.stopped = false;
2945 }
2946 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07002947 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948 TAG, "Handle window " + r + " visibility: " + show);
2949 updateVisibility(r, show);
2950 }
2951 }
2952
Romain Guy65b345f2011-07-27 18:51:50 -07002953 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002954 ActivityClientRecord r = mActivities.get(token);
2955
2956 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002957 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002958 return;
2959 }
2960
2961 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002962 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002963 try {
2964 // Now we are idle.
2965 r.activity.performStop();
2966 } catch (Exception e) {
2967 if (!mInstrumentation.onException(r.activity, e)) {
2968 throw new RuntimeException(
2969 "Unable to stop activity "
2970 + r.intent.getComponent().toShortString()
2971 + ": " + e.toString(), e);
2972 }
2973 }
2974 r.stopped = true;
2975 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002976
2977 // Make sure any pending writes are now committed.
2978 if (!r.isPreHoneycomb()) {
2979 QueuedWork.waitToFinish();
2980 }
2981
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002982 // Tell activity manager we slept.
2983 try {
2984 ActivityManagerNative.getDefault().activitySlept(r.token);
2985 } catch (RemoteException ex) {
2986 }
2987 } else {
2988 if (r.stopped && r.activity.mVisibleFromServer) {
2989 r.activity.performRestart();
2990 r.stopped = false;
2991 }
2992 }
2993 }
2994
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002995 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08002996 synchronized (mPackages) {
2997 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002998 }
2999 }
3000
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003001 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3002 LoadedApk apk = peekPackageInfo(data.pkg, false);
3003 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003004 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003005 }
3006 apk = peekPackageInfo(data.pkg, true);
3007 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003008 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003009 }
3010 handleConfigurationChanged(mConfiguration, data.info);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003011 WindowManagerImpl.getDefault().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003012 }
3013
Romain Guy65b345f2011-07-27 18:51:50 -07003014 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 final int N = results.size();
3016 for (int i=0; i<N; i++) {
3017 ResultInfo ri = results.get(i);
3018 try {
3019 if (ri.mData != null) {
3020 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
3021 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003022 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003023 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 r.activity.dispatchActivityResult(ri.mResultWho,
3025 ri.mRequestCode, ri.mResultCode, ri.mData);
3026 } catch (Exception e) {
3027 if (!mInstrumentation.onException(r.activity, e)) {
3028 throw new RuntimeException(
3029 "Failure delivering result " + ri + " to activity "
3030 + r.intent.getComponent().toShortString()
3031 + ": " + e.toString(), e);
3032 }
3033 }
3034 }
3035 }
3036
Romain Guy65b345f2011-07-27 18:51:50 -07003037 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003038 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003039 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 if (r != null) {
3041 final boolean resumed = !r.paused;
3042 if (!r.activity.mFinished && r.activity.mDecor != null
3043 && r.hideForNow && resumed) {
3044 // We had hidden the activity because it started another
3045 // one... we have gotten a result back and we are not
3046 // paused, so make sure our window is visible.
3047 updateVisibility(r, true);
3048 }
3049 if (resumed) {
3050 try {
3051 // Now we are idle.
3052 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003053 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003054 mInstrumentation.callActivityOnPause(r.activity);
3055 if (!r.activity.mCalled) {
3056 throw new SuperNotCalledException(
3057 "Activity " + r.intent.getComponent().toShortString()
3058 + " did not call through to super.onPause()");
3059 }
3060 } catch (SuperNotCalledException e) {
3061 throw e;
3062 } catch (Exception e) {
3063 if (!mInstrumentation.onException(r.activity, e)) {
3064 throw new RuntimeException(
3065 "Unable to pause activity "
3066 + r.intent.getComponent().toShortString()
3067 + ": " + e.toString(), e);
3068 }
3069 }
3070 }
3071 deliverResults(r, res.results);
3072 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003073 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003074 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 }
3076 }
3077 }
3078
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003079 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 return performDestroyActivity(token, finishing, 0, false);
3081 }
3082
Romain Guy65b345f2011-07-27 18:51:50 -07003083 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003085 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003086 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003087 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003089 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 r.activity.mConfigChangeFlags |= configChanges;
3091 if (finishing) {
3092 r.activity.mFinished = true;
3093 }
3094 if (!r.paused) {
3095 try {
3096 r.activity.mCalled = false;
3097 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07003098 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 r.activity.getComponentName().getClassName());
3100 if (!r.activity.mCalled) {
3101 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003102 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 + " did not call through to super.onPause()");
3104 }
3105 } catch (SuperNotCalledException e) {
3106 throw e;
3107 } catch (Exception e) {
3108 if (!mInstrumentation.onException(r.activity, e)) {
3109 throw new RuntimeException(
3110 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003111 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 + ": " + e.toString(), e);
3113 }
3114 }
3115 r.paused = true;
3116 }
3117 if (!r.stopped) {
3118 try {
3119 r.activity.performStop();
3120 } catch (SuperNotCalledException e) {
3121 throw e;
3122 } catch (Exception e) {
3123 if (!mInstrumentation.onException(r.activity, e)) {
3124 throw new RuntimeException(
3125 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003126 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127 + ": " + e.toString(), e);
3128 }
3129 }
3130 r.stopped = true;
3131 }
3132 if (getNonConfigInstance) {
3133 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003134 r.lastNonConfigurationInstances
3135 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136 } catch (Exception e) {
3137 if (!mInstrumentation.onException(r.activity, e)) {
3138 throw new RuntimeException(
3139 "Unable to retain activity "
3140 + r.intent.getComponent().toShortString()
3141 + ": " + e.toString(), e);
3142 }
3143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 }
3145 try {
3146 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003147 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 if (!r.activity.mCalled) {
3149 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003150 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 " did not call through to super.onDestroy()");
3152 }
3153 if (r.window != null) {
3154 r.window.closeAllPanels();
3155 }
3156 } catch (SuperNotCalledException e) {
3157 throw e;
3158 } catch (Exception e) {
3159 if (!mInstrumentation.onException(r.activity, e)) {
3160 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003161 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3162 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 }
3164 }
3165 }
3166 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003167 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 return r;
3169 }
3170
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003171 private static String safeToComponentShortString(Intent intent) {
3172 ComponentName component = intent.getComponent();
3173 return component == null ? "[Unknown]" : component.toShortString();
3174 }
3175
Romain Guy65b345f2011-07-27 18:51:50 -07003176 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003178 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179 configChanges, getNonConfigInstance);
3180 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003181 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 WindowManager wm = r.activity.getWindowManager();
3183 View v = r.activity.mDecor;
3184 if (v != null) {
3185 if (r.activity.mVisibleFromServer) {
3186 mNumVisibleActivities--;
3187 }
3188 IBinder wtoken = v.getWindowToken();
3189 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003190 if (r.onlyLocalRequest) {
3191 // Hold off on removing this until the new activity's
3192 // window is being added.
3193 r.mPendingRemoveWindow = v;
3194 r.mPendingRemoveWindowManager = wm;
3195 } else {
3196 wm.removeViewImmediate(v);
3197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003199 if (wtoken != null && r.mPendingRemoveWindow == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 WindowManagerImpl.getDefault().closeAll(wtoken,
3201 r.activity.getClass().getName(), "Activity");
3202 }
3203 r.activity.mDecor = null;
3204 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003205 if (r.mPendingRemoveWindow == null) {
3206 // If we are delaying the removal of the activity window, then
3207 // we can't clean up all windows here. Note that we can't do
3208 // so later either, which means any windows that aren't closed
3209 // by the app will leak. Well we try to warning them a lot
3210 // about leaking windows, because that is a bug, so if they are
3211 // using this recreate facility then they get to live with leaks.
3212 WindowManagerImpl.getDefault().closeAll(token,
3213 r.activity.getClass().getName(), "Activity");
3214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215
3216 // Mocked out contexts won't be participating in the normal
3217 // process lifecycle, but if we're running with a proper
3218 // ApplicationContext we need to have it tear down things
3219 // cleanly.
3220 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003221 if (c instanceof ContextImpl) {
3222 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 r.activity.getClass().getName(), "Activity");
3224 }
3225 }
3226 if (finishing) {
3227 try {
3228 ActivityManagerNative.getDefault().activityDestroyed(token);
3229 } catch (RemoteException ex) {
3230 // If the system process has died, it's game over for everyone.
3231 }
3232 }
3233 }
3234
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003235 public final void requestRelaunchActivity(IBinder token,
3236 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3237 int configChanges, boolean notResumed, Configuration config,
3238 boolean fromServer) {
3239 ActivityClientRecord target = null;
3240
3241 synchronized (mPackages) {
3242 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3243 ActivityClientRecord r = mRelaunchingActivities.get(i);
3244 if (r.token == token) {
3245 target = r;
3246 if (pendingResults != null) {
3247 if (r.pendingResults != null) {
3248 r.pendingResults.addAll(pendingResults);
3249 } else {
3250 r.pendingResults = pendingResults;
3251 }
3252 }
3253 if (pendingNewIntents != null) {
3254 if (r.pendingIntents != null) {
3255 r.pendingIntents.addAll(pendingNewIntents);
3256 } else {
3257 r.pendingIntents = pendingNewIntents;
3258 }
3259 }
3260 break;
3261 }
3262 }
3263
3264 if (target == null) {
3265 target = new ActivityClientRecord();
3266 target.token = token;
3267 target.pendingResults = pendingResults;
3268 target.pendingIntents = pendingNewIntents;
3269 if (!fromServer) {
3270 ActivityClientRecord existing = mActivities.get(token);
3271 if (existing != null) {
3272 target.startsNotResumed = existing.paused;
3273 }
3274 target.onlyLocalRequest = true;
3275 }
3276 mRelaunchingActivities.add(target);
3277 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3278 }
3279
3280 if (fromServer) {
3281 target.startsNotResumed = notResumed;
3282 target.onlyLocalRequest = false;
3283 }
3284 if (config != null) {
3285 target.createdConfig = config;
3286 }
3287 target.pendingConfigChanges |= configChanges;
3288 }
3289 }
3290
Romain Guy65b345f2011-07-27 18:51:50 -07003291 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 // If we are getting ready to gc after going to the background, well
3293 // we are back active so skip it.
3294 unscheduleGcIdler();
3295
3296 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003297 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 // First: make sure we have the most recent configuration and most
3300 // recent version of the activity, or skip it if some previous call
3301 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003302 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 int N = mRelaunchingActivities.size();
3304 IBinder token = tmp.token;
3305 tmp = null;
3306 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003307 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 if (r.token == token) {
3309 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003310 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 mRelaunchingActivities.remove(i);
3312 i--;
3313 N--;
3314 }
3315 }
Bob Leee5408332009-09-04 18:31:17 -07003316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003318 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 return;
3320 }
Bob Leee5408332009-09-04 18:31:17 -07003321
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003322 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3323 + tmp.token + " with configChanges=0x"
3324 + Integer.toHexString(configChanges));
3325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 if (mPendingConfiguration != null) {
3327 changedConfig = mPendingConfiguration;
3328 mPendingConfiguration = null;
3329 }
3330 }
Bob Leee5408332009-09-04 18:31:17 -07003331
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003332 if (tmp.createdConfig != null) {
3333 // If the activity manager is passing us its current config,
3334 // assume that is really what we want regardless of what we
3335 // may have pending.
3336 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003337 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3338 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3339 if (changedConfig == null
3340 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3341 changedConfig = tmp.createdConfig;
3342 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003343 }
3344 }
3345
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003346 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003347 + tmp.token + ": changedConfig=" + changedConfig);
3348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 // If there was a pending configuration change, execute it first.
3350 if (changedConfig != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003351 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 }
Bob Leee5408332009-09-04 18:31:17 -07003353
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003354 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003355 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 if (r == null) {
3357 return;
3358 }
Bob Leee5408332009-09-04 18:31:17 -07003359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003361 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003362 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003363
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003364 r.activity.mChangingConfigurations = true;
3365
Dianne Hackborne2b04802010-12-09 09:24:55 -08003366 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003368 performPauseActivity(r.token, false, r.isPreHoneycomb());
3369 }
3370 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3371 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003372 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003373 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 }
Bob Leee5408332009-09-04 18:31:17 -07003375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378 r.activity = null;
3379 r.window = null;
3380 r.hideForNow = false;
3381 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003382 // Merge any pending results and pending intents; don't just replace them
3383 if (tmp.pendingResults != null) {
3384 if (r.pendingResults == null) {
3385 r.pendingResults = tmp.pendingResults;
3386 } else {
3387 r.pendingResults.addAll(tmp.pendingResults);
3388 }
3389 }
3390 if (tmp.pendingIntents != null) {
3391 if (r.pendingIntents == null) {
3392 r.pendingIntents = tmp.pendingIntents;
3393 } else {
3394 r.pendingIntents.addAll(tmp.pendingIntents);
3395 }
3396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003398
Christopher Tateb70f3df2009-04-07 16:07:59 -07003399 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 }
3401
Romain Guy65b345f2011-07-27 18:51:50 -07003402 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003403 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 Bitmap thumbnail = createThumbnailBitmap(r);
3405 CharSequence description = null;
3406 try {
3407 description = r.activity.onCreateDescription();
3408 } catch (Exception e) {
3409 if (!mInstrumentation.onException(r.activity, e)) {
3410 throw new RuntimeException(
3411 "Unable to create description of activity "
3412 + r.intent.getComponent().toShortString()
3413 + ": " + e.toString(), e);
3414 }
3415 }
3416 //System.out.println("Reporting top thumbnail " + thumbnail);
3417 try {
3418 ActivityManagerNative.getDefault().reportThumbnail(
3419 token, thumbnail, description);
3420 } catch (RemoteException ex) {
3421 }
3422 }
3423
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003424 ArrayList<ComponentCallbacks2> collectComponentCallbacksLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003425 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003426 ArrayList<ComponentCallbacks2> callbacks
3427 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 if (mActivities.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003430 Iterator<ActivityClientRecord> it = mActivities.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 while (it.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003432 ActivityClientRecord ar = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 Activity a = ar.activity;
3434 if (a != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003435 Configuration thisConfig = applyConfigCompatMainThread(newConfig,
3436 ar.packageInfo.mCompatibilityInfo.getIfNeeded());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437 if (!ar.activity.mFinished && (allActivities ||
3438 (a != null && !ar.paused))) {
3439 // If the activity is currently resumed, its configuration
3440 // needs to change right now.
3441 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003442 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443 // Otherwise, we will tell it about the change
3444 // the next time it is resumed or shown. Note that
3445 // the activity manager may, before then, decide the
3446 // activity needs to be destroyed to handle its new
3447 // configuration.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003448 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Setting activity "
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003449 + ar.activityInfo.name + " newConfig=" + thisConfig);
3450 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 }
3452 }
3453 }
3454 }
3455 if (mServices.size() > 0) {
3456 Iterator<Service> it = mServices.values().iterator();
3457 while (it.hasNext()) {
3458 callbacks.add(it.next());
3459 }
3460 }
3461 synchronized (mProviderMap) {
3462 if (mLocalProviders.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003463 Iterator<ProviderClientRecord> it = mLocalProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 while (it.hasNext()) {
3465 callbacks.add(it.next().mLocalProvider);
3466 }
3467 }
3468 }
3469 final int N = mAllApplications.size();
3470 for (int i=0; i<N; i++) {
3471 callbacks.add(mAllApplications.get(i));
3472 }
Bob Leee5408332009-09-04 18:31:17 -07003473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003474 return callbacks;
3475 }
Bob Leee5408332009-09-04 18:31:17 -07003476
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003477 private final void performConfigurationChanged(
3478 ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003480 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 // we check the runtime type and act accordingly.
3482 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3483 if (activity != null) {
3484 activity.mCalled = false;
3485 }
Bob Leee5408332009-09-04 18:31:17 -07003486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 boolean shouldChangeConfig = false;
3488 if ((activity == null) || (activity.mCurrentConfig == null)) {
3489 shouldChangeConfig = true;
3490 } else {
Bob Leee5408332009-09-04 18:31:17 -07003491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 // If the new config is the same as the config this Activity
3493 // is already running with then don't bother calling
3494 // onConfigurationChanged
3495 int diff = activity.mCurrentConfig.diff(config);
3496 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 // If this activity doesn't handle any of the config changes
3498 // then don't bother calling onConfigurationChanged as we're
3499 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003500 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 shouldChangeConfig = true;
3502 }
3503 }
3504 }
Bob Leee5408332009-09-04 18:31:17 -07003505
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003506 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003507 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508 if (shouldChangeConfig) {
3509 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003511 if (activity != null) {
3512 if (!activity.mCalled) {
3513 throw new SuperNotCalledException(
3514 "Activity " + activity.getLocalClassName() +
3515 " did not call through to super.onConfigurationChanged()");
3516 }
3517 activity.mConfigChangeFlags = 0;
3518 activity.mCurrentConfig = new Configuration(config);
3519 }
3520 }
3521 }
3522
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003523 public final void applyConfigurationToResources(Configuration config) {
3524 synchronized (mPackages) {
3525 applyConfigurationToResourcesLocked(config, null);
3526 }
3527 }
3528
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003529 final boolean applyConfigurationToResourcesLocked(Configuration config,
3530 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003531 if (mResConfiguration == null) {
3532 mResConfiguration = new Configuration();
3533 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003534 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003535 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003536 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003537 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003538 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003539 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003540 DisplayMetrics dm = getDisplayMetricsLocked(null, true);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003541
3542 if (compat != null && (mResCompatibilityInfo == null ||
3543 !mResCompatibilityInfo.equals(compat))) {
3544 mResCompatibilityInfo = compat;
3545 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3546 | ActivityInfo.CONFIG_SCREEN_SIZE
3547 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3548 }
Bob Leee5408332009-09-04 18:31:17 -07003549
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003550 // set it for java, this also affects newly created Resources
3551 if (config.locale != null) {
3552 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 }
Bob Leee5408332009-09-04 18:31:17 -07003554
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003555 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003556
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003557 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003558 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003559
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003560 Iterator<WeakReference<Resources>> it =
3561 mActiveResources.values().iterator();
3562 //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3563 // mActiveResources.entrySet().iterator();
3564 while (it.hasNext()) {
3565 WeakReference<Resources> v = it.next();
3566 Resources r = v.get();
3567 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003568 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003569 + r + " config to: " + config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003570 r.updateConfiguration(config, dm, compat);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003571 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003572 // + " " + r + ": " + r.getConfiguration());
3573 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003574 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003575 it.remove();
3576 }
3577 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003578
3579 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003580 }
Dianne Hackborn836e2622011-10-04 18:32:39 -07003581
3582 final Configuration applyCompatConfiguration() {
3583 Configuration config = mConfiguration;
3584 if (mCompatConfiguration == null) {
3585 mCompatConfiguration = new Configuration();
3586 }
3587 mCompatConfiguration.setTo(mConfiguration);
3588 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
3589 mResCompatibilityInfo.applyToConfiguration(mCompatConfiguration);
3590 config = mCompatConfiguration;
3591 }
3592 return config;
3593 }
3594
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003595 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003596
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003597 ArrayList<ComponentCallbacks2> callbacks = null;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003598
3599 synchronized (mPackages) {
3600 if (mPendingConfiguration != null) {
3601 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3602 config = mPendingConfiguration;
3603 }
3604 mPendingConfiguration = null;
3605 }
3606
3607 if (config == null) {
3608 return;
3609 }
3610
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003611 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003612 + config);
3613
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003614 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 if (mConfiguration == null) {
3617 mConfiguration = new Configuration();
3618 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003619 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003620 return;
3621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 mConfiguration.updateFrom(config);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003623 config = applyCompatConfiguration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 callbacks = collectComponentCallbacksLocked(false, config);
3625 }
Romain Guy65b345f2011-07-27 18:51:50 -07003626
3627 // Cleanup hardware accelerated stuff
3628 WindowManagerImpl.getDefault().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003629
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003630 if (callbacks != null) {
3631 final int N = callbacks.size();
3632 for (int i=0; i<N; i++) {
3633 performConfigurationChanged(callbacks.get(i), config);
3634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003635 }
3636 }
3637
3638 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003639 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640 if (r == null || r.activity == null) {
3641 return;
3642 }
Bob Leee5408332009-09-04 18:31:17 -07003643
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003644 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003645 + r.activityInfo.name);
3646
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003647 performConfigurationChanged(r.activity, mCompatConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 }
3649
Romain Guy7eabe552011-07-21 14:56:34 -07003650 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003651 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003652 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003653 switch (profileType) {
3654 case 1:
3655 ViewDebug.startLooperProfiling(pcd.path, pcd.fd.getFileDescriptor());
3656 break;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003657 default:
3658 mProfiler.setProfiler(pcd.path, pcd.fd);
3659 mProfiler.autoStopProfiler = false;
3660 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003661 break;
3662 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003663 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003664 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003665 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003666 } finally {
3667 try {
3668 pcd.fd.close();
3669 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003670 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003671 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003672 }
3673 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003674 switch (profileType) {
3675 case 1:
3676 ViewDebug.stopLooperProfiling();
3677 break;
3678 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003679 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003680 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003681 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003682 }
3683 }
Bob Leee5408332009-09-04 18:31:17 -07003684
Andy McFadden824c5102010-07-09 16:26:57 -07003685 final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
3686 if (managed) {
3687 try {
3688 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3689 } catch (IOException e) {
3690 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3691 + " -- can the process access this path?");
3692 } finally {
3693 try {
3694 dhd.fd.close();
3695 } catch (IOException e) {
3696 Slog.w(TAG, "Failure closing profile fd", e);
3697 }
3698 }
3699 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003700 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003701 }
3702 }
3703
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003704 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3705 boolean hasPkgInfo = false;
3706 if (packages != null) {
3707 for (int i=packages.length-1; i>=0; i--) {
3708 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3709 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003710 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003711 ref = mPackages.get(packages[i]);
3712 if (ref != null && ref.get() != null) {
3713 hasPkgInfo = true;
3714 } else {
3715 ref = mResourcePackages.get(packages[i]);
3716 if (ref != null && ref.get() != null) {
3717 hasPkgInfo = true;
3718 }
3719 }
3720 }
3721 mPackages.remove(packages[i]);
3722 mResourcePackages.remove(packages[i]);
3723 }
3724 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003725 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003726 hasPkgInfo);
3727 }
3728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 final void handleLowMemory() {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003730 ArrayList<ComponentCallbacks2> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003732 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 callbacks = collectComponentCallbacksLocked(true, null);
3734 }
Bob Leee5408332009-09-04 18:31:17 -07003735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003736 final int N = callbacks.size();
3737 for (int i=0; i<N; i++) {
3738 callbacks.get(i).onLowMemory();
3739 }
3740
Chris Tatece229052009-03-25 16:44:52 -07003741 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3742 if (Process.myUid() != Process.SYSTEM_UID) {
3743 int sqliteReleased = SQLiteDatabase.releaseMemory();
3744 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3745 }
Bob Leee5408332009-09-04 18:31:17 -07003746
Mike Reedcaf0df12009-04-27 14:32:05 -04003747 // Ask graphics to free up as much as possible (font/image caches)
3748 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749
3750 BinderInternal.forceGc("mem");
3751 }
3752
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003753 final void handleTrimMemory(int level) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003754 ArrayList<ComponentCallbacks2> callbacks;
3755
3756 synchronized (mPackages) {
3757 callbacks = collectComponentCallbacksLocked(true, null);
3758 }
3759
3760 final int N = callbacks.size();
3761 for (int i=0; i<N; i++) {
3762 callbacks.get(i).onTrimMemory(level);
3763 }
Teng-Hui Zhua34eb912012-01-25 13:46:16 -08003764 WindowManagerImpl.getDefault().trimMemory(level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003765 }
3766
Romain Guya9582652011-11-10 14:20:10 -08003767 private void setupGraphicsSupport(LoadedApk info) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003768 if (Process.isIsolated()) {
3769 // Isolated processes aren't going to do UI.
3770 return;
3771 }
Romain Guya9582652011-11-10 14:20:10 -08003772 try {
3773 int uid = Process.myUid();
3774 String[] packages = getPackageManager().getPackagesForUid(uid);
3775
3776 // If there are several packages in this application we won't
3777 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003778 if (packages != null && packages.length == 1) {
Romain Guya9582652011-11-10 14:20:10 -08003779 ContextImpl appContext = new ContextImpl();
3780 appContext.init(info, null, this);
3781
3782 HardwareRenderer.setupDiskCache(appContext.getCacheDir());
Jason Samsa6f338c2012-02-24 16:22:16 -08003783 RenderScript.setupDiskCache(appContext.getCacheDir());
Romain Guya9582652011-11-10 14:20:10 -08003784 }
3785 } catch (RemoteException e) {
3786 // Ignore
3787 }
3788 }
3789
Romain Guy65b345f2011-07-27 18:51:50 -07003790 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003791 mBoundApplication = data;
3792 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003793 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003794
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003795 mProfiler = new Profiler();
3796 mProfiler.profileFile = data.initProfileFile;
3797 mProfiler.profileFd = data.initProfileFd;
3798 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
3799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003800 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003801 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003802 android.ddm.DdmHandleAppName.setAppName(data.processName);
3803
Dianne Hackborn5d927c22011-09-02 12:22:18 -07003804 if (data.persistent) {
3805 // Persistent processes on low-memory devices do not get to
3806 // use hardware accelerated drawing, since this can add too much
3807 // overhead to the process.
3808 Display display = WindowManagerImpl.getDefault().getDefaultDisplay();
3809 if (!ActivityManager.isHighEndGfx(display)) {
3810 HardwareRenderer.disable(false);
3811 }
3812 }
Romain Guya9582652011-11-10 14:20:10 -08003813
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003814 if (mProfiler.profileFd != null) {
3815 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003816 }
3817
Joe Onoratod630f102011-03-17 18:42:26 -07003818 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
3819 // implementation to use the pool executor. Normally, we use the
3820 // serialized executor as the default. This has to happen in the
3821 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07003822 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07003823 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
3824 }
3825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 /*
3827 * Before spawning a new process, reset the time zone to be the system time zone.
3828 * This needs to be done because the system time zone could have changed after the
3829 * the spawning of this process. Without doing this this process would have the incorrect
3830 * system time zone.
3831 */
3832 TimeZone.setDefault(null);
3833
3834 /*
3835 * Initialize the default locale in this process for the reasons we set the time zone.
3836 */
3837 Locale.setDefault(data.config.locale);
3838
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003839 /*
3840 * Update the system configuration since its preloaded and might not
3841 * reflect configuration changes. The configuration object passed
3842 * in AppBindData can be safely assumed to be up to date
3843 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003844 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003845 applyCompatConfiguration();
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003846
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003847 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003848
Romain Guya9582652011-11-10 14:20:10 -08003849 setupGraphicsSupport(data.info);
3850
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003851 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003852 * For system applications on userdebug/eng builds, log stack
3853 * traces of disk and network access to dropbox for analysis.
3854 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07003855 if ((data.appInfo.flags &
3856 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07003857 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
3858 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003859 }
3860
3861 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07003862 * For apps targetting SDK Honeycomb or later, we don't allow
3863 * network usage on the main event loop / UI thread.
3864 *
3865 * Note to those grepping: this is what ultimately throws
3866 * NetworkOnMainThreadException ...
3867 */
3868 if (data.appInfo.targetSdkVersion > 9) {
3869 StrictMode.enableDeathOnNetwork();
3870 }
3871
3872 /**
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003873 * Switch this process to density compatibility mode if needed.
3874 */
3875 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
3876 == 0) {
3877 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3878 }
Bob Leee5408332009-09-04 18:31:17 -07003879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003880 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
3881 // XXX should have option to change the port.
3882 Debug.changeDebugPort(8100);
3883 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003884 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 + " is waiting for the debugger on port 8100...");
3886
3887 IActivityManager mgr = ActivityManagerNative.getDefault();
3888 try {
3889 mgr.showWaitingForDebugger(mAppThread, true);
3890 } catch (RemoteException ex) {
3891 }
3892
3893 Debug.waitForDebugger();
3894
3895 try {
3896 mgr.showWaitingForDebugger(mAppThread, false);
3897 } catch (RemoteException ex) {
3898 }
3899
3900 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003901 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 + " can be debugged on port 8100...");
3903 }
3904 }
3905
Robert Greenwalt434203a2010-10-11 16:00:27 -07003906 /**
3907 * Initialize the default http proxy in this process for the reasons we set the time zone.
3908 */
3909 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08003910 if (b != null) {
3911 // In pre-boot mode (doing initial launch to collect password), not
3912 // all system is up. This includes the connectivity service, so don't
3913 // crash if we can't get it.
3914 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
3915 try {
3916 ProxyProperties proxyProperties = service.getProxy();
3917 Proxy.setHttpProxySystemProperty(proxyProperties);
3918 } catch (RemoteException e) {}
3919 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07003920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 if (data.instrumentationName != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08003922 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003923 appContext.init(data.info, null, this);
3924 InstrumentationInfo ii = null;
3925 try {
3926 ii = appContext.getPackageManager().
3927 getInstrumentationInfo(data.instrumentationName, 0);
3928 } catch (PackageManager.NameNotFoundException e) {
3929 }
3930 if (ii == null) {
3931 throw new RuntimeException(
3932 "Unable to find instrumentation info for: "
3933 + data.instrumentationName);
3934 }
3935
3936 mInstrumentationAppDir = ii.sourceDir;
3937 mInstrumentationAppPackage = ii.packageName;
3938 mInstrumentedAppDir = data.info.getAppDir();
3939
3940 ApplicationInfo instrApp = new ApplicationInfo();
3941 instrApp.packageName = ii.packageName;
3942 instrApp.sourceDir = ii.sourceDir;
3943 instrApp.publicSourceDir = ii.publicSourceDir;
3944 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07003945 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003946 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08003948 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003949 instrContext.init(pi, null, this);
3950
3951 try {
3952 java.lang.ClassLoader cl = instrContext.getClassLoader();
3953 mInstrumentation = (Instrumentation)
3954 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
3955 } catch (Exception e) {
3956 throw new RuntimeException(
3957 "Unable to instantiate instrumentation "
3958 + data.instrumentationName + ": " + e.toString(), e);
3959 }
3960
3961 mInstrumentation.init(this, instrContext, appContext,
3962 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
3963
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003964 if (mProfiler.profileFile != null && !ii.handleProfiling
3965 && mProfiler.profileFd == null) {
3966 mProfiler.handlingProfiling = true;
3967 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003968 file.getParentFile().mkdirs();
3969 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
3970 }
3971
3972 try {
3973 mInstrumentation.onCreate(data.instrumentationArgs);
3974 }
3975 catch (Exception e) {
3976 throw new RuntimeException(
3977 "Exception thrown in onCreate() of "
3978 + data.instrumentationName + ": " + e.toString(), e);
3979 }
3980
3981 } else {
3982 mInstrumentation = new Instrumentation();
3983 }
3984
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003985 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08003986 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003987 }
3988
Christopher Tate181fafa2009-05-14 11:12:14 -07003989 // If the app is being launched for full backup or restore, bring it up in
3990 // a restricted environment with the base application class.
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003991 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003992 mInitialApplication = app;
3993
Christopher Tate75a99702011-05-18 16:28:19 -07003994 // don't bring up providers in restricted mode; they may depend on the
3995 // app's custom Application class
3996 if (!data.restrictedBackupMode){
3997 List<ProviderInfo> providers = data.providers;
3998 if (providers != null) {
3999 installContentProviders(app, providers);
4000 // For process that contains content providers, we want to
4001 // ensure that the JIT is enabled "at some point".
4002 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004004 }
4005
4006 try {
4007 mInstrumentation.callApplicationOnCreate(app);
4008 } catch (Exception e) {
4009 if (!mInstrumentation.onException(app, e)) {
4010 throw new RuntimeException(
4011 "Unable to create application " + app.getClass().getName()
4012 + ": " + e.toString(), e);
4013 }
4014 }
4015 }
4016
4017 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4018 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004019 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4020 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004021 Debug.stopMethodTracing();
4022 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004023 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 // + ", app thr: " + mAppThread);
4025 try {
4026 am.finishInstrumentation(mAppThread, resultCode, results);
4027 } catch (RemoteException ex) {
4028 }
4029 }
4030
Romain Guy65b345f2011-07-27 18:51:50 -07004031 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004032 Context context, List<ProviderInfo> providers) {
4033 final ArrayList<IActivityManager.ContentProviderHolder> results =
4034 new ArrayList<IActivityManager.ContentProviderHolder>();
4035
4036 Iterator<ProviderInfo> i = providers.iterator();
4037 while (i.hasNext()) {
4038 ProviderInfo cpi = i.next();
4039 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07004040 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004041 buf.append(cpi.authority);
4042 buf.append(": ");
4043 buf.append(cpi.name);
4044 Log.i(TAG, buf.toString());
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004045 IContentProvider cp = installProvider(context, null, cpi,
4046 false /*noisy*/, true /*noReleaseNeeded*/);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004047 if (cp != null) {
4048 IActivityManager.ContentProviderHolder cph =
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004049 new IActivityManager.ContentProviderHolder(cpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 cph.provider = cp;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004051 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004052 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 }
4054 }
4055
4056 try {
4057 ActivityManagerNative.getDefault().publishContentProviders(
4058 getApplicationThread(), results);
4059 } catch (RemoteException ex) {
4060 }
4061 }
4062
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004063 public final IContentProvider acquireProvider(Context c, String name) {
4064 IContentProvider provider = acquireExistingProvider(c, name);
4065 if (provider != null) {
4066 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 }
4068
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004069 // There is a possible race here. Another thread may try to acquire
4070 // the same provider at the same time. When this happens, we want to ensure
4071 // that the first one wins.
4072 // Note that we cannot hold the lock while acquiring and installing the
4073 // provider since it might take a long time to run and it could also potentially
4074 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004075 IActivityManager.ContentProviderHolder holder = null;
4076 try {
4077 holder = ActivityManagerNative.getDefault().getContentProvider(
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004078 getApplicationThread(), name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004079 } catch (RemoteException ex) {
4080 }
4081 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004082 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 return null;
4084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004086 // Install provider will increment the reference count for us, and break
4087 // any ties in the race.
4088 provider = installProvider(c, holder.provider, holder.info,
4089 true /*noisy*/, holder.noReleaseNeeded);
4090 if (holder.provider != null && provider != holder.provider) {
4091 if (localLOGV) {
4092 Slog.v(TAG, "acquireProvider: lost the race, releasing extraneous "
4093 + "reference to the content provider");
4094 }
4095 try {
4096 ActivityManagerNative.getDefault().removeContentProvider(
4097 getApplicationThread(), name);
4098 } catch (RemoteException ex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 }
4100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 return provider;
4102 }
4103
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004104 public final IContentProvider acquireExistingProvider(Context c, String name) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004105 synchronized (mProviderMap) {
4106 ProviderClientRecord pr = mProviderMap.get(name);
4107 if (pr == null) {
4108 return null;
4109 }
4110
4111 IContentProvider provider = pr.mProvider;
4112 IBinder jBinder = provider.asBinder();
4113
4114 // Only increment the ref count if we have one. If we don't then the
4115 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004116 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004117 if (prc != null) {
4118 prc.count += 1;
4119 if (prc.count == 1) {
4120 if (localLOGV) {
4121 Slog.v(TAG, "acquireExistingProvider: "
4122 + "snatched provider from the jaws of death");
4123 }
4124 // Because the provider previously had a reference count of zero,
4125 // it was scheduled to be removed. Cancel that.
4126 mH.removeMessages(H.REMOVE_PROVIDER, provider);
4127 }
4128 }
4129 return provider;
4130 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004131 }
4132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 public final boolean releaseProvider(IContentProvider provider) {
4134 if(provider == null) {
4135 return false;
4136 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004139 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004141 if (prc == null) {
4142 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004143 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004144 }
4145
4146 if (prc.count == 0) {
4147 if (localLOGV) Slog.v(TAG, "releaseProvider: ref count already 0, how?");
4148 return false;
4149 }
4150
4151 prc.count -= 1;
4152 if (prc.count == 0) {
4153 // Schedule the actual remove asynchronously, since we don't know the context
4154 // this will be called in.
4155 // TODO: it would be nice to post a delayed message, so
4156 // if we come back and need the same provider quickly
4157 // we will still have it available.
4158 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, provider);
4159 mH.sendMessage(msg);
4160 }
4161 return true;
4162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004163 }
4164
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004165 final void completeRemoveProvider(IContentProvider provider) {
4166 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004167 String remoteProviderName = null;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004168 synchronized(mProviderMap) {
4169 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004170 if (prc == null) {
4171 // Either no release is needed (so we shouldn't be here) or the
4172 // provider was already released.
4173 if (localLOGV) Slog.v(TAG, "completeRemoveProvider: release not needed");
4174 return;
4175 }
4176
4177 if (prc.count != 0) {
4178 // There was a race! Some other client managed to acquire
4179 // the provider before the removal was completed.
4180 // Abort the removal. We will do it later.
4181 if (localLOGV) Slog.v(TAG, "completeRemoveProvider: lost the race, "
4182 + "provider still in use");
4183 return;
4184 }
4185
4186 mProviderRefCountMap.remove(jBinder);
4187
4188 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
4189 while (iter.hasNext()) {
4190 ProviderClientRecord pr = iter.next();
4191 IBinder myBinder = pr.mProvider.asBinder();
4192 if (myBinder == jBinder) {
4193 iter.remove();
4194 if (pr.mLocalProvider == null) {
4195 myBinder.unlinkToDeath(pr, 0);
4196 if (remoteProviderName == null) {
4197 remoteProviderName = pr.mName;
4198 }
4199 }
4200 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004201 }
4202 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004203
4204 if (remoteProviderName != null) {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004205 try {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004206 if (localLOGV) {
4207 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4208 + "removeContentProvider(" + remoteProviderName + ")");
4209 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004210 ActivityManagerNative.getDefault().removeContentProvider(
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004211 getApplicationThread(), remoteProviderName);
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004212 } catch (RemoteException e) {
4213 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004214 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004215 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004217
4218 final void removeDeadProvider(String name, IContentProvider provider) {
4219 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004220 ProviderClientRecord pr = mProviderMap.get(name);
Jeff Brown2a4c5ac2011-08-23 11:58:26 -07004221 if (pr != null && pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004222 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004223 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004224 if (removed != null) {
4225 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
4226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 }
4228 }
4229 }
4230
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004231 /**
4232 * Installs the provider.
4233 *
4234 * Providers that are local to the process or that come from the system server
4235 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4236 * Other remote providers are reference counted. The initial reference count
4237 * for all reference counted providers is one. Providers that are not reference
4238 * counted do not have a reference count (at all).
4239 *
4240 * This method detects when a provider has already been installed. When this happens,
4241 * it increments the reference count of the existing provider (if appropriate)
4242 * and returns the existing provider. This can happen due to concurrent
4243 * attempts to acquire the same provider.
4244 */
Romain Guy65b345f2011-07-27 18:51:50 -07004245 private IContentProvider installProvider(Context context,
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004246 IContentProvider provider, ProviderInfo info,
4247 boolean noisy, boolean noReleaseNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 ContentProvider localProvider = null;
4249 if (provider == null) {
4250 if (noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004251 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004252 + info.name);
4253 }
4254 Context c = null;
4255 ApplicationInfo ai = info.applicationInfo;
4256 if (context.getPackageName().equals(ai.packageName)) {
4257 c = context;
4258 } else if (mInitialApplication != null &&
4259 mInitialApplication.getPackageName().equals(ai.packageName)) {
4260 c = mInitialApplication;
4261 } else {
4262 try {
4263 c = context.createPackageContext(ai.packageName,
4264 Context.CONTEXT_INCLUDE_CODE);
4265 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004266 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 }
4268 }
4269 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004270 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004271 ai.packageName +
4272 " while loading content provider " +
4273 info.name);
4274 return null;
4275 }
4276 try {
4277 final java.lang.ClassLoader cl = c.getClassLoader();
4278 localProvider = (ContentProvider)cl.
4279 loadClass(info.name).newInstance();
4280 provider = localProvider.getIContentProvider();
4281 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004282 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283 info.name + " from sourceDir " +
4284 info.applicationInfo.sourceDir);
4285 return null;
4286 }
Joe Onorato43a17652011-04-06 19:22:23 -07004287 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 TAG, "Instantiating local provider " + info.name);
4289 // XXX Need to create the correct context for this provider.
4290 localProvider.attachInfo(c, info);
4291 } catch (java.lang.Exception e) {
4292 if (!mInstrumentation.onException(null, e)) {
4293 throw new RuntimeException(
4294 "Unable to get provider " + info.name
4295 + ": " + e.toString(), e);
4296 }
4297 return null;
4298 }
4299 } else if (localLOGV) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004300 Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301 + info.name);
4302 }
4303
4304 synchronized (mProviderMap) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004305 // There is a possibility that this thread raced with another thread to
4306 // add the provider. If we find another thread got there first then we
4307 // just get out of the way and return the original provider.
4308 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 String names[] = PATTERN_SEMICOLON.split(info.authority);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004310 for (int i = 0; i < names.length; i++) {
4311 ProviderClientRecord pr = mProviderMap.get(names[i]);
4312 if (pr != null) {
4313 if (localLOGV) {
4314 Slog.v(TAG, "installProvider: lost the race, "
4315 + "using existing named provider");
4316 }
4317 provider = pr.mProvider;
4318 } else {
4319 pr = new ProviderClientRecord(names[i], provider, localProvider);
4320 if (localProvider == null) {
4321 try {
4322 jBinder.linkToDeath(pr, 0);
4323 } catch (RemoteException e) {
4324 // Provider already dead. Bail out of here without making
4325 // any changes to the provider map or other data structures.
4326 return null;
4327 }
4328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 mProviderMap.put(names[i], pr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004330 }
4331 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 if (localProvider != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004334 ProviderClientRecord pr = mLocalProviders.get(jBinder);
4335 if (pr != null) {
4336 if (localLOGV) {
4337 Slog.v(TAG, "installProvider: lost the race, "
4338 + "using existing local provider");
4339 }
4340 provider = pr.mProvider;
4341 } else {
4342 pr = new ProviderClientRecord(null, provider, localProvider);
4343 mLocalProviders.put(jBinder, pr);
4344 }
4345 }
4346
4347 if (!noReleaseNeeded) {
4348 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4349 if (prc != null) {
4350 if (localLOGV) {
4351 Slog.v(TAG, "installProvider: lost the race, incrementing ref count");
4352 }
4353 prc.count += 1;
4354 if (prc.count == 1) {
4355 if (localLOGV) {
4356 Slog.v(TAG, "installProvider: "
4357 + "snatched provider from the jaws of death");
4358 }
4359 // Because the provider previously had a reference count of zero,
4360 // it was scheduled to be removed. Cancel that.
4361 mH.removeMessages(H.REMOVE_PROVIDER, provider);
4362 }
4363 } else {
4364 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
4365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004366 }
4367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 return provider;
4369 }
4370
Romain Guy65b345f2011-07-27 18:51:50 -07004371 private void attach(boolean system) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 sThreadLocal.set(this);
4373 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004375 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004376 public void run() {
4377 ensureJitEnabled();
4378 }
4379 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004380 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4381 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4382 IActivityManager mgr = ActivityManagerNative.getDefault();
4383 try {
4384 mgr.attachApplication(mAppThread);
4385 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004386 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004387 }
4388 } else {
4389 // Don't set application object here -- if the system crashes,
4390 // we can't display an alert, we just want to die die die.
4391 android.ddm.DdmHandleAppName.setAppName("system_process");
4392 try {
4393 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004394 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 context.init(getSystemContext().mPackageInfo, null, this);
4396 Application app = Instrumentation.newApplication(Application.class, context);
4397 mAllApplications.add(app);
4398 mInitialApplication = app;
4399 app.onCreate();
4400 } catch (Exception e) {
4401 throw new RuntimeException(
4402 "Unable to instantiate Application():" + e.toString(), e);
4403 }
4404 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004405
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004406 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004407 public void onConfigurationChanged(Configuration newConfig) {
4408 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004409 // We need to apply this change to the resources
4410 // immediately, because upon returning the view
4411 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004412 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004413 // This actually changed the resources! Tell
4414 // everyone about it.
4415 if (mPendingConfiguration == null ||
4416 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4417 mPendingConfiguration = newConfig;
4418
4419 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4420 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004421 }
4422 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004423 }
4424 public void onLowMemory() {
4425 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004426 public void onTrimMemory(int level) {
4427 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004428 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004429 }
4430
Romain Guy5e9120d2012-01-30 12:17:22 -08004431 public static ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004432 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004433 ActivityThread thread = new ActivityThread();
4434 thread.attach(true);
4435 return thread;
4436 }
4437
Jeff Brown10e89712011-07-08 18:52:57 -07004438 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004439 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004440 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004441 }
4442 }
4443
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004444 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004445 synchronized (mPackages) {
4446 if (mCoreSettings != null) {
4447 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004448 } else {
4449 return defaultValue;
4450 }
4451 }
4452 }
4453
Romain Guy65b345f2011-07-27 18:51:50 -07004454 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004455 SamplingProfilerIntegration.start();
4456
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004457 // CloseGuard defaults to true and can be quite spammy. We
4458 // disable it here, but selectively enable it later (via
4459 // StrictMode) on debug builds, but using DropBox, not logs.
4460 CloseGuard.setEnabled(false);
4461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004462 Process.setArgV0("<pre-initialized>");
4463
4464 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07004465 if (sMainThreadHandler == null) {
4466 sMainThreadHandler = new Handler();
4467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468
4469 ActivityThread thread = new ActivityThread();
4470 thread.attach(false);
4471
Romain Guy5e9120d2012-01-30 12:17:22 -08004472 AsyncTask.init();
4473
Dianne Hackborn287952c2010-09-22 22:34:31 -07004474 if (false) {
4475 Looper.myLooper().setMessageLogging(new
4476 LogPrinter(Log.DEBUG, "ActivityThread"));
4477 }
4478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004479 Looper.loop();
4480
Jeff Brown10e89712011-07-08 18:52:57 -07004481 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004482 }
4483}