blob: 85e59b308448f7c7649ad0791a810d69f2c5d612 [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;
21import android.content.ComponentCallbacks;
22import 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;
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070073import android.view.ViewAncestor;
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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 private static final boolean DEBUG = false;
Joe Onorato43a17652011-04-06 19:22:23 -0700127 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700128 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700129 /** @hide */
130 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700131 private static final boolean DEBUG_RESULTS = false;
Christopher Tate4a627c72011-04-01 14:43:32 -0700132 private static final boolean DEBUG_BACKUP = true;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700133 private static final boolean DEBUG_CONFIGURATION = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
135 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
136 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
137 private static final int LOG_ON_PAUSE_CALLED = 30021;
138 private static final int LOG_ON_RESUME_CALLED = 30022;
139
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700140 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700141
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700142 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700144 final ApplicationThread mAppThread = new ApplicationThread();
145 final Looper mLooper = Looper.myLooper();
146 final H mH = new H();
147 final HashMap<IBinder, ActivityClientRecord> mActivities
148 = new HashMap<IBinder, ActivityClientRecord>();
149 // List of new activities (via ActivityRecord.nextIdle) that should
150 // be reported when next we idle.
151 ActivityClientRecord mNewActivities = null;
152 // Number of activities that are currently visible on-screen.
153 int mNumVisibleActivities = 0;
154 final HashMap<IBinder, Service> mServices
155 = new HashMap<IBinder, Service>();
156 AppBindData mBoundApplication;
157 Configuration mConfiguration;
158 Configuration mResConfiguration;
159 Application mInitialApplication;
160 final ArrayList<Application> mAllApplications
161 = new ArrayList<Application>();
162 // set of instantiated backup agents, keyed by package name
163 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700164 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700165 Instrumentation mInstrumentation;
166 String mInstrumentationAppDir = null;
167 String mInstrumentationAppPackage = null;
168 String mInstrumentedAppDir = null;
169 boolean mSystemThread = false;
170 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700172 // These can be accessed by multiple threads; mPackages is the lock.
173 // XXX For now we keep around information about all packages we have
174 // seen, not removing entries from this map.
175 final HashMap<String, WeakReference<LoadedApk>> mPackages
176 = new HashMap<String, WeakReference<LoadedApk>>();
177 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
178 = new HashMap<String, WeakReference<LoadedApk>>();
179 Display mDisplay = null;
180 DisplayMetrics mDisplayMetrics = null;
181 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
182 = new HashMap<ResourcesKey, WeakReference<Resources> >();
183 final ArrayList<ActivityClientRecord> mRelaunchingActivities
184 = new ArrayList<ActivityClientRecord>();
185 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700187 // The lock of mProviderMap protects the following variables.
188 final HashMap<String, ProviderClientRecord> mProviderMap
189 = new HashMap<String, ProviderClientRecord>();
190 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
191 = new HashMap<IBinder, ProviderRefCount>();
192 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
193 = new HashMap<IBinder, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194
Jeff Hamilton52d32032011-01-08 15:31:26 -0600195 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
196 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
197
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700198 final GcIdler mGcIdler = new GcIdler();
199 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700201 static Handler sMainThreadHandler; // set once in main()
202
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800203 Bundle mCoreSettings = null;
204
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400205 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700207 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 Intent intent;
209 Bundle state;
210 Activity activity;
211 Window window;
212 Activity parent;
213 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700214 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 boolean paused;
216 boolean stopped;
217 boolean hideForNow;
218 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700219 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700220 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221
222 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400223 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700224 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225
226 List<ResultInfo> pendingResults;
227 List<Intent> pendingIntents;
228
229 boolean startsNotResumed;
230 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800231 int pendingConfigChanges;
232 boolean onlyLocalRequest;
233
234 View mPendingRemoveWindow;
235 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700237 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 parent = null;
239 embeddedID = null;
240 paused = false;
241 stopped = false;
242 hideForNow = false;
243 nextIdle = null;
244 }
245
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800246 public boolean isPreHoneycomb() {
247 if (activity != null) {
248 return activity.getApplicationInfo().targetSdkVersion
249 < android.os.Build.VERSION_CODES.HONEYCOMB;
250 }
251 return false;
252 }
253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 public String toString() {
255 ComponentName componentName = intent.getComponent();
256 return "ActivityRecord{"
257 + Integer.toHexString(System.identityHashCode(this))
258 + " token=" + token + " " + (componentName == null
259 ? "no component name" : componentName.toShortString())
260 + "}";
261 }
262 }
263
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400264 final class ProviderClientRecord implements IBinder.DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 final String mName;
266 final IContentProvider mProvider;
267 final ContentProvider mLocalProvider;
268
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700269 ProviderClientRecord(String name, IContentProvider provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 ContentProvider localProvider) {
271 mName = name;
272 mProvider = provider;
273 mLocalProvider = localProvider;
274 }
275
276 public void binderDied() {
277 removeDeadProvider(mName, mProvider);
278 }
279 }
280
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400281 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 List<Intent> intents;
283 IBinder token;
284 public String toString() {
285 return "NewIntentData{intents=" + intents + " token=" + token + "}";
286 }
287 }
288
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400289 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700290 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
291 boolean ordered, boolean sticky, IBinder token) {
292 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky, token);
293 this.intent = intent;
294 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 Intent intent;
297 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400298 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 public String toString() {
300 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700301 info.packageName + " resultCode=" + getResultCode()
302 + " resultData=" + getResultData() + " resultExtras="
303 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 }
305 }
306
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400307 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700308 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400309 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700310 int backupMode;
311 public String toString() {
312 return "CreateBackupAgentData{appInfo=" + appInfo
313 + " backupAgent=" + appInfo.backupAgentName
314 + " mode=" + backupMode + "}";
315 }
316 }
Bob Leee5408332009-09-04 18:31:17 -0700317
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400318 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 IBinder token;
320 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400321 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 Intent intent;
323 public String toString() {
324 return "CreateServiceData{token=" + token + " className="
325 + info.name + " packageName=" + info.packageName
326 + " intent=" + intent + "}";
327 }
328 }
329
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400330 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 IBinder token;
332 Intent intent;
333 boolean rebind;
334 public String toString() {
335 return "BindServiceData{token=" + token + " intent=" + intent + "}";
336 }
337 }
338
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400339 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700341 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700343 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 Intent args;
345 public String toString() {
346 return "ServiceArgsData{token=" + token + " startId=" + startId
347 + " args=" + args + "}";
348 }
349 }
350
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400351 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700352 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 String processName;
354 ApplicationInfo appInfo;
355 List<ProviderInfo> providers;
356 ComponentName instrumentationName;
357 String profileFile;
358 Bundle instrumentationArgs;
359 IInstrumentationWatcher instrumentationWatcher;
360 int debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700361 boolean restrictedBackupMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400363 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 boolean handlingProfiling;
365 public String toString() {
366 return "AppBindData{appInfo=" + appInfo + "}";
367 }
368 }
369
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400370 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700371 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700372 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800373 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 }
376
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400377 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 IBinder token;
379 List<ResultInfo> results;
380 public String toString() {
381 return "ResultData{token=" + token + " results" + results + "}";
382 }
383 }
384
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400385 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800386 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 String what;
388 String who;
389 }
390
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400391 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700392 String path;
393 ParcelFileDescriptor fd;
394 }
395
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400396 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700397 String path;
398 ParcelFileDescriptor fd;
399 }
400
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400401 static final class UpdateCompatibilityData {
402 String pkg;
403 CompatibilityInfo info;
404 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700405
Chet Haase9c1e23b2011-03-24 10:51:31 -0700406 native private void dumpGraphicsInfo(FileDescriptor fd);
407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 private final class ApplicationThread extends ApplicationThreadNative {
409 private static final String HEAP_COLUMN = "%17s %8s %8s %8s %8s";
410 private static final String ONE_COUNT_COLUMN = "%17s %8d";
411 private static final String TWO_COUNT_COLUMNS = "%17s %8d %17s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700412 private static final String TWO_COUNT_COLUMNS_DB = "%20s %8d %20s %8d";
413 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 // Formatting for checkin service - update version if row format changes
416 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 public final void schedulePauseActivity(IBinder token, boolean finished,
419 boolean userLeaving, int configChanges) {
420 queueOrSendMessage(
421 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
422 token,
423 (userLeaving ? 1 : 0),
424 configChanges);
425 }
426
427 public final void scheduleStopActivity(IBinder token, boolean showWindow,
428 int configChanges) {
429 queueOrSendMessage(
430 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
431 token, 0, configChanges);
432 }
433
434 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
435 queueOrSendMessage(
436 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
437 token);
438 }
439
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800440 public final void scheduleSleeping(IBinder token, boolean sleeping) {
441 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
442 }
443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
445 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
446 }
447
448 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
449 ResultData res = new ResultData();
450 res.token = token;
451 res.results = results;
452 queueOrSendMessage(H.SEND_RESULT, res);
453 }
454
455 // we use token to identify this activity without having to send the
456 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700457 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400458 ActivityInfo info, CompatibilityInfo compatInfo, Bundle state,
459 List<ResultInfo> pendingResults,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700461 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462
463 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700464 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 r.intent = intent;
466 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400467 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 r.state = state;
469
470 r.pendingResults = pendingResults;
471 r.pendingIntents = pendingNewIntents;
472
473 r.startsNotResumed = notResumed;
474 r.isForward = isForward;
475
476 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
477 }
478
479 public final void scheduleRelaunchActivity(IBinder token,
480 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800481 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800482 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
483 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 }
485
486 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
487 NewIntentData data = new NewIntentData();
488 data.intents = intents;
489 data.token = token;
490
491 queueOrSendMessage(H.NEW_INTENT, data);
492 }
493
494 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
495 int configChanges) {
496 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
497 configChanges);
498 }
499
500 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400501 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
502 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700503 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
504 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400506 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 queueOrSendMessage(H.RECEIVER, r);
508 }
509
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400510 public final void scheduleCreateBackupAgent(ApplicationInfo app,
511 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700512 CreateBackupAgentData d = new CreateBackupAgentData();
513 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400514 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700515 d.backupMode = backupMode;
516
517 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
518 }
519
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400520 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
521 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700522 CreateBackupAgentData d = new CreateBackupAgentData();
523 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400524 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700525
526 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
527 }
528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400530 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 CreateServiceData s = new CreateServiceData();
532 s.token = token;
533 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400534 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535
536 queueOrSendMessage(H.CREATE_SERVICE, s);
537 }
538
539 public final void scheduleBindService(IBinder token, Intent intent,
540 boolean rebind) {
541 BindServiceData s = new BindServiceData();
542 s.token = token;
543 s.intent = intent;
544 s.rebind = rebind;
545
546 queueOrSendMessage(H.BIND_SERVICE, s);
547 }
548
549 public final void scheduleUnbindService(IBinder token, Intent intent) {
550 BindServiceData s = new BindServiceData();
551 s.token = token;
552 s.intent = intent;
553
554 queueOrSendMessage(H.UNBIND_SERVICE, s);
555 }
556
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700557 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700558 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 ServiceArgsData s = new ServiceArgsData();
560 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700561 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700563 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 s.args = args;
565
566 queueOrSendMessage(H.SERVICE_ARGS, s);
567 }
568
569 public final void scheduleStopService(IBinder token) {
570 queueOrSendMessage(H.STOP_SERVICE, token);
571 }
572
573 public final void bindApplication(String processName,
574 ApplicationInfo appInfo, List<ProviderInfo> providers,
575 ComponentName instrumentationName, String profileFile,
576 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Christopher Tate181fafa2009-05-14 11:12:14 -0700577 int debugMode, boolean isRestrictedBackupMode, Configuration config,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400578 CompatibilityInfo compatInfo, Map<String, IBinder> services,
579 Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580
581 if (services != null) {
582 // Setup the service cache in the ServiceManager
583 ServiceManager.initServiceCache(services);
584 }
585
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800586 setCoreSettings(coreSettings);
587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 AppBindData data = new AppBindData();
589 data.processName = processName;
590 data.appInfo = appInfo;
591 data.providers = providers;
592 data.instrumentationName = instrumentationName;
593 data.profileFile = profileFile;
594 data.instrumentationArgs = instrumentationArgs;
595 data.instrumentationWatcher = instrumentationWatcher;
596 data.debugMode = debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700597 data.restrictedBackupMode = isRestrictedBackupMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400599 data.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 queueOrSendMessage(H.BIND_APPLICATION, data);
601 }
602
603 public final void scheduleExit() {
604 queueOrSendMessage(H.EXIT_APPLICATION, null);
605 }
606
Christopher Tate5e1ab332009-09-01 20:32:49 -0700607 public final void scheduleSuicide() {
608 queueOrSendMessage(H.SUICIDE, null);
609 }
610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 public void requestThumbnail(IBinder token) {
612 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
613 }
614
615 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800616 synchronized (mPackages) {
617 if (mPendingConfiguration == null ||
618 mPendingConfiguration.isOtherSeqNewer(config)) {
619 mPendingConfiguration = config;
620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
622 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
623 }
624
625 public void updateTimeZone() {
626 TimeZone.setDefault(null);
627 }
628
Robert Greenwalt03595d02010-11-02 14:08:23 -0700629 public void clearDnsCache() {
630 // a non-standard API to get this to libcore
631 InetAddress.clearDnsCache();
632 }
633
Robert Greenwalt434203a2010-10-11 16:00:27 -0700634 public void setHttpProxy(String host, String port, String exclList) {
635 Proxy.setHttpProxySystemProperty(host, port, exclList);
636 }
637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 public void processInBackground() {
639 mH.removeMessages(H.GC_WHEN_IDLE);
640 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
641 }
642
643 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700644 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700645 try {
646 data.fd = ParcelFileDescriptor.dup(fd);
647 data.token = servicetoken;
648 data.args = args;
649 queueOrSendMessage(H.DUMP_SERVICE, data);
650 } catch (IOException e) {
651 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 }
653 }
654
655 // This function exists to make sure all receiver dispatching is
656 // correctly ordered, since these are one-way calls and the binder driver
657 // applies transaction ordering per object for such calls.
658 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700659 int resultCode, String dataStr, Bundle extras, boolean ordered,
660 boolean sticky) throws RemoteException {
661 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 }
Bob Leee5408332009-09-04 18:31:17 -0700663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 public void scheduleLowMemory() {
665 queueOrSendMessage(H.LOW_MEMORY, null);
666 }
667
668 public void scheduleActivityConfigurationChanged(IBinder token) {
669 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
670 }
671
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700672 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd) {
673 ProfilerControlData pcd = new ProfilerControlData();
674 pcd.path = path;
675 pcd.fd = fd;
676 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800677 }
678
Andy McFadden824c5102010-07-09 16:26:57 -0700679 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
680 DumpHeapData dhd = new DumpHeapData();
681 dhd.path = path;
682 dhd.fd = fd;
683 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
684 }
685
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700686 public void setSchedulingGroup(int group) {
687 // Note: do this immediately, since going into the foreground
688 // should happen regardless of what pending work we have to do
689 // and the activity manager will wait for us to report back that
690 // we are done before sending us to the background.
691 try {
692 Process.setProcessGroup(Process.myPid(), group);
693 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800694 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700695 }
696 }
Bob Leee5408332009-09-04 18:31:17 -0700697
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700698 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
699 Debug.getMemoryInfo(outInfo);
700 }
Bob Leee5408332009-09-04 18:31:17 -0700701
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700702 public void dispatchPackageBroadcast(int cmd, String[] packages) {
703 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
704 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700705
706 public void scheduleCrash(String msg) {
707 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
708 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700709
Dianne Hackborn30d71892010-12-11 10:37:55 -0800710 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
711 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700712 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700713 try {
714 data.fd = ParcelFileDescriptor.dup(fd);
715 data.token = activitytoken;
716 data.prefix = prefix;
717 data.args = args;
718 queueOrSendMessage(H.DUMP_ACTIVITY, data);
719 } catch (IOException e) {
720 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700721 }
722 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 @Override
725 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700726 if (args != null && args.length == 1 && args[0].equals("graphics")) {
727 pw.flush();
728 dumpGraphicsInfo(fd);
729 return;
730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 long nativeMax = Debug.getNativeHeapSize() / 1024;
732 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
733 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
734
735 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
736 Debug.getMemoryInfo(memInfo);
737
738 final int nativeShared = memInfo.nativeSharedDirty;
739 final int dalvikShared = memInfo.dalvikSharedDirty;
740 final int otherShared = memInfo.otherSharedDirty;
741
742 final int nativePrivate = memInfo.nativePrivateDirty;
743 final int dalvikPrivate = memInfo.dalvikPrivateDirty;
744 final int otherPrivate = memInfo.otherPrivateDirty;
745
746 Runtime runtime = Runtime.getRuntime();
747
748 long dalvikMax = runtime.totalMemory() / 1024;
749 long dalvikFree = runtime.freeMemory() / 1024;
750 long dalvikAllocated = dalvikMax - dalvikFree;
751 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Joe Onoratoc6cc0f82011-04-12 11:53:13 -0700752 long viewRootInstanceCount = ViewDebug.getViewAncestorInstanceCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700753 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
754 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 int globalAssetCount = AssetManager.getGlobalAssetCount();
756 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
757 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
758 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
759 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700760 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 long sqliteAllocated = SQLiteDebug.getHeapAllocatedSize() / 1024;
Vasu Noric3849202010-03-09 10:47:25 -0800762 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 // Check to see if we were called by checkin server. If so, print terse format.
765 boolean doCheckinFormat = false;
766 if (args != null) {
767 for (String arg : args) {
768 if ("-c".equals(arg)) doCheckinFormat = true;
769 }
770 }
Bob Leee5408332009-09-04 18:31:17 -0700771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 // For checkin, we print one long comma-separated list of values
773 if (doCheckinFormat) {
774 // NOTE: if you change anything significant below, also consider changing
775 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700776 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 // Header
780 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
781 pw.print(Process.myPid()); pw.print(',');
782 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 // Heap info - max
785 pw.print(nativeMax); pw.print(',');
786 pw.print(dalvikMax); pw.print(',');
787 pw.print("N/A,");
788 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 // Heap info - allocated
791 pw.print(nativeAllocated); pw.print(',');
792 pw.print(dalvikAllocated); pw.print(',');
793 pw.print("N/A,");
794 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 // Heap info - free
797 pw.print(nativeFree); pw.print(',');
798 pw.print(dalvikFree); pw.print(',');
799 pw.print("N/A,");
800 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 // Heap info - proportional set size
803 pw.print(memInfo.nativePss); pw.print(',');
804 pw.print(memInfo.dalvikPss); pw.print(',');
805 pw.print(memInfo.otherPss); pw.print(',');
806 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 // Heap info - shared
Bob Leee5408332009-09-04 18:31:17 -0700809 pw.print(nativeShared); pw.print(',');
810 pw.print(dalvikShared); pw.print(',');
811 pw.print(otherShared); pw.print(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 pw.print(nativeShared + dalvikShared + otherShared); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 // Heap info - private
Bob Leee5408332009-09-04 18:31:17 -0700815 pw.print(nativePrivate); pw.print(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 pw.print(dalvikPrivate); pw.print(',');
817 pw.print(otherPrivate); pw.print(',');
818 pw.print(nativePrivate + dalvikPrivate + otherPrivate); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 // Object counts
821 pw.print(viewInstanceCount); pw.print(',');
822 pw.print(viewRootInstanceCount); pw.print(',');
823 pw.print(appContextInstanceCount); pw.print(',');
824 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 pw.print(globalAssetCount); pw.print(',');
827 pw.print(globalAssetManagerCount); pw.print(',');
828 pw.print(binderLocalObjectCount); pw.print(',');
829 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 pw.print(binderDeathObjectCount); pw.print(',');
832 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 // SQL
835 pw.print(sqliteAllocated); pw.print(',');
Vasu Noric3849202010-03-09 10:47:25 -0800836 pw.print(stats.memoryUsed / 1024); pw.print(',');
837 pw.print(stats.pageCacheOverflo / 1024); pw.print(',');
838 pw.print(stats.largestMemAlloc / 1024); pw.print(',');
839 for (int i = 0; i < stats.dbStats.size(); i++) {
840 DbStats dbStats = stats.dbStats.get(i);
841 printRow(pw, DB_INFO_FORMAT, dbStats.pageSize, dbStats.dbSize,
Vasu Nori90a367262010-04-12 12:49:09 -0700842 dbStats.lookaside, dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -0800843 pw.print(',');
844 }
Bob Leee5408332009-09-04 18:31:17 -0700845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 return;
847 }
Bob Leee5408332009-09-04 18:31:17 -0700848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 // otherwise, show human-readable format
850 printRow(pw, HEAP_COLUMN, "", "native", "dalvik", "other", "total");
851 printRow(pw, HEAP_COLUMN, "size:", nativeMax, dalvikMax, "N/A", nativeMax + dalvikMax);
852 printRow(pw, HEAP_COLUMN, "allocated:", nativeAllocated, dalvikAllocated, "N/A",
853 nativeAllocated + dalvikAllocated);
854 printRow(pw, HEAP_COLUMN, "free:", nativeFree, dalvikFree, "N/A",
855 nativeFree + dalvikFree);
856
857 printRow(pw, HEAP_COLUMN, "(Pss):", memInfo.nativePss, memInfo.dalvikPss,
858 memInfo.otherPss, memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss);
859
860 printRow(pw, HEAP_COLUMN, "(shared dirty):", nativeShared, dalvikShared, otherShared,
861 nativeShared + dalvikShared + otherShared);
862 printRow(pw, HEAP_COLUMN, "(priv dirty):", nativePrivate, dalvikPrivate, otherPrivate,
863 nativePrivate + dalvikPrivate + otherPrivate);
864
865 pw.println(" ");
866 pw.println(" Objects");
Joe Onoratoc6cc0f82011-04-12 11:53:13 -0700867 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewAncestors:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 viewRootInstanceCount);
869
870 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
871 "Activities:", activityInstanceCount);
872
873 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
874 "AssetManagers:", globalAssetManagerCount);
875
876 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
877 "Proxy Binders:", binderProxyObjectCount);
878 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
879
880 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -0700881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 // SQLite mem info
883 pw.println(" ");
884 pw.println(" SQL");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700885 printRow(pw, TWO_COUNT_COLUMNS_DB, "heap:", sqliteAllocated, "MEMORY_USED:",
Vasu Noric3849202010-03-09 10:47:25 -0800886 stats.memoryUsed / 1024);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700887 printRow(pw, TWO_COUNT_COLUMNS_DB, "PAGECACHE_OVERFLOW:",
888 stats.pageCacheOverflo / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800889 pw.println(" ");
890 int N = stats.dbStats.size();
891 if (N > 0) {
892 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700893 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
894 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -0800895 for (int i = 0; i < N; i++) {
896 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700897 printRow(pw, DB_INFO_FORMAT,
898 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
899 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
900 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
901 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -0800902 }
903 }
Bob Leee5408332009-09-04 18:31:17 -0700904
Dianne Hackborn82e1ee92009-08-11 18:56:41 -0700905 // Asset details.
906 String assetAlloc = AssetManager.getAssetAllocations();
907 if (assetAlloc != null) {
908 pw.println(" ");
909 pw.println(" Asset Allocations");
910 pw.print(assetAlloc);
911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 }
913
914 private void printRow(PrintWriter pw, String format, Object...objs) {
915 pw.println(String.format(format, objs));
916 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800917
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800918 public void setCoreSettings(Bundle coreSettings) {
919 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800920 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400921
922 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
923 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
924 ucd.pkg = pkg;
925 ucd.info = info;
926 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 }
929
930 private final class H extends Handler {
931 public static final int LAUNCH_ACTIVITY = 100;
932 public static final int PAUSE_ACTIVITY = 101;
933 public static final int PAUSE_ACTIVITY_FINISHING= 102;
934 public static final int STOP_ACTIVITY_SHOW = 103;
935 public static final int STOP_ACTIVITY_HIDE = 104;
936 public static final int SHOW_WINDOW = 105;
937 public static final int HIDE_WINDOW = 106;
938 public static final int RESUME_ACTIVITY = 107;
939 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -0700940 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 public static final int BIND_APPLICATION = 110;
942 public static final int EXIT_APPLICATION = 111;
943 public static final int NEW_INTENT = 112;
944 public static final int RECEIVER = 113;
945 public static final int CREATE_SERVICE = 114;
946 public static final int SERVICE_ARGS = 115;
947 public static final int STOP_SERVICE = 116;
948 public static final int REQUEST_THUMBNAIL = 117;
949 public static final int CONFIGURATION_CHANGED = 118;
950 public static final int CLEAN_UP_CONTEXT = 119;
951 public static final int GC_WHEN_IDLE = 120;
952 public static final int BIND_SERVICE = 121;
953 public static final int UNBIND_SERVICE = 122;
954 public static final int DUMP_SERVICE = 123;
955 public static final int LOW_MEMORY = 124;
956 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
957 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800958 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -0700959 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -0700960 public static final int DESTROY_BACKUP_AGENT = 129;
961 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700962 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800963 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700964 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700965 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -0700966 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700967 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800968 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800969 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400970 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -0700972 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 switch (code) {
974 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
975 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
976 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
977 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
978 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
979 case SHOW_WINDOW: return "SHOW_WINDOW";
980 case HIDE_WINDOW: return "HIDE_WINDOW";
981 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
982 case SEND_RESULT: return "SEND_RESULT";
983 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
984 case BIND_APPLICATION: return "BIND_APPLICATION";
985 case EXIT_APPLICATION: return "EXIT_APPLICATION";
986 case NEW_INTENT: return "NEW_INTENT";
987 case RECEIVER: return "RECEIVER";
988 case CREATE_SERVICE: return "CREATE_SERVICE";
989 case SERVICE_ARGS: return "SERVICE_ARGS";
990 case STOP_SERVICE: return "STOP_SERVICE";
991 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
992 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
993 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
994 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
995 case BIND_SERVICE: return "BIND_SERVICE";
996 case UNBIND_SERVICE: return "UNBIND_SERVICE";
997 case DUMP_SERVICE: return "DUMP_SERVICE";
998 case LOW_MEMORY: return "LOW_MEMORY";
999 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1000 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001001 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001002 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1003 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001004 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001005 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001006 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001007 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001008 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001009 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001010 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001011 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001012 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001013 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 }
1015 }
1016 return "(unknown)";
1017 }
1018 public void handleMessage(Message msg) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001019 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 switch (msg.what) {
1021 case LAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001022 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023
1024 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001025 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001026 handleLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 } break;
1028 case RELAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001029 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001030 handleRelaunchActivity(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 } break;
1032 case PAUSE_ACTIVITY:
1033 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001034 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 break;
1036 case PAUSE_ACTIVITY_FINISHING:
1037 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
1038 break;
1039 case STOP_ACTIVITY_SHOW:
1040 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
1041 break;
1042 case STOP_ACTIVITY_HIDE:
1043 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
1044 break;
1045 case SHOW_WINDOW:
1046 handleWindowVisibility((IBinder)msg.obj, true);
1047 break;
1048 case HIDE_WINDOW:
1049 handleWindowVisibility((IBinder)msg.obj, false);
1050 break;
1051 case RESUME_ACTIVITY:
1052 handleResumeActivity((IBinder)msg.obj, true,
1053 msg.arg1 != 0);
1054 break;
1055 case SEND_RESULT:
1056 handleSendResult((ResultData)msg.obj);
1057 break;
1058 case DESTROY_ACTIVITY:
1059 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1060 msg.arg2, false);
1061 break;
1062 case BIND_APPLICATION:
1063 AppBindData data = (AppBindData)msg.obj;
1064 handleBindApplication(data);
1065 break;
1066 case EXIT_APPLICATION:
1067 if (mInitialApplication != null) {
1068 mInitialApplication.onTerminate();
1069 }
1070 Looper.myLooper().quit();
1071 break;
1072 case NEW_INTENT:
1073 handleNewIntent((NewIntentData)msg.obj);
1074 break;
1075 case RECEIVER:
1076 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001077 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 break;
1079 case CREATE_SERVICE:
1080 handleCreateService((CreateServiceData)msg.obj);
1081 break;
1082 case BIND_SERVICE:
1083 handleBindService((BindServiceData)msg.obj);
1084 break;
1085 case UNBIND_SERVICE:
1086 handleUnbindService((BindServiceData)msg.obj);
1087 break;
1088 case SERVICE_ARGS:
1089 handleServiceArgs((ServiceArgsData)msg.obj);
1090 break;
1091 case STOP_SERVICE:
1092 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001093 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 break;
1095 case REQUEST_THUMBNAIL:
1096 handleRequestThumbnail((IBinder)msg.obj);
1097 break;
1098 case CONFIGURATION_CHANGED:
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001099 handleConfigurationChanged((Configuration)msg.obj, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 break;
1101 case CLEAN_UP_CONTEXT:
1102 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1103 cci.context.performFinalCleanup(cci.who, cci.what);
1104 break;
1105 case GC_WHEN_IDLE:
1106 scheduleGcIdler();
1107 break;
1108 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001109 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 break;
1111 case LOW_MEMORY:
1112 handleLowMemory();
1113 break;
1114 case ACTIVITY_CONFIGURATION_CHANGED:
1115 handleActivityConfigurationChanged((IBinder)msg.obj);
1116 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001117 case PROFILER_CONTROL:
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001118 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001119 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001120 case CREATE_BACKUP_AGENT:
1121 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
1122 break;
1123 case DESTROY_BACKUP_AGENT:
1124 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
1125 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001126 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001127 Process.killProcess(Process.myPid());
1128 break;
1129 case REMOVE_PROVIDER:
1130 completeRemoveProvider((IContentProvider)msg.obj);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001131 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001132 case ENABLE_JIT:
1133 ensureJitEnabled();
1134 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001135 case DISPATCH_PACKAGE_BROADCAST:
1136 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
1137 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001138 case SCHEDULE_CRASH:
1139 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001140 case DUMP_HEAP:
1141 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1142 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001143 case DUMP_ACTIVITY:
1144 handleDumpActivity((DumpComponentInfo)msg.obj);
1145 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001146 case SLEEPING:
1147 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
1148 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001149 case SET_CORE_SETTINGS:
1150 handleSetCoreSettings((Bundle) msg.obj);
1151 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001152 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1153 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07001155 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
Bob Leee5408332009-09-04 18:31:17 -07001157
Brian Carlstromed7e0072011-03-24 13:27:57 -07001158 private void maybeSnapshot() {
1159 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001160 // convert the *private* ActivityThread.PackageInfo to *public* known
1161 // android.content.pm.PackageInfo
1162 String packageName = mBoundApplication.info.mPackageName;
1163 android.content.pm.PackageInfo packageInfo = null;
1164 try {
1165 Context context = getSystemContext();
1166 if(context == null) {
1167 Log.e(TAG, "cannot get a valid context");
1168 return;
1169 }
1170 PackageManager pm = context.getPackageManager();
1171 if(pm == null) {
1172 Log.e(TAG, "cannot get a valid PackageManager");
1173 return;
1174 }
1175 packageInfo = pm.getPackageInfo(
1176 packageName, PackageManager.GET_ACTIVITIES);
1177 } catch (NameNotFoundException e) {
1178 Log.e(TAG, "cannot get package info for " + packageName, e);
1179 }
1180 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001181 }
1182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 }
1184
1185 private final class Idler implements MessageQueue.IdleHandler {
1186 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001187 ActivityClientRecord a = mNewActivities;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 if (a != null) {
1189 mNewActivities = null;
1190 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001191 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001193 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 TAG, "Reporting idle of " + a +
1195 " finished=" +
1196 (a.activity != null ? a.activity.mFinished : false));
1197 if (a.activity != null && !a.activity.mFinished) {
1198 try {
Dianne Hackborne88846e2009-09-30 21:34:25 -07001199 am.activityIdle(a.token, a.createdConfig);
1200 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 } catch (RemoteException ex) {
1202 }
1203 }
1204 prev = a;
1205 a = a.nextIdle;
1206 prev.nextIdle = null;
1207 } while (a != null);
1208 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001209 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 return false;
1211 }
1212 }
1213
1214 final class GcIdler implements MessageQueue.IdleHandler {
1215 public final boolean queueIdle() {
1216 doGcIfNeeded();
1217 return false;
1218 }
1219 }
1220
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001221 private final static class ResourcesKey {
1222 final private String mResDir;
1223 final private float mScale;
1224 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001225
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001226 ResourcesKey(String resDir, float scale) {
1227 mResDir = resDir;
1228 mScale = scale;
1229 mHash = mResDir.hashCode() << 2 + (int) (mScale * 2);
1230 }
Bob Leee5408332009-09-04 18:31:17 -07001231
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001232 @Override
1233 public int hashCode() {
1234 return mHash;
1235 }
1236
1237 @Override
1238 public boolean equals(Object obj) {
1239 if (!(obj instanceof ResourcesKey)) {
1240 return false;
1241 }
1242 ResourcesKey peer = (ResourcesKey) obj;
1243 return mResDir.equals(peer.mResDir) && mScale == peer.mScale;
1244 }
1245 }
1246
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001247 public static final ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001248 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001251 public static final String currentPackageName() {
1252 ActivityThread am = currentActivityThread();
1253 return (am != null && am.mBoundApplication != null)
1254 ? am.mBoundApplication.processName : null;
1255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001257 public static final Application currentApplication() {
1258 ActivityThread am = currentActivityThread();
1259 return am != null ? am.mInitialApplication : null;
1260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001262 public static IPackageManager getPackageManager() {
1263 if (sPackageManager != null) {
1264 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1265 return sPackageManager;
1266 }
1267 IBinder b = ServiceManager.getService("package");
1268 //Slog.v("PackageManager", "default service binder = " + b);
1269 sPackageManager = IPackageManager.Stub.asInterface(b);
1270 //Slog.v("PackageManager", "default service = " + sPackageManager);
1271 return sPackageManager;
1272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001274 DisplayMetrics getDisplayMetricsLocked(boolean forceUpdate) {
1275 if (mDisplayMetrics != null && !forceUpdate) {
1276 return mDisplayMetrics;
1277 }
1278 if (mDisplay == null) {
1279 WindowManager wm = WindowManagerImpl.getDefault();
1280 mDisplay = wm.getDefaultDisplay();
1281 }
1282 DisplayMetrics metrics = mDisplayMetrics = new DisplayMetrics();
1283 mDisplay.getMetrics(metrics);
1284 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1285 // + metrics.heightPixels + " den=" + metrics.density
1286 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
1287 return metrics;
1288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001290 /**
1291 * Creates the top level Resources for applications with the given compatibility info.
1292 *
1293 * @param resDir the resource directory.
1294 * @param compInfo the compability info. It will use the default compatibility info when it's
1295 * null.
1296 */
1297 Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) {
1298 ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale);
1299 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001301 // Resources is app scale dependent.
1302 if (false) {
1303 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1304 + compInfo.applicationScale);
1305 }
1306 WeakReference<Resources> wr = mActiveResources.get(key);
1307 r = wr != null ? wr.get() : null;
1308 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1309 if (r != null && r.getAssets().isUpToDate()) {
1310 if (false) {
1311 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1312 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1313 }
1314 return r;
1315 }
1316 }
1317
1318 //if (r != null) {
1319 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1320 // + r + " " + resDir);
1321 //}
1322
1323 AssetManager assets = new AssetManager();
1324 if (assets.addAssetPath(resDir) == 0) {
1325 return null;
1326 }
1327
1328 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
1329 DisplayMetrics metrics = getDisplayMetricsLocked(false);
1330 r = new Resources(assets, metrics, getConfiguration(), compInfo);
1331 if (false) {
1332 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1333 + r.getConfiguration() + " appScale="
1334 + r.getCompatibilityInfo().applicationScale);
1335 }
1336
1337 synchronized (mPackages) {
1338 WeakReference<Resources> wr = mActiveResources.get(key);
1339 Resources existing = wr != null ? wr.get() : null;
1340 if (existing != null && existing.getAssets().isUpToDate()) {
1341 // Someone else already created the resources while we were
1342 // unlocked; go ahead and use theirs.
1343 r.getAssets().close();
1344 return existing;
1345 }
1346
1347 // XXX need to remove entries when weak references go away
1348 mActiveResources.put(key, new WeakReference<Resources>(r));
1349 return r;
1350 }
1351 }
1352
1353 /**
1354 * Creates the top level resources for the given package.
1355 */
1356 Resources getTopLevelResources(String resDir, LoadedApk pkgInfo) {
1357 return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo);
1358 }
1359
1360 final Handler getHandler() {
1361 return mH;
1362 }
1363
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001364 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1365 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001366 synchronized (mPackages) {
1367 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1369 ref = mPackages.get(packageName);
1370 } else {
1371 ref = mResourcePackages.get(packageName);
1372 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001373 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001374 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001375 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1376 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 if (packageInfo != null && (packageInfo.mResources == null
1378 || packageInfo.mResources.getAssets().isUpToDate())) {
1379 if (packageInfo.isSecurityViolation()
1380 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1381 throw new SecurityException(
1382 "Requesting code from " + packageName
1383 + " to be run in process "
1384 + mBoundApplication.processName
1385 + "/" + mBoundApplication.appInfo.uid);
1386 }
1387 return packageInfo;
1388 }
1389 }
1390
1391 ApplicationInfo ai = null;
1392 try {
1393 ai = getPackageManager().getApplicationInfo(packageName,
1394 PackageManager.GET_SHARED_LIBRARY_FILES);
1395 } catch (RemoteException e) {
1396 }
1397
1398 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001399 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 }
1401
1402 return null;
1403 }
1404
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001405 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1406 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1408 boolean securityViolation = includeCode && ai.uid != 0
1409 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
1410 ? ai.uid != mBoundApplication.appInfo.uid : true);
1411 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1412 |Context.CONTEXT_IGNORE_SECURITY))
1413 == Context.CONTEXT_INCLUDE_CODE) {
1414 if (securityViolation) {
1415 String msg = "Requesting code from " + ai.packageName
1416 + " (with uid " + ai.uid + ")";
1417 if (mBoundApplication != null) {
1418 msg = msg + " to be run in process "
1419 + mBoundApplication.processName + " (with uid "
1420 + mBoundApplication.appInfo.uid + ")";
1421 }
1422 throw new SecurityException(msg);
1423 }
1424 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001425 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 }
1427
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001428 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1429 CompatibilityInfo compatInfo) {
1430 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 }
1432
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001433 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1434 synchronized (mPackages) {
1435 WeakReference<LoadedApk> ref;
1436 if (includeCode) {
1437 ref = mPackages.get(packageName);
1438 } else {
1439 ref = mResourcePackages.get(packageName);
1440 }
1441 return ref != null ? ref.get() : null;
1442 }
1443 }
1444
1445 private final LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1447 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001448 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 if (includeCode) {
1450 ref = mPackages.get(aInfo.packageName);
1451 } else {
1452 ref = mResourcePackages.get(aInfo.packageName);
1453 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001454 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 if (packageInfo == null || (packageInfo.mResources != null
1456 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001457 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 : "Loading resource-only package ") + aInfo.packageName
1459 + " (in " + (mBoundApplication != null
1460 ? mBoundApplication.processName : null)
1461 + ")");
1462 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001463 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 securityViolation, includeCode &&
1465 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1466 if (includeCode) {
1467 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001468 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 } else {
1470 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001471 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 }
1473 }
1474 return packageInfo;
1475 }
1476 }
1477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 ActivityThread() {
1479 }
1480
1481 public ApplicationThread getApplicationThread()
1482 {
1483 return mAppThread;
1484 }
1485
1486 public Instrumentation getInstrumentation()
1487 {
1488 return mInstrumentation;
1489 }
1490
1491 public Configuration getConfiguration() {
1492 return mConfiguration;
1493 }
1494
1495 public boolean isProfiling() {
1496 return mBoundApplication != null && mBoundApplication.profileFile != null;
1497 }
1498
1499 public String getProfileFilePath() {
1500 return mBoundApplication.profileFile;
1501 }
1502
1503 public Looper getLooper() {
1504 return mLooper;
1505 }
1506
1507 public Application getApplication() {
1508 return mInitialApplication;
1509 }
Bob Leee5408332009-09-04 18:31:17 -07001510
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001511 public String getProcessName() {
1512 return mBoundApplication.processName;
1513 }
Bob Leee5408332009-09-04 18:31:17 -07001514
Dianne Hackborn21556372010-02-04 16:34:40 -08001515 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 synchronized (this) {
1517 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001518 ContextImpl context =
1519 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001520 LoadedApk info = new LoadedApk(this, "android", context, null,
1521 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 context.init(info, null, this);
1523 context.getResources().updateConfiguration(
1524 getConfiguration(), getDisplayMetricsLocked(false));
1525 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001526 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 // + ": " + context.getResources().getConfiguration());
1528 }
1529 }
1530 return mSystemContext;
1531 }
1532
Mike Cleron432b7132009-09-24 15:28:29 -07001533 public void installSystemApplicationInfo(ApplicationInfo info) {
1534 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001535 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001536 context.init(new LoadedApk(this, "android", context, info,
1537 new CompatibilityInfo(info, 0, false)), null, this);
Mike Cleron432b7132009-09-24 15:28:29 -07001538 }
1539 }
1540
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001541 void ensureJitEnabled() {
1542 if (!mJitEnabled) {
1543 mJitEnabled = true;
1544 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1545 }
1546 }
1547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 void scheduleGcIdler() {
1549 if (!mGcIdlerScheduled) {
1550 mGcIdlerScheduled = true;
1551 Looper.myQueue().addIdleHandler(mGcIdler);
1552 }
1553 mH.removeMessages(H.GC_WHEN_IDLE);
1554 }
1555
1556 void unscheduleGcIdler() {
1557 if (mGcIdlerScheduled) {
1558 mGcIdlerScheduled = false;
1559 Looper.myQueue().removeIdleHandler(mGcIdler);
1560 }
1561 mH.removeMessages(H.GC_WHEN_IDLE);
1562 }
1563
1564 void doGcIfNeeded() {
1565 mGcIdlerScheduled = false;
1566 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001567 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 // + "m now=" + now);
1569 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001570 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 BinderInternal.forceGc("bg");
1572 }
1573 }
1574
Jeff Hamilton52d32032011-01-08 15:31:26 -06001575 public void registerOnActivityPausedListener(Activity activity,
1576 OnActivityPausedListener listener) {
1577 synchronized (mOnPauseListeners) {
1578 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1579 if (list == null) {
1580 list = new ArrayList<OnActivityPausedListener>();
1581 mOnPauseListeners.put(activity, list);
1582 }
1583 list.add(listener);
1584 }
1585 }
1586
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001587 public void unregisterOnActivityPausedListener(Activity activity,
1588 OnActivityPausedListener listener) {
1589 synchronized (mOnPauseListeners) {
1590 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1591 if (list != null) {
1592 list.remove(listener);
1593 }
1594 }
1595 }
1596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 public final ActivityInfo resolveActivityInfo(Intent intent) {
1598 ActivityInfo aInfo = intent.resolveActivityInfo(
1599 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1600 if (aInfo == null) {
1601 // Throw an exception.
1602 Instrumentation.checkStartActivityResult(
1603 IActivityManager.START_CLASS_NOT_FOUND, intent);
1604 }
1605 return aInfo;
1606 }
Bob Leee5408332009-09-04 18:31:17 -07001607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001610 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001611 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001613 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 r.intent = intent;
1615 r.state = state;
1616 r.parent = parent;
1617 r.embeddedID = id;
1618 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001619 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 if (localLOGV) {
1621 ComponentName compname = intent.getComponent();
1622 String name;
1623 if (compname != null) {
1624 name = compname.toShortString();
1625 } else {
1626 name = "(Intent " + intent + ").getComponent() returned null";
1627 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001628 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 + ", comp=" + name
1630 + ", token=" + token);
1631 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001632 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 }
1634
1635 public final Activity getActivity(IBinder token) {
1636 return mActivities.get(token).activity;
1637 }
1638
1639 public final void sendActivityResult(
1640 IBinder token, String id, int requestCode,
1641 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001642 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001643 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1645 list.add(new ResultInfo(id, requestCode, resultCode, data));
1646 mAppThread.scheduleSendResult(token, list);
1647 }
1648
1649 // if the thread hasn't started yet, we don't have the handler, so just
1650 // save the messages until we're ready.
1651 private final void queueOrSendMessage(int what, Object obj) {
1652 queueOrSendMessage(what, obj, 0, 0);
1653 }
1654
1655 private final void queueOrSendMessage(int what, Object obj, int arg1) {
1656 queueOrSendMessage(what, obj, arg1, 0);
1657 }
1658
1659 private final void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
1660 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001661 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1663 + ": " + arg1 + " / " + obj);
1664 Message msg = Message.obtain();
1665 msg.what = what;
1666 msg.obj = obj;
1667 msg.arg1 = arg1;
1668 msg.arg2 = arg2;
1669 mH.sendMessage(msg);
1670 }
1671 }
1672
Dianne Hackborn21556372010-02-04 16:34:40 -08001673 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 String what) {
1675 ContextCleanupInfo cci = new ContextCleanupInfo();
1676 cci.context = context;
1677 cci.who = who;
1678 cci.what = what;
1679 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1680 }
1681
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001682 private final Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1684
1685 ActivityInfo aInfo = r.activityInfo;
1686 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001687 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 Context.CONTEXT_INCLUDE_CODE);
1689 }
Bob Leee5408332009-09-04 18:31:17 -07001690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 ComponentName component = r.intent.getComponent();
1692 if (component == null) {
1693 component = r.intent.resolveActivity(
1694 mInitialApplication.getPackageManager());
1695 r.intent.setComponent(component);
1696 }
1697
1698 if (r.activityInfo.targetActivity != null) {
1699 component = new ComponentName(r.activityInfo.packageName,
1700 r.activityInfo.targetActivity);
1701 }
1702
1703 Activity activity = null;
1704 try {
1705 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
1706 activity = mInstrumentation.newActivity(
1707 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08001708 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 r.intent.setExtrasClassLoader(cl);
1710 if (r.state != null) {
1711 r.state.setClassLoader(cl);
1712 }
1713 } catch (Exception e) {
1714 if (!mInstrumentation.onException(activity, e)) {
1715 throw new RuntimeException(
1716 "Unable to instantiate activity " + component
1717 + ": " + e.toString(), e);
1718 }
1719 }
1720
1721 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001722 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001723
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001724 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
1725 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 TAG, r + ": app=" + app
1727 + ", appName=" + app.getPackageName()
1728 + ", pkg=" + r.packageInfo.getPackageName()
1729 + ", comp=" + r.intent.getComponent().toShortString()
1730 + ", dir=" + r.packageInfo.getAppDir());
1731
1732 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001733 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 appContext.init(r.packageInfo, r.token, this);
1735 appContext.setOuterContext(activity);
1736 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
1737 Configuration config = new Configuration(mConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001738 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07001739 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001740 activity.attach(appContext, this, getInstrumentation(), r.token,
1741 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001742 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07001743
Christopher Tateb70f3df2009-04-07 16:07:59 -07001744 if (customIntent != null) {
1745 activity.mIntent = customIntent;
1746 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001747 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 activity.mStartedActivity = false;
1749 int theme = r.activityInfo.getThemeResource();
1750 if (theme != 0) {
1751 activity.setTheme(theme);
1752 }
1753
1754 activity.mCalled = false;
1755 mInstrumentation.callActivityOnCreate(activity, r.state);
1756 if (!activity.mCalled) {
1757 throw new SuperNotCalledException(
1758 "Activity " + r.intent.getComponent().toShortString() +
1759 " did not call through to super.onCreate()");
1760 }
1761 r.activity = activity;
1762 r.stopped = true;
1763 if (!r.activity.mFinished) {
1764 activity.performStart();
1765 r.stopped = false;
1766 }
1767 if (!r.activity.mFinished) {
1768 if (r.state != null) {
1769 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
1770 }
1771 }
1772 if (!r.activity.mFinished) {
1773 activity.mCalled = false;
1774 mInstrumentation.callActivityOnPostCreate(activity, r.state);
1775 if (!activity.mCalled) {
1776 throw new SuperNotCalledException(
1777 "Activity " + r.intent.getComponent().toShortString() +
1778 " did not call through to super.onPostCreate()");
1779 }
1780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 }
1782 r.paused = true;
1783
1784 mActivities.put(r.token, r);
1785
1786 } catch (SuperNotCalledException e) {
1787 throw e;
1788
1789 } catch (Exception e) {
1790 if (!mInstrumentation.onException(activity, e)) {
1791 throw new RuntimeException(
1792 "Unable to start activity " + component
1793 + ": " + e.toString(), e);
1794 }
1795 }
1796
1797 return activity;
1798 }
1799
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001800 private final void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 // If we are getting ready to gc after going to the background, well
1802 // we are back active so skip it.
1803 unscheduleGcIdler();
1804
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001805 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001807 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808
1809 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08001810 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001811 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 handleResumeActivity(r.token, false, r.isForward);
1813
1814 if (!r.activity.mFinished && r.startsNotResumed) {
1815 // The activity manager actually wants this one to start out
1816 // paused, because it needs to be visible but isn't in the
1817 // foreground. We accomplish this by going through the
1818 // normal startup (because activities expect to go through
1819 // onResume() the first time they run, before their window
1820 // is displayed), and then pausing it. However, in this case
1821 // we do -not- need to do the full pause cycle (of freezing
1822 // and such) because the activity manager assumes it can just
1823 // retain the current state it has.
1824 try {
1825 r.activity.mCalled = false;
1826 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001827 // We need to keep around the original state, in case
1828 // we need to be created again.
1829 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 if (!r.activity.mCalled) {
1831 throw new SuperNotCalledException(
1832 "Activity " + r.intent.getComponent().toShortString() +
1833 " did not call through to super.onPause()");
1834 }
1835
1836 } catch (SuperNotCalledException e) {
1837 throw e;
1838
1839 } catch (Exception e) {
1840 if (!mInstrumentation.onException(r.activity, e)) {
1841 throw new RuntimeException(
1842 "Unable to pause activity "
1843 + r.intent.getComponent().toShortString()
1844 + ": " + e.toString(), e);
1845 }
1846 }
1847 r.paused = true;
1848 }
1849 } else {
1850 // If there was an error, for any reason, tell the activity
1851 // manager to stop us.
1852 try {
1853 ActivityManagerNative.getDefault()
1854 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
1855 } catch (RemoteException ex) {
1856 }
1857 }
1858 }
1859
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001860 private final void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 List<Intent> intents) {
1862 final int N = intents.size();
1863 for (int i=0; i<N; i++) {
1864 Intent intent = intents.get(i);
1865 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001866 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
1868 }
1869 }
1870
1871 public final void performNewIntents(IBinder token,
1872 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001873 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 if (r != null) {
1875 final boolean resumed = !r.paused;
1876 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001877 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 mInstrumentation.callActivityOnPause(r.activity);
1879 }
1880 deliverNewIntents(r, intents);
1881 if (resumed) {
1882 mInstrumentation.callActivityOnResume(r.activity);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001883 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 }
1885 }
1886 }
Bob Leee5408332009-09-04 18:31:17 -07001887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 private final void handleNewIntent(NewIntentData data) {
1889 performNewIntents(data.token, data.intents);
1890 }
1891
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07001892 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
1893
1894 /**
1895 * Return the Intent that's currently being handled by a
1896 * BroadcastReceiver on this thread, or null if none.
1897 * @hide
1898 */
1899 public static Intent getIntentBeingBroadcast() {
1900 return sCurrentBroadcastIntent.get();
1901 }
1902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 private final void handleReceiver(ReceiverData data) {
1904 // If we are getting ready to gc after going to the background, well
1905 // we are back active so skip it.
1906 unscheduleGcIdler();
1907
1908 String component = data.intent.getComponent().getClassName();
1909
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001910 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001911 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912
1913 IActivityManager mgr = ActivityManagerNative.getDefault();
1914
1915 BroadcastReceiver receiver = null;
1916 try {
1917 java.lang.ClassLoader cl = packageInfo.getClassLoader();
1918 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07001919 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
1921 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07001922 if (DEBUG_BROADCAST) Slog.i(TAG,
1923 "Finishing failed broadcast to " + data.intent.getComponent());
1924 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 throw new RuntimeException(
1926 "Unable to instantiate receiver " + component
1927 + ": " + e.toString(), e);
1928 }
1929
1930 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001931 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001932
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001933 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 TAG, "Performing receive of " + data.intent
1935 + ": app=" + app
1936 + ", appName=" + app.getPackageName()
1937 + ", pkg=" + packageInfo.getPackageName()
1938 + ", comp=" + data.intent.getComponent().toShortString()
1939 + ", dir=" + packageInfo.getAppDir());
1940
Dianne Hackborn21556372010-02-04 16:34:40 -08001941 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07001942 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07001943 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 receiver.onReceive(context.getReceiverRestrictedContext(),
1945 data.intent);
1946 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07001947 if (DEBUG_BROADCAST) Slog.i(TAG,
1948 "Finishing failed broadcast to " + data.intent.getComponent());
1949 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 if (!mInstrumentation.onException(receiver, e)) {
1951 throw new RuntimeException(
1952 "Unable to start receiver " + component
1953 + ": " + e.toString(), e);
1954 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07001955 } finally {
1956 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 }
1958
Dianne Hackborne829fef2010-10-26 17:44:01 -07001959 if (receiver.getPendingResult() != null) {
1960 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
1962 }
1963
Christopher Tate181fafa2009-05-14 11:12:14 -07001964 // Instantiate a BackupAgent and tell it that it's alive
1965 private final void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001966 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07001967
1968 // no longer idle; we have backup work to do
1969 unscheduleGcIdler();
1970
1971 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001972 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07001973 String packageName = packageInfo.mPackageName;
1974 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001975 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07001976 + " already exists");
1977 return;
1978 }
Bob Leee5408332009-09-04 18:31:17 -07001979
Christopher Tate181fafa2009-05-14 11:12:14 -07001980 BackupAgent agent = null;
1981 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07001982
1983 if (data.backupMode == IApplicationThread.BACKUP_MODE_FULL) {
1984 classname = "android.app.backup.FullBackupAgent";
1985 if ((data.appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
1986 // system packages can supply their own full-backup agent
1987 if (data.appInfo.fullBackupAgentName != null) {
1988 classname = data.appInfo.fullBackupAgentName;
1989 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001990 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001991 }
Christopher Tate4a627c72011-04-01 14:43:32 -07001992
Christopher Tate181fafa2009-05-14 11:12:14 -07001993 try {
Christopher Tated1475e02009-07-09 15:36:17 -07001994 IBinder binder = null;
1995 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07001996 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
1997
Christopher Tated1475e02009-07-09 15:36:17 -07001998 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07001999 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002000
2001 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002002 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002003 context.init(packageInfo, null, this);
2004 context.setOuterContext(agent);
2005 agent.attach(context);
2006
2007 agent.onCreate();
2008 binder = agent.onBind();
2009 mBackupAgents.put(packageName, agent);
2010 } catch (Exception e) {
2011 // If this is during restore, fail silently; otherwise go
2012 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002013 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tated1475e02009-07-09 15:36:17 -07002014 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE) {
2015 throw e;
2016 }
2017 // falling through with 'binder' still null
2018 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002019
2020 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002021 try {
2022 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2023 } catch (RemoteException e) {
2024 // nothing to do.
2025 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002026 } catch (Exception e) {
2027 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002028 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002029 }
2030 }
2031
2032 // Tear down a BackupAgent
2033 private final void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002034 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002035
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002036 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002037 String packageName = packageInfo.mPackageName;
2038 BackupAgent agent = mBackupAgents.get(packageName);
2039 if (agent != null) {
2040 try {
2041 agent.onDestroy();
2042 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002043 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002044 e.printStackTrace();
2045 }
2046 mBackupAgents.remove(packageName);
2047 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002048 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002049 }
2050 }
2051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 private final void handleCreateService(CreateServiceData data) {
2053 // If we are getting ready to gc after going to the background, well
2054 // we are back active so skip it.
2055 unscheduleGcIdler();
2056
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002057 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002058 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 Service service = null;
2060 try {
2061 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2062 service = (Service) cl.loadClass(data.info.name).newInstance();
2063 } catch (Exception e) {
2064 if (!mInstrumentation.onException(service, e)) {
2065 throw new RuntimeException(
2066 "Unable to instantiate service " + data.info.name
2067 + ": " + e.toString(), e);
2068 }
2069 }
2070
2071 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002072 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073
Dianne Hackborn21556372010-02-04 16:34:40 -08002074 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 context.init(packageInfo, null, this);
2076
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002077 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 context.setOuterContext(service);
2079 service.attach(context, this, data.info.name, data.token, app,
2080 ActivityManagerNative.getDefault());
2081 service.onCreate();
2082 mServices.put(data.token, service);
2083 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002084 ActivityManagerNative.getDefault().serviceDoneExecuting(
2085 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 } catch (RemoteException e) {
2087 // nothing to do.
2088 }
2089 } catch (Exception e) {
2090 if (!mInstrumentation.onException(service, e)) {
2091 throw new RuntimeException(
2092 "Unable to create service " + data.info.name
2093 + ": " + e.toString(), e);
2094 }
2095 }
2096 }
2097
2098 private final void handleBindService(BindServiceData data) {
2099 Service s = mServices.get(data.token);
2100 if (s != null) {
2101 try {
2102 data.intent.setExtrasClassLoader(s.getClassLoader());
2103 try {
2104 if (!data.rebind) {
2105 IBinder binder = s.onBind(data.intent);
2106 ActivityManagerNative.getDefault().publishService(
2107 data.token, data.intent, binder);
2108 } else {
2109 s.onRebind(data.intent);
2110 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002111 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002113 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 } catch (RemoteException ex) {
2115 }
2116 } catch (Exception e) {
2117 if (!mInstrumentation.onException(s, e)) {
2118 throw new RuntimeException(
2119 "Unable to bind to service " + s
2120 + " with " + data.intent + ": " + e.toString(), e);
2121 }
2122 }
2123 }
2124 }
2125
2126 private final void handleUnbindService(BindServiceData data) {
2127 Service s = mServices.get(data.token);
2128 if (s != null) {
2129 try {
2130 data.intent.setExtrasClassLoader(s.getClassLoader());
2131 boolean doRebind = s.onUnbind(data.intent);
2132 try {
2133 if (doRebind) {
2134 ActivityManagerNative.getDefault().unbindFinished(
2135 data.token, data.intent, doRebind);
2136 } else {
2137 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002138 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 }
2140 } catch (RemoteException ex) {
2141 }
2142 } catch (Exception e) {
2143 if (!mInstrumentation.onException(s, e)) {
2144 throw new RuntimeException(
2145 "Unable to unbind to service " + s
2146 + " with " + data.intent + ": " + e.toString(), e);
2147 }
2148 }
2149 }
2150 }
2151
Dianne Hackborn625ac272010-09-17 18:29:22 -07002152 private void handleDumpService(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002153 Service s = mServices.get(info.token);
2154 if (s != null) {
2155 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2156 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2157 pw.flush();
2158 try {
2159 info.fd.close();
2160 } catch (IOException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 }
2162 }
2163 }
2164
Dianne Hackborn625ac272010-09-17 18:29:22 -07002165 private void handleDumpActivity(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002166 ActivityClientRecord r = mActivities.get(info.token);
2167 if (r != null && r.activity != null) {
2168 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2169 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2170 pw.flush();
2171 try {
2172 info.fd.close();
2173 } catch (IOException e) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07002174 }
2175 }
2176 }
2177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 private final void handleServiceArgs(ServiceArgsData data) {
2179 Service s = mServices.get(data.token);
2180 if (s != null) {
2181 try {
2182 if (data.args != null) {
2183 data.args.setExtrasClassLoader(s.getClassLoader());
2184 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002185 int res;
2186 if (!data.taskRemoved) {
2187 res = s.onStartCommand(data.args, data.flags, data.startId);
2188 } else {
2189 s.onTaskRemoved(data.args);
2190 res = Service.START_TASK_REMOVED_COMPLETE;
2191 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002192
2193 QueuedWork.waitToFinish();
2194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002196 ActivityManagerNative.getDefault().serviceDoneExecuting(
2197 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 } catch (RemoteException e) {
2199 // nothing to do.
2200 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002201 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 } catch (Exception e) {
2203 if (!mInstrumentation.onException(s, e)) {
2204 throw new RuntimeException(
2205 "Unable to start service " + s
2206 + " with " + data.args + ": " + e.toString(), e);
2207 }
2208 }
2209 }
2210 }
2211
2212 private final void handleStopService(IBinder token) {
2213 Service s = mServices.remove(token);
2214 if (s != null) {
2215 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002216 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 s.onDestroy();
2218 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002219 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002221 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002223
2224 QueuedWork.waitToFinish();
2225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002227 ActivityManagerNative.getDefault().serviceDoneExecuting(
2228 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 } catch (RemoteException e) {
2230 // nothing to do.
2231 }
2232 } catch (Exception e) {
2233 if (!mInstrumentation.onException(s, e)) {
2234 throw new RuntimeException(
2235 "Unable to stop service " + s
2236 + ": " + e.toString(), e);
2237 }
2238 }
2239 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002240 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 }
2242
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002243 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002245 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002246 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 + " finished=" + r.activity.mFinished);
2248 if (r != null && !r.activity.mFinished) {
2249 if (clearHide) {
2250 r.hideForNow = false;
2251 r.activity.mStartedActivity = false;
2252 }
2253 try {
2254 if (r.pendingIntents != null) {
2255 deliverNewIntents(r, r.pendingIntents);
2256 r.pendingIntents = null;
2257 }
2258 if (r.pendingResults != null) {
2259 deliverResults(r, r.pendingResults);
2260 r.pendingResults = null;
2261 }
2262 r.activity.performResume();
2263
Bob Leee5408332009-09-04 18:31:17 -07002264 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 r.paused = false;
2268 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 r.state = null;
2270 } catch (Exception e) {
2271 if (!mInstrumentation.onException(r.activity, e)) {
2272 throw new RuntimeException(
2273 "Unable to resume activity "
2274 + r.intent.getComponent().toShortString()
2275 + ": " + e.toString(), e);
2276 }
2277 }
2278 }
2279 return r;
2280 }
2281
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002282 final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
2283 if (r.mPendingRemoveWindow != null) {
2284 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2285 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2286 if (wtoken != null) {
2287 WindowManagerImpl.getDefault().closeAll(wtoken,
2288 r.activity.getClass().getName(), "Activity");
2289 }
2290 }
2291 r.mPendingRemoveWindow = null;
2292 r.mPendingRemoveWindowManager = null;
2293 }
2294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2296 // If we are getting ready to gc after going to the background, well
2297 // we are back active so skip it.
2298 unscheduleGcIdler();
2299
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002300 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301
2302 if (r != null) {
2303 final Activity a = r.activity;
2304
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002305 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 TAG, "Resume " + r + " started activity: " +
2307 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2308 + ", finished: " + a.mFinished);
2309
2310 final int forwardBit = isForward ?
2311 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 // If the window hasn't yet been added to the window manager,
2314 // and this guy didn't finish itself or start another activity,
2315 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002316 boolean willBeVisible = !a.mStartedActivity;
2317 if (!willBeVisible) {
2318 try {
2319 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2320 a.getActivityToken());
2321 } catch (RemoteException e) {
2322 }
2323 }
2324 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 r.window = r.activity.getWindow();
2326 View decor = r.window.getDecorView();
2327 decor.setVisibility(View.INVISIBLE);
2328 ViewManager wm = a.getWindowManager();
2329 WindowManager.LayoutParams l = r.window.getAttributes();
2330 a.mDecor = decor;
2331 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2332 l.softInputMode |= forwardBit;
2333 if (a.mVisibleFromClient) {
2334 a.mWindowAdded = true;
2335 wm.addView(decor, l);
2336 }
2337
2338 // If the window has already been added, but during resume
2339 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002340 // window visible.
2341 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002342 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 TAG, "Launch " + r + " mStartedActivity set");
2344 r.hideForNow = true;
2345 }
2346
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002347 // Get rid of anything left hanging around.
2348 cleanUpPendingRemoveWindows(r);
2349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 // The window is now visible if it has been added, we are not
2351 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002352 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002353 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002355 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002356 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 performConfigurationChanged(r.activity, r.newConfig);
2358 r.newConfig = null;
2359 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002360 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 + isForward);
2362 WindowManager.LayoutParams l = r.window.getAttributes();
2363 if ((l.softInputMode
2364 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2365 != forwardBit) {
2366 l.softInputMode = (l.softInputMode
2367 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2368 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002369 if (r.activity.mVisibleFromClient) {
2370 ViewManager wm = a.getWindowManager();
2371 View decor = r.window.getDecorView();
2372 wm.updateViewLayout(decor, l);
2373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 }
2375 r.activity.mVisibleFromServer = true;
2376 mNumVisibleActivities++;
2377 if (r.activity.mVisibleFromClient) {
2378 r.activity.makeVisible();
2379 }
2380 }
2381
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002382 if (!r.onlyLocalRequest) {
2383 r.nextIdle = mNewActivities;
2384 mNewActivities = r;
2385 if (localLOGV) Slog.v(
2386 TAG, "Scheduling idle handler for " + r);
2387 Looper.myQueue().addIdleHandler(new Idler());
2388 }
2389 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390
2391 } else {
2392 // If an exception was thrown when trying to resume, then
2393 // just end this activity.
2394 try {
2395 ActivityManagerNative.getDefault()
2396 .finishActivity(token, Activity.RESULT_CANCELED, null);
2397 } catch (RemoteException ex) {
2398 }
2399 }
2400 }
2401
2402 private int mThumbnailWidth = -1;
2403 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002404 private Bitmap mAvailThumbnailBitmap = null;
2405 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002407 private final Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002408 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002410 if (thumbnail == null) {
2411 int w = mThumbnailWidth;
2412 int h;
2413 if (w < 0) {
2414 Resources res = r.activity.getResources();
2415 mThumbnailHeight = h =
2416 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002418 mThumbnailWidth = w =
2419 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2420 } else {
2421 h = mThumbnailHeight;
2422 }
2423
2424 // On platforms where we don't want thumbnails, set dims to (0,0)
2425 if ((w > 0) && (h > 0)) {
2426 thumbnail = Bitmap.createBitmap(w, h, THUMBNAIL_FORMAT);
2427 thumbnail.eraseColor(0);
2428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 }
2430
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002431 if (thumbnail != null) {
2432 Canvas cv = mThumbnailCanvas;
2433 if (cv == null) {
2434 mThumbnailCanvas = cv = new Canvas();
2435 }
2436
2437 cv.setBitmap(thumbnail);
2438 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2439 mAvailThumbnailBitmap = thumbnail;
2440 thumbnail = null;
2441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 } catch (Exception e) {
2445 if (!mInstrumentation.onException(r.activity, e)) {
2446 throw new RuntimeException(
2447 "Unable to create thumbnail of "
2448 + r.intent.getComponent().toShortString()
2449 + ": " + e.toString(), e);
2450 }
2451 thumbnail = null;
2452 }
2453
2454 return thumbnail;
2455 }
2456
2457 private final void handlePauseActivity(IBinder token, boolean finished,
2458 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002459 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002461 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 if (userLeaving) {
2463 performUserLeavingActivity(r);
2464 }
Bob Leee5408332009-09-04 18:31:17 -07002465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002467 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002469 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002470 if (r.isPreHoneycomb()) {
2471 QueuedWork.waitToFinish();
2472 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 // Tell the activity manager we have paused.
2475 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002476 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 } catch (RemoteException ex) {
2478 }
2479 }
2480 }
2481
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002482 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 mInstrumentation.callActivityOnUserLeaving(r.activity);
2484 }
2485
2486 final Bundle performPauseActivity(IBinder token, boolean finished,
2487 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002488 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 return r != null ? performPauseActivity(r, finished, saveState) : null;
2490 }
2491
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002492 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 boolean saveState) {
2494 if (r.paused) {
2495 if (r.activity.mFinished) {
2496 // If we are finishing, we won't call onResume() in certain cases.
2497 // So here we likewise don't want to call onPause() if the activity
2498 // isn't resumed.
2499 return null;
2500 }
2501 RuntimeException e = new RuntimeException(
2502 "Performing pause of activity that is not resumed: "
2503 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002504 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 }
2506 Bundle state = null;
2507 if (finished) {
2508 r.activity.mFinished = true;
2509 }
2510 try {
2511 // Next have the activity save its current state and managed dialogs...
2512 if (!r.activity.mFinished && saveState) {
2513 state = new Bundle();
2514 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2515 r.state = state;
2516 }
2517 // Now we are idle.
2518 r.activity.mCalled = false;
2519 mInstrumentation.callActivityOnPause(r.activity);
2520 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2521 if (!r.activity.mCalled) {
2522 throw new SuperNotCalledException(
2523 "Activity " + r.intent.getComponent().toShortString() +
2524 " did not call through to super.onPause()");
2525 }
2526
2527 } catch (SuperNotCalledException e) {
2528 throw e;
2529
2530 } catch (Exception e) {
2531 if (!mInstrumentation.onException(r.activity, e)) {
2532 throw new RuntimeException(
2533 "Unable to pause activity "
2534 + r.intent.getComponent().toShortString()
2535 + ": " + e.toString(), e);
2536 }
2537 }
2538 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002539
2540 // Notify any outstanding on paused listeners
2541 ArrayList<OnActivityPausedListener> listeners;
2542 synchronized (mOnPauseListeners) {
2543 listeners = mOnPauseListeners.remove(r.activity);
2544 }
2545 int size = (listeners != null ? listeners.size() : 0);
2546 for (int i = 0; i < size; i++) {
2547 listeners.get(i).onPaused(r.activity);
2548 }
2549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 return state;
2551 }
2552
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002553 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002554 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002555 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 }
2557
2558 private static class StopInfo {
2559 Bitmap thumbnail;
2560 CharSequence description;
2561 }
2562
2563 private final class ProviderRefCount {
2564 public int count;
2565 ProviderRefCount(int pCount) {
2566 count = pCount;
2567 }
2568 }
2569
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002570 /**
2571 * Core implementation of stopping an activity. Note this is a little
2572 * tricky because the server's meaning of stop is slightly different
2573 * than our client -- for the server, stop means to save state and give
2574 * it the result when it is done, but the window may still be visible.
2575 * For the client, we want to call onStop()/onStart() to indicate when
2576 * the activity's UI visibillity changes.
2577 */
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002578 private final void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002579 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002580 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002581 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 if (r != null) {
2583 if (!keepShown && r.stopped) {
2584 if (r.activity.mFinished) {
2585 // If we are finishing, we won't call onResume() in certain
2586 // cases. So here we likewise don't want to call onStop()
2587 // if the activity isn't resumed.
2588 return;
2589 }
2590 RuntimeException e = new RuntimeException(
2591 "Performing stop of activity that is not resumed: "
2592 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002593 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 }
2595
2596 if (info != null) {
2597 try {
2598 // First create a thumbnail for the activity...
Jim Miller0b2a6d02010-07-13 18:01:29 -07002599 info.thumbnail = createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 info.description = r.activity.onCreateDescription();
2601 } catch (Exception e) {
2602 if (!mInstrumentation.onException(r.activity, e)) {
2603 throw new RuntimeException(
2604 "Unable to save state of activity "
2605 + r.intent.getComponent().toShortString()
2606 + ": " + e.toString(), e);
2607 }
2608 }
2609 }
2610
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002611 // Next have the activity save its current state and managed dialogs...
2612 if (!r.activity.mFinished && saveState) {
2613 if (r.state == null) {
2614 state = new Bundle();
2615 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2616 r.state = state;
2617 } else {
2618 state = r.state;
2619 }
2620 }
2621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 if (!keepShown) {
2623 try {
2624 // Now we are idle.
2625 r.activity.performStop();
2626 } catch (Exception e) {
2627 if (!mInstrumentation.onException(r.activity, e)) {
2628 throw new RuntimeException(
2629 "Unable to stop activity "
2630 + r.intent.getComponent().toShortString()
2631 + ": " + e.toString(), e);
2632 }
2633 }
2634 r.stopped = true;
2635 }
2636
2637 r.paused = true;
2638 }
2639 }
2640
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002641 private final void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 View v = r.activity.mDecor;
2643 if (v != null) {
2644 if (show) {
2645 if (!r.activity.mVisibleFromServer) {
2646 r.activity.mVisibleFromServer = true;
2647 mNumVisibleActivities++;
2648 if (r.activity.mVisibleFromClient) {
2649 r.activity.makeVisible();
2650 }
2651 }
2652 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002653 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002654 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 performConfigurationChanged(r.activity, r.newConfig);
2656 r.newConfig = null;
2657 }
2658 } else {
2659 if (r.activity.mVisibleFromServer) {
2660 r.activity.mVisibleFromServer = false;
2661 mNumVisibleActivities--;
2662 v.setVisibility(View.INVISIBLE);
2663 }
2664 }
2665 }
2666 }
2667
2668 private final void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002669 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 r.activity.mConfigChangeFlags |= configChanges;
2671
2672 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002673 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002675 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 TAG, "Finishing stop of " + r + ": show=" + show
2677 + " win=" + r.window);
2678
2679 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07002680
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002681 // Make sure any pending writes are now committed.
2682 if (!r.isPreHoneycomb()) {
2683 QueuedWork.waitToFinish();
2684 }
2685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 // Tell activity manager we have been stopped.
2687 try {
2688 ActivityManagerNative.getDefault().activityStopped(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002689 r.token, r.state, info.thumbnail, info.description);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 } catch (RemoteException ex) {
2691 }
2692 }
2693
2694 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002695 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 if (r.stopped) {
2697 r.activity.performRestart();
2698 r.stopped = false;
2699 }
2700 }
2701
2702 private final void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002703 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08002704
2705 if (r == null) {
2706 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
2707 return;
2708 }
2709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002711 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 } else if (show && r.stopped) {
2713 // If we are getting ready to gc after going to the background, well
2714 // we are back active so skip it.
2715 unscheduleGcIdler();
2716
2717 r.activity.performRestart();
2718 r.stopped = false;
2719 }
2720 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07002721 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 TAG, "Handle window " + r + " visibility: " + show);
2723 updateVisibility(r, show);
2724 }
2725 }
2726
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002727 private final void handleSleeping(IBinder token, boolean sleeping) {
2728 ActivityClientRecord r = mActivities.get(token);
2729
2730 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002731 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002732 return;
2733 }
2734
2735 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002736 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002737 try {
2738 // Now we are idle.
2739 r.activity.performStop();
2740 } catch (Exception e) {
2741 if (!mInstrumentation.onException(r.activity, e)) {
2742 throw new RuntimeException(
2743 "Unable to stop activity "
2744 + r.intent.getComponent().toShortString()
2745 + ": " + e.toString(), e);
2746 }
2747 }
2748 r.stopped = true;
2749 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002750
2751 // Make sure any pending writes are now committed.
2752 if (!r.isPreHoneycomb()) {
2753 QueuedWork.waitToFinish();
2754 }
2755
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002756 // Tell activity manager we slept.
2757 try {
2758 ActivityManagerNative.getDefault().activitySlept(r.token);
2759 } catch (RemoteException ex) {
2760 }
2761 } else {
2762 if (r.stopped && r.activity.mVisibleFromServer) {
2763 r.activity.performRestart();
2764 r.stopped = false;
2765 }
2766 }
2767 }
2768
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002769 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08002770 synchronized (mPackages) {
2771 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002772 }
2773 }
2774
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002775 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
2776 LoadedApk apk = peekPackageInfo(data.pkg, false);
2777 if (apk != null) {
2778 apk.mCompatibilityInfo = data.info;
2779 }
2780 apk = peekPackageInfo(data.pkg, true);
2781 if (apk != null) {
2782 apk.mCompatibilityInfo = data.info;
2783 }
2784 handleConfigurationChanged(mConfiguration, data.info);
2785 }
2786
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002787 private final void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 final int N = results.size();
2789 for (int i=0; i<N; i++) {
2790 ResultInfo ri = results.get(i);
2791 try {
2792 if (ri.mData != null) {
2793 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
2794 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002795 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07002796 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 r.activity.dispatchActivityResult(ri.mResultWho,
2798 ri.mRequestCode, ri.mResultCode, ri.mData);
2799 } catch (Exception e) {
2800 if (!mInstrumentation.onException(r.activity, e)) {
2801 throw new RuntimeException(
2802 "Failure delivering result " + ri + " to activity "
2803 + r.intent.getComponent().toShortString()
2804 + ": " + e.toString(), e);
2805 }
2806 }
2807 }
2808 }
2809
2810 private final void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002811 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002812 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 if (r != null) {
2814 final boolean resumed = !r.paused;
2815 if (!r.activity.mFinished && r.activity.mDecor != null
2816 && r.hideForNow && resumed) {
2817 // We had hidden the activity because it started another
2818 // one... we have gotten a result back and we are not
2819 // paused, so make sure our window is visible.
2820 updateVisibility(r, true);
2821 }
2822 if (resumed) {
2823 try {
2824 // Now we are idle.
2825 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002826 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 mInstrumentation.callActivityOnPause(r.activity);
2828 if (!r.activity.mCalled) {
2829 throw new SuperNotCalledException(
2830 "Activity " + r.intent.getComponent().toShortString()
2831 + " did not call through to super.onPause()");
2832 }
2833 } catch (SuperNotCalledException e) {
2834 throw e;
2835 } catch (Exception e) {
2836 if (!mInstrumentation.onException(r.activity, e)) {
2837 throw new RuntimeException(
2838 "Unable to pause activity "
2839 + r.intent.getComponent().toShortString()
2840 + ": " + e.toString(), e);
2841 }
2842 }
2843 }
2844 deliverResults(r, res.results);
2845 if (resumed) {
2846 mInstrumentation.callActivityOnResume(r.activity);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002847 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 }
2849 }
2850 }
2851
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002852 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 return performDestroyActivity(token, finishing, 0, false);
2854 }
2855
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002856 private final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002858 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002859 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002860 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002862 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 r.activity.mConfigChangeFlags |= configChanges;
2864 if (finishing) {
2865 r.activity.mFinished = true;
2866 }
2867 if (!r.paused) {
2868 try {
2869 r.activity.mCalled = false;
2870 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07002871 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 r.activity.getComponentName().getClassName());
2873 if (!r.activity.mCalled) {
2874 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002875 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 + " did not call through to super.onPause()");
2877 }
2878 } catch (SuperNotCalledException e) {
2879 throw e;
2880 } catch (Exception e) {
2881 if (!mInstrumentation.onException(r.activity, e)) {
2882 throw new RuntimeException(
2883 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002884 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 + ": " + e.toString(), e);
2886 }
2887 }
2888 r.paused = true;
2889 }
2890 if (!r.stopped) {
2891 try {
2892 r.activity.performStop();
2893 } catch (SuperNotCalledException e) {
2894 throw e;
2895 } catch (Exception e) {
2896 if (!mInstrumentation.onException(r.activity, e)) {
2897 throw new RuntimeException(
2898 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002899 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 + ": " + e.toString(), e);
2901 }
2902 }
2903 r.stopped = true;
2904 }
2905 if (getNonConfigInstance) {
2906 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002907 r.lastNonConfigurationInstances
2908 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 } catch (Exception e) {
2910 if (!mInstrumentation.onException(r.activity, e)) {
2911 throw new RuntimeException(
2912 "Unable to retain activity "
2913 + r.intent.getComponent().toShortString()
2914 + ": " + e.toString(), e);
2915 }
2916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 }
2918 try {
2919 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002920 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 if (!r.activity.mCalled) {
2922 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002923 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 " did not call through to super.onDestroy()");
2925 }
2926 if (r.window != null) {
2927 r.window.closeAllPanels();
2928 }
2929 } catch (SuperNotCalledException e) {
2930 throw e;
2931 } catch (Exception e) {
2932 if (!mInstrumentation.onException(r.activity, e)) {
2933 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002934 "Unable to destroy activity " + safeToComponentShortString(r.intent)
2935 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 }
2937 }
2938 }
2939 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002940 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 return r;
2942 }
2943
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002944 private static String safeToComponentShortString(Intent intent) {
2945 ComponentName component = intent.getComponent();
2946 return component == null ? "[Unknown]" : component.toShortString();
2947 }
2948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 private final void handleDestroyActivity(IBinder token, boolean finishing,
2950 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002951 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 configChanges, getNonConfigInstance);
2953 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002954 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 WindowManager wm = r.activity.getWindowManager();
2956 View v = r.activity.mDecor;
2957 if (v != null) {
2958 if (r.activity.mVisibleFromServer) {
2959 mNumVisibleActivities--;
2960 }
2961 IBinder wtoken = v.getWindowToken();
2962 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002963 if (r.onlyLocalRequest) {
2964 // Hold off on removing this until the new activity's
2965 // window is being added.
2966 r.mPendingRemoveWindow = v;
2967 r.mPendingRemoveWindowManager = wm;
2968 } else {
2969 wm.removeViewImmediate(v);
2970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002972 if (wtoken != null && r.mPendingRemoveWindow == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 WindowManagerImpl.getDefault().closeAll(wtoken,
2974 r.activity.getClass().getName(), "Activity");
2975 }
2976 r.activity.mDecor = null;
2977 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002978 if (r.mPendingRemoveWindow == null) {
2979 // If we are delaying the removal of the activity window, then
2980 // we can't clean up all windows here. Note that we can't do
2981 // so later either, which means any windows that aren't closed
2982 // by the app will leak. Well we try to warning them a lot
2983 // about leaking windows, because that is a bug, so if they are
2984 // using this recreate facility then they get to live with leaks.
2985 WindowManagerImpl.getDefault().closeAll(token,
2986 r.activity.getClass().getName(), "Activity");
2987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988
2989 // Mocked out contexts won't be participating in the normal
2990 // process lifecycle, but if we're running with a proper
2991 // ApplicationContext we need to have it tear down things
2992 // cleanly.
2993 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002994 if (c instanceof ContextImpl) {
2995 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 r.activity.getClass().getName(), "Activity");
2997 }
2998 }
2999 if (finishing) {
3000 try {
3001 ActivityManagerNative.getDefault().activityDestroyed(token);
3002 } catch (RemoteException ex) {
3003 // If the system process has died, it's game over for everyone.
3004 }
3005 }
3006 }
3007
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003008 public final void requestRelaunchActivity(IBinder token,
3009 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3010 int configChanges, boolean notResumed, Configuration config,
3011 boolean fromServer) {
3012 ActivityClientRecord target = null;
3013
3014 synchronized (mPackages) {
3015 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3016 ActivityClientRecord r = mRelaunchingActivities.get(i);
3017 if (r.token == token) {
3018 target = r;
3019 if (pendingResults != null) {
3020 if (r.pendingResults != null) {
3021 r.pendingResults.addAll(pendingResults);
3022 } else {
3023 r.pendingResults = pendingResults;
3024 }
3025 }
3026 if (pendingNewIntents != null) {
3027 if (r.pendingIntents != null) {
3028 r.pendingIntents.addAll(pendingNewIntents);
3029 } else {
3030 r.pendingIntents = pendingNewIntents;
3031 }
3032 }
3033 break;
3034 }
3035 }
3036
3037 if (target == null) {
3038 target = new ActivityClientRecord();
3039 target.token = token;
3040 target.pendingResults = pendingResults;
3041 target.pendingIntents = pendingNewIntents;
3042 if (!fromServer) {
3043 ActivityClientRecord existing = mActivities.get(token);
3044 if (existing != null) {
3045 target.startsNotResumed = existing.paused;
3046 }
3047 target.onlyLocalRequest = true;
3048 }
3049 mRelaunchingActivities.add(target);
3050 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3051 }
3052
3053 if (fromServer) {
3054 target.startsNotResumed = notResumed;
3055 target.onlyLocalRequest = false;
3056 }
3057 if (config != null) {
3058 target.createdConfig = config;
3059 }
3060 target.pendingConfigChanges |= configChanges;
3061 }
3062 }
3063
3064 private final void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 // If we are getting ready to gc after going to the background, well
3066 // we are back active so skip it.
3067 unscheduleGcIdler();
3068
3069 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003070 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 // First: make sure we have the most recent configuration and most
3073 // recent version of the activity, or skip it if some previous call
3074 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003075 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 int N = mRelaunchingActivities.size();
3077 IBinder token = tmp.token;
3078 tmp = null;
3079 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003080 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 if (r.token == token) {
3082 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003083 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 mRelaunchingActivities.remove(i);
3085 i--;
3086 N--;
3087 }
3088 }
Bob Leee5408332009-09-04 18:31:17 -07003089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003091 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 return;
3093 }
Bob Leee5408332009-09-04 18:31:17 -07003094
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003095 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3096 + tmp.token + " with configChanges=0x"
3097 + Integer.toHexString(configChanges));
3098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 if (mPendingConfiguration != null) {
3100 changedConfig = mPendingConfiguration;
3101 mPendingConfiguration = null;
3102 }
3103 }
Bob Leee5408332009-09-04 18:31:17 -07003104
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003105 if (tmp.createdConfig != null) {
3106 // If the activity manager is passing us its current config,
3107 // assume that is really what we want regardless of what we
3108 // may have pending.
3109 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003110 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3111 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3112 if (changedConfig == null
3113 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3114 changedConfig = tmp.createdConfig;
3115 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003116 }
3117 }
3118
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003119 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003120 + tmp.token + ": changedConfig=" + changedConfig);
3121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 // If there was a pending configuration change, execute it first.
3123 if (changedConfig != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003124 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 }
Bob Leee5408332009-09-04 18:31:17 -07003126
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003127 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003128 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 if (r == null) {
3130 return;
3131 }
Bob Leee5408332009-09-04 18:31:17 -07003132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003134 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003135 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003136
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003137 r.activity.mChangingConfigurations = true;
3138
Dianne Hackborne2b04802010-12-09 09:24:55 -08003139 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003141 performPauseActivity(r.token, false, r.isPreHoneycomb());
3142 }
3143 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3144 r.state = new Bundle();
3145 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146 }
Bob Leee5408332009-09-04 18:31:17 -07003147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 r.activity = null;
3151 r.window = null;
3152 r.hideForNow = false;
3153 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003154 // Merge any pending results and pending intents; don't just replace them
3155 if (tmp.pendingResults != null) {
3156 if (r.pendingResults == null) {
3157 r.pendingResults = tmp.pendingResults;
3158 } else {
3159 r.pendingResults.addAll(tmp.pendingResults);
3160 }
3161 }
3162 if (tmp.pendingIntents != null) {
3163 if (r.pendingIntents == null) {
3164 r.pendingIntents = tmp.pendingIntents;
3165 } else {
3166 r.pendingIntents.addAll(tmp.pendingIntents);
3167 }
3168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003170
Christopher Tateb70f3df2009-04-07 16:07:59 -07003171 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 }
3173
3174 private final void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003175 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 Bitmap thumbnail = createThumbnailBitmap(r);
3177 CharSequence description = null;
3178 try {
3179 description = r.activity.onCreateDescription();
3180 } catch (Exception e) {
3181 if (!mInstrumentation.onException(r.activity, e)) {
3182 throw new RuntimeException(
3183 "Unable to create description of activity "
3184 + r.intent.getComponent().toShortString()
3185 + ": " + e.toString(), e);
3186 }
3187 }
3188 //System.out.println("Reporting top thumbnail " + thumbnail);
3189 try {
3190 ActivityManagerNative.getDefault().reportThumbnail(
3191 token, thumbnail, description);
3192 } catch (RemoteException ex) {
3193 }
3194 }
3195
3196 ArrayList<ComponentCallbacks> collectComponentCallbacksLocked(
3197 boolean allActivities, Configuration newConfig) {
3198 ArrayList<ComponentCallbacks> callbacks
3199 = new ArrayList<ComponentCallbacks>();
Bob Leee5408332009-09-04 18:31:17 -07003200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 if (mActivities.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003202 Iterator<ActivityClientRecord> it = mActivities.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 while (it.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003204 ActivityClientRecord ar = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 Activity a = ar.activity;
3206 if (a != null) {
3207 if (!ar.activity.mFinished && (allActivities ||
3208 (a != null && !ar.paused))) {
3209 // If the activity is currently resumed, its configuration
3210 // needs to change right now.
3211 callbacks.add(a);
3212 } else if (newConfig != null) {
3213 // Otherwise, we will tell it about the change
3214 // the next time it is resumed or shown. Note that
3215 // the activity manager may, before then, decide the
3216 // activity needs to be destroyed to handle its new
3217 // configuration.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003218 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Setting activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003219 + ar.activityInfo.name + " newConfig=" + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 ar.newConfig = newConfig;
3221 }
3222 }
3223 }
3224 }
3225 if (mServices.size() > 0) {
3226 Iterator<Service> it = mServices.values().iterator();
3227 while (it.hasNext()) {
3228 callbacks.add(it.next());
3229 }
3230 }
3231 synchronized (mProviderMap) {
3232 if (mLocalProviders.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003233 Iterator<ProviderClientRecord> it = mLocalProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 while (it.hasNext()) {
3235 callbacks.add(it.next().mLocalProvider);
3236 }
3237 }
3238 }
3239 final int N = mAllApplications.size();
3240 for (int i=0; i<N; i++) {
3241 callbacks.add(mAllApplications.get(i));
3242 }
Bob Leee5408332009-09-04 18:31:17 -07003243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 return callbacks;
3245 }
Bob Leee5408332009-09-04 18:31:17 -07003246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 private final void performConfigurationChanged(
3248 ComponentCallbacks cb, Configuration config) {
3249 // Only for Activity objects, check that they actually call up to their
3250 // superclass implementation. ComponentCallbacks is an interface, so
3251 // we check the runtime type and act accordingly.
3252 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3253 if (activity != null) {
3254 activity.mCalled = false;
3255 }
Bob Leee5408332009-09-04 18:31:17 -07003256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 boolean shouldChangeConfig = false;
3258 if ((activity == null) || (activity.mCurrentConfig == null)) {
3259 shouldChangeConfig = true;
3260 } else {
Bob Leee5408332009-09-04 18:31:17 -07003261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262 // If the new config is the same as the config this Activity
3263 // is already running with then don't bother calling
3264 // onConfigurationChanged
3265 int diff = activity.mCurrentConfig.diff(config);
3266 if (diff != 0) {
Bob Leee5408332009-09-04 18:31:17 -07003267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003268 // If this activity doesn't handle any of the config changes
3269 // then don't bother calling onConfigurationChanged as we're
3270 // going to destroy it.
3271 if ((~activity.mActivityInfo.configChanges & diff) == 0) {
3272 shouldChangeConfig = true;
3273 }
3274 }
3275 }
Bob Leee5408332009-09-04 18:31:17 -07003276
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003277 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003278 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 if (shouldChangeConfig) {
3280 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 if (activity != null) {
3283 if (!activity.mCalled) {
3284 throw new SuperNotCalledException(
3285 "Activity " + activity.getLocalClassName() +
3286 " did not call through to super.onConfigurationChanged()");
3287 }
3288 activity.mConfigChangeFlags = 0;
3289 activity.mCurrentConfig = new Configuration(config);
3290 }
3291 }
3292 }
3293
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003294 final boolean applyConfigurationToResourcesLocked(Configuration config,
3295 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003296 if (mResConfiguration == null) {
3297 mResConfiguration = new Configuration();
3298 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003299 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003300 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003301 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003302 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003303 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003304 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003305 DisplayMetrics dm = getDisplayMetricsLocked(true);
Bob Leee5408332009-09-04 18:31:17 -07003306
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003307 // set it for java, this also affects newly created Resources
3308 if (config.locale != null) {
3309 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 }
Bob Leee5408332009-09-04 18:31:17 -07003311
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003312 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003313
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003314 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003315 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003316
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003317 Iterator<WeakReference<Resources>> it =
3318 mActiveResources.values().iterator();
3319 //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3320 // mActiveResources.entrySet().iterator();
3321 while (it.hasNext()) {
3322 WeakReference<Resources> v = it.next();
3323 Resources r = v.get();
3324 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003325 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003326 + r + " config to: " + config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003327 r.updateConfiguration(config, dm, compat);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003328 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003329 // + " " + r + ": " + r.getConfiguration());
3330 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003331 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003332 it.remove();
3333 }
3334 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003335
3336 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003337 }
3338
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003339 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003340
3341 ArrayList<ComponentCallbacks> callbacks = null;
3342
3343 synchronized (mPackages) {
3344 if (mPendingConfiguration != null) {
3345 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3346 config = mPendingConfiguration;
3347 }
3348 mPendingConfiguration = null;
3349 }
3350
3351 if (config == null) {
3352 return;
3353 }
3354
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003355 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003356 + config);
3357
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003358 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 if (mConfiguration == null) {
3361 mConfiguration = new Configuration();
3362 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003363 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003364 return;
3365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003366 mConfiguration.updateFrom(config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003367 if (compat != null) {
3368 // Can't do this here, because it causes us to report the
3369 // comatible config back to the am as the current config
3370 // of the activity, and much unhappiness results.
3371 //compat.applyToConfiguration(mConfiguration);
3372 }
Bob Leee5408332009-09-04 18:31:17 -07003373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 callbacks = collectComponentCallbacksLocked(false, config);
3375 }
Bob Leee5408332009-09-04 18:31:17 -07003376
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003377 if (callbacks != null) {
3378 final int N = callbacks.size();
3379 for (int i=0; i<N; i++) {
3380 performConfigurationChanged(callbacks.get(i), config);
3381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 }
3383 }
3384
3385 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003386 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387 if (r == null || r.activity == null) {
3388 return;
3389 }
Bob Leee5408332009-09-04 18:31:17 -07003390
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003391 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003392 + r.activityInfo.name);
3393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 performConfigurationChanged(r.activity, mConfiguration);
3395 }
3396
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003397 final void handleProfilerControl(boolean start, ProfilerControlData pcd) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003398 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003399 try {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003400 Debug.startMethodTracing(pcd.path, pcd.fd.getFileDescriptor(),
3401 8 * 1024 * 1024, 0);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003402 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003403 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003404 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003405 } finally {
3406 try {
3407 pcd.fd.close();
3408 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003409 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003410 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003411 }
3412 } else {
3413 Debug.stopMethodTracing();
3414 }
3415 }
Bob Leee5408332009-09-04 18:31:17 -07003416
Andy McFadden824c5102010-07-09 16:26:57 -07003417 final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
3418 if (managed) {
3419 try {
3420 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3421 } catch (IOException e) {
3422 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3423 + " -- can the process access this path?");
3424 } finally {
3425 try {
3426 dhd.fd.close();
3427 } catch (IOException e) {
3428 Slog.w(TAG, "Failure closing profile fd", e);
3429 }
3430 }
3431 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003432 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003433 }
3434 }
3435
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003436 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3437 boolean hasPkgInfo = false;
3438 if (packages != null) {
3439 for (int i=packages.length-1; i>=0; i--) {
3440 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3441 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003442 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003443 ref = mPackages.get(packages[i]);
3444 if (ref != null && ref.get() != null) {
3445 hasPkgInfo = true;
3446 } else {
3447 ref = mResourcePackages.get(packages[i]);
3448 if (ref != null && ref.get() != null) {
3449 hasPkgInfo = true;
3450 }
3451 }
3452 }
3453 mPackages.remove(packages[i]);
3454 mResourcePackages.remove(packages[i]);
3455 }
3456 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003457 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003458 hasPkgInfo);
3459 }
3460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 final void handleLowMemory() {
Brian Carlstromed7e0072011-03-24 13:27:57 -07003462 ArrayList<ComponentCallbacks> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003464 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 callbacks = collectComponentCallbacksLocked(true, null);
3466 }
Bob Leee5408332009-09-04 18:31:17 -07003467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 final int N = callbacks.size();
3469 for (int i=0; i<N; i++) {
3470 callbacks.get(i).onLowMemory();
3471 }
3472
Chris Tatece229052009-03-25 16:44:52 -07003473 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3474 if (Process.myUid() != Process.SYSTEM_UID) {
3475 int sqliteReleased = SQLiteDatabase.releaseMemory();
3476 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3477 }
Bob Leee5408332009-09-04 18:31:17 -07003478
Mike Reedcaf0df12009-04-27 14:32:05 -04003479 // Ask graphics to free up as much as possible (font/image caches)
3480 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481
3482 BinderInternal.forceGc("mem");
3483 }
3484
3485 private final void handleBindApplication(AppBindData data) {
3486 mBoundApplication = data;
3487 mConfiguration = new Configuration(data.config);
3488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003490 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 android.ddm.DdmHandleAppName.setAppName(data.processName);
3492
Joe Onoratod630f102011-03-17 18:42:26 -07003493 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
3494 // implementation to use the pool executor. Normally, we use the
3495 // serialized executor as the default. This has to happen in the
3496 // main thread so the main looper is set right.
3497 if (data.appInfo.targetSdkVersion <= 12) {
3498 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
3499 }
3500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 /*
3502 * Before spawning a new process, reset the time zone to be the system time zone.
3503 * This needs to be done because the system time zone could have changed after the
3504 * the spawning of this process. Without doing this this process would have the incorrect
3505 * system time zone.
3506 */
3507 TimeZone.setDefault(null);
3508
3509 /*
3510 * Initialize the default locale in this process for the reasons we set the time zone.
3511 */
3512 Locale.setDefault(data.config.locale);
3513
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003514 /*
3515 * Update the system configuration since its preloaded and might not
3516 * reflect configuration changes. The configuration object passed
3517 * in AppBindData can be safely assumed to be up to date
3518 */
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003519 Resources.getSystem().updateConfiguration(mConfiguration,
3520 Resources.getSystem().getDisplayMetrics(), data.compatInfo);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003521
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003522 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003524 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003525 * For system applications on userdebug/eng builds, log stack
3526 * traces of disk and network access to dropbox for analysis.
3527 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07003528 if ((data.appInfo.flags &
3529 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07003530 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
3531 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003532 }
3533
3534 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07003535 * For apps targetting SDK Honeycomb or later, we don't allow
3536 * network usage on the main event loop / UI thread.
3537 *
3538 * Note to those grepping: this is what ultimately throws
3539 * NetworkOnMainThreadException ...
3540 */
3541 if (data.appInfo.targetSdkVersion > 9) {
3542 StrictMode.enableDeathOnNetwork();
3543 }
3544
3545 /**
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003546 * Switch this process to density compatibility mode if needed.
3547 */
3548 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
3549 == 0) {
3550 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3551 }
Bob Leee5408332009-09-04 18:31:17 -07003552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
3554 // XXX should have option to change the port.
3555 Debug.changeDebugPort(8100);
3556 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003557 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 + " is waiting for the debugger on port 8100...");
3559
3560 IActivityManager mgr = ActivityManagerNative.getDefault();
3561 try {
3562 mgr.showWaitingForDebugger(mAppThread, true);
3563 } catch (RemoteException ex) {
3564 }
3565
3566 Debug.waitForDebugger();
3567
3568 try {
3569 mgr.showWaitingForDebugger(mAppThread, false);
3570 } catch (RemoteException ex) {
3571 }
3572
3573 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003574 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 + " can be debugged on port 8100...");
3576 }
3577 }
3578
Robert Greenwalt434203a2010-10-11 16:00:27 -07003579 /**
3580 * Initialize the default http proxy in this process for the reasons we set the time zone.
3581 */
3582 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
3583 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
3584 try {
3585 ProxyProperties proxyProperties = service.getProxy();
3586 Proxy.setHttpProxySystemProperty(proxyProperties);
3587 } catch (RemoteException e) {}
3588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 if (data.instrumentationName != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08003590 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003591 appContext.init(data.info, null, this);
3592 InstrumentationInfo ii = null;
3593 try {
3594 ii = appContext.getPackageManager().
3595 getInstrumentationInfo(data.instrumentationName, 0);
3596 } catch (PackageManager.NameNotFoundException e) {
3597 }
3598 if (ii == null) {
3599 throw new RuntimeException(
3600 "Unable to find instrumentation info for: "
3601 + data.instrumentationName);
3602 }
3603
3604 mInstrumentationAppDir = ii.sourceDir;
3605 mInstrumentationAppPackage = ii.packageName;
3606 mInstrumentedAppDir = data.info.getAppDir();
3607
3608 ApplicationInfo instrApp = new ApplicationInfo();
3609 instrApp.packageName = ii.packageName;
3610 instrApp.sourceDir = ii.sourceDir;
3611 instrApp.publicSourceDir = ii.publicSourceDir;
3612 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07003613 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003614 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003615 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08003616 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003617 instrContext.init(pi, null, this);
3618
3619 try {
3620 java.lang.ClassLoader cl = instrContext.getClassLoader();
3621 mInstrumentation = (Instrumentation)
3622 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
3623 } catch (Exception e) {
3624 throw new RuntimeException(
3625 "Unable to instantiate instrumentation "
3626 + data.instrumentationName + ": " + e.toString(), e);
3627 }
3628
3629 mInstrumentation.init(this, instrContext, appContext,
3630 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
3631
3632 if (data.profileFile != null && !ii.handleProfiling) {
3633 data.handlingProfiling = true;
3634 File file = new File(data.profileFile);
3635 file.getParentFile().mkdirs();
3636 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
3637 }
3638
3639 try {
3640 mInstrumentation.onCreate(data.instrumentationArgs);
3641 }
3642 catch (Exception e) {
3643 throw new RuntimeException(
3644 "Exception thrown in onCreate() of "
3645 + data.instrumentationName + ": " + e.toString(), e);
3646 }
3647
3648 } else {
3649 mInstrumentation = new Instrumentation();
3650 }
3651
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003652 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08003653 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003654 }
3655
Christopher Tate181fafa2009-05-14 11:12:14 -07003656 // If the app is being launched for full backup or restore, bring it up in
3657 // a restricted environment with the base application class.
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003658 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 mInitialApplication = app;
3660
3661 List<ProviderInfo> providers = data.providers;
3662 if (providers != null) {
3663 installContentProviders(app, providers);
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003664 // For process that contains content providers, we want to
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003665 // ensure that the JIT is enabled "at some point".
3666 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 }
3668
3669 try {
3670 mInstrumentation.callApplicationOnCreate(app);
3671 } catch (Exception e) {
3672 if (!mInstrumentation.onException(app, e)) {
3673 throw new RuntimeException(
3674 "Unable to create application " + app.getClass().getName()
3675 + ": " + e.toString(), e);
3676 }
3677 }
3678 }
3679
3680 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
3681 IActivityManager am = ActivityManagerNative.getDefault();
3682 if (mBoundApplication.profileFile != null && mBoundApplication.handlingProfiling) {
3683 Debug.stopMethodTracing();
3684 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003685 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 // + ", app thr: " + mAppThread);
3687 try {
3688 am.finishInstrumentation(mAppThread, resultCode, results);
3689 } catch (RemoteException ex) {
3690 }
3691 }
3692
3693 private final void installContentProviders(
3694 Context context, List<ProviderInfo> providers) {
3695 final ArrayList<IActivityManager.ContentProviderHolder> results =
3696 new ArrayList<IActivityManager.ContentProviderHolder>();
3697
3698 Iterator<ProviderInfo> i = providers.iterator();
3699 while (i.hasNext()) {
3700 ProviderInfo cpi = i.next();
3701 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07003702 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 buf.append(cpi.authority);
3704 buf.append(": ");
3705 buf.append(cpi.name);
3706 Log.i(TAG, buf.toString());
3707 IContentProvider cp = installProvider(context, null, cpi, false);
3708 if (cp != null) {
3709 IActivityManager.ContentProviderHolder cph =
3710 new IActivityManager.ContentProviderHolder(cpi);
3711 cph.provider = cp;
3712 results.add(cph);
3713 // Don't ever unload this provider from the process.
3714 synchronized(mProviderMap) {
3715 mProviderRefCountMap.put(cp.asBinder(), new ProviderRefCount(10000));
3716 }
3717 }
3718 }
3719
3720 try {
3721 ActivityManagerNative.getDefault().publishContentProviders(
3722 getApplicationThread(), results);
3723 } catch (RemoteException ex) {
3724 }
3725 }
3726
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003727 private final IContentProvider getExistingProvider(Context context, String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003729 final ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 if (pr != null) {
3731 return pr.mProvider;
3732 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003733 return null;
3734 }
3735 }
3736
3737 private final IContentProvider getProvider(Context context, String name) {
3738 IContentProvider existing = getExistingProvider(context, name);
3739 if (existing != null) {
3740 return existing;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 }
3742
3743 IActivityManager.ContentProviderHolder holder = null;
3744 try {
3745 holder = ActivityManagerNative.getDefault().getContentProvider(
3746 getApplicationThread(), name);
3747 } catch (RemoteException ex) {
3748 }
3749 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003750 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003751 return null;
3752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003753
3754 IContentProvider prov = installProvider(context, holder.provider,
3755 holder.info, true);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003756 //Slog.i(TAG, "noReleaseNeeded=" + holder.noReleaseNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 if (holder.noReleaseNeeded || holder.provider == null) {
3758 // We are not going to release the provider if it is an external
3759 // provider that doesn't care about being released, or if it is
3760 // a local provider running in this process.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003761 //Slog.i(TAG, "*** NO RELEASE NEEDED");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003762 synchronized(mProviderMap) {
3763 mProviderRefCountMap.put(prov.asBinder(), new ProviderRefCount(10000));
3764 }
3765 }
3766 return prov;
3767 }
3768
3769 public final IContentProvider acquireProvider(Context c, String name) {
3770 IContentProvider provider = getProvider(c, name);
3771 if(provider == null)
3772 return null;
3773 IBinder jBinder = provider.asBinder();
3774 synchronized(mProviderMap) {
3775 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3776 if(prc == null) {
3777 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
3778 } else {
3779 prc.count++;
3780 } //end else
3781 } //end synchronized
3782 return provider;
3783 }
3784
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003785 public final IContentProvider acquireExistingProvider(Context c, String name) {
3786 IContentProvider provider = getExistingProvider(c, name);
3787 if(provider == null)
3788 return null;
3789 IBinder jBinder = provider.asBinder();
3790 synchronized(mProviderMap) {
3791 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3792 if(prc == null) {
3793 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
3794 } else {
3795 prc.count++;
3796 } //end else
3797 } //end synchronized
3798 return provider;
3799 }
3800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003801 public final boolean releaseProvider(IContentProvider provider) {
3802 if(provider == null) {
3803 return false;
3804 }
3805 IBinder jBinder = provider.asBinder();
3806 synchronized(mProviderMap) {
3807 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3808 if(prc == null) {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003809 if(localLOGV) Slog.v(TAG, "releaseProvider::Weird shouldn't be here");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 return false;
3811 } else {
3812 prc.count--;
3813 if(prc.count == 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003814 // Schedule the actual remove asynchronously, since we
3815 // don't know the context this will be called in.
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003816 // TODO: it would be nice to post a delayed message, so
3817 // if we come back and need the same provider quickly
3818 // we will still have it available.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003819 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, provider);
3820 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 } //end if
3822 } //end else
3823 } //end synchronized
3824 return true;
3825 }
3826
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003827 final void completeRemoveProvider(IContentProvider provider) {
3828 IBinder jBinder = provider.asBinder();
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003829 String name = null;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003830 synchronized(mProviderMap) {
3831 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3832 if(prc != null && prc.count == 0) {
3833 mProviderRefCountMap.remove(jBinder);
3834 //invoke removeProvider to dereference provider
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003835 name = removeProviderLocked(provider);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003836 }
3837 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003838
3839 if (name != null) {
3840 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003841 if(localLOGV) Slog.v(TAG, "removeProvider::Invoking " +
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003842 "ActivityManagerNative.removeContentProvider(" + name);
3843 ActivityManagerNative.getDefault().removeContentProvider(
3844 getApplicationThread(), name);
3845 } catch (RemoteException e) {
3846 //do nothing content provider object is dead any way
3847 } //end catch
3848 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003849 }
3850
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003851 public final String removeProviderLocked(IContentProvider provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003852 if (provider == null) {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003853 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003854 }
3855 IBinder providerBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003856
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003857 String name = null;
3858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 // remove the provider from mProviderMap
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003860 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003861 while (iter.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003862 ProviderClientRecord pr = iter.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003863 IBinder myBinder = pr.mProvider.asBinder();
3864 if (myBinder == providerBinder) {
3865 //find if its published by this process itself
3866 if(pr.mLocalProvider != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003867 if(localLOGV) Slog.i(TAG, "removeProvider::found local provider returning");
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003868 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003869 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003870 if(localLOGV) Slog.v(TAG, "removeProvider::Not local provider Unlinking " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 "death recipient");
3872 //content provider is in another process
3873 myBinder.unlinkToDeath(pr, 0);
3874 iter.remove();
3875 //invoke remove only once for the very first name seen
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003876 if(name == null) {
3877 name = pr.mName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 }
3879 } //end if myBinder
3880 } //end while iter
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003881
3882 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003883 }
3884
3885 final void removeDeadProvider(String name, IContentProvider provider) {
3886 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003887 ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 if (pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003889 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003890 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003891 if (removed != null) {
3892 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
3893 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 }
3895 }
3896 }
3897
3898 final void removeDeadProviderLocked(String name, IContentProvider provider) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003899 ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900 if (pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003901 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003902 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003903 if (removed != null) {
3904 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
3905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003906 }
3907 }
3908
3909 private final IContentProvider installProvider(Context context,
3910 IContentProvider provider, ProviderInfo info, boolean noisy) {
3911 ContentProvider localProvider = null;
3912 if (provider == null) {
3913 if (noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003914 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 + info.name);
3916 }
3917 Context c = null;
3918 ApplicationInfo ai = info.applicationInfo;
3919 if (context.getPackageName().equals(ai.packageName)) {
3920 c = context;
3921 } else if (mInitialApplication != null &&
3922 mInitialApplication.getPackageName().equals(ai.packageName)) {
3923 c = mInitialApplication;
3924 } else {
3925 try {
3926 c = context.createPackageContext(ai.packageName,
3927 Context.CONTEXT_INCLUDE_CODE);
3928 } catch (PackageManager.NameNotFoundException e) {
3929 }
3930 }
3931 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003932 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003933 ai.packageName +
3934 " while loading content provider " +
3935 info.name);
3936 return null;
3937 }
3938 try {
3939 final java.lang.ClassLoader cl = c.getClassLoader();
3940 localProvider = (ContentProvider)cl.
3941 loadClass(info.name).newInstance();
3942 provider = localProvider.getIContentProvider();
3943 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003944 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003945 info.name + " from sourceDir " +
3946 info.applicationInfo.sourceDir);
3947 return null;
3948 }
Joe Onorato43a17652011-04-06 19:22:23 -07003949 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003950 TAG, "Instantiating local provider " + info.name);
3951 // XXX Need to create the correct context for this provider.
3952 localProvider.attachInfo(c, info);
3953 } catch (java.lang.Exception e) {
3954 if (!mInstrumentation.onException(null, e)) {
3955 throw new RuntimeException(
3956 "Unable to get provider " + info.name
3957 + ": " + e.toString(), e);
3958 }
3959 return null;
3960 }
3961 } else if (localLOGV) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003962 Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 + info.name);
3964 }
3965
3966 synchronized (mProviderMap) {
3967 // Cache the pointer for the remote provider.
3968 String names[] = PATTERN_SEMICOLON.split(info.authority);
3969 for (int i=0; i<names.length; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003970 ProviderClientRecord pr = new ProviderClientRecord(names[i], provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 localProvider);
3972 try {
3973 provider.asBinder().linkToDeath(pr, 0);
3974 mProviderMap.put(names[i], pr);
3975 } catch (RemoteException e) {
3976 return null;
3977 }
3978 }
3979 if (localProvider != null) {
3980 mLocalProviders.put(provider.asBinder(),
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003981 new ProviderClientRecord(null, provider, localProvider));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 }
3983 }
3984
3985 return provider;
3986 }
3987
3988 private final void attach(boolean system) {
3989 sThreadLocal.set(this);
3990 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003991 if (!system) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07003992 ViewAncestor.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003993 public void run() {
3994 ensureJitEnabled();
3995 }
3996 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003997 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
3998 RuntimeInit.setApplicationObject(mAppThread.asBinder());
3999 IActivityManager mgr = ActivityManagerNative.getDefault();
4000 try {
4001 mgr.attachApplication(mAppThread);
4002 } catch (RemoteException ex) {
4003 }
4004 } else {
4005 // Don't set application object here -- if the system crashes,
4006 // we can't display an alert, we just want to die die die.
4007 android.ddm.DdmHandleAppName.setAppName("system_process");
4008 try {
4009 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004010 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004011 context.init(getSystemContext().mPackageInfo, null, this);
4012 Application app = Instrumentation.newApplication(Application.class, context);
4013 mAllApplications.add(app);
4014 mInitialApplication = app;
4015 app.onCreate();
4016 } catch (Exception e) {
4017 throw new RuntimeException(
4018 "Unable to instantiate Application():" + e.toString(), e);
4019 }
4020 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004021
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004022 ViewAncestor.addConfigCallback(new ComponentCallbacks() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004023 public void onConfigurationChanged(Configuration newConfig) {
4024 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004025 // We need to apply this change to the resources
4026 // immediately, because upon returning the view
4027 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004028 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004029 // This actually changed the resources! Tell
4030 // everyone about it.
4031 if (mPendingConfiguration == null ||
4032 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4033 mPendingConfiguration = newConfig;
4034
4035 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4036 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004037 }
4038 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004039 }
4040 public void onLowMemory() {
4041 }
4042 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004043 }
4044
4045 private final void detach()
4046 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004047 sThreadLocal.set(null);
4048 }
4049
4050 public static final ActivityThread systemMain() {
Romain Guy52339202010-09-03 16:04:46 -07004051 HardwareRenderer.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004052 ActivityThread thread = new ActivityThread();
4053 thread.attach(true);
4054 return thread;
4055 }
4056
4057 public final void installSystemProviders(List providers) {
4058 if (providers != null) {
4059 installContentProviders(mInitialApplication,
4060 (List<ProviderInfo>)providers);
4061 }
4062 }
4063
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004064 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004065 synchronized (mPackages) {
4066 if (mCoreSettings != null) {
4067 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004068 } else {
4069 return defaultValue;
4070 }
4071 }
4072 }
4073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004074 public static final void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004075 SamplingProfilerIntegration.start();
4076
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004077 // CloseGuard defaults to true and can be quite spammy. We
4078 // disable it here, but selectively enable it later (via
4079 // StrictMode) on debug builds, but using DropBox, not logs.
4080 CloseGuard.setEnabled(false);
4081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 Process.setArgV0("<pre-initialized>");
4083
4084 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07004085 if (sMainThreadHandler == null) {
4086 sMainThreadHandler = new Handler();
4087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004088
4089 ActivityThread thread = new ActivityThread();
4090 thread.attach(false);
4091
Dianne Hackborn287952c2010-09-22 22:34:31 -07004092 if (false) {
4093 Looper.myLooper().setMessageLogging(new
4094 LogPrinter(Log.DEBUG, "ActivityThread"));
4095 }
4096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 Looper.loop();
4098
4099 if (Process.supportsProcesses()) {
4100 throw new RuntimeException("Main thread loop unexpectedly exited");
4101 }
4102
4103 thread.detach();
Bob Leeeec2f412009-09-10 11:01:24 +02004104 String name = (thread.mInitialApplication != null)
4105 ? thread.mInitialApplication.getPackageName()
4106 : "<unknown>";
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004107 Slog.i(TAG, "Main thread of " + name + " is now exiting");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004108 }
4109}