blob: 00fe953f70e336897fc7cbfab3ff2ea6127f06c2 [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;
Joe Onoratod630f102011-03-17 18:42:26 -070048import android.os.AsyncTask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.Bundle;
50import android.os.Debug;
51import android.os.Handler;
52import android.os.IBinder;
53import android.os.Looper;
54import android.os.Message;
55import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070056import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.os.Process;
58import android.os.RemoteException;
59import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070060import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.SystemClock;
62import android.util.AndroidRuntimeException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.util.DisplayMetrics;
64import android.util.EventLog;
65import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070066import android.util.LogPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080067import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070069import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.view.View;
71import android.view.ViewDebug;
72import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070073import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.view.Window;
75import android.view.WindowManager;
76import android.view.WindowManagerImpl;
77
78import com.android.internal.os.BinderInternal;
79import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070080import com.android.internal.os.SamplingProfilerIntegration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
82import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl;
83
84import java.io.File;
85import java.io.FileDescriptor;
86import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070087import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.io.PrintWriter;
89import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -070090import java.net.InetAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import java.util.ArrayList;
92import java.util.HashMap;
93import java.util.Iterator;
94import java.util.List;
95import java.util.Locale;
96import java.util.Map;
97import java.util.TimeZone;
98import java.util.regex.Pattern;
99
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800100import dalvik.system.CloseGuard;
Bob Leee5408332009-09-04 18:31:17 -0700101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102final class SuperNotCalledException extends AndroidRuntimeException {
103 public SuperNotCalledException(String msg) {
104 super(msg);
105 }
106}
107
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700108final class RemoteServiceException extends AndroidRuntimeException {
109 public RemoteServiceException(String msg) {
110 super(msg);
111 }
112}
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114/**
115 * This manages the execution of the main thread in an
116 * application process, scheduling and executing activities,
117 * broadcasts, and other operations on it as the activity
118 * manager requests.
119 *
120 * {@hide}
121 */
122public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700123 /** @hide */
124 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700125 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700126 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700127 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700128 /** @hide */
129 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700130 private static final boolean DEBUG_RESULTS = false;
Christopher Tate4a627c72011-04-01 14:43:32 -0700131 private static final boolean DEBUG_BACKUP = true;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700132 private static final boolean DEBUG_CONFIGURATION = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
134 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
135 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
136 private static final int LOG_ON_PAUSE_CALLED = 30021;
137 private static final int LOG_ON_RESUME_CALLED = 30022;
138
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700139 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700140
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700141 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700143 final ApplicationThread mAppThread = new ApplicationThread();
144 final Looper mLooper = Looper.myLooper();
145 final H mH = new H();
146 final HashMap<IBinder, ActivityClientRecord> mActivities
147 = new HashMap<IBinder, ActivityClientRecord>();
148 // List of new activities (via ActivityRecord.nextIdle) that should
149 // be reported when next we idle.
150 ActivityClientRecord mNewActivities = null;
151 // Number of activities that are currently visible on-screen.
152 int mNumVisibleActivities = 0;
153 final HashMap<IBinder, Service> mServices
154 = new HashMap<IBinder, Service>();
155 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700156 Profiler mProfiler;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700157 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700158 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700159 Configuration mResConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700160 CompatibilityInfo mResCompatibilityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700161 Application mInitialApplication;
162 final ArrayList<Application> mAllApplications
163 = new ArrayList<Application>();
164 // set of instantiated backup agents, keyed by package name
165 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Romain Guy65b345f2011-07-27 18:51:50 -0700166 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700167 Instrumentation mInstrumentation;
168 String mInstrumentationAppDir = null;
169 String mInstrumentationAppPackage = null;
170 String mInstrumentedAppDir = null;
171 boolean mSystemThread = false;
172 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700174 // These can be accessed by multiple threads; mPackages is the lock.
175 // XXX For now we keep around information about all packages we have
176 // seen, not removing entries from this map.
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700177 // NOTE: The activity manager in its process needs to call in to
178 // ActivityThread to do things like update resource configurations,
179 // which means this lock gets held while the activity manager holds its
180 // own lock. Thus you MUST NEVER call back into the activity manager
181 // or anything that depends on it while holding this lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700182 final HashMap<String, WeakReference<LoadedApk>> mPackages
183 = new HashMap<String, WeakReference<LoadedApk>>();
184 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
185 = new HashMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700186 final HashMap<CompatibilityInfo, DisplayMetrics> mDisplayMetrics
187 = new HashMap<CompatibilityInfo, DisplayMetrics>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700188 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
189 = new HashMap<ResourcesKey, WeakReference<Resources> >();
190 final ArrayList<ActivityClientRecord> mRelaunchingActivities
191 = new ArrayList<ActivityClientRecord>();
192 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700194 // The lock of mProviderMap protects the following variables.
195 final HashMap<String, ProviderClientRecord> mProviderMap
196 = new HashMap<String, ProviderClientRecord>();
197 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
198 = new HashMap<IBinder, ProviderRefCount>();
199 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
200 = new HashMap<IBinder, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201
Jeff Hamilton52d32032011-01-08 15:31:26 -0600202 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
203 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
204
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700205 final GcIdler mGcIdler = new GcIdler();
206 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700208 static Handler sMainThreadHandler; // set once in main()
209
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800210 Bundle mCoreSettings = null;
211
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400212 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700214 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 Intent intent;
216 Bundle state;
217 Activity activity;
218 Window window;
219 Activity parent;
220 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700221 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 boolean paused;
223 boolean stopped;
224 boolean hideForNow;
225 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700226 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700227 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700229 String profileFile;
230 ParcelFileDescriptor profileFd;
231 boolean autoStopProfiler;
232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400234 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700235 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
237 List<ResultInfo> pendingResults;
238 List<Intent> pendingIntents;
239
240 boolean startsNotResumed;
241 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800242 int pendingConfigChanges;
243 boolean onlyLocalRequest;
244
245 View mPendingRemoveWindow;
246 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700248 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 parent = null;
250 embeddedID = null;
251 paused = false;
252 stopped = false;
253 hideForNow = false;
254 nextIdle = null;
255 }
256
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800257 public boolean isPreHoneycomb() {
258 if (activity != null) {
259 return activity.getApplicationInfo().targetSdkVersion
260 < android.os.Build.VERSION_CODES.HONEYCOMB;
261 }
262 return false;
263 }
264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 public String toString() {
266 ComponentName componentName = intent.getComponent();
267 return "ActivityRecord{"
268 + Integer.toHexString(System.identityHashCode(this))
269 + " token=" + token + " " + (componentName == null
270 ? "no component name" : componentName.toShortString())
271 + "}";
272 }
273 }
274
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400275 final class ProviderClientRecord implements IBinder.DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 final String mName;
277 final IContentProvider mProvider;
278 final ContentProvider mLocalProvider;
279
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700280 ProviderClientRecord(String name, IContentProvider provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 ContentProvider localProvider) {
282 mName = name;
283 mProvider = provider;
284 mLocalProvider = localProvider;
285 }
286
287 public void binderDied() {
288 removeDeadProvider(mName, mProvider);
289 }
290 }
291
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400292 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 List<Intent> intents;
294 IBinder token;
295 public String toString() {
296 return "NewIntentData{intents=" + intents + " token=" + token + "}";
297 }
298 }
299
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400300 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700301 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
302 boolean ordered, boolean sticky, IBinder token) {
303 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky, token);
304 this.intent = intent;
305 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 Intent intent;
308 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400309 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 public String toString() {
311 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700312 info.packageName + " resultCode=" + getResultCode()
313 + " resultData=" + getResultData() + " resultExtras="
314 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 }
316 }
317
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400318 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700319 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400320 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700321 int backupMode;
322 public String toString() {
323 return "CreateBackupAgentData{appInfo=" + appInfo
324 + " backupAgent=" + appInfo.backupAgentName
325 + " mode=" + backupMode + "}";
326 }
327 }
Bob Leee5408332009-09-04 18:31:17 -0700328
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400329 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 IBinder token;
331 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400332 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 Intent intent;
334 public String toString() {
335 return "CreateServiceData{token=" + token + " className="
336 + info.name + " packageName=" + info.packageName
337 + " intent=" + intent + "}";
338 }
339 }
340
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400341 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 IBinder token;
343 Intent intent;
344 boolean rebind;
345 public String toString() {
346 return "BindServiceData{token=" + token + " intent=" + intent + "}";
347 }
348 }
349
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400350 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700352 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700354 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 Intent args;
356 public String toString() {
357 return "ServiceArgsData{token=" + token + " startId=" + startId
358 + " args=" + args + "}";
359 }
360 }
361
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400362 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700363 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 String processName;
365 ApplicationInfo appInfo;
366 List<ProviderInfo> providers;
367 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 Bundle instrumentationArgs;
369 IInstrumentationWatcher instrumentationWatcher;
370 int debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700371 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700372 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400374 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700375
376 /** Initial values for {@link Profiler}. */
377 String initProfileFile;
378 ParcelFileDescriptor initProfileFd;
379 boolean initAutoStopProfiler;
380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 public String toString() {
382 return "AppBindData{appInfo=" + appInfo + "}";
383 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700384 }
385
386 static final class Profiler {
387 String profileFile;
388 ParcelFileDescriptor profileFd;
389 boolean autoStopProfiler;
390 boolean profiling;
391 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700392 public void setProfiler(String file, ParcelFileDescriptor fd) {
393 if (profiling) {
394 if (fd != null) {
395 try {
396 fd.close();
397 } catch (IOException e) {
398 }
399 }
400 return;
401 }
402 if (profileFd != null) {
403 try {
404 profileFd.close();
405 } catch (IOException e) {
406 }
407 }
408 profileFile = file;
409 profileFd = fd;
410 }
411 public void startProfiling() {
412 if (profileFd == null || profiling) {
413 return;
414 }
415 try {
416 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
417 8 * 1024 * 1024, 0);
418 profiling = true;
419 } catch (RuntimeException e) {
420 Slog.w(TAG, "Profiling failed on path " + profileFile);
421 try {
422 profileFd.close();
423 profileFd = null;
424 } catch (IOException e2) {
425 Slog.w(TAG, "Failure closing profile fd", e2);
426 }
427 }
428 }
429 public void stopProfiling() {
430 if (profiling) {
431 profiling = false;
432 Debug.stopMethodTracing();
433 if (profileFd != null) {
434 try {
435 profileFd.close();
436 } catch (IOException e) {
437 }
438 }
439 profileFd = null;
440 profileFile = null;
441 }
442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 }
444
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400445 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700446 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700447 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800448 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 }
451
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400452 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 IBinder token;
454 List<ResultInfo> results;
455 public String toString() {
456 return "ResultData{token=" + token + " results" + results + "}";
457 }
458 }
459
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400460 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800461 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 String what;
463 String who;
464 }
465
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400466 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700467 String path;
468 ParcelFileDescriptor fd;
469 }
470
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400471 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700472 String path;
473 ParcelFileDescriptor fd;
474 }
475
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400476 static final class UpdateCompatibilityData {
477 String pkg;
478 CompatibilityInfo info;
479 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700480
Romain Guy65b345f2011-07-27 18:51:50 -0700481 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700482
Romain Guy65b345f2011-07-27 18:51:50 -0700483 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700484 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
485 private static final String ONE_COUNT_COLUMN = "%21s %8d";
486 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
487 private static final String TWO_COUNT_COLUMNS_DB = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700488 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 // Formatting for checkin service - update version if row format changes
491 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700492
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700493 private void updatePendingConfiguration(Configuration config) {
494 synchronized (mPackages) {
495 if (mPendingConfiguration == null ||
496 mPendingConfiguration.isOtherSeqNewer(config)) {
497 mPendingConfiguration = config;
498 }
499 }
500 }
501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 public final void schedulePauseActivity(IBinder token, boolean finished,
503 boolean userLeaving, int configChanges) {
504 queueOrSendMessage(
505 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
506 token,
507 (userLeaving ? 1 : 0),
508 configChanges);
509 }
510
511 public final void scheduleStopActivity(IBinder token, boolean showWindow,
512 int configChanges) {
513 queueOrSendMessage(
514 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
515 token, 0, configChanges);
516 }
517
518 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
519 queueOrSendMessage(
520 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
521 token);
522 }
523
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800524 public final void scheduleSleeping(IBinder token, boolean sleeping) {
525 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
526 }
527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
529 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
530 }
531
532 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
533 ResultData res = new ResultData();
534 res.token = token;
535 res.results = results;
536 queueOrSendMessage(H.SEND_RESULT, res);
537 }
538
539 // we use token to identify this activity without having to send the
540 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700541 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700542 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
543 Bundle state, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700544 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
545 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700546 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547
548 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700549 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 r.intent = intent;
551 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400552 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 r.state = state;
554
555 r.pendingResults = pendingResults;
556 r.pendingIntents = pendingNewIntents;
557
558 r.startsNotResumed = notResumed;
559 r.isForward = isForward;
560
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700561 r.profileFile = profileName;
562 r.profileFd = profileFd;
563 r.autoStopProfiler = autoStopProfiler;
564
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700565 updatePendingConfiguration(curConfig);
566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
568 }
569
570 public final void scheduleRelaunchActivity(IBinder token,
571 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800572 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800573 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
574 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 }
576
577 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
578 NewIntentData data = new NewIntentData();
579 data.intents = intents;
580 data.token = token;
581
582 queueOrSendMessage(H.NEW_INTENT, data);
583 }
584
585 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
586 int configChanges) {
587 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
588 configChanges);
589 }
590
591 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400592 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
593 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700594 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
595 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400597 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 queueOrSendMessage(H.RECEIVER, r);
599 }
600
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400601 public final void scheduleCreateBackupAgent(ApplicationInfo app,
602 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700603 CreateBackupAgentData d = new CreateBackupAgentData();
604 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400605 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700606 d.backupMode = backupMode;
607
608 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
609 }
610
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400611 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
612 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700613 CreateBackupAgentData d = new CreateBackupAgentData();
614 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400615 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700616
617 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
618 }
619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400621 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 CreateServiceData s = new CreateServiceData();
623 s.token = token;
624 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400625 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626
627 queueOrSendMessage(H.CREATE_SERVICE, s);
628 }
629
630 public final void scheduleBindService(IBinder token, Intent intent,
631 boolean rebind) {
632 BindServiceData s = new BindServiceData();
633 s.token = token;
634 s.intent = intent;
635 s.rebind = rebind;
636
637 queueOrSendMessage(H.BIND_SERVICE, s);
638 }
639
640 public final void scheduleUnbindService(IBinder token, Intent intent) {
641 BindServiceData s = new BindServiceData();
642 s.token = token;
643 s.intent = intent;
644
645 queueOrSendMessage(H.UNBIND_SERVICE, s);
646 }
647
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700648 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700649 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 ServiceArgsData s = new ServiceArgsData();
651 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700652 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700654 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 s.args = args;
656
657 queueOrSendMessage(H.SERVICE_ARGS, s);
658 }
659
660 public final void scheduleStopService(IBinder token) {
661 queueOrSendMessage(H.STOP_SERVICE, token);
662 }
663
664 public final void bindApplication(String processName,
665 ApplicationInfo appInfo, List<ProviderInfo> providers,
666 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700667 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700669 int debugMode, boolean isRestrictedBackupMode, boolean persistent,
670 Configuration config, CompatibilityInfo compatInfo,
671 Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672
673 if (services != null) {
674 // Setup the service cache in the ServiceManager
675 ServiceManager.initServiceCache(services);
676 }
677
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800678 setCoreSettings(coreSettings);
679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 AppBindData data = new AppBindData();
681 data.processName = processName;
682 data.appInfo = appInfo;
683 data.providers = providers;
684 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 data.instrumentationArgs = instrumentationArgs;
686 data.instrumentationWatcher = instrumentationWatcher;
687 data.debugMode = debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700688 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700689 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400691 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700692 data.initProfileFile = profileFile;
693 data.initProfileFd = profileFd;
694 data.initAutoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 queueOrSendMessage(H.BIND_APPLICATION, data);
696 }
697
698 public final void scheduleExit() {
699 queueOrSendMessage(H.EXIT_APPLICATION, null);
700 }
701
Christopher Tate5e1ab332009-09-01 20:32:49 -0700702 public final void scheduleSuicide() {
703 queueOrSendMessage(H.SUICIDE, null);
704 }
705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 public void requestThumbnail(IBinder token) {
707 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
708 }
709
710 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700711 updatePendingConfiguration(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
713 }
714
715 public void updateTimeZone() {
716 TimeZone.setDefault(null);
717 }
718
Robert Greenwalt03595d02010-11-02 14:08:23 -0700719 public void clearDnsCache() {
720 // a non-standard API to get this to libcore
721 InetAddress.clearDnsCache();
722 }
723
Robert Greenwalt434203a2010-10-11 16:00:27 -0700724 public void setHttpProxy(String host, String port, String exclList) {
725 Proxy.setHttpProxySystemProperty(host, port, exclList);
726 }
727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 public void processInBackground() {
729 mH.removeMessages(H.GC_WHEN_IDLE);
730 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
731 }
732
733 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700734 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700735 try {
736 data.fd = ParcelFileDescriptor.dup(fd);
737 data.token = servicetoken;
738 data.args = args;
739 queueOrSendMessage(H.DUMP_SERVICE, data);
740 } catch (IOException e) {
741 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 }
743 }
744
745 // This function exists to make sure all receiver dispatching is
746 // correctly ordered, since these are one-way calls and the binder driver
747 // applies transaction ordering per object for such calls.
748 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700749 int resultCode, String dataStr, Bundle extras, boolean ordered,
750 boolean sticky) throws RemoteException {
751 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 }
Bob Leee5408332009-09-04 18:31:17 -0700753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 public void scheduleLowMemory() {
755 queueOrSendMessage(H.LOW_MEMORY, null);
756 }
757
758 public void scheduleActivityConfigurationChanged(IBinder token) {
759 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
760 }
761
Romain Guy7eabe552011-07-21 14:56:34 -0700762 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
763 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700764 ProfilerControlData pcd = new ProfilerControlData();
765 pcd.path = path;
766 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700767 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800768 }
769
Andy McFadden824c5102010-07-09 16:26:57 -0700770 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
771 DumpHeapData dhd = new DumpHeapData();
772 dhd.path = path;
773 dhd.fd = fd;
774 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
775 }
776
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700777 public void setSchedulingGroup(int group) {
778 // Note: do this immediately, since going into the foreground
779 // should happen regardless of what pending work we have to do
780 // and the activity manager will wait for us to report back that
781 // we are done before sending us to the background.
782 try {
783 Process.setProcessGroup(Process.myPid(), group);
784 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800785 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700786 }
787 }
Bob Leee5408332009-09-04 18:31:17 -0700788
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700789 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
790 Debug.getMemoryInfo(outInfo);
791 }
Bob Leee5408332009-09-04 18:31:17 -0700792
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700793 public void dispatchPackageBroadcast(int cmd, String[] packages) {
794 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
795 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700796
797 public void scheduleCrash(String msg) {
798 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
799 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700800
Dianne Hackborn30d71892010-12-11 10:37:55 -0800801 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
802 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700803 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700804 try {
805 data.fd = ParcelFileDescriptor.dup(fd);
806 data.token = activitytoken;
807 data.prefix = prefix;
808 data.args = args;
809 queueOrSendMessage(H.DUMP_ACTIVITY, data);
810 } catch (IOException e) {
811 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700812 }
813 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700816 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
817 boolean all, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700818 FileOutputStream fout = new FileOutputStream(fd);
819 PrintWriter pw = new PrintWriter(fout);
820 try {
Dianne Hackbornb437e092011-08-05 17:50:29 -0700821 return dumpMemInfo(pw, checkin, all, args);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700822 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700823 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700824 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700825 }
826
Dianne Hackbornb437e092011-08-05 17:50:29 -0700827 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean all,
828 String[] args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 long nativeMax = Debug.getNativeHeapSize() / 1024;
830 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
831 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
832
833 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
834 Debug.getMemoryInfo(memInfo);
835
Dianne Hackbornb437e092011-08-05 17:50:29 -0700836 if (!all) {
837 return memInfo;
838 }
839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 Runtime runtime = Runtime.getRuntime();
841
842 long dalvikMax = runtime.totalMemory() / 1024;
843 long dalvikFree = runtime.freeMemory() / 1024;
844 long dalvikAllocated = dalvikMax - dalvikFree;
845 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700846 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700847 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
848 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 int globalAssetCount = AssetManager.getGlobalAssetCount();
850 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
851 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
852 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
853 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700854 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 long sqliteAllocated = SQLiteDebug.getHeapAllocatedSize() / 1024;
Vasu Noric3849202010-03-09 10:47:25 -0800856 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700859 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 // NOTE: if you change anything significant below, also consider changing
861 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700862 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 // Header
866 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
867 pw.print(Process.myPid()); pw.print(',');
868 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 // Heap info - max
871 pw.print(nativeMax); pw.print(',');
872 pw.print(dalvikMax); pw.print(',');
873 pw.print("N/A,");
874 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 // Heap info - allocated
877 pw.print(nativeAllocated); pw.print(',');
878 pw.print(dalvikAllocated); pw.print(',');
879 pw.print("N/A,");
880 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 // Heap info - free
883 pw.print(nativeFree); pw.print(',');
884 pw.print(dalvikFree); pw.print(',');
885 pw.print("N/A,");
886 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 // Heap info - proportional set size
889 pw.print(memInfo.nativePss); pw.print(',');
890 pw.print(memInfo.dalvikPss); pw.print(',');
891 pw.print(memInfo.otherPss); pw.print(',');
892 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700895 pw.print(memInfo.nativeSharedDirty); pw.print(',');
896 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
897 pw.print(memInfo.otherSharedDirty); pw.print(',');
898 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
899 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700902 pw.print(memInfo.nativePrivateDirty); pw.print(',');
903 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
904 pw.print(memInfo.otherPrivateDirty); pw.print(',');
905 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
906 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 // Object counts
909 pw.print(viewInstanceCount); pw.print(',');
910 pw.print(viewRootInstanceCount); pw.print(',');
911 pw.print(appContextInstanceCount); pw.print(',');
912 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 pw.print(globalAssetCount); pw.print(',');
915 pw.print(globalAssetManagerCount); pw.print(',');
916 pw.print(binderLocalObjectCount); pw.print(',');
917 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 pw.print(binderDeathObjectCount); pw.print(',');
920 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 // SQL
923 pw.print(sqliteAllocated); pw.print(',');
Vasu Noric3849202010-03-09 10:47:25 -0800924 pw.print(stats.memoryUsed / 1024); pw.print(',');
925 pw.print(stats.pageCacheOverflo / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700926 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800927 for (int i = 0; i < stats.dbStats.size(); i++) {
928 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700929 pw.print(','); pw.print(dbStats.dbName);
930 pw.print(','); pw.print(dbStats.pageSize);
931 pw.print(','); pw.print(dbStats.dbSize);
932 pw.print(','); pw.print(dbStats.lookaside);
933 pw.print(','); pw.print(dbStats.cache);
934 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800935 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700936 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700937
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700938 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 }
Bob Leee5408332009-09-04 18:31:17 -0700940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700942 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
943 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
944 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
945 "------");
946 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
947 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
948 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
949 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700951 int otherPss = memInfo.otherPss;
952 int otherSharedDirty = memInfo.otherSharedDirty;
953 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700955 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Romain Guy65b345f2011-07-27 18:51:50 -0700956 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700957 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
958 memInfo.getOtherPrivateDirty(i), "", "", "");
959 otherPss -= memInfo.getOtherPss(i);
960 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
961 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
962 }
963
964 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
965 otherPrivateDirty, "", "", "");
966 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
967 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
968 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
969 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970
971 pw.println(" ");
972 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -0700973 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 viewRootInstanceCount);
975
976 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
977 "Activities:", activityInstanceCount);
978
979 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
980 "AssetManagers:", globalAssetManagerCount);
981
982 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
983 "Proxy Binders:", binderProxyObjectCount);
984 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
985
986 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -0700987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 // SQLite mem info
989 pw.println(" ");
990 pw.println(" SQL");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700991 printRow(pw, TWO_COUNT_COLUMNS_DB, "heap:", sqliteAllocated, "MEMORY_USED:",
Vasu Noric3849202010-03-09 10:47:25 -0800992 stats.memoryUsed / 1024);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700993 printRow(pw, TWO_COUNT_COLUMNS_DB, "PAGECACHE_OVERFLOW:",
994 stats.pageCacheOverflo / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800995 pw.println(" ");
996 int N = stats.dbStats.size();
997 if (N > 0) {
998 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700999 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1000 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001001 for (int i = 0; i < N; i++) {
1002 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001003 printRow(pw, DB_INFO_FORMAT,
1004 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1005 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1006 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1007 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001008 }
1009 }
Bob Leee5408332009-09-04 18:31:17 -07001010
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001011 // Asset details.
1012 String assetAlloc = AssetManager.getAssetAllocations();
1013 if (assetAlloc != null) {
1014 pw.println(" ");
1015 pw.println(" Asset Allocations");
1016 pw.print(assetAlloc);
1017 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001018
1019 return memInfo;
1020 }
1021
1022 @Override
1023 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1024 dumpGraphicsInfo(fd);
Romain Guy65b345f2011-07-27 18:51:50 -07001025 WindowManagerImpl.getDefault().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 }
1027
1028 private void printRow(PrintWriter pw, String format, Object...objs) {
1029 pw.println(String.format(format, objs));
1030 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001031
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001032 public void setCoreSettings(Bundle coreSettings) {
1033 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001034 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001035
1036 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1037 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1038 ucd.pkg = pkg;
1039 ucd.info = info;
1040 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1041 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001042
1043 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001044 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001045 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 }
1047
Romain Guy65b345f2011-07-27 18:51:50 -07001048 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 public static final int LAUNCH_ACTIVITY = 100;
1050 public static final int PAUSE_ACTIVITY = 101;
1051 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1052 public static final int STOP_ACTIVITY_SHOW = 103;
1053 public static final int STOP_ACTIVITY_HIDE = 104;
1054 public static final int SHOW_WINDOW = 105;
1055 public static final int HIDE_WINDOW = 106;
1056 public static final int RESUME_ACTIVITY = 107;
1057 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001058 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 public static final int BIND_APPLICATION = 110;
1060 public static final int EXIT_APPLICATION = 111;
1061 public static final int NEW_INTENT = 112;
1062 public static final int RECEIVER = 113;
1063 public static final int CREATE_SERVICE = 114;
1064 public static final int SERVICE_ARGS = 115;
1065 public static final int STOP_SERVICE = 116;
1066 public static final int REQUEST_THUMBNAIL = 117;
1067 public static final int CONFIGURATION_CHANGED = 118;
1068 public static final int CLEAN_UP_CONTEXT = 119;
1069 public static final int GC_WHEN_IDLE = 120;
1070 public static final int BIND_SERVICE = 121;
1071 public static final int UNBIND_SERVICE = 122;
1072 public static final int DUMP_SERVICE = 123;
1073 public static final int LOW_MEMORY = 124;
1074 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1075 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001076 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001077 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001078 public static final int DESTROY_BACKUP_AGENT = 129;
1079 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001080 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001081 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001082 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001083 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001084 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001085 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001086 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001087 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001088 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001089 public static final int TRIM_MEMORY = 140;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001091 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 switch (code) {
1093 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1094 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1095 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1096 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1097 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1098 case SHOW_WINDOW: return "SHOW_WINDOW";
1099 case HIDE_WINDOW: return "HIDE_WINDOW";
1100 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1101 case SEND_RESULT: return "SEND_RESULT";
1102 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1103 case BIND_APPLICATION: return "BIND_APPLICATION";
1104 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1105 case NEW_INTENT: return "NEW_INTENT";
1106 case RECEIVER: return "RECEIVER";
1107 case CREATE_SERVICE: return "CREATE_SERVICE";
1108 case SERVICE_ARGS: return "SERVICE_ARGS";
1109 case STOP_SERVICE: return "STOP_SERVICE";
1110 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1111 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1112 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1113 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1114 case BIND_SERVICE: return "BIND_SERVICE";
1115 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1116 case DUMP_SERVICE: return "DUMP_SERVICE";
1117 case LOW_MEMORY: return "LOW_MEMORY";
1118 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1119 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001120 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001121 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1122 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001123 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001124 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001125 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001126 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001127 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001128 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001129 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001130 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001131 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001132 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001133 case TRIM_MEMORY: return "TRIM_MEMORY";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 }
1135 }
1136 return "(unknown)";
1137 }
1138 public void handleMessage(Message msg) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001139 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 switch (msg.what) {
1141 case LAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001142 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143
1144 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001145 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001146 handleLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 } break;
1148 case RELAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001149 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001150 handleRelaunchActivity(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 } break;
1152 case PAUSE_ACTIVITY:
1153 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001154 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 break;
1156 case PAUSE_ACTIVITY_FINISHING:
1157 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
1158 break;
1159 case STOP_ACTIVITY_SHOW:
1160 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
1161 break;
1162 case STOP_ACTIVITY_HIDE:
1163 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
1164 break;
1165 case SHOW_WINDOW:
1166 handleWindowVisibility((IBinder)msg.obj, true);
1167 break;
1168 case HIDE_WINDOW:
1169 handleWindowVisibility((IBinder)msg.obj, false);
1170 break;
1171 case RESUME_ACTIVITY:
1172 handleResumeActivity((IBinder)msg.obj, true,
1173 msg.arg1 != 0);
1174 break;
1175 case SEND_RESULT:
1176 handleSendResult((ResultData)msg.obj);
1177 break;
1178 case DESTROY_ACTIVITY:
1179 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1180 msg.arg2, false);
1181 break;
1182 case BIND_APPLICATION:
1183 AppBindData data = (AppBindData)msg.obj;
1184 handleBindApplication(data);
1185 break;
1186 case EXIT_APPLICATION:
1187 if (mInitialApplication != null) {
1188 mInitialApplication.onTerminate();
1189 }
1190 Looper.myLooper().quit();
1191 break;
1192 case NEW_INTENT:
1193 handleNewIntent((NewIntentData)msg.obj);
1194 break;
1195 case RECEIVER:
1196 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001197 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 break;
1199 case CREATE_SERVICE:
1200 handleCreateService((CreateServiceData)msg.obj);
1201 break;
1202 case BIND_SERVICE:
1203 handleBindService((BindServiceData)msg.obj);
1204 break;
1205 case UNBIND_SERVICE:
1206 handleUnbindService((BindServiceData)msg.obj);
1207 break;
1208 case SERVICE_ARGS:
1209 handleServiceArgs((ServiceArgsData)msg.obj);
1210 break;
1211 case STOP_SERVICE:
1212 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001213 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 break;
1215 case REQUEST_THUMBNAIL:
1216 handleRequestThumbnail((IBinder)msg.obj);
1217 break;
1218 case CONFIGURATION_CHANGED:
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001219 handleConfigurationChanged((Configuration)msg.obj, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 break;
1221 case CLEAN_UP_CONTEXT:
1222 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1223 cci.context.performFinalCleanup(cci.who, cci.what);
1224 break;
1225 case GC_WHEN_IDLE:
1226 scheduleGcIdler();
1227 break;
1228 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001229 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 break;
1231 case LOW_MEMORY:
1232 handleLowMemory();
1233 break;
1234 case ACTIVITY_CONFIGURATION_CHANGED:
1235 handleActivityConfigurationChanged((IBinder)msg.obj);
1236 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001237 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001238 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001239 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001240 case CREATE_BACKUP_AGENT:
1241 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
1242 break;
1243 case DESTROY_BACKUP_AGENT:
1244 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
1245 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001246 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001247 Process.killProcess(Process.myPid());
1248 break;
1249 case REMOVE_PROVIDER:
1250 completeRemoveProvider((IContentProvider)msg.obj);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001251 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001252 case ENABLE_JIT:
1253 ensureJitEnabled();
1254 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001255 case DISPATCH_PACKAGE_BROADCAST:
1256 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
1257 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001258 case SCHEDULE_CRASH:
1259 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001260 case DUMP_HEAP:
1261 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1262 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001263 case DUMP_ACTIVITY:
1264 handleDumpActivity((DumpComponentInfo)msg.obj);
1265 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001266 case SLEEPING:
1267 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
1268 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001269 case SET_CORE_SETTINGS:
1270 handleSetCoreSettings((Bundle) msg.obj);
1271 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001272 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1273 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001274 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001275 case TRIM_MEMORY:
1276 handleTrimMemory(msg.arg1);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001277 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07001279 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 }
Bob Leee5408332009-09-04 18:31:17 -07001281
Brian Carlstromed7e0072011-03-24 13:27:57 -07001282 private void maybeSnapshot() {
1283 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001284 // convert the *private* ActivityThread.PackageInfo to *public* known
1285 // android.content.pm.PackageInfo
1286 String packageName = mBoundApplication.info.mPackageName;
1287 android.content.pm.PackageInfo packageInfo = null;
1288 try {
1289 Context context = getSystemContext();
1290 if(context == null) {
1291 Log.e(TAG, "cannot get a valid context");
1292 return;
1293 }
1294 PackageManager pm = context.getPackageManager();
1295 if(pm == null) {
1296 Log.e(TAG, "cannot get a valid PackageManager");
1297 return;
1298 }
1299 packageInfo = pm.getPackageInfo(
1300 packageName, PackageManager.GET_ACTIVITIES);
1301 } catch (NameNotFoundException e) {
1302 Log.e(TAG, "cannot get package info for " + packageName, e);
1303 }
1304 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001305 }
1306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 }
1308
Romain Guy65b345f2011-07-27 18:51:50 -07001309 private class Idler implements MessageQueue.IdleHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001311 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001312 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001313 if (mBoundApplication != null && mProfiler.profileFd != null
1314 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001315 stopProfiling = true;
1316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 if (a != null) {
1318 mNewActivities = null;
1319 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001320 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001322 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 TAG, "Reporting idle of " + a +
1324 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001325 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 if (a.activity != null && !a.activity.mFinished) {
1327 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001328 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001329 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001331 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 }
1333 }
1334 prev = a;
1335 a = a.nextIdle;
1336 prev.nextIdle = null;
1337 } while (a != null);
1338 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001339 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001340 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001341 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001342 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 return false;
1344 }
1345 }
1346
1347 final class GcIdler implements MessageQueue.IdleHandler {
1348 public final boolean queueIdle() {
1349 doGcIfNeeded();
1350 return false;
1351 }
1352 }
1353
Romain Guy65b345f2011-07-27 18:51:50 -07001354 private static class ResourcesKey {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001355 final private String mResDir;
1356 final private float mScale;
1357 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001358
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001359 ResourcesKey(String resDir, float scale) {
1360 mResDir = resDir;
1361 mScale = scale;
1362 mHash = mResDir.hashCode() << 2 + (int) (mScale * 2);
1363 }
Bob Leee5408332009-09-04 18:31:17 -07001364
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001365 @Override
1366 public int hashCode() {
1367 return mHash;
1368 }
1369
1370 @Override
1371 public boolean equals(Object obj) {
1372 if (!(obj instanceof ResourcesKey)) {
1373 return false;
1374 }
1375 ResourcesKey peer = (ResourcesKey) obj;
1376 return mResDir.equals(peer.mResDir) && mScale == peer.mScale;
1377 }
1378 }
1379
Romain Guy65b345f2011-07-27 18:51:50 -07001380 public static ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001381 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383
Romain Guy65b345f2011-07-27 18:51:50 -07001384 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001385 ActivityThread am = currentActivityThread();
1386 return (am != null && am.mBoundApplication != null)
1387 ? am.mBoundApplication.processName : null;
1388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389
Romain Guy65b345f2011-07-27 18:51:50 -07001390 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001391 ActivityThread am = currentActivityThread();
1392 return am != null ? am.mInitialApplication : null;
1393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001395 public static IPackageManager getPackageManager() {
1396 if (sPackageManager != null) {
1397 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1398 return sPackageManager;
1399 }
1400 IBinder b = ServiceManager.getService("package");
1401 //Slog.v("PackageManager", "default service binder = " + b);
1402 sPackageManager = IPackageManager.Stub.asInterface(b);
1403 //Slog.v("PackageManager", "default service = " + sPackageManager);
1404 return sPackageManager;
1405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001407 DisplayMetrics getDisplayMetricsLocked(CompatibilityInfo ci, boolean forceUpdate) {
1408 DisplayMetrics dm = mDisplayMetrics.get(ci);
1409 if (dm != null && !forceUpdate) {
1410 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001411 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001412 if (dm == null) {
1413 dm = new DisplayMetrics();
1414 mDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001415 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001416 Display d = WindowManagerImpl.getDefault(ci).getDefaultDisplay();
1417 d.getMetrics(dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001418 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1419 // + metrics.heightPixels + " den=" + metrics.density
1420 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001421 return dm;
1422 }
1423
1424 static Configuration applyConfigCompat(Configuration config, CompatibilityInfo compat) {
1425 if (config == null) {
1426 return null;
1427 }
1428 if (compat != null && !compat.supportsScreen()) {
1429 config = new Configuration(config);
1430 compat.applyToConfiguration(config);
1431 }
1432 return config;
1433 }
1434
Romain Guy65b345f2011-07-27 18:51:50 -07001435 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001436 Configuration applyConfigCompatMainThread(Configuration config, CompatibilityInfo compat) {
1437 if (config == null) {
1438 return null;
1439 }
1440 if (compat != null && !compat.supportsScreen()) {
1441 mMainThreadConfig.setTo(config);
1442 config = mMainThreadConfig;
1443 compat.applyToConfiguration(config);
1444 }
1445 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001448 /**
1449 * Creates the top level Resources for applications with the given compatibility info.
1450 *
1451 * @param resDir the resource directory.
1452 * @param compInfo the compability info. It will use the default compatibility info when it's
1453 * null.
1454 */
1455 Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) {
1456 ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale);
1457 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001459 // Resources is app scale dependent.
1460 if (false) {
1461 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1462 + compInfo.applicationScale);
1463 }
1464 WeakReference<Resources> wr = mActiveResources.get(key);
1465 r = wr != null ? wr.get() : null;
1466 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1467 if (r != null && r.getAssets().isUpToDate()) {
1468 if (false) {
1469 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1470 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1471 }
1472 return r;
1473 }
1474 }
1475
1476 //if (r != null) {
1477 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1478 // + r + " " + resDir);
1479 //}
1480
1481 AssetManager assets = new AssetManager();
1482 if (assets.addAssetPath(resDir) == 0) {
1483 return null;
1484 }
1485
1486 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Dianne Hackborn836e2622011-10-04 18:32:39 -07001487 DisplayMetrics metrics = getDisplayMetricsLocked(null, false);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001488 r = new Resources(assets, metrics, getConfiguration(), compInfo);
1489 if (false) {
1490 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1491 + r.getConfiguration() + " appScale="
1492 + r.getCompatibilityInfo().applicationScale);
1493 }
1494
1495 synchronized (mPackages) {
1496 WeakReference<Resources> wr = mActiveResources.get(key);
1497 Resources existing = wr != null ? wr.get() : null;
1498 if (existing != null && existing.getAssets().isUpToDate()) {
1499 // Someone else already created the resources while we were
1500 // unlocked; go ahead and use theirs.
1501 r.getAssets().close();
1502 return existing;
1503 }
1504
1505 // XXX need to remove entries when weak references go away
1506 mActiveResources.put(key, new WeakReference<Resources>(r));
1507 return r;
1508 }
1509 }
1510
1511 /**
1512 * Creates the top level resources for the given package.
1513 */
1514 Resources getTopLevelResources(String resDir, LoadedApk pkgInfo) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001515 return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001516 }
1517
1518 final Handler getHandler() {
1519 return mH;
1520 }
1521
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001522 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1523 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001524 synchronized (mPackages) {
1525 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1527 ref = mPackages.get(packageName);
1528 } else {
1529 ref = mResourcePackages.get(packageName);
1530 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001531 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001532 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001533 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1534 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 if (packageInfo != null && (packageInfo.mResources == null
1536 || packageInfo.mResources.getAssets().isUpToDate())) {
1537 if (packageInfo.isSecurityViolation()
1538 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1539 throw new SecurityException(
1540 "Requesting code from " + packageName
1541 + " to be run in process "
1542 + mBoundApplication.processName
1543 + "/" + mBoundApplication.appInfo.uid);
1544 }
1545 return packageInfo;
1546 }
1547 }
1548
1549 ApplicationInfo ai = null;
1550 try {
1551 ai = getPackageManager().getApplicationInfo(packageName,
1552 PackageManager.GET_SHARED_LIBRARY_FILES);
1553 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001554 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 }
1556
1557 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001558 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 }
1560
1561 return null;
1562 }
1563
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001564 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1565 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1567 boolean securityViolation = includeCode && ai.uid != 0
1568 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
1569 ? ai.uid != mBoundApplication.appInfo.uid : true);
1570 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1571 |Context.CONTEXT_IGNORE_SECURITY))
1572 == Context.CONTEXT_INCLUDE_CODE) {
1573 if (securityViolation) {
1574 String msg = "Requesting code from " + ai.packageName
1575 + " (with uid " + ai.uid + ")";
1576 if (mBoundApplication != null) {
1577 msg = msg + " to be run in process "
1578 + mBoundApplication.processName + " (with uid "
1579 + mBoundApplication.appInfo.uid + ")";
1580 }
1581 throw new SecurityException(msg);
1582 }
1583 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001584 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 }
1586
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001587 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1588 CompatibilityInfo compatInfo) {
1589 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 }
1591
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001592 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1593 synchronized (mPackages) {
1594 WeakReference<LoadedApk> ref;
1595 if (includeCode) {
1596 ref = mPackages.get(packageName);
1597 } else {
1598 ref = mResourcePackages.get(packageName);
1599 }
1600 return ref != null ? ref.get() : null;
1601 }
1602 }
1603
Romain Guy65b345f2011-07-27 18:51:50 -07001604 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1606 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001607 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 if (includeCode) {
1609 ref = mPackages.get(aInfo.packageName);
1610 } else {
1611 ref = mResourcePackages.get(aInfo.packageName);
1612 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001613 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 if (packageInfo == null || (packageInfo.mResources != null
1615 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001616 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 : "Loading resource-only package ") + aInfo.packageName
1618 + " (in " + (mBoundApplication != null
1619 ? mBoundApplication.processName : null)
1620 + ")");
1621 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001622 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 securityViolation, includeCode &&
1624 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1625 if (includeCode) {
1626 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001627 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 } else {
1629 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001630 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 }
1632 }
1633 return packageInfo;
1634 }
1635 }
1636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 ActivityThread() {
1638 }
1639
1640 public ApplicationThread getApplicationThread()
1641 {
1642 return mAppThread;
1643 }
1644
1645 public Instrumentation getInstrumentation()
1646 {
1647 return mInstrumentation;
1648 }
1649
1650 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001651 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 }
1653
1654 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001655 return mProfiler != null && mProfiler.profileFile != null
1656 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 }
1658
1659 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001660 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 }
1662
1663 public Looper getLooper() {
1664 return mLooper;
1665 }
1666
1667 public Application getApplication() {
1668 return mInitialApplication;
1669 }
Bob Leee5408332009-09-04 18:31:17 -07001670
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001671 public String getProcessName() {
1672 return mBoundApplication.processName;
1673 }
Bob Leee5408332009-09-04 18:31:17 -07001674
Dianne Hackborn21556372010-02-04 16:34:40 -08001675 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 synchronized (this) {
1677 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001678 ContextImpl context =
1679 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001680 LoadedApk info = new LoadedApk(this, "android", context, null,
1681 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 context.init(info, null, this);
1683 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001684 getConfiguration(), getDisplayMetricsLocked(
1685 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001687 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 // + ": " + context.getResources().getConfiguration());
1689 }
1690 }
1691 return mSystemContext;
1692 }
1693
Mike Cleron432b7132009-09-24 15:28:29 -07001694 public void installSystemApplicationInfo(ApplicationInfo info) {
1695 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001696 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001697 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001698 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001699
1700 // give ourselves a default profiler
1701 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001702 }
1703 }
1704
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001705 void ensureJitEnabled() {
1706 if (!mJitEnabled) {
1707 mJitEnabled = true;
1708 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1709 }
1710 }
1711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 void scheduleGcIdler() {
1713 if (!mGcIdlerScheduled) {
1714 mGcIdlerScheduled = true;
1715 Looper.myQueue().addIdleHandler(mGcIdler);
1716 }
1717 mH.removeMessages(H.GC_WHEN_IDLE);
1718 }
1719
1720 void unscheduleGcIdler() {
1721 if (mGcIdlerScheduled) {
1722 mGcIdlerScheduled = false;
1723 Looper.myQueue().removeIdleHandler(mGcIdler);
1724 }
1725 mH.removeMessages(H.GC_WHEN_IDLE);
1726 }
1727
1728 void doGcIfNeeded() {
1729 mGcIdlerScheduled = false;
1730 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001731 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 // + "m now=" + now);
1733 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001734 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 BinderInternal.forceGc("bg");
1736 }
1737 }
1738
Jeff Hamilton52d32032011-01-08 15:31:26 -06001739 public void registerOnActivityPausedListener(Activity activity,
1740 OnActivityPausedListener listener) {
1741 synchronized (mOnPauseListeners) {
1742 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1743 if (list == null) {
1744 list = new ArrayList<OnActivityPausedListener>();
1745 mOnPauseListeners.put(activity, list);
1746 }
1747 list.add(listener);
1748 }
1749 }
1750
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001751 public void unregisterOnActivityPausedListener(Activity activity,
1752 OnActivityPausedListener listener) {
1753 synchronized (mOnPauseListeners) {
1754 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1755 if (list != null) {
1756 list.remove(listener);
1757 }
1758 }
1759 }
1760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 public final ActivityInfo resolveActivityInfo(Intent intent) {
1762 ActivityInfo aInfo = intent.resolveActivityInfo(
1763 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1764 if (aInfo == null) {
1765 // Throw an exception.
1766 Instrumentation.checkStartActivityResult(
1767 IActivityManager.START_CLASS_NOT_FOUND, intent);
1768 }
1769 return aInfo;
1770 }
Bob Leee5408332009-09-04 18:31:17 -07001771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001774 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001775 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001777 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 r.intent = intent;
1779 r.state = state;
1780 r.parent = parent;
1781 r.embeddedID = id;
1782 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001783 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 if (localLOGV) {
1785 ComponentName compname = intent.getComponent();
1786 String name;
1787 if (compname != null) {
1788 name = compname.toShortString();
1789 } else {
1790 name = "(Intent " + intent + ").getComponent() returned null";
1791 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001792 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 + ", comp=" + name
1794 + ", token=" + token);
1795 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001796 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 }
1798
1799 public final Activity getActivity(IBinder token) {
1800 return mActivities.get(token).activity;
1801 }
1802
1803 public final void sendActivityResult(
1804 IBinder token, String id, int requestCode,
1805 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001806 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001807 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1809 list.add(new ResultInfo(id, requestCode, resultCode, data));
1810 mAppThread.scheduleSendResult(token, list);
1811 }
1812
1813 // if the thread hasn't started yet, we don't have the handler, so just
1814 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07001815 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 queueOrSendMessage(what, obj, 0, 0);
1817 }
1818
Romain Guy65b345f2011-07-27 18:51:50 -07001819 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 queueOrSendMessage(what, obj, arg1, 0);
1821 }
1822
Romain Guy65b345f2011-07-27 18:51:50 -07001823 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001825 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1827 + ": " + arg1 + " / " + obj);
1828 Message msg = Message.obtain();
1829 msg.what = what;
1830 msg.obj = obj;
1831 msg.arg1 = arg1;
1832 msg.arg2 = arg2;
1833 mH.sendMessage(msg);
1834 }
1835 }
1836
Dianne Hackborn21556372010-02-04 16:34:40 -08001837 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 String what) {
1839 ContextCleanupInfo cci = new ContextCleanupInfo();
1840 cci.context = context;
1841 cci.who = who;
1842 cci.what = what;
1843 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1844 }
1845
Romain Guy65b345f2011-07-27 18:51:50 -07001846 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1848
1849 ActivityInfo aInfo = r.activityInfo;
1850 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001851 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 Context.CONTEXT_INCLUDE_CODE);
1853 }
Bob Leee5408332009-09-04 18:31:17 -07001854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 ComponentName component = r.intent.getComponent();
1856 if (component == null) {
1857 component = r.intent.resolveActivity(
1858 mInitialApplication.getPackageManager());
1859 r.intent.setComponent(component);
1860 }
1861
1862 if (r.activityInfo.targetActivity != null) {
1863 component = new ComponentName(r.activityInfo.packageName,
1864 r.activityInfo.targetActivity);
1865 }
1866
1867 Activity activity = null;
1868 try {
1869 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
1870 activity = mInstrumentation.newActivity(
1871 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08001872 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 r.intent.setExtrasClassLoader(cl);
1874 if (r.state != null) {
1875 r.state.setClassLoader(cl);
1876 }
1877 } catch (Exception e) {
1878 if (!mInstrumentation.onException(activity, e)) {
1879 throw new RuntimeException(
1880 "Unable to instantiate activity " + component
1881 + ": " + e.toString(), e);
1882 }
1883 }
1884
1885 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001886 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001887
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001888 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
1889 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 TAG, r + ": app=" + app
1891 + ", appName=" + app.getPackageName()
1892 + ", pkg=" + r.packageInfo.getPackageName()
1893 + ", comp=" + r.intent.getComponent().toShortString()
1894 + ", dir=" + r.packageInfo.getAppDir());
1895
1896 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001897 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 appContext.init(r.packageInfo, r.token, this);
1899 appContext.setOuterContext(activity);
1900 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001901 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001902 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07001903 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001904 activity.attach(appContext, this, getInstrumentation(), r.token,
1905 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001906 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07001907
Christopher Tateb70f3df2009-04-07 16:07:59 -07001908 if (customIntent != null) {
1909 activity.mIntent = customIntent;
1910 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001911 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 activity.mStartedActivity = false;
1913 int theme = r.activityInfo.getThemeResource();
1914 if (theme != 0) {
1915 activity.setTheme(theme);
1916 }
1917
1918 activity.mCalled = false;
1919 mInstrumentation.callActivityOnCreate(activity, r.state);
1920 if (!activity.mCalled) {
1921 throw new SuperNotCalledException(
1922 "Activity " + r.intent.getComponent().toShortString() +
1923 " did not call through to super.onCreate()");
1924 }
1925 r.activity = activity;
1926 r.stopped = true;
1927 if (!r.activity.mFinished) {
1928 activity.performStart();
1929 r.stopped = false;
1930 }
1931 if (!r.activity.mFinished) {
1932 if (r.state != null) {
1933 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
1934 }
1935 }
1936 if (!r.activity.mFinished) {
1937 activity.mCalled = false;
1938 mInstrumentation.callActivityOnPostCreate(activity, r.state);
1939 if (!activity.mCalled) {
1940 throw new SuperNotCalledException(
1941 "Activity " + r.intent.getComponent().toShortString() +
1942 " did not call through to super.onPostCreate()");
1943 }
1944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 }
1946 r.paused = true;
1947
1948 mActivities.put(r.token, r);
1949
1950 } catch (SuperNotCalledException e) {
1951 throw e;
1952
1953 } catch (Exception e) {
1954 if (!mInstrumentation.onException(activity, e)) {
1955 throw new RuntimeException(
1956 "Unable to start activity " + component
1957 + ": " + e.toString(), e);
1958 }
1959 }
1960
1961 return activity;
1962 }
1963
Romain Guy65b345f2011-07-27 18:51:50 -07001964 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 // If we are getting ready to gc after going to the background, well
1966 // we are back active so skip it.
1967 unscheduleGcIdler();
1968
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001969 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001970 mProfiler.setProfiler(r.profileFile, r.profileFd);
1971 mProfiler.startProfiling();
1972 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001973 }
1974
Dianne Hackborn58f42a52011-10-10 13:46:34 -07001975 // Make sure we are running with the most recent config.
1976 handleConfigurationChanged(null, null);
1977
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001978 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001980 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981
1982 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08001983 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001984 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 handleResumeActivity(r.token, false, r.isForward);
1986
1987 if (!r.activity.mFinished && r.startsNotResumed) {
1988 // The activity manager actually wants this one to start out
1989 // paused, because it needs to be visible but isn't in the
1990 // foreground. We accomplish this by going through the
1991 // normal startup (because activities expect to go through
1992 // onResume() the first time they run, before their window
1993 // is displayed), and then pausing it. However, in this case
1994 // we do -not- need to do the full pause cycle (of freezing
1995 // and such) because the activity manager assumes it can just
1996 // retain the current state it has.
1997 try {
1998 r.activity.mCalled = false;
1999 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002000 // We need to keep around the original state, in case
2001 // we need to be created again.
2002 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 if (!r.activity.mCalled) {
2004 throw new SuperNotCalledException(
2005 "Activity " + r.intent.getComponent().toShortString() +
2006 " did not call through to super.onPause()");
2007 }
2008
2009 } catch (SuperNotCalledException e) {
2010 throw e;
2011
2012 } catch (Exception e) {
2013 if (!mInstrumentation.onException(r.activity, e)) {
2014 throw new RuntimeException(
2015 "Unable to pause activity "
2016 + r.intent.getComponent().toShortString()
2017 + ": " + e.toString(), e);
2018 }
2019 }
2020 r.paused = true;
2021 }
2022 } else {
2023 // If there was an error, for any reason, tell the activity
2024 // manager to stop us.
2025 try {
2026 ActivityManagerNative.getDefault()
2027 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2028 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002029 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 }
2031 }
2032 }
2033
Romain Guy65b345f2011-07-27 18:51:50 -07002034 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 List<Intent> intents) {
2036 final int N = intents.size();
2037 for (int i=0; i<N; i++) {
2038 Intent intent = intents.get(i);
2039 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002040 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2042 }
2043 }
2044
2045 public final void performNewIntents(IBinder token,
2046 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002047 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 if (r != null) {
2049 final boolean resumed = !r.paused;
2050 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002051 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 mInstrumentation.callActivityOnPause(r.activity);
2053 }
2054 deliverNewIntents(r, intents);
2055 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002056 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002057 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 }
2059 }
2060 }
Bob Leee5408332009-09-04 18:31:17 -07002061
Romain Guy65b345f2011-07-27 18:51:50 -07002062 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 performNewIntents(data.token, data.intents);
2064 }
2065
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002066 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2067
2068 /**
2069 * Return the Intent that's currently being handled by a
2070 * BroadcastReceiver on this thread, or null if none.
2071 * @hide
2072 */
2073 public static Intent getIntentBeingBroadcast() {
2074 return sCurrentBroadcastIntent.get();
2075 }
2076
Romain Guy65b345f2011-07-27 18:51:50 -07002077 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 // If we are getting ready to gc after going to the background, well
2079 // we are back active so skip it.
2080 unscheduleGcIdler();
2081
2082 String component = data.intent.getComponent().getClassName();
2083
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002084 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002085 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086
2087 IActivityManager mgr = ActivityManagerNative.getDefault();
2088
Romain Guy65b345f2011-07-27 18:51:50 -07002089 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 try {
2091 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2092 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002093 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2095 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002096 if (DEBUG_BROADCAST) Slog.i(TAG,
2097 "Finishing failed broadcast to " + data.intent.getComponent());
2098 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 throw new RuntimeException(
2100 "Unable to instantiate receiver " + component
2101 + ": " + e.toString(), e);
2102 }
2103
2104 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002105 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002106
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002107 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 TAG, "Performing receive of " + data.intent
2109 + ": app=" + app
2110 + ", appName=" + app.getPackageName()
2111 + ", pkg=" + packageInfo.getPackageName()
2112 + ", comp=" + data.intent.getComponent().toShortString()
2113 + ", dir=" + packageInfo.getAppDir());
2114
Dianne Hackborn21556372010-02-04 16:34:40 -08002115 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002116 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002117 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 receiver.onReceive(context.getReceiverRestrictedContext(),
2119 data.intent);
2120 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002121 if (DEBUG_BROADCAST) Slog.i(TAG,
2122 "Finishing failed broadcast to " + data.intent.getComponent());
2123 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 if (!mInstrumentation.onException(receiver, e)) {
2125 throw new RuntimeException(
2126 "Unable to start receiver " + component
2127 + ": " + e.toString(), e);
2128 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002129 } finally {
2130 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 }
2132
Dianne Hackborne829fef2010-10-26 17:44:01 -07002133 if (receiver.getPendingResult() != null) {
2134 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 }
2136 }
2137
Christopher Tate181fafa2009-05-14 11:12:14 -07002138 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002139 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002140 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002141
2142 // no longer idle; we have backup work to do
2143 unscheduleGcIdler();
2144
2145 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002146 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002147 String packageName = packageInfo.mPackageName;
2148 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002149 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002150 + " already exists");
2151 return;
2152 }
Bob Leee5408332009-09-04 18:31:17 -07002153
Christopher Tate181fafa2009-05-14 11:12:14 -07002154 BackupAgent agent = null;
2155 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002156
Christopher Tate79ec80d2011-06-24 14:58:49 -07002157 // full backup operation but no app-supplied agent? use the default implementation
2158 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2159 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002160 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002161 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002162
Christopher Tate181fafa2009-05-14 11:12:14 -07002163 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002164 IBinder binder = null;
2165 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002166 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2167
Christopher Tated1475e02009-07-09 15:36:17 -07002168 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002169 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002170
2171 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002172 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002173 context.init(packageInfo, null, this);
2174 context.setOuterContext(agent);
2175 agent.attach(context);
2176
2177 agent.onCreate();
2178 binder = agent.onBind();
2179 mBackupAgents.put(packageName, agent);
2180 } catch (Exception e) {
2181 // If this is during restore, fail silently; otherwise go
2182 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002183 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002184 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2185 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002186 throw e;
2187 }
2188 // falling through with 'binder' still null
2189 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002190
2191 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002192 try {
2193 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2194 } catch (RemoteException e) {
2195 // nothing to do.
2196 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002197 } catch (Exception e) {
2198 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002199 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002200 }
2201 }
2202
2203 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002204 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002205 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002206
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002207 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002208 String packageName = packageInfo.mPackageName;
2209 BackupAgent agent = mBackupAgents.get(packageName);
2210 if (agent != null) {
2211 try {
2212 agent.onDestroy();
2213 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002214 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002215 e.printStackTrace();
2216 }
2217 mBackupAgents.remove(packageName);
2218 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002219 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002220 }
2221 }
2222
Romain Guy65b345f2011-07-27 18:51:50 -07002223 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 // If we are getting ready to gc after going to the background, well
2225 // we are back active so skip it.
2226 unscheduleGcIdler();
2227
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002228 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002229 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 Service service = null;
2231 try {
2232 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2233 service = (Service) cl.loadClass(data.info.name).newInstance();
2234 } catch (Exception e) {
2235 if (!mInstrumentation.onException(service, e)) {
2236 throw new RuntimeException(
2237 "Unable to instantiate service " + data.info.name
2238 + ": " + e.toString(), e);
2239 }
2240 }
2241
2242 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002243 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244
Dianne Hackborn21556372010-02-04 16:34:40 -08002245 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 context.init(packageInfo, null, this);
2247
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002248 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 context.setOuterContext(service);
2250 service.attach(context, this, data.info.name, data.token, app,
2251 ActivityManagerNative.getDefault());
2252 service.onCreate();
2253 mServices.put(data.token, service);
2254 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002255 ActivityManagerNative.getDefault().serviceDoneExecuting(
2256 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 } catch (RemoteException e) {
2258 // nothing to do.
2259 }
2260 } catch (Exception e) {
2261 if (!mInstrumentation.onException(service, e)) {
2262 throw new RuntimeException(
2263 "Unable to create service " + data.info.name
2264 + ": " + e.toString(), e);
2265 }
2266 }
2267 }
2268
Romain Guy65b345f2011-07-27 18:51:50 -07002269 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 Service s = mServices.get(data.token);
2271 if (s != null) {
2272 try {
2273 data.intent.setExtrasClassLoader(s.getClassLoader());
2274 try {
2275 if (!data.rebind) {
2276 IBinder binder = s.onBind(data.intent);
2277 ActivityManagerNative.getDefault().publishService(
2278 data.token, data.intent, binder);
2279 } else {
2280 s.onRebind(data.intent);
2281 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002282 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002284 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 } catch (RemoteException ex) {
2286 }
2287 } catch (Exception e) {
2288 if (!mInstrumentation.onException(s, e)) {
2289 throw new RuntimeException(
2290 "Unable to bind to service " + s
2291 + " with " + data.intent + ": " + e.toString(), e);
2292 }
2293 }
2294 }
2295 }
2296
Romain Guy65b345f2011-07-27 18:51:50 -07002297 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 Service s = mServices.get(data.token);
2299 if (s != null) {
2300 try {
2301 data.intent.setExtrasClassLoader(s.getClassLoader());
2302 boolean doRebind = s.onUnbind(data.intent);
2303 try {
2304 if (doRebind) {
2305 ActivityManagerNative.getDefault().unbindFinished(
2306 data.token, data.intent, doRebind);
2307 } else {
2308 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002309 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 }
2311 } catch (RemoteException ex) {
2312 }
2313 } catch (Exception e) {
2314 if (!mInstrumentation.onException(s, e)) {
2315 throw new RuntimeException(
2316 "Unable to unbind to service " + s
2317 + " with " + data.intent + ": " + e.toString(), e);
2318 }
2319 }
2320 }
2321 }
2322
Dianne Hackborn625ac272010-09-17 18:29:22 -07002323 private void handleDumpService(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002324 Service s = mServices.get(info.token);
2325 if (s != null) {
2326 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2327 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2328 pw.flush();
2329 try {
2330 info.fd.close();
2331 } catch (IOException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 }
2333 }
2334 }
2335
Dianne Hackborn625ac272010-09-17 18:29:22 -07002336 private void handleDumpActivity(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002337 ActivityClientRecord r = mActivities.get(info.token);
2338 if (r != null && r.activity != null) {
2339 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2340 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2341 pw.flush();
2342 try {
2343 info.fd.close();
2344 } catch (IOException e) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07002345 }
2346 }
2347 }
2348
Romain Guy65b345f2011-07-27 18:51:50 -07002349 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 Service s = mServices.get(data.token);
2351 if (s != null) {
2352 try {
2353 if (data.args != null) {
2354 data.args.setExtrasClassLoader(s.getClassLoader());
2355 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002356 int res;
2357 if (!data.taskRemoved) {
2358 res = s.onStartCommand(data.args, data.flags, data.startId);
2359 } else {
2360 s.onTaskRemoved(data.args);
2361 res = Service.START_TASK_REMOVED_COMPLETE;
2362 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002363
2364 QueuedWork.waitToFinish();
2365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002367 ActivityManagerNative.getDefault().serviceDoneExecuting(
2368 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 } catch (RemoteException e) {
2370 // nothing to do.
2371 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002372 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 } catch (Exception e) {
2374 if (!mInstrumentation.onException(s, e)) {
2375 throw new RuntimeException(
2376 "Unable to start service " + s
2377 + " with " + data.args + ": " + e.toString(), e);
2378 }
2379 }
2380 }
2381 }
2382
Romain Guy65b345f2011-07-27 18:51:50 -07002383 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 Service s = mServices.remove(token);
2385 if (s != null) {
2386 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002387 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 s.onDestroy();
2389 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002390 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002392 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002394
2395 QueuedWork.waitToFinish();
2396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002398 ActivityManagerNative.getDefault().serviceDoneExecuting(
2399 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 } catch (RemoteException e) {
2401 // nothing to do.
2402 }
2403 } catch (Exception e) {
2404 if (!mInstrumentation.onException(s, e)) {
2405 throw new RuntimeException(
2406 "Unable to stop service " + s
2407 + ": " + e.toString(), e);
2408 }
2409 }
2410 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002411 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 }
2413
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002414 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002416 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002417 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 + " finished=" + r.activity.mFinished);
2419 if (r != null && !r.activity.mFinished) {
2420 if (clearHide) {
2421 r.hideForNow = false;
2422 r.activity.mStartedActivity = false;
2423 }
2424 try {
2425 if (r.pendingIntents != null) {
2426 deliverNewIntents(r, r.pendingIntents);
2427 r.pendingIntents = null;
2428 }
2429 if (r.pendingResults != null) {
2430 deliverResults(r, r.pendingResults);
2431 r.pendingResults = null;
2432 }
2433 r.activity.performResume();
2434
Bob Leee5408332009-09-04 18:31:17 -07002435 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 r.paused = false;
2439 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 r.state = null;
2441 } catch (Exception e) {
2442 if (!mInstrumentation.onException(r.activity, e)) {
2443 throw new RuntimeException(
2444 "Unable to resume activity "
2445 + r.intent.getComponent().toShortString()
2446 + ": " + e.toString(), e);
2447 }
2448 }
2449 }
2450 return r;
2451 }
2452
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002453 final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
2454 if (r.mPendingRemoveWindow != null) {
2455 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2456 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2457 if (wtoken != null) {
2458 WindowManagerImpl.getDefault().closeAll(wtoken,
2459 r.activity.getClass().getName(), "Activity");
2460 }
2461 }
2462 r.mPendingRemoveWindow = null;
2463 r.mPendingRemoveWindowManager = null;
2464 }
2465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2467 // If we are getting ready to gc after going to the background, well
2468 // we are back active so skip it.
2469 unscheduleGcIdler();
2470
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002471 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472
2473 if (r != null) {
2474 final Activity a = r.activity;
2475
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002476 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 TAG, "Resume " + r + " started activity: " +
2478 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2479 + ", finished: " + a.mFinished);
2480
2481 final int forwardBit = isForward ?
2482 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 // If the window hasn't yet been added to the window manager,
2485 // and this guy didn't finish itself or start another activity,
2486 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002487 boolean willBeVisible = !a.mStartedActivity;
2488 if (!willBeVisible) {
2489 try {
2490 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2491 a.getActivityToken());
2492 } catch (RemoteException e) {
2493 }
2494 }
2495 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 r.window = r.activity.getWindow();
2497 View decor = r.window.getDecorView();
2498 decor.setVisibility(View.INVISIBLE);
2499 ViewManager wm = a.getWindowManager();
2500 WindowManager.LayoutParams l = r.window.getAttributes();
2501 a.mDecor = decor;
2502 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2503 l.softInputMode |= forwardBit;
2504 if (a.mVisibleFromClient) {
2505 a.mWindowAdded = true;
2506 wm.addView(decor, l);
2507 }
2508
2509 // If the window has already been added, but during resume
2510 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002511 // window visible.
2512 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002513 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 TAG, "Launch " + r + " mStartedActivity set");
2515 r.hideForNow = true;
2516 }
2517
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002518 // Get rid of anything left hanging around.
2519 cleanUpPendingRemoveWindows(r);
2520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 // The window is now visible if it has been added, we are not
2522 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002523 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002524 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002526 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002527 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 performConfigurationChanged(r.activity, r.newConfig);
2529 r.newConfig = null;
2530 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002531 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 + isForward);
2533 WindowManager.LayoutParams l = r.window.getAttributes();
2534 if ((l.softInputMode
2535 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2536 != forwardBit) {
2537 l.softInputMode = (l.softInputMode
2538 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2539 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002540 if (r.activity.mVisibleFromClient) {
2541 ViewManager wm = a.getWindowManager();
2542 View decor = r.window.getDecorView();
2543 wm.updateViewLayout(decor, l);
2544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 }
2546 r.activity.mVisibleFromServer = true;
2547 mNumVisibleActivities++;
2548 if (r.activity.mVisibleFromClient) {
2549 r.activity.makeVisible();
2550 }
2551 }
2552
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002553 if (!r.onlyLocalRequest) {
2554 r.nextIdle = mNewActivities;
2555 mNewActivities = r;
2556 if (localLOGV) Slog.v(
2557 TAG, "Scheduling idle handler for " + r);
2558 Looper.myQueue().addIdleHandler(new Idler());
2559 }
2560 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561
2562 } else {
2563 // If an exception was thrown when trying to resume, then
2564 // just end this activity.
2565 try {
2566 ActivityManagerNative.getDefault()
2567 .finishActivity(token, Activity.RESULT_CANCELED, null);
2568 } catch (RemoteException ex) {
2569 }
2570 }
2571 }
2572
2573 private int mThumbnailWidth = -1;
2574 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002575 private Bitmap mAvailThumbnailBitmap = null;
2576 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577
Romain Guy65b345f2011-07-27 18:51:50 -07002578 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002579 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002581 if (thumbnail == null) {
2582 int w = mThumbnailWidth;
2583 int h;
2584 if (w < 0) {
2585 Resources res = r.activity.getResources();
2586 mThumbnailHeight = h =
2587 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002589 mThumbnailWidth = w =
2590 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2591 } else {
2592 h = mThumbnailHeight;
2593 }
2594
2595 // On platforms where we don't want thumbnails, set dims to (0,0)
2596 if ((w > 0) && (h > 0)) {
2597 thumbnail = Bitmap.createBitmap(w, h, THUMBNAIL_FORMAT);
2598 thumbnail.eraseColor(0);
2599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 }
2601
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002602 if (thumbnail != null) {
2603 Canvas cv = mThumbnailCanvas;
2604 if (cv == null) {
2605 mThumbnailCanvas = cv = new Canvas();
2606 }
2607
2608 cv.setBitmap(thumbnail);
2609 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2610 mAvailThumbnailBitmap = thumbnail;
2611 thumbnail = null;
2612 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002613 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 } catch (Exception e) {
2617 if (!mInstrumentation.onException(r.activity, e)) {
2618 throw new RuntimeException(
2619 "Unable to create thumbnail of "
2620 + r.intent.getComponent().toShortString()
2621 + ": " + e.toString(), e);
2622 }
2623 thumbnail = null;
2624 }
2625
2626 return thumbnail;
2627 }
2628
Romain Guy65b345f2011-07-27 18:51:50 -07002629 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002631 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002633 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 if (userLeaving) {
2635 performUserLeavingActivity(r);
2636 }
Bob Leee5408332009-09-04 18:31:17 -07002637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002639 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002641 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002642 if (r.isPreHoneycomb()) {
2643 QueuedWork.waitToFinish();
2644 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 // Tell the activity manager we have paused.
2647 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002648 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 } catch (RemoteException ex) {
2650 }
2651 }
2652 }
2653
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002654 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 mInstrumentation.callActivityOnUserLeaving(r.activity);
2656 }
2657
2658 final Bundle performPauseActivity(IBinder token, boolean finished,
2659 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002660 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 return r != null ? performPauseActivity(r, finished, saveState) : null;
2662 }
2663
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002664 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 boolean saveState) {
2666 if (r.paused) {
2667 if (r.activity.mFinished) {
2668 // If we are finishing, we won't call onResume() in certain cases.
2669 // So here we likewise don't want to call onPause() if the activity
2670 // isn't resumed.
2671 return null;
2672 }
2673 RuntimeException e = new RuntimeException(
2674 "Performing pause of activity that is not resumed: "
2675 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002676 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 }
2678 Bundle state = null;
2679 if (finished) {
2680 r.activity.mFinished = true;
2681 }
2682 try {
2683 // Next have the activity save its current state and managed dialogs...
2684 if (!r.activity.mFinished && saveState) {
2685 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002686 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2688 r.state = state;
2689 }
2690 // Now we are idle.
2691 r.activity.mCalled = false;
2692 mInstrumentation.callActivityOnPause(r.activity);
2693 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2694 if (!r.activity.mCalled) {
2695 throw new SuperNotCalledException(
2696 "Activity " + r.intent.getComponent().toShortString() +
2697 " did not call through to super.onPause()");
2698 }
2699
2700 } catch (SuperNotCalledException e) {
2701 throw e;
2702
2703 } catch (Exception e) {
2704 if (!mInstrumentation.onException(r.activity, e)) {
2705 throw new RuntimeException(
2706 "Unable to pause activity "
2707 + r.intent.getComponent().toShortString()
2708 + ": " + e.toString(), e);
2709 }
2710 }
2711 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002712
2713 // Notify any outstanding on paused listeners
2714 ArrayList<OnActivityPausedListener> listeners;
2715 synchronized (mOnPauseListeners) {
2716 listeners = mOnPauseListeners.remove(r.activity);
2717 }
2718 int size = (listeners != null ? listeners.size() : 0);
2719 for (int i = 0; i < size; i++) {
2720 listeners.get(i).onPaused(r.activity);
2721 }
2722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 return state;
2724 }
2725
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002726 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002727 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002728 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 }
2730
2731 private static class StopInfo {
2732 Bitmap thumbnail;
2733 CharSequence description;
2734 }
2735
Romain Guy65b345f2011-07-27 18:51:50 -07002736 private class ProviderRefCount {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 public int count;
2738 ProviderRefCount(int pCount) {
2739 count = pCount;
2740 }
2741 }
2742
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002743 /**
2744 * Core implementation of stopping an activity. Note this is a little
2745 * tricky because the server's meaning of stop is slightly different
2746 * than our client -- for the server, stop means to save state and give
2747 * it the result when it is done, but the window may still be visible.
2748 * For the client, we want to call onStop()/onStart() to indicate when
2749 * the activity's UI visibillity changes.
2750 */
Romain Guy65b345f2011-07-27 18:51:50 -07002751 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002752 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002753 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002754 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 if (r != null) {
2756 if (!keepShown && r.stopped) {
2757 if (r.activity.mFinished) {
2758 // If we are finishing, we won't call onResume() in certain
2759 // cases. So here we likewise don't want to call onStop()
2760 // if the activity isn't resumed.
2761 return;
2762 }
2763 RuntimeException e = new RuntimeException(
2764 "Performing stop of activity that is not resumed: "
2765 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002766 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 }
2768
2769 if (info != null) {
2770 try {
2771 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002772 // For now, don't create the thumbnail here; we are
2773 // doing that by doing a screen snapshot.
2774 info.thumbnail = null; //createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 info.description = r.activity.onCreateDescription();
2776 } catch (Exception e) {
2777 if (!mInstrumentation.onException(r.activity, e)) {
2778 throw new RuntimeException(
2779 "Unable to save state of activity "
2780 + r.intent.getComponent().toShortString()
2781 + ": " + e.toString(), e);
2782 }
2783 }
2784 }
2785
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002786 // Next have the activity save its current state and managed dialogs...
2787 if (!r.activity.mFinished && saveState) {
2788 if (r.state == null) {
2789 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002790 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002791 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2792 r.state = state;
2793 } else {
2794 state = r.state;
2795 }
2796 }
2797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 if (!keepShown) {
2799 try {
2800 // Now we are idle.
2801 r.activity.performStop();
2802 } catch (Exception e) {
2803 if (!mInstrumentation.onException(r.activity, e)) {
2804 throw new RuntimeException(
2805 "Unable to stop activity "
2806 + r.intent.getComponent().toShortString()
2807 + ": " + e.toString(), e);
2808 }
2809 }
2810 r.stopped = true;
2811 }
2812
2813 r.paused = true;
2814 }
2815 }
2816
Romain Guy65b345f2011-07-27 18:51:50 -07002817 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 View v = r.activity.mDecor;
2819 if (v != null) {
2820 if (show) {
2821 if (!r.activity.mVisibleFromServer) {
2822 r.activity.mVisibleFromServer = true;
2823 mNumVisibleActivities++;
2824 if (r.activity.mVisibleFromClient) {
2825 r.activity.makeVisible();
2826 }
2827 }
2828 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002829 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002830 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 performConfigurationChanged(r.activity, r.newConfig);
2832 r.newConfig = null;
2833 }
2834 } else {
2835 if (r.activity.mVisibleFromServer) {
2836 r.activity.mVisibleFromServer = false;
2837 mNumVisibleActivities--;
2838 v.setVisibility(View.INVISIBLE);
2839 }
2840 }
2841 }
2842 }
2843
Romain Guy65b345f2011-07-27 18:51:50 -07002844 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002845 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 r.activity.mConfigChangeFlags |= configChanges;
2847
2848 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002849 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002851 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 TAG, "Finishing stop of " + r + ": show=" + show
2853 + " win=" + r.window);
2854
2855 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07002856
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002857 // Make sure any pending writes are now committed.
2858 if (!r.isPreHoneycomb()) {
2859 QueuedWork.waitToFinish();
2860 }
2861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 // Tell activity manager we have been stopped.
2863 try {
2864 ActivityManagerNative.getDefault().activityStopped(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002865 r.token, r.state, info.thumbnail, info.description);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 } catch (RemoteException ex) {
2867 }
2868 }
2869
2870 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002871 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 if (r.stopped) {
2873 r.activity.performRestart();
2874 r.stopped = false;
2875 }
2876 }
2877
Romain Guy65b345f2011-07-27 18:51:50 -07002878 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002879 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08002880
2881 if (r == null) {
2882 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
2883 return;
2884 }
2885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002887 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 } else if (show && r.stopped) {
2889 // If we are getting ready to gc after going to the background, well
2890 // we are back active so skip it.
2891 unscheduleGcIdler();
2892
2893 r.activity.performRestart();
2894 r.stopped = false;
2895 }
2896 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07002897 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 TAG, "Handle window " + r + " visibility: " + show);
2899 updateVisibility(r, show);
2900 }
2901 }
2902
Romain Guy65b345f2011-07-27 18:51:50 -07002903 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002904 ActivityClientRecord r = mActivities.get(token);
2905
2906 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002907 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002908 return;
2909 }
2910
2911 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002912 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002913 try {
2914 // Now we are idle.
2915 r.activity.performStop();
2916 } catch (Exception e) {
2917 if (!mInstrumentation.onException(r.activity, e)) {
2918 throw new RuntimeException(
2919 "Unable to stop activity "
2920 + r.intent.getComponent().toShortString()
2921 + ": " + e.toString(), e);
2922 }
2923 }
2924 r.stopped = true;
2925 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002926
2927 // Make sure any pending writes are now committed.
2928 if (!r.isPreHoneycomb()) {
2929 QueuedWork.waitToFinish();
2930 }
2931
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002932 // Tell activity manager we slept.
2933 try {
2934 ActivityManagerNative.getDefault().activitySlept(r.token);
2935 } catch (RemoteException ex) {
2936 }
2937 } else {
2938 if (r.stopped && r.activity.mVisibleFromServer) {
2939 r.activity.performRestart();
2940 r.stopped = false;
2941 }
2942 }
2943 }
2944
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002945 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08002946 synchronized (mPackages) {
2947 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002948 }
2949 }
2950
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002951 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
2952 LoadedApk apk = peekPackageInfo(data.pkg, false);
2953 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002954 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002955 }
2956 apk = peekPackageInfo(data.pkg, true);
2957 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002958 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002959 }
2960 handleConfigurationChanged(mConfiguration, data.info);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002961 WindowManagerImpl.getDefault().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002962 }
2963
Romain Guy65b345f2011-07-27 18:51:50 -07002964 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 final int N = results.size();
2966 for (int i=0; i<N; i++) {
2967 ResultInfo ri = results.get(i);
2968 try {
2969 if (ri.mData != null) {
2970 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
2971 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002972 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07002973 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 r.activity.dispatchActivityResult(ri.mResultWho,
2975 ri.mRequestCode, ri.mResultCode, ri.mData);
2976 } catch (Exception e) {
2977 if (!mInstrumentation.onException(r.activity, e)) {
2978 throw new RuntimeException(
2979 "Failure delivering result " + ri + " to activity "
2980 + r.intent.getComponent().toShortString()
2981 + ": " + e.toString(), e);
2982 }
2983 }
2984 }
2985 }
2986
Romain Guy65b345f2011-07-27 18:51:50 -07002987 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002988 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002989 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 if (r != null) {
2991 final boolean resumed = !r.paused;
2992 if (!r.activity.mFinished && r.activity.mDecor != null
2993 && r.hideForNow && resumed) {
2994 // We had hidden the activity because it started another
2995 // one... we have gotten a result back and we are not
2996 // paused, so make sure our window is visible.
2997 updateVisibility(r, true);
2998 }
2999 if (resumed) {
3000 try {
3001 // Now we are idle.
3002 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003003 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 mInstrumentation.callActivityOnPause(r.activity);
3005 if (!r.activity.mCalled) {
3006 throw new SuperNotCalledException(
3007 "Activity " + r.intent.getComponent().toShortString()
3008 + " did not call through to super.onPause()");
3009 }
3010 } catch (SuperNotCalledException e) {
3011 throw e;
3012 } catch (Exception e) {
3013 if (!mInstrumentation.onException(r.activity, e)) {
3014 throw new RuntimeException(
3015 "Unable to pause activity "
3016 + r.intent.getComponent().toShortString()
3017 + ": " + e.toString(), e);
3018 }
3019 }
3020 }
3021 deliverResults(r, res.results);
3022 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003023 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003024 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 }
3026 }
3027 }
3028
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003029 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 return performDestroyActivity(token, finishing, 0, false);
3031 }
3032
Romain Guy65b345f2011-07-27 18:51:50 -07003033 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003035 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003036 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003037 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003039 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 r.activity.mConfigChangeFlags |= configChanges;
3041 if (finishing) {
3042 r.activity.mFinished = true;
3043 }
3044 if (!r.paused) {
3045 try {
3046 r.activity.mCalled = false;
3047 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07003048 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049 r.activity.getComponentName().getClassName());
3050 if (!r.activity.mCalled) {
3051 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003052 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003053 + " did not call through to super.onPause()");
3054 }
3055 } catch (SuperNotCalledException e) {
3056 throw e;
3057 } catch (Exception e) {
3058 if (!mInstrumentation.onException(r.activity, e)) {
3059 throw new RuntimeException(
3060 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003061 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 + ": " + e.toString(), e);
3063 }
3064 }
3065 r.paused = true;
3066 }
3067 if (!r.stopped) {
3068 try {
3069 r.activity.performStop();
3070 } catch (SuperNotCalledException e) {
3071 throw e;
3072 } catch (Exception e) {
3073 if (!mInstrumentation.onException(r.activity, e)) {
3074 throw new RuntimeException(
3075 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003076 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 + ": " + e.toString(), e);
3078 }
3079 }
3080 r.stopped = true;
3081 }
3082 if (getNonConfigInstance) {
3083 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003084 r.lastNonConfigurationInstances
3085 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 } catch (Exception e) {
3087 if (!mInstrumentation.onException(r.activity, e)) {
3088 throw new RuntimeException(
3089 "Unable to retain activity "
3090 + r.intent.getComponent().toShortString()
3091 + ": " + e.toString(), e);
3092 }
3093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 }
3095 try {
3096 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003097 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 if (!r.activity.mCalled) {
3099 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003100 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 " did not call through to super.onDestroy()");
3102 }
3103 if (r.window != null) {
3104 r.window.closeAllPanels();
3105 }
3106 } catch (SuperNotCalledException e) {
3107 throw e;
3108 } catch (Exception e) {
3109 if (!mInstrumentation.onException(r.activity, e)) {
3110 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003111 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3112 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 }
3114 }
3115 }
3116 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003117 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 return r;
3119 }
3120
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003121 private static String safeToComponentShortString(Intent intent) {
3122 ComponentName component = intent.getComponent();
3123 return component == null ? "[Unknown]" : component.toShortString();
3124 }
3125
Romain Guy65b345f2011-07-27 18:51:50 -07003126 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003128 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 configChanges, getNonConfigInstance);
3130 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003131 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 WindowManager wm = r.activity.getWindowManager();
3133 View v = r.activity.mDecor;
3134 if (v != null) {
3135 if (r.activity.mVisibleFromServer) {
3136 mNumVisibleActivities--;
3137 }
3138 IBinder wtoken = v.getWindowToken();
3139 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003140 if (r.onlyLocalRequest) {
3141 // Hold off on removing this until the new activity's
3142 // window is being added.
3143 r.mPendingRemoveWindow = v;
3144 r.mPendingRemoveWindowManager = wm;
3145 } else {
3146 wm.removeViewImmediate(v);
3147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003149 if (wtoken != null && r.mPendingRemoveWindow == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 WindowManagerImpl.getDefault().closeAll(wtoken,
3151 r.activity.getClass().getName(), "Activity");
3152 }
3153 r.activity.mDecor = null;
3154 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003155 if (r.mPendingRemoveWindow == null) {
3156 // If we are delaying the removal of the activity window, then
3157 // we can't clean up all windows here. Note that we can't do
3158 // so later either, which means any windows that aren't closed
3159 // by the app will leak. Well we try to warning them a lot
3160 // about leaking windows, because that is a bug, so if they are
3161 // using this recreate facility then they get to live with leaks.
3162 WindowManagerImpl.getDefault().closeAll(token,
3163 r.activity.getClass().getName(), "Activity");
3164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165
3166 // Mocked out contexts won't be participating in the normal
3167 // process lifecycle, but if we're running with a proper
3168 // ApplicationContext we need to have it tear down things
3169 // cleanly.
3170 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003171 if (c instanceof ContextImpl) {
3172 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003173 r.activity.getClass().getName(), "Activity");
3174 }
3175 }
3176 if (finishing) {
3177 try {
3178 ActivityManagerNative.getDefault().activityDestroyed(token);
3179 } catch (RemoteException ex) {
3180 // If the system process has died, it's game over for everyone.
3181 }
3182 }
3183 }
3184
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003185 public final void requestRelaunchActivity(IBinder token,
3186 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3187 int configChanges, boolean notResumed, Configuration config,
3188 boolean fromServer) {
3189 ActivityClientRecord target = null;
3190
3191 synchronized (mPackages) {
3192 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3193 ActivityClientRecord r = mRelaunchingActivities.get(i);
3194 if (r.token == token) {
3195 target = r;
3196 if (pendingResults != null) {
3197 if (r.pendingResults != null) {
3198 r.pendingResults.addAll(pendingResults);
3199 } else {
3200 r.pendingResults = pendingResults;
3201 }
3202 }
3203 if (pendingNewIntents != null) {
3204 if (r.pendingIntents != null) {
3205 r.pendingIntents.addAll(pendingNewIntents);
3206 } else {
3207 r.pendingIntents = pendingNewIntents;
3208 }
3209 }
3210 break;
3211 }
3212 }
3213
3214 if (target == null) {
3215 target = new ActivityClientRecord();
3216 target.token = token;
3217 target.pendingResults = pendingResults;
3218 target.pendingIntents = pendingNewIntents;
3219 if (!fromServer) {
3220 ActivityClientRecord existing = mActivities.get(token);
3221 if (existing != null) {
3222 target.startsNotResumed = existing.paused;
3223 }
3224 target.onlyLocalRequest = true;
3225 }
3226 mRelaunchingActivities.add(target);
3227 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3228 }
3229
3230 if (fromServer) {
3231 target.startsNotResumed = notResumed;
3232 target.onlyLocalRequest = false;
3233 }
3234 if (config != null) {
3235 target.createdConfig = config;
3236 }
3237 target.pendingConfigChanges |= configChanges;
3238 }
3239 }
3240
Romain Guy65b345f2011-07-27 18:51:50 -07003241 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 // If we are getting ready to gc after going to the background, well
3243 // we are back active so skip it.
3244 unscheduleGcIdler();
3245
3246 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003247 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 // First: make sure we have the most recent configuration and most
3250 // recent version of the activity, or skip it if some previous call
3251 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003252 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 int N = mRelaunchingActivities.size();
3254 IBinder token = tmp.token;
3255 tmp = null;
3256 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003257 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 if (r.token == token) {
3259 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003260 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 mRelaunchingActivities.remove(i);
3262 i--;
3263 N--;
3264 }
3265 }
Bob Leee5408332009-09-04 18:31:17 -07003266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003268 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269 return;
3270 }
Bob Leee5408332009-09-04 18:31:17 -07003271
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003272 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3273 + tmp.token + " with configChanges=0x"
3274 + Integer.toHexString(configChanges));
3275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 if (mPendingConfiguration != null) {
3277 changedConfig = mPendingConfiguration;
3278 mPendingConfiguration = null;
3279 }
3280 }
Bob Leee5408332009-09-04 18:31:17 -07003281
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003282 if (tmp.createdConfig != null) {
3283 // If the activity manager is passing us its current config,
3284 // assume that is really what we want regardless of what we
3285 // may have pending.
3286 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003287 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3288 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3289 if (changedConfig == null
3290 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3291 changedConfig = tmp.createdConfig;
3292 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003293 }
3294 }
3295
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003296 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003297 + tmp.token + ": changedConfig=" + changedConfig);
3298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 // If there was a pending configuration change, execute it first.
3300 if (changedConfig != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003301 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 }
Bob Leee5408332009-09-04 18:31:17 -07003303
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003304 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003305 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 if (r == null) {
3307 return;
3308 }
Bob Leee5408332009-09-04 18:31:17 -07003309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003311 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003312 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003313
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003314 r.activity.mChangingConfigurations = true;
3315
Dianne Hackborne2b04802010-12-09 09:24:55 -08003316 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003318 performPauseActivity(r.token, false, r.isPreHoneycomb());
3319 }
3320 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3321 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003322 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003323 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324 }
Bob Leee5408332009-09-04 18:31:17 -07003325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 r.activity = null;
3329 r.window = null;
3330 r.hideForNow = false;
3331 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003332 // Merge any pending results and pending intents; don't just replace them
3333 if (tmp.pendingResults != null) {
3334 if (r.pendingResults == null) {
3335 r.pendingResults = tmp.pendingResults;
3336 } else {
3337 r.pendingResults.addAll(tmp.pendingResults);
3338 }
3339 }
3340 if (tmp.pendingIntents != null) {
3341 if (r.pendingIntents == null) {
3342 r.pendingIntents = tmp.pendingIntents;
3343 } else {
3344 r.pendingIntents.addAll(tmp.pendingIntents);
3345 }
3346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003348
Christopher Tateb70f3df2009-04-07 16:07:59 -07003349 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 }
3351
Romain Guy65b345f2011-07-27 18:51:50 -07003352 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003353 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003354 Bitmap thumbnail = createThumbnailBitmap(r);
3355 CharSequence description = null;
3356 try {
3357 description = r.activity.onCreateDescription();
3358 } catch (Exception e) {
3359 if (!mInstrumentation.onException(r.activity, e)) {
3360 throw new RuntimeException(
3361 "Unable to create description of activity "
3362 + r.intent.getComponent().toShortString()
3363 + ": " + e.toString(), e);
3364 }
3365 }
3366 //System.out.println("Reporting top thumbnail " + thumbnail);
3367 try {
3368 ActivityManagerNative.getDefault().reportThumbnail(
3369 token, thumbnail, description);
3370 } catch (RemoteException ex) {
3371 }
3372 }
3373
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003374 ArrayList<ComponentCallbacks2> collectComponentCallbacksLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003375 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003376 ArrayList<ComponentCallbacks2> callbacks
3377 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 if (mActivities.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003380 Iterator<ActivityClientRecord> it = mActivities.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 while (it.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003382 ActivityClientRecord ar = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003383 Activity a = ar.activity;
3384 if (a != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003385 Configuration thisConfig = applyConfigCompatMainThread(newConfig,
3386 ar.packageInfo.mCompatibilityInfo.getIfNeeded());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387 if (!ar.activity.mFinished && (allActivities ||
3388 (a != null && !ar.paused))) {
3389 // If the activity is currently resumed, its configuration
3390 // needs to change right now.
3391 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003392 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 // Otherwise, we will tell it about the change
3394 // the next time it is resumed or shown. Note that
3395 // the activity manager may, before then, decide the
3396 // activity needs to be destroyed to handle its new
3397 // configuration.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003398 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Setting activity "
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003399 + ar.activityInfo.name + " newConfig=" + thisConfig);
3400 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 }
3402 }
3403 }
3404 }
3405 if (mServices.size() > 0) {
3406 Iterator<Service> it = mServices.values().iterator();
3407 while (it.hasNext()) {
3408 callbacks.add(it.next());
3409 }
3410 }
3411 synchronized (mProviderMap) {
3412 if (mLocalProviders.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003413 Iterator<ProviderClientRecord> it = mLocalProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414 while (it.hasNext()) {
3415 callbacks.add(it.next().mLocalProvider);
3416 }
3417 }
3418 }
3419 final int N = mAllApplications.size();
3420 for (int i=0; i<N; i++) {
3421 callbacks.add(mAllApplications.get(i));
3422 }
Bob Leee5408332009-09-04 18:31:17 -07003423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 return callbacks;
3425 }
Bob Leee5408332009-09-04 18:31:17 -07003426
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003427 private final void performConfigurationChanged(
3428 ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003430 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 // we check the runtime type and act accordingly.
3432 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3433 if (activity != null) {
3434 activity.mCalled = false;
3435 }
Bob Leee5408332009-09-04 18:31:17 -07003436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437 boolean shouldChangeConfig = false;
3438 if ((activity == null) || (activity.mCurrentConfig == null)) {
3439 shouldChangeConfig = true;
3440 } else {
Bob Leee5408332009-09-04 18:31:17 -07003441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 // If the new config is the same as the config this Activity
3443 // is already running with then don't bother calling
3444 // onConfigurationChanged
3445 int diff = activity.mCurrentConfig.diff(config);
3446 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 // If this activity doesn't handle any of the config changes
3448 // then don't bother calling onConfigurationChanged as we're
3449 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003450 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 shouldChangeConfig = true;
3452 }
3453 }
3454 }
Bob Leee5408332009-09-04 18:31:17 -07003455
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003456 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003457 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 if (shouldChangeConfig) {
3459 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 if (activity != null) {
3462 if (!activity.mCalled) {
3463 throw new SuperNotCalledException(
3464 "Activity " + activity.getLocalClassName() +
3465 " did not call through to super.onConfigurationChanged()");
3466 }
3467 activity.mConfigChangeFlags = 0;
3468 activity.mCurrentConfig = new Configuration(config);
3469 }
3470 }
3471 }
3472
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003473 public final void applyConfigurationToResources(Configuration config) {
3474 synchronized (mPackages) {
3475 applyConfigurationToResourcesLocked(config, null);
3476 }
3477 }
3478
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003479 final boolean applyConfigurationToResourcesLocked(Configuration config,
3480 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003481 if (mResConfiguration == null) {
3482 mResConfiguration = new Configuration();
3483 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003484 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003485 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003486 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003487 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003488 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003489 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003490 DisplayMetrics dm = getDisplayMetricsLocked(null, true);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003491
3492 if (compat != null && (mResCompatibilityInfo == null ||
3493 !mResCompatibilityInfo.equals(compat))) {
3494 mResCompatibilityInfo = compat;
3495 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3496 | ActivityInfo.CONFIG_SCREEN_SIZE
3497 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3498 }
Bob Leee5408332009-09-04 18:31:17 -07003499
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003500 // set it for java, this also affects newly created Resources
3501 if (config.locale != null) {
3502 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 }
Bob Leee5408332009-09-04 18:31:17 -07003504
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003505 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003506
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003507 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003508 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003509
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003510 Iterator<WeakReference<Resources>> it =
3511 mActiveResources.values().iterator();
3512 //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3513 // mActiveResources.entrySet().iterator();
3514 while (it.hasNext()) {
3515 WeakReference<Resources> v = it.next();
3516 Resources r = v.get();
3517 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003518 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003519 + r + " config to: " + config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003520 r.updateConfiguration(config, dm, compat);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003521 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003522 // + " " + r + ": " + r.getConfiguration());
3523 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003524 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003525 it.remove();
3526 }
3527 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003528
3529 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003530 }
Dianne Hackborn836e2622011-10-04 18:32:39 -07003531
3532 final Configuration applyCompatConfiguration() {
3533 Configuration config = mConfiguration;
3534 if (mCompatConfiguration == null) {
3535 mCompatConfiguration = new Configuration();
3536 }
3537 mCompatConfiguration.setTo(mConfiguration);
3538 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
3539 mResCompatibilityInfo.applyToConfiguration(mCompatConfiguration);
3540 config = mCompatConfiguration;
3541 }
3542 return config;
3543 }
3544
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003545 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003546
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003547 ArrayList<ComponentCallbacks2> callbacks = null;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003548
3549 synchronized (mPackages) {
3550 if (mPendingConfiguration != null) {
3551 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3552 config = mPendingConfiguration;
3553 }
3554 mPendingConfiguration = null;
3555 }
3556
3557 if (config == null) {
3558 return;
3559 }
3560
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003561 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003562 + config);
3563
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003564 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 if (mConfiguration == null) {
3567 mConfiguration = new Configuration();
3568 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003569 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003570 return;
3571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 mConfiguration.updateFrom(config);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003573 config = applyCompatConfiguration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003574 callbacks = collectComponentCallbacksLocked(false, config);
3575 }
Romain Guy65b345f2011-07-27 18:51:50 -07003576
3577 // Cleanup hardware accelerated stuff
3578 WindowManagerImpl.getDefault().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003579
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003580 if (callbacks != null) {
3581 final int N = callbacks.size();
3582 for (int i=0; i<N; i++) {
3583 performConfigurationChanged(callbacks.get(i), config);
3584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003585 }
3586 }
3587
3588 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003589 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003590 if (r == null || r.activity == null) {
3591 return;
3592 }
Bob Leee5408332009-09-04 18:31:17 -07003593
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003594 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003595 + r.activityInfo.name);
3596
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003597 performConfigurationChanged(r.activity, mCompatConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 }
3599
Romain Guy7eabe552011-07-21 14:56:34 -07003600 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003601 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003602 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003603 switch (profileType) {
3604 case 1:
3605 ViewDebug.startLooperProfiling(pcd.path, pcd.fd.getFileDescriptor());
3606 break;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003607 default:
3608 mProfiler.setProfiler(pcd.path, pcd.fd);
3609 mProfiler.autoStopProfiler = false;
3610 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003611 break;
3612 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003613 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003614 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003615 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003616 } finally {
3617 try {
3618 pcd.fd.close();
3619 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003620 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003621 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003622 }
3623 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003624 switch (profileType) {
3625 case 1:
3626 ViewDebug.stopLooperProfiling();
3627 break;
3628 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003629 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003630 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003631 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003632 }
3633 }
Bob Leee5408332009-09-04 18:31:17 -07003634
Andy McFadden824c5102010-07-09 16:26:57 -07003635 final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
3636 if (managed) {
3637 try {
3638 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3639 } catch (IOException e) {
3640 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3641 + " -- can the process access this path?");
3642 } finally {
3643 try {
3644 dhd.fd.close();
3645 } catch (IOException e) {
3646 Slog.w(TAG, "Failure closing profile fd", e);
3647 }
3648 }
3649 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003650 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003651 }
3652 }
3653
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003654 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3655 boolean hasPkgInfo = false;
3656 if (packages != null) {
3657 for (int i=packages.length-1; i>=0; i--) {
3658 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3659 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003660 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003661 ref = mPackages.get(packages[i]);
3662 if (ref != null && ref.get() != null) {
3663 hasPkgInfo = true;
3664 } else {
3665 ref = mResourcePackages.get(packages[i]);
3666 if (ref != null && ref.get() != null) {
3667 hasPkgInfo = true;
3668 }
3669 }
3670 }
3671 mPackages.remove(packages[i]);
3672 mResourcePackages.remove(packages[i]);
3673 }
3674 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003675 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003676 hasPkgInfo);
3677 }
3678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 final void handleLowMemory() {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003680 ArrayList<ComponentCallbacks2> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003681
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003682 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 callbacks = collectComponentCallbacksLocked(true, null);
3684 }
Bob Leee5408332009-09-04 18:31:17 -07003685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 final int N = callbacks.size();
3687 for (int i=0; i<N; i++) {
3688 callbacks.get(i).onLowMemory();
3689 }
3690
Chris Tatece229052009-03-25 16:44:52 -07003691 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3692 if (Process.myUid() != Process.SYSTEM_UID) {
3693 int sqliteReleased = SQLiteDatabase.releaseMemory();
3694 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3695 }
Bob Leee5408332009-09-04 18:31:17 -07003696
Mike Reedcaf0df12009-04-27 14:32:05 -04003697 // Ask graphics to free up as much as possible (font/image caches)
3698 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699
3700 BinderInternal.forceGc("mem");
3701 }
3702
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003703 final void handleTrimMemory(int level) {
Romain Guybdf76092011-07-18 15:00:43 -07003704 WindowManagerImpl.getDefault().trimMemory(level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003705 ArrayList<ComponentCallbacks2> callbacks;
3706
3707 synchronized (mPackages) {
3708 callbacks = collectComponentCallbacksLocked(true, null);
3709 }
3710
3711 final int N = callbacks.size();
3712 for (int i=0; i<N; i++) {
3713 callbacks.get(i).onTrimMemory(level);
3714 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003715 }
3716
Romain Guy65b345f2011-07-27 18:51:50 -07003717 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718 mBoundApplication = data;
3719 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003720 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003722 mProfiler = new Profiler();
3723 mProfiler.profileFile = data.initProfileFile;
3724 mProfiler.profileFd = data.initProfileFd;
3725 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
3726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003728 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 android.ddm.DdmHandleAppName.setAppName(data.processName);
3730
Dianne Hackborn5d927c22011-09-02 12:22:18 -07003731 if (data.persistent) {
3732 // Persistent processes on low-memory devices do not get to
3733 // use hardware accelerated drawing, since this can add too much
3734 // overhead to the process.
3735 Display display = WindowManagerImpl.getDefault().getDefaultDisplay();
3736 if (!ActivityManager.isHighEndGfx(display)) {
3737 HardwareRenderer.disable(false);
3738 }
3739 }
3740
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003741 if (mProfiler.profileFd != null) {
3742 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003743 }
3744
Joe Onoratod630f102011-03-17 18:42:26 -07003745 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
3746 // implementation to use the pool executor. Normally, we use the
3747 // serialized executor as the default. This has to happen in the
3748 // main thread so the main looper is set right.
3749 if (data.appInfo.targetSdkVersion <= 12) {
3750 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
3751 }
3752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003753 /*
3754 * Before spawning a new process, reset the time zone to be the system time zone.
3755 * This needs to be done because the system time zone could have changed after the
3756 * the spawning of this process. Without doing this this process would have the incorrect
3757 * system time zone.
3758 */
3759 TimeZone.setDefault(null);
3760
3761 /*
3762 * Initialize the default locale in this process for the reasons we set the time zone.
3763 */
3764 Locale.setDefault(data.config.locale);
3765
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003766 /*
3767 * Update the system configuration since its preloaded and might not
3768 * reflect configuration changes. The configuration object passed
3769 * in AppBindData can be safely assumed to be up to date
3770 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003771 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003772 applyCompatConfiguration();
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003773
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003774 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003776 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003777 * For system applications on userdebug/eng builds, log stack
3778 * traces of disk and network access to dropbox for analysis.
3779 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07003780 if ((data.appInfo.flags &
3781 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07003782 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
3783 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003784 }
3785
3786 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07003787 * For apps targetting SDK Honeycomb or later, we don't allow
3788 * network usage on the main event loop / UI thread.
3789 *
3790 * Note to those grepping: this is what ultimately throws
3791 * NetworkOnMainThreadException ...
3792 */
3793 if (data.appInfo.targetSdkVersion > 9) {
3794 StrictMode.enableDeathOnNetwork();
3795 }
3796
3797 /**
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003798 * Switch this process to density compatibility mode if needed.
3799 */
3800 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
3801 == 0) {
3802 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3803 }
Bob Leee5408332009-09-04 18:31:17 -07003804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
3806 // XXX should have option to change the port.
3807 Debug.changeDebugPort(8100);
3808 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003809 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 + " is waiting for the debugger on port 8100...");
3811
3812 IActivityManager mgr = ActivityManagerNative.getDefault();
3813 try {
3814 mgr.showWaitingForDebugger(mAppThread, true);
3815 } catch (RemoteException ex) {
3816 }
3817
3818 Debug.waitForDebugger();
3819
3820 try {
3821 mgr.showWaitingForDebugger(mAppThread, false);
3822 } catch (RemoteException ex) {
3823 }
3824
3825 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003826 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 + " can be debugged on port 8100...");
3828 }
3829 }
3830
Robert Greenwalt434203a2010-10-11 16:00:27 -07003831 /**
3832 * Initialize the default http proxy in this process for the reasons we set the time zone.
3833 */
3834 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
3835 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
3836 try {
3837 ProxyProperties proxyProperties = service.getProxy();
3838 Proxy.setHttpProxySystemProperty(proxyProperties);
3839 } catch (RemoteException e) {}
3840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003841 if (data.instrumentationName != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08003842 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003843 appContext.init(data.info, null, this);
3844 InstrumentationInfo ii = null;
3845 try {
3846 ii = appContext.getPackageManager().
3847 getInstrumentationInfo(data.instrumentationName, 0);
3848 } catch (PackageManager.NameNotFoundException e) {
3849 }
3850 if (ii == null) {
3851 throw new RuntimeException(
3852 "Unable to find instrumentation info for: "
3853 + data.instrumentationName);
3854 }
3855
3856 mInstrumentationAppDir = ii.sourceDir;
3857 mInstrumentationAppPackage = ii.packageName;
3858 mInstrumentedAppDir = data.info.getAppDir();
3859
3860 ApplicationInfo instrApp = new ApplicationInfo();
3861 instrApp.packageName = ii.packageName;
3862 instrApp.sourceDir = ii.sourceDir;
3863 instrApp.publicSourceDir = ii.publicSourceDir;
3864 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07003865 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003866 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003867 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08003868 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003869 instrContext.init(pi, null, this);
3870
3871 try {
3872 java.lang.ClassLoader cl = instrContext.getClassLoader();
3873 mInstrumentation = (Instrumentation)
3874 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
3875 } catch (Exception e) {
3876 throw new RuntimeException(
3877 "Unable to instantiate instrumentation "
3878 + data.instrumentationName + ": " + e.toString(), e);
3879 }
3880
3881 mInstrumentation.init(this, instrContext, appContext,
3882 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
3883
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003884 if (mProfiler.profileFile != null && !ii.handleProfiling
3885 && mProfiler.profileFd == null) {
3886 mProfiler.handlingProfiling = true;
3887 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 file.getParentFile().mkdirs();
3889 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
3890 }
3891
3892 try {
3893 mInstrumentation.onCreate(data.instrumentationArgs);
3894 }
3895 catch (Exception e) {
3896 throw new RuntimeException(
3897 "Exception thrown in onCreate() of "
3898 + data.instrumentationName + ": " + e.toString(), e);
3899 }
3900
3901 } else {
3902 mInstrumentation = new Instrumentation();
3903 }
3904
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003905 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08003906 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003907 }
3908
Christopher Tate181fafa2009-05-14 11:12:14 -07003909 // If the app is being launched for full backup or restore, bring it up in
3910 // a restricted environment with the base application class.
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003911 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 mInitialApplication = app;
3913
Christopher Tate75a99702011-05-18 16:28:19 -07003914 // don't bring up providers in restricted mode; they may depend on the
3915 // app's custom Application class
3916 if (!data.restrictedBackupMode){
3917 List<ProviderInfo> providers = data.providers;
3918 if (providers != null) {
3919 installContentProviders(app, providers);
3920 // For process that contains content providers, we want to
3921 // ensure that the JIT is enabled "at some point".
3922 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
3923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003924 }
3925
3926 try {
3927 mInstrumentation.callApplicationOnCreate(app);
3928 } catch (Exception e) {
3929 if (!mInstrumentation.onException(app, e)) {
3930 throw new RuntimeException(
3931 "Unable to create application " + app.getClass().getName()
3932 + ": " + e.toString(), e);
3933 }
3934 }
3935 }
3936
3937 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
3938 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003939 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
3940 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941 Debug.stopMethodTracing();
3942 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003943 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003944 // + ", app thr: " + mAppThread);
3945 try {
3946 am.finishInstrumentation(mAppThread, resultCode, results);
3947 } catch (RemoteException ex) {
3948 }
3949 }
3950
Romain Guy65b345f2011-07-27 18:51:50 -07003951 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003952 Context context, List<ProviderInfo> providers) {
3953 final ArrayList<IActivityManager.ContentProviderHolder> results =
3954 new ArrayList<IActivityManager.ContentProviderHolder>();
3955
3956 Iterator<ProviderInfo> i = providers.iterator();
3957 while (i.hasNext()) {
3958 ProviderInfo cpi = i.next();
3959 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07003960 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003961 buf.append(cpi.authority);
3962 buf.append(": ");
3963 buf.append(cpi.name);
3964 Log.i(TAG, buf.toString());
3965 IContentProvider cp = installProvider(context, null, cpi, false);
3966 if (cp != null) {
3967 IActivityManager.ContentProviderHolder cph =
3968 new IActivityManager.ContentProviderHolder(cpi);
3969 cph.provider = cp;
3970 results.add(cph);
3971 // Don't ever unload this provider from the process.
3972 synchronized(mProviderMap) {
3973 mProviderRefCountMap.put(cp.asBinder(), new ProviderRefCount(10000));
3974 }
3975 }
3976 }
3977
3978 try {
3979 ActivityManagerNative.getDefault().publishContentProviders(
3980 getApplicationThread(), results);
3981 } catch (RemoteException ex) {
3982 }
3983 }
3984
Romain Guy65b345f2011-07-27 18:51:50 -07003985 private IContentProvider getExistingProvider(Context context, String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003986 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003987 final ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003988 if (pr != null) {
3989 return pr.mProvider;
3990 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003991 return null;
3992 }
3993 }
3994
Romain Guy65b345f2011-07-27 18:51:50 -07003995 private IContentProvider getProvider(Context context, String name) {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003996 IContentProvider existing = getExistingProvider(context, name);
3997 if (existing != null) {
3998 return existing;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003999 }
4000
4001 IActivityManager.ContentProviderHolder holder = null;
4002 try {
4003 holder = ActivityManagerNative.getDefault().getContentProvider(
4004 getApplicationThread(), name);
4005 } catch (RemoteException ex) {
4006 }
4007 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004008 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 return null;
4010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004011
4012 IContentProvider prov = installProvider(context, holder.provider,
4013 holder.info, true);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004014 //Slog.i(TAG, "noReleaseNeeded=" + holder.noReleaseNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 if (holder.noReleaseNeeded || holder.provider == null) {
4016 // We are not going to release the provider if it is an external
4017 // provider that doesn't care about being released, or if it is
4018 // a local provider running in this process.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004019 //Slog.i(TAG, "*** NO RELEASE NEEDED");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004020 synchronized(mProviderMap) {
4021 mProviderRefCountMap.put(prov.asBinder(), new ProviderRefCount(10000));
4022 }
4023 }
4024 return prov;
4025 }
4026
4027 public final IContentProvider acquireProvider(Context c, String name) {
4028 IContentProvider provider = getProvider(c, name);
4029 if(provider == null)
4030 return null;
4031 IBinder jBinder = provider.asBinder();
4032 synchronized(mProviderMap) {
4033 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4034 if(prc == null) {
4035 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
4036 } else {
4037 prc.count++;
4038 } //end else
4039 } //end synchronized
4040 return provider;
4041 }
4042
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004043 public final IContentProvider acquireExistingProvider(Context c, String name) {
4044 IContentProvider provider = getExistingProvider(c, name);
4045 if(provider == null)
4046 return null;
4047 IBinder jBinder = provider.asBinder();
4048 synchronized(mProviderMap) {
4049 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4050 if(prc == null) {
4051 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
4052 } else {
4053 prc.count++;
4054 } //end else
4055 } //end synchronized
4056 return provider;
4057 }
4058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 public final boolean releaseProvider(IContentProvider provider) {
4060 if(provider == null) {
4061 return false;
4062 }
4063 IBinder jBinder = provider.asBinder();
4064 synchronized(mProviderMap) {
4065 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4066 if(prc == null) {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004067 if(localLOGV) Slog.v(TAG, "releaseProvider::Weird shouldn't be here");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 return false;
4069 } else {
4070 prc.count--;
4071 if(prc.count == 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004072 // Schedule the actual remove asynchronously, since we
4073 // don't know the context this will be called in.
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004074 // TODO: it would be nice to post a delayed message, so
4075 // if we come back and need the same provider quickly
4076 // we will still have it available.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004077 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, provider);
4078 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004079 } //end if
4080 } //end else
4081 } //end synchronized
4082 return true;
4083 }
4084
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004085 final void completeRemoveProvider(IContentProvider provider) {
4086 IBinder jBinder = provider.asBinder();
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004087 String name = null;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004088 synchronized(mProviderMap) {
4089 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4090 if(prc != null && prc.count == 0) {
4091 mProviderRefCountMap.remove(jBinder);
4092 //invoke removeProvider to dereference provider
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004093 name = removeProviderLocked(provider);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004094 }
4095 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004096
4097 if (name != null) {
4098 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004099 if(localLOGV) Slog.v(TAG, "removeProvider::Invoking " +
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004100 "ActivityManagerNative.removeContentProvider(" + name);
4101 ActivityManagerNative.getDefault().removeContentProvider(
4102 getApplicationThread(), name);
4103 } catch (RemoteException e) {
4104 //do nothing content provider object is dead any way
4105 } //end catch
4106 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004107 }
4108
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004109 public final String removeProviderLocked(IContentProvider provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 if (provider == null) {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004111 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 }
4113 IBinder providerBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004114
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004115 String name = null;
4116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117 // remove the provider from mProviderMap
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004118 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004119 while (iter.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004120 ProviderClientRecord pr = iter.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 IBinder myBinder = pr.mProvider.asBinder();
4122 if (myBinder == providerBinder) {
4123 //find if its published by this process itself
4124 if(pr.mLocalProvider != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004125 if(localLOGV) Slog.i(TAG, "removeProvider::found local provider returning");
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004126 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004127 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004128 if(localLOGV) Slog.v(TAG, "removeProvider::Not local provider Unlinking " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004129 "death recipient");
4130 //content provider is in another process
4131 myBinder.unlinkToDeath(pr, 0);
4132 iter.remove();
4133 //invoke remove only once for the very first name seen
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004134 if(name == null) {
4135 name = pr.mName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136 }
4137 } //end if myBinder
4138 } //end while iter
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004139
4140 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004141 }
4142
4143 final void removeDeadProvider(String name, IContentProvider provider) {
4144 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004145 ProviderClientRecord pr = mProviderMap.get(name);
Jeff Brown2a4c5ac2011-08-23 11:58:26 -07004146 if (pr != null && pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004147 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004148 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004149 if (removed != null) {
4150 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
4151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 }
4153 }
4154 }
4155
Romain Guy65b345f2011-07-27 18:51:50 -07004156 private IContentProvider installProvider(Context context,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004157 IContentProvider provider, ProviderInfo info, boolean noisy) {
4158 ContentProvider localProvider = null;
4159 if (provider == null) {
4160 if (noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004161 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 + info.name);
4163 }
4164 Context c = null;
4165 ApplicationInfo ai = info.applicationInfo;
4166 if (context.getPackageName().equals(ai.packageName)) {
4167 c = context;
4168 } else if (mInitialApplication != null &&
4169 mInitialApplication.getPackageName().equals(ai.packageName)) {
4170 c = mInitialApplication;
4171 } else {
4172 try {
4173 c = context.createPackageContext(ai.packageName,
4174 Context.CONTEXT_INCLUDE_CODE);
4175 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004176 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004177 }
4178 }
4179 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004180 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 ai.packageName +
4182 " while loading content provider " +
4183 info.name);
4184 return null;
4185 }
4186 try {
4187 final java.lang.ClassLoader cl = c.getClassLoader();
4188 localProvider = (ContentProvider)cl.
4189 loadClass(info.name).newInstance();
4190 provider = localProvider.getIContentProvider();
4191 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004192 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 info.name + " from sourceDir " +
4194 info.applicationInfo.sourceDir);
4195 return null;
4196 }
Joe Onorato43a17652011-04-06 19:22:23 -07004197 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 TAG, "Instantiating local provider " + info.name);
4199 // XXX Need to create the correct context for this provider.
4200 localProvider.attachInfo(c, info);
4201 } catch (java.lang.Exception e) {
4202 if (!mInstrumentation.onException(null, e)) {
4203 throw new RuntimeException(
4204 "Unable to get provider " + info.name
4205 + ": " + e.toString(), e);
4206 }
4207 return null;
4208 }
4209 } else if (localLOGV) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004210 Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 + info.name);
4212 }
4213
4214 synchronized (mProviderMap) {
4215 // Cache the pointer for the remote provider.
4216 String names[] = PATTERN_SEMICOLON.split(info.authority);
4217 for (int i=0; i<names.length; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004218 ProviderClientRecord pr = new ProviderClientRecord(names[i], provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 localProvider);
4220 try {
4221 provider.asBinder().linkToDeath(pr, 0);
4222 mProviderMap.put(names[i], pr);
4223 } catch (RemoteException e) {
4224 return null;
4225 }
4226 }
4227 if (localProvider != null) {
4228 mLocalProviders.put(provider.asBinder(),
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004229 new ProviderClientRecord(null, provider, localProvider));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230 }
4231 }
4232
4233 return provider;
4234 }
4235
Romain Guy65b345f2011-07-27 18:51:50 -07004236 private void attach(boolean system) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 sThreadLocal.set(this);
4238 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004240 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004241 public void run() {
4242 ensureJitEnabled();
4243 }
4244 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4246 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4247 IActivityManager mgr = ActivityManagerNative.getDefault();
4248 try {
4249 mgr.attachApplication(mAppThread);
4250 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004251 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004252 }
4253 } else {
4254 // Don't set application object here -- if the system crashes,
4255 // we can't display an alert, we just want to die die die.
4256 android.ddm.DdmHandleAppName.setAppName("system_process");
4257 try {
4258 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004259 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 context.init(getSystemContext().mPackageInfo, null, this);
4261 Application app = Instrumentation.newApplication(Application.class, context);
4262 mAllApplications.add(app);
4263 mInitialApplication = app;
4264 app.onCreate();
4265 } catch (Exception e) {
4266 throw new RuntimeException(
4267 "Unable to instantiate Application():" + e.toString(), e);
4268 }
4269 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004270
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004271 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004272 public void onConfigurationChanged(Configuration newConfig) {
4273 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004274 // We need to apply this change to the resources
4275 // immediately, because upon returning the view
4276 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004277 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004278 // This actually changed the resources! Tell
4279 // everyone about it.
4280 if (mPendingConfiguration == null ||
4281 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4282 mPendingConfiguration = newConfig;
4283
4284 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4285 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004286 }
4287 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004288 }
4289 public void onLowMemory() {
4290 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004291 public void onTrimMemory(int level) {
4292 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004293 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004294 }
4295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004296 public static final ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004297 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004298 ActivityThread thread = new ActivityThread();
4299 thread.attach(true);
4300 return thread;
4301 }
4302
Jeff Brown10e89712011-07-08 18:52:57 -07004303 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004304 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004305 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004306 }
4307 }
4308
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004309 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004310 synchronized (mPackages) {
4311 if (mCoreSettings != null) {
4312 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004313 } else {
4314 return defaultValue;
4315 }
4316 }
4317 }
4318
Romain Guy65b345f2011-07-27 18:51:50 -07004319 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004320 SamplingProfilerIntegration.start();
4321
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004322 // CloseGuard defaults to true and can be quite spammy. We
4323 // disable it here, but selectively enable it later (via
4324 // StrictMode) on debug builds, but using DropBox, not logs.
4325 CloseGuard.setEnabled(false);
4326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 Process.setArgV0("<pre-initialized>");
4328
4329 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07004330 if (sMainThreadHandler == null) {
4331 sMainThreadHandler = new Handler();
4332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333
4334 ActivityThread thread = new ActivityThread();
4335 thread.attach(false);
4336
Dianne Hackborn287952c2010-09-22 22:34:31 -07004337 if (false) {
4338 Looper.myLooper().setMessageLogging(new
4339 LogPrinter(Log.DEBUG, "ActivityThread"));
4340 }
4341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004342 Looper.loop();
4343
Jeff Brown10e89712011-07-08 18:52:57 -07004344 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004345 }
4346}