blob: e3762206588d0f4ff550bdeacda787497b7c7fcb [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;
156 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700157 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700158 Configuration mResConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700159 CompatibilityInfo mResCompatibilityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700160 Application mInitialApplication;
161 final ArrayList<Application> mAllApplications
162 = new ArrayList<Application>();
163 // set of instantiated backup agents, keyed by package name
164 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Romain Guy65b345f2011-07-27 18:51:50 -0700165 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700166 Instrumentation mInstrumentation;
167 String mInstrumentationAppDir = null;
168 String mInstrumentationAppPackage = null;
169 String mInstrumentedAppDir = null;
170 boolean mSystemThread = false;
171 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700173 // These can be accessed by multiple threads; mPackages is the lock.
174 // XXX For now we keep around information about all packages we have
175 // seen, not removing entries from this map.
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700176 // NOTE: The activity manager in its process needs to call in to
177 // ActivityThread to do things like update resource configurations,
178 // which means this lock gets held while the activity manager holds its
179 // own lock. Thus you MUST NEVER call back into the activity manager
180 // or anything that depends on it while holding this lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700181 final HashMap<String, WeakReference<LoadedApk>> mPackages
182 = new HashMap<String, WeakReference<LoadedApk>>();
183 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
184 = new HashMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700185 final HashMap<CompatibilityInfo, DisplayMetrics> mDisplayMetrics
186 = new HashMap<CompatibilityInfo, DisplayMetrics>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700187 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
188 = new HashMap<ResourcesKey, WeakReference<Resources> >();
189 final ArrayList<ActivityClientRecord> mRelaunchingActivities
190 = new ArrayList<ActivityClientRecord>();
191 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700193 // The lock of mProviderMap protects the following variables.
194 final HashMap<String, ProviderClientRecord> mProviderMap
195 = new HashMap<String, ProviderClientRecord>();
196 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
197 = new HashMap<IBinder, ProviderRefCount>();
198 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
199 = new HashMap<IBinder, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200
Jeff Hamilton52d32032011-01-08 15:31:26 -0600201 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
202 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
203
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700204 final GcIdler mGcIdler = new GcIdler();
205 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700207 static Handler sMainThreadHandler; // set once in main()
208
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800209 Bundle mCoreSettings = null;
210
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400211 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700213 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 Intent intent;
215 Bundle state;
216 Activity activity;
217 Window window;
218 Activity parent;
219 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700220 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 boolean paused;
222 boolean stopped;
223 boolean hideForNow;
224 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700225 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700226 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700228 String profileFile;
229 ParcelFileDescriptor profileFd;
230 boolean autoStopProfiler;
231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400233 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700234 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235
236 List<ResultInfo> pendingResults;
237 List<Intent> pendingIntents;
238
239 boolean startsNotResumed;
240 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800241 int pendingConfigChanges;
242 boolean onlyLocalRequest;
243
244 View mPendingRemoveWindow;
245 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700247 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 parent = null;
249 embeddedID = null;
250 paused = false;
251 stopped = false;
252 hideForNow = false;
253 nextIdle = null;
254 }
255
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800256 public boolean isPreHoneycomb() {
257 if (activity != null) {
258 return activity.getApplicationInfo().targetSdkVersion
259 < android.os.Build.VERSION_CODES.HONEYCOMB;
260 }
261 return false;
262 }
263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 public String toString() {
265 ComponentName componentName = intent.getComponent();
266 return "ActivityRecord{"
267 + Integer.toHexString(System.identityHashCode(this))
268 + " token=" + token + " " + (componentName == null
269 ? "no component name" : componentName.toShortString())
270 + "}";
271 }
272 }
273
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400274 final class ProviderClientRecord implements IBinder.DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 final String mName;
276 final IContentProvider mProvider;
277 final ContentProvider mLocalProvider;
278
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700279 ProviderClientRecord(String name, IContentProvider provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 ContentProvider localProvider) {
281 mName = name;
282 mProvider = provider;
283 mLocalProvider = localProvider;
284 }
285
286 public void binderDied() {
287 removeDeadProvider(mName, mProvider);
288 }
289 }
290
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400291 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 List<Intent> intents;
293 IBinder token;
294 public String toString() {
295 return "NewIntentData{intents=" + intents + " token=" + token + "}";
296 }
297 }
298
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400299 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700300 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
301 boolean ordered, boolean sticky, IBinder token) {
302 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky, token);
303 this.intent = intent;
304 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 Intent intent;
307 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400308 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 public String toString() {
310 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700311 info.packageName + " resultCode=" + getResultCode()
312 + " resultData=" + getResultData() + " resultExtras="
313 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 }
315 }
316
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400317 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700318 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400319 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700320 int backupMode;
321 public String toString() {
322 return "CreateBackupAgentData{appInfo=" + appInfo
323 + " backupAgent=" + appInfo.backupAgentName
324 + " mode=" + backupMode + "}";
325 }
326 }
Bob Leee5408332009-09-04 18:31:17 -0700327
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400328 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 IBinder token;
330 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400331 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 Intent intent;
333 public String toString() {
334 return "CreateServiceData{token=" + token + " className="
335 + info.name + " packageName=" + info.packageName
336 + " intent=" + intent + "}";
337 }
338 }
339
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400340 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 IBinder token;
342 Intent intent;
343 boolean rebind;
344 public String toString() {
345 return "BindServiceData{token=" + token + " intent=" + intent + "}";
346 }
347 }
348
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400349 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700351 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700353 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 Intent args;
355 public String toString() {
356 return "ServiceArgsData{token=" + token + " startId=" + startId
357 + " args=" + args + "}";
358 }
359 }
360
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400361 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700362 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 String processName;
364 ApplicationInfo appInfo;
365 List<ProviderInfo> providers;
366 ComponentName instrumentationName;
367 String profileFile;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700368 ParcelFileDescriptor profileFd;
369 boolean autoStopProfiler;
370 boolean profiling;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 Bundle instrumentationArgs;
372 IInstrumentationWatcher instrumentationWatcher;
373 int debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700374 boolean restrictedBackupMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400376 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 boolean handlingProfiling;
378 public String toString() {
379 return "AppBindData{appInfo=" + appInfo + "}";
380 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700381 public void setProfiler(String file, ParcelFileDescriptor fd) {
382 if (profiling) {
383 if (fd != null) {
384 try {
385 fd.close();
386 } catch (IOException e) {
387 }
388 }
389 return;
390 }
391 if (profileFd != null) {
392 try {
393 profileFd.close();
394 } catch (IOException e) {
395 }
396 }
397 profileFile = file;
398 profileFd = fd;
399 }
400 public void startProfiling() {
401 if (profileFd == null || profiling) {
402 return;
403 }
404 try {
405 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
406 8 * 1024 * 1024, 0);
407 profiling = true;
408 } catch (RuntimeException e) {
409 Slog.w(TAG, "Profiling failed on path " + profileFile);
410 try {
411 profileFd.close();
412 profileFd = null;
413 } catch (IOException e2) {
414 Slog.w(TAG, "Failure closing profile fd", e2);
415 }
416 }
417 }
418 public void stopProfiling() {
419 if (profiling) {
420 profiling = false;
421 Debug.stopMethodTracing();
422 if (profileFd != null) {
423 try {
424 profileFd.close();
425 } catch (IOException e) {
426 }
427 }
428 profileFd = null;
429 profileFile = null;
430 }
431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 }
433
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400434 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700435 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700436 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800437 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 }
440
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400441 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 IBinder token;
443 List<ResultInfo> results;
444 public String toString() {
445 return "ResultData{token=" + token + " results" + results + "}";
446 }
447 }
448
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400449 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800450 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 String what;
452 String who;
453 }
454
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400455 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700456 String path;
457 ParcelFileDescriptor fd;
458 }
459
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400460 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700461 String path;
462 ParcelFileDescriptor fd;
463 }
464
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400465 static final class UpdateCompatibilityData {
466 String pkg;
467 CompatibilityInfo info;
468 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700469
Romain Guy65b345f2011-07-27 18:51:50 -0700470 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700471
Romain Guy65b345f2011-07-27 18:51:50 -0700472 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700473 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
474 private static final String ONE_COUNT_COLUMN = "%21s %8d";
475 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
476 private static final String TWO_COUNT_COLUMNS_DB = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700477 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 // Formatting for checkin service - update version if row format changes
480 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 public final void schedulePauseActivity(IBinder token, boolean finished,
483 boolean userLeaving, int configChanges) {
484 queueOrSendMessage(
485 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
486 token,
487 (userLeaving ? 1 : 0),
488 configChanges);
489 }
490
491 public final void scheduleStopActivity(IBinder token, boolean showWindow,
492 int configChanges) {
493 queueOrSendMessage(
494 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
495 token, 0, configChanges);
496 }
497
498 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
499 queueOrSendMessage(
500 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
501 token);
502 }
503
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800504 public final void scheduleSleeping(IBinder token, boolean sleeping) {
505 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
506 }
507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
509 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
510 }
511
512 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
513 ResultData res = new ResultData();
514 res.token = token;
515 res.results = results;
516 queueOrSendMessage(H.SEND_RESULT, res);
517 }
518
519 // we use token to identify this activity without having to send the
520 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700521 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400522 ActivityInfo info, CompatibilityInfo compatInfo, Bundle state,
523 List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700524 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
525 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700526 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527
528 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700529 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 r.intent = intent;
531 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400532 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 r.state = state;
534
535 r.pendingResults = pendingResults;
536 r.pendingIntents = pendingNewIntents;
537
538 r.startsNotResumed = notResumed;
539 r.isForward = isForward;
540
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700541 r.profileFile = profileName;
542 r.profileFd = profileFd;
543 r.autoStopProfiler = autoStopProfiler;
544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
546 }
547
548 public final void scheduleRelaunchActivity(IBinder token,
549 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800550 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800551 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
552 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
554
555 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
556 NewIntentData data = new NewIntentData();
557 data.intents = intents;
558 data.token = token;
559
560 queueOrSendMessage(H.NEW_INTENT, data);
561 }
562
563 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
564 int configChanges) {
565 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
566 configChanges);
567 }
568
569 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400570 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
571 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700572 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
573 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400575 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 queueOrSendMessage(H.RECEIVER, r);
577 }
578
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400579 public final void scheduleCreateBackupAgent(ApplicationInfo app,
580 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700581 CreateBackupAgentData d = new CreateBackupAgentData();
582 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400583 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700584 d.backupMode = backupMode;
585
586 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
587 }
588
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400589 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
590 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700591 CreateBackupAgentData d = new CreateBackupAgentData();
592 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400593 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700594
595 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
596 }
597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400599 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 CreateServiceData s = new CreateServiceData();
601 s.token = token;
602 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400603 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604
605 queueOrSendMessage(H.CREATE_SERVICE, s);
606 }
607
608 public final void scheduleBindService(IBinder token, Intent intent,
609 boolean rebind) {
610 BindServiceData s = new BindServiceData();
611 s.token = token;
612 s.intent = intent;
613 s.rebind = rebind;
614
615 queueOrSendMessage(H.BIND_SERVICE, s);
616 }
617
618 public final void scheduleUnbindService(IBinder token, Intent intent) {
619 BindServiceData s = new BindServiceData();
620 s.token = token;
621 s.intent = intent;
622
623 queueOrSendMessage(H.UNBIND_SERVICE, s);
624 }
625
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700626 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700627 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 ServiceArgsData s = new ServiceArgsData();
629 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700630 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700632 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 s.args = args;
634
635 queueOrSendMessage(H.SERVICE_ARGS, s);
636 }
637
638 public final void scheduleStopService(IBinder token) {
639 queueOrSendMessage(H.STOP_SERVICE, token);
640 }
641
642 public final void bindApplication(String processName,
643 ApplicationInfo appInfo, List<ProviderInfo> providers,
644 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700645 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Christopher Tate181fafa2009-05-14 11:12:14 -0700647 int debugMode, boolean isRestrictedBackupMode, Configuration config,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400648 CompatibilityInfo compatInfo, Map<String, IBinder> services,
649 Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650
651 if (services != null) {
652 // Setup the service cache in the ServiceManager
653 ServiceManager.initServiceCache(services);
654 }
655
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800656 setCoreSettings(coreSettings);
657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 AppBindData data = new AppBindData();
659 data.processName = processName;
660 data.appInfo = appInfo;
661 data.providers = providers;
662 data.instrumentationName = instrumentationName;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700663 data.setProfiler(profileFile, profileFd);
664 data.autoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 data.instrumentationArgs = instrumentationArgs;
666 data.instrumentationWatcher = instrumentationWatcher;
667 data.debugMode = debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700668 data.restrictedBackupMode = isRestrictedBackupMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400670 data.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 queueOrSendMessage(H.BIND_APPLICATION, data);
672 }
673
674 public final void scheduleExit() {
675 queueOrSendMessage(H.EXIT_APPLICATION, null);
676 }
677
Christopher Tate5e1ab332009-09-01 20:32:49 -0700678 public final void scheduleSuicide() {
679 queueOrSendMessage(H.SUICIDE, null);
680 }
681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 public void requestThumbnail(IBinder token) {
683 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
684 }
685
686 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800687 synchronized (mPackages) {
688 if (mPendingConfiguration == null ||
689 mPendingConfiguration.isOtherSeqNewer(config)) {
690 mPendingConfiguration = config;
691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 }
693 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
694 }
695
696 public void updateTimeZone() {
697 TimeZone.setDefault(null);
698 }
699
Robert Greenwalt03595d02010-11-02 14:08:23 -0700700 public void clearDnsCache() {
701 // a non-standard API to get this to libcore
702 InetAddress.clearDnsCache();
703 }
704
Robert Greenwalt434203a2010-10-11 16:00:27 -0700705 public void setHttpProxy(String host, String port, String exclList) {
706 Proxy.setHttpProxySystemProperty(host, port, exclList);
707 }
708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 public void processInBackground() {
710 mH.removeMessages(H.GC_WHEN_IDLE);
711 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
712 }
713
714 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700715 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700716 try {
717 data.fd = ParcelFileDescriptor.dup(fd);
718 data.token = servicetoken;
719 data.args = args;
720 queueOrSendMessage(H.DUMP_SERVICE, data);
721 } catch (IOException e) {
722 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 }
724 }
725
726 // This function exists to make sure all receiver dispatching is
727 // correctly ordered, since these are one-way calls and the binder driver
728 // applies transaction ordering per object for such calls.
729 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700730 int resultCode, String dataStr, Bundle extras, boolean ordered,
731 boolean sticky) throws RemoteException {
732 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 }
Bob Leee5408332009-09-04 18:31:17 -0700734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 public void scheduleLowMemory() {
736 queueOrSendMessage(H.LOW_MEMORY, null);
737 }
738
739 public void scheduleActivityConfigurationChanged(IBinder token) {
740 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
741 }
742
Romain Guy7eabe552011-07-21 14:56:34 -0700743 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
744 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700745 ProfilerControlData pcd = new ProfilerControlData();
746 pcd.path = path;
747 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700748 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800749 }
750
Andy McFadden824c5102010-07-09 16:26:57 -0700751 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
752 DumpHeapData dhd = new DumpHeapData();
753 dhd.path = path;
754 dhd.fd = fd;
755 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
756 }
757
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700758 public void setSchedulingGroup(int group) {
759 // Note: do this immediately, since going into the foreground
760 // should happen regardless of what pending work we have to do
761 // and the activity manager will wait for us to report back that
762 // we are done before sending us to the background.
763 try {
764 Process.setProcessGroup(Process.myPid(), group);
765 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800766 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700767 }
768 }
Bob Leee5408332009-09-04 18:31:17 -0700769
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700770 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
771 Debug.getMemoryInfo(outInfo);
772 }
Bob Leee5408332009-09-04 18:31:17 -0700773
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700774 public void dispatchPackageBroadcast(int cmd, String[] packages) {
775 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
776 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700777
778 public void scheduleCrash(String msg) {
779 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
780 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700781
Dianne Hackborn30d71892010-12-11 10:37:55 -0800782 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
783 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700784 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700785 try {
786 data.fd = ParcelFileDescriptor.dup(fd);
787 data.token = activitytoken;
788 data.prefix = prefix;
789 data.args = args;
790 queueOrSendMessage(H.DUMP_ACTIVITY, data);
791 } catch (IOException e) {
792 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700793 }
794 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700797 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
798 boolean all, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700799 FileOutputStream fout = new FileOutputStream(fd);
800 PrintWriter pw = new PrintWriter(fout);
801 try {
Dianne Hackbornb437e092011-08-05 17:50:29 -0700802 return dumpMemInfo(pw, checkin, all, args);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700803 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700804 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700805 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700806 }
807
Dianne Hackbornb437e092011-08-05 17:50:29 -0700808 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean all,
809 String[] args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 long nativeMax = Debug.getNativeHeapSize() / 1024;
811 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
812 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
813
814 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
815 Debug.getMemoryInfo(memInfo);
816
Dianne Hackbornb437e092011-08-05 17:50:29 -0700817 if (!all) {
818 return memInfo;
819 }
820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 Runtime runtime = Runtime.getRuntime();
822
823 long dalvikMax = runtime.totalMemory() / 1024;
824 long dalvikFree = runtime.freeMemory() / 1024;
825 long dalvikAllocated = dalvikMax - dalvikFree;
826 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700827 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700828 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
829 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 int globalAssetCount = AssetManager.getGlobalAssetCount();
831 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
832 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
833 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
834 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700835 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 long sqliteAllocated = SQLiteDebug.getHeapAllocatedSize() / 1024;
Vasu Noric3849202010-03-09 10:47:25 -0800837 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700840 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 // NOTE: if you change anything significant below, also consider changing
842 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700843 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 // Header
847 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
848 pw.print(Process.myPid()); pw.print(',');
849 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 // Heap info - max
852 pw.print(nativeMax); pw.print(',');
853 pw.print(dalvikMax); pw.print(',');
854 pw.print("N/A,");
855 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 // Heap info - allocated
858 pw.print(nativeAllocated); pw.print(',');
859 pw.print(dalvikAllocated); pw.print(',');
860 pw.print("N/A,");
861 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 // Heap info - free
864 pw.print(nativeFree); pw.print(',');
865 pw.print(dalvikFree); pw.print(',');
866 pw.print("N/A,");
867 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 // Heap info - proportional set size
870 pw.print(memInfo.nativePss); pw.print(',');
871 pw.print(memInfo.dalvikPss); pw.print(',');
872 pw.print(memInfo.otherPss); pw.print(',');
873 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700876 pw.print(memInfo.nativeSharedDirty); pw.print(',');
877 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
878 pw.print(memInfo.otherSharedDirty); pw.print(',');
879 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
880 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700883 pw.print(memInfo.nativePrivateDirty); pw.print(',');
884 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
885 pw.print(memInfo.otherPrivateDirty); pw.print(',');
886 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
887 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 // Object counts
890 pw.print(viewInstanceCount); pw.print(',');
891 pw.print(viewRootInstanceCount); pw.print(',');
892 pw.print(appContextInstanceCount); pw.print(',');
893 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 pw.print(globalAssetCount); pw.print(',');
896 pw.print(globalAssetManagerCount); pw.print(',');
897 pw.print(binderLocalObjectCount); pw.print(',');
898 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 pw.print(binderDeathObjectCount); pw.print(',');
901 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 // SQL
904 pw.print(sqliteAllocated); pw.print(',');
Vasu Noric3849202010-03-09 10:47:25 -0800905 pw.print(stats.memoryUsed / 1024); pw.print(',');
906 pw.print(stats.pageCacheOverflo / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700907 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800908 for (int i = 0; i < stats.dbStats.size(); i++) {
909 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700910 pw.print(','); pw.print(dbStats.dbName);
911 pw.print(','); pw.print(dbStats.pageSize);
912 pw.print(','); pw.print(dbStats.dbSize);
913 pw.print(','); pw.print(dbStats.lookaside);
914 pw.print(','); pw.print(dbStats.cache);
915 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800916 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700917 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700918
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700919 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 }
Bob Leee5408332009-09-04 18:31:17 -0700921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700923 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
924 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
925 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
926 "------");
927 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
928 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
929 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
930 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700932 int otherPss = memInfo.otherPss;
933 int otherSharedDirty = memInfo.otherSharedDirty;
934 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700936 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Romain Guy65b345f2011-07-27 18:51:50 -0700937 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700938 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
939 memInfo.getOtherPrivateDirty(i), "", "", "");
940 otherPss -= memInfo.getOtherPss(i);
941 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
942 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
943 }
944
945 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
946 otherPrivateDirty, "", "", "");
947 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
948 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
949 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
950 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951
952 pw.println(" ");
953 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -0700954 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 viewRootInstanceCount);
956
957 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
958 "Activities:", activityInstanceCount);
959
960 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
961 "AssetManagers:", globalAssetManagerCount);
962
963 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
964 "Proxy Binders:", binderProxyObjectCount);
965 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
966
967 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -0700968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 // SQLite mem info
970 pw.println(" ");
971 pw.println(" SQL");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700972 printRow(pw, TWO_COUNT_COLUMNS_DB, "heap:", sqliteAllocated, "MEMORY_USED:",
Vasu Noric3849202010-03-09 10:47:25 -0800973 stats.memoryUsed / 1024);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700974 printRow(pw, TWO_COUNT_COLUMNS_DB, "PAGECACHE_OVERFLOW:",
975 stats.pageCacheOverflo / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800976 pw.println(" ");
977 int N = stats.dbStats.size();
978 if (N > 0) {
979 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700980 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
981 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -0800982 for (int i = 0; i < N; i++) {
983 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700984 printRow(pw, DB_INFO_FORMAT,
985 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
986 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
987 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
988 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -0800989 }
990 }
Bob Leee5408332009-09-04 18:31:17 -0700991
Dianne Hackborn82e1ee92009-08-11 18:56:41 -0700992 // Asset details.
993 String assetAlloc = AssetManager.getAssetAllocations();
994 if (assetAlloc != null) {
995 pw.println(" ");
996 pw.println(" Asset Allocations");
997 pw.print(assetAlloc);
998 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700999
1000 return memInfo;
1001 }
1002
1003 @Override
1004 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1005 dumpGraphicsInfo(fd);
Romain Guy65b345f2011-07-27 18:51:50 -07001006 WindowManagerImpl.getDefault().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 }
1008
1009 private void printRow(PrintWriter pw, String format, Object...objs) {
1010 pw.println(String.format(format, objs));
1011 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001012
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001013 public void setCoreSettings(Bundle coreSettings) {
1014 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001015 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001016
1017 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1018 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1019 ucd.pkg = pkg;
1020 ucd.info = info;
1021 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1022 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001023
1024 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001025 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 }
1028
Romain Guy65b345f2011-07-27 18:51:50 -07001029 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 public static final int LAUNCH_ACTIVITY = 100;
1031 public static final int PAUSE_ACTIVITY = 101;
1032 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1033 public static final int STOP_ACTIVITY_SHOW = 103;
1034 public static final int STOP_ACTIVITY_HIDE = 104;
1035 public static final int SHOW_WINDOW = 105;
1036 public static final int HIDE_WINDOW = 106;
1037 public static final int RESUME_ACTIVITY = 107;
1038 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001039 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 public static final int BIND_APPLICATION = 110;
1041 public static final int EXIT_APPLICATION = 111;
1042 public static final int NEW_INTENT = 112;
1043 public static final int RECEIVER = 113;
1044 public static final int CREATE_SERVICE = 114;
1045 public static final int SERVICE_ARGS = 115;
1046 public static final int STOP_SERVICE = 116;
1047 public static final int REQUEST_THUMBNAIL = 117;
1048 public static final int CONFIGURATION_CHANGED = 118;
1049 public static final int CLEAN_UP_CONTEXT = 119;
1050 public static final int GC_WHEN_IDLE = 120;
1051 public static final int BIND_SERVICE = 121;
1052 public static final int UNBIND_SERVICE = 122;
1053 public static final int DUMP_SERVICE = 123;
1054 public static final int LOW_MEMORY = 124;
1055 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1056 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001057 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001058 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001059 public static final int DESTROY_BACKUP_AGENT = 129;
1060 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001061 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001062 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001063 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001064 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001065 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001066 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001067 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001068 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001069 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001070 public static final int TRIM_MEMORY = 140;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001072 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 switch (code) {
1074 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1075 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1076 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1077 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1078 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1079 case SHOW_WINDOW: return "SHOW_WINDOW";
1080 case HIDE_WINDOW: return "HIDE_WINDOW";
1081 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1082 case SEND_RESULT: return "SEND_RESULT";
1083 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1084 case BIND_APPLICATION: return "BIND_APPLICATION";
1085 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1086 case NEW_INTENT: return "NEW_INTENT";
1087 case RECEIVER: return "RECEIVER";
1088 case CREATE_SERVICE: return "CREATE_SERVICE";
1089 case SERVICE_ARGS: return "SERVICE_ARGS";
1090 case STOP_SERVICE: return "STOP_SERVICE";
1091 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1092 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1093 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1094 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1095 case BIND_SERVICE: return "BIND_SERVICE";
1096 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1097 case DUMP_SERVICE: return "DUMP_SERVICE";
1098 case LOW_MEMORY: return "LOW_MEMORY";
1099 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1100 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001101 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001102 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1103 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001104 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001105 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001106 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001107 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001108 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001109 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001110 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001111 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001112 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001113 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001114 case TRIM_MEMORY: return "TRIM_MEMORY";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 }
1116 }
1117 return "(unknown)";
1118 }
1119 public void handleMessage(Message msg) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001120 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 switch (msg.what) {
1122 case LAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001123 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124
1125 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001126 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001127 handleLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 } break;
1129 case RELAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001130 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001131 handleRelaunchActivity(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 } break;
1133 case PAUSE_ACTIVITY:
1134 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001135 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 break;
1137 case PAUSE_ACTIVITY_FINISHING:
1138 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
1139 break;
1140 case STOP_ACTIVITY_SHOW:
1141 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
1142 break;
1143 case STOP_ACTIVITY_HIDE:
1144 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
1145 break;
1146 case SHOW_WINDOW:
1147 handleWindowVisibility((IBinder)msg.obj, true);
1148 break;
1149 case HIDE_WINDOW:
1150 handleWindowVisibility((IBinder)msg.obj, false);
1151 break;
1152 case RESUME_ACTIVITY:
1153 handleResumeActivity((IBinder)msg.obj, true,
1154 msg.arg1 != 0);
1155 break;
1156 case SEND_RESULT:
1157 handleSendResult((ResultData)msg.obj);
1158 break;
1159 case DESTROY_ACTIVITY:
1160 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1161 msg.arg2, false);
1162 break;
1163 case BIND_APPLICATION:
1164 AppBindData data = (AppBindData)msg.obj;
1165 handleBindApplication(data);
1166 break;
1167 case EXIT_APPLICATION:
1168 if (mInitialApplication != null) {
1169 mInitialApplication.onTerminate();
1170 }
1171 Looper.myLooper().quit();
1172 break;
1173 case NEW_INTENT:
1174 handleNewIntent((NewIntentData)msg.obj);
1175 break;
1176 case RECEIVER:
1177 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001178 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 break;
1180 case CREATE_SERVICE:
1181 handleCreateService((CreateServiceData)msg.obj);
1182 break;
1183 case BIND_SERVICE:
1184 handleBindService((BindServiceData)msg.obj);
1185 break;
1186 case UNBIND_SERVICE:
1187 handleUnbindService((BindServiceData)msg.obj);
1188 break;
1189 case SERVICE_ARGS:
1190 handleServiceArgs((ServiceArgsData)msg.obj);
1191 break;
1192 case STOP_SERVICE:
1193 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001194 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 break;
1196 case REQUEST_THUMBNAIL:
1197 handleRequestThumbnail((IBinder)msg.obj);
1198 break;
1199 case CONFIGURATION_CHANGED:
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001200 handleConfigurationChanged((Configuration)msg.obj, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 break;
1202 case CLEAN_UP_CONTEXT:
1203 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1204 cci.context.performFinalCleanup(cci.who, cci.what);
1205 break;
1206 case GC_WHEN_IDLE:
1207 scheduleGcIdler();
1208 break;
1209 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001210 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 break;
1212 case LOW_MEMORY:
1213 handleLowMemory();
1214 break;
1215 case ACTIVITY_CONFIGURATION_CHANGED:
1216 handleActivityConfigurationChanged((IBinder)msg.obj);
1217 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001218 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001219 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001220 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001221 case CREATE_BACKUP_AGENT:
1222 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
1223 break;
1224 case DESTROY_BACKUP_AGENT:
1225 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
1226 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001227 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001228 Process.killProcess(Process.myPid());
1229 break;
1230 case REMOVE_PROVIDER:
1231 completeRemoveProvider((IContentProvider)msg.obj);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001232 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001233 case ENABLE_JIT:
1234 ensureJitEnabled();
1235 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001236 case DISPATCH_PACKAGE_BROADCAST:
1237 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
1238 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001239 case SCHEDULE_CRASH:
1240 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001241 case DUMP_HEAP:
1242 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1243 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001244 case DUMP_ACTIVITY:
1245 handleDumpActivity((DumpComponentInfo)msg.obj);
1246 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001247 case SLEEPING:
1248 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
1249 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001250 case SET_CORE_SETTINGS:
1251 handleSetCoreSettings((Bundle) msg.obj);
1252 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001253 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1254 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001255 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001256 case TRIM_MEMORY:
1257 handleTrimMemory(msg.arg1);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001258 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07001260 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 }
Bob Leee5408332009-09-04 18:31:17 -07001262
Brian Carlstromed7e0072011-03-24 13:27:57 -07001263 private void maybeSnapshot() {
1264 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001265 // convert the *private* ActivityThread.PackageInfo to *public* known
1266 // android.content.pm.PackageInfo
1267 String packageName = mBoundApplication.info.mPackageName;
1268 android.content.pm.PackageInfo packageInfo = null;
1269 try {
1270 Context context = getSystemContext();
1271 if(context == null) {
1272 Log.e(TAG, "cannot get a valid context");
1273 return;
1274 }
1275 PackageManager pm = context.getPackageManager();
1276 if(pm == null) {
1277 Log.e(TAG, "cannot get a valid PackageManager");
1278 return;
1279 }
1280 packageInfo = pm.getPackageInfo(
1281 packageName, PackageManager.GET_ACTIVITIES);
1282 } catch (NameNotFoundException e) {
1283 Log.e(TAG, "cannot get package info for " + packageName, e);
1284 }
1285 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001286 }
1287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 }
1289
Romain Guy65b345f2011-07-27 18:51:50 -07001290 private class Idler implements MessageQueue.IdleHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001292 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001293 boolean stopProfiling = false;
1294 if (mBoundApplication.profileFd != null && mBoundApplication.autoStopProfiler) {
1295 stopProfiling = true;
1296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 if (a != null) {
1298 mNewActivities = null;
1299 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001300 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001302 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 TAG, "Reporting idle of " + a +
1304 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001305 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 if (a.activity != null && !a.activity.mFinished) {
1307 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001308 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001309 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001311 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 }
1313 }
1314 prev = a;
1315 a = a.nextIdle;
1316 prev.nextIdle = null;
1317 } while (a != null);
1318 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001319 if (stopProfiling) {
1320 mBoundApplication.stopProfiling();
1321 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001322 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 return false;
1324 }
1325 }
1326
1327 final class GcIdler implements MessageQueue.IdleHandler {
1328 public final boolean queueIdle() {
1329 doGcIfNeeded();
1330 return false;
1331 }
1332 }
1333
Romain Guy65b345f2011-07-27 18:51:50 -07001334 private static class ResourcesKey {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001335 final private String mResDir;
1336 final private float mScale;
1337 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001338
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001339 ResourcesKey(String resDir, float scale) {
1340 mResDir = resDir;
1341 mScale = scale;
1342 mHash = mResDir.hashCode() << 2 + (int) (mScale * 2);
1343 }
Bob Leee5408332009-09-04 18:31:17 -07001344
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001345 @Override
1346 public int hashCode() {
1347 return mHash;
1348 }
1349
1350 @Override
1351 public boolean equals(Object obj) {
1352 if (!(obj instanceof ResourcesKey)) {
1353 return false;
1354 }
1355 ResourcesKey peer = (ResourcesKey) obj;
1356 return mResDir.equals(peer.mResDir) && mScale == peer.mScale;
1357 }
1358 }
1359
Romain Guy65b345f2011-07-27 18:51:50 -07001360 public static ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001361 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363
Romain Guy65b345f2011-07-27 18:51:50 -07001364 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001365 ActivityThread am = currentActivityThread();
1366 return (am != null && am.mBoundApplication != null)
1367 ? am.mBoundApplication.processName : null;
1368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369
Romain Guy65b345f2011-07-27 18:51:50 -07001370 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001371 ActivityThread am = currentActivityThread();
1372 return am != null ? am.mInitialApplication : null;
1373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001375 public static IPackageManager getPackageManager() {
1376 if (sPackageManager != null) {
1377 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1378 return sPackageManager;
1379 }
1380 IBinder b = ServiceManager.getService("package");
1381 //Slog.v("PackageManager", "default service binder = " + b);
1382 sPackageManager = IPackageManager.Stub.asInterface(b);
1383 //Slog.v("PackageManager", "default service = " + sPackageManager);
1384 return sPackageManager;
1385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001387 DisplayMetrics getDisplayMetricsLocked(CompatibilityInfo ci, boolean forceUpdate) {
1388 DisplayMetrics dm = mDisplayMetrics.get(ci);
1389 if (dm != null && !forceUpdate) {
1390 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001391 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001392 if (dm == null) {
1393 dm = new DisplayMetrics();
1394 mDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001395 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001396 Display d = WindowManagerImpl.getDefault(ci).getDefaultDisplay();
1397 d.getMetrics(dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001398 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1399 // + metrics.heightPixels + " den=" + metrics.density
1400 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001401 return dm;
1402 }
1403
1404 static Configuration applyConfigCompat(Configuration config, CompatibilityInfo compat) {
1405 if (config == null) {
1406 return null;
1407 }
1408 if (compat != null && !compat.supportsScreen()) {
1409 config = new Configuration(config);
1410 compat.applyToConfiguration(config);
1411 }
1412 return config;
1413 }
1414
Romain Guy65b345f2011-07-27 18:51:50 -07001415 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001416 Configuration applyConfigCompatMainThread(Configuration config, CompatibilityInfo compat) {
1417 if (config == null) {
1418 return null;
1419 }
1420 if (compat != null && !compat.supportsScreen()) {
1421 mMainThreadConfig.setTo(config);
1422 config = mMainThreadConfig;
1423 compat.applyToConfiguration(config);
1424 }
1425 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001428 /**
1429 * Creates the top level Resources for applications with the given compatibility info.
1430 *
1431 * @param resDir the resource directory.
1432 * @param compInfo the compability info. It will use the default compatibility info when it's
1433 * null.
1434 */
1435 Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) {
1436 ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale);
1437 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001439 // Resources is app scale dependent.
1440 if (false) {
1441 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1442 + compInfo.applicationScale);
1443 }
1444 WeakReference<Resources> wr = mActiveResources.get(key);
1445 r = wr != null ? wr.get() : null;
1446 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1447 if (r != null && r.getAssets().isUpToDate()) {
1448 if (false) {
1449 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1450 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1451 }
1452 return r;
1453 }
1454 }
1455
1456 //if (r != null) {
1457 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1458 // + r + " " + resDir);
1459 //}
1460
1461 AssetManager assets = new AssetManager();
1462 if (assets.addAssetPath(resDir) == 0) {
1463 return null;
1464 }
1465
1466 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001467 DisplayMetrics metrics = getDisplayMetricsLocked(compInfo, false);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001468 r = new Resources(assets, metrics, getConfiguration(), compInfo);
1469 if (false) {
1470 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1471 + r.getConfiguration() + " appScale="
1472 + r.getCompatibilityInfo().applicationScale);
1473 }
1474
1475 synchronized (mPackages) {
1476 WeakReference<Resources> wr = mActiveResources.get(key);
1477 Resources existing = wr != null ? wr.get() : null;
1478 if (existing != null && existing.getAssets().isUpToDate()) {
1479 // Someone else already created the resources while we were
1480 // unlocked; go ahead and use theirs.
1481 r.getAssets().close();
1482 return existing;
1483 }
1484
1485 // XXX need to remove entries when weak references go away
1486 mActiveResources.put(key, new WeakReference<Resources>(r));
1487 return r;
1488 }
1489 }
1490
1491 /**
1492 * Creates the top level resources for the given package.
1493 */
1494 Resources getTopLevelResources(String resDir, LoadedApk pkgInfo) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001495 return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001496 }
1497
1498 final Handler getHandler() {
1499 return mH;
1500 }
1501
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001502 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1503 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001504 synchronized (mPackages) {
1505 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1507 ref = mPackages.get(packageName);
1508 } else {
1509 ref = mResourcePackages.get(packageName);
1510 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001511 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001512 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001513 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1514 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 if (packageInfo != null && (packageInfo.mResources == null
1516 || packageInfo.mResources.getAssets().isUpToDate())) {
1517 if (packageInfo.isSecurityViolation()
1518 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1519 throw new SecurityException(
1520 "Requesting code from " + packageName
1521 + " to be run in process "
1522 + mBoundApplication.processName
1523 + "/" + mBoundApplication.appInfo.uid);
1524 }
1525 return packageInfo;
1526 }
1527 }
1528
1529 ApplicationInfo ai = null;
1530 try {
1531 ai = getPackageManager().getApplicationInfo(packageName,
1532 PackageManager.GET_SHARED_LIBRARY_FILES);
1533 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001534 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 }
1536
1537 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001538 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 }
1540
1541 return null;
1542 }
1543
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001544 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1545 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1547 boolean securityViolation = includeCode && ai.uid != 0
1548 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
1549 ? ai.uid != mBoundApplication.appInfo.uid : true);
1550 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1551 |Context.CONTEXT_IGNORE_SECURITY))
1552 == Context.CONTEXT_INCLUDE_CODE) {
1553 if (securityViolation) {
1554 String msg = "Requesting code from " + ai.packageName
1555 + " (with uid " + ai.uid + ")";
1556 if (mBoundApplication != null) {
1557 msg = msg + " to be run in process "
1558 + mBoundApplication.processName + " (with uid "
1559 + mBoundApplication.appInfo.uid + ")";
1560 }
1561 throw new SecurityException(msg);
1562 }
1563 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001564 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 }
1566
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001567 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1568 CompatibilityInfo compatInfo) {
1569 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 }
1571
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001572 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1573 synchronized (mPackages) {
1574 WeakReference<LoadedApk> ref;
1575 if (includeCode) {
1576 ref = mPackages.get(packageName);
1577 } else {
1578 ref = mResourcePackages.get(packageName);
1579 }
1580 return ref != null ? ref.get() : null;
1581 }
1582 }
1583
Romain Guy65b345f2011-07-27 18:51:50 -07001584 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1586 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001587 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 if (includeCode) {
1589 ref = mPackages.get(aInfo.packageName);
1590 } else {
1591 ref = mResourcePackages.get(aInfo.packageName);
1592 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001593 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 if (packageInfo == null || (packageInfo.mResources != null
1595 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001596 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 : "Loading resource-only package ") + aInfo.packageName
1598 + " (in " + (mBoundApplication != null
1599 ? mBoundApplication.processName : null)
1600 + ")");
1601 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001602 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 securityViolation, includeCode &&
1604 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1605 if (includeCode) {
1606 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001607 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 } else {
1609 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001610 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 }
1612 }
1613 return packageInfo;
1614 }
1615 }
1616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 ActivityThread() {
1618 }
1619
1620 public ApplicationThread getApplicationThread()
1621 {
1622 return mAppThread;
1623 }
1624
1625 public Instrumentation getInstrumentation()
1626 {
1627 return mInstrumentation;
1628 }
1629
1630 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001631 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 }
1633
1634 public boolean isProfiling() {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001635 return mBoundApplication != null && mBoundApplication.profileFile != null
1636 && mBoundApplication.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 }
1638
1639 public String getProfileFilePath() {
1640 return mBoundApplication.profileFile;
1641 }
1642
1643 public Looper getLooper() {
1644 return mLooper;
1645 }
1646
1647 public Application getApplication() {
1648 return mInitialApplication;
1649 }
Bob Leee5408332009-09-04 18:31:17 -07001650
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001651 public String getProcessName() {
1652 return mBoundApplication.processName;
1653 }
Bob Leee5408332009-09-04 18:31:17 -07001654
Dianne Hackborn21556372010-02-04 16:34:40 -08001655 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 synchronized (this) {
1657 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001658 ContextImpl context =
1659 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001660 LoadedApk info = new LoadedApk(this, "android", context, null,
1661 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 context.init(info, null, this);
1663 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001664 getConfiguration(), getDisplayMetricsLocked(
1665 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001667 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 // + ": " + context.getResources().getConfiguration());
1669 }
1670 }
1671 return mSystemContext;
1672 }
1673
Mike Cleron432b7132009-09-24 15:28:29 -07001674 public void installSystemApplicationInfo(ApplicationInfo info) {
1675 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001676 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001677 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001678 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Mike Cleron432b7132009-09-24 15:28:29 -07001679 }
1680 }
1681
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001682 void ensureJitEnabled() {
1683 if (!mJitEnabled) {
1684 mJitEnabled = true;
1685 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1686 }
1687 }
1688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 void scheduleGcIdler() {
1690 if (!mGcIdlerScheduled) {
1691 mGcIdlerScheduled = true;
1692 Looper.myQueue().addIdleHandler(mGcIdler);
1693 }
1694 mH.removeMessages(H.GC_WHEN_IDLE);
1695 }
1696
1697 void unscheduleGcIdler() {
1698 if (mGcIdlerScheduled) {
1699 mGcIdlerScheduled = false;
1700 Looper.myQueue().removeIdleHandler(mGcIdler);
1701 }
1702 mH.removeMessages(H.GC_WHEN_IDLE);
1703 }
1704
1705 void doGcIfNeeded() {
1706 mGcIdlerScheduled = false;
1707 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001708 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 // + "m now=" + now);
1710 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001711 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 BinderInternal.forceGc("bg");
1713 }
1714 }
1715
Jeff Hamilton52d32032011-01-08 15:31:26 -06001716 public void registerOnActivityPausedListener(Activity activity,
1717 OnActivityPausedListener listener) {
1718 synchronized (mOnPauseListeners) {
1719 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1720 if (list == null) {
1721 list = new ArrayList<OnActivityPausedListener>();
1722 mOnPauseListeners.put(activity, list);
1723 }
1724 list.add(listener);
1725 }
1726 }
1727
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001728 public void unregisterOnActivityPausedListener(Activity activity,
1729 OnActivityPausedListener listener) {
1730 synchronized (mOnPauseListeners) {
1731 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1732 if (list != null) {
1733 list.remove(listener);
1734 }
1735 }
1736 }
1737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 public final ActivityInfo resolveActivityInfo(Intent intent) {
1739 ActivityInfo aInfo = intent.resolveActivityInfo(
1740 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1741 if (aInfo == null) {
1742 // Throw an exception.
1743 Instrumentation.checkStartActivityResult(
1744 IActivityManager.START_CLASS_NOT_FOUND, intent);
1745 }
1746 return aInfo;
1747 }
Bob Leee5408332009-09-04 18:31:17 -07001748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001751 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001752 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001754 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 r.intent = intent;
1756 r.state = state;
1757 r.parent = parent;
1758 r.embeddedID = id;
1759 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001760 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 if (localLOGV) {
1762 ComponentName compname = intent.getComponent();
1763 String name;
1764 if (compname != null) {
1765 name = compname.toShortString();
1766 } else {
1767 name = "(Intent " + intent + ").getComponent() returned null";
1768 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001769 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 + ", comp=" + name
1771 + ", token=" + token);
1772 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001773 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 }
1775
1776 public final Activity getActivity(IBinder token) {
1777 return mActivities.get(token).activity;
1778 }
1779
1780 public final void sendActivityResult(
1781 IBinder token, String id, int requestCode,
1782 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001783 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001784 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1786 list.add(new ResultInfo(id, requestCode, resultCode, data));
1787 mAppThread.scheduleSendResult(token, list);
1788 }
1789
1790 // if the thread hasn't started yet, we don't have the handler, so just
1791 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07001792 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 queueOrSendMessage(what, obj, 0, 0);
1794 }
1795
Romain Guy65b345f2011-07-27 18:51:50 -07001796 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 queueOrSendMessage(what, obj, arg1, 0);
1798 }
1799
Romain Guy65b345f2011-07-27 18:51:50 -07001800 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001802 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1804 + ": " + arg1 + " / " + obj);
1805 Message msg = Message.obtain();
1806 msg.what = what;
1807 msg.obj = obj;
1808 msg.arg1 = arg1;
1809 msg.arg2 = arg2;
1810 mH.sendMessage(msg);
1811 }
1812 }
1813
Dianne Hackborn21556372010-02-04 16:34:40 -08001814 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 String what) {
1816 ContextCleanupInfo cci = new ContextCleanupInfo();
1817 cci.context = context;
1818 cci.who = who;
1819 cci.what = what;
1820 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1821 }
1822
Romain Guy65b345f2011-07-27 18:51:50 -07001823 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1825
1826 ActivityInfo aInfo = r.activityInfo;
1827 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001828 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 Context.CONTEXT_INCLUDE_CODE);
1830 }
Bob Leee5408332009-09-04 18:31:17 -07001831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 ComponentName component = r.intent.getComponent();
1833 if (component == null) {
1834 component = r.intent.resolveActivity(
1835 mInitialApplication.getPackageManager());
1836 r.intent.setComponent(component);
1837 }
1838
1839 if (r.activityInfo.targetActivity != null) {
1840 component = new ComponentName(r.activityInfo.packageName,
1841 r.activityInfo.targetActivity);
1842 }
1843
1844 Activity activity = null;
1845 try {
1846 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
1847 activity = mInstrumentation.newActivity(
1848 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08001849 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 r.intent.setExtrasClassLoader(cl);
1851 if (r.state != null) {
1852 r.state.setClassLoader(cl);
1853 }
1854 } catch (Exception e) {
1855 if (!mInstrumentation.onException(activity, e)) {
1856 throw new RuntimeException(
1857 "Unable to instantiate activity " + component
1858 + ": " + e.toString(), e);
1859 }
1860 }
1861
1862 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001863 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001864
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001865 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
1866 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 TAG, r + ": app=" + app
1868 + ", appName=" + app.getPackageName()
1869 + ", pkg=" + r.packageInfo.getPackageName()
1870 + ", comp=" + r.intent.getComponent().toShortString()
1871 + ", dir=" + r.packageInfo.getAppDir());
1872
1873 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001874 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 appContext.init(r.packageInfo, r.token, this);
1876 appContext.setOuterContext(activity);
1877 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001878 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001879 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07001880 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001881 activity.attach(appContext, this, getInstrumentation(), r.token,
1882 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001883 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07001884
Christopher Tateb70f3df2009-04-07 16:07:59 -07001885 if (customIntent != null) {
1886 activity.mIntent = customIntent;
1887 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001888 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 activity.mStartedActivity = false;
1890 int theme = r.activityInfo.getThemeResource();
1891 if (theme != 0) {
1892 activity.setTheme(theme);
1893 }
1894
1895 activity.mCalled = false;
1896 mInstrumentation.callActivityOnCreate(activity, r.state);
1897 if (!activity.mCalled) {
1898 throw new SuperNotCalledException(
1899 "Activity " + r.intent.getComponent().toShortString() +
1900 " did not call through to super.onCreate()");
1901 }
1902 r.activity = activity;
1903 r.stopped = true;
1904 if (!r.activity.mFinished) {
1905 activity.performStart();
1906 r.stopped = false;
1907 }
1908 if (!r.activity.mFinished) {
1909 if (r.state != null) {
1910 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
1911 }
1912 }
1913 if (!r.activity.mFinished) {
1914 activity.mCalled = false;
1915 mInstrumentation.callActivityOnPostCreate(activity, r.state);
1916 if (!activity.mCalled) {
1917 throw new SuperNotCalledException(
1918 "Activity " + r.intent.getComponent().toShortString() +
1919 " did not call through to super.onPostCreate()");
1920 }
1921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 }
1923 r.paused = true;
1924
1925 mActivities.put(r.token, r);
1926
1927 } catch (SuperNotCalledException e) {
1928 throw e;
1929
1930 } catch (Exception e) {
1931 if (!mInstrumentation.onException(activity, e)) {
1932 throw new RuntimeException(
1933 "Unable to start activity " + component
1934 + ": " + e.toString(), e);
1935 }
1936 }
1937
1938 return activity;
1939 }
1940
Romain Guy65b345f2011-07-27 18:51:50 -07001941 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 // If we are getting ready to gc after going to the background, well
1943 // we are back active so skip it.
1944 unscheduleGcIdler();
1945
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001946 Slog.i(TAG, "Launch: profileFd=" + r.profileFile + " stop=" + r.autoStopProfiler);
1947 if (r.profileFd != null) {
1948 mBoundApplication.setProfiler(r.profileFile, r.profileFd);
1949 mBoundApplication.startProfiling();
1950 mBoundApplication.autoStopProfiler = r.autoStopProfiler;
1951 }
1952
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001953 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001955 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956
1957 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08001958 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001959 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 handleResumeActivity(r.token, false, r.isForward);
1961
1962 if (!r.activity.mFinished && r.startsNotResumed) {
1963 // The activity manager actually wants this one to start out
1964 // paused, because it needs to be visible but isn't in the
1965 // foreground. We accomplish this by going through the
1966 // normal startup (because activities expect to go through
1967 // onResume() the first time they run, before their window
1968 // is displayed), and then pausing it. However, in this case
1969 // we do -not- need to do the full pause cycle (of freezing
1970 // and such) because the activity manager assumes it can just
1971 // retain the current state it has.
1972 try {
1973 r.activity.mCalled = false;
1974 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001975 // We need to keep around the original state, in case
1976 // we need to be created again.
1977 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 if (!r.activity.mCalled) {
1979 throw new SuperNotCalledException(
1980 "Activity " + r.intent.getComponent().toShortString() +
1981 " did not call through to super.onPause()");
1982 }
1983
1984 } catch (SuperNotCalledException e) {
1985 throw e;
1986
1987 } catch (Exception e) {
1988 if (!mInstrumentation.onException(r.activity, e)) {
1989 throw new RuntimeException(
1990 "Unable to pause activity "
1991 + r.intent.getComponent().toShortString()
1992 + ": " + e.toString(), e);
1993 }
1994 }
1995 r.paused = true;
1996 }
1997 } else {
1998 // If there was an error, for any reason, tell the activity
1999 // manager to stop us.
2000 try {
2001 ActivityManagerNative.getDefault()
2002 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2003 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002004 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 }
2006 }
2007 }
2008
Romain Guy65b345f2011-07-27 18:51:50 -07002009 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 List<Intent> intents) {
2011 final int N = intents.size();
2012 for (int i=0; i<N; i++) {
2013 Intent intent = intents.get(i);
2014 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002015 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2017 }
2018 }
2019
2020 public final void performNewIntents(IBinder token,
2021 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002022 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 if (r != null) {
2024 final boolean resumed = !r.paused;
2025 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002026 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 mInstrumentation.callActivityOnPause(r.activity);
2028 }
2029 deliverNewIntents(r, intents);
2030 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002031 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002032 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 }
2034 }
2035 }
Bob Leee5408332009-09-04 18:31:17 -07002036
Romain Guy65b345f2011-07-27 18:51:50 -07002037 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 performNewIntents(data.token, data.intents);
2039 }
2040
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002041 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2042
2043 /**
2044 * Return the Intent that's currently being handled by a
2045 * BroadcastReceiver on this thread, or null if none.
2046 * @hide
2047 */
2048 public static Intent getIntentBeingBroadcast() {
2049 return sCurrentBroadcastIntent.get();
2050 }
2051
Romain Guy65b345f2011-07-27 18:51:50 -07002052 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 // If we are getting ready to gc after going to the background, well
2054 // we are back active so skip it.
2055 unscheduleGcIdler();
2056
2057 String component = data.intent.getComponent().getClassName();
2058
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002059 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002060 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061
2062 IActivityManager mgr = ActivityManagerNative.getDefault();
2063
Romain Guy65b345f2011-07-27 18:51:50 -07002064 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 try {
2066 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2067 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002068 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2070 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002071 if (DEBUG_BROADCAST) Slog.i(TAG,
2072 "Finishing failed broadcast to " + data.intent.getComponent());
2073 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 throw new RuntimeException(
2075 "Unable to instantiate receiver " + component
2076 + ": " + e.toString(), e);
2077 }
2078
2079 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002080 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002081
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002082 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 TAG, "Performing receive of " + data.intent
2084 + ": app=" + app
2085 + ", appName=" + app.getPackageName()
2086 + ", pkg=" + packageInfo.getPackageName()
2087 + ", comp=" + data.intent.getComponent().toShortString()
2088 + ", dir=" + packageInfo.getAppDir());
2089
Dianne Hackborn21556372010-02-04 16:34:40 -08002090 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002091 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002092 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 receiver.onReceive(context.getReceiverRestrictedContext(),
2094 data.intent);
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 if (!mInstrumentation.onException(receiver, e)) {
2100 throw new RuntimeException(
2101 "Unable to start receiver " + component
2102 + ": " + e.toString(), e);
2103 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002104 } finally {
2105 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 }
2107
Dianne Hackborne829fef2010-10-26 17:44:01 -07002108 if (receiver.getPendingResult() != null) {
2109 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 }
2111 }
2112
Christopher Tate181fafa2009-05-14 11:12:14 -07002113 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002114 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002115 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002116
2117 // no longer idle; we have backup work to do
2118 unscheduleGcIdler();
2119
2120 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002121 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002122 String packageName = packageInfo.mPackageName;
2123 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002124 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002125 + " already exists");
2126 return;
2127 }
Bob Leee5408332009-09-04 18:31:17 -07002128
Christopher Tate181fafa2009-05-14 11:12:14 -07002129 BackupAgent agent = null;
2130 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002131
Christopher Tate79ec80d2011-06-24 14:58:49 -07002132 // full backup operation but no app-supplied agent? use the default implementation
2133 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2134 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002135 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002136 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002137
Christopher Tate181fafa2009-05-14 11:12:14 -07002138 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002139 IBinder binder = null;
2140 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002141 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2142
Christopher Tated1475e02009-07-09 15:36:17 -07002143 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002144 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002145
2146 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002147 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002148 context.init(packageInfo, null, this);
2149 context.setOuterContext(agent);
2150 agent.attach(context);
2151
2152 agent.onCreate();
2153 binder = agent.onBind();
2154 mBackupAgents.put(packageName, agent);
2155 } catch (Exception e) {
2156 // If this is during restore, fail silently; otherwise go
2157 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002158 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002159 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2160 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002161 throw e;
2162 }
2163 // falling through with 'binder' still null
2164 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002165
2166 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002167 try {
2168 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2169 } catch (RemoteException e) {
2170 // nothing to do.
2171 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002172 } catch (Exception e) {
2173 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002174 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002175 }
2176 }
2177
2178 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002179 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002180 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002181
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002182 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002183 String packageName = packageInfo.mPackageName;
2184 BackupAgent agent = mBackupAgents.get(packageName);
2185 if (agent != null) {
2186 try {
2187 agent.onDestroy();
2188 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002189 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002190 e.printStackTrace();
2191 }
2192 mBackupAgents.remove(packageName);
2193 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002194 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002195 }
2196 }
2197
Romain Guy65b345f2011-07-27 18:51:50 -07002198 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 // If we are getting ready to gc after going to the background, well
2200 // we are back active so skip it.
2201 unscheduleGcIdler();
2202
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002203 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002204 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 Service service = null;
2206 try {
2207 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2208 service = (Service) cl.loadClass(data.info.name).newInstance();
2209 } catch (Exception e) {
2210 if (!mInstrumentation.onException(service, e)) {
2211 throw new RuntimeException(
2212 "Unable to instantiate service " + data.info.name
2213 + ": " + e.toString(), e);
2214 }
2215 }
2216
2217 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002218 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219
Dianne Hackborn21556372010-02-04 16:34:40 -08002220 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 context.init(packageInfo, null, this);
2222
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002223 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 context.setOuterContext(service);
2225 service.attach(context, this, data.info.name, data.token, app,
2226 ActivityManagerNative.getDefault());
2227 service.onCreate();
2228 mServices.put(data.token, service);
2229 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002230 ActivityManagerNative.getDefault().serviceDoneExecuting(
2231 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 } catch (RemoteException e) {
2233 // nothing to do.
2234 }
2235 } catch (Exception e) {
2236 if (!mInstrumentation.onException(service, e)) {
2237 throw new RuntimeException(
2238 "Unable to create service " + data.info.name
2239 + ": " + e.toString(), e);
2240 }
2241 }
2242 }
2243
Romain Guy65b345f2011-07-27 18:51:50 -07002244 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 Service s = mServices.get(data.token);
2246 if (s != null) {
2247 try {
2248 data.intent.setExtrasClassLoader(s.getClassLoader());
2249 try {
2250 if (!data.rebind) {
2251 IBinder binder = s.onBind(data.intent);
2252 ActivityManagerNative.getDefault().publishService(
2253 data.token, data.intent, binder);
2254 } else {
2255 s.onRebind(data.intent);
2256 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002257 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002259 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 } catch (RemoteException ex) {
2261 }
2262 } catch (Exception e) {
2263 if (!mInstrumentation.onException(s, e)) {
2264 throw new RuntimeException(
2265 "Unable to bind to service " + s
2266 + " with " + data.intent + ": " + e.toString(), e);
2267 }
2268 }
2269 }
2270 }
2271
Romain Guy65b345f2011-07-27 18:51:50 -07002272 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 Service s = mServices.get(data.token);
2274 if (s != null) {
2275 try {
2276 data.intent.setExtrasClassLoader(s.getClassLoader());
2277 boolean doRebind = s.onUnbind(data.intent);
2278 try {
2279 if (doRebind) {
2280 ActivityManagerNative.getDefault().unbindFinished(
2281 data.token, data.intent, doRebind);
2282 } else {
2283 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002284 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 }
2286 } catch (RemoteException ex) {
2287 }
2288 } catch (Exception e) {
2289 if (!mInstrumentation.onException(s, e)) {
2290 throw new RuntimeException(
2291 "Unable to unbind to service " + s
2292 + " with " + data.intent + ": " + e.toString(), e);
2293 }
2294 }
2295 }
2296 }
2297
Dianne Hackborn625ac272010-09-17 18:29:22 -07002298 private void handleDumpService(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002299 Service s = mServices.get(info.token);
2300 if (s != null) {
2301 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2302 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2303 pw.flush();
2304 try {
2305 info.fd.close();
2306 } catch (IOException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 }
2308 }
2309 }
2310
Dianne Hackborn625ac272010-09-17 18:29:22 -07002311 private void handleDumpActivity(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002312 ActivityClientRecord r = mActivities.get(info.token);
2313 if (r != null && r.activity != null) {
2314 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2315 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2316 pw.flush();
2317 try {
2318 info.fd.close();
2319 } catch (IOException e) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07002320 }
2321 }
2322 }
2323
Romain Guy65b345f2011-07-27 18:51:50 -07002324 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 Service s = mServices.get(data.token);
2326 if (s != null) {
2327 try {
2328 if (data.args != null) {
2329 data.args.setExtrasClassLoader(s.getClassLoader());
2330 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002331 int res;
2332 if (!data.taskRemoved) {
2333 res = s.onStartCommand(data.args, data.flags, data.startId);
2334 } else {
2335 s.onTaskRemoved(data.args);
2336 res = Service.START_TASK_REMOVED_COMPLETE;
2337 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002338
2339 QueuedWork.waitToFinish();
2340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002342 ActivityManagerNative.getDefault().serviceDoneExecuting(
2343 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 } catch (RemoteException e) {
2345 // nothing to do.
2346 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002347 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 } catch (Exception e) {
2349 if (!mInstrumentation.onException(s, e)) {
2350 throw new RuntimeException(
2351 "Unable to start service " + s
2352 + " with " + data.args + ": " + e.toString(), e);
2353 }
2354 }
2355 }
2356 }
2357
Romain Guy65b345f2011-07-27 18:51:50 -07002358 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 Service s = mServices.remove(token);
2360 if (s != null) {
2361 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002362 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 s.onDestroy();
2364 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002365 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002367 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002369
2370 QueuedWork.waitToFinish();
2371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002373 ActivityManagerNative.getDefault().serviceDoneExecuting(
2374 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 } catch (RemoteException e) {
2376 // nothing to do.
2377 }
2378 } catch (Exception e) {
2379 if (!mInstrumentation.onException(s, e)) {
2380 throw new RuntimeException(
2381 "Unable to stop service " + s
2382 + ": " + e.toString(), e);
2383 }
2384 }
2385 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002386 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 }
2388
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002389 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002391 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002392 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 + " finished=" + r.activity.mFinished);
2394 if (r != null && !r.activity.mFinished) {
2395 if (clearHide) {
2396 r.hideForNow = false;
2397 r.activity.mStartedActivity = false;
2398 }
2399 try {
2400 if (r.pendingIntents != null) {
2401 deliverNewIntents(r, r.pendingIntents);
2402 r.pendingIntents = null;
2403 }
2404 if (r.pendingResults != null) {
2405 deliverResults(r, r.pendingResults);
2406 r.pendingResults = null;
2407 }
2408 r.activity.performResume();
2409
Bob Leee5408332009-09-04 18:31:17 -07002410 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 r.paused = false;
2414 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 r.state = null;
2416 } catch (Exception e) {
2417 if (!mInstrumentation.onException(r.activity, e)) {
2418 throw new RuntimeException(
2419 "Unable to resume activity "
2420 + r.intent.getComponent().toShortString()
2421 + ": " + e.toString(), e);
2422 }
2423 }
2424 }
2425 return r;
2426 }
2427
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002428 final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
2429 if (r.mPendingRemoveWindow != null) {
2430 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2431 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2432 if (wtoken != null) {
2433 WindowManagerImpl.getDefault().closeAll(wtoken,
2434 r.activity.getClass().getName(), "Activity");
2435 }
2436 }
2437 r.mPendingRemoveWindow = null;
2438 r.mPendingRemoveWindowManager = null;
2439 }
2440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2442 // If we are getting ready to gc after going to the background, well
2443 // we are back active so skip it.
2444 unscheduleGcIdler();
2445
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002446 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447
2448 if (r != null) {
2449 final Activity a = r.activity;
2450
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002451 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 TAG, "Resume " + r + " started activity: " +
2453 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2454 + ", finished: " + a.mFinished);
2455
2456 final int forwardBit = isForward ?
2457 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 // If the window hasn't yet been added to the window manager,
2460 // and this guy didn't finish itself or start another activity,
2461 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002462 boolean willBeVisible = !a.mStartedActivity;
2463 if (!willBeVisible) {
2464 try {
2465 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2466 a.getActivityToken());
2467 } catch (RemoteException e) {
2468 }
2469 }
2470 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 r.window = r.activity.getWindow();
2472 View decor = r.window.getDecorView();
2473 decor.setVisibility(View.INVISIBLE);
2474 ViewManager wm = a.getWindowManager();
2475 WindowManager.LayoutParams l = r.window.getAttributes();
2476 a.mDecor = decor;
2477 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2478 l.softInputMode |= forwardBit;
2479 if (a.mVisibleFromClient) {
2480 a.mWindowAdded = true;
2481 wm.addView(decor, l);
2482 }
2483
2484 // If the window has already been added, but during resume
2485 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002486 // window visible.
2487 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002488 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 TAG, "Launch " + r + " mStartedActivity set");
2490 r.hideForNow = true;
2491 }
2492
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002493 // Get rid of anything left hanging around.
2494 cleanUpPendingRemoveWindows(r);
2495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 // The window is now visible if it has been added, we are not
2497 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002498 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002499 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002501 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002502 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 performConfigurationChanged(r.activity, r.newConfig);
2504 r.newConfig = null;
2505 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002506 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 + isForward);
2508 WindowManager.LayoutParams l = r.window.getAttributes();
2509 if ((l.softInputMode
2510 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2511 != forwardBit) {
2512 l.softInputMode = (l.softInputMode
2513 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2514 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002515 if (r.activity.mVisibleFromClient) {
2516 ViewManager wm = a.getWindowManager();
2517 View decor = r.window.getDecorView();
2518 wm.updateViewLayout(decor, l);
2519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 }
2521 r.activity.mVisibleFromServer = true;
2522 mNumVisibleActivities++;
2523 if (r.activity.mVisibleFromClient) {
2524 r.activity.makeVisible();
2525 }
2526 }
2527
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002528 if (!r.onlyLocalRequest) {
2529 r.nextIdle = mNewActivities;
2530 mNewActivities = r;
2531 if (localLOGV) Slog.v(
2532 TAG, "Scheduling idle handler for " + r);
2533 Looper.myQueue().addIdleHandler(new Idler());
2534 }
2535 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536
2537 } else {
2538 // If an exception was thrown when trying to resume, then
2539 // just end this activity.
2540 try {
2541 ActivityManagerNative.getDefault()
2542 .finishActivity(token, Activity.RESULT_CANCELED, null);
2543 } catch (RemoteException ex) {
2544 }
2545 }
2546 }
2547
2548 private int mThumbnailWidth = -1;
2549 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002550 private Bitmap mAvailThumbnailBitmap = null;
2551 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552
Romain Guy65b345f2011-07-27 18:51:50 -07002553 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002554 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002556 if (thumbnail == null) {
2557 int w = mThumbnailWidth;
2558 int h;
2559 if (w < 0) {
2560 Resources res = r.activity.getResources();
2561 mThumbnailHeight = h =
2562 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002564 mThumbnailWidth = w =
2565 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2566 } else {
2567 h = mThumbnailHeight;
2568 }
2569
2570 // On platforms where we don't want thumbnails, set dims to (0,0)
2571 if ((w > 0) && (h > 0)) {
2572 thumbnail = Bitmap.createBitmap(w, h, THUMBNAIL_FORMAT);
2573 thumbnail.eraseColor(0);
2574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 }
2576
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002577 if (thumbnail != null) {
2578 Canvas cv = mThumbnailCanvas;
2579 if (cv == null) {
2580 mThumbnailCanvas = cv = new Canvas();
2581 }
2582
2583 cv.setBitmap(thumbnail);
2584 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2585 mAvailThumbnailBitmap = thumbnail;
2586 thumbnail = null;
2587 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002588 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 } catch (Exception e) {
2592 if (!mInstrumentation.onException(r.activity, e)) {
2593 throw new RuntimeException(
2594 "Unable to create thumbnail of "
2595 + r.intent.getComponent().toShortString()
2596 + ": " + e.toString(), e);
2597 }
2598 thumbnail = null;
2599 }
2600
2601 return thumbnail;
2602 }
2603
Romain Guy65b345f2011-07-27 18:51:50 -07002604 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002606 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002608 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 if (userLeaving) {
2610 performUserLeavingActivity(r);
2611 }
Bob Leee5408332009-09-04 18:31:17 -07002612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002614 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002616 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002617 if (r.isPreHoneycomb()) {
2618 QueuedWork.waitToFinish();
2619 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 // Tell the activity manager we have paused.
2622 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002623 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 } catch (RemoteException ex) {
2625 }
2626 }
2627 }
2628
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002629 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 mInstrumentation.callActivityOnUserLeaving(r.activity);
2631 }
2632
2633 final Bundle performPauseActivity(IBinder token, boolean finished,
2634 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002635 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 return r != null ? performPauseActivity(r, finished, saveState) : null;
2637 }
2638
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002639 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 boolean saveState) {
2641 if (r.paused) {
2642 if (r.activity.mFinished) {
2643 // If we are finishing, we won't call onResume() in certain cases.
2644 // So here we likewise don't want to call onPause() if the activity
2645 // isn't resumed.
2646 return null;
2647 }
2648 RuntimeException e = new RuntimeException(
2649 "Performing pause of activity that is not resumed: "
2650 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002651 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 }
2653 Bundle state = null;
2654 if (finished) {
2655 r.activity.mFinished = true;
2656 }
2657 try {
2658 // Next have the activity save its current state and managed dialogs...
2659 if (!r.activity.mFinished && saveState) {
2660 state = new Bundle();
2661 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2662 r.state = state;
2663 }
2664 // Now we are idle.
2665 r.activity.mCalled = false;
2666 mInstrumentation.callActivityOnPause(r.activity);
2667 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2668 if (!r.activity.mCalled) {
2669 throw new SuperNotCalledException(
2670 "Activity " + r.intent.getComponent().toShortString() +
2671 " did not call through to super.onPause()");
2672 }
2673
2674 } catch (SuperNotCalledException e) {
2675 throw e;
2676
2677 } catch (Exception e) {
2678 if (!mInstrumentation.onException(r.activity, e)) {
2679 throw new RuntimeException(
2680 "Unable to pause activity "
2681 + r.intent.getComponent().toShortString()
2682 + ": " + e.toString(), e);
2683 }
2684 }
2685 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002686
2687 // Notify any outstanding on paused listeners
2688 ArrayList<OnActivityPausedListener> listeners;
2689 synchronized (mOnPauseListeners) {
2690 listeners = mOnPauseListeners.remove(r.activity);
2691 }
2692 int size = (listeners != null ? listeners.size() : 0);
2693 for (int i = 0; i < size; i++) {
2694 listeners.get(i).onPaused(r.activity);
2695 }
2696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 return state;
2698 }
2699
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002700 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002701 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002702 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 }
2704
2705 private static class StopInfo {
2706 Bitmap thumbnail;
2707 CharSequence description;
2708 }
2709
Romain Guy65b345f2011-07-27 18:51:50 -07002710 private class ProviderRefCount {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 public int count;
2712 ProviderRefCount(int pCount) {
2713 count = pCount;
2714 }
2715 }
2716
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002717 /**
2718 * Core implementation of stopping an activity. Note this is a little
2719 * tricky because the server's meaning of stop is slightly different
2720 * than our client -- for the server, stop means to save state and give
2721 * it the result when it is done, but the window may still be visible.
2722 * For the client, we want to call onStop()/onStart() to indicate when
2723 * the activity's UI visibillity changes.
2724 */
Romain Guy65b345f2011-07-27 18:51:50 -07002725 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002726 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002727 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002728 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 if (r != null) {
2730 if (!keepShown && r.stopped) {
2731 if (r.activity.mFinished) {
2732 // If we are finishing, we won't call onResume() in certain
2733 // cases. So here we likewise don't want to call onStop()
2734 // if the activity isn't resumed.
2735 return;
2736 }
2737 RuntimeException e = new RuntimeException(
2738 "Performing stop of activity that is not resumed: "
2739 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002740 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 }
2742
2743 if (info != null) {
2744 try {
2745 // First create a thumbnail for the activity...
Jim Miller0b2a6d02010-07-13 18:01:29 -07002746 info.thumbnail = createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 info.description = r.activity.onCreateDescription();
2748 } catch (Exception e) {
2749 if (!mInstrumentation.onException(r.activity, e)) {
2750 throw new RuntimeException(
2751 "Unable to save state of activity "
2752 + r.intent.getComponent().toShortString()
2753 + ": " + e.toString(), e);
2754 }
2755 }
2756 }
2757
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002758 // Next have the activity save its current state and managed dialogs...
2759 if (!r.activity.mFinished && saveState) {
2760 if (r.state == null) {
2761 state = new Bundle();
2762 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2763 r.state = state;
2764 } else {
2765 state = r.state;
2766 }
2767 }
2768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 if (!keepShown) {
2770 try {
2771 // Now we are idle.
2772 r.activity.performStop();
2773 } catch (Exception e) {
2774 if (!mInstrumentation.onException(r.activity, e)) {
2775 throw new RuntimeException(
2776 "Unable to stop activity "
2777 + r.intent.getComponent().toShortString()
2778 + ": " + e.toString(), e);
2779 }
2780 }
2781 r.stopped = true;
2782 }
2783
2784 r.paused = true;
2785 }
2786 }
2787
Romain Guy65b345f2011-07-27 18:51:50 -07002788 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 View v = r.activity.mDecor;
2790 if (v != null) {
2791 if (show) {
2792 if (!r.activity.mVisibleFromServer) {
2793 r.activity.mVisibleFromServer = true;
2794 mNumVisibleActivities++;
2795 if (r.activity.mVisibleFromClient) {
2796 r.activity.makeVisible();
2797 }
2798 }
2799 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002800 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002801 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 performConfigurationChanged(r.activity, r.newConfig);
2803 r.newConfig = null;
2804 }
2805 } else {
2806 if (r.activity.mVisibleFromServer) {
2807 r.activity.mVisibleFromServer = false;
2808 mNumVisibleActivities--;
2809 v.setVisibility(View.INVISIBLE);
2810 }
2811 }
2812 }
2813 }
2814
Romain Guy65b345f2011-07-27 18:51:50 -07002815 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002816 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 r.activity.mConfigChangeFlags |= configChanges;
2818
2819 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002820 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002822 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 TAG, "Finishing stop of " + r + ": show=" + show
2824 + " win=" + r.window);
2825
2826 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07002827
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002828 // Make sure any pending writes are now committed.
2829 if (!r.isPreHoneycomb()) {
2830 QueuedWork.waitToFinish();
2831 }
2832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 // Tell activity manager we have been stopped.
2834 try {
2835 ActivityManagerNative.getDefault().activityStopped(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002836 r.token, r.state, info.thumbnail, info.description);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 } catch (RemoteException ex) {
2838 }
2839 }
2840
2841 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002842 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 if (r.stopped) {
2844 r.activity.performRestart();
2845 r.stopped = false;
2846 }
2847 }
2848
Romain Guy65b345f2011-07-27 18:51:50 -07002849 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002850 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08002851
2852 if (r == null) {
2853 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
2854 return;
2855 }
2856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002858 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 } else if (show && r.stopped) {
2860 // If we are getting ready to gc after going to the background, well
2861 // we are back active so skip it.
2862 unscheduleGcIdler();
2863
2864 r.activity.performRestart();
2865 r.stopped = false;
2866 }
2867 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07002868 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 TAG, "Handle window " + r + " visibility: " + show);
2870 updateVisibility(r, show);
2871 }
2872 }
2873
Romain Guy65b345f2011-07-27 18:51:50 -07002874 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002875 ActivityClientRecord r = mActivities.get(token);
2876
2877 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002878 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002879 return;
2880 }
2881
2882 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002883 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002884 try {
2885 // Now we are idle.
2886 r.activity.performStop();
2887 } catch (Exception e) {
2888 if (!mInstrumentation.onException(r.activity, e)) {
2889 throw new RuntimeException(
2890 "Unable to stop activity "
2891 + r.intent.getComponent().toShortString()
2892 + ": " + e.toString(), e);
2893 }
2894 }
2895 r.stopped = true;
2896 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002897
2898 // Make sure any pending writes are now committed.
2899 if (!r.isPreHoneycomb()) {
2900 QueuedWork.waitToFinish();
2901 }
2902
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002903 // Tell activity manager we slept.
2904 try {
2905 ActivityManagerNative.getDefault().activitySlept(r.token);
2906 } catch (RemoteException ex) {
2907 }
2908 } else {
2909 if (r.stopped && r.activity.mVisibleFromServer) {
2910 r.activity.performRestart();
2911 r.stopped = false;
2912 }
2913 }
2914 }
2915
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002916 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08002917 synchronized (mPackages) {
2918 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002919 }
2920 }
2921
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002922 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
2923 LoadedApk apk = peekPackageInfo(data.pkg, false);
2924 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002925 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002926 }
2927 apk = peekPackageInfo(data.pkg, true);
2928 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002929 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002930 }
2931 handleConfigurationChanged(mConfiguration, data.info);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002932 WindowManagerImpl.getDefault().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002933 }
2934
Romain Guy65b345f2011-07-27 18:51:50 -07002935 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 final int N = results.size();
2937 for (int i=0; i<N; i++) {
2938 ResultInfo ri = results.get(i);
2939 try {
2940 if (ri.mData != null) {
2941 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
2942 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002943 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07002944 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 r.activity.dispatchActivityResult(ri.mResultWho,
2946 ri.mRequestCode, ri.mResultCode, ri.mData);
2947 } catch (Exception e) {
2948 if (!mInstrumentation.onException(r.activity, e)) {
2949 throw new RuntimeException(
2950 "Failure delivering result " + ri + " to activity "
2951 + r.intent.getComponent().toShortString()
2952 + ": " + e.toString(), e);
2953 }
2954 }
2955 }
2956 }
2957
Romain Guy65b345f2011-07-27 18:51:50 -07002958 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002959 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002960 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 if (r != null) {
2962 final boolean resumed = !r.paused;
2963 if (!r.activity.mFinished && r.activity.mDecor != null
2964 && r.hideForNow && resumed) {
2965 // We had hidden the activity because it started another
2966 // one... we have gotten a result back and we are not
2967 // paused, so make sure our window is visible.
2968 updateVisibility(r, true);
2969 }
2970 if (resumed) {
2971 try {
2972 // Now we are idle.
2973 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002974 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 mInstrumentation.callActivityOnPause(r.activity);
2976 if (!r.activity.mCalled) {
2977 throw new SuperNotCalledException(
2978 "Activity " + r.intent.getComponent().toShortString()
2979 + " did not call through to super.onPause()");
2980 }
2981 } catch (SuperNotCalledException e) {
2982 throw e;
2983 } catch (Exception e) {
2984 if (!mInstrumentation.onException(r.activity, e)) {
2985 throw new RuntimeException(
2986 "Unable to pause activity "
2987 + r.intent.getComponent().toShortString()
2988 + ": " + e.toString(), e);
2989 }
2990 }
2991 }
2992 deliverResults(r, res.results);
2993 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002994 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002995 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 }
2997 }
2998 }
2999
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003000 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 return performDestroyActivity(token, finishing, 0, false);
3002 }
3003
Romain Guy65b345f2011-07-27 18:51:50 -07003004 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003006 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003007 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003008 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003010 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 r.activity.mConfigChangeFlags |= configChanges;
3012 if (finishing) {
3013 r.activity.mFinished = true;
3014 }
3015 if (!r.paused) {
3016 try {
3017 r.activity.mCalled = false;
3018 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07003019 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 r.activity.getComponentName().getClassName());
3021 if (!r.activity.mCalled) {
3022 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003023 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 + " did not call through to super.onPause()");
3025 }
3026 } catch (SuperNotCalledException e) {
3027 throw e;
3028 } catch (Exception e) {
3029 if (!mInstrumentation.onException(r.activity, e)) {
3030 throw new RuntimeException(
3031 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003032 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 + ": " + e.toString(), e);
3034 }
3035 }
3036 r.paused = true;
3037 }
3038 if (!r.stopped) {
3039 try {
3040 r.activity.performStop();
3041 } catch (SuperNotCalledException e) {
3042 throw e;
3043 } catch (Exception e) {
3044 if (!mInstrumentation.onException(r.activity, e)) {
3045 throw new RuntimeException(
3046 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003047 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 + ": " + e.toString(), e);
3049 }
3050 }
3051 r.stopped = true;
3052 }
3053 if (getNonConfigInstance) {
3054 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003055 r.lastNonConfigurationInstances
3056 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 } catch (Exception e) {
3058 if (!mInstrumentation.onException(r.activity, e)) {
3059 throw new RuntimeException(
3060 "Unable to retain activity "
3061 + r.intent.getComponent().toShortString()
3062 + ": " + e.toString(), e);
3063 }
3064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 }
3066 try {
3067 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003068 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 if (!r.activity.mCalled) {
3070 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003071 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 " did not call through to super.onDestroy()");
3073 }
3074 if (r.window != null) {
3075 r.window.closeAllPanels();
3076 }
3077 } catch (SuperNotCalledException e) {
3078 throw e;
3079 } catch (Exception e) {
3080 if (!mInstrumentation.onException(r.activity, e)) {
3081 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003082 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3083 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 }
3085 }
3086 }
3087 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003088 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 return r;
3090 }
3091
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003092 private static String safeToComponentShortString(Intent intent) {
3093 ComponentName component = intent.getComponent();
3094 return component == null ? "[Unknown]" : component.toShortString();
3095 }
3096
Romain Guy65b345f2011-07-27 18:51:50 -07003097 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003099 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 configChanges, getNonConfigInstance);
3101 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003102 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 WindowManager wm = r.activity.getWindowManager();
3104 View v = r.activity.mDecor;
3105 if (v != null) {
3106 if (r.activity.mVisibleFromServer) {
3107 mNumVisibleActivities--;
3108 }
3109 IBinder wtoken = v.getWindowToken();
3110 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003111 if (r.onlyLocalRequest) {
3112 // Hold off on removing this until the new activity's
3113 // window is being added.
3114 r.mPendingRemoveWindow = v;
3115 r.mPendingRemoveWindowManager = wm;
3116 } else {
3117 wm.removeViewImmediate(v);
3118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003120 if (wtoken != null && r.mPendingRemoveWindow == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121 WindowManagerImpl.getDefault().closeAll(wtoken,
3122 r.activity.getClass().getName(), "Activity");
3123 }
3124 r.activity.mDecor = null;
3125 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003126 if (r.mPendingRemoveWindow == null) {
3127 // If we are delaying the removal of the activity window, then
3128 // we can't clean up all windows here. Note that we can't do
3129 // so later either, which means any windows that aren't closed
3130 // by the app will leak. Well we try to warning them a lot
3131 // about leaking windows, because that is a bug, so if they are
3132 // using this recreate facility then they get to live with leaks.
3133 WindowManagerImpl.getDefault().closeAll(token,
3134 r.activity.getClass().getName(), "Activity");
3135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136
3137 // Mocked out contexts won't be participating in the normal
3138 // process lifecycle, but if we're running with a proper
3139 // ApplicationContext we need to have it tear down things
3140 // cleanly.
3141 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003142 if (c instanceof ContextImpl) {
3143 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 r.activity.getClass().getName(), "Activity");
3145 }
3146 }
3147 if (finishing) {
3148 try {
3149 ActivityManagerNative.getDefault().activityDestroyed(token);
3150 } catch (RemoteException ex) {
3151 // If the system process has died, it's game over for everyone.
3152 }
3153 }
3154 }
3155
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003156 public final void requestRelaunchActivity(IBinder token,
3157 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3158 int configChanges, boolean notResumed, Configuration config,
3159 boolean fromServer) {
3160 ActivityClientRecord target = null;
3161
3162 synchronized (mPackages) {
3163 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3164 ActivityClientRecord r = mRelaunchingActivities.get(i);
3165 if (r.token == token) {
3166 target = r;
3167 if (pendingResults != null) {
3168 if (r.pendingResults != null) {
3169 r.pendingResults.addAll(pendingResults);
3170 } else {
3171 r.pendingResults = pendingResults;
3172 }
3173 }
3174 if (pendingNewIntents != null) {
3175 if (r.pendingIntents != null) {
3176 r.pendingIntents.addAll(pendingNewIntents);
3177 } else {
3178 r.pendingIntents = pendingNewIntents;
3179 }
3180 }
3181 break;
3182 }
3183 }
3184
3185 if (target == null) {
3186 target = new ActivityClientRecord();
3187 target.token = token;
3188 target.pendingResults = pendingResults;
3189 target.pendingIntents = pendingNewIntents;
3190 if (!fromServer) {
3191 ActivityClientRecord existing = mActivities.get(token);
3192 if (existing != null) {
3193 target.startsNotResumed = existing.paused;
3194 }
3195 target.onlyLocalRequest = true;
3196 }
3197 mRelaunchingActivities.add(target);
3198 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3199 }
3200
3201 if (fromServer) {
3202 target.startsNotResumed = notResumed;
3203 target.onlyLocalRequest = false;
3204 }
3205 if (config != null) {
3206 target.createdConfig = config;
3207 }
3208 target.pendingConfigChanges |= configChanges;
3209 }
3210 }
3211
Romain Guy65b345f2011-07-27 18:51:50 -07003212 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 // If we are getting ready to gc after going to the background, well
3214 // we are back active so skip it.
3215 unscheduleGcIdler();
3216
3217 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003218 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 // First: make sure we have the most recent configuration and most
3221 // recent version of the activity, or skip it if some previous call
3222 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003223 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 int N = mRelaunchingActivities.size();
3225 IBinder token = tmp.token;
3226 tmp = null;
3227 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003228 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 if (r.token == token) {
3230 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003231 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 mRelaunchingActivities.remove(i);
3233 i--;
3234 N--;
3235 }
3236 }
Bob Leee5408332009-09-04 18:31:17 -07003237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003239 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 return;
3241 }
Bob Leee5408332009-09-04 18:31:17 -07003242
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003243 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3244 + tmp.token + " with configChanges=0x"
3245 + Integer.toHexString(configChanges));
3246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 if (mPendingConfiguration != null) {
3248 changedConfig = mPendingConfiguration;
3249 mPendingConfiguration = null;
3250 }
3251 }
Bob Leee5408332009-09-04 18:31:17 -07003252
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003253 if (tmp.createdConfig != null) {
3254 // If the activity manager is passing us its current config,
3255 // assume that is really what we want regardless of what we
3256 // may have pending.
3257 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003258 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3259 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3260 if (changedConfig == null
3261 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3262 changedConfig = tmp.createdConfig;
3263 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003264 }
3265 }
3266
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003267 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003268 + tmp.token + ": changedConfig=" + changedConfig);
3269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 // If there was a pending configuration change, execute it first.
3271 if (changedConfig != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003272 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 }
Bob Leee5408332009-09-04 18:31:17 -07003274
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003275 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003276 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277 if (r == null) {
3278 return;
3279 }
Bob Leee5408332009-09-04 18:31:17 -07003280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003282 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003283 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003284
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003285 r.activity.mChangingConfigurations = true;
3286
Dianne Hackborne2b04802010-12-09 09:24:55 -08003287 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003289 performPauseActivity(r.token, false, r.isPreHoneycomb());
3290 }
3291 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3292 r.state = new Bundle();
3293 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294 }
Bob Leee5408332009-09-04 18:31:17 -07003295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 r.activity = null;
3299 r.window = null;
3300 r.hideForNow = false;
3301 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003302 // Merge any pending results and pending intents; don't just replace them
3303 if (tmp.pendingResults != null) {
3304 if (r.pendingResults == null) {
3305 r.pendingResults = tmp.pendingResults;
3306 } else {
3307 r.pendingResults.addAll(tmp.pendingResults);
3308 }
3309 }
3310 if (tmp.pendingIntents != null) {
3311 if (r.pendingIntents == null) {
3312 r.pendingIntents = tmp.pendingIntents;
3313 } else {
3314 r.pendingIntents.addAll(tmp.pendingIntents);
3315 }
3316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003318
Christopher Tateb70f3df2009-04-07 16:07:59 -07003319 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 }
3321
Romain Guy65b345f2011-07-27 18:51:50 -07003322 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003323 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324 Bitmap thumbnail = createThumbnailBitmap(r);
3325 CharSequence description = null;
3326 try {
3327 description = r.activity.onCreateDescription();
3328 } catch (Exception e) {
3329 if (!mInstrumentation.onException(r.activity, e)) {
3330 throw new RuntimeException(
3331 "Unable to create description of activity "
3332 + r.intent.getComponent().toShortString()
3333 + ": " + e.toString(), e);
3334 }
3335 }
3336 //System.out.println("Reporting top thumbnail " + thumbnail);
3337 try {
3338 ActivityManagerNative.getDefault().reportThumbnail(
3339 token, thumbnail, description);
3340 } catch (RemoteException ex) {
3341 }
3342 }
3343
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003344 ArrayList<ComponentCallbacks2> collectComponentCallbacksLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003346 ArrayList<ComponentCallbacks2> callbacks
3347 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 if (mActivities.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003350 Iterator<ActivityClientRecord> it = mActivities.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 while (it.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003352 ActivityClientRecord ar = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 Activity a = ar.activity;
3354 if (a != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003355 Configuration thisConfig = applyConfigCompatMainThread(newConfig,
3356 ar.packageInfo.mCompatibilityInfo.getIfNeeded());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 if (!ar.activity.mFinished && (allActivities ||
3358 (a != null && !ar.paused))) {
3359 // If the activity is currently resumed, its configuration
3360 // needs to change right now.
3361 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003362 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 // Otherwise, we will tell it about the change
3364 // the next time it is resumed or shown. Note that
3365 // the activity manager may, before then, decide the
3366 // activity needs to be destroyed to handle its new
3367 // configuration.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003368 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Setting activity "
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003369 + ar.activityInfo.name + " newConfig=" + thisConfig);
3370 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 }
3372 }
3373 }
3374 }
3375 if (mServices.size() > 0) {
3376 Iterator<Service> it = mServices.values().iterator();
3377 while (it.hasNext()) {
3378 callbacks.add(it.next());
3379 }
3380 }
3381 synchronized (mProviderMap) {
3382 if (mLocalProviders.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003383 Iterator<ProviderClientRecord> it = mLocalProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384 while (it.hasNext()) {
3385 callbacks.add(it.next().mLocalProvider);
3386 }
3387 }
3388 }
3389 final int N = mAllApplications.size();
3390 for (int i=0; i<N; i++) {
3391 callbacks.add(mAllApplications.get(i));
3392 }
Bob Leee5408332009-09-04 18:31:17 -07003393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 return callbacks;
3395 }
Bob Leee5408332009-09-04 18:31:17 -07003396
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003397 private final void performConfigurationChanged(
3398 ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003400 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 // we check the runtime type and act accordingly.
3402 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3403 if (activity != null) {
3404 activity.mCalled = false;
3405 }
Bob Leee5408332009-09-04 18:31:17 -07003406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 boolean shouldChangeConfig = false;
3408 if ((activity == null) || (activity.mCurrentConfig == null)) {
3409 shouldChangeConfig = true;
3410 } else {
Bob Leee5408332009-09-04 18:31:17 -07003411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412 // If the new config is the same as the config this Activity
3413 // is already running with then don't bother calling
3414 // onConfigurationChanged
3415 int diff = activity.mCurrentConfig.diff(config);
3416 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 // If this activity doesn't handle any of the config changes
3418 // then don't bother calling onConfigurationChanged as we're
3419 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003420 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 shouldChangeConfig = true;
3422 }
3423 }
3424 }
Bob Leee5408332009-09-04 18:31:17 -07003425
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003426 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003427 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 if (shouldChangeConfig) {
3429 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 if (activity != null) {
3432 if (!activity.mCalled) {
3433 throw new SuperNotCalledException(
3434 "Activity " + activity.getLocalClassName() +
3435 " did not call through to super.onConfigurationChanged()");
3436 }
3437 activity.mConfigChangeFlags = 0;
3438 activity.mCurrentConfig = new Configuration(config);
3439 }
3440 }
3441 }
3442
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003443 public final void applyConfigurationToResources(Configuration config) {
3444 synchronized (mPackages) {
3445 applyConfigurationToResourcesLocked(config, null);
3446 }
3447 }
3448
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003449 final boolean applyConfigurationToResourcesLocked(Configuration config,
3450 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003451 if (mResConfiguration == null) {
3452 mResConfiguration = new Configuration();
3453 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003454 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003455 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003456 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003457 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003458 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003459 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003460 DisplayMetrics dm = getDisplayMetricsLocked(compat, true);
3461
3462 if (compat != null && (mResCompatibilityInfo == null ||
3463 !mResCompatibilityInfo.equals(compat))) {
3464 mResCompatibilityInfo = compat;
3465 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3466 | ActivityInfo.CONFIG_SCREEN_SIZE
3467 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3468 }
Bob Leee5408332009-09-04 18:31:17 -07003469
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003470 // set it for java, this also affects newly created Resources
3471 if (config.locale != null) {
3472 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 }
Bob Leee5408332009-09-04 18:31:17 -07003474
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003475 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003476
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003477 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003478 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003479
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003480 Iterator<WeakReference<Resources>> it =
3481 mActiveResources.values().iterator();
3482 //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3483 // mActiveResources.entrySet().iterator();
3484 while (it.hasNext()) {
3485 WeakReference<Resources> v = it.next();
3486 Resources r = v.get();
3487 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003488 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003489 + r + " config to: " + config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003490 r.updateConfiguration(config, dm, compat);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003491 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003492 // + " " + r + ": " + r.getConfiguration());
3493 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003494 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003495 it.remove();
3496 }
3497 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003498
3499 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003500 }
3501
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003502 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003503
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003504 ArrayList<ComponentCallbacks2> callbacks = null;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003505
3506 synchronized (mPackages) {
3507 if (mPendingConfiguration != null) {
3508 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3509 config = mPendingConfiguration;
3510 }
3511 mPendingConfiguration = null;
3512 }
3513
3514 if (config == null) {
3515 return;
3516 }
3517
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003518 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003519 + config);
3520
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003521 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 if (mConfiguration == null) {
3524 mConfiguration = new Configuration();
3525 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003526 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003527 return;
3528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003529 mConfiguration.updateFrom(config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003530 if (mCompatConfiguration == null) {
3531 mCompatConfiguration = new Configuration();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003532 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003533 mCompatConfiguration.setTo(mConfiguration);
3534 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
3535 mResCompatibilityInfo.applyToConfiguration(mCompatConfiguration);
3536 config = mCompatConfiguration;
3537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 callbacks = collectComponentCallbacksLocked(false, config);
3539 }
Romain Guy65b345f2011-07-27 18:51:50 -07003540
3541 // Cleanup hardware accelerated stuff
3542 WindowManagerImpl.getDefault().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003543
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003544 if (callbacks != null) {
3545 final int N = callbacks.size();
3546 for (int i=0; i<N; i++) {
3547 performConfigurationChanged(callbacks.get(i), config);
3548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 }
3550 }
3551
3552 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003553 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 if (r == null || r.activity == null) {
3555 return;
3556 }
Bob Leee5408332009-09-04 18:31:17 -07003557
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003558 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003559 + r.activityInfo.name);
3560
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003561 performConfigurationChanged(r.activity, mCompatConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 }
3563
Romain Guy7eabe552011-07-21 14:56:34 -07003564 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003565 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003566 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003567 switch (profileType) {
3568 case 1:
3569 ViewDebug.startLooperProfiling(pcd.path, pcd.fd.getFileDescriptor());
3570 break;
3571 default:
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003572 mBoundApplication.setProfiler(pcd.path, pcd.fd);
3573 mBoundApplication.autoStopProfiler = false;
3574 mBoundApplication.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003575 break;
3576 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003577 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003578 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003579 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003580 } finally {
3581 try {
3582 pcd.fd.close();
3583 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003584 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003585 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003586 }
3587 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003588 switch (profileType) {
3589 case 1:
3590 ViewDebug.stopLooperProfiling();
3591 break;
3592 default:
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003593 mBoundApplication.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003594 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003595 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003596 }
3597 }
Bob Leee5408332009-09-04 18:31:17 -07003598
Andy McFadden824c5102010-07-09 16:26:57 -07003599 final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
3600 if (managed) {
3601 try {
3602 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3603 } catch (IOException e) {
3604 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3605 + " -- can the process access this path?");
3606 } finally {
3607 try {
3608 dhd.fd.close();
3609 } catch (IOException e) {
3610 Slog.w(TAG, "Failure closing profile fd", e);
3611 }
3612 }
3613 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003614 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003615 }
3616 }
3617
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003618 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3619 boolean hasPkgInfo = false;
3620 if (packages != null) {
3621 for (int i=packages.length-1; i>=0; i--) {
3622 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3623 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003624 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003625 ref = mPackages.get(packages[i]);
3626 if (ref != null && ref.get() != null) {
3627 hasPkgInfo = true;
3628 } else {
3629 ref = mResourcePackages.get(packages[i]);
3630 if (ref != null && ref.get() != null) {
3631 hasPkgInfo = true;
3632 }
3633 }
3634 }
3635 mPackages.remove(packages[i]);
3636 mResourcePackages.remove(packages[i]);
3637 }
3638 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003639 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003640 hasPkgInfo);
3641 }
3642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003643 final void handleLowMemory() {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003644 ArrayList<ComponentCallbacks2> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003645
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003646 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 callbacks = collectComponentCallbacksLocked(true, null);
3648 }
Bob Leee5408332009-09-04 18:31:17 -07003649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003650 final int N = callbacks.size();
3651 for (int i=0; i<N; i++) {
3652 callbacks.get(i).onLowMemory();
3653 }
3654
Chris Tatece229052009-03-25 16:44:52 -07003655 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3656 if (Process.myUid() != Process.SYSTEM_UID) {
3657 int sqliteReleased = SQLiteDatabase.releaseMemory();
3658 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3659 }
Bob Leee5408332009-09-04 18:31:17 -07003660
Mike Reedcaf0df12009-04-27 14:32:05 -04003661 // Ask graphics to free up as much as possible (font/image caches)
3662 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003663
3664 BinderInternal.forceGc("mem");
3665 }
3666
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003667 final void handleTrimMemory(int level) {
Romain Guybdf76092011-07-18 15:00:43 -07003668 WindowManagerImpl.getDefault().trimMemory(level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003669 ArrayList<ComponentCallbacks2> callbacks;
3670
3671 synchronized (mPackages) {
3672 callbacks = collectComponentCallbacksLocked(true, null);
3673 }
3674
3675 final int N = callbacks.size();
3676 for (int i=0; i<N; i++) {
3677 callbacks.get(i).onTrimMemory(level);
3678 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003679 }
3680
Romain Guy65b345f2011-07-27 18:51:50 -07003681 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682 mBoundApplication = data;
3683 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003684 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003687 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 android.ddm.DdmHandleAppName.setAppName(data.processName);
3689
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003690 if (data.profileFd != null) {
3691 data.startProfiling();
3692 }
3693
Joe Onoratod630f102011-03-17 18:42:26 -07003694 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
3695 // implementation to use the pool executor. Normally, we use the
3696 // serialized executor as the default. This has to happen in the
3697 // main thread so the main looper is set right.
3698 if (data.appInfo.targetSdkVersion <= 12) {
3699 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
3700 }
3701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 /*
3703 * Before spawning a new process, reset the time zone to be the system time zone.
3704 * This needs to be done because the system time zone could have changed after the
3705 * the spawning of this process. Without doing this this process would have the incorrect
3706 * system time zone.
3707 */
3708 TimeZone.setDefault(null);
3709
3710 /*
3711 * Initialize the default locale in this process for the reasons we set the time zone.
3712 */
3713 Locale.setDefault(data.config.locale);
3714
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003715 /*
3716 * Update the system configuration since its preloaded and might not
3717 * reflect configuration changes. The configuration object passed
3718 * in AppBindData can be safely assumed to be up to date
3719 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003720 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003721
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003722 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003724 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003725 * For system applications on userdebug/eng builds, log stack
3726 * traces of disk and network access to dropbox for analysis.
3727 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07003728 if ((data.appInfo.flags &
3729 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07003730 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
3731 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003732 }
3733
3734 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07003735 * For apps targetting SDK Honeycomb or later, we don't allow
3736 * network usage on the main event loop / UI thread.
3737 *
3738 * Note to those grepping: this is what ultimately throws
3739 * NetworkOnMainThreadException ...
3740 */
3741 if (data.appInfo.targetSdkVersion > 9) {
3742 StrictMode.enableDeathOnNetwork();
3743 }
3744
3745 /**
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003746 * Switch this process to density compatibility mode if needed.
3747 */
3748 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
3749 == 0) {
3750 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3751 }
Bob Leee5408332009-09-04 18:31:17 -07003752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003753 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
3754 // XXX should have option to change the port.
3755 Debug.changeDebugPort(8100);
3756 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003757 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 + " is waiting for the debugger on port 8100...");
3759
3760 IActivityManager mgr = ActivityManagerNative.getDefault();
3761 try {
3762 mgr.showWaitingForDebugger(mAppThread, true);
3763 } catch (RemoteException ex) {
3764 }
3765
3766 Debug.waitForDebugger();
3767
3768 try {
3769 mgr.showWaitingForDebugger(mAppThread, false);
3770 } catch (RemoteException ex) {
3771 }
3772
3773 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003774 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 + " can be debugged on port 8100...");
3776 }
3777 }
3778
Robert Greenwalt434203a2010-10-11 16:00:27 -07003779 /**
3780 * Initialize the default http proxy in this process for the reasons we set the time zone.
3781 */
3782 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
3783 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
3784 try {
3785 ProxyProperties proxyProperties = service.getProxy();
3786 Proxy.setHttpProxySystemProperty(proxyProperties);
3787 } catch (RemoteException e) {}
3788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003789 if (data.instrumentationName != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08003790 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003791 appContext.init(data.info, null, this);
3792 InstrumentationInfo ii = null;
3793 try {
3794 ii = appContext.getPackageManager().
3795 getInstrumentationInfo(data.instrumentationName, 0);
3796 } catch (PackageManager.NameNotFoundException e) {
3797 }
3798 if (ii == null) {
3799 throw new RuntimeException(
3800 "Unable to find instrumentation info for: "
3801 + data.instrumentationName);
3802 }
3803
3804 mInstrumentationAppDir = ii.sourceDir;
3805 mInstrumentationAppPackage = ii.packageName;
3806 mInstrumentedAppDir = data.info.getAppDir();
3807
3808 ApplicationInfo instrApp = new ApplicationInfo();
3809 instrApp.packageName = ii.packageName;
3810 instrApp.sourceDir = ii.sourceDir;
3811 instrApp.publicSourceDir = ii.publicSourceDir;
3812 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07003813 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003814 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08003816 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003817 instrContext.init(pi, null, this);
3818
3819 try {
3820 java.lang.ClassLoader cl = instrContext.getClassLoader();
3821 mInstrumentation = (Instrumentation)
3822 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
3823 } catch (Exception e) {
3824 throw new RuntimeException(
3825 "Unable to instantiate instrumentation "
3826 + data.instrumentationName + ": " + e.toString(), e);
3827 }
3828
3829 mInstrumentation.init(this, instrContext, appContext,
3830 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
3831
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003832 if (data.profileFile != null && !ii.handleProfiling
3833 && data.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 data.handlingProfiling = true;
3835 File file = new File(data.profileFile);
3836 file.getParentFile().mkdirs();
3837 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
3838 }
3839
3840 try {
3841 mInstrumentation.onCreate(data.instrumentationArgs);
3842 }
3843 catch (Exception e) {
3844 throw new RuntimeException(
3845 "Exception thrown in onCreate() of "
3846 + data.instrumentationName + ": " + e.toString(), e);
3847 }
3848
3849 } else {
3850 mInstrumentation = new Instrumentation();
3851 }
3852
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003853 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08003854 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003855 }
3856
Christopher Tate181fafa2009-05-14 11:12:14 -07003857 // If the app is being launched for full backup or restore, bring it up in
3858 // a restricted environment with the base application class.
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003859 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 mInitialApplication = app;
3861
Christopher Tate75a99702011-05-18 16:28:19 -07003862 // don't bring up providers in restricted mode; they may depend on the
3863 // app's custom Application class
3864 if (!data.restrictedBackupMode){
3865 List<ProviderInfo> providers = data.providers;
3866 if (providers != null) {
3867 installContentProviders(app, providers);
3868 // For process that contains content providers, we want to
3869 // ensure that the JIT is enabled "at some point".
3870 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
3871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 }
3873
3874 try {
3875 mInstrumentation.callApplicationOnCreate(app);
3876 } catch (Exception e) {
3877 if (!mInstrumentation.onException(app, e)) {
3878 throw new RuntimeException(
3879 "Unable to create application " + app.getClass().getName()
3880 + ": " + e.toString(), e);
3881 }
3882 }
3883 }
3884
3885 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
3886 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003887 if (mBoundApplication.profileFile != null && mBoundApplication.handlingProfiling
3888 && mBoundApplication.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 Debug.stopMethodTracing();
3890 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003891 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 // + ", app thr: " + mAppThread);
3893 try {
3894 am.finishInstrumentation(mAppThread, resultCode, results);
3895 } catch (RemoteException ex) {
3896 }
3897 }
3898
Romain Guy65b345f2011-07-27 18:51:50 -07003899 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900 Context context, List<ProviderInfo> providers) {
3901 final ArrayList<IActivityManager.ContentProviderHolder> results =
3902 new ArrayList<IActivityManager.ContentProviderHolder>();
3903
3904 Iterator<ProviderInfo> i = providers.iterator();
3905 while (i.hasNext()) {
3906 ProviderInfo cpi = i.next();
3907 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07003908 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003909 buf.append(cpi.authority);
3910 buf.append(": ");
3911 buf.append(cpi.name);
3912 Log.i(TAG, buf.toString());
3913 IContentProvider cp = installProvider(context, null, cpi, false);
3914 if (cp != null) {
3915 IActivityManager.ContentProviderHolder cph =
3916 new IActivityManager.ContentProviderHolder(cpi);
3917 cph.provider = cp;
3918 results.add(cph);
3919 // Don't ever unload this provider from the process.
3920 synchronized(mProviderMap) {
3921 mProviderRefCountMap.put(cp.asBinder(), new ProviderRefCount(10000));
3922 }
3923 }
3924 }
3925
3926 try {
3927 ActivityManagerNative.getDefault().publishContentProviders(
3928 getApplicationThread(), results);
3929 } catch (RemoteException ex) {
3930 }
3931 }
3932
Romain Guy65b345f2011-07-27 18:51:50 -07003933 private IContentProvider getExistingProvider(Context context, String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003934 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003935 final ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 if (pr != null) {
3937 return pr.mProvider;
3938 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003939 return null;
3940 }
3941 }
3942
Romain Guy65b345f2011-07-27 18:51:50 -07003943 private IContentProvider getProvider(Context context, String name) {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003944 IContentProvider existing = getExistingProvider(context, name);
3945 if (existing != null) {
3946 return existing;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 }
3948
3949 IActivityManager.ContentProviderHolder holder = null;
3950 try {
3951 holder = ActivityManagerNative.getDefault().getContentProvider(
3952 getApplicationThread(), name);
3953 } catch (RemoteException ex) {
3954 }
3955 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003956 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003957 return null;
3958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003959
3960 IContentProvider prov = installProvider(context, holder.provider,
3961 holder.info, true);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003962 //Slog.i(TAG, "noReleaseNeeded=" + holder.noReleaseNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 if (holder.noReleaseNeeded || holder.provider == null) {
3964 // We are not going to release the provider if it is an external
3965 // provider that doesn't care about being released, or if it is
3966 // a local provider running in this process.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003967 //Slog.i(TAG, "*** NO RELEASE NEEDED");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003968 synchronized(mProviderMap) {
3969 mProviderRefCountMap.put(prov.asBinder(), new ProviderRefCount(10000));
3970 }
3971 }
3972 return prov;
3973 }
3974
3975 public final IContentProvider acquireProvider(Context c, String name) {
3976 IContentProvider provider = getProvider(c, name);
3977 if(provider == null)
3978 return null;
3979 IBinder jBinder = provider.asBinder();
3980 synchronized(mProviderMap) {
3981 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3982 if(prc == null) {
3983 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
3984 } else {
3985 prc.count++;
3986 } //end else
3987 } //end synchronized
3988 return provider;
3989 }
3990
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003991 public final IContentProvider acquireExistingProvider(Context c, String name) {
3992 IContentProvider provider = getExistingProvider(c, name);
3993 if(provider == null)
3994 return null;
3995 IBinder jBinder = provider.asBinder();
3996 synchronized(mProviderMap) {
3997 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3998 if(prc == null) {
3999 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
4000 } else {
4001 prc.count++;
4002 } //end else
4003 } //end synchronized
4004 return provider;
4005 }
4006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 public final boolean releaseProvider(IContentProvider provider) {
4008 if(provider == null) {
4009 return false;
4010 }
4011 IBinder jBinder = provider.asBinder();
4012 synchronized(mProviderMap) {
4013 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4014 if(prc == null) {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004015 if(localLOGV) Slog.v(TAG, "releaseProvider::Weird shouldn't be here");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016 return false;
4017 } else {
4018 prc.count--;
4019 if(prc.count == 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004020 // Schedule the actual remove asynchronously, since we
4021 // don't know the context this will be called in.
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004022 // TODO: it would be nice to post a delayed message, so
4023 // if we come back and need the same provider quickly
4024 // we will still have it available.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004025 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, provider);
4026 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004027 } //end if
4028 } //end else
4029 } //end synchronized
4030 return true;
4031 }
4032
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004033 final void completeRemoveProvider(IContentProvider provider) {
4034 IBinder jBinder = provider.asBinder();
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004035 String name = null;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004036 synchronized(mProviderMap) {
4037 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4038 if(prc != null && prc.count == 0) {
4039 mProviderRefCountMap.remove(jBinder);
4040 //invoke removeProvider to dereference provider
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004041 name = removeProviderLocked(provider);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004042 }
4043 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004044
4045 if (name != null) {
4046 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004047 if(localLOGV) Slog.v(TAG, "removeProvider::Invoking " +
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004048 "ActivityManagerNative.removeContentProvider(" + name);
4049 ActivityManagerNative.getDefault().removeContentProvider(
4050 getApplicationThread(), name);
4051 } catch (RemoteException e) {
4052 //do nothing content provider object is dead any way
4053 } //end catch
4054 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004055 }
4056
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004057 public final String removeProviderLocked(IContentProvider provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 if (provider == null) {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004059 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004060 }
4061 IBinder providerBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004063 String name = null;
4064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004065 // remove the provider from mProviderMap
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004066 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 while (iter.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004068 ProviderClientRecord pr = iter.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 IBinder myBinder = pr.mProvider.asBinder();
4070 if (myBinder == providerBinder) {
4071 //find if its published by this process itself
4072 if(pr.mLocalProvider != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004073 if(localLOGV) Slog.i(TAG, "removeProvider::found local provider returning");
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004074 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004075 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004076 if(localLOGV) Slog.v(TAG, "removeProvider::Not local provider Unlinking " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004077 "death recipient");
4078 //content provider is in another process
4079 myBinder.unlinkToDeath(pr, 0);
4080 iter.remove();
4081 //invoke remove only once for the very first name seen
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004082 if(name == null) {
4083 name = pr.mName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 }
4085 } //end if myBinder
4086 } //end while iter
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004087
4088 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 }
4090
4091 final void removeDeadProvider(String name, IContentProvider provider) {
4092 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004093 ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 if (pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004095 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004096 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004097 if (removed != null) {
4098 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
4099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004100 }
4101 }
4102 }
4103
4104 final void removeDeadProviderLocked(String name, IContentProvider provider) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004105 ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 if (pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004107 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004108 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004109 if (removed != null) {
4110 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
4111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 }
4113 }
4114
Romain Guy65b345f2011-07-27 18:51:50 -07004115 private IContentProvider installProvider(Context context,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 IContentProvider provider, ProviderInfo info, boolean noisy) {
4117 ContentProvider localProvider = null;
4118 if (provider == null) {
4119 if (noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004120 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 + info.name);
4122 }
4123 Context c = null;
4124 ApplicationInfo ai = info.applicationInfo;
4125 if (context.getPackageName().equals(ai.packageName)) {
4126 c = context;
4127 } else if (mInitialApplication != null &&
4128 mInitialApplication.getPackageName().equals(ai.packageName)) {
4129 c = mInitialApplication;
4130 } else {
4131 try {
4132 c = context.createPackageContext(ai.packageName,
4133 Context.CONTEXT_INCLUDE_CODE);
4134 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004135 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136 }
4137 }
4138 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004139 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140 ai.packageName +
4141 " while loading content provider " +
4142 info.name);
4143 return null;
4144 }
4145 try {
4146 final java.lang.ClassLoader cl = c.getClassLoader();
4147 localProvider = (ContentProvider)cl.
4148 loadClass(info.name).newInstance();
4149 provider = localProvider.getIContentProvider();
4150 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004151 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 info.name + " from sourceDir " +
4153 info.applicationInfo.sourceDir);
4154 return null;
4155 }
Joe Onorato43a17652011-04-06 19:22:23 -07004156 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004157 TAG, "Instantiating local provider " + info.name);
4158 // XXX Need to create the correct context for this provider.
4159 localProvider.attachInfo(c, info);
4160 } catch (java.lang.Exception e) {
4161 if (!mInstrumentation.onException(null, e)) {
4162 throw new RuntimeException(
4163 "Unable to get provider " + info.name
4164 + ": " + e.toString(), e);
4165 }
4166 return null;
4167 }
4168 } else if (localLOGV) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004169 Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170 + info.name);
4171 }
4172
4173 synchronized (mProviderMap) {
4174 // Cache the pointer for the remote provider.
4175 String names[] = PATTERN_SEMICOLON.split(info.authority);
4176 for (int i=0; i<names.length; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004177 ProviderClientRecord pr = new ProviderClientRecord(names[i], provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 localProvider);
4179 try {
4180 provider.asBinder().linkToDeath(pr, 0);
4181 mProviderMap.put(names[i], pr);
4182 } catch (RemoteException e) {
4183 return null;
4184 }
4185 }
4186 if (localProvider != null) {
4187 mLocalProviders.put(provider.asBinder(),
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004188 new ProviderClientRecord(null, provider, localProvider));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 }
4190 }
4191
4192 return provider;
4193 }
4194
Romain Guy65b345f2011-07-27 18:51:50 -07004195 private void attach(boolean system) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004196 sThreadLocal.set(this);
4197 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004199 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004200 public void run() {
4201 ensureJitEnabled();
4202 }
4203 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004204 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4205 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4206 IActivityManager mgr = ActivityManagerNative.getDefault();
4207 try {
4208 mgr.attachApplication(mAppThread);
4209 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004210 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 }
4212 } else {
4213 // Don't set application object here -- if the system crashes,
4214 // we can't display an alert, we just want to die die die.
4215 android.ddm.DdmHandleAppName.setAppName("system_process");
4216 try {
4217 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004218 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 context.init(getSystemContext().mPackageInfo, null, this);
4220 Application app = Instrumentation.newApplication(Application.class, context);
4221 mAllApplications.add(app);
4222 mInitialApplication = app;
4223 app.onCreate();
4224 } catch (Exception e) {
4225 throw new RuntimeException(
4226 "Unable to instantiate Application():" + e.toString(), e);
4227 }
4228 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004229
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004230 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004231 public void onConfigurationChanged(Configuration newConfig) {
4232 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004233 // We need to apply this change to the resources
4234 // immediately, because upon returning the view
4235 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004236 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004237 // This actually changed the resources! Tell
4238 // everyone about it.
4239 if (mPendingConfiguration == null ||
4240 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4241 mPendingConfiguration = newConfig;
4242
4243 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4244 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004245 }
4246 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004247 }
4248 public void onLowMemory() {
4249 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004250 public void onTrimMemory(int level) {
4251 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004252 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004253 }
4254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 public static final ActivityThread systemMain() {
Romain Guy52339202010-09-03 16:04:46 -07004256 HardwareRenderer.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 ActivityThread thread = new ActivityThread();
4258 thread.attach(true);
4259 return thread;
4260 }
4261
Jeff Brown10e89712011-07-08 18:52:57 -07004262 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004263 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004264 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004265 }
4266 }
4267
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004268 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004269 synchronized (mPackages) {
4270 if (mCoreSettings != null) {
4271 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004272 } else {
4273 return defaultValue;
4274 }
4275 }
4276 }
4277
Romain Guy65b345f2011-07-27 18:51:50 -07004278 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004279 SamplingProfilerIntegration.start();
4280
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004281 // CloseGuard defaults to true and can be quite spammy. We
4282 // disable it here, but selectively enable it later (via
4283 // StrictMode) on debug builds, but using DropBox, not logs.
4284 CloseGuard.setEnabled(false);
4285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 Process.setArgV0("<pre-initialized>");
4287
4288 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07004289 if (sMainThreadHandler == null) {
4290 sMainThreadHandler = new Handler();
4291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004292
4293 ActivityThread thread = new ActivityThread();
4294 thread.attach(false);
4295
Dianne Hackborn287952c2010-09-22 22:34:31 -07004296 if (false) {
4297 Looper.myLooper().setMessageLogging(new
4298 LogPrinter(Log.DEBUG, "ActivityThread"));
4299 }
4300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301 Looper.loop();
4302
Jeff Brown10e89712011-07-08 18:52:57 -07004303 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004304 }
4305}