blob: 2c2a493f85e20cf03f30a422e8052284fc20f2d7 [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;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700375 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400377 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 boolean handlingProfiling;
379 public String toString() {
380 return "AppBindData{appInfo=" + appInfo + "}";
381 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700382 public void setProfiler(String file, ParcelFileDescriptor fd) {
383 if (profiling) {
384 if (fd != null) {
385 try {
386 fd.close();
387 } catch (IOException e) {
388 }
389 }
390 return;
391 }
392 if (profileFd != null) {
393 try {
394 profileFd.close();
395 } catch (IOException e) {
396 }
397 }
398 profileFile = file;
399 profileFd = fd;
400 }
401 public void startProfiling() {
402 if (profileFd == null || profiling) {
403 return;
404 }
405 try {
406 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
407 8 * 1024 * 1024, 0);
408 profiling = true;
409 } catch (RuntimeException e) {
410 Slog.w(TAG, "Profiling failed on path " + profileFile);
411 try {
412 profileFd.close();
413 profileFd = null;
414 } catch (IOException e2) {
415 Slog.w(TAG, "Failure closing profile fd", e2);
416 }
417 }
418 }
419 public void stopProfiling() {
420 if (profiling) {
421 profiling = false;
422 Debug.stopMethodTracing();
423 if (profileFd != null) {
424 try {
425 profileFd.close();
426 } catch (IOException e) {
427 }
428 }
429 profileFd = null;
430 profileFile = null;
431 }
432 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 }
434
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400435 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700436 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700437 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800438 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 }
441
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400442 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 IBinder token;
444 List<ResultInfo> results;
445 public String toString() {
446 return "ResultData{token=" + token + " results" + results + "}";
447 }
448 }
449
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400450 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800451 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 String what;
453 String who;
454 }
455
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400456 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700457 String path;
458 ParcelFileDescriptor fd;
459 }
460
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400461 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700462 String path;
463 ParcelFileDescriptor fd;
464 }
465
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400466 static final class UpdateCompatibilityData {
467 String pkg;
468 CompatibilityInfo info;
469 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700470
Romain Guy65b345f2011-07-27 18:51:50 -0700471 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700472
Romain Guy65b345f2011-07-27 18:51:50 -0700473 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700474 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
475 private static final String ONE_COUNT_COLUMN = "%21s %8d";
476 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
477 private static final String TWO_COUNT_COLUMNS_DB = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700478 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 // Formatting for checkin service - update version if row format changes
481 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 public final void schedulePauseActivity(IBinder token, boolean finished,
484 boolean userLeaving, int configChanges) {
485 queueOrSendMessage(
486 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
487 token,
488 (userLeaving ? 1 : 0),
489 configChanges);
490 }
491
492 public final void scheduleStopActivity(IBinder token, boolean showWindow,
493 int configChanges) {
494 queueOrSendMessage(
495 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
496 token, 0, configChanges);
497 }
498
499 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
500 queueOrSendMessage(
501 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
502 token);
503 }
504
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800505 public final void scheduleSleeping(IBinder token, boolean sleeping) {
506 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
507 }
508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
510 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
511 }
512
513 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
514 ResultData res = new ResultData();
515 res.token = token;
516 res.results = results;
517 queueOrSendMessage(H.SEND_RESULT, res);
518 }
519
520 // we use token to identify this activity without having to send the
521 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700522 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400523 ActivityInfo info, CompatibilityInfo compatInfo, Bundle state,
524 List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700525 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
526 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700527 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528
529 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700530 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 r.intent = intent;
532 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400533 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 r.state = state;
535
536 r.pendingResults = pendingResults;
537 r.pendingIntents = pendingNewIntents;
538
539 r.startsNotResumed = notResumed;
540 r.isForward = isForward;
541
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700542 r.profileFile = profileName;
543 r.profileFd = profileFd;
544 r.autoStopProfiler = autoStopProfiler;
545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
547 }
548
549 public final void scheduleRelaunchActivity(IBinder token,
550 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800551 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800552 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
553 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 }
555
556 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
557 NewIntentData data = new NewIntentData();
558 data.intents = intents;
559 data.token = token;
560
561 queueOrSendMessage(H.NEW_INTENT, data);
562 }
563
564 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
565 int configChanges) {
566 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
567 configChanges);
568 }
569
570 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400571 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
572 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700573 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
574 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400576 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 queueOrSendMessage(H.RECEIVER, r);
578 }
579
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400580 public final void scheduleCreateBackupAgent(ApplicationInfo app,
581 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700582 CreateBackupAgentData d = new CreateBackupAgentData();
583 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400584 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700585 d.backupMode = backupMode;
586
587 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
588 }
589
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400590 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
591 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700592 CreateBackupAgentData d = new CreateBackupAgentData();
593 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400594 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700595
596 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
597 }
598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400600 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 CreateServiceData s = new CreateServiceData();
602 s.token = token;
603 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400604 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605
606 queueOrSendMessage(H.CREATE_SERVICE, s);
607 }
608
609 public final void scheduleBindService(IBinder token, Intent intent,
610 boolean rebind) {
611 BindServiceData s = new BindServiceData();
612 s.token = token;
613 s.intent = intent;
614 s.rebind = rebind;
615
616 queueOrSendMessage(H.BIND_SERVICE, s);
617 }
618
619 public final void scheduleUnbindService(IBinder token, Intent intent) {
620 BindServiceData s = new BindServiceData();
621 s.token = token;
622 s.intent = intent;
623
624 queueOrSendMessage(H.UNBIND_SERVICE, s);
625 }
626
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700627 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700628 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 ServiceArgsData s = new ServiceArgsData();
630 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700631 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700633 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 s.args = args;
635
636 queueOrSendMessage(H.SERVICE_ARGS, s);
637 }
638
639 public final void scheduleStopService(IBinder token) {
640 queueOrSendMessage(H.STOP_SERVICE, token);
641 }
642
643 public final void bindApplication(String processName,
644 ApplicationInfo appInfo, List<ProviderInfo> providers,
645 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700646 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700648 int debugMode, boolean isRestrictedBackupMode, boolean persistent,
649 Configuration config, CompatibilityInfo compatInfo,
650 Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651
652 if (services != null) {
653 // Setup the service cache in the ServiceManager
654 ServiceManager.initServiceCache(services);
655 }
656
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800657 setCoreSettings(coreSettings);
658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 AppBindData data = new AppBindData();
660 data.processName = processName;
661 data.appInfo = appInfo;
662 data.providers = providers;
663 data.instrumentationName = instrumentationName;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700664 data.setProfiler(profileFile, profileFd);
665 data.autoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 data.instrumentationArgs = instrumentationArgs;
667 data.instrumentationWatcher = instrumentationWatcher;
668 data.debugMode = debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700669 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700670 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400672 data.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 queueOrSendMessage(H.BIND_APPLICATION, data);
674 }
675
676 public final void scheduleExit() {
677 queueOrSendMessage(H.EXIT_APPLICATION, null);
678 }
679
Christopher Tate5e1ab332009-09-01 20:32:49 -0700680 public final void scheduleSuicide() {
681 queueOrSendMessage(H.SUICIDE, null);
682 }
683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 public void requestThumbnail(IBinder token) {
685 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
686 }
687
688 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800689 synchronized (mPackages) {
690 if (mPendingConfiguration == null ||
691 mPendingConfiguration.isOtherSeqNewer(config)) {
692 mPendingConfiguration = config;
693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 }
695 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
696 }
697
698 public void updateTimeZone() {
699 TimeZone.setDefault(null);
700 }
701
Robert Greenwalt03595d02010-11-02 14:08:23 -0700702 public void clearDnsCache() {
703 // a non-standard API to get this to libcore
704 InetAddress.clearDnsCache();
705 }
706
Robert Greenwalt434203a2010-10-11 16:00:27 -0700707 public void setHttpProxy(String host, String port, String exclList) {
708 Proxy.setHttpProxySystemProperty(host, port, exclList);
709 }
710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 public void processInBackground() {
712 mH.removeMessages(H.GC_WHEN_IDLE);
713 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
714 }
715
716 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700717 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700718 try {
719 data.fd = ParcelFileDescriptor.dup(fd);
720 data.token = servicetoken;
721 data.args = args;
722 queueOrSendMessage(H.DUMP_SERVICE, data);
723 } catch (IOException e) {
724 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 }
726 }
727
728 // This function exists to make sure all receiver dispatching is
729 // correctly ordered, since these are one-way calls and the binder driver
730 // applies transaction ordering per object for such calls.
731 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700732 int resultCode, String dataStr, Bundle extras, boolean ordered,
733 boolean sticky) throws RemoteException {
734 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
Bob Leee5408332009-09-04 18:31:17 -0700736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 public void scheduleLowMemory() {
738 queueOrSendMessage(H.LOW_MEMORY, null);
739 }
740
741 public void scheduleActivityConfigurationChanged(IBinder token) {
742 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
743 }
744
Romain Guy7eabe552011-07-21 14:56:34 -0700745 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
746 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700747 ProfilerControlData pcd = new ProfilerControlData();
748 pcd.path = path;
749 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700750 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800751 }
752
Andy McFadden824c5102010-07-09 16:26:57 -0700753 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
754 DumpHeapData dhd = new DumpHeapData();
755 dhd.path = path;
756 dhd.fd = fd;
757 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
758 }
759
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700760 public void setSchedulingGroup(int group) {
761 // Note: do this immediately, since going into the foreground
762 // should happen regardless of what pending work we have to do
763 // and the activity manager will wait for us to report back that
764 // we are done before sending us to the background.
765 try {
766 Process.setProcessGroup(Process.myPid(), group);
767 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800768 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700769 }
770 }
Bob Leee5408332009-09-04 18:31:17 -0700771
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700772 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
773 Debug.getMemoryInfo(outInfo);
774 }
Bob Leee5408332009-09-04 18:31:17 -0700775
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700776 public void dispatchPackageBroadcast(int cmd, String[] packages) {
777 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
778 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700779
780 public void scheduleCrash(String msg) {
781 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
782 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700783
Dianne Hackborn30d71892010-12-11 10:37:55 -0800784 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
785 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700786 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700787 try {
788 data.fd = ParcelFileDescriptor.dup(fd);
789 data.token = activitytoken;
790 data.prefix = prefix;
791 data.args = args;
792 queueOrSendMessage(H.DUMP_ACTIVITY, data);
793 } catch (IOException e) {
794 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700795 }
796 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700799 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
800 boolean all, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700801 FileOutputStream fout = new FileOutputStream(fd);
802 PrintWriter pw = new PrintWriter(fout);
803 try {
Dianne Hackbornb437e092011-08-05 17:50:29 -0700804 return dumpMemInfo(pw, checkin, all, args);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700805 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700806 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700807 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700808 }
809
Dianne Hackbornb437e092011-08-05 17:50:29 -0700810 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean all,
811 String[] args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 long nativeMax = Debug.getNativeHeapSize() / 1024;
813 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
814 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
815
816 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
817 Debug.getMemoryInfo(memInfo);
818
Dianne Hackbornb437e092011-08-05 17:50:29 -0700819 if (!all) {
820 return memInfo;
821 }
822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 Runtime runtime = Runtime.getRuntime();
824
825 long dalvikMax = runtime.totalMemory() / 1024;
826 long dalvikFree = runtime.freeMemory() / 1024;
827 long dalvikAllocated = dalvikMax - dalvikFree;
828 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700829 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700830 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
831 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 int globalAssetCount = AssetManager.getGlobalAssetCount();
833 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
834 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
835 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
836 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700837 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 long sqliteAllocated = SQLiteDebug.getHeapAllocatedSize() / 1024;
Vasu Noric3849202010-03-09 10:47:25 -0800839 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700842 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 // NOTE: if you change anything significant below, also consider changing
844 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700845 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 // Header
849 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
850 pw.print(Process.myPid()); pw.print(',');
851 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 // Heap info - max
854 pw.print(nativeMax); pw.print(',');
855 pw.print(dalvikMax); pw.print(',');
856 pw.print("N/A,");
857 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 // Heap info - allocated
860 pw.print(nativeAllocated); pw.print(',');
861 pw.print(dalvikAllocated); pw.print(',');
862 pw.print("N/A,");
863 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 // Heap info - free
866 pw.print(nativeFree); pw.print(',');
867 pw.print(dalvikFree); pw.print(',');
868 pw.print("N/A,");
869 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 // Heap info - proportional set size
872 pw.print(memInfo.nativePss); pw.print(',');
873 pw.print(memInfo.dalvikPss); pw.print(',');
874 pw.print(memInfo.otherPss); pw.print(',');
875 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700878 pw.print(memInfo.nativeSharedDirty); pw.print(',');
879 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
880 pw.print(memInfo.otherSharedDirty); pw.print(',');
881 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
882 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700885 pw.print(memInfo.nativePrivateDirty); pw.print(',');
886 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
887 pw.print(memInfo.otherPrivateDirty); pw.print(',');
888 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
889 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 // Object counts
892 pw.print(viewInstanceCount); pw.print(',');
893 pw.print(viewRootInstanceCount); pw.print(',');
894 pw.print(appContextInstanceCount); pw.print(',');
895 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 pw.print(globalAssetCount); pw.print(',');
898 pw.print(globalAssetManagerCount); pw.print(',');
899 pw.print(binderLocalObjectCount); pw.print(',');
900 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 pw.print(binderDeathObjectCount); pw.print(',');
903 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 // SQL
906 pw.print(sqliteAllocated); pw.print(',');
Vasu Noric3849202010-03-09 10:47:25 -0800907 pw.print(stats.memoryUsed / 1024); pw.print(',');
908 pw.print(stats.pageCacheOverflo / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700909 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800910 for (int i = 0; i < stats.dbStats.size(); i++) {
911 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700912 pw.print(','); pw.print(dbStats.dbName);
913 pw.print(','); pw.print(dbStats.pageSize);
914 pw.print(','); pw.print(dbStats.dbSize);
915 pw.print(','); pw.print(dbStats.lookaside);
916 pw.print(','); pw.print(dbStats.cache);
917 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800918 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700919 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700920
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700921 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 }
Bob Leee5408332009-09-04 18:31:17 -0700923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700925 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
926 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
927 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
928 "------");
929 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
930 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
931 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
932 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700934 int otherPss = memInfo.otherPss;
935 int otherSharedDirty = memInfo.otherSharedDirty;
936 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700938 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Romain Guy65b345f2011-07-27 18:51:50 -0700939 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700940 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
941 memInfo.getOtherPrivateDirty(i), "", "", "");
942 otherPss -= memInfo.getOtherPss(i);
943 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
944 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
945 }
946
947 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
948 otherPrivateDirty, "", "", "");
949 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
950 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
951 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
952 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953
954 pw.println(" ");
955 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -0700956 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 viewRootInstanceCount);
958
959 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
960 "Activities:", activityInstanceCount);
961
962 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
963 "AssetManagers:", globalAssetManagerCount);
964
965 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
966 "Proxy Binders:", binderProxyObjectCount);
967 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
968
969 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -0700970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 // SQLite mem info
972 pw.println(" ");
973 pw.println(" SQL");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700974 printRow(pw, TWO_COUNT_COLUMNS_DB, "heap:", sqliteAllocated, "MEMORY_USED:",
Vasu Noric3849202010-03-09 10:47:25 -0800975 stats.memoryUsed / 1024);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700976 printRow(pw, TWO_COUNT_COLUMNS_DB, "PAGECACHE_OVERFLOW:",
977 stats.pageCacheOverflo / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800978 pw.println(" ");
979 int N = stats.dbStats.size();
980 if (N > 0) {
981 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700982 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
983 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -0800984 for (int i = 0; i < N; i++) {
985 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700986 printRow(pw, DB_INFO_FORMAT,
987 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
988 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
989 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
990 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -0800991 }
992 }
Bob Leee5408332009-09-04 18:31:17 -0700993
Dianne Hackborn82e1ee92009-08-11 18:56:41 -0700994 // Asset details.
995 String assetAlloc = AssetManager.getAssetAllocations();
996 if (assetAlloc != null) {
997 pw.println(" ");
998 pw.println(" Asset Allocations");
999 pw.print(assetAlloc);
1000 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001001
1002 return memInfo;
1003 }
1004
1005 @Override
1006 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1007 dumpGraphicsInfo(fd);
Romain Guy65b345f2011-07-27 18:51:50 -07001008 WindowManagerImpl.getDefault().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 }
1010
1011 private void printRow(PrintWriter pw, String format, Object...objs) {
1012 pw.println(String.format(format, objs));
1013 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001014
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001015 public void setCoreSettings(Bundle coreSettings) {
1016 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001017 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001018
1019 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1020 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1021 ucd.pkg = pkg;
1022 ucd.info = info;
1023 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1024 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001025
1026 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001027 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 }
1030
Romain Guy65b345f2011-07-27 18:51:50 -07001031 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 public static final int LAUNCH_ACTIVITY = 100;
1033 public static final int PAUSE_ACTIVITY = 101;
1034 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1035 public static final int STOP_ACTIVITY_SHOW = 103;
1036 public static final int STOP_ACTIVITY_HIDE = 104;
1037 public static final int SHOW_WINDOW = 105;
1038 public static final int HIDE_WINDOW = 106;
1039 public static final int RESUME_ACTIVITY = 107;
1040 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001041 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 public static final int BIND_APPLICATION = 110;
1043 public static final int EXIT_APPLICATION = 111;
1044 public static final int NEW_INTENT = 112;
1045 public static final int RECEIVER = 113;
1046 public static final int CREATE_SERVICE = 114;
1047 public static final int SERVICE_ARGS = 115;
1048 public static final int STOP_SERVICE = 116;
1049 public static final int REQUEST_THUMBNAIL = 117;
1050 public static final int CONFIGURATION_CHANGED = 118;
1051 public static final int CLEAN_UP_CONTEXT = 119;
1052 public static final int GC_WHEN_IDLE = 120;
1053 public static final int BIND_SERVICE = 121;
1054 public static final int UNBIND_SERVICE = 122;
1055 public static final int DUMP_SERVICE = 123;
1056 public static final int LOW_MEMORY = 124;
1057 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1058 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001059 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001060 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001061 public static final int DESTROY_BACKUP_AGENT = 129;
1062 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001063 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001064 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001065 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001066 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001067 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001068 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001069 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001070 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001071 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001072 public static final int TRIM_MEMORY = 140;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001074 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 switch (code) {
1076 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1077 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1078 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1079 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1080 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1081 case SHOW_WINDOW: return "SHOW_WINDOW";
1082 case HIDE_WINDOW: return "HIDE_WINDOW";
1083 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1084 case SEND_RESULT: return "SEND_RESULT";
1085 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1086 case BIND_APPLICATION: return "BIND_APPLICATION";
1087 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1088 case NEW_INTENT: return "NEW_INTENT";
1089 case RECEIVER: return "RECEIVER";
1090 case CREATE_SERVICE: return "CREATE_SERVICE";
1091 case SERVICE_ARGS: return "SERVICE_ARGS";
1092 case STOP_SERVICE: return "STOP_SERVICE";
1093 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1094 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1095 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1096 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1097 case BIND_SERVICE: return "BIND_SERVICE";
1098 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1099 case DUMP_SERVICE: return "DUMP_SERVICE";
1100 case LOW_MEMORY: return "LOW_MEMORY";
1101 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1102 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001103 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001104 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1105 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001106 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001107 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001108 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001109 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001110 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001111 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001112 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001113 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001114 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001115 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001116 case TRIM_MEMORY: return "TRIM_MEMORY";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 }
1118 }
1119 return "(unknown)";
1120 }
1121 public void handleMessage(Message msg) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001122 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 switch (msg.what) {
1124 case LAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001125 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126
1127 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001128 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001129 handleLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 } break;
1131 case RELAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001132 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001133 handleRelaunchActivity(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 } break;
1135 case PAUSE_ACTIVITY:
1136 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001137 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 break;
1139 case PAUSE_ACTIVITY_FINISHING:
1140 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
1141 break;
1142 case STOP_ACTIVITY_SHOW:
1143 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
1144 break;
1145 case STOP_ACTIVITY_HIDE:
1146 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
1147 break;
1148 case SHOW_WINDOW:
1149 handleWindowVisibility((IBinder)msg.obj, true);
1150 break;
1151 case HIDE_WINDOW:
1152 handleWindowVisibility((IBinder)msg.obj, false);
1153 break;
1154 case RESUME_ACTIVITY:
1155 handleResumeActivity((IBinder)msg.obj, true,
1156 msg.arg1 != 0);
1157 break;
1158 case SEND_RESULT:
1159 handleSendResult((ResultData)msg.obj);
1160 break;
1161 case DESTROY_ACTIVITY:
1162 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1163 msg.arg2, false);
1164 break;
1165 case BIND_APPLICATION:
1166 AppBindData data = (AppBindData)msg.obj;
1167 handleBindApplication(data);
1168 break;
1169 case EXIT_APPLICATION:
1170 if (mInitialApplication != null) {
1171 mInitialApplication.onTerminate();
1172 }
1173 Looper.myLooper().quit();
1174 break;
1175 case NEW_INTENT:
1176 handleNewIntent((NewIntentData)msg.obj);
1177 break;
1178 case RECEIVER:
1179 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001180 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 break;
1182 case CREATE_SERVICE:
1183 handleCreateService((CreateServiceData)msg.obj);
1184 break;
1185 case BIND_SERVICE:
1186 handleBindService((BindServiceData)msg.obj);
1187 break;
1188 case UNBIND_SERVICE:
1189 handleUnbindService((BindServiceData)msg.obj);
1190 break;
1191 case SERVICE_ARGS:
1192 handleServiceArgs((ServiceArgsData)msg.obj);
1193 break;
1194 case STOP_SERVICE:
1195 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001196 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 break;
1198 case REQUEST_THUMBNAIL:
1199 handleRequestThumbnail((IBinder)msg.obj);
1200 break;
1201 case CONFIGURATION_CHANGED:
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001202 handleConfigurationChanged((Configuration)msg.obj, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 break;
1204 case CLEAN_UP_CONTEXT:
1205 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1206 cci.context.performFinalCleanup(cci.who, cci.what);
1207 break;
1208 case GC_WHEN_IDLE:
1209 scheduleGcIdler();
1210 break;
1211 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001212 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 break;
1214 case LOW_MEMORY:
1215 handleLowMemory();
1216 break;
1217 case ACTIVITY_CONFIGURATION_CHANGED:
1218 handleActivityConfigurationChanged((IBinder)msg.obj);
1219 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001220 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001221 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001222 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001223 case CREATE_BACKUP_AGENT:
1224 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
1225 break;
1226 case DESTROY_BACKUP_AGENT:
1227 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
1228 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001229 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001230 Process.killProcess(Process.myPid());
1231 break;
1232 case REMOVE_PROVIDER:
1233 completeRemoveProvider((IContentProvider)msg.obj);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001234 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001235 case ENABLE_JIT:
1236 ensureJitEnabled();
1237 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001238 case DISPATCH_PACKAGE_BROADCAST:
1239 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
1240 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001241 case SCHEDULE_CRASH:
1242 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001243 case DUMP_HEAP:
1244 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1245 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001246 case DUMP_ACTIVITY:
1247 handleDumpActivity((DumpComponentInfo)msg.obj);
1248 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001249 case SLEEPING:
1250 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
1251 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001252 case SET_CORE_SETTINGS:
1253 handleSetCoreSettings((Bundle) msg.obj);
1254 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001255 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1256 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001257 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001258 case TRIM_MEMORY:
1259 handleTrimMemory(msg.arg1);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001260 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07001262 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 }
Bob Leee5408332009-09-04 18:31:17 -07001264
Brian Carlstromed7e0072011-03-24 13:27:57 -07001265 private void maybeSnapshot() {
1266 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001267 // convert the *private* ActivityThread.PackageInfo to *public* known
1268 // android.content.pm.PackageInfo
1269 String packageName = mBoundApplication.info.mPackageName;
1270 android.content.pm.PackageInfo packageInfo = null;
1271 try {
1272 Context context = getSystemContext();
1273 if(context == null) {
1274 Log.e(TAG, "cannot get a valid context");
1275 return;
1276 }
1277 PackageManager pm = context.getPackageManager();
1278 if(pm == null) {
1279 Log.e(TAG, "cannot get a valid PackageManager");
1280 return;
1281 }
1282 packageInfo = pm.getPackageInfo(
1283 packageName, PackageManager.GET_ACTIVITIES);
1284 } catch (NameNotFoundException e) {
1285 Log.e(TAG, "cannot get package info for " + packageName, e);
1286 }
1287 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001288 }
1289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 }
1291
Romain Guy65b345f2011-07-27 18:51:50 -07001292 private class Idler implements MessageQueue.IdleHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001294 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001295 boolean stopProfiling = false;
Dianne Hackbornba24e4d2011-09-01 11:17:06 -07001296 if (mBoundApplication != null && mBoundApplication.profileFd != null
1297 && mBoundApplication.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001298 stopProfiling = true;
1299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 if (a != null) {
1301 mNewActivities = null;
1302 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001303 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001305 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 TAG, "Reporting idle of " + a +
1307 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001308 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 if (a.activity != null && !a.activity.mFinished) {
1310 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001311 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001312 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001314 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 }
1316 }
1317 prev = a;
1318 a = a.nextIdle;
1319 prev.nextIdle = null;
1320 } while (a != null);
1321 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001322 if (stopProfiling) {
1323 mBoundApplication.stopProfiling();
1324 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001325 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 return false;
1327 }
1328 }
1329
1330 final class GcIdler implements MessageQueue.IdleHandler {
1331 public final boolean queueIdle() {
1332 doGcIfNeeded();
1333 return false;
1334 }
1335 }
1336
Romain Guy65b345f2011-07-27 18:51:50 -07001337 private static class ResourcesKey {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001338 final private String mResDir;
1339 final private float mScale;
1340 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001341
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001342 ResourcesKey(String resDir, float scale) {
1343 mResDir = resDir;
1344 mScale = scale;
1345 mHash = mResDir.hashCode() << 2 + (int) (mScale * 2);
1346 }
Bob Leee5408332009-09-04 18:31:17 -07001347
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001348 @Override
1349 public int hashCode() {
1350 return mHash;
1351 }
1352
1353 @Override
1354 public boolean equals(Object obj) {
1355 if (!(obj instanceof ResourcesKey)) {
1356 return false;
1357 }
1358 ResourcesKey peer = (ResourcesKey) obj;
1359 return mResDir.equals(peer.mResDir) && mScale == peer.mScale;
1360 }
1361 }
1362
Romain Guy65b345f2011-07-27 18:51:50 -07001363 public static ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001364 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366
Romain Guy65b345f2011-07-27 18:51:50 -07001367 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001368 ActivityThread am = currentActivityThread();
1369 return (am != null && am.mBoundApplication != null)
1370 ? am.mBoundApplication.processName : null;
1371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372
Romain Guy65b345f2011-07-27 18:51:50 -07001373 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001374 ActivityThread am = currentActivityThread();
1375 return am != null ? am.mInitialApplication : null;
1376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001378 public static IPackageManager getPackageManager() {
1379 if (sPackageManager != null) {
1380 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1381 return sPackageManager;
1382 }
1383 IBinder b = ServiceManager.getService("package");
1384 //Slog.v("PackageManager", "default service binder = " + b);
1385 sPackageManager = IPackageManager.Stub.asInterface(b);
1386 //Slog.v("PackageManager", "default service = " + sPackageManager);
1387 return sPackageManager;
1388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001390 DisplayMetrics getDisplayMetricsLocked(CompatibilityInfo ci, boolean forceUpdate) {
1391 DisplayMetrics dm = mDisplayMetrics.get(ci);
1392 if (dm != null && !forceUpdate) {
1393 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001394 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001395 if (dm == null) {
1396 dm = new DisplayMetrics();
1397 mDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001398 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001399 Display d = WindowManagerImpl.getDefault(ci).getDefaultDisplay();
1400 d.getMetrics(dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001401 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1402 // + metrics.heightPixels + " den=" + metrics.density
1403 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001404 return dm;
1405 }
1406
1407 static Configuration applyConfigCompat(Configuration config, CompatibilityInfo compat) {
1408 if (config == null) {
1409 return null;
1410 }
1411 if (compat != null && !compat.supportsScreen()) {
1412 config = new Configuration(config);
1413 compat.applyToConfiguration(config);
1414 }
1415 return config;
1416 }
1417
Romain Guy65b345f2011-07-27 18:51:50 -07001418 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001419 Configuration applyConfigCompatMainThread(Configuration config, CompatibilityInfo compat) {
1420 if (config == null) {
1421 return null;
1422 }
1423 if (compat != null && !compat.supportsScreen()) {
1424 mMainThreadConfig.setTo(config);
1425 config = mMainThreadConfig;
1426 compat.applyToConfiguration(config);
1427 }
1428 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001429 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001431 /**
1432 * Creates the top level Resources for applications with the given compatibility info.
1433 *
1434 * @param resDir the resource directory.
1435 * @param compInfo the compability info. It will use the default compatibility info when it's
1436 * null.
1437 */
1438 Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) {
1439 ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale);
1440 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001442 // Resources is app scale dependent.
1443 if (false) {
1444 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1445 + compInfo.applicationScale);
1446 }
1447 WeakReference<Resources> wr = mActiveResources.get(key);
1448 r = wr != null ? wr.get() : null;
1449 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1450 if (r != null && r.getAssets().isUpToDate()) {
1451 if (false) {
1452 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1453 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1454 }
1455 return r;
1456 }
1457 }
1458
1459 //if (r != null) {
1460 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1461 // + r + " " + resDir);
1462 //}
1463
1464 AssetManager assets = new AssetManager();
1465 if (assets.addAssetPath(resDir) == 0) {
1466 return null;
1467 }
1468
1469 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001470 DisplayMetrics metrics = getDisplayMetricsLocked(compInfo, false);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001471 r = new Resources(assets, metrics, getConfiguration(), compInfo);
1472 if (false) {
1473 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1474 + r.getConfiguration() + " appScale="
1475 + r.getCompatibilityInfo().applicationScale);
1476 }
1477
1478 synchronized (mPackages) {
1479 WeakReference<Resources> wr = mActiveResources.get(key);
1480 Resources existing = wr != null ? wr.get() : null;
1481 if (existing != null && existing.getAssets().isUpToDate()) {
1482 // Someone else already created the resources while we were
1483 // unlocked; go ahead and use theirs.
1484 r.getAssets().close();
1485 return existing;
1486 }
1487
1488 // XXX need to remove entries when weak references go away
1489 mActiveResources.put(key, new WeakReference<Resources>(r));
1490 return r;
1491 }
1492 }
1493
1494 /**
1495 * Creates the top level resources for the given package.
1496 */
1497 Resources getTopLevelResources(String resDir, LoadedApk pkgInfo) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001498 return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001499 }
1500
1501 final Handler getHandler() {
1502 return mH;
1503 }
1504
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001505 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1506 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001507 synchronized (mPackages) {
1508 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1510 ref = mPackages.get(packageName);
1511 } else {
1512 ref = mResourcePackages.get(packageName);
1513 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001514 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001515 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001516 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1517 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 if (packageInfo != null && (packageInfo.mResources == null
1519 || packageInfo.mResources.getAssets().isUpToDate())) {
1520 if (packageInfo.isSecurityViolation()
1521 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1522 throw new SecurityException(
1523 "Requesting code from " + packageName
1524 + " to be run in process "
1525 + mBoundApplication.processName
1526 + "/" + mBoundApplication.appInfo.uid);
1527 }
1528 return packageInfo;
1529 }
1530 }
1531
1532 ApplicationInfo ai = null;
1533 try {
1534 ai = getPackageManager().getApplicationInfo(packageName,
1535 PackageManager.GET_SHARED_LIBRARY_FILES);
1536 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001537 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 }
1539
1540 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001541 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 }
1543
1544 return null;
1545 }
1546
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001547 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1548 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1550 boolean securityViolation = includeCode && ai.uid != 0
1551 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
1552 ? ai.uid != mBoundApplication.appInfo.uid : true);
1553 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1554 |Context.CONTEXT_IGNORE_SECURITY))
1555 == Context.CONTEXT_INCLUDE_CODE) {
1556 if (securityViolation) {
1557 String msg = "Requesting code from " + ai.packageName
1558 + " (with uid " + ai.uid + ")";
1559 if (mBoundApplication != null) {
1560 msg = msg + " to be run in process "
1561 + mBoundApplication.processName + " (with uid "
1562 + mBoundApplication.appInfo.uid + ")";
1563 }
1564 throw new SecurityException(msg);
1565 }
1566 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001567 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 }
1569
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001570 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1571 CompatibilityInfo compatInfo) {
1572 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 }
1574
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001575 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1576 synchronized (mPackages) {
1577 WeakReference<LoadedApk> ref;
1578 if (includeCode) {
1579 ref = mPackages.get(packageName);
1580 } else {
1581 ref = mResourcePackages.get(packageName);
1582 }
1583 return ref != null ? ref.get() : null;
1584 }
1585 }
1586
Romain Guy65b345f2011-07-27 18:51:50 -07001587 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1589 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001590 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 if (includeCode) {
1592 ref = mPackages.get(aInfo.packageName);
1593 } else {
1594 ref = mResourcePackages.get(aInfo.packageName);
1595 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001596 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 if (packageInfo == null || (packageInfo.mResources != null
1598 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001599 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 : "Loading resource-only package ") + aInfo.packageName
1601 + " (in " + (mBoundApplication != null
1602 ? mBoundApplication.processName : null)
1603 + ")");
1604 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001605 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 securityViolation, includeCode &&
1607 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1608 if (includeCode) {
1609 mPackages.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 } else {
1612 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001613 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 }
1615 }
1616 return packageInfo;
1617 }
1618 }
1619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 ActivityThread() {
1621 }
1622
1623 public ApplicationThread getApplicationThread()
1624 {
1625 return mAppThread;
1626 }
1627
1628 public Instrumentation getInstrumentation()
1629 {
1630 return mInstrumentation;
1631 }
1632
1633 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001634 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 }
1636
1637 public boolean isProfiling() {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001638 return mBoundApplication != null && mBoundApplication.profileFile != null
1639 && mBoundApplication.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 }
1641
1642 public String getProfileFilePath() {
1643 return mBoundApplication.profileFile;
1644 }
1645
1646 public Looper getLooper() {
1647 return mLooper;
1648 }
1649
1650 public Application getApplication() {
1651 return mInitialApplication;
1652 }
Bob Leee5408332009-09-04 18:31:17 -07001653
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001654 public String getProcessName() {
1655 return mBoundApplication.processName;
1656 }
Bob Leee5408332009-09-04 18:31:17 -07001657
Dianne Hackborn21556372010-02-04 16:34:40 -08001658 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 synchronized (this) {
1660 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001661 ContextImpl context =
1662 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001663 LoadedApk info = new LoadedApk(this, "android", context, null,
1664 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 context.init(info, null, this);
1666 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001667 getConfiguration(), getDisplayMetricsLocked(
1668 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001670 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 // + ": " + context.getResources().getConfiguration());
1672 }
1673 }
1674 return mSystemContext;
1675 }
1676
Mike Cleron432b7132009-09-24 15:28:29 -07001677 public void installSystemApplicationInfo(ApplicationInfo info) {
1678 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001679 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001680 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001681 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Mike Cleron432b7132009-09-24 15:28:29 -07001682 }
1683 }
1684
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001685 void ensureJitEnabled() {
1686 if (!mJitEnabled) {
1687 mJitEnabled = true;
1688 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1689 }
1690 }
1691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 void scheduleGcIdler() {
1693 if (!mGcIdlerScheduled) {
1694 mGcIdlerScheduled = true;
1695 Looper.myQueue().addIdleHandler(mGcIdler);
1696 }
1697 mH.removeMessages(H.GC_WHEN_IDLE);
1698 }
1699
1700 void unscheduleGcIdler() {
1701 if (mGcIdlerScheduled) {
1702 mGcIdlerScheduled = false;
1703 Looper.myQueue().removeIdleHandler(mGcIdler);
1704 }
1705 mH.removeMessages(H.GC_WHEN_IDLE);
1706 }
1707
1708 void doGcIfNeeded() {
1709 mGcIdlerScheduled = false;
1710 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001711 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 // + "m now=" + now);
1713 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001714 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 BinderInternal.forceGc("bg");
1716 }
1717 }
1718
Jeff Hamilton52d32032011-01-08 15:31:26 -06001719 public void registerOnActivityPausedListener(Activity activity,
1720 OnActivityPausedListener listener) {
1721 synchronized (mOnPauseListeners) {
1722 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1723 if (list == null) {
1724 list = new ArrayList<OnActivityPausedListener>();
1725 mOnPauseListeners.put(activity, list);
1726 }
1727 list.add(listener);
1728 }
1729 }
1730
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001731 public void unregisterOnActivityPausedListener(Activity activity,
1732 OnActivityPausedListener listener) {
1733 synchronized (mOnPauseListeners) {
1734 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1735 if (list != null) {
1736 list.remove(listener);
1737 }
1738 }
1739 }
1740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 public final ActivityInfo resolveActivityInfo(Intent intent) {
1742 ActivityInfo aInfo = intent.resolveActivityInfo(
1743 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1744 if (aInfo == null) {
1745 // Throw an exception.
1746 Instrumentation.checkStartActivityResult(
1747 IActivityManager.START_CLASS_NOT_FOUND, intent);
1748 }
1749 return aInfo;
1750 }
Bob Leee5408332009-09-04 18:31:17 -07001751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001754 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001755 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001757 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 r.intent = intent;
1759 r.state = state;
1760 r.parent = parent;
1761 r.embeddedID = id;
1762 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001763 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 if (localLOGV) {
1765 ComponentName compname = intent.getComponent();
1766 String name;
1767 if (compname != null) {
1768 name = compname.toShortString();
1769 } else {
1770 name = "(Intent " + intent + ").getComponent() returned null";
1771 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001772 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 + ", comp=" + name
1774 + ", token=" + token);
1775 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001776 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 }
1778
1779 public final Activity getActivity(IBinder token) {
1780 return mActivities.get(token).activity;
1781 }
1782
1783 public final void sendActivityResult(
1784 IBinder token, String id, int requestCode,
1785 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001786 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001787 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1789 list.add(new ResultInfo(id, requestCode, resultCode, data));
1790 mAppThread.scheduleSendResult(token, list);
1791 }
1792
1793 // if the thread hasn't started yet, we don't have the handler, so just
1794 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07001795 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 queueOrSendMessage(what, obj, 0, 0);
1797 }
1798
Romain Guy65b345f2011-07-27 18:51:50 -07001799 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 queueOrSendMessage(what, obj, arg1, 0);
1801 }
1802
Romain Guy65b345f2011-07-27 18:51:50 -07001803 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001805 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1807 + ": " + arg1 + " / " + obj);
1808 Message msg = Message.obtain();
1809 msg.what = what;
1810 msg.obj = obj;
1811 msg.arg1 = arg1;
1812 msg.arg2 = arg2;
1813 mH.sendMessage(msg);
1814 }
1815 }
1816
Dianne Hackborn21556372010-02-04 16:34:40 -08001817 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 String what) {
1819 ContextCleanupInfo cci = new ContextCleanupInfo();
1820 cci.context = context;
1821 cci.who = who;
1822 cci.what = what;
1823 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1824 }
1825
Romain Guy65b345f2011-07-27 18:51:50 -07001826 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1828
1829 ActivityInfo aInfo = r.activityInfo;
1830 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001831 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 Context.CONTEXT_INCLUDE_CODE);
1833 }
Bob Leee5408332009-09-04 18:31:17 -07001834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 ComponentName component = r.intent.getComponent();
1836 if (component == null) {
1837 component = r.intent.resolveActivity(
1838 mInitialApplication.getPackageManager());
1839 r.intent.setComponent(component);
1840 }
1841
1842 if (r.activityInfo.targetActivity != null) {
1843 component = new ComponentName(r.activityInfo.packageName,
1844 r.activityInfo.targetActivity);
1845 }
1846
1847 Activity activity = null;
1848 try {
1849 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
1850 activity = mInstrumentation.newActivity(
1851 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08001852 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 r.intent.setExtrasClassLoader(cl);
1854 if (r.state != null) {
1855 r.state.setClassLoader(cl);
1856 }
1857 } catch (Exception e) {
1858 if (!mInstrumentation.onException(activity, e)) {
1859 throw new RuntimeException(
1860 "Unable to instantiate activity " + component
1861 + ": " + e.toString(), e);
1862 }
1863 }
1864
1865 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001866 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001867
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001868 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
1869 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 TAG, r + ": app=" + app
1871 + ", appName=" + app.getPackageName()
1872 + ", pkg=" + r.packageInfo.getPackageName()
1873 + ", comp=" + r.intent.getComponent().toShortString()
1874 + ", dir=" + r.packageInfo.getAppDir());
1875
1876 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001877 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 appContext.init(r.packageInfo, r.token, this);
1879 appContext.setOuterContext(activity);
1880 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001881 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001882 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07001883 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001884 activity.attach(appContext, this, getInstrumentation(), r.token,
1885 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001886 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07001887
Christopher Tateb70f3df2009-04-07 16:07:59 -07001888 if (customIntent != null) {
1889 activity.mIntent = customIntent;
1890 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001891 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 activity.mStartedActivity = false;
1893 int theme = r.activityInfo.getThemeResource();
1894 if (theme != 0) {
1895 activity.setTheme(theme);
1896 }
1897
1898 activity.mCalled = false;
1899 mInstrumentation.callActivityOnCreate(activity, r.state);
1900 if (!activity.mCalled) {
1901 throw new SuperNotCalledException(
1902 "Activity " + r.intent.getComponent().toShortString() +
1903 " did not call through to super.onCreate()");
1904 }
1905 r.activity = activity;
1906 r.stopped = true;
1907 if (!r.activity.mFinished) {
1908 activity.performStart();
1909 r.stopped = false;
1910 }
1911 if (!r.activity.mFinished) {
1912 if (r.state != null) {
1913 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
1914 }
1915 }
1916 if (!r.activity.mFinished) {
1917 activity.mCalled = false;
1918 mInstrumentation.callActivityOnPostCreate(activity, r.state);
1919 if (!activity.mCalled) {
1920 throw new SuperNotCalledException(
1921 "Activity " + r.intent.getComponent().toShortString() +
1922 " did not call through to super.onPostCreate()");
1923 }
1924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 }
1926 r.paused = true;
1927
1928 mActivities.put(r.token, r);
1929
1930 } catch (SuperNotCalledException e) {
1931 throw e;
1932
1933 } catch (Exception e) {
1934 if (!mInstrumentation.onException(activity, e)) {
1935 throw new RuntimeException(
1936 "Unable to start activity " + component
1937 + ": " + e.toString(), e);
1938 }
1939 }
1940
1941 return activity;
1942 }
1943
Romain Guy65b345f2011-07-27 18:51:50 -07001944 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 // If we are getting ready to gc after going to the background, well
1946 // we are back active so skip it.
1947 unscheduleGcIdler();
1948
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001949 if (r.profileFd != null) {
1950 mBoundApplication.setProfiler(r.profileFile, r.profileFd);
1951 mBoundApplication.startProfiling();
1952 mBoundApplication.autoStopProfiler = r.autoStopProfiler;
1953 }
1954
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001955 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001957 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958
1959 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08001960 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001961 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 handleResumeActivity(r.token, false, r.isForward);
1963
1964 if (!r.activity.mFinished && r.startsNotResumed) {
1965 // The activity manager actually wants this one to start out
1966 // paused, because it needs to be visible but isn't in the
1967 // foreground. We accomplish this by going through the
1968 // normal startup (because activities expect to go through
1969 // onResume() the first time they run, before their window
1970 // is displayed), and then pausing it. However, in this case
1971 // we do -not- need to do the full pause cycle (of freezing
1972 // and such) because the activity manager assumes it can just
1973 // retain the current state it has.
1974 try {
1975 r.activity.mCalled = false;
1976 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001977 // We need to keep around the original state, in case
1978 // we need to be created again.
1979 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 if (!r.activity.mCalled) {
1981 throw new SuperNotCalledException(
1982 "Activity " + r.intent.getComponent().toShortString() +
1983 " did not call through to super.onPause()");
1984 }
1985
1986 } catch (SuperNotCalledException e) {
1987 throw e;
1988
1989 } catch (Exception e) {
1990 if (!mInstrumentation.onException(r.activity, e)) {
1991 throw new RuntimeException(
1992 "Unable to pause activity "
1993 + r.intent.getComponent().toShortString()
1994 + ": " + e.toString(), e);
1995 }
1996 }
1997 r.paused = true;
1998 }
1999 } else {
2000 // If there was an error, for any reason, tell the activity
2001 // manager to stop us.
2002 try {
2003 ActivityManagerNative.getDefault()
2004 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2005 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002006 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 }
2008 }
2009 }
2010
Romain Guy65b345f2011-07-27 18:51:50 -07002011 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 List<Intent> intents) {
2013 final int N = intents.size();
2014 for (int i=0; i<N; i++) {
2015 Intent intent = intents.get(i);
2016 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002017 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2019 }
2020 }
2021
2022 public final void performNewIntents(IBinder token,
2023 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002024 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 if (r != null) {
2026 final boolean resumed = !r.paused;
2027 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002028 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 mInstrumentation.callActivityOnPause(r.activity);
2030 }
2031 deliverNewIntents(r, intents);
2032 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002033 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002034 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 }
2036 }
2037 }
Bob Leee5408332009-09-04 18:31:17 -07002038
Romain Guy65b345f2011-07-27 18:51:50 -07002039 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 performNewIntents(data.token, data.intents);
2041 }
2042
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002043 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2044
2045 /**
2046 * Return the Intent that's currently being handled by a
2047 * BroadcastReceiver on this thread, or null if none.
2048 * @hide
2049 */
2050 public static Intent getIntentBeingBroadcast() {
2051 return sCurrentBroadcastIntent.get();
2052 }
2053
Romain Guy65b345f2011-07-27 18:51:50 -07002054 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 // If we are getting ready to gc after going to the background, well
2056 // we are back active so skip it.
2057 unscheduleGcIdler();
2058
2059 String component = data.intent.getComponent().getClassName();
2060
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002061 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002062 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063
2064 IActivityManager mgr = ActivityManagerNative.getDefault();
2065
Romain Guy65b345f2011-07-27 18:51:50 -07002066 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 try {
2068 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2069 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002070 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2072 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002073 if (DEBUG_BROADCAST) Slog.i(TAG,
2074 "Finishing failed broadcast to " + data.intent.getComponent());
2075 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 throw new RuntimeException(
2077 "Unable to instantiate receiver " + component
2078 + ": " + e.toString(), e);
2079 }
2080
2081 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002082 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002083
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002084 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 TAG, "Performing receive of " + data.intent
2086 + ": app=" + app
2087 + ", appName=" + app.getPackageName()
2088 + ", pkg=" + packageInfo.getPackageName()
2089 + ", comp=" + data.intent.getComponent().toShortString()
2090 + ", dir=" + packageInfo.getAppDir());
2091
Dianne Hackborn21556372010-02-04 16:34:40 -08002092 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002093 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002094 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 receiver.onReceive(context.getReceiverRestrictedContext(),
2096 data.intent);
2097 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002098 if (DEBUG_BROADCAST) Slog.i(TAG,
2099 "Finishing failed broadcast to " + data.intent.getComponent());
2100 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 if (!mInstrumentation.onException(receiver, e)) {
2102 throw new RuntimeException(
2103 "Unable to start receiver " + component
2104 + ": " + e.toString(), e);
2105 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002106 } finally {
2107 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 }
2109
Dianne Hackborne829fef2010-10-26 17:44:01 -07002110 if (receiver.getPendingResult() != null) {
2111 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 }
2113 }
2114
Christopher Tate181fafa2009-05-14 11:12:14 -07002115 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002116 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002117 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002118
2119 // no longer idle; we have backup work to do
2120 unscheduleGcIdler();
2121
2122 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002123 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002124 String packageName = packageInfo.mPackageName;
2125 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002126 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002127 + " already exists");
2128 return;
2129 }
Bob Leee5408332009-09-04 18:31:17 -07002130
Christopher Tate181fafa2009-05-14 11:12:14 -07002131 BackupAgent agent = null;
2132 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002133
Christopher Tate79ec80d2011-06-24 14:58:49 -07002134 // full backup operation but no app-supplied agent? use the default implementation
2135 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2136 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002137 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002138 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002139
Christopher Tate181fafa2009-05-14 11:12:14 -07002140 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002141 IBinder binder = null;
2142 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002143 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2144
Christopher Tated1475e02009-07-09 15:36:17 -07002145 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002146 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002147
2148 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002149 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002150 context.init(packageInfo, null, this);
2151 context.setOuterContext(agent);
2152 agent.attach(context);
2153
2154 agent.onCreate();
2155 binder = agent.onBind();
2156 mBackupAgents.put(packageName, agent);
2157 } catch (Exception e) {
2158 // If this is during restore, fail silently; otherwise go
2159 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002160 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002161 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2162 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002163 throw e;
2164 }
2165 // falling through with 'binder' still null
2166 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002167
2168 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002169 try {
2170 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2171 } catch (RemoteException e) {
2172 // nothing to do.
2173 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002174 } catch (Exception e) {
2175 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002176 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002177 }
2178 }
2179
2180 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002181 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002182 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002183
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002184 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002185 String packageName = packageInfo.mPackageName;
2186 BackupAgent agent = mBackupAgents.get(packageName);
2187 if (agent != null) {
2188 try {
2189 agent.onDestroy();
2190 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002191 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002192 e.printStackTrace();
2193 }
2194 mBackupAgents.remove(packageName);
2195 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002196 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002197 }
2198 }
2199
Romain Guy65b345f2011-07-27 18:51:50 -07002200 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 // If we are getting ready to gc after going to the background, well
2202 // we are back active so skip it.
2203 unscheduleGcIdler();
2204
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002205 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002206 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 Service service = null;
2208 try {
2209 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2210 service = (Service) cl.loadClass(data.info.name).newInstance();
2211 } catch (Exception e) {
2212 if (!mInstrumentation.onException(service, e)) {
2213 throw new RuntimeException(
2214 "Unable to instantiate service " + data.info.name
2215 + ": " + e.toString(), e);
2216 }
2217 }
2218
2219 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002220 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221
Dianne Hackborn21556372010-02-04 16:34:40 -08002222 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 context.init(packageInfo, null, this);
2224
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002225 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 context.setOuterContext(service);
2227 service.attach(context, this, data.info.name, data.token, app,
2228 ActivityManagerNative.getDefault());
2229 service.onCreate();
2230 mServices.put(data.token, service);
2231 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002232 ActivityManagerNative.getDefault().serviceDoneExecuting(
2233 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 } catch (RemoteException e) {
2235 // nothing to do.
2236 }
2237 } catch (Exception e) {
2238 if (!mInstrumentation.onException(service, e)) {
2239 throw new RuntimeException(
2240 "Unable to create service " + data.info.name
2241 + ": " + e.toString(), e);
2242 }
2243 }
2244 }
2245
Romain Guy65b345f2011-07-27 18:51:50 -07002246 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 Service s = mServices.get(data.token);
2248 if (s != null) {
2249 try {
2250 data.intent.setExtrasClassLoader(s.getClassLoader());
2251 try {
2252 if (!data.rebind) {
2253 IBinder binder = s.onBind(data.intent);
2254 ActivityManagerNative.getDefault().publishService(
2255 data.token, data.intent, binder);
2256 } else {
2257 s.onRebind(data.intent);
2258 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002259 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002261 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 } catch (RemoteException ex) {
2263 }
2264 } catch (Exception e) {
2265 if (!mInstrumentation.onException(s, e)) {
2266 throw new RuntimeException(
2267 "Unable to bind to service " + s
2268 + " with " + data.intent + ": " + e.toString(), e);
2269 }
2270 }
2271 }
2272 }
2273
Romain Guy65b345f2011-07-27 18:51:50 -07002274 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 Service s = mServices.get(data.token);
2276 if (s != null) {
2277 try {
2278 data.intent.setExtrasClassLoader(s.getClassLoader());
2279 boolean doRebind = s.onUnbind(data.intent);
2280 try {
2281 if (doRebind) {
2282 ActivityManagerNative.getDefault().unbindFinished(
2283 data.token, data.intent, doRebind);
2284 } else {
2285 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002286 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 }
2288 } catch (RemoteException ex) {
2289 }
2290 } catch (Exception e) {
2291 if (!mInstrumentation.onException(s, e)) {
2292 throw new RuntimeException(
2293 "Unable to unbind to service " + s
2294 + " with " + data.intent + ": " + e.toString(), e);
2295 }
2296 }
2297 }
2298 }
2299
Dianne Hackborn625ac272010-09-17 18:29:22 -07002300 private void handleDumpService(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002301 Service s = mServices.get(info.token);
2302 if (s != null) {
2303 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2304 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2305 pw.flush();
2306 try {
2307 info.fd.close();
2308 } catch (IOException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 }
2310 }
2311 }
2312
Dianne Hackborn625ac272010-09-17 18:29:22 -07002313 private void handleDumpActivity(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002314 ActivityClientRecord r = mActivities.get(info.token);
2315 if (r != null && r.activity != null) {
2316 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2317 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2318 pw.flush();
2319 try {
2320 info.fd.close();
2321 } catch (IOException e) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07002322 }
2323 }
2324 }
2325
Romain Guy65b345f2011-07-27 18:51:50 -07002326 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 Service s = mServices.get(data.token);
2328 if (s != null) {
2329 try {
2330 if (data.args != null) {
2331 data.args.setExtrasClassLoader(s.getClassLoader());
2332 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002333 int res;
2334 if (!data.taskRemoved) {
2335 res = s.onStartCommand(data.args, data.flags, data.startId);
2336 } else {
2337 s.onTaskRemoved(data.args);
2338 res = Service.START_TASK_REMOVED_COMPLETE;
2339 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002340
2341 QueuedWork.waitToFinish();
2342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002344 ActivityManagerNative.getDefault().serviceDoneExecuting(
2345 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 } catch (RemoteException e) {
2347 // nothing to do.
2348 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002349 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 } catch (Exception e) {
2351 if (!mInstrumentation.onException(s, e)) {
2352 throw new RuntimeException(
2353 "Unable to start service " + s
2354 + " with " + data.args + ": " + e.toString(), e);
2355 }
2356 }
2357 }
2358 }
2359
Romain Guy65b345f2011-07-27 18:51:50 -07002360 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 Service s = mServices.remove(token);
2362 if (s != null) {
2363 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002364 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 s.onDestroy();
2366 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002367 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002369 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002371
2372 QueuedWork.waitToFinish();
2373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002375 ActivityManagerNative.getDefault().serviceDoneExecuting(
2376 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 } catch (RemoteException e) {
2378 // nothing to do.
2379 }
2380 } catch (Exception e) {
2381 if (!mInstrumentation.onException(s, e)) {
2382 throw new RuntimeException(
2383 "Unable to stop service " + s
2384 + ": " + e.toString(), e);
2385 }
2386 }
2387 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002388 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 }
2390
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002391 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002393 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002394 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 + " finished=" + r.activity.mFinished);
2396 if (r != null && !r.activity.mFinished) {
2397 if (clearHide) {
2398 r.hideForNow = false;
2399 r.activity.mStartedActivity = false;
2400 }
2401 try {
2402 if (r.pendingIntents != null) {
2403 deliverNewIntents(r, r.pendingIntents);
2404 r.pendingIntents = null;
2405 }
2406 if (r.pendingResults != null) {
2407 deliverResults(r, r.pendingResults);
2408 r.pendingResults = null;
2409 }
2410 r.activity.performResume();
2411
Bob Leee5408332009-09-04 18:31:17 -07002412 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 r.paused = false;
2416 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 r.state = null;
2418 } catch (Exception e) {
2419 if (!mInstrumentation.onException(r.activity, e)) {
2420 throw new RuntimeException(
2421 "Unable to resume activity "
2422 + r.intent.getComponent().toShortString()
2423 + ": " + e.toString(), e);
2424 }
2425 }
2426 }
2427 return r;
2428 }
2429
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002430 final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
2431 if (r.mPendingRemoveWindow != null) {
2432 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2433 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2434 if (wtoken != null) {
2435 WindowManagerImpl.getDefault().closeAll(wtoken,
2436 r.activity.getClass().getName(), "Activity");
2437 }
2438 }
2439 r.mPendingRemoveWindow = null;
2440 r.mPendingRemoveWindowManager = null;
2441 }
2442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2444 // If we are getting ready to gc after going to the background, well
2445 // we are back active so skip it.
2446 unscheduleGcIdler();
2447
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002448 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449
2450 if (r != null) {
2451 final Activity a = r.activity;
2452
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002453 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 TAG, "Resume " + r + " started activity: " +
2455 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2456 + ", finished: " + a.mFinished);
2457
2458 final int forwardBit = isForward ?
2459 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 // If the window hasn't yet been added to the window manager,
2462 // and this guy didn't finish itself or start another activity,
2463 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002464 boolean willBeVisible = !a.mStartedActivity;
2465 if (!willBeVisible) {
2466 try {
2467 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2468 a.getActivityToken());
2469 } catch (RemoteException e) {
2470 }
2471 }
2472 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 r.window = r.activity.getWindow();
2474 View decor = r.window.getDecorView();
2475 decor.setVisibility(View.INVISIBLE);
2476 ViewManager wm = a.getWindowManager();
2477 WindowManager.LayoutParams l = r.window.getAttributes();
2478 a.mDecor = decor;
2479 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2480 l.softInputMode |= forwardBit;
2481 if (a.mVisibleFromClient) {
2482 a.mWindowAdded = true;
2483 wm.addView(decor, l);
2484 }
2485
2486 // If the window has already been added, but during resume
2487 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002488 // window visible.
2489 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002490 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 TAG, "Launch " + r + " mStartedActivity set");
2492 r.hideForNow = true;
2493 }
2494
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002495 // Get rid of anything left hanging around.
2496 cleanUpPendingRemoveWindows(r);
2497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 // The window is now visible if it has been added, we are not
2499 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002500 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002501 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002503 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002504 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 performConfigurationChanged(r.activity, r.newConfig);
2506 r.newConfig = null;
2507 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002508 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 + isForward);
2510 WindowManager.LayoutParams l = r.window.getAttributes();
2511 if ((l.softInputMode
2512 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2513 != forwardBit) {
2514 l.softInputMode = (l.softInputMode
2515 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2516 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002517 if (r.activity.mVisibleFromClient) {
2518 ViewManager wm = a.getWindowManager();
2519 View decor = r.window.getDecorView();
2520 wm.updateViewLayout(decor, l);
2521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 }
2523 r.activity.mVisibleFromServer = true;
2524 mNumVisibleActivities++;
2525 if (r.activity.mVisibleFromClient) {
2526 r.activity.makeVisible();
2527 }
2528 }
2529
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002530 if (!r.onlyLocalRequest) {
2531 r.nextIdle = mNewActivities;
2532 mNewActivities = r;
2533 if (localLOGV) Slog.v(
2534 TAG, "Scheduling idle handler for " + r);
2535 Looper.myQueue().addIdleHandler(new Idler());
2536 }
2537 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538
2539 } else {
2540 // If an exception was thrown when trying to resume, then
2541 // just end this activity.
2542 try {
2543 ActivityManagerNative.getDefault()
2544 .finishActivity(token, Activity.RESULT_CANCELED, null);
2545 } catch (RemoteException ex) {
2546 }
2547 }
2548 }
2549
2550 private int mThumbnailWidth = -1;
2551 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002552 private Bitmap mAvailThumbnailBitmap = null;
2553 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554
Romain Guy65b345f2011-07-27 18:51:50 -07002555 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002556 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002558 if (thumbnail == null) {
2559 int w = mThumbnailWidth;
2560 int h;
2561 if (w < 0) {
2562 Resources res = r.activity.getResources();
2563 mThumbnailHeight = h =
2564 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002566 mThumbnailWidth = w =
2567 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2568 } else {
2569 h = mThumbnailHeight;
2570 }
2571
2572 // On platforms where we don't want thumbnails, set dims to (0,0)
2573 if ((w > 0) && (h > 0)) {
2574 thumbnail = Bitmap.createBitmap(w, h, THUMBNAIL_FORMAT);
2575 thumbnail.eraseColor(0);
2576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 }
2578
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002579 if (thumbnail != null) {
2580 Canvas cv = mThumbnailCanvas;
2581 if (cv == null) {
2582 mThumbnailCanvas = cv = new Canvas();
2583 }
2584
2585 cv.setBitmap(thumbnail);
2586 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2587 mAvailThumbnailBitmap = thumbnail;
2588 thumbnail = null;
2589 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002590 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 } catch (Exception e) {
2594 if (!mInstrumentation.onException(r.activity, e)) {
2595 throw new RuntimeException(
2596 "Unable to create thumbnail of "
2597 + r.intent.getComponent().toShortString()
2598 + ": " + e.toString(), e);
2599 }
2600 thumbnail = null;
2601 }
2602
2603 return thumbnail;
2604 }
2605
Romain Guy65b345f2011-07-27 18:51:50 -07002606 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002608 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002610 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 if (userLeaving) {
2612 performUserLeavingActivity(r);
2613 }
Bob Leee5408332009-09-04 18:31:17 -07002614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002616 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002618 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002619 if (r.isPreHoneycomb()) {
2620 QueuedWork.waitToFinish();
2621 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 // Tell the activity manager we have paused.
2624 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002625 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 } catch (RemoteException ex) {
2627 }
2628 }
2629 }
2630
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002631 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 mInstrumentation.callActivityOnUserLeaving(r.activity);
2633 }
2634
2635 final Bundle performPauseActivity(IBinder token, boolean finished,
2636 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002637 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 return r != null ? performPauseActivity(r, finished, saveState) : null;
2639 }
2640
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002641 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 boolean saveState) {
2643 if (r.paused) {
2644 if (r.activity.mFinished) {
2645 // If we are finishing, we won't call onResume() in certain cases.
2646 // So here we likewise don't want to call onPause() if the activity
2647 // isn't resumed.
2648 return null;
2649 }
2650 RuntimeException e = new RuntimeException(
2651 "Performing pause of activity that is not resumed: "
2652 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002653 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 }
2655 Bundle state = null;
2656 if (finished) {
2657 r.activity.mFinished = true;
2658 }
2659 try {
2660 // Next have the activity save its current state and managed dialogs...
2661 if (!r.activity.mFinished && saveState) {
2662 state = new Bundle();
2663 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2664 r.state = state;
2665 }
2666 // Now we are idle.
2667 r.activity.mCalled = false;
2668 mInstrumentation.callActivityOnPause(r.activity);
2669 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2670 if (!r.activity.mCalled) {
2671 throw new SuperNotCalledException(
2672 "Activity " + r.intent.getComponent().toShortString() +
2673 " did not call through to super.onPause()");
2674 }
2675
2676 } catch (SuperNotCalledException e) {
2677 throw e;
2678
2679 } catch (Exception e) {
2680 if (!mInstrumentation.onException(r.activity, e)) {
2681 throw new RuntimeException(
2682 "Unable to pause activity "
2683 + r.intent.getComponent().toShortString()
2684 + ": " + e.toString(), e);
2685 }
2686 }
2687 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002688
2689 // Notify any outstanding on paused listeners
2690 ArrayList<OnActivityPausedListener> listeners;
2691 synchronized (mOnPauseListeners) {
2692 listeners = mOnPauseListeners.remove(r.activity);
2693 }
2694 int size = (listeners != null ? listeners.size() : 0);
2695 for (int i = 0; i < size; i++) {
2696 listeners.get(i).onPaused(r.activity);
2697 }
2698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 return state;
2700 }
2701
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002702 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002703 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002704 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 }
2706
2707 private static class StopInfo {
2708 Bitmap thumbnail;
2709 CharSequence description;
2710 }
2711
Romain Guy65b345f2011-07-27 18:51:50 -07002712 private class ProviderRefCount {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 public int count;
2714 ProviderRefCount(int pCount) {
2715 count = pCount;
2716 }
2717 }
2718
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002719 /**
2720 * Core implementation of stopping an activity. Note this is a little
2721 * tricky because the server's meaning of stop is slightly different
2722 * than our client -- for the server, stop means to save state and give
2723 * it the result when it is done, but the window may still be visible.
2724 * For the client, we want to call onStop()/onStart() to indicate when
2725 * the activity's UI visibillity changes.
2726 */
Romain Guy65b345f2011-07-27 18:51:50 -07002727 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002728 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002729 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002730 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 if (r != null) {
2732 if (!keepShown && r.stopped) {
2733 if (r.activity.mFinished) {
2734 // If we are finishing, we won't call onResume() in certain
2735 // cases. So here we likewise don't want to call onStop()
2736 // if the activity isn't resumed.
2737 return;
2738 }
2739 RuntimeException e = new RuntimeException(
2740 "Performing stop of activity that is not resumed: "
2741 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002742 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 }
2744
2745 if (info != null) {
2746 try {
2747 // First create a thumbnail for the activity...
Jim Miller0b2a6d02010-07-13 18:01:29 -07002748 info.thumbnail = createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 info.description = r.activity.onCreateDescription();
2750 } catch (Exception e) {
2751 if (!mInstrumentation.onException(r.activity, e)) {
2752 throw new RuntimeException(
2753 "Unable to save state of activity "
2754 + r.intent.getComponent().toShortString()
2755 + ": " + e.toString(), e);
2756 }
2757 }
2758 }
2759
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002760 // Next have the activity save its current state and managed dialogs...
2761 if (!r.activity.mFinished && saveState) {
2762 if (r.state == null) {
2763 state = new Bundle();
2764 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2765 r.state = state;
2766 } else {
2767 state = r.state;
2768 }
2769 }
2770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 if (!keepShown) {
2772 try {
2773 // Now we are idle.
2774 r.activity.performStop();
2775 } catch (Exception e) {
2776 if (!mInstrumentation.onException(r.activity, e)) {
2777 throw new RuntimeException(
2778 "Unable to stop activity "
2779 + r.intent.getComponent().toShortString()
2780 + ": " + e.toString(), e);
2781 }
2782 }
2783 r.stopped = true;
2784 }
2785
2786 r.paused = true;
2787 }
2788 }
2789
Romain Guy65b345f2011-07-27 18:51:50 -07002790 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 View v = r.activity.mDecor;
2792 if (v != null) {
2793 if (show) {
2794 if (!r.activity.mVisibleFromServer) {
2795 r.activity.mVisibleFromServer = true;
2796 mNumVisibleActivities++;
2797 if (r.activity.mVisibleFromClient) {
2798 r.activity.makeVisible();
2799 }
2800 }
2801 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002802 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002803 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 performConfigurationChanged(r.activity, r.newConfig);
2805 r.newConfig = null;
2806 }
2807 } else {
2808 if (r.activity.mVisibleFromServer) {
2809 r.activity.mVisibleFromServer = false;
2810 mNumVisibleActivities--;
2811 v.setVisibility(View.INVISIBLE);
2812 }
2813 }
2814 }
2815 }
2816
Romain Guy65b345f2011-07-27 18:51:50 -07002817 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002818 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 r.activity.mConfigChangeFlags |= configChanges;
2820
2821 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002822 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002824 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 TAG, "Finishing stop of " + r + ": show=" + show
2826 + " win=" + r.window);
2827
2828 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07002829
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002830 // Make sure any pending writes are now committed.
2831 if (!r.isPreHoneycomb()) {
2832 QueuedWork.waitToFinish();
2833 }
2834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 // Tell activity manager we have been stopped.
2836 try {
2837 ActivityManagerNative.getDefault().activityStopped(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002838 r.token, r.state, info.thumbnail, info.description);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 } catch (RemoteException ex) {
2840 }
2841 }
2842
2843 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002844 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 if (r.stopped) {
2846 r.activity.performRestart();
2847 r.stopped = false;
2848 }
2849 }
2850
Romain Guy65b345f2011-07-27 18:51:50 -07002851 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002852 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08002853
2854 if (r == null) {
2855 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
2856 return;
2857 }
2858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002860 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 } else if (show && r.stopped) {
2862 // If we are getting ready to gc after going to the background, well
2863 // we are back active so skip it.
2864 unscheduleGcIdler();
2865
2866 r.activity.performRestart();
2867 r.stopped = false;
2868 }
2869 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07002870 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 TAG, "Handle window " + r + " visibility: " + show);
2872 updateVisibility(r, show);
2873 }
2874 }
2875
Romain Guy65b345f2011-07-27 18:51:50 -07002876 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002877 ActivityClientRecord r = mActivities.get(token);
2878
2879 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002880 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002881 return;
2882 }
2883
2884 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002885 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002886 try {
2887 // Now we are idle.
2888 r.activity.performStop();
2889 } catch (Exception e) {
2890 if (!mInstrumentation.onException(r.activity, e)) {
2891 throw new RuntimeException(
2892 "Unable to stop activity "
2893 + r.intent.getComponent().toShortString()
2894 + ": " + e.toString(), e);
2895 }
2896 }
2897 r.stopped = true;
2898 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002899
2900 // Make sure any pending writes are now committed.
2901 if (!r.isPreHoneycomb()) {
2902 QueuedWork.waitToFinish();
2903 }
2904
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002905 // Tell activity manager we slept.
2906 try {
2907 ActivityManagerNative.getDefault().activitySlept(r.token);
2908 } catch (RemoteException ex) {
2909 }
2910 } else {
2911 if (r.stopped && r.activity.mVisibleFromServer) {
2912 r.activity.performRestart();
2913 r.stopped = false;
2914 }
2915 }
2916 }
2917
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002918 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08002919 synchronized (mPackages) {
2920 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002921 }
2922 }
2923
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002924 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
2925 LoadedApk apk = peekPackageInfo(data.pkg, false);
2926 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002927 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002928 }
2929 apk = peekPackageInfo(data.pkg, true);
2930 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002931 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002932 }
2933 handleConfigurationChanged(mConfiguration, data.info);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002934 WindowManagerImpl.getDefault().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002935 }
2936
Romain Guy65b345f2011-07-27 18:51:50 -07002937 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 final int N = results.size();
2939 for (int i=0; i<N; i++) {
2940 ResultInfo ri = results.get(i);
2941 try {
2942 if (ri.mData != null) {
2943 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
2944 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002945 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07002946 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 r.activity.dispatchActivityResult(ri.mResultWho,
2948 ri.mRequestCode, ri.mResultCode, ri.mData);
2949 } catch (Exception e) {
2950 if (!mInstrumentation.onException(r.activity, e)) {
2951 throw new RuntimeException(
2952 "Failure delivering result " + ri + " to activity "
2953 + r.intent.getComponent().toShortString()
2954 + ": " + e.toString(), e);
2955 }
2956 }
2957 }
2958 }
2959
Romain Guy65b345f2011-07-27 18:51:50 -07002960 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002961 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002962 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 if (r != null) {
2964 final boolean resumed = !r.paused;
2965 if (!r.activity.mFinished && r.activity.mDecor != null
2966 && r.hideForNow && resumed) {
2967 // We had hidden the activity because it started another
2968 // one... we have gotten a result back and we are not
2969 // paused, so make sure our window is visible.
2970 updateVisibility(r, true);
2971 }
2972 if (resumed) {
2973 try {
2974 // Now we are idle.
2975 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002976 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 mInstrumentation.callActivityOnPause(r.activity);
2978 if (!r.activity.mCalled) {
2979 throw new SuperNotCalledException(
2980 "Activity " + r.intent.getComponent().toShortString()
2981 + " did not call through to super.onPause()");
2982 }
2983 } catch (SuperNotCalledException e) {
2984 throw e;
2985 } catch (Exception e) {
2986 if (!mInstrumentation.onException(r.activity, e)) {
2987 throw new RuntimeException(
2988 "Unable to pause activity "
2989 + r.intent.getComponent().toShortString()
2990 + ": " + e.toString(), e);
2991 }
2992 }
2993 }
2994 deliverResults(r, res.results);
2995 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002996 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002997 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 }
2999 }
3000 }
3001
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003002 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 return performDestroyActivity(token, finishing, 0, false);
3004 }
3005
Romain Guy65b345f2011-07-27 18:51:50 -07003006 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003008 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003009 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003010 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003012 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 r.activity.mConfigChangeFlags |= configChanges;
3014 if (finishing) {
3015 r.activity.mFinished = true;
3016 }
3017 if (!r.paused) {
3018 try {
3019 r.activity.mCalled = false;
3020 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07003021 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 r.activity.getComponentName().getClassName());
3023 if (!r.activity.mCalled) {
3024 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003025 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 + " did not call through to super.onPause()");
3027 }
3028 } catch (SuperNotCalledException e) {
3029 throw e;
3030 } catch (Exception e) {
3031 if (!mInstrumentation.onException(r.activity, e)) {
3032 throw new RuntimeException(
3033 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003034 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 + ": " + e.toString(), e);
3036 }
3037 }
3038 r.paused = true;
3039 }
3040 if (!r.stopped) {
3041 try {
3042 r.activity.performStop();
3043 } catch (SuperNotCalledException e) {
3044 throw e;
3045 } catch (Exception e) {
3046 if (!mInstrumentation.onException(r.activity, e)) {
3047 throw new RuntimeException(
3048 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003049 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 + ": " + e.toString(), e);
3051 }
3052 }
3053 r.stopped = true;
3054 }
3055 if (getNonConfigInstance) {
3056 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003057 r.lastNonConfigurationInstances
3058 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 } catch (Exception e) {
3060 if (!mInstrumentation.onException(r.activity, e)) {
3061 throw new RuntimeException(
3062 "Unable to retain activity "
3063 + r.intent.getComponent().toShortString()
3064 + ": " + e.toString(), e);
3065 }
3066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 }
3068 try {
3069 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003070 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 if (!r.activity.mCalled) {
3072 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003073 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 " did not call through to super.onDestroy()");
3075 }
3076 if (r.window != null) {
3077 r.window.closeAllPanels();
3078 }
3079 } catch (SuperNotCalledException e) {
3080 throw e;
3081 } catch (Exception e) {
3082 if (!mInstrumentation.onException(r.activity, e)) {
3083 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003084 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3085 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 }
3087 }
3088 }
3089 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003090 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 return r;
3092 }
3093
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003094 private static String safeToComponentShortString(Intent intent) {
3095 ComponentName component = intent.getComponent();
3096 return component == null ? "[Unknown]" : component.toShortString();
3097 }
3098
Romain Guy65b345f2011-07-27 18:51:50 -07003099 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003101 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 configChanges, getNonConfigInstance);
3103 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003104 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003105 WindowManager wm = r.activity.getWindowManager();
3106 View v = r.activity.mDecor;
3107 if (v != null) {
3108 if (r.activity.mVisibleFromServer) {
3109 mNumVisibleActivities--;
3110 }
3111 IBinder wtoken = v.getWindowToken();
3112 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003113 if (r.onlyLocalRequest) {
3114 // Hold off on removing this until the new activity's
3115 // window is being added.
3116 r.mPendingRemoveWindow = v;
3117 r.mPendingRemoveWindowManager = wm;
3118 } else {
3119 wm.removeViewImmediate(v);
3120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003122 if (wtoken != null && r.mPendingRemoveWindow == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 WindowManagerImpl.getDefault().closeAll(wtoken,
3124 r.activity.getClass().getName(), "Activity");
3125 }
3126 r.activity.mDecor = null;
3127 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003128 if (r.mPendingRemoveWindow == null) {
3129 // If we are delaying the removal of the activity window, then
3130 // we can't clean up all windows here. Note that we can't do
3131 // so later either, which means any windows that aren't closed
3132 // by the app will leak. Well we try to warning them a lot
3133 // about leaking windows, because that is a bug, so if they are
3134 // using this recreate facility then they get to live with leaks.
3135 WindowManagerImpl.getDefault().closeAll(token,
3136 r.activity.getClass().getName(), "Activity");
3137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138
3139 // Mocked out contexts won't be participating in the normal
3140 // process lifecycle, but if we're running with a proper
3141 // ApplicationContext we need to have it tear down things
3142 // cleanly.
3143 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003144 if (c instanceof ContextImpl) {
3145 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146 r.activity.getClass().getName(), "Activity");
3147 }
3148 }
3149 if (finishing) {
3150 try {
3151 ActivityManagerNative.getDefault().activityDestroyed(token);
3152 } catch (RemoteException ex) {
3153 // If the system process has died, it's game over for everyone.
3154 }
3155 }
3156 }
3157
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003158 public final void requestRelaunchActivity(IBinder token,
3159 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3160 int configChanges, boolean notResumed, Configuration config,
3161 boolean fromServer) {
3162 ActivityClientRecord target = null;
3163
3164 synchronized (mPackages) {
3165 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3166 ActivityClientRecord r = mRelaunchingActivities.get(i);
3167 if (r.token == token) {
3168 target = r;
3169 if (pendingResults != null) {
3170 if (r.pendingResults != null) {
3171 r.pendingResults.addAll(pendingResults);
3172 } else {
3173 r.pendingResults = pendingResults;
3174 }
3175 }
3176 if (pendingNewIntents != null) {
3177 if (r.pendingIntents != null) {
3178 r.pendingIntents.addAll(pendingNewIntents);
3179 } else {
3180 r.pendingIntents = pendingNewIntents;
3181 }
3182 }
3183 break;
3184 }
3185 }
3186
3187 if (target == null) {
3188 target = new ActivityClientRecord();
3189 target.token = token;
3190 target.pendingResults = pendingResults;
3191 target.pendingIntents = pendingNewIntents;
3192 if (!fromServer) {
3193 ActivityClientRecord existing = mActivities.get(token);
3194 if (existing != null) {
3195 target.startsNotResumed = existing.paused;
3196 }
3197 target.onlyLocalRequest = true;
3198 }
3199 mRelaunchingActivities.add(target);
3200 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3201 }
3202
3203 if (fromServer) {
3204 target.startsNotResumed = notResumed;
3205 target.onlyLocalRequest = false;
3206 }
3207 if (config != null) {
3208 target.createdConfig = config;
3209 }
3210 target.pendingConfigChanges |= configChanges;
3211 }
3212 }
3213
Romain Guy65b345f2011-07-27 18:51:50 -07003214 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 // If we are getting ready to gc after going to the background, well
3216 // we are back active so skip it.
3217 unscheduleGcIdler();
3218
3219 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003220 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 // First: make sure we have the most recent configuration and most
3223 // recent version of the activity, or skip it if some previous call
3224 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003225 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 int N = mRelaunchingActivities.size();
3227 IBinder token = tmp.token;
3228 tmp = null;
3229 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003230 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 if (r.token == token) {
3232 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003233 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 mRelaunchingActivities.remove(i);
3235 i--;
3236 N--;
3237 }
3238 }
Bob Leee5408332009-09-04 18:31:17 -07003239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003241 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 return;
3243 }
Bob Leee5408332009-09-04 18:31:17 -07003244
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003245 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3246 + tmp.token + " with configChanges=0x"
3247 + Integer.toHexString(configChanges));
3248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 if (mPendingConfiguration != null) {
3250 changedConfig = mPendingConfiguration;
3251 mPendingConfiguration = null;
3252 }
3253 }
Bob Leee5408332009-09-04 18:31:17 -07003254
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003255 if (tmp.createdConfig != null) {
3256 // If the activity manager is passing us its current config,
3257 // assume that is really what we want regardless of what we
3258 // may have pending.
3259 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003260 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3261 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3262 if (changedConfig == null
3263 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3264 changedConfig = tmp.createdConfig;
3265 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003266 }
3267 }
3268
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003269 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003270 + tmp.token + ": changedConfig=" + changedConfig);
3271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 // If there was a pending configuration change, execute it first.
3273 if (changedConfig != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003274 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003275 }
Bob Leee5408332009-09-04 18:31:17 -07003276
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003277 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003278 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 if (r == null) {
3280 return;
3281 }
Bob Leee5408332009-09-04 18:31:17 -07003282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003284 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003285 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003286
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003287 r.activity.mChangingConfigurations = true;
3288
Dianne Hackborne2b04802010-12-09 09:24:55 -08003289 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003291 performPauseActivity(r.token, false, r.isPreHoneycomb());
3292 }
3293 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3294 r.state = new Bundle();
3295 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 }
Bob Leee5408332009-09-04 18:31:17 -07003297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 r.activity = null;
3301 r.window = null;
3302 r.hideForNow = false;
3303 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003304 // Merge any pending results and pending intents; don't just replace them
3305 if (tmp.pendingResults != null) {
3306 if (r.pendingResults == null) {
3307 r.pendingResults = tmp.pendingResults;
3308 } else {
3309 r.pendingResults.addAll(tmp.pendingResults);
3310 }
3311 }
3312 if (tmp.pendingIntents != null) {
3313 if (r.pendingIntents == null) {
3314 r.pendingIntents = tmp.pendingIntents;
3315 } else {
3316 r.pendingIntents.addAll(tmp.pendingIntents);
3317 }
3318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003320
Christopher Tateb70f3df2009-04-07 16:07:59 -07003321 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 }
3323
Romain Guy65b345f2011-07-27 18:51:50 -07003324 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003325 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 Bitmap thumbnail = createThumbnailBitmap(r);
3327 CharSequence description = null;
3328 try {
3329 description = r.activity.onCreateDescription();
3330 } catch (Exception e) {
3331 if (!mInstrumentation.onException(r.activity, e)) {
3332 throw new RuntimeException(
3333 "Unable to create description of activity "
3334 + r.intent.getComponent().toShortString()
3335 + ": " + e.toString(), e);
3336 }
3337 }
3338 //System.out.println("Reporting top thumbnail " + thumbnail);
3339 try {
3340 ActivityManagerNative.getDefault().reportThumbnail(
3341 token, thumbnail, description);
3342 } catch (RemoteException ex) {
3343 }
3344 }
3345
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003346 ArrayList<ComponentCallbacks2> collectComponentCallbacksLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003348 ArrayList<ComponentCallbacks2> callbacks
3349 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 if (mActivities.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003352 Iterator<ActivityClientRecord> it = mActivities.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 while (it.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003354 ActivityClientRecord ar = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003355 Activity a = ar.activity;
3356 if (a != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003357 Configuration thisConfig = applyConfigCompatMainThread(newConfig,
3358 ar.packageInfo.mCompatibilityInfo.getIfNeeded());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 if (!ar.activity.mFinished && (allActivities ||
3360 (a != null && !ar.paused))) {
3361 // If the activity is currently resumed, its configuration
3362 // needs to change right now.
3363 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003364 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 // Otherwise, we will tell it about the change
3366 // the next time it is resumed or shown. Note that
3367 // the activity manager may, before then, decide the
3368 // activity needs to be destroyed to handle its new
3369 // configuration.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003370 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Setting activity "
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003371 + ar.activityInfo.name + " newConfig=" + thisConfig);
3372 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 }
3374 }
3375 }
3376 }
3377 if (mServices.size() > 0) {
3378 Iterator<Service> it = mServices.values().iterator();
3379 while (it.hasNext()) {
3380 callbacks.add(it.next());
3381 }
3382 }
3383 synchronized (mProviderMap) {
3384 if (mLocalProviders.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003385 Iterator<ProviderClientRecord> it = mLocalProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 while (it.hasNext()) {
3387 callbacks.add(it.next().mLocalProvider);
3388 }
3389 }
3390 }
3391 final int N = mAllApplications.size();
3392 for (int i=0; i<N; i++) {
3393 callbacks.add(mAllApplications.get(i));
3394 }
Bob Leee5408332009-09-04 18:31:17 -07003395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 return callbacks;
3397 }
Bob Leee5408332009-09-04 18:31:17 -07003398
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003399 private final void performConfigurationChanged(
3400 ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003402 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003403 // we check the runtime type and act accordingly.
3404 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3405 if (activity != null) {
3406 activity.mCalled = false;
3407 }
Bob Leee5408332009-09-04 18:31:17 -07003408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 boolean shouldChangeConfig = false;
3410 if ((activity == null) || (activity.mCurrentConfig == null)) {
3411 shouldChangeConfig = true;
3412 } else {
Bob Leee5408332009-09-04 18:31:17 -07003413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414 // If the new config is the same as the config this Activity
3415 // is already running with then don't bother calling
3416 // onConfigurationChanged
3417 int diff = activity.mCurrentConfig.diff(config);
3418 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003419 // If this activity doesn't handle any of the config changes
3420 // then don't bother calling onConfigurationChanged as we're
3421 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003422 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 shouldChangeConfig = true;
3424 }
3425 }
3426 }
Bob Leee5408332009-09-04 18:31:17 -07003427
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003428 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003429 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 if (shouldChangeConfig) {
3431 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 if (activity != null) {
3434 if (!activity.mCalled) {
3435 throw new SuperNotCalledException(
3436 "Activity " + activity.getLocalClassName() +
3437 " did not call through to super.onConfigurationChanged()");
3438 }
3439 activity.mConfigChangeFlags = 0;
3440 activity.mCurrentConfig = new Configuration(config);
3441 }
3442 }
3443 }
3444
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003445 public final void applyConfigurationToResources(Configuration config) {
3446 synchronized (mPackages) {
3447 applyConfigurationToResourcesLocked(config, null);
3448 }
3449 }
3450
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003451 final boolean applyConfigurationToResourcesLocked(Configuration config,
3452 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003453 if (mResConfiguration == null) {
3454 mResConfiguration = new Configuration();
3455 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003456 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003457 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003458 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003459 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003460 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003461 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003462 DisplayMetrics dm = getDisplayMetricsLocked(compat, true);
3463
3464 if (compat != null && (mResCompatibilityInfo == null ||
3465 !mResCompatibilityInfo.equals(compat))) {
3466 mResCompatibilityInfo = compat;
3467 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3468 | ActivityInfo.CONFIG_SCREEN_SIZE
3469 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3470 }
Bob Leee5408332009-09-04 18:31:17 -07003471
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003472 // set it for java, this also affects newly created Resources
3473 if (config.locale != null) {
3474 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003475 }
Bob Leee5408332009-09-04 18:31:17 -07003476
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003477 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003478
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003479 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003480 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003481
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003482 Iterator<WeakReference<Resources>> it =
3483 mActiveResources.values().iterator();
3484 //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3485 // mActiveResources.entrySet().iterator();
3486 while (it.hasNext()) {
3487 WeakReference<Resources> v = it.next();
3488 Resources r = v.get();
3489 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003490 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003491 + r + " config to: " + config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003492 r.updateConfiguration(config, dm, compat);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003493 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003494 // + " " + r + ": " + r.getConfiguration());
3495 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003496 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003497 it.remove();
3498 }
3499 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003500
3501 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003502 }
3503
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003504 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003505
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003506 ArrayList<ComponentCallbacks2> callbacks = null;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003507
3508 synchronized (mPackages) {
3509 if (mPendingConfiguration != null) {
3510 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3511 config = mPendingConfiguration;
3512 }
3513 mPendingConfiguration = null;
3514 }
3515
3516 if (config == null) {
3517 return;
3518 }
3519
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003520 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003521 + config);
3522
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003523 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 if (mConfiguration == null) {
3526 mConfiguration = new Configuration();
3527 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003528 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003529 return;
3530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 mConfiguration.updateFrom(config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003532 if (mCompatConfiguration == null) {
3533 mCompatConfiguration = new Configuration();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003534 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003535 mCompatConfiguration.setTo(mConfiguration);
3536 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
3537 mResCompatibilityInfo.applyToConfiguration(mCompatConfiguration);
3538 config = mCompatConfiguration;
3539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 callbacks = collectComponentCallbacksLocked(false, config);
3541 }
Romain Guy65b345f2011-07-27 18:51:50 -07003542
3543 // Cleanup hardware accelerated stuff
3544 WindowManagerImpl.getDefault().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003545
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003546 if (callbacks != null) {
3547 final int N = callbacks.size();
3548 for (int i=0; i<N; i++) {
3549 performConfigurationChanged(callbacks.get(i), config);
3550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 }
3552 }
3553
3554 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003555 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 if (r == null || r.activity == null) {
3557 return;
3558 }
Bob Leee5408332009-09-04 18:31:17 -07003559
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003560 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003561 + r.activityInfo.name);
3562
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003563 performConfigurationChanged(r.activity, mCompatConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003564 }
3565
Romain Guy7eabe552011-07-21 14:56:34 -07003566 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003567 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003568 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003569 switch (profileType) {
3570 case 1:
3571 ViewDebug.startLooperProfiling(pcd.path, pcd.fd.getFileDescriptor());
3572 break;
3573 default:
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003574 mBoundApplication.setProfiler(pcd.path, pcd.fd);
3575 mBoundApplication.autoStopProfiler = false;
3576 mBoundApplication.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003577 break;
3578 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003579 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003580 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003581 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003582 } finally {
3583 try {
3584 pcd.fd.close();
3585 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003586 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003587 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003588 }
3589 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003590 switch (profileType) {
3591 case 1:
3592 ViewDebug.stopLooperProfiling();
3593 break;
3594 default:
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003595 mBoundApplication.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003596 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003597 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003598 }
3599 }
Bob Leee5408332009-09-04 18:31:17 -07003600
Andy McFadden824c5102010-07-09 16:26:57 -07003601 final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
3602 if (managed) {
3603 try {
3604 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3605 } catch (IOException e) {
3606 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3607 + " -- can the process access this path?");
3608 } finally {
3609 try {
3610 dhd.fd.close();
3611 } catch (IOException e) {
3612 Slog.w(TAG, "Failure closing profile fd", e);
3613 }
3614 }
3615 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003616 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003617 }
3618 }
3619
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003620 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3621 boolean hasPkgInfo = false;
3622 if (packages != null) {
3623 for (int i=packages.length-1; i>=0; i--) {
3624 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3625 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003626 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003627 ref = mPackages.get(packages[i]);
3628 if (ref != null && ref.get() != null) {
3629 hasPkgInfo = true;
3630 } else {
3631 ref = mResourcePackages.get(packages[i]);
3632 if (ref != null && ref.get() != null) {
3633 hasPkgInfo = true;
3634 }
3635 }
3636 }
3637 mPackages.remove(packages[i]);
3638 mResourcePackages.remove(packages[i]);
3639 }
3640 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003641 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003642 hasPkgInfo);
3643 }
3644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003645 final void handleLowMemory() {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003646 ArrayList<ComponentCallbacks2> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003648 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003649 callbacks = collectComponentCallbacksLocked(true, null);
3650 }
Bob Leee5408332009-09-04 18:31:17 -07003651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652 final int N = callbacks.size();
3653 for (int i=0; i<N; i++) {
3654 callbacks.get(i).onLowMemory();
3655 }
3656
Chris Tatece229052009-03-25 16:44:52 -07003657 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3658 if (Process.myUid() != Process.SYSTEM_UID) {
3659 int sqliteReleased = SQLiteDatabase.releaseMemory();
3660 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3661 }
Bob Leee5408332009-09-04 18:31:17 -07003662
Mike Reedcaf0df12009-04-27 14:32:05 -04003663 // Ask graphics to free up as much as possible (font/image caches)
3664 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665
3666 BinderInternal.forceGc("mem");
3667 }
3668
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003669 final void handleTrimMemory(int level) {
Romain Guybdf76092011-07-18 15:00:43 -07003670 WindowManagerImpl.getDefault().trimMemory(level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003671 ArrayList<ComponentCallbacks2> callbacks;
3672
3673 synchronized (mPackages) {
3674 callbacks = collectComponentCallbacksLocked(true, null);
3675 }
3676
3677 final int N = callbacks.size();
3678 for (int i=0; i<N; i++) {
3679 callbacks.get(i).onTrimMemory(level);
3680 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003681 }
3682
Romain Guy65b345f2011-07-27 18:51:50 -07003683 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 mBoundApplication = data;
3685 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003686 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003689 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 android.ddm.DdmHandleAppName.setAppName(data.processName);
3691
Dianne Hackborn5d927c22011-09-02 12:22:18 -07003692 if (data.persistent) {
3693 // Persistent processes on low-memory devices do not get to
3694 // use hardware accelerated drawing, since this can add too much
3695 // overhead to the process.
3696 Display display = WindowManagerImpl.getDefault().getDefaultDisplay();
3697 if (!ActivityManager.isHighEndGfx(display)) {
3698 HardwareRenderer.disable(false);
3699 }
3700 }
3701
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003702 if (data.profileFd != null) {
3703 data.startProfiling();
3704 }
3705
Joe Onoratod630f102011-03-17 18:42:26 -07003706 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
3707 // implementation to use the pool executor. Normally, we use the
3708 // serialized executor as the default. This has to happen in the
3709 // main thread so the main looper is set right.
3710 if (data.appInfo.targetSdkVersion <= 12) {
3711 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
3712 }
3713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 /*
3715 * Before spawning a new process, reset the time zone to be the system time zone.
3716 * This needs to be done because the system time zone could have changed after the
3717 * the spawning of this process. Without doing this this process would have the incorrect
3718 * system time zone.
3719 */
3720 TimeZone.setDefault(null);
3721
3722 /*
3723 * Initialize the default locale in this process for the reasons we set the time zone.
3724 */
3725 Locale.setDefault(data.config.locale);
3726
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003727 /*
3728 * Update the system configuration since its preloaded and might not
3729 * reflect configuration changes. The configuration object passed
3730 * in AppBindData can be safely assumed to be up to date
3731 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003732 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003733
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003734 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003735
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003736 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003737 * For system applications on userdebug/eng builds, log stack
3738 * traces of disk and network access to dropbox for analysis.
3739 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07003740 if ((data.appInfo.flags &
3741 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07003742 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
3743 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003744 }
3745
3746 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07003747 * For apps targetting SDK Honeycomb or later, we don't allow
3748 * network usage on the main event loop / UI thread.
3749 *
3750 * Note to those grepping: this is what ultimately throws
3751 * NetworkOnMainThreadException ...
3752 */
3753 if (data.appInfo.targetSdkVersion > 9) {
3754 StrictMode.enableDeathOnNetwork();
3755 }
3756
3757 /**
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003758 * Switch this process to density compatibility mode if needed.
3759 */
3760 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
3761 == 0) {
3762 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3763 }
Bob Leee5408332009-09-04 18:31:17 -07003764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
3766 // XXX should have option to change the port.
3767 Debug.changeDebugPort(8100);
3768 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003769 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 + " is waiting for the debugger on port 8100...");
3771
3772 IActivityManager mgr = ActivityManagerNative.getDefault();
3773 try {
3774 mgr.showWaitingForDebugger(mAppThread, true);
3775 } catch (RemoteException ex) {
3776 }
3777
3778 Debug.waitForDebugger();
3779
3780 try {
3781 mgr.showWaitingForDebugger(mAppThread, false);
3782 } catch (RemoteException ex) {
3783 }
3784
3785 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003786 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 + " can be debugged on port 8100...");
3788 }
3789 }
3790
Robert Greenwalt434203a2010-10-11 16:00:27 -07003791 /**
3792 * Initialize the default http proxy in this process for the reasons we set the time zone.
3793 */
3794 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
3795 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
3796 try {
3797 ProxyProperties proxyProperties = service.getProxy();
3798 Proxy.setHttpProxySystemProperty(proxyProperties);
3799 } catch (RemoteException e) {}
3800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003801 if (data.instrumentationName != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08003802 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003803 appContext.init(data.info, null, this);
3804 InstrumentationInfo ii = null;
3805 try {
3806 ii = appContext.getPackageManager().
3807 getInstrumentationInfo(data.instrumentationName, 0);
3808 } catch (PackageManager.NameNotFoundException e) {
3809 }
3810 if (ii == null) {
3811 throw new RuntimeException(
3812 "Unable to find instrumentation info for: "
3813 + data.instrumentationName);
3814 }
3815
3816 mInstrumentationAppDir = ii.sourceDir;
3817 mInstrumentationAppPackage = ii.packageName;
3818 mInstrumentedAppDir = data.info.getAppDir();
3819
3820 ApplicationInfo instrApp = new ApplicationInfo();
3821 instrApp.packageName = ii.packageName;
3822 instrApp.sourceDir = ii.sourceDir;
3823 instrApp.publicSourceDir = ii.publicSourceDir;
3824 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07003825 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003826 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08003828 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829 instrContext.init(pi, null, this);
3830
3831 try {
3832 java.lang.ClassLoader cl = instrContext.getClassLoader();
3833 mInstrumentation = (Instrumentation)
3834 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
3835 } catch (Exception e) {
3836 throw new RuntimeException(
3837 "Unable to instantiate instrumentation "
3838 + data.instrumentationName + ": " + e.toString(), e);
3839 }
3840
3841 mInstrumentation.init(this, instrContext, appContext,
3842 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
3843
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003844 if (data.profileFile != null && !ii.handleProfiling
3845 && data.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003846 data.handlingProfiling = true;
3847 File file = new File(data.profileFile);
3848 file.getParentFile().mkdirs();
3849 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
3850 }
3851
3852 try {
3853 mInstrumentation.onCreate(data.instrumentationArgs);
3854 }
3855 catch (Exception e) {
3856 throw new RuntimeException(
3857 "Exception thrown in onCreate() of "
3858 + data.instrumentationName + ": " + e.toString(), e);
3859 }
3860
3861 } else {
3862 mInstrumentation = new Instrumentation();
3863 }
3864
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003865 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08003866 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003867 }
3868
Christopher Tate181fafa2009-05-14 11:12:14 -07003869 // If the app is being launched for full backup or restore, bring it up in
3870 // a restricted environment with the base application class.
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003871 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 mInitialApplication = app;
3873
Christopher Tate75a99702011-05-18 16:28:19 -07003874 // don't bring up providers in restricted mode; they may depend on the
3875 // app's custom Application class
3876 if (!data.restrictedBackupMode){
3877 List<ProviderInfo> providers = data.providers;
3878 if (providers != null) {
3879 installContentProviders(app, providers);
3880 // For process that contains content providers, we want to
3881 // ensure that the JIT is enabled "at some point".
3882 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
3883 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 }
3885
3886 try {
3887 mInstrumentation.callApplicationOnCreate(app);
3888 } catch (Exception e) {
3889 if (!mInstrumentation.onException(app, e)) {
3890 throw new RuntimeException(
3891 "Unable to create application " + app.getClass().getName()
3892 + ": " + e.toString(), e);
3893 }
3894 }
3895 }
3896
3897 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
3898 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003899 if (mBoundApplication.profileFile != null && mBoundApplication.handlingProfiling
3900 && mBoundApplication.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003901 Debug.stopMethodTracing();
3902 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003903 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 // + ", app thr: " + mAppThread);
3905 try {
3906 am.finishInstrumentation(mAppThread, resultCode, results);
3907 } catch (RemoteException ex) {
3908 }
3909 }
3910
Romain Guy65b345f2011-07-27 18:51:50 -07003911 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 Context context, List<ProviderInfo> providers) {
3913 final ArrayList<IActivityManager.ContentProviderHolder> results =
3914 new ArrayList<IActivityManager.ContentProviderHolder>();
3915
3916 Iterator<ProviderInfo> i = providers.iterator();
3917 while (i.hasNext()) {
3918 ProviderInfo cpi = i.next();
3919 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07003920 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 buf.append(cpi.authority);
3922 buf.append(": ");
3923 buf.append(cpi.name);
3924 Log.i(TAG, buf.toString());
3925 IContentProvider cp = installProvider(context, null, cpi, false);
3926 if (cp != null) {
3927 IActivityManager.ContentProviderHolder cph =
3928 new IActivityManager.ContentProviderHolder(cpi);
3929 cph.provider = cp;
3930 results.add(cph);
3931 // Don't ever unload this provider from the process.
3932 synchronized(mProviderMap) {
3933 mProviderRefCountMap.put(cp.asBinder(), new ProviderRefCount(10000));
3934 }
3935 }
3936 }
3937
3938 try {
3939 ActivityManagerNative.getDefault().publishContentProviders(
3940 getApplicationThread(), results);
3941 } catch (RemoteException ex) {
3942 }
3943 }
3944
Romain Guy65b345f2011-07-27 18:51:50 -07003945 private IContentProvider getExistingProvider(Context context, String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003946 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003947 final ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 if (pr != null) {
3949 return pr.mProvider;
3950 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003951 return null;
3952 }
3953 }
3954
Romain Guy65b345f2011-07-27 18:51:50 -07003955 private IContentProvider getProvider(Context context, String name) {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003956 IContentProvider existing = getExistingProvider(context, name);
3957 if (existing != null) {
3958 return existing;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003959 }
3960
3961 IActivityManager.ContentProviderHolder holder = null;
3962 try {
3963 holder = ActivityManagerNative.getDefault().getContentProvider(
3964 getApplicationThread(), name);
3965 } catch (RemoteException ex) {
3966 }
3967 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003968 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 return null;
3970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971
3972 IContentProvider prov = installProvider(context, holder.provider,
3973 holder.info, true);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003974 //Slog.i(TAG, "noReleaseNeeded=" + holder.noReleaseNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003975 if (holder.noReleaseNeeded || holder.provider == null) {
3976 // We are not going to release the provider if it is an external
3977 // provider that doesn't care about being released, or if it is
3978 // a local provider running in this process.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003979 //Slog.i(TAG, "*** NO RELEASE NEEDED");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 synchronized(mProviderMap) {
3981 mProviderRefCountMap.put(prov.asBinder(), new ProviderRefCount(10000));
3982 }
3983 }
3984 return prov;
3985 }
3986
3987 public final IContentProvider acquireProvider(Context c, String name) {
3988 IContentProvider provider = getProvider(c, name);
3989 if(provider == null)
3990 return null;
3991 IBinder jBinder = provider.asBinder();
3992 synchronized(mProviderMap) {
3993 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3994 if(prc == null) {
3995 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
3996 } else {
3997 prc.count++;
3998 } //end else
3999 } //end synchronized
4000 return provider;
4001 }
4002
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004003 public final IContentProvider acquireExistingProvider(Context c, String name) {
4004 IContentProvider provider = getExistingProvider(c, name);
4005 if(provider == null)
4006 return null;
4007 IBinder jBinder = provider.asBinder();
4008 synchronized(mProviderMap) {
4009 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4010 if(prc == null) {
4011 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
4012 } else {
4013 prc.count++;
4014 } //end else
4015 } //end synchronized
4016 return provider;
4017 }
4018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004019 public final boolean releaseProvider(IContentProvider provider) {
4020 if(provider == null) {
4021 return false;
4022 }
4023 IBinder jBinder = provider.asBinder();
4024 synchronized(mProviderMap) {
4025 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4026 if(prc == null) {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004027 if(localLOGV) Slog.v(TAG, "releaseProvider::Weird shouldn't be here");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 return false;
4029 } else {
4030 prc.count--;
4031 if(prc.count == 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004032 // Schedule the actual remove asynchronously, since we
4033 // don't know the context this will be called in.
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004034 // TODO: it would be nice to post a delayed message, so
4035 // if we come back and need the same provider quickly
4036 // we will still have it available.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004037 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, provider);
4038 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 } //end if
4040 } //end else
4041 } //end synchronized
4042 return true;
4043 }
4044
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004045 final void completeRemoveProvider(IContentProvider provider) {
4046 IBinder jBinder = provider.asBinder();
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004047 String name = null;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004048 synchronized(mProviderMap) {
4049 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4050 if(prc != null && prc.count == 0) {
4051 mProviderRefCountMap.remove(jBinder);
4052 //invoke removeProvider to dereference provider
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004053 name = removeProviderLocked(provider);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004054 }
4055 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004056
4057 if (name != null) {
4058 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004059 if(localLOGV) Slog.v(TAG, "removeProvider::Invoking " +
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004060 "ActivityManagerNative.removeContentProvider(" + name);
4061 ActivityManagerNative.getDefault().removeContentProvider(
4062 getApplicationThread(), name);
4063 } catch (RemoteException e) {
4064 //do nothing content provider object is dead any way
4065 } //end catch
4066 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004067 }
4068
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004069 public final String removeProviderLocked(IContentProvider provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 if (provider == null) {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004071 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072 }
4073 IBinder providerBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004074
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004075 String name = null;
4076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004077 // remove the provider from mProviderMap
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004078 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004079 while (iter.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004080 ProviderClientRecord pr = iter.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004081 IBinder myBinder = pr.mProvider.asBinder();
4082 if (myBinder == providerBinder) {
4083 //find if its published by this process itself
4084 if(pr.mLocalProvider != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004085 if(localLOGV) Slog.i(TAG, "removeProvider::found local provider returning");
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004086 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004088 if(localLOGV) Slog.v(TAG, "removeProvider::Not local provider Unlinking " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 "death recipient");
4090 //content provider is in another process
4091 myBinder.unlinkToDeath(pr, 0);
4092 iter.remove();
4093 //invoke remove only once for the very first name seen
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004094 if(name == null) {
4095 name = pr.mName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 }
4097 } //end if myBinder
4098 } //end while iter
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004099
4100 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 }
4102
4103 final void removeDeadProvider(String name, IContentProvider provider) {
4104 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004105 ProviderClientRecord pr = mProviderMap.get(name);
Jeff Brown2a4c5ac2011-08-23 11:58:26 -07004106 if (pr != null && 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 }
4115
Romain Guy65b345f2011-07-27 18:51:50 -07004116 private IContentProvider installProvider(Context context,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117 IContentProvider provider, ProviderInfo info, boolean noisy) {
4118 ContentProvider localProvider = null;
4119 if (provider == null) {
4120 if (noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004121 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004122 + info.name);
4123 }
4124 Context c = null;
4125 ApplicationInfo ai = info.applicationInfo;
4126 if (context.getPackageName().equals(ai.packageName)) {
4127 c = context;
4128 } else if (mInitialApplication != null &&
4129 mInitialApplication.getPackageName().equals(ai.packageName)) {
4130 c = mInitialApplication;
4131 } else {
4132 try {
4133 c = context.createPackageContext(ai.packageName,
4134 Context.CONTEXT_INCLUDE_CODE);
4135 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004136 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004137 }
4138 }
4139 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004140 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004141 ai.packageName +
4142 " while loading content provider " +
4143 info.name);
4144 return null;
4145 }
4146 try {
4147 final java.lang.ClassLoader cl = c.getClassLoader();
4148 localProvider = (ContentProvider)cl.
4149 loadClass(info.name).newInstance();
4150 provider = localProvider.getIContentProvider();
4151 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004152 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004153 info.name + " from sourceDir " +
4154 info.applicationInfo.sourceDir);
4155 return null;
4156 }
Joe Onorato43a17652011-04-06 19:22:23 -07004157 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004158 TAG, "Instantiating local provider " + info.name);
4159 // XXX Need to create the correct context for this provider.
4160 localProvider.attachInfo(c, info);
4161 } catch (java.lang.Exception e) {
4162 if (!mInstrumentation.onException(null, e)) {
4163 throw new RuntimeException(
4164 "Unable to get provider " + info.name
4165 + ": " + e.toString(), e);
4166 }
4167 return null;
4168 }
4169 } else if (localLOGV) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004170 Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 + info.name);
4172 }
4173
4174 synchronized (mProviderMap) {
4175 // Cache the pointer for the remote provider.
4176 String names[] = PATTERN_SEMICOLON.split(info.authority);
4177 for (int i=0; i<names.length; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004178 ProviderClientRecord pr = new ProviderClientRecord(names[i], provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 localProvider);
4180 try {
4181 provider.asBinder().linkToDeath(pr, 0);
4182 mProviderMap.put(names[i], pr);
4183 } catch (RemoteException e) {
4184 return null;
4185 }
4186 }
4187 if (localProvider != null) {
4188 mLocalProviders.put(provider.asBinder(),
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004189 new ProviderClientRecord(null, provider, localProvider));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 }
4191 }
4192
4193 return provider;
4194 }
4195
Romain Guy65b345f2011-07-27 18:51:50 -07004196 private void attach(boolean system) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 sThreadLocal.set(this);
4198 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004200 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004201 public void run() {
4202 ensureJitEnabled();
4203 }
4204 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4206 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4207 IActivityManager mgr = ActivityManagerNative.getDefault();
4208 try {
4209 mgr.attachApplication(mAppThread);
4210 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004211 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 }
4213 } else {
4214 // Don't set application object here -- if the system crashes,
4215 // we can't display an alert, we just want to die die die.
4216 android.ddm.DdmHandleAppName.setAppName("system_process");
4217 try {
4218 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004219 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 context.init(getSystemContext().mPackageInfo, null, this);
4221 Application app = Instrumentation.newApplication(Application.class, context);
4222 mAllApplications.add(app);
4223 mInitialApplication = app;
4224 app.onCreate();
4225 } catch (Exception e) {
4226 throw new RuntimeException(
4227 "Unable to instantiate Application():" + e.toString(), e);
4228 }
4229 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004230
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004231 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004232 public void onConfigurationChanged(Configuration newConfig) {
4233 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004234 // We need to apply this change to the resources
4235 // immediately, because upon returning the view
4236 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004237 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004238 // This actually changed the resources! Tell
4239 // everyone about it.
4240 if (mPendingConfiguration == null ||
4241 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4242 mPendingConfiguration = newConfig;
4243
4244 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4245 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004246 }
4247 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004248 }
4249 public void onLowMemory() {
4250 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004251 public void onTrimMemory(int level) {
4252 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004253 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 }
4255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004256 public static final ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004257 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 ActivityThread thread = new ActivityThread();
4259 thread.attach(true);
4260 return thread;
4261 }
4262
Jeff Brown10e89712011-07-08 18:52:57 -07004263 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004264 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004265 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004266 }
4267 }
4268
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004269 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004270 synchronized (mPackages) {
4271 if (mCoreSettings != null) {
4272 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004273 } else {
4274 return defaultValue;
4275 }
4276 }
4277 }
4278
Romain Guy65b345f2011-07-27 18:51:50 -07004279 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004280 SamplingProfilerIntegration.start();
4281
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004282 // CloseGuard defaults to true and can be quite spammy. We
4283 // disable it here, but selectively enable it later (via
4284 // StrictMode) on debug builds, but using DropBox, not logs.
4285 CloseGuard.setEnabled(false);
4286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 Process.setArgV0("<pre-initialized>");
4288
4289 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07004290 if (sMainThreadHandler == null) {
4291 sMainThreadHandler = new Handler();
4292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004293
4294 ActivityThread thread = new ActivityThread();
4295 thread.attach(false);
4296
Dianne Hackborn287952c2010-09-22 22:34:31 -07004297 if (false) {
4298 Looper.myLooper().setMessageLogging(new
4299 LogPrinter(Log.DEBUG, "ActivityThread"));
4300 }
4301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004302 Looper.loop();
4303
Jeff Brown10e89712011-07-08 18:52:57 -07004304 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004305 }
4306}