blob: 9e88bc6e8442a6b8e06c5187796534b50a03bb21 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.os.Bundle;
51import android.os.Debug;
52import android.os.Handler;
53import android.os.IBinder;
54import android.os.Looper;
55import android.os.Message;
56import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070057import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.os.Process;
59import android.os.RemoteException;
60import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070061import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.os.SystemClock;
63import android.util.AndroidRuntimeException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.util.DisplayMetrics;
65import android.util.EventLog;
66import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070067import android.util.LogPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080068import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070070import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.View;
72import android.view.ViewDebug;
73import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070074import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.Window;
76import android.view.WindowManager;
77import android.view.WindowManagerImpl;
78
79import com.android.internal.os.BinderInternal;
80import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070081import com.android.internal.os.SamplingProfilerIntegration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl;
84
85import java.io.File;
86import java.io.FileDescriptor;
87import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070088import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.io.PrintWriter;
90import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -070091import java.net.InetAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import java.util.ArrayList;
93import java.util.HashMap;
94import java.util.Iterator;
95import java.util.List;
96import java.util.Locale;
97import java.util.Map;
98import java.util.TimeZone;
99import java.util.regex.Pattern;
100
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800101import dalvik.system.CloseGuard;
Bob Leee5408332009-09-04 18:31:17 -0700102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103final class SuperNotCalledException extends AndroidRuntimeException {
104 public SuperNotCalledException(String msg) {
105 super(msg);
106 }
107}
108
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700109final class RemoteServiceException extends AndroidRuntimeException {
110 public RemoteServiceException(String msg) {
111 super(msg);
112 }
113}
114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115/**
116 * This manages the execution of the main thread in an
117 * application process, scheduling and executing activities,
118 * broadcasts, and other operations on it as the activity
119 * manager requests.
120 *
121 * {@hide}
122 */
123public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700124 /** @hide */
125 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700126 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700127 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700128 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700129 /** @hide */
130 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700131 private static final boolean DEBUG_RESULTS = false;
Christopher Tate4a627c72011-04-01 14:43:32 -0700132 private static final boolean DEBUG_BACKUP = true;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700133 private static final boolean DEBUG_CONFIGURATION = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
135 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
136 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
137 private static final int LOG_ON_PAUSE_CALLED = 30021;
138 private static final int LOG_ON_RESUME_CALLED = 30022;
139
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700140 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700141
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700142 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700144 final ApplicationThread mAppThread = new ApplicationThread();
145 final Looper mLooper = Looper.myLooper();
146 final H mH = new H();
147 final HashMap<IBinder, ActivityClientRecord> mActivities
148 = new HashMap<IBinder, ActivityClientRecord>();
149 // List of new activities (via ActivityRecord.nextIdle) that should
150 // be reported when next we idle.
151 ActivityClientRecord mNewActivities = null;
152 // Number of activities that are currently visible on-screen.
153 int mNumVisibleActivities = 0;
154 final HashMap<IBinder, Service> mServices
155 = new HashMap<IBinder, Service>();
156 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700157 Profiler mProfiler;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700158 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700159 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700160 Configuration mResConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700161 CompatibilityInfo mResCompatibilityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700162 Application mInitialApplication;
163 final ArrayList<Application> mAllApplications
164 = new ArrayList<Application>();
165 // set of instantiated backup agents, keyed by package name
166 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Romain Guy65b345f2011-07-27 18:51:50 -0700167 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700168 Instrumentation mInstrumentation;
169 String mInstrumentationAppDir = null;
170 String mInstrumentationAppPackage = null;
171 String mInstrumentedAppDir = null;
172 boolean mSystemThread = false;
173 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700175 // These can be accessed by multiple threads; mPackages is the lock.
176 // XXX For now we keep around information about all packages we have
177 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800178 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700179 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800180 // which means this lock gets held while the activity and window managers
181 // holds their own lock. Thus you MUST NEVER call back into the activity manager
182 // or window manager or anything that depends on them while holding this lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700183 final HashMap<String, WeakReference<LoadedApk>> mPackages
184 = new HashMap<String, WeakReference<LoadedApk>>();
185 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
186 = new HashMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700187 final HashMap<CompatibilityInfo, DisplayMetrics> mDisplayMetrics
188 = new HashMap<CompatibilityInfo, DisplayMetrics>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700189 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
190 = new HashMap<ResourcesKey, WeakReference<Resources> >();
191 final ArrayList<ActivityClientRecord> mRelaunchingActivities
192 = new ArrayList<ActivityClientRecord>();
193 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700195 // The lock of mProviderMap protects the following variables.
196 final HashMap<String, ProviderClientRecord> mProviderMap
197 = new HashMap<String, ProviderClientRecord>();
198 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
199 = new HashMap<IBinder, ProviderRefCount>();
200 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
201 = new HashMap<IBinder, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202
Jeff Hamilton52d32032011-01-08 15:31:26 -0600203 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
204 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
205
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700206 final GcIdler mGcIdler = new GcIdler();
207 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700209 static Handler sMainThreadHandler; // set once in main()
210
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800211 Bundle mCoreSettings = null;
212
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400213 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700215 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 Intent intent;
217 Bundle state;
218 Activity activity;
219 Window window;
220 Activity parent;
221 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700222 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 boolean paused;
224 boolean stopped;
225 boolean hideForNow;
226 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700227 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700228 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700230 String profileFile;
231 ParcelFileDescriptor profileFd;
232 boolean autoStopProfiler;
233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400235 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700236 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237
238 List<ResultInfo> pendingResults;
239 List<Intent> pendingIntents;
240
241 boolean startsNotResumed;
242 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800243 int pendingConfigChanges;
244 boolean onlyLocalRequest;
245
246 View mPendingRemoveWindow;
247 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700249 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 parent = null;
251 embeddedID = null;
252 paused = false;
253 stopped = false;
254 hideForNow = false;
255 nextIdle = null;
256 }
257
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800258 public boolean isPreHoneycomb() {
259 if (activity != null) {
260 return activity.getApplicationInfo().targetSdkVersion
261 < android.os.Build.VERSION_CODES.HONEYCOMB;
262 }
263 return false;
264 }
265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 public String toString() {
267 ComponentName componentName = intent.getComponent();
268 return "ActivityRecord{"
269 + Integer.toHexString(System.identityHashCode(this))
270 + " token=" + token + " " + (componentName == null
271 ? "no component name" : componentName.toShortString())
272 + "}";
273 }
274 }
275
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400276 final class ProviderClientRecord implements IBinder.DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 final String mName;
278 final IContentProvider mProvider;
279 final ContentProvider mLocalProvider;
280
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700281 ProviderClientRecord(String name, IContentProvider provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 ContentProvider localProvider) {
283 mName = name;
284 mProvider = provider;
285 mLocalProvider = localProvider;
286 }
287
288 public void binderDied() {
289 removeDeadProvider(mName, mProvider);
290 }
291 }
292
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400293 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 List<Intent> intents;
295 IBinder token;
296 public String toString() {
297 return "NewIntentData{intents=" + intents + " token=" + token + "}";
298 }
299 }
300
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400301 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700302 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
303 boolean ordered, boolean sticky, IBinder token) {
304 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky, token);
305 this.intent = intent;
306 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 Intent intent;
309 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400310 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 public String toString() {
312 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700313 info.packageName + " resultCode=" + getResultCode()
314 + " resultData=" + getResultData() + " resultExtras="
315 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 }
317 }
318
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400319 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700320 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400321 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700322 int backupMode;
323 public String toString() {
324 return "CreateBackupAgentData{appInfo=" + appInfo
325 + " backupAgent=" + appInfo.backupAgentName
326 + " mode=" + backupMode + "}";
327 }
328 }
Bob Leee5408332009-09-04 18:31:17 -0700329
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400330 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 IBinder token;
332 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400333 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 Intent intent;
335 public String toString() {
336 return "CreateServiceData{token=" + token + " className="
337 + info.name + " packageName=" + info.packageName
338 + " intent=" + intent + "}";
339 }
340 }
341
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400342 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 IBinder token;
344 Intent intent;
345 boolean rebind;
346 public String toString() {
347 return "BindServiceData{token=" + token + " intent=" + intent + "}";
348 }
349 }
350
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400351 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700353 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700355 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 Intent args;
357 public String toString() {
358 return "ServiceArgsData{token=" + token + " startId=" + startId
359 + " args=" + args + "}";
360 }
361 }
362
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400363 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700364 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 String processName;
366 ApplicationInfo appInfo;
367 List<ProviderInfo> providers;
368 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 Bundle instrumentationArgs;
370 IInstrumentationWatcher instrumentationWatcher;
371 int debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700372 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700373 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400375 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700376
377 /** Initial values for {@link Profiler}. */
378 String initProfileFile;
379 ParcelFileDescriptor initProfileFd;
380 boolean initAutoStopProfiler;
381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 public String toString() {
383 return "AppBindData{appInfo=" + appInfo + "}";
384 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700385 }
386
387 static final class Profiler {
388 String profileFile;
389 ParcelFileDescriptor profileFd;
390 boolean autoStopProfiler;
391 boolean profiling;
392 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700393 public void setProfiler(String file, ParcelFileDescriptor fd) {
394 if (profiling) {
395 if (fd != null) {
396 try {
397 fd.close();
398 } catch (IOException e) {
399 }
400 }
401 return;
402 }
403 if (profileFd != null) {
404 try {
405 profileFd.close();
406 } catch (IOException e) {
407 }
408 }
409 profileFile = file;
410 profileFd = fd;
411 }
412 public void startProfiling() {
413 if (profileFd == null || profiling) {
414 return;
415 }
416 try {
417 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
418 8 * 1024 * 1024, 0);
419 profiling = true;
420 } catch (RuntimeException e) {
421 Slog.w(TAG, "Profiling failed on path " + profileFile);
422 try {
423 profileFd.close();
424 profileFd = null;
425 } catch (IOException e2) {
426 Slog.w(TAG, "Failure closing profile fd", e2);
427 }
428 }
429 }
430 public void stopProfiling() {
431 if (profiling) {
432 profiling = false;
433 Debug.stopMethodTracing();
434 if (profileFd != null) {
435 try {
436 profileFd.close();
437 } catch (IOException e) {
438 }
439 }
440 profileFd = null;
441 profileFile = null;
442 }
443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
445
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400446 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700447 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700448 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800449 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 }
452
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400453 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 IBinder token;
455 List<ResultInfo> results;
456 public String toString() {
457 return "ResultData{token=" + token + " results" + results + "}";
458 }
459 }
460
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400461 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800462 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 String what;
464 String who;
465 }
466
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400467 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700468 String path;
469 ParcelFileDescriptor fd;
470 }
471
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400472 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700473 String path;
474 ParcelFileDescriptor fd;
475 }
476
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400477 static final class UpdateCompatibilityData {
478 String pkg;
479 CompatibilityInfo info;
480 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700481
Romain Guy65b345f2011-07-27 18:51:50 -0700482 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700483
Romain Guy65b345f2011-07-27 18:51:50 -0700484 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700485 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
486 private static final String ONE_COUNT_COLUMN = "%21s %8d";
487 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
488 private static final String TWO_COUNT_COLUMNS_DB = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700489 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 // Formatting for checkin service - update version if row format changes
492 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700493
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700494 private void updatePendingConfiguration(Configuration config) {
495 synchronized (mPackages) {
496 if (mPendingConfiguration == null ||
497 mPendingConfiguration.isOtherSeqNewer(config)) {
498 mPendingConfiguration = config;
499 }
500 }
501 }
502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 public final void schedulePauseActivity(IBinder token, boolean finished,
504 boolean userLeaving, int configChanges) {
505 queueOrSendMessage(
506 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
507 token,
508 (userLeaving ? 1 : 0),
509 configChanges);
510 }
511
512 public final void scheduleStopActivity(IBinder token, boolean showWindow,
513 int configChanges) {
514 queueOrSendMessage(
515 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
516 token, 0, configChanges);
517 }
518
519 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
520 queueOrSendMessage(
521 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
522 token);
523 }
524
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800525 public final void scheduleSleeping(IBinder token, boolean sleeping) {
526 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
527 }
528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
530 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
531 }
532
533 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
534 ResultData res = new ResultData();
535 res.token = token;
536 res.results = results;
537 queueOrSendMessage(H.SEND_RESULT, res);
538 }
539
540 // we use token to identify this activity without having to send the
541 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700542 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700543 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
544 Bundle state, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700545 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
546 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700547 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548
549 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700550 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 r.intent = intent;
552 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400553 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 r.state = state;
555
556 r.pendingResults = pendingResults;
557 r.pendingIntents = pendingNewIntents;
558
559 r.startsNotResumed = notResumed;
560 r.isForward = isForward;
561
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700562 r.profileFile = profileName;
563 r.profileFd = profileFd;
564 r.autoStopProfiler = autoStopProfiler;
565
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700566 updatePendingConfiguration(curConfig);
567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
569 }
570
571 public final void scheduleRelaunchActivity(IBinder token,
572 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800573 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800574 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
575 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 }
577
578 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
579 NewIntentData data = new NewIntentData();
580 data.intents = intents;
581 data.token = token;
582
583 queueOrSendMessage(H.NEW_INTENT, data);
584 }
585
586 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
587 int configChanges) {
588 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
589 configChanges);
590 }
591
592 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400593 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
594 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700595 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
596 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400598 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 queueOrSendMessage(H.RECEIVER, r);
600 }
601
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400602 public final void scheduleCreateBackupAgent(ApplicationInfo app,
603 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700604 CreateBackupAgentData d = new CreateBackupAgentData();
605 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400606 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700607 d.backupMode = backupMode;
608
609 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
610 }
611
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400612 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
613 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700614 CreateBackupAgentData d = new CreateBackupAgentData();
615 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400616 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700617
618 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
619 }
620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400622 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 CreateServiceData s = new CreateServiceData();
624 s.token = token;
625 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400626 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627
628 queueOrSendMessage(H.CREATE_SERVICE, s);
629 }
630
631 public final void scheduleBindService(IBinder token, Intent intent,
632 boolean rebind) {
633 BindServiceData s = new BindServiceData();
634 s.token = token;
635 s.intent = intent;
636 s.rebind = rebind;
637
638 queueOrSendMessage(H.BIND_SERVICE, s);
639 }
640
641 public final void scheduleUnbindService(IBinder token, Intent intent) {
642 BindServiceData s = new BindServiceData();
643 s.token = token;
644 s.intent = intent;
645
646 queueOrSendMessage(H.UNBIND_SERVICE, s);
647 }
648
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700649 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700650 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 ServiceArgsData s = new ServiceArgsData();
652 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700653 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700655 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 s.args = args;
657
658 queueOrSendMessage(H.SERVICE_ARGS, s);
659 }
660
661 public final void scheduleStopService(IBinder token) {
662 queueOrSendMessage(H.STOP_SERVICE, token);
663 }
664
665 public final void bindApplication(String processName,
666 ApplicationInfo appInfo, List<ProviderInfo> providers,
667 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700668 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700670 int debugMode, boolean isRestrictedBackupMode, boolean persistent,
671 Configuration config, CompatibilityInfo compatInfo,
672 Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673
674 if (services != null) {
675 // Setup the service cache in the ServiceManager
676 ServiceManager.initServiceCache(services);
677 }
678
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800679 setCoreSettings(coreSettings);
680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 AppBindData data = new AppBindData();
682 data.processName = processName;
683 data.appInfo = appInfo;
684 data.providers = providers;
685 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 data.instrumentationArgs = instrumentationArgs;
687 data.instrumentationWatcher = instrumentationWatcher;
688 data.debugMode = debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700689 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700690 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400692 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700693 data.initProfileFile = profileFile;
694 data.initProfileFd = profileFd;
695 data.initAutoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 queueOrSendMessage(H.BIND_APPLICATION, data);
697 }
698
699 public final void scheduleExit() {
700 queueOrSendMessage(H.EXIT_APPLICATION, null);
701 }
702
Christopher Tate5e1ab332009-09-01 20:32:49 -0700703 public final void scheduleSuicide() {
704 queueOrSendMessage(H.SUICIDE, null);
705 }
706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 public void requestThumbnail(IBinder token) {
708 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
709 }
710
711 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700712 updatePendingConfiguration(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
714 }
715
716 public void updateTimeZone() {
717 TimeZone.setDefault(null);
718 }
719
Robert Greenwalt03595d02010-11-02 14:08:23 -0700720 public void clearDnsCache() {
721 // a non-standard API to get this to libcore
722 InetAddress.clearDnsCache();
723 }
724
Robert Greenwalt434203a2010-10-11 16:00:27 -0700725 public void setHttpProxy(String host, String port, String exclList) {
726 Proxy.setHttpProxySystemProperty(host, port, exclList);
727 }
728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 public void processInBackground() {
730 mH.removeMessages(H.GC_WHEN_IDLE);
731 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
732 }
733
734 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700735 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700736 try {
737 data.fd = ParcelFileDescriptor.dup(fd);
738 data.token = servicetoken;
739 data.args = args;
740 queueOrSendMessage(H.DUMP_SERVICE, data);
741 } catch (IOException e) {
742 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 }
744 }
745
746 // This function exists to make sure all receiver dispatching is
747 // correctly ordered, since these are one-way calls and the binder driver
748 // applies transaction ordering per object for such calls.
749 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700750 int resultCode, String dataStr, Bundle extras, boolean ordered,
751 boolean sticky) throws RemoteException {
752 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 }
Bob Leee5408332009-09-04 18:31:17 -0700754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 public void scheduleLowMemory() {
756 queueOrSendMessage(H.LOW_MEMORY, null);
757 }
758
759 public void scheduleActivityConfigurationChanged(IBinder token) {
760 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
761 }
762
Romain Guy7eabe552011-07-21 14:56:34 -0700763 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
764 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700765 ProfilerControlData pcd = new ProfilerControlData();
766 pcd.path = path;
767 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700768 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800769 }
770
Andy McFadden824c5102010-07-09 16:26:57 -0700771 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
772 DumpHeapData dhd = new DumpHeapData();
773 dhd.path = path;
774 dhd.fd = fd;
775 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
776 }
777
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700778 public void setSchedulingGroup(int group) {
779 // Note: do this immediately, since going into the foreground
780 // should happen regardless of what pending work we have to do
781 // and the activity manager will wait for us to report back that
782 // we are done before sending us to the background.
783 try {
784 Process.setProcessGroup(Process.myPid(), group);
785 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800786 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700787 }
788 }
Bob Leee5408332009-09-04 18:31:17 -0700789
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700790 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
791 Debug.getMemoryInfo(outInfo);
792 }
Bob Leee5408332009-09-04 18:31:17 -0700793
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700794 public void dispatchPackageBroadcast(int cmd, String[] packages) {
795 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
796 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700797
798 public void scheduleCrash(String msg) {
799 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
800 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700801
Dianne Hackborn30d71892010-12-11 10:37:55 -0800802 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
803 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700804 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700805 try {
806 data.fd = ParcelFileDescriptor.dup(fd);
807 data.token = activitytoken;
808 data.prefix = prefix;
809 data.args = args;
810 queueOrSendMessage(H.DUMP_ACTIVITY, data);
811 } catch (IOException e) {
812 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700813 }
814 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700815
Marco Nelissen18cb2872011-11-15 11:19:53 -0800816 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
817 String[] args) {
818 DumpComponentInfo data = new DumpComponentInfo();
819 try {
820 data.fd = ParcelFileDescriptor.dup(fd);
821 data.token = providertoken;
822 data.args = args;
823 queueOrSendMessage(H.DUMP_PROVIDER, data);
824 } catch (IOException e) {
825 Slog.w(TAG, "dumpProvider failed", e);
826 }
827 }
828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700830 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
831 boolean all, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700832 FileOutputStream fout = new FileOutputStream(fd);
833 PrintWriter pw = new PrintWriter(fout);
834 try {
Dianne Hackbornb437e092011-08-05 17:50:29 -0700835 return dumpMemInfo(pw, checkin, all, args);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700836 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700837 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700838 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700839 }
840
Dianne Hackbornb437e092011-08-05 17:50:29 -0700841 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean all,
842 String[] args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 long nativeMax = Debug.getNativeHeapSize() / 1024;
844 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
845 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
846
847 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
848 Debug.getMemoryInfo(memInfo);
849
Dianne Hackbornb437e092011-08-05 17:50:29 -0700850 if (!all) {
851 return memInfo;
852 }
853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 Runtime runtime = Runtime.getRuntime();
855
856 long dalvikMax = runtime.totalMemory() / 1024;
857 long dalvikFree = runtime.freeMemory() / 1024;
858 long dalvikAllocated = dalvikMax - dalvikFree;
859 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700860 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700861 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
862 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 int globalAssetCount = AssetManager.getGlobalAssetCount();
864 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
865 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
866 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
867 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700868 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 long sqliteAllocated = SQLiteDebug.getHeapAllocatedSize() / 1024;
Vasu Noric3849202010-03-09 10:47:25 -0800870 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700873 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 // NOTE: if you change anything significant below, also consider changing
875 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700876 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 // Header
880 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
881 pw.print(Process.myPid()); pw.print(',');
882 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 // Heap info - max
885 pw.print(nativeMax); pw.print(',');
886 pw.print(dalvikMax); pw.print(',');
887 pw.print("N/A,");
888 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 // Heap info - allocated
891 pw.print(nativeAllocated); pw.print(',');
892 pw.print(dalvikAllocated); pw.print(',');
893 pw.print("N/A,");
894 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 // Heap info - free
897 pw.print(nativeFree); pw.print(',');
898 pw.print(dalvikFree); pw.print(',');
899 pw.print("N/A,");
900 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 // Heap info - proportional set size
903 pw.print(memInfo.nativePss); pw.print(',');
904 pw.print(memInfo.dalvikPss); pw.print(',');
905 pw.print(memInfo.otherPss); pw.print(',');
906 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700909 pw.print(memInfo.nativeSharedDirty); pw.print(',');
910 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
911 pw.print(memInfo.otherSharedDirty); pw.print(',');
912 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
913 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700916 pw.print(memInfo.nativePrivateDirty); pw.print(',');
917 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
918 pw.print(memInfo.otherPrivateDirty); pw.print(',');
919 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
920 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 // Object counts
923 pw.print(viewInstanceCount); pw.print(',');
924 pw.print(viewRootInstanceCount); pw.print(',');
925 pw.print(appContextInstanceCount); pw.print(',');
926 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 pw.print(globalAssetCount); pw.print(',');
929 pw.print(globalAssetManagerCount); pw.print(',');
930 pw.print(binderLocalObjectCount); pw.print(',');
931 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 pw.print(binderDeathObjectCount); pw.print(',');
934 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 // SQL
937 pw.print(sqliteAllocated); pw.print(',');
Vasu Noric3849202010-03-09 10:47:25 -0800938 pw.print(stats.memoryUsed / 1024); pw.print(',');
939 pw.print(stats.pageCacheOverflo / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700940 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800941 for (int i = 0; i < stats.dbStats.size(); i++) {
942 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700943 pw.print(','); pw.print(dbStats.dbName);
944 pw.print(','); pw.print(dbStats.pageSize);
945 pw.print(','); pw.print(dbStats.dbSize);
946 pw.print(','); pw.print(dbStats.lookaside);
947 pw.print(','); pw.print(dbStats.cache);
948 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800949 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700950 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700951
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700952 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 }
Bob Leee5408332009-09-04 18:31:17 -0700954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700956 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
957 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
958 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
959 "------");
960 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
961 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
962 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
963 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700965 int otherPss = memInfo.otherPss;
966 int otherSharedDirty = memInfo.otherSharedDirty;
967 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700969 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Romain Guy65b345f2011-07-27 18:51:50 -0700970 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700971 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
972 memInfo.getOtherPrivateDirty(i), "", "", "");
973 otherPss -= memInfo.getOtherPss(i);
974 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
975 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
976 }
977
978 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
979 otherPrivateDirty, "", "", "");
980 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
981 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
982 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
983 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984
985 pw.println(" ");
986 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -0700987 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 viewRootInstanceCount);
989
990 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
991 "Activities:", activityInstanceCount);
992
993 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
994 "AssetManagers:", globalAssetManagerCount);
995
996 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
997 "Proxy Binders:", binderProxyObjectCount);
998 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
999
1000 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 // SQLite mem info
1003 pw.println(" ");
1004 pw.println(" SQL");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001005 printRow(pw, TWO_COUNT_COLUMNS_DB, "heap:", sqliteAllocated, "MEMORY_USED:",
Vasu Noric3849202010-03-09 10:47:25 -08001006 stats.memoryUsed / 1024);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001007 printRow(pw, TWO_COUNT_COLUMNS_DB, "PAGECACHE_OVERFLOW:",
1008 stats.pageCacheOverflo / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001009 pw.println(" ");
1010 int N = stats.dbStats.size();
1011 if (N > 0) {
1012 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001013 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1014 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001015 for (int i = 0; i < N; i++) {
1016 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001017 printRow(pw, DB_INFO_FORMAT,
1018 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1019 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1020 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1021 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001022 }
1023 }
Bob Leee5408332009-09-04 18:31:17 -07001024
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001025 // Asset details.
1026 String assetAlloc = AssetManager.getAssetAllocations();
1027 if (assetAlloc != null) {
1028 pw.println(" ");
1029 pw.println(" Asset Allocations");
1030 pw.print(assetAlloc);
1031 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001032
1033 return memInfo;
1034 }
1035
1036 @Override
1037 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1038 dumpGraphicsInfo(fd);
Romain Guy65b345f2011-07-27 18:51:50 -07001039 WindowManagerImpl.getDefault().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 }
1041
1042 private void printRow(PrintWriter pw, String format, Object...objs) {
1043 pw.println(String.format(format, objs));
1044 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001045
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001046 public void setCoreSettings(Bundle coreSettings) {
1047 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001048 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001049
1050 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1051 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1052 ucd.pkg = pkg;
1053 ucd.info = info;
1054 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1055 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001056
1057 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001058 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001059 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 }
1062
Romain Guy65b345f2011-07-27 18:51:50 -07001063 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 public static final int LAUNCH_ACTIVITY = 100;
1065 public static final int PAUSE_ACTIVITY = 101;
1066 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1067 public static final int STOP_ACTIVITY_SHOW = 103;
1068 public static final int STOP_ACTIVITY_HIDE = 104;
1069 public static final int SHOW_WINDOW = 105;
1070 public static final int HIDE_WINDOW = 106;
1071 public static final int RESUME_ACTIVITY = 107;
1072 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001073 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 public static final int BIND_APPLICATION = 110;
1075 public static final int EXIT_APPLICATION = 111;
1076 public static final int NEW_INTENT = 112;
1077 public static final int RECEIVER = 113;
1078 public static final int CREATE_SERVICE = 114;
1079 public static final int SERVICE_ARGS = 115;
1080 public static final int STOP_SERVICE = 116;
1081 public static final int REQUEST_THUMBNAIL = 117;
1082 public static final int CONFIGURATION_CHANGED = 118;
1083 public static final int CLEAN_UP_CONTEXT = 119;
1084 public static final int GC_WHEN_IDLE = 120;
1085 public static final int BIND_SERVICE = 121;
1086 public static final int UNBIND_SERVICE = 122;
1087 public static final int DUMP_SERVICE = 123;
1088 public static final int LOW_MEMORY = 124;
1089 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1090 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001091 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001092 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001093 public static final int DESTROY_BACKUP_AGENT = 129;
1094 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001095 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001096 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001097 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001098 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001099 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001100 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001101 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001102 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001103 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001104 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001105 public static final int DUMP_PROVIDER = 141;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001107 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 switch (code) {
1109 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1110 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1111 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1112 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1113 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1114 case SHOW_WINDOW: return "SHOW_WINDOW";
1115 case HIDE_WINDOW: return "HIDE_WINDOW";
1116 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1117 case SEND_RESULT: return "SEND_RESULT";
1118 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1119 case BIND_APPLICATION: return "BIND_APPLICATION";
1120 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1121 case NEW_INTENT: return "NEW_INTENT";
1122 case RECEIVER: return "RECEIVER";
1123 case CREATE_SERVICE: return "CREATE_SERVICE";
1124 case SERVICE_ARGS: return "SERVICE_ARGS";
1125 case STOP_SERVICE: return "STOP_SERVICE";
1126 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1127 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1128 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1129 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1130 case BIND_SERVICE: return "BIND_SERVICE";
1131 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1132 case DUMP_SERVICE: return "DUMP_SERVICE";
1133 case LOW_MEMORY: return "LOW_MEMORY";
1134 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1135 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001136 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001137 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1138 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001139 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001140 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001141 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001142 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001143 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001144 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001145 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001146 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001147 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001148 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001149 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001150 case DUMP_PROVIDER: return "DUMP_PROVIDER";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 }
1152 }
1153 return "(unknown)";
1154 }
1155 public void handleMessage(Message msg) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001156 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 switch (msg.what) {
1158 case LAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001159 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160
1161 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001162 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001163 handleLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 } break;
1165 case RELAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001166 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001167 handleRelaunchActivity(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 } break;
1169 case PAUSE_ACTIVITY:
1170 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001171 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 break;
1173 case PAUSE_ACTIVITY_FINISHING:
1174 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
1175 break;
1176 case STOP_ACTIVITY_SHOW:
1177 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
1178 break;
1179 case STOP_ACTIVITY_HIDE:
1180 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
1181 break;
1182 case SHOW_WINDOW:
1183 handleWindowVisibility((IBinder)msg.obj, true);
1184 break;
1185 case HIDE_WINDOW:
1186 handleWindowVisibility((IBinder)msg.obj, false);
1187 break;
1188 case RESUME_ACTIVITY:
1189 handleResumeActivity((IBinder)msg.obj, true,
1190 msg.arg1 != 0);
1191 break;
1192 case SEND_RESULT:
1193 handleSendResult((ResultData)msg.obj);
1194 break;
1195 case DESTROY_ACTIVITY:
1196 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1197 msg.arg2, false);
1198 break;
1199 case BIND_APPLICATION:
1200 AppBindData data = (AppBindData)msg.obj;
1201 handleBindApplication(data);
1202 break;
1203 case EXIT_APPLICATION:
1204 if (mInitialApplication != null) {
1205 mInitialApplication.onTerminate();
1206 }
1207 Looper.myLooper().quit();
1208 break;
1209 case NEW_INTENT:
1210 handleNewIntent((NewIntentData)msg.obj);
1211 break;
1212 case RECEIVER:
1213 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001214 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 break;
1216 case CREATE_SERVICE:
1217 handleCreateService((CreateServiceData)msg.obj);
1218 break;
1219 case BIND_SERVICE:
1220 handleBindService((BindServiceData)msg.obj);
1221 break;
1222 case UNBIND_SERVICE:
1223 handleUnbindService((BindServiceData)msg.obj);
1224 break;
1225 case SERVICE_ARGS:
1226 handleServiceArgs((ServiceArgsData)msg.obj);
1227 break;
1228 case STOP_SERVICE:
1229 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001230 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 break;
1232 case REQUEST_THUMBNAIL:
1233 handleRequestThumbnail((IBinder)msg.obj);
1234 break;
1235 case CONFIGURATION_CHANGED:
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001236 handleConfigurationChanged((Configuration)msg.obj, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 break;
1238 case CLEAN_UP_CONTEXT:
1239 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1240 cci.context.performFinalCleanup(cci.who, cci.what);
1241 break;
1242 case GC_WHEN_IDLE:
1243 scheduleGcIdler();
1244 break;
1245 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001246 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 break;
1248 case LOW_MEMORY:
1249 handleLowMemory();
1250 break;
1251 case ACTIVITY_CONFIGURATION_CHANGED:
1252 handleActivityConfigurationChanged((IBinder)msg.obj);
1253 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001254 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001255 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001256 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001257 case CREATE_BACKUP_AGENT:
1258 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
1259 break;
1260 case DESTROY_BACKUP_AGENT:
1261 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
1262 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001263 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001264 Process.killProcess(Process.myPid());
1265 break;
1266 case REMOVE_PROVIDER:
1267 completeRemoveProvider((IContentProvider)msg.obj);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001268 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001269 case ENABLE_JIT:
1270 ensureJitEnabled();
1271 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001272 case DISPATCH_PACKAGE_BROADCAST:
1273 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
1274 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001275 case SCHEDULE_CRASH:
1276 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001277 case DUMP_HEAP:
1278 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1279 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001280 case DUMP_ACTIVITY:
1281 handleDumpActivity((DumpComponentInfo)msg.obj);
1282 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001283 case DUMP_PROVIDER:
1284 handleDumpProvider((DumpComponentInfo)msg.obj);
1285 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001286 case SLEEPING:
1287 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
1288 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001289 case SET_CORE_SETTINGS:
1290 handleSetCoreSettings((Bundle) msg.obj);
1291 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001292 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1293 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001294 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001295 case TRIM_MEMORY:
1296 handleTrimMemory(msg.arg1);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001297 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07001299 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 }
Bob Leee5408332009-09-04 18:31:17 -07001301
Brian Carlstromed7e0072011-03-24 13:27:57 -07001302 private void maybeSnapshot() {
1303 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001304 // convert the *private* ActivityThread.PackageInfo to *public* known
1305 // android.content.pm.PackageInfo
1306 String packageName = mBoundApplication.info.mPackageName;
1307 android.content.pm.PackageInfo packageInfo = null;
1308 try {
1309 Context context = getSystemContext();
1310 if(context == null) {
1311 Log.e(TAG, "cannot get a valid context");
1312 return;
1313 }
1314 PackageManager pm = context.getPackageManager();
1315 if(pm == null) {
1316 Log.e(TAG, "cannot get a valid PackageManager");
1317 return;
1318 }
1319 packageInfo = pm.getPackageInfo(
1320 packageName, PackageManager.GET_ACTIVITIES);
1321 } catch (NameNotFoundException e) {
1322 Log.e(TAG, "cannot get package info for " + packageName, e);
1323 }
1324 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001325 }
1326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 }
1328
Romain Guy65b345f2011-07-27 18:51:50 -07001329 private class Idler implements MessageQueue.IdleHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001331 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001332 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001333 if (mBoundApplication != null && mProfiler.profileFd != null
1334 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001335 stopProfiling = true;
1336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 if (a != null) {
1338 mNewActivities = null;
1339 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001340 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001342 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 TAG, "Reporting idle of " + a +
1344 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001345 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 if (a.activity != null && !a.activity.mFinished) {
1347 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001348 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001349 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001351 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 }
1353 }
1354 prev = a;
1355 a = a.nextIdle;
1356 prev.nextIdle = null;
1357 } while (a != null);
1358 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001359 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001360 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001361 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001362 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 return false;
1364 }
1365 }
1366
1367 final class GcIdler implements MessageQueue.IdleHandler {
1368 public final boolean queueIdle() {
1369 doGcIfNeeded();
1370 return false;
1371 }
1372 }
1373
Romain Guy65b345f2011-07-27 18:51:50 -07001374 private static class ResourcesKey {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001375 final private String mResDir;
1376 final private float mScale;
1377 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001378
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001379 ResourcesKey(String resDir, float scale) {
1380 mResDir = resDir;
1381 mScale = scale;
1382 mHash = mResDir.hashCode() << 2 + (int) (mScale * 2);
1383 }
Bob Leee5408332009-09-04 18:31:17 -07001384
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001385 @Override
1386 public int hashCode() {
1387 return mHash;
1388 }
1389
1390 @Override
1391 public boolean equals(Object obj) {
1392 if (!(obj instanceof ResourcesKey)) {
1393 return false;
1394 }
1395 ResourcesKey peer = (ResourcesKey) obj;
1396 return mResDir.equals(peer.mResDir) && mScale == peer.mScale;
1397 }
1398 }
1399
Romain Guy65b345f2011-07-27 18:51:50 -07001400 public static ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001401 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403
Romain Guy65b345f2011-07-27 18:51:50 -07001404 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001405 ActivityThread am = currentActivityThread();
1406 return (am != null && am.mBoundApplication != null)
1407 ? am.mBoundApplication.processName : null;
1408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409
Romain Guy65b345f2011-07-27 18:51:50 -07001410 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001411 ActivityThread am = currentActivityThread();
1412 return am != null ? am.mInitialApplication : null;
1413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001415 public static IPackageManager getPackageManager() {
1416 if (sPackageManager != null) {
1417 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1418 return sPackageManager;
1419 }
1420 IBinder b = ServiceManager.getService("package");
1421 //Slog.v("PackageManager", "default service binder = " + b);
1422 sPackageManager = IPackageManager.Stub.asInterface(b);
1423 //Slog.v("PackageManager", "default service = " + sPackageManager);
1424 return sPackageManager;
1425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001427 DisplayMetrics getDisplayMetricsLocked(CompatibilityInfo ci, boolean forceUpdate) {
1428 DisplayMetrics dm = mDisplayMetrics.get(ci);
1429 if (dm != null && !forceUpdate) {
1430 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001431 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001432 if (dm == null) {
1433 dm = new DisplayMetrics();
1434 mDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001435 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001436 Display d = WindowManagerImpl.getDefault(ci).getDefaultDisplay();
1437 d.getMetrics(dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001438 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1439 // + metrics.heightPixels + " den=" + metrics.density
1440 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001441 return dm;
1442 }
1443
1444 static Configuration applyConfigCompat(Configuration config, CompatibilityInfo compat) {
1445 if (config == null) {
1446 return null;
1447 }
1448 if (compat != null && !compat.supportsScreen()) {
1449 config = new Configuration(config);
1450 compat.applyToConfiguration(config);
1451 }
1452 return config;
1453 }
1454
Romain Guy65b345f2011-07-27 18:51:50 -07001455 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001456 Configuration applyConfigCompatMainThread(Configuration config, CompatibilityInfo compat) {
1457 if (config == null) {
1458 return null;
1459 }
1460 if (compat != null && !compat.supportsScreen()) {
1461 mMainThreadConfig.setTo(config);
1462 config = mMainThreadConfig;
1463 compat.applyToConfiguration(config);
1464 }
1465 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001468 /**
1469 * Creates the top level Resources for applications with the given compatibility info.
1470 *
1471 * @param resDir the resource directory.
1472 * @param compInfo the compability info. It will use the default compatibility info when it's
1473 * null.
1474 */
1475 Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) {
1476 ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale);
1477 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001479 // Resources is app scale dependent.
1480 if (false) {
1481 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1482 + compInfo.applicationScale);
1483 }
1484 WeakReference<Resources> wr = mActiveResources.get(key);
1485 r = wr != null ? wr.get() : null;
1486 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1487 if (r != null && r.getAssets().isUpToDate()) {
1488 if (false) {
1489 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1490 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1491 }
1492 return r;
1493 }
1494 }
1495
1496 //if (r != null) {
1497 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1498 // + r + " " + resDir);
1499 //}
1500
1501 AssetManager assets = new AssetManager();
1502 if (assets.addAssetPath(resDir) == 0) {
1503 return null;
1504 }
1505
1506 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Dianne Hackborn836e2622011-10-04 18:32:39 -07001507 DisplayMetrics metrics = getDisplayMetricsLocked(null, false);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001508 r = new Resources(assets, metrics, getConfiguration(), compInfo);
1509 if (false) {
1510 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1511 + r.getConfiguration() + " appScale="
1512 + r.getCompatibilityInfo().applicationScale);
1513 }
1514
1515 synchronized (mPackages) {
1516 WeakReference<Resources> wr = mActiveResources.get(key);
1517 Resources existing = wr != null ? wr.get() : null;
1518 if (existing != null && existing.getAssets().isUpToDate()) {
1519 // Someone else already created the resources while we were
1520 // unlocked; go ahead and use theirs.
1521 r.getAssets().close();
1522 return existing;
1523 }
1524
1525 // XXX need to remove entries when weak references go away
1526 mActiveResources.put(key, new WeakReference<Resources>(r));
1527 return r;
1528 }
1529 }
1530
1531 /**
1532 * Creates the top level resources for the given package.
1533 */
1534 Resources getTopLevelResources(String resDir, LoadedApk pkgInfo) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001535 return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001536 }
1537
1538 final Handler getHandler() {
1539 return mH;
1540 }
1541
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001542 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1543 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001544 synchronized (mPackages) {
1545 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1547 ref = mPackages.get(packageName);
1548 } else {
1549 ref = mResourcePackages.get(packageName);
1550 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001551 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001552 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001553 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1554 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 if (packageInfo != null && (packageInfo.mResources == null
1556 || packageInfo.mResources.getAssets().isUpToDate())) {
1557 if (packageInfo.isSecurityViolation()
1558 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1559 throw new SecurityException(
1560 "Requesting code from " + packageName
1561 + " to be run in process "
1562 + mBoundApplication.processName
1563 + "/" + mBoundApplication.appInfo.uid);
1564 }
1565 return packageInfo;
1566 }
1567 }
1568
1569 ApplicationInfo ai = null;
1570 try {
1571 ai = getPackageManager().getApplicationInfo(packageName,
1572 PackageManager.GET_SHARED_LIBRARY_FILES);
1573 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001574 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 }
1576
1577 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001578 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 }
1580
1581 return null;
1582 }
1583
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001584 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1585 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1587 boolean securityViolation = includeCode && ai.uid != 0
1588 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
1589 ? ai.uid != mBoundApplication.appInfo.uid : true);
1590 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1591 |Context.CONTEXT_IGNORE_SECURITY))
1592 == Context.CONTEXT_INCLUDE_CODE) {
1593 if (securityViolation) {
1594 String msg = "Requesting code from " + ai.packageName
1595 + " (with uid " + ai.uid + ")";
1596 if (mBoundApplication != null) {
1597 msg = msg + " to be run in process "
1598 + mBoundApplication.processName + " (with uid "
1599 + mBoundApplication.appInfo.uid + ")";
1600 }
1601 throw new SecurityException(msg);
1602 }
1603 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001604 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 }
1606
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001607 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1608 CompatibilityInfo compatInfo) {
1609 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 }
1611
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001612 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1613 synchronized (mPackages) {
1614 WeakReference<LoadedApk> ref;
1615 if (includeCode) {
1616 ref = mPackages.get(packageName);
1617 } else {
1618 ref = mResourcePackages.get(packageName);
1619 }
1620 return ref != null ? ref.get() : null;
1621 }
1622 }
1623
Romain Guy65b345f2011-07-27 18:51:50 -07001624 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1626 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001627 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 if (includeCode) {
1629 ref = mPackages.get(aInfo.packageName);
1630 } else {
1631 ref = mResourcePackages.get(aInfo.packageName);
1632 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001633 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 if (packageInfo == null || (packageInfo.mResources != null
1635 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001636 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 : "Loading resource-only package ") + aInfo.packageName
1638 + " (in " + (mBoundApplication != null
1639 ? mBoundApplication.processName : null)
1640 + ")");
1641 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001642 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 securityViolation, includeCode &&
1644 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1645 if (includeCode) {
1646 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001647 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 } else {
1649 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001650 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 }
1652 }
1653 return packageInfo;
1654 }
1655 }
1656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 ActivityThread() {
1658 }
1659
1660 public ApplicationThread getApplicationThread()
1661 {
1662 return mAppThread;
1663 }
1664
1665 public Instrumentation getInstrumentation()
1666 {
1667 return mInstrumentation;
1668 }
1669
1670 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001671 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 }
1673
1674 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001675 return mProfiler != null && mProfiler.profileFile != null
1676 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 }
1678
1679 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001680 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 }
1682
1683 public Looper getLooper() {
1684 return mLooper;
1685 }
1686
1687 public Application getApplication() {
1688 return mInitialApplication;
1689 }
Bob Leee5408332009-09-04 18:31:17 -07001690
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001691 public String getProcessName() {
1692 return mBoundApplication.processName;
1693 }
Bob Leee5408332009-09-04 18:31:17 -07001694
Dianne Hackborn21556372010-02-04 16:34:40 -08001695 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 synchronized (this) {
1697 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001698 ContextImpl context =
1699 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001700 LoadedApk info = new LoadedApk(this, "android", context, null,
1701 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 context.init(info, null, this);
1703 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001704 getConfiguration(), getDisplayMetricsLocked(
1705 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001707 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 // + ": " + context.getResources().getConfiguration());
1709 }
1710 }
1711 return mSystemContext;
1712 }
1713
Mike Cleron432b7132009-09-24 15:28:29 -07001714 public void installSystemApplicationInfo(ApplicationInfo info) {
1715 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001716 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001717 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001718 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001719
1720 // give ourselves a default profiler
1721 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001722 }
1723 }
1724
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001725 void ensureJitEnabled() {
1726 if (!mJitEnabled) {
1727 mJitEnabled = true;
1728 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1729 }
1730 }
1731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 void scheduleGcIdler() {
1733 if (!mGcIdlerScheduled) {
1734 mGcIdlerScheduled = true;
1735 Looper.myQueue().addIdleHandler(mGcIdler);
1736 }
1737 mH.removeMessages(H.GC_WHEN_IDLE);
1738 }
1739
1740 void unscheduleGcIdler() {
1741 if (mGcIdlerScheduled) {
1742 mGcIdlerScheduled = false;
1743 Looper.myQueue().removeIdleHandler(mGcIdler);
1744 }
1745 mH.removeMessages(H.GC_WHEN_IDLE);
1746 }
1747
1748 void doGcIfNeeded() {
1749 mGcIdlerScheduled = false;
1750 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001751 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 // + "m now=" + now);
1753 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001754 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 BinderInternal.forceGc("bg");
1756 }
1757 }
1758
Jeff Hamilton52d32032011-01-08 15:31:26 -06001759 public void registerOnActivityPausedListener(Activity activity,
1760 OnActivityPausedListener listener) {
1761 synchronized (mOnPauseListeners) {
1762 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1763 if (list == null) {
1764 list = new ArrayList<OnActivityPausedListener>();
1765 mOnPauseListeners.put(activity, list);
1766 }
1767 list.add(listener);
1768 }
1769 }
1770
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001771 public void unregisterOnActivityPausedListener(Activity activity,
1772 OnActivityPausedListener listener) {
1773 synchronized (mOnPauseListeners) {
1774 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1775 if (list != null) {
1776 list.remove(listener);
1777 }
1778 }
1779 }
1780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 public final ActivityInfo resolveActivityInfo(Intent intent) {
1782 ActivityInfo aInfo = intent.resolveActivityInfo(
1783 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1784 if (aInfo == null) {
1785 // Throw an exception.
1786 Instrumentation.checkStartActivityResult(
1787 IActivityManager.START_CLASS_NOT_FOUND, intent);
1788 }
1789 return aInfo;
1790 }
Bob Leee5408332009-09-04 18:31:17 -07001791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001794 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001795 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001797 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 r.intent = intent;
1799 r.state = state;
1800 r.parent = parent;
1801 r.embeddedID = id;
1802 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001803 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 if (localLOGV) {
1805 ComponentName compname = intent.getComponent();
1806 String name;
1807 if (compname != null) {
1808 name = compname.toShortString();
1809 } else {
1810 name = "(Intent " + intent + ").getComponent() returned null";
1811 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001812 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 + ", comp=" + name
1814 + ", token=" + token);
1815 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001816 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 }
1818
1819 public final Activity getActivity(IBinder token) {
1820 return mActivities.get(token).activity;
1821 }
1822
1823 public final void sendActivityResult(
1824 IBinder token, String id, int requestCode,
1825 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001826 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001827 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1829 list.add(new ResultInfo(id, requestCode, resultCode, data));
1830 mAppThread.scheduleSendResult(token, list);
1831 }
1832
1833 // if the thread hasn't started yet, we don't have the handler, so just
1834 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07001835 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 queueOrSendMessage(what, obj, 0, 0);
1837 }
1838
Romain Guy65b345f2011-07-27 18:51:50 -07001839 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 queueOrSendMessage(what, obj, arg1, 0);
1841 }
1842
Romain Guy65b345f2011-07-27 18:51:50 -07001843 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001845 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1847 + ": " + arg1 + " / " + obj);
1848 Message msg = Message.obtain();
1849 msg.what = what;
1850 msg.obj = obj;
1851 msg.arg1 = arg1;
1852 msg.arg2 = arg2;
1853 mH.sendMessage(msg);
1854 }
1855 }
1856
Dianne Hackborn21556372010-02-04 16:34:40 -08001857 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 String what) {
1859 ContextCleanupInfo cci = new ContextCleanupInfo();
1860 cci.context = context;
1861 cci.who = who;
1862 cci.what = what;
1863 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1864 }
1865
Romain Guy65b345f2011-07-27 18:51:50 -07001866 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1868
1869 ActivityInfo aInfo = r.activityInfo;
1870 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001871 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 Context.CONTEXT_INCLUDE_CODE);
1873 }
Bob Leee5408332009-09-04 18:31:17 -07001874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 ComponentName component = r.intent.getComponent();
1876 if (component == null) {
1877 component = r.intent.resolveActivity(
1878 mInitialApplication.getPackageManager());
1879 r.intent.setComponent(component);
1880 }
1881
1882 if (r.activityInfo.targetActivity != null) {
1883 component = new ComponentName(r.activityInfo.packageName,
1884 r.activityInfo.targetActivity);
1885 }
1886
1887 Activity activity = null;
1888 try {
1889 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
1890 activity = mInstrumentation.newActivity(
1891 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08001892 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 r.intent.setExtrasClassLoader(cl);
1894 if (r.state != null) {
1895 r.state.setClassLoader(cl);
1896 }
1897 } catch (Exception e) {
1898 if (!mInstrumentation.onException(activity, e)) {
1899 throw new RuntimeException(
1900 "Unable to instantiate activity " + component
1901 + ": " + e.toString(), e);
1902 }
1903 }
1904
1905 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001906 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001907
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001908 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
1909 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 TAG, r + ": app=" + app
1911 + ", appName=" + app.getPackageName()
1912 + ", pkg=" + r.packageInfo.getPackageName()
1913 + ", comp=" + r.intent.getComponent().toShortString()
1914 + ", dir=" + r.packageInfo.getAppDir());
1915
1916 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001917 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 appContext.init(r.packageInfo, r.token, this);
1919 appContext.setOuterContext(activity);
1920 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001921 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001922 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07001923 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001924 activity.attach(appContext, this, getInstrumentation(), r.token,
1925 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001926 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07001927
Christopher Tateb70f3df2009-04-07 16:07:59 -07001928 if (customIntent != null) {
1929 activity.mIntent = customIntent;
1930 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001931 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 activity.mStartedActivity = false;
1933 int theme = r.activityInfo.getThemeResource();
1934 if (theme != 0) {
1935 activity.setTheme(theme);
1936 }
1937
1938 activity.mCalled = false;
1939 mInstrumentation.callActivityOnCreate(activity, r.state);
1940 if (!activity.mCalled) {
1941 throw new SuperNotCalledException(
1942 "Activity " + r.intent.getComponent().toShortString() +
1943 " did not call through to super.onCreate()");
1944 }
1945 r.activity = activity;
1946 r.stopped = true;
1947 if (!r.activity.mFinished) {
1948 activity.performStart();
1949 r.stopped = false;
1950 }
1951 if (!r.activity.mFinished) {
1952 if (r.state != null) {
1953 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
1954 }
1955 }
1956 if (!r.activity.mFinished) {
1957 activity.mCalled = false;
1958 mInstrumentation.callActivityOnPostCreate(activity, r.state);
1959 if (!activity.mCalled) {
1960 throw new SuperNotCalledException(
1961 "Activity " + r.intent.getComponent().toShortString() +
1962 " did not call through to super.onPostCreate()");
1963 }
1964 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 }
1966 r.paused = true;
1967
1968 mActivities.put(r.token, r);
1969
1970 } catch (SuperNotCalledException e) {
1971 throw e;
1972
1973 } catch (Exception e) {
1974 if (!mInstrumentation.onException(activity, e)) {
1975 throw new RuntimeException(
1976 "Unable to start activity " + component
1977 + ": " + e.toString(), e);
1978 }
1979 }
1980
1981 return activity;
1982 }
1983
Romain Guy65b345f2011-07-27 18:51:50 -07001984 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 // If we are getting ready to gc after going to the background, well
1986 // we are back active so skip it.
1987 unscheduleGcIdler();
1988
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001989 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001990 mProfiler.setProfiler(r.profileFile, r.profileFd);
1991 mProfiler.startProfiling();
1992 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001993 }
1994
Dianne Hackborn58f42a52011-10-10 13:46:34 -07001995 // Make sure we are running with the most recent config.
1996 handleConfigurationChanged(null, null);
1997
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001998 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07002000 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001
2002 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002003 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002004 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 handleResumeActivity(r.token, false, r.isForward);
2006
2007 if (!r.activity.mFinished && r.startsNotResumed) {
2008 // The activity manager actually wants this one to start out
2009 // paused, because it needs to be visible but isn't in the
2010 // foreground. We accomplish this by going through the
2011 // normal startup (because activities expect to go through
2012 // onResume() the first time they run, before their window
2013 // is displayed), and then pausing it. However, in this case
2014 // we do -not- need to do the full pause cycle (of freezing
2015 // and such) because the activity manager assumes it can just
2016 // retain the current state it has.
2017 try {
2018 r.activity.mCalled = false;
2019 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002020 // We need to keep around the original state, in case
2021 // we need to be created again.
2022 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 if (!r.activity.mCalled) {
2024 throw new SuperNotCalledException(
2025 "Activity " + r.intent.getComponent().toShortString() +
2026 " did not call through to super.onPause()");
2027 }
2028
2029 } catch (SuperNotCalledException e) {
2030 throw e;
2031
2032 } catch (Exception e) {
2033 if (!mInstrumentation.onException(r.activity, e)) {
2034 throw new RuntimeException(
2035 "Unable to pause activity "
2036 + r.intent.getComponent().toShortString()
2037 + ": " + e.toString(), e);
2038 }
2039 }
2040 r.paused = true;
2041 }
2042 } else {
2043 // If there was an error, for any reason, tell the activity
2044 // manager to stop us.
2045 try {
2046 ActivityManagerNative.getDefault()
2047 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2048 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002049 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 }
2051 }
2052 }
2053
Romain Guy65b345f2011-07-27 18:51:50 -07002054 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 List<Intent> intents) {
2056 final int N = intents.size();
2057 for (int i=0; i<N; i++) {
2058 Intent intent = intents.get(i);
2059 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002060 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2062 }
2063 }
2064
2065 public final void performNewIntents(IBinder token,
2066 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002067 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 if (r != null) {
2069 final boolean resumed = !r.paused;
2070 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002071 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 mInstrumentation.callActivityOnPause(r.activity);
2073 }
2074 deliverNewIntents(r, intents);
2075 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002076 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002077 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 }
2079 }
2080 }
Bob Leee5408332009-09-04 18:31:17 -07002081
Romain Guy65b345f2011-07-27 18:51:50 -07002082 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 performNewIntents(data.token, data.intents);
2084 }
2085
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002086 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2087
2088 /**
2089 * Return the Intent that's currently being handled by a
2090 * BroadcastReceiver on this thread, or null if none.
2091 * @hide
2092 */
2093 public static Intent getIntentBeingBroadcast() {
2094 return sCurrentBroadcastIntent.get();
2095 }
2096
Romain Guy65b345f2011-07-27 18:51:50 -07002097 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 // If we are getting ready to gc after going to the background, well
2099 // we are back active so skip it.
2100 unscheduleGcIdler();
2101
2102 String component = data.intent.getComponent().getClassName();
2103
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002104 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002105 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106
2107 IActivityManager mgr = ActivityManagerNative.getDefault();
2108
Romain Guy65b345f2011-07-27 18:51:50 -07002109 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 try {
2111 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2112 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002113 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2115 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002116 if (DEBUG_BROADCAST) Slog.i(TAG,
2117 "Finishing failed broadcast to " + data.intent.getComponent());
2118 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 throw new RuntimeException(
2120 "Unable to instantiate receiver " + component
2121 + ": " + e.toString(), e);
2122 }
2123
2124 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002125 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002126
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002127 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 TAG, "Performing receive of " + data.intent
2129 + ": app=" + app
2130 + ", appName=" + app.getPackageName()
2131 + ", pkg=" + packageInfo.getPackageName()
2132 + ", comp=" + data.intent.getComponent().toShortString()
2133 + ", dir=" + packageInfo.getAppDir());
2134
Dianne Hackborn21556372010-02-04 16:34:40 -08002135 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002136 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002137 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 receiver.onReceive(context.getReceiverRestrictedContext(),
2139 data.intent);
2140 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002141 if (DEBUG_BROADCAST) Slog.i(TAG,
2142 "Finishing failed broadcast to " + data.intent.getComponent());
2143 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 if (!mInstrumentation.onException(receiver, e)) {
2145 throw new RuntimeException(
2146 "Unable to start receiver " + component
2147 + ": " + e.toString(), e);
2148 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002149 } finally {
2150 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 }
2152
Dianne Hackborne829fef2010-10-26 17:44:01 -07002153 if (receiver.getPendingResult() != null) {
2154 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 }
2156 }
2157
Christopher Tate181fafa2009-05-14 11:12:14 -07002158 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002159 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002160 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002161
2162 // no longer idle; we have backup work to do
2163 unscheduleGcIdler();
2164
2165 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002166 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002167 String packageName = packageInfo.mPackageName;
2168 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002169 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002170 + " already exists");
2171 return;
2172 }
Bob Leee5408332009-09-04 18:31:17 -07002173
Christopher Tate181fafa2009-05-14 11:12:14 -07002174 BackupAgent agent = null;
2175 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002176
Christopher Tate79ec80d2011-06-24 14:58:49 -07002177 // full backup operation but no app-supplied agent? use the default implementation
2178 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2179 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002180 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002181 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002182
Christopher Tate181fafa2009-05-14 11:12:14 -07002183 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002184 IBinder binder = null;
2185 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002186 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2187
Christopher Tated1475e02009-07-09 15:36:17 -07002188 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002189 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002190
2191 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002192 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002193 context.init(packageInfo, null, this);
2194 context.setOuterContext(agent);
2195 agent.attach(context);
2196
2197 agent.onCreate();
2198 binder = agent.onBind();
2199 mBackupAgents.put(packageName, agent);
2200 } catch (Exception e) {
2201 // If this is during restore, fail silently; otherwise go
2202 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002203 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002204 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2205 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002206 throw e;
2207 }
2208 // falling through with 'binder' still null
2209 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002210
2211 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002212 try {
2213 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2214 } catch (RemoteException e) {
2215 // nothing to do.
2216 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002217 } catch (Exception e) {
2218 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002219 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002220 }
2221 }
2222
2223 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002224 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002225 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002226
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002227 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002228 String packageName = packageInfo.mPackageName;
2229 BackupAgent agent = mBackupAgents.get(packageName);
2230 if (agent != null) {
2231 try {
2232 agent.onDestroy();
2233 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002234 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002235 e.printStackTrace();
2236 }
2237 mBackupAgents.remove(packageName);
2238 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002239 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002240 }
2241 }
2242
Romain Guy65b345f2011-07-27 18:51:50 -07002243 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 // If we are getting ready to gc after going to the background, well
2245 // we are back active so skip it.
2246 unscheduleGcIdler();
2247
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002248 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002249 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 Service service = null;
2251 try {
2252 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2253 service = (Service) cl.loadClass(data.info.name).newInstance();
2254 } catch (Exception e) {
2255 if (!mInstrumentation.onException(service, e)) {
2256 throw new RuntimeException(
2257 "Unable to instantiate service " + data.info.name
2258 + ": " + e.toString(), e);
2259 }
2260 }
2261
2262 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002263 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264
Dianne Hackborn21556372010-02-04 16:34:40 -08002265 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 context.init(packageInfo, null, this);
2267
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002268 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 context.setOuterContext(service);
2270 service.attach(context, this, data.info.name, data.token, app,
2271 ActivityManagerNative.getDefault());
2272 service.onCreate();
2273 mServices.put(data.token, service);
2274 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002275 ActivityManagerNative.getDefault().serviceDoneExecuting(
2276 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 } catch (RemoteException e) {
2278 // nothing to do.
2279 }
2280 } catch (Exception e) {
2281 if (!mInstrumentation.onException(service, e)) {
2282 throw new RuntimeException(
2283 "Unable to create service " + data.info.name
2284 + ": " + e.toString(), e);
2285 }
2286 }
2287 }
2288
Romain Guy65b345f2011-07-27 18:51:50 -07002289 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 Service s = mServices.get(data.token);
2291 if (s != null) {
2292 try {
2293 data.intent.setExtrasClassLoader(s.getClassLoader());
2294 try {
2295 if (!data.rebind) {
2296 IBinder binder = s.onBind(data.intent);
2297 ActivityManagerNative.getDefault().publishService(
2298 data.token, data.intent, binder);
2299 } else {
2300 s.onRebind(data.intent);
2301 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002302 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002304 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 } catch (RemoteException ex) {
2306 }
2307 } catch (Exception e) {
2308 if (!mInstrumentation.onException(s, e)) {
2309 throw new RuntimeException(
2310 "Unable to bind to service " + s
2311 + " with " + data.intent + ": " + e.toString(), e);
2312 }
2313 }
2314 }
2315 }
2316
Romain Guy65b345f2011-07-27 18:51:50 -07002317 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 Service s = mServices.get(data.token);
2319 if (s != null) {
2320 try {
2321 data.intent.setExtrasClassLoader(s.getClassLoader());
2322 boolean doRebind = s.onUnbind(data.intent);
2323 try {
2324 if (doRebind) {
2325 ActivityManagerNative.getDefault().unbindFinished(
2326 data.token, data.intent, doRebind);
2327 } else {
2328 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002329 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 }
2331 } catch (RemoteException ex) {
2332 }
2333 } catch (Exception e) {
2334 if (!mInstrumentation.onException(s, e)) {
2335 throw new RuntimeException(
2336 "Unable to unbind to service " + s
2337 + " with " + data.intent + ": " + e.toString(), e);
2338 }
2339 }
2340 }
2341 }
2342
Dianne Hackborn625ac272010-09-17 18:29:22 -07002343 private void handleDumpService(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002344 Service s = mServices.get(info.token);
2345 if (s != null) {
2346 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2347 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2348 pw.flush();
2349 try {
2350 info.fd.close();
2351 } catch (IOException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 }
2353 }
2354 }
2355
Dianne Hackborn625ac272010-09-17 18:29:22 -07002356 private void handleDumpActivity(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002357 ActivityClientRecord r = mActivities.get(info.token);
2358 if (r != null && r.activity != null) {
2359 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2360 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2361 pw.flush();
2362 try {
2363 info.fd.close();
2364 } catch (IOException e) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07002365 }
2366 }
2367 }
2368
Marco Nelissen18cb2872011-11-15 11:19:53 -08002369 private void handleDumpProvider(DumpComponentInfo info) {
2370 ProviderClientRecord r = mLocalProviders.get(info.token);
2371 if (r != null && r.mLocalProvider != null) {
2372 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2373 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2374 pw.flush();
2375 try {
2376 info.fd.close();
2377 } catch (IOException e) {
2378 }
2379 }
2380 }
2381
Romain Guy65b345f2011-07-27 18:51:50 -07002382 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 Service s = mServices.get(data.token);
2384 if (s != null) {
2385 try {
2386 if (data.args != null) {
2387 data.args.setExtrasClassLoader(s.getClassLoader());
2388 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002389 int res;
2390 if (!data.taskRemoved) {
2391 res = s.onStartCommand(data.args, data.flags, data.startId);
2392 } else {
2393 s.onTaskRemoved(data.args);
2394 res = Service.START_TASK_REMOVED_COMPLETE;
2395 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002396
2397 QueuedWork.waitToFinish();
2398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002400 ActivityManagerNative.getDefault().serviceDoneExecuting(
2401 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 } catch (RemoteException e) {
2403 // nothing to do.
2404 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002405 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 } catch (Exception e) {
2407 if (!mInstrumentation.onException(s, e)) {
2408 throw new RuntimeException(
2409 "Unable to start service " + s
2410 + " with " + data.args + ": " + e.toString(), e);
2411 }
2412 }
2413 }
2414 }
2415
Romain Guy65b345f2011-07-27 18:51:50 -07002416 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 Service s = mServices.remove(token);
2418 if (s != null) {
2419 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002420 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 s.onDestroy();
2422 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002423 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002425 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002427
2428 QueuedWork.waitToFinish();
2429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002431 ActivityManagerNative.getDefault().serviceDoneExecuting(
2432 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 } catch (RemoteException e) {
2434 // nothing to do.
2435 }
2436 } catch (Exception e) {
2437 if (!mInstrumentation.onException(s, e)) {
2438 throw new RuntimeException(
2439 "Unable to stop service " + s
2440 + ": " + e.toString(), e);
2441 }
2442 }
2443 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002444 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 }
2446
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002447 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002449 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002450 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 + " finished=" + r.activity.mFinished);
2452 if (r != null && !r.activity.mFinished) {
2453 if (clearHide) {
2454 r.hideForNow = false;
2455 r.activity.mStartedActivity = false;
2456 }
2457 try {
2458 if (r.pendingIntents != null) {
2459 deliverNewIntents(r, r.pendingIntents);
2460 r.pendingIntents = null;
2461 }
2462 if (r.pendingResults != null) {
2463 deliverResults(r, r.pendingResults);
2464 r.pendingResults = null;
2465 }
2466 r.activity.performResume();
2467
Bob Leee5408332009-09-04 18:31:17 -07002468 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 r.paused = false;
2472 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 r.state = null;
2474 } catch (Exception e) {
2475 if (!mInstrumentation.onException(r.activity, e)) {
2476 throw new RuntimeException(
2477 "Unable to resume activity "
2478 + r.intent.getComponent().toShortString()
2479 + ": " + e.toString(), e);
2480 }
2481 }
2482 }
2483 return r;
2484 }
2485
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002486 final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
2487 if (r.mPendingRemoveWindow != null) {
2488 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2489 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2490 if (wtoken != null) {
2491 WindowManagerImpl.getDefault().closeAll(wtoken,
2492 r.activity.getClass().getName(), "Activity");
2493 }
2494 }
2495 r.mPendingRemoveWindow = null;
2496 r.mPendingRemoveWindowManager = null;
2497 }
2498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2500 // If we are getting ready to gc after going to the background, well
2501 // we are back active so skip it.
2502 unscheduleGcIdler();
2503
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002504 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505
2506 if (r != null) {
2507 final Activity a = r.activity;
2508
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002509 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 TAG, "Resume " + r + " started activity: " +
2511 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2512 + ", finished: " + a.mFinished);
2513
2514 final int forwardBit = isForward ?
2515 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 // If the window hasn't yet been added to the window manager,
2518 // and this guy didn't finish itself or start another activity,
2519 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002520 boolean willBeVisible = !a.mStartedActivity;
2521 if (!willBeVisible) {
2522 try {
2523 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2524 a.getActivityToken());
2525 } catch (RemoteException e) {
2526 }
2527 }
2528 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 r.window = r.activity.getWindow();
2530 View decor = r.window.getDecorView();
2531 decor.setVisibility(View.INVISIBLE);
2532 ViewManager wm = a.getWindowManager();
2533 WindowManager.LayoutParams l = r.window.getAttributes();
2534 a.mDecor = decor;
2535 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2536 l.softInputMode |= forwardBit;
2537 if (a.mVisibleFromClient) {
2538 a.mWindowAdded = true;
2539 wm.addView(decor, l);
2540 }
2541
2542 // If the window has already been added, but during resume
2543 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002544 // window visible.
2545 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002546 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 TAG, "Launch " + r + " mStartedActivity set");
2548 r.hideForNow = true;
2549 }
2550
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002551 // Get rid of anything left hanging around.
2552 cleanUpPendingRemoveWindows(r);
2553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 // The window is now visible if it has been added, we are not
2555 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002556 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002557 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002559 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002560 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 performConfigurationChanged(r.activity, r.newConfig);
2562 r.newConfig = null;
2563 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002564 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 + isForward);
2566 WindowManager.LayoutParams l = r.window.getAttributes();
2567 if ((l.softInputMode
2568 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2569 != forwardBit) {
2570 l.softInputMode = (l.softInputMode
2571 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2572 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002573 if (r.activity.mVisibleFromClient) {
2574 ViewManager wm = a.getWindowManager();
2575 View decor = r.window.getDecorView();
2576 wm.updateViewLayout(decor, l);
2577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 }
2579 r.activity.mVisibleFromServer = true;
2580 mNumVisibleActivities++;
2581 if (r.activity.mVisibleFromClient) {
2582 r.activity.makeVisible();
2583 }
2584 }
2585
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002586 if (!r.onlyLocalRequest) {
2587 r.nextIdle = mNewActivities;
2588 mNewActivities = r;
2589 if (localLOGV) Slog.v(
2590 TAG, "Scheduling idle handler for " + r);
2591 Looper.myQueue().addIdleHandler(new Idler());
2592 }
2593 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594
2595 } else {
2596 // If an exception was thrown when trying to resume, then
2597 // just end this activity.
2598 try {
2599 ActivityManagerNative.getDefault()
2600 .finishActivity(token, Activity.RESULT_CANCELED, null);
2601 } catch (RemoteException ex) {
2602 }
2603 }
2604 }
2605
2606 private int mThumbnailWidth = -1;
2607 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002608 private Bitmap mAvailThumbnailBitmap = null;
2609 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610
Romain Guy65b345f2011-07-27 18:51:50 -07002611 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002612 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002614 if (thumbnail == null) {
2615 int w = mThumbnailWidth;
2616 int h;
2617 if (w < 0) {
2618 Resources res = r.activity.getResources();
2619 mThumbnailHeight = h =
2620 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002622 mThumbnailWidth = w =
2623 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2624 } else {
2625 h = mThumbnailHeight;
2626 }
2627
2628 // On platforms where we don't want thumbnails, set dims to (0,0)
2629 if ((w > 0) && (h > 0)) {
2630 thumbnail = Bitmap.createBitmap(w, h, THUMBNAIL_FORMAT);
2631 thumbnail.eraseColor(0);
2632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 }
2634
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002635 if (thumbnail != null) {
2636 Canvas cv = mThumbnailCanvas;
2637 if (cv == null) {
2638 mThumbnailCanvas = cv = new Canvas();
2639 }
2640
2641 cv.setBitmap(thumbnail);
2642 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2643 mAvailThumbnailBitmap = thumbnail;
2644 thumbnail = null;
2645 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002646 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 } catch (Exception e) {
2650 if (!mInstrumentation.onException(r.activity, e)) {
2651 throw new RuntimeException(
2652 "Unable to create thumbnail of "
2653 + r.intent.getComponent().toShortString()
2654 + ": " + e.toString(), e);
2655 }
2656 thumbnail = null;
2657 }
2658
2659 return thumbnail;
2660 }
2661
Romain Guy65b345f2011-07-27 18:51:50 -07002662 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002664 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002666 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 if (userLeaving) {
2668 performUserLeavingActivity(r);
2669 }
Bob Leee5408332009-09-04 18:31:17 -07002670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002672 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002674 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002675 if (r.isPreHoneycomb()) {
2676 QueuedWork.waitToFinish();
2677 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 // Tell the activity manager we have paused.
2680 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002681 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 } catch (RemoteException ex) {
2683 }
2684 }
2685 }
2686
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002687 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 mInstrumentation.callActivityOnUserLeaving(r.activity);
2689 }
2690
2691 final Bundle performPauseActivity(IBinder token, boolean finished,
2692 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002693 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 return r != null ? performPauseActivity(r, finished, saveState) : null;
2695 }
2696
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002697 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 boolean saveState) {
2699 if (r.paused) {
2700 if (r.activity.mFinished) {
2701 // If we are finishing, we won't call onResume() in certain cases.
2702 // So here we likewise don't want to call onPause() if the activity
2703 // isn't resumed.
2704 return null;
2705 }
2706 RuntimeException e = new RuntimeException(
2707 "Performing pause of activity that is not resumed: "
2708 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002709 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 }
2711 Bundle state = null;
2712 if (finished) {
2713 r.activity.mFinished = true;
2714 }
2715 try {
2716 // Next have the activity save its current state and managed dialogs...
2717 if (!r.activity.mFinished && saveState) {
2718 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002719 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2721 r.state = state;
2722 }
2723 // Now we are idle.
2724 r.activity.mCalled = false;
2725 mInstrumentation.callActivityOnPause(r.activity);
2726 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2727 if (!r.activity.mCalled) {
2728 throw new SuperNotCalledException(
2729 "Activity " + r.intent.getComponent().toShortString() +
2730 " did not call through to super.onPause()");
2731 }
2732
2733 } catch (SuperNotCalledException e) {
2734 throw e;
2735
2736 } catch (Exception e) {
2737 if (!mInstrumentation.onException(r.activity, e)) {
2738 throw new RuntimeException(
2739 "Unable to pause activity "
2740 + r.intent.getComponent().toShortString()
2741 + ": " + e.toString(), e);
2742 }
2743 }
2744 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002745
2746 // Notify any outstanding on paused listeners
2747 ArrayList<OnActivityPausedListener> listeners;
2748 synchronized (mOnPauseListeners) {
2749 listeners = mOnPauseListeners.remove(r.activity);
2750 }
2751 int size = (listeners != null ? listeners.size() : 0);
2752 for (int i = 0; i < size; i++) {
2753 listeners.get(i).onPaused(r.activity);
2754 }
2755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 return state;
2757 }
2758
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002759 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002760 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002761 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 }
2763
2764 private static class StopInfo {
2765 Bitmap thumbnail;
2766 CharSequence description;
2767 }
2768
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002769 private static final class ProviderRefCount {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 public int count;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 ProviderRefCount(int pCount) {
2773 count = pCount;
2774 }
2775 }
2776
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002777 /**
2778 * Core implementation of stopping an activity. Note this is a little
2779 * tricky because the server's meaning of stop is slightly different
2780 * than our client -- for the server, stop means to save state and give
2781 * it the result when it is done, but the window may still be visible.
2782 * For the client, we want to call onStop()/onStart() to indicate when
2783 * the activity's UI visibillity changes.
2784 */
Romain Guy65b345f2011-07-27 18:51:50 -07002785 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002786 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002787 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002788 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 if (r != null) {
2790 if (!keepShown && r.stopped) {
2791 if (r.activity.mFinished) {
2792 // If we are finishing, we won't call onResume() in certain
2793 // cases. So here we likewise don't want to call onStop()
2794 // if the activity isn't resumed.
2795 return;
2796 }
2797 RuntimeException e = new RuntimeException(
2798 "Performing stop of activity that is not resumed: "
2799 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002800 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 }
2802
2803 if (info != null) {
2804 try {
2805 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002806 // For now, don't create the thumbnail here; we are
2807 // doing that by doing a screen snapshot.
2808 info.thumbnail = null; //createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 info.description = r.activity.onCreateDescription();
2810 } catch (Exception e) {
2811 if (!mInstrumentation.onException(r.activity, e)) {
2812 throw new RuntimeException(
2813 "Unable to save state of activity "
2814 + r.intent.getComponent().toShortString()
2815 + ": " + e.toString(), e);
2816 }
2817 }
2818 }
2819
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002820 // Next have the activity save its current state and managed dialogs...
2821 if (!r.activity.mFinished && saveState) {
2822 if (r.state == null) {
2823 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002824 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002825 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2826 r.state = state;
2827 } else {
2828 state = r.state;
2829 }
2830 }
2831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 if (!keepShown) {
2833 try {
2834 // Now we are idle.
2835 r.activity.performStop();
2836 } catch (Exception e) {
2837 if (!mInstrumentation.onException(r.activity, e)) {
2838 throw new RuntimeException(
2839 "Unable to stop activity "
2840 + r.intent.getComponent().toShortString()
2841 + ": " + e.toString(), e);
2842 }
2843 }
2844 r.stopped = true;
2845 }
2846
2847 r.paused = true;
2848 }
2849 }
2850
Romain Guy65b345f2011-07-27 18:51:50 -07002851 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 View v = r.activity.mDecor;
2853 if (v != null) {
2854 if (show) {
2855 if (!r.activity.mVisibleFromServer) {
2856 r.activity.mVisibleFromServer = true;
2857 mNumVisibleActivities++;
2858 if (r.activity.mVisibleFromClient) {
2859 r.activity.makeVisible();
2860 }
2861 }
2862 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002863 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002864 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 performConfigurationChanged(r.activity, r.newConfig);
2866 r.newConfig = null;
2867 }
2868 } else {
2869 if (r.activity.mVisibleFromServer) {
2870 r.activity.mVisibleFromServer = false;
2871 mNumVisibleActivities--;
2872 v.setVisibility(View.INVISIBLE);
2873 }
2874 }
2875 }
2876 }
2877
Romain Guy65b345f2011-07-27 18:51:50 -07002878 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002879 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 r.activity.mConfigChangeFlags |= configChanges;
2881
2882 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002883 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002885 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 TAG, "Finishing stop of " + r + ": show=" + show
2887 + " win=" + r.window);
2888
2889 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07002890
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002891 // Make sure any pending writes are now committed.
2892 if (!r.isPreHoneycomb()) {
2893 QueuedWork.waitToFinish();
2894 }
2895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 // Tell activity manager we have been stopped.
2897 try {
2898 ActivityManagerNative.getDefault().activityStopped(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002899 r.token, r.state, info.thumbnail, info.description);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 } catch (RemoteException ex) {
2901 }
2902 }
2903
2904 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002905 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 if (r.stopped) {
2907 r.activity.performRestart();
2908 r.stopped = false;
2909 }
2910 }
2911
Romain Guy65b345f2011-07-27 18:51:50 -07002912 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002913 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08002914
2915 if (r == null) {
2916 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
2917 return;
2918 }
2919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002921 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 } else if (show && r.stopped) {
2923 // If we are getting ready to gc after going to the background, well
2924 // we are back active so skip it.
2925 unscheduleGcIdler();
2926
2927 r.activity.performRestart();
2928 r.stopped = false;
2929 }
2930 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07002931 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 TAG, "Handle window " + r + " visibility: " + show);
2933 updateVisibility(r, show);
2934 }
2935 }
2936
Romain Guy65b345f2011-07-27 18:51:50 -07002937 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002938 ActivityClientRecord r = mActivities.get(token);
2939
2940 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002941 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002942 return;
2943 }
2944
2945 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002946 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002947 try {
2948 // Now we are idle.
2949 r.activity.performStop();
2950 } catch (Exception e) {
2951 if (!mInstrumentation.onException(r.activity, e)) {
2952 throw new RuntimeException(
2953 "Unable to stop activity "
2954 + r.intent.getComponent().toShortString()
2955 + ": " + e.toString(), e);
2956 }
2957 }
2958 r.stopped = true;
2959 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002960
2961 // Make sure any pending writes are now committed.
2962 if (!r.isPreHoneycomb()) {
2963 QueuedWork.waitToFinish();
2964 }
2965
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002966 // Tell activity manager we slept.
2967 try {
2968 ActivityManagerNative.getDefault().activitySlept(r.token);
2969 } catch (RemoteException ex) {
2970 }
2971 } else {
2972 if (r.stopped && r.activity.mVisibleFromServer) {
2973 r.activity.performRestart();
2974 r.stopped = false;
2975 }
2976 }
2977 }
2978
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002979 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08002980 synchronized (mPackages) {
2981 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002982 }
2983 }
2984
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002985 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
2986 LoadedApk apk = peekPackageInfo(data.pkg, false);
2987 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002988 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002989 }
2990 apk = peekPackageInfo(data.pkg, true);
2991 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002992 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002993 }
2994 handleConfigurationChanged(mConfiguration, data.info);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002995 WindowManagerImpl.getDefault().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002996 }
2997
Romain Guy65b345f2011-07-27 18:51:50 -07002998 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 final int N = results.size();
3000 for (int i=0; i<N; i++) {
3001 ResultInfo ri = results.get(i);
3002 try {
3003 if (ri.mData != null) {
3004 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
3005 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003006 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003007 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 r.activity.dispatchActivityResult(ri.mResultWho,
3009 ri.mRequestCode, ri.mResultCode, ri.mData);
3010 } catch (Exception e) {
3011 if (!mInstrumentation.onException(r.activity, e)) {
3012 throw new RuntimeException(
3013 "Failure delivering result " + ri + " to activity "
3014 + r.intent.getComponent().toShortString()
3015 + ": " + e.toString(), e);
3016 }
3017 }
3018 }
3019 }
3020
Romain Guy65b345f2011-07-27 18:51:50 -07003021 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003022 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003023 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 if (r != null) {
3025 final boolean resumed = !r.paused;
3026 if (!r.activity.mFinished && r.activity.mDecor != null
3027 && r.hideForNow && resumed) {
3028 // We had hidden the activity because it started another
3029 // one... we have gotten a result back and we are not
3030 // paused, so make sure our window is visible.
3031 updateVisibility(r, true);
3032 }
3033 if (resumed) {
3034 try {
3035 // Now we are idle.
3036 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003037 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038 mInstrumentation.callActivityOnPause(r.activity);
3039 if (!r.activity.mCalled) {
3040 throw new SuperNotCalledException(
3041 "Activity " + r.intent.getComponent().toShortString()
3042 + " did not call through to super.onPause()");
3043 }
3044 } catch (SuperNotCalledException e) {
3045 throw e;
3046 } catch (Exception e) {
3047 if (!mInstrumentation.onException(r.activity, e)) {
3048 throw new RuntimeException(
3049 "Unable to pause activity "
3050 + r.intent.getComponent().toShortString()
3051 + ": " + e.toString(), e);
3052 }
3053 }
3054 }
3055 deliverResults(r, res.results);
3056 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003057 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003058 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 }
3060 }
3061 }
3062
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003063 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064 return performDestroyActivity(token, finishing, 0, false);
3065 }
3066
Romain Guy65b345f2011-07-27 18:51:50 -07003067 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003069 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003070 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003071 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003073 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 r.activity.mConfigChangeFlags |= configChanges;
3075 if (finishing) {
3076 r.activity.mFinished = true;
3077 }
3078 if (!r.paused) {
3079 try {
3080 r.activity.mCalled = false;
3081 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07003082 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 r.activity.getComponentName().getClassName());
3084 if (!r.activity.mCalled) {
3085 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003086 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 + " did not call through to super.onPause()");
3088 }
3089 } catch (SuperNotCalledException e) {
3090 throw e;
3091 } catch (Exception e) {
3092 if (!mInstrumentation.onException(r.activity, e)) {
3093 throw new RuntimeException(
3094 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003095 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 + ": " + e.toString(), e);
3097 }
3098 }
3099 r.paused = true;
3100 }
3101 if (!r.stopped) {
3102 try {
3103 r.activity.performStop();
3104 } catch (SuperNotCalledException e) {
3105 throw e;
3106 } catch (Exception e) {
3107 if (!mInstrumentation.onException(r.activity, e)) {
3108 throw new RuntimeException(
3109 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003110 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 + ": " + e.toString(), e);
3112 }
3113 }
3114 r.stopped = true;
3115 }
3116 if (getNonConfigInstance) {
3117 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003118 r.lastNonConfigurationInstances
3119 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 } catch (Exception e) {
3121 if (!mInstrumentation.onException(r.activity, e)) {
3122 throw new RuntimeException(
3123 "Unable to retain activity "
3124 + r.intent.getComponent().toShortString()
3125 + ": " + e.toString(), e);
3126 }
3127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003128 }
3129 try {
3130 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003131 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 if (!r.activity.mCalled) {
3133 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003134 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 " did not call through to super.onDestroy()");
3136 }
3137 if (r.window != null) {
3138 r.window.closeAllPanels();
3139 }
3140 } catch (SuperNotCalledException e) {
3141 throw e;
3142 } catch (Exception e) {
3143 if (!mInstrumentation.onException(r.activity, e)) {
3144 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003145 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3146 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 }
3148 }
3149 }
3150 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003151 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152 return r;
3153 }
3154
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003155 private static String safeToComponentShortString(Intent intent) {
3156 ComponentName component = intent.getComponent();
3157 return component == null ? "[Unknown]" : component.toShortString();
3158 }
3159
Romain Guy65b345f2011-07-27 18:51:50 -07003160 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003162 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 configChanges, getNonConfigInstance);
3164 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003165 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 WindowManager wm = r.activity.getWindowManager();
3167 View v = r.activity.mDecor;
3168 if (v != null) {
3169 if (r.activity.mVisibleFromServer) {
3170 mNumVisibleActivities--;
3171 }
3172 IBinder wtoken = v.getWindowToken();
3173 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003174 if (r.onlyLocalRequest) {
3175 // Hold off on removing this until the new activity's
3176 // window is being added.
3177 r.mPendingRemoveWindow = v;
3178 r.mPendingRemoveWindowManager = wm;
3179 } else {
3180 wm.removeViewImmediate(v);
3181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003183 if (wtoken != null && r.mPendingRemoveWindow == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 WindowManagerImpl.getDefault().closeAll(wtoken,
3185 r.activity.getClass().getName(), "Activity");
3186 }
3187 r.activity.mDecor = null;
3188 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003189 if (r.mPendingRemoveWindow == null) {
3190 // If we are delaying the removal of the activity window, then
3191 // we can't clean up all windows here. Note that we can't do
3192 // so later either, which means any windows that aren't closed
3193 // by the app will leak. Well we try to warning them a lot
3194 // about leaking windows, because that is a bug, so if they are
3195 // using this recreate facility then they get to live with leaks.
3196 WindowManagerImpl.getDefault().closeAll(token,
3197 r.activity.getClass().getName(), "Activity");
3198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199
3200 // Mocked out contexts won't be participating in the normal
3201 // process lifecycle, but if we're running with a proper
3202 // ApplicationContext we need to have it tear down things
3203 // cleanly.
3204 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003205 if (c instanceof ContextImpl) {
3206 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 r.activity.getClass().getName(), "Activity");
3208 }
3209 }
3210 if (finishing) {
3211 try {
3212 ActivityManagerNative.getDefault().activityDestroyed(token);
3213 } catch (RemoteException ex) {
3214 // If the system process has died, it's game over for everyone.
3215 }
3216 }
3217 }
3218
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003219 public final void requestRelaunchActivity(IBinder token,
3220 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3221 int configChanges, boolean notResumed, Configuration config,
3222 boolean fromServer) {
3223 ActivityClientRecord target = null;
3224
3225 synchronized (mPackages) {
3226 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3227 ActivityClientRecord r = mRelaunchingActivities.get(i);
3228 if (r.token == token) {
3229 target = r;
3230 if (pendingResults != null) {
3231 if (r.pendingResults != null) {
3232 r.pendingResults.addAll(pendingResults);
3233 } else {
3234 r.pendingResults = pendingResults;
3235 }
3236 }
3237 if (pendingNewIntents != null) {
3238 if (r.pendingIntents != null) {
3239 r.pendingIntents.addAll(pendingNewIntents);
3240 } else {
3241 r.pendingIntents = pendingNewIntents;
3242 }
3243 }
3244 break;
3245 }
3246 }
3247
3248 if (target == null) {
3249 target = new ActivityClientRecord();
3250 target.token = token;
3251 target.pendingResults = pendingResults;
3252 target.pendingIntents = pendingNewIntents;
3253 if (!fromServer) {
3254 ActivityClientRecord existing = mActivities.get(token);
3255 if (existing != null) {
3256 target.startsNotResumed = existing.paused;
3257 }
3258 target.onlyLocalRequest = true;
3259 }
3260 mRelaunchingActivities.add(target);
3261 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3262 }
3263
3264 if (fromServer) {
3265 target.startsNotResumed = notResumed;
3266 target.onlyLocalRequest = false;
3267 }
3268 if (config != null) {
3269 target.createdConfig = config;
3270 }
3271 target.pendingConfigChanges |= configChanges;
3272 }
3273 }
3274
Romain Guy65b345f2011-07-27 18:51:50 -07003275 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 // If we are getting ready to gc after going to the background, well
3277 // we are back active so skip it.
3278 unscheduleGcIdler();
3279
3280 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003281 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 // First: make sure we have the most recent configuration and most
3284 // recent version of the activity, or skip it if some previous call
3285 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003286 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 int N = mRelaunchingActivities.size();
3288 IBinder token = tmp.token;
3289 tmp = null;
3290 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003291 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 if (r.token == token) {
3293 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003294 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 mRelaunchingActivities.remove(i);
3296 i--;
3297 N--;
3298 }
3299 }
Bob Leee5408332009-09-04 18:31:17 -07003300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003302 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 return;
3304 }
Bob Leee5408332009-09-04 18:31:17 -07003305
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003306 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3307 + tmp.token + " with configChanges=0x"
3308 + Integer.toHexString(configChanges));
3309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 if (mPendingConfiguration != null) {
3311 changedConfig = mPendingConfiguration;
3312 mPendingConfiguration = null;
3313 }
3314 }
Bob Leee5408332009-09-04 18:31:17 -07003315
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003316 if (tmp.createdConfig != null) {
3317 // If the activity manager is passing us its current config,
3318 // assume that is really what we want regardless of what we
3319 // may have pending.
3320 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003321 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3322 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3323 if (changedConfig == null
3324 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3325 changedConfig = tmp.createdConfig;
3326 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003327 }
3328 }
3329
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003330 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003331 + tmp.token + ": changedConfig=" + changedConfig);
3332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 // If there was a pending configuration change, execute it first.
3334 if (changedConfig != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003335 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 }
Bob Leee5408332009-09-04 18:31:17 -07003337
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003338 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003339 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 if (r == null) {
3341 return;
3342 }
Bob Leee5408332009-09-04 18:31:17 -07003343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003345 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003346 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003347
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003348 r.activity.mChangingConfigurations = true;
3349
Dianne Hackborne2b04802010-12-09 09:24:55 -08003350 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003352 performPauseActivity(r.token, false, r.isPreHoneycomb());
3353 }
3354 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3355 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003356 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003357 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 }
Bob Leee5408332009-09-04 18:31:17 -07003359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 r.activity = null;
3363 r.window = null;
3364 r.hideForNow = false;
3365 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003366 // Merge any pending results and pending intents; don't just replace them
3367 if (tmp.pendingResults != null) {
3368 if (r.pendingResults == null) {
3369 r.pendingResults = tmp.pendingResults;
3370 } else {
3371 r.pendingResults.addAll(tmp.pendingResults);
3372 }
3373 }
3374 if (tmp.pendingIntents != null) {
3375 if (r.pendingIntents == null) {
3376 r.pendingIntents = tmp.pendingIntents;
3377 } else {
3378 r.pendingIntents.addAll(tmp.pendingIntents);
3379 }
3380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003382
Christopher Tateb70f3df2009-04-07 16:07:59 -07003383 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384 }
3385
Romain Guy65b345f2011-07-27 18:51:50 -07003386 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003387 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 Bitmap thumbnail = createThumbnailBitmap(r);
3389 CharSequence description = null;
3390 try {
3391 description = r.activity.onCreateDescription();
3392 } catch (Exception e) {
3393 if (!mInstrumentation.onException(r.activity, e)) {
3394 throw new RuntimeException(
3395 "Unable to create description of activity "
3396 + r.intent.getComponent().toShortString()
3397 + ": " + e.toString(), e);
3398 }
3399 }
3400 //System.out.println("Reporting top thumbnail " + thumbnail);
3401 try {
3402 ActivityManagerNative.getDefault().reportThumbnail(
3403 token, thumbnail, description);
3404 } catch (RemoteException ex) {
3405 }
3406 }
3407
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003408 ArrayList<ComponentCallbacks2> collectComponentCallbacksLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003410 ArrayList<ComponentCallbacks2> callbacks
3411 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 if (mActivities.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003414 Iterator<ActivityClientRecord> it = mActivities.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003415 while (it.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003416 ActivityClientRecord ar = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 Activity a = ar.activity;
3418 if (a != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003419 Configuration thisConfig = applyConfigCompatMainThread(newConfig,
3420 ar.packageInfo.mCompatibilityInfo.getIfNeeded());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 if (!ar.activity.mFinished && (allActivities ||
3422 (a != null && !ar.paused))) {
3423 // If the activity is currently resumed, its configuration
3424 // needs to change right now.
3425 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003426 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 // Otherwise, we will tell it about the change
3428 // the next time it is resumed or shown. Note that
3429 // the activity manager may, before then, decide the
3430 // activity needs to be destroyed to handle its new
3431 // configuration.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003432 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Setting activity "
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003433 + ar.activityInfo.name + " newConfig=" + thisConfig);
3434 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 }
3436 }
3437 }
3438 }
3439 if (mServices.size() > 0) {
3440 Iterator<Service> it = mServices.values().iterator();
3441 while (it.hasNext()) {
3442 callbacks.add(it.next());
3443 }
3444 }
3445 synchronized (mProviderMap) {
3446 if (mLocalProviders.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003447 Iterator<ProviderClientRecord> it = mLocalProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 while (it.hasNext()) {
3449 callbacks.add(it.next().mLocalProvider);
3450 }
3451 }
3452 }
3453 final int N = mAllApplications.size();
3454 for (int i=0; i<N; i++) {
3455 callbacks.add(mAllApplications.get(i));
3456 }
Bob Leee5408332009-09-04 18:31:17 -07003457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 return callbacks;
3459 }
Bob Leee5408332009-09-04 18:31:17 -07003460
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003461 private final void performConfigurationChanged(
3462 ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003464 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 // we check the runtime type and act accordingly.
3466 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3467 if (activity != null) {
3468 activity.mCalled = false;
3469 }
Bob Leee5408332009-09-04 18:31:17 -07003470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 boolean shouldChangeConfig = false;
3472 if ((activity == null) || (activity.mCurrentConfig == null)) {
3473 shouldChangeConfig = true;
3474 } else {
Bob Leee5408332009-09-04 18:31:17 -07003475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003476 // If the new config is the same as the config this Activity
3477 // is already running with then don't bother calling
3478 // onConfigurationChanged
3479 int diff = activity.mCurrentConfig.diff(config);
3480 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 // If this activity doesn't handle any of the config changes
3482 // then don't bother calling onConfigurationChanged as we're
3483 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003484 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003485 shouldChangeConfig = true;
3486 }
3487 }
3488 }
Bob Leee5408332009-09-04 18:31:17 -07003489
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003490 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003491 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 if (shouldChangeConfig) {
3493 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 if (activity != null) {
3496 if (!activity.mCalled) {
3497 throw new SuperNotCalledException(
3498 "Activity " + activity.getLocalClassName() +
3499 " did not call through to super.onConfigurationChanged()");
3500 }
3501 activity.mConfigChangeFlags = 0;
3502 activity.mCurrentConfig = new Configuration(config);
3503 }
3504 }
3505 }
3506
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003507 public final void applyConfigurationToResources(Configuration config) {
3508 synchronized (mPackages) {
3509 applyConfigurationToResourcesLocked(config, null);
3510 }
3511 }
3512
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003513 final boolean applyConfigurationToResourcesLocked(Configuration config,
3514 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003515 if (mResConfiguration == null) {
3516 mResConfiguration = new Configuration();
3517 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003518 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003519 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003520 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003521 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003522 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003523 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003524 DisplayMetrics dm = getDisplayMetricsLocked(null, true);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003525
3526 if (compat != null && (mResCompatibilityInfo == null ||
3527 !mResCompatibilityInfo.equals(compat))) {
3528 mResCompatibilityInfo = compat;
3529 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3530 | ActivityInfo.CONFIG_SCREEN_SIZE
3531 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3532 }
Bob Leee5408332009-09-04 18:31:17 -07003533
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003534 // set it for java, this also affects newly created Resources
3535 if (config.locale != null) {
3536 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 }
Bob Leee5408332009-09-04 18:31:17 -07003538
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003539 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003540
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003541 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003542 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003543
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003544 Iterator<WeakReference<Resources>> it =
3545 mActiveResources.values().iterator();
3546 //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3547 // mActiveResources.entrySet().iterator();
3548 while (it.hasNext()) {
3549 WeakReference<Resources> v = it.next();
3550 Resources r = v.get();
3551 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003552 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003553 + r + " config to: " + config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003554 r.updateConfiguration(config, dm, compat);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003555 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003556 // + " " + r + ": " + r.getConfiguration());
3557 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003558 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003559 it.remove();
3560 }
3561 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003562
3563 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003564 }
Dianne Hackborn836e2622011-10-04 18:32:39 -07003565
3566 final Configuration applyCompatConfiguration() {
3567 Configuration config = mConfiguration;
3568 if (mCompatConfiguration == null) {
3569 mCompatConfiguration = new Configuration();
3570 }
3571 mCompatConfiguration.setTo(mConfiguration);
3572 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
3573 mResCompatibilityInfo.applyToConfiguration(mCompatConfiguration);
3574 config = mCompatConfiguration;
3575 }
3576 return config;
3577 }
3578
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003579 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003580
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003581 ArrayList<ComponentCallbacks2> callbacks = null;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003582
3583 synchronized (mPackages) {
3584 if (mPendingConfiguration != null) {
3585 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3586 config = mPendingConfiguration;
3587 }
3588 mPendingConfiguration = null;
3589 }
3590
3591 if (config == null) {
3592 return;
3593 }
3594
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003595 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003596 + config);
3597
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003598 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003600 if (mConfiguration == null) {
3601 mConfiguration = new Configuration();
3602 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003603 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003604 return;
3605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003606 mConfiguration.updateFrom(config);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003607 config = applyCompatConfiguration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003608 callbacks = collectComponentCallbacksLocked(false, config);
3609 }
Romain Guy65b345f2011-07-27 18:51:50 -07003610
3611 // Cleanup hardware accelerated stuff
3612 WindowManagerImpl.getDefault().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003613
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003614 if (callbacks != null) {
3615 final int N = callbacks.size();
3616 for (int i=0; i<N; i++) {
3617 performConfigurationChanged(callbacks.get(i), config);
3618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 }
3620 }
3621
3622 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003623 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 if (r == null || r.activity == null) {
3625 return;
3626 }
Bob Leee5408332009-09-04 18:31:17 -07003627
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003628 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003629 + r.activityInfo.name);
3630
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003631 performConfigurationChanged(r.activity, mCompatConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 }
3633
Romain Guy7eabe552011-07-21 14:56:34 -07003634 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003635 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003636 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003637 switch (profileType) {
3638 case 1:
3639 ViewDebug.startLooperProfiling(pcd.path, pcd.fd.getFileDescriptor());
3640 break;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003641 default:
3642 mProfiler.setProfiler(pcd.path, pcd.fd);
3643 mProfiler.autoStopProfiler = false;
3644 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003645 break;
3646 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003647 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003648 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003649 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003650 } finally {
3651 try {
3652 pcd.fd.close();
3653 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003654 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003655 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003656 }
3657 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003658 switch (profileType) {
3659 case 1:
3660 ViewDebug.stopLooperProfiling();
3661 break;
3662 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003663 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003664 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003665 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003666 }
3667 }
Bob Leee5408332009-09-04 18:31:17 -07003668
Andy McFadden824c5102010-07-09 16:26:57 -07003669 final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
3670 if (managed) {
3671 try {
3672 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3673 } catch (IOException e) {
3674 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3675 + " -- can the process access this path?");
3676 } finally {
3677 try {
3678 dhd.fd.close();
3679 } catch (IOException e) {
3680 Slog.w(TAG, "Failure closing profile fd", e);
3681 }
3682 }
3683 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003684 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003685 }
3686 }
3687
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003688 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3689 boolean hasPkgInfo = false;
3690 if (packages != null) {
3691 for (int i=packages.length-1; i>=0; i--) {
3692 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3693 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003694 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003695 ref = mPackages.get(packages[i]);
3696 if (ref != null && ref.get() != null) {
3697 hasPkgInfo = true;
3698 } else {
3699 ref = mResourcePackages.get(packages[i]);
3700 if (ref != null && ref.get() != null) {
3701 hasPkgInfo = true;
3702 }
3703 }
3704 }
3705 mPackages.remove(packages[i]);
3706 mResourcePackages.remove(packages[i]);
3707 }
3708 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003709 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003710 hasPkgInfo);
3711 }
3712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003713 final void handleLowMemory() {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003714 ArrayList<ComponentCallbacks2> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003715
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003716 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 callbacks = collectComponentCallbacksLocked(true, null);
3718 }
Bob Leee5408332009-09-04 18:31:17 -07003719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 final int N = callbacks.size();
3721 for (int i=0; i<N; i++) {
3722 callbacks.get(i).onLowMemory();
3723 }
3724
Chris Tatece229052009-03-25 16:44:52 -07003725 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3726 if (Process.myUid() != Process.SYSTEM_UID) {
3727 int sqliteReleased = SQLiteDatabase.releaseMemory();
3728 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3729 }
Bob Leee5408332009-09-04 18:31:17 -07003730
Mike Reedcaf0df12009-04-27 14:32:05 -04003731 // Ask graphics to free up as much as possible (font/image caches)
3732 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733
3734 BinderInternal.forceGc("mem");
3735 }
3736
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003737 final void handleTrimMemory(int level) {
Romain Guybdf76092011-07-18 15:00:43 -07003738 WindowManagerImpl.getDefault().trimMemory(level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003739 ArrayList<ComponentCallbacks2> callbacks;
3740
3741 synchronized (mPackages) {
3742 callbacks = collectComponentCallbacksLocked(true, null);
3743 }
3744
3745 final int N = callbacks.size();
3746 for (int i=0; i<N; i++) {
3747 callbacks.get(i).onTrimMemory(level);
3748 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003749 }
3750
Romain Guya9582652011-11-10 14:20:10 -08003751 private void setupGraphicsSupport(LoadedApk info) {
3752 try {
3753 int uid = Process.myUid();
3754 String[] packages = getPackageManager().getPackagesForUid(uid);
3755
3756 // If there are several packages in this application we won't
3757 // initialize the graphics disk caches
3758 if (packages.length == 1) {
3759 ContextImpl appContext = new ContextImpl();
3760 appContext.init(info, null, this);
3761
3762 HardwareRenderer.setupDiskCache(appContext.getCacheDir());
3763 }
3764 } catch (RemoteException e) {
3765 // Ignore
3766 }
3767 }
3768
Romain Guy65b345f2011-07-27 18:51:50 -07003769 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 mBoundApplication = data;
3771 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003772 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003774 mProfiler = new Profiler();
3775 mProfiler.profileFile = data.initProfileFile;
3776 mProfiler.profileFd = data.initProfileFd;
3777 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
3778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003780 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003781 android.ddm.DdmHandleAppName.setAppName(data.processName);
3782
Dianne Hackborn5d927c22011-09-02 12:22:18 -07003783 if (data.persistent) {
3784 // Persistent processes on low-memory devices do not get to
3785 // use hardware accelerated drawing, since this can add too much
3786 // overhead to the process.
3787 Display display = WindowManagerImpl.getDefault().getDefaultDisplay();
3788 if (!ActivityManager.isHighEndGfx(display)) {
3789 HardwareRenderer.disable(false);
3790 }
3791 }
Romain Guya9582652011-11-10 14:20:10 -08003792
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003793 if (mProfiler.profileFd != null) {
3794 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003795 }
3796
Joe Onoratod630f102011-03-17 18:42:26 -07003797 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
3798 // implementation to use the pool executor. Normally, we use the
3799 // serialized executor as the default. This has to happen in the
3800 // main thread so the main looper is set right.
3801 if (data.appInfo.targetSdkVersion <= 12) {
3802 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
3803 }
3804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 /*
3806 * Before spawning a new process, reset the time zone to be the system time zone.
3807 * This needs to be done because the system time zone could have changed after the
3808 * the spawning of this process. Without doing this this process would have the incorrect
3809 * system time zone.
3810 */
3811 TimeZone.setDefault(null);
3812
3813 /*
3814 * Initialize the default locale in this process for the reasons we set the time zone.
3815 */
3816 Locale.setDefault(data.config.locale);
3817
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003818 /*
3819 * Update the system configuration since its preloaded and might not
3820 * reflect configuration changes. The configuration object passed
3821 * in AppBindData can be safely assumed to be up to date
3822 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003823 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003824 applyCompatConfiguration();
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003825
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003826 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827
Romain Guya9582652011-11-10 14:20:10 -08003828 setupGraphicsSupport(data.info);
3829
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003830 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003831 * For system applications on userdebug/eng builds, log stack
3832 * traces of disk and network access to dropbox for analysis.
3833 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07003834 if ((data.appInfo.flags &
3835 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07003836 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
3837 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003838 }
3839
3840 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07003841 * For apps targetting SDK Honeycomb or later, we don't allow
3842 * network usage on the main event loop / UI thread.
3843 *
3844 * Note to those grepping: this is what ultimately throws
3845 * NetworkOnMainThreadException ...
3846 */
3847 if (data.appInfo.targetSdkVersion > 9) {
3848 StrictMode.enableDeathOnNetwork();
3849 }
3850
3851 /**
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003852 * Switch this process to density compatibility mode if needed.
3853 */
3854 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
3855 == 0) {
3856 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3857 }
Bob Leee5408332009-09-04 18:31:17 -07003858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
3860 // XXX should have option to change the port.
3861 Debug.changeDebugPort(8100);
3862 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003863 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003864 + " is waiting for the debugger on port 8100...");
3865
3866 IActivityManager mgr = ActivityManagerNative.getDefault();
3867 try {
3868 mgr.showWaitingForDebugger(mAppThread, true);
3869 } catch (RemoteException ex) {
3870 }
3871
3872 Debug.waitForDebugger();
3873
3874 try {
3875 mgr.showWaitingForDebugger(mAppThread, false);
3876 } catch (RemoteException ex) {
3877 }
3878
3879 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003880 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003881 + " can be debugged on port 8100...");
3882 }
3883 }
3884
Robert Greenwalt434203a2010-10-11 16:00:27 -07003885 /**
3886 * Initialize the default http proxy in this process for the reasons we set the time zone.
3887 */
3888 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08003889 if (b != null) {
3890 // In pre-boot mode (doing initial launch to collect password), not
3891 // all system is up. This includes the connectivity service, so don't
3892 // crash if we can't get it.
3893 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
3894 try {
3895 ProxyProperties proxyProperties = service.getProxy();
3896 Proxy.setHttpProxySystemProperty(proxyProperties);
3897 } catch (RemoteException e) {}
3898 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07003899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900 if (data.instrumentationName != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08003901 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 appContext.init(data.info, null, this);
3903 InstrumentationInfo ii = null;
3904 try {
3905 ii = appContext.getPackageManager().
3906 getInstrumentationInfo(data.instrumentationName, 0);
3907 } catch (PackageManager.NameNotFoundException e) {
3908 }
3909 if (ii == null) {
3910 throw new RuntimeException(
3911 "Unable to find instrumentation info for: "
3912 + data.instrumentationName);
3913 }
3914
3915 mInstrumentationAppDir = ii.sourceDir;
3916 mInstrumentationAppPackage = ii.packageName;
3917 mInstrumentedAppDir = data.info.getAppDir();
3918
3919 ApplicationInfo instrApp = new ApplicationInfo();
3920 instrApp.packageName = ii.packageName;
3921 instrApp.sourceDir = ii.sourceDir;
3922 instrApp.publicSourceDir = ii.publicSourceDir;
3923 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07003924 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003925 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003926 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08003927 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 instrContext.init(pi, null, this);
3929
3930 try {
3931 java.lang.ClassLoader cl = instrContext.getClassLoader();
3932 mInstrumentation = (Instrumentation)
3933 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
3934 } catch (Exception e) {
3935 throw new RuntimeException(
3936 "Unable to instantiate instrumentation "
3937 + data.instrumentationName + ": " + e.toString(), e);
3938 }
3939
3940 mInstrumentation.init(this, instrContext, appContext,
3941 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
3942
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003943 if (mProfiler.profileFile != null && !ii.handleProfiling
3944 && mProfiler.profileFd == null) {
3945 mProfiler.handlingProfiling = true;
3946 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 file.getParentFile().mkdirs();
3948 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
3949 }
3950
3951 try {
3952 mInstrumentation.onCreate(data.instrumentationArgs);
3953 }
3954 catch (Exception e) {
3955 throw new RuntimeException(
3956 "Exception thrown in onCreate() of "
3957 + data.instrumentationName + ": " + e.toString(), e);
3958 }
3959
3960 } else {
3961 mInstrumentation = new Instrumentation();
3962 }
3963
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003964 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08003965 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003966 }
3967
Christopher Tate181fafa2009-05-14 11:12:14 -07003968 // If the app is being launched for full backup or restore, bring it up in
3969 // a restricted environment with the base application class.
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003970 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 mInitialApplication = app;
3972
Christopher Tate75a99702011-05-18 16:28:19 -07003973 // don't bring up providers in restricted mode; they may depend on the
3974 // app's custom Application class
3975 if (!data.restrictedBackupMode){
3976 List<ProviderInfo> providers = data.providers;
3977 if (providers != null) {
3978 installContentProviders(app, providers);
3979 // For process that contains content providers, we want to
3980 // ensure that the JIT is enabled "at some point".
3981 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
3982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003983 }
3984
3985 try {
3986 mInstrumentation.callApplicationOnCreate(app);
3987 } catch (Exception e) {
3988 if (!mInstrumentation.onException(app, e)) {
3989 throw new RuntimeException(
3990 "Unable to create application " + app.getClass().getName()
3991 + ": " + e.toString(), e);
3992 }
3993 }
3994 }
3995
3996 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
3997 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003998 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
3999 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004000 Debug.stopMethodTracing();
4001 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004002 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 // + ", app thr: " + mAppThread);
4004 try {
4005 am.finishInstrumentation(mAppThread, resultCode, results);
4006 } catch (RemoteException ex) {
4007 }
4008 }
4009
Romain Guy65b345f2011-07-27 18:51:50 -07004010 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004011 Context context, List<ProviderInfo> providers) {
4012 final ArrayList<IActivityManager.ContentProviderHolder> results =
4013 new ArrayList<IActivityManager.ContentProviderHolder>();
4014
4015 Iterator<ProviderInfo> i = providers.iterator();
4016 while (i.hasNext()) {
4017 ProviderInfo cpi = i.next();
4018 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07004019 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004020 buf.append(cpi.authority);
4021 buf.append(": ");
4022 buf.append(cpi.name);
4023 Log.i(TAG, buf.toString());
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004024 IContentProvider cp = installProvider(context, null, cpi,
4025 false /*noisy*/, true /*noReleaseNeeded*/);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004026 if (cp != null) {
4027 IActivityManager.ContentProviderHolder cph =
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004028 new IActivityManager.ContentProviderHolder(cpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 cph.provider = cp;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004030 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004031 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004032 }
4033 }
4034
4035 try {
4036 ActivityManagerNative.getDefault().publishContentProviders(
4037 getApplicationThread(), results);
4038 } catch (RemoteException ex) {
4039 }
4040 }
4041
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004042 public final IContentProvider acquireProvider(Context c, String name) {
4043 IContentProvider provider = acquireExistingProvider(c, name);
4044 if (provider != null) {
4045 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004046 }
4047
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004048 // There is a possible race here. Another thread may try to acquire
4049 // the same provider at the same time. When this happens, we want to ensure
4050 // that the first one wins.
4051 // Note that we cannot hold the lock while acquiring and installing the
4052 // provider since it might take a long time to run and it could also potentially
4053 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 IActivityManager.ContentProviderHolder holder = null;
4055 try {
4056 holder = ActivityManagerNative.getDefault().getContentProvider(
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004057 getApplicationThread(), name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 } catch (RemoteException ex) {
4059 }
4060 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004061 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 return null;
4063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004065 // Install provider will increment the reference count for us, and break
4066 // any ties in the race.
4067 provider = installProvider(c, holder.provider, holder.info,
4068 true /*noisy*/, holder.noReleaseNeeded);
4069 if (holder.provider != null && provider != holder.provider) {
4070 if (localLOGV) {
4071 Slog.v(TAG, "acquireProvider: lost the race, releasing extraneous "
4072 + "reference to the content provider");
4073 }
4074 try {
4075 ActivityManagerNative.getDefault().removeContentProvider(
4076 getApplicationThread(), name);
4077 } catch (RemoteException ex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 }
4079 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080 return provider;
4081 }
4082
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004083 public final IContentProvider acquireExistingProvider(Context c, String name) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004084 synchronized (mProviderMap) {
4085 ProviderClientRecord pr = mProviderMap.get(name);
4086 if (pr == null) {
4087 return null;
4088 }
4089
4090 IContentProvider provider = pr.mProvider;
4091 IBinder jBinder = provider.asBinder();
4092
4093 // Only increment the ref count if we have one. If we don't then the
4094 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004095 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004096 if (prc != null) {
4097 prc.count += 1;
4098 if (prc.count == 1) {
4099 if (localLOGV) {
4100 Slog.v(TAG, "acquireExistingProvider: "
4101 + "snatched provider from the jaws of death");
4102 }
4103 // Because the provider previously had a reference count of zero,
4104 // it was scheduled to be removed. Cancel that.
4105 mH.removeMessages(H.REMOVE_PROVIDER, provider);
4106 }
4107 }
4108 return provider;
4109 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004110 }
4111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 public final boolean releaseProvider(IContentProvider provider) {
4113 if(provider == null) {
4114 return false;
4115 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004118 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004119 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004120 if (prc == null) {
4121 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004122 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004123 }
4124
4125 if (prc.count == 0) {
4126 if (localLOGV) Slog.v(TAG, "releaseProvider: ref count already 0, how?");
4127 return false;
4128 }
4129
4130 prc.count -= 1;
4131 if (prc.count == 0) {
4132 // Schedule the actual remove asynchronously, since we don't know the context
4133 // this will be called in.
4134 // TODO: it would be nice to post a delayed message, so
4135 // if we come back and need the same provider quickly
4136 // we will still have it available.
4137 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, provider);
4138 mH.sendMessage(msg);
4139 }
4140 return true;
4141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 }
4143
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004144 final void completeRemoveProvider(IContentProvider provider) {
4145 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004146 String remoteProviderName = null;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004147 synchronized(mProviderMap) {
4148 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004149 if (prc == null) {
4150 // Either no release is needed (so we shouldn't be here) or the
4151 // provider was already released.
4152 if (localLOGV) Slog.v(TAG, "completeRemoveProvider: release not needed");
4153 return;
4154 }
4155
4156 if (prc.count != 0) {
4157 // There was a race! Some other client managed to acquire
4158 // the provider before the removal was completed.
4159 // Abort the removal. We will do it later.
4160 if (localLOGV) Slog.v(TAG, "completeRemoveProvider: lost the race, "
4161 + "provider still in use");
4162 return;
4163 }
4164
4165 mProviderRefCountMap.remove(jBinder);
4166
4167 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
4168 while (iter.hasNext()) {
4169 ProviderClientRecord pr = iter.next();
4170 IBinder myBinder = pr.mProvider.asBinder();
4171 if (myBinder == jBinder) {
4172 iter.remove();
4173 if (pr.mLocalProvider == null) {
4174 myBinder.unlinkToDeath(pr, 0);
4175 if (remoteProviderName == null) {
4176 remoteProviderName = pr.mName;
4177 }
4178 }
4179 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004180 }
4181 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004182
4183 if (remoteProviderName != null) {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004184 try {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004185 if (localLOGV) {
4186 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4187 + "removeContentProvider(" + remoteProviderName + ")");
4188 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004189 ActivityManagerNative.getDefault().removeContentProvider(
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004190 getApplicationThread(), remoteProviderName);
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004191 } catch (RemoteException e) {
4192 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004193 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004194 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004196
4197 final void removeDeadProvider(String name, IContentProvider provider) {
4198 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004199 ProviderClientRecord pr = mProviderMap.get(name);
Jeff Brown2a4c5ac2011-08-23 11:58:26 -07004200 if (pr != null && pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004201 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004202 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004203 if (removed != null) {
4204 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
4205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004206 }
4207 }
4208 }
4209
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004210 /**
4211 * Installs the provider.
4212 *
4213 * Providers that are local to the process or that come from the system server
4214 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4215 * Other remote providers are reference counted. The initial reference count
4216 * for all reference counted providers is one. Providers that are not reference
4217 * counted do not have a reference count (at all).
4218 *
4219 * This method detects when a provider has already been installed. When this happens,
4220 * it increments the reference count of the existing provider (if appropriate)
4221 * and returns the existing provider. This can happen due to concurrent
4222 * attempts to acquire the same provider.
4223 */
Romain Guy65b345f2011-07-27 18:51:50 -07004224 private IContentProvider installProvider(Context context,
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004225 IContentProvider provider, ProviderInfo info,
4226 boolean noisy, boolean noReleaseNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 ContentProvider localProvider = null;
4228 if (provider == null) {
4229 if (noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004230 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004231 + info.name);
4232 }
4233 Context c = null;
4234 ApplicationInfo ai = info.applicationInfo;
4235 if (context.getPackageName().equals(ai.packageName)) {
4236 c = context;
4237 } else if (mInitialApplication != null &&
4238 mInitialApplication.getPackageName().equals(ai.packageName)) {
4239 c = mInitialApplication;
4240 } else {
4241 try {
4242 c = context.createPackageContext(ai.packageName,
4243 Context.CONTEXT_INCLUDE_CODE);
4244 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004245 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004246 }
4247 }
4248 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004249 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004250 ai.packageName +
4251 " while loading content provider " +
4252 info.name);
4253 return null;
4254 }
4255 try {
4256 final java.lang.ClassLoader cl = c.getClassLoader();
4257 localProvider = (ContentProvider)cl.
4258 loadClass(info.name).newInstance();
4259 provider = localProvider.getIContentProvider();
4260 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004261 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004262 info.name + " from sourceDir " +
4263 info.applicationInfo.sourceDir);
4264 return null;
4265 }
Joe Onorato43a17652011-04-06 19:22:23 -07004266 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 TAG, "Instantiating local provider " + info.name);
4268 // XXX Need to create the correct context for this provider.
4269 localProvider.attachInfo(c, info);
4270 } catch (java.lang.Exception e) {
4271 if (!mInstrumentation.onException(null, e)) {
4272 throw new RuntimeException(
4273 "Unable to get provider " + info.name
4274 + ": " + e.toString(), e);
4275 }
4276 return null;
4277 }
4278 } else if (localLOGV) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004279 Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280 + info.name);
4281 }
4282
4283 synchronized (mProviderMap) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004284 // There is a possibility that this thread raced with another thread to
4285 // add the provider. If we find another thread got there first then we
4286 // just get out of the way and return the original provider.
4287 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 String names[] = PATTERN_SEMICOLON.split(info.authority);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004289 for (int i = 0; i < names.length; i++) {
4290 ProviderClientRecord pr = mProviderMap.get(names[i]);
4291 if (pr != null) {
4292 if (localLOGV) {
4293 Slog.v(TAG, "installProvider: lost the race, "
4294 + "using existing named provider");
4295 }
4296 provider = pr.mProvider;
4297 } else {
4298 pr = new ProviderClientRecord(names[i], provider, localProvider);
4299 if (localProvider == null) {
4300 try {
4301 jBinder.linkToDeath(pr, 0);
4302 } catch (RemoteException e) {
4303 // Provider already dead. Bail out of here without making
4304 // any changes to the provider map or other data structures.
4305 return null;
4306 }
4307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004308 mProviderMap.put(names[i], pr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 }
4310 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312 if (localProvider != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004313 ProviderClientRecord pr = mLocalProviders.get(jBinder);
4314 if (pr != null) {
4315 if (localLOGV) {
4316 Slog.v(TAG, "installProvider: lost the race, "
4317 + "using existing local provider");
4318 }
4319 provider = pr.mProvider;
4320 } else {
4321 pr = new ProviderClientRecord(null, provider, localProvider);
4322 mLocalProviders.put(jBinder, pr);
4323 }
4324 }
4325
4326 if (!noReleaseNeeded) {
4327 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4328 if (prc != null) {
4329 if (localLOGV) {
4330 Slog.v(TAG, "installProvider: lost the race, incrementing ref count");
4331 }
4332 prc.count += 1;
4333 if (prc.count == 1) {
4334 if (localLOGV) {
4335 Slog.v(TAG, "installProvider: "
4336 + "snatched provider from the jaws of death");
4337 }
4338 // Because the provider previously had a reference count of zero,
4339 // it was scheduled to be removed. Cancel that.
4340 mH.removeMessages(H.REMOVE_PROVIDER, provider);
4341 }
4342 } else {
4343 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
4344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004345 }
4346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004347 return provider;
4348 }
4349
Romain Guy65b345f2011-07-27 18:51:50 -07004350 private void attach(boolean system) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 sThreadLocal.set(this);
4352 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004353 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004354 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004355 public void run() {
4356 ensureJitEnabled();
4357 }
4358 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4360 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4361 IActivityManager mgr = ActivityManagerNative.getDefault();
4362 try {
4363 mgr.attachApplication(mAppThread);
4364 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004365 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004366 }
4367 } else {
4368 // Don't set application object here -- if the system crashes,
4369 // we can't display an alert, we just want to die die die.
4370 android.ddm.DdmHandleAppName.setAppName("system_process");
4371 try {
4372 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004373 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 context.init(getSystemContext().mPackageInfo, null, this);
4375 Application app = Instrumentation.newApplication(Application.class, context);
4376 mAllApplications.add(app);
4377 mInitialApplication = app;
4378 app.onCreate();
4379 } catch (Exception e) {
4380 throw new RuntimeException(
4381 "Unable to instantiate Application():" + e.toString(), e);
4382 }
4383 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004384
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004385 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004386 public void onConfigurationChanged(Configuration newConfig) {
4387 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004388 // We need to apply this change to the resources
4389 // immediately, because upon returning the view
4390 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004391 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004392 // This actually changed the resources! Tell
4393 // everyone about it.
4394 if (mPendingConfiguration == null ||
4395 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4396 mPendingConfiguration = newConfig;
4397
4398 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4399 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004400 }
4401 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004402 }
4403 public void onLowMemory() {
4404 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004405 public void onTrimMemory(int level) {
4406 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004407 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 }
4409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 public static final ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004411 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004412 ActivityThread thread = new ActivityThread();
4413 thread.attach(true);
4414 return thread;
4415 }
4416
Jeff Brown10e89712011-07-08 18:52:57 -07004417 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004418 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004419 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004420 }
4421 }
4422
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004423 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004424 synchronized (mPackages) {
4425 if (mCoreSettings != null) {
4426 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004427 } else {
4428 return defaultValue;
4429 }
4430 }
4431 }
4432
Romain Guy65b345f2011-07-27 18:51:50 -07004433 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004434 SamplingProfilerIntegration.start();
4435
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004436 // CloseGuard defaults to true and can be quite spammy. We
4437 // disable it here, but selectively enable it later (via
4438 // StrictMode) on debug builds, but using DropBox, not logs.
4439 CloseGuard.setEnabled(false);
4440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004441 Process.setArgV0("<pre-initialized>");
4442
4443 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07004444 if (sMainThreadHandler == null) {
4445 sMainThreadHandler = new Handler();
4446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004447
4448 ActivityThread thread = new ActivityThread();
4449 thread.attach(false);
4450
Dianne Hackborn287952c2010-09-22 22:34:31 -07004451 if (false) {
4452 Looper.myLooper().setMessageLogging(new
4453 LogPrinter(Log.DEBUG, "ActivityThread"));
4454 }
4455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004456 Looper.loop();
4457
Jeff Brown10e89712011-07-08 18:52:57 -07004458 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 }
4460}