blob: f0e7e98355e9184c8058e9ab78f20e4422a54e23 [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 Tate436344a2009-09-30 16:17:37 -0700132 private static final boolean DEBUG_BACKUP = false;
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;
1982 if (classname == null) {
1983 if (data.backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08001984 Slog.e(TAG, "Attempted incremental backup but no defined agent for "
Christopher Tate181fafa2009-05-14 11:12:14 -07001985 + packageName);
1986 return;
1987 }
1988 classname = "android.app.FullBackupAgent";
1989 }
1990 try {
Christopher Tated1475e02009-07-09 15:36:17 -07001991 IBinder binder = null;
1992 try {
1993 java.lang.ClassLoader cl = packageInfo.getClassLoader();
1994 agent = (BackupAgent) cl.loadClass(data.appInfo.backupAgentName).newInstance();
1995
1996 // set up the agent's context
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001997 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing BackupAgent "
Christopher Tated1475e02009-07-09 15:36:17 -07001998 + data.appInfo.backupAgentName);
1999
Dianne Hackborn21556372010-02-04 16:34:40 -08002000 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002001 context.init(packageInfo, null, this);
2002 context.setOuterContext(agent);
2003 agent.attach(context);
2004
2005 agent.onCreate();
2006 binder = agent.onBind();
2007 mBackupAgents.put(packageName, agent);
2008 } catch (Exception e) {
2009 // If this is during restore, fail silently; otherwise go
2010 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002011 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tated1475e02009-07-09 15:36:17 -07002012 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE) {
2013 throw e;
2014 }
2015 // falling through with 'binder' still null
2016 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002017
2018 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002019 try {
2020 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2021 } catch (RemoteException e) {
2022 // nothing to do.
2023 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002024 } catch (Exception e) {
2025 throw new RuntimeException("Unable to create BackupAgent "
2026 + data.appInfo.backupAgentName + ": " + e.toString(), e);
2027 }
2028 }
2029
2030 // Tear down a BackupAgent
2031 private final void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002032 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002033
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002034 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002035 String packageName = packageInfo.mPackageName;
2036 BackupAgent agent = mBackupAgents.get(packageName);
2037 if (agent != null) {
2038 try {
2039 agent.onDestroy();
2040 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002041 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002042 e.printStackTrace();
2043 }
2044 mBackupAgents.remove(packageName);
2045 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002046 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002047 }
2048 }
2049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 private final void handleCreateService(CreateServiceData data) {
2051 // If we are getting ready to gc after going to the background, well
2052 // we are back active so skip it.
2053 unscheduleGcIdler();
2054
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002055 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002056 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 Service service = null;
2058 try {
2059 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2060 service = (Service) cl.loadClass(data.info.name).newInstance();
2061 } catch (Exception e) {
2062 if (!mInstrumentation.onException(service, e)) {
2063 throw new RuntimeException(
2064 "Unable to instantiate service " + data.info.name
2065 + ": " + e.toString(), e);
2066 }
2067 }
2068
2069 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002070 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071
Dianne Hackborn21556372010-02-04 16:34:40 -08002072 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 context.init(packageInfo, null, this);
2074
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002075 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 context.setOuterContext(service);
2077 service.attach(context, this, data.info.name, data.token, app,
2078 ActivityManagerNative.getDefault());
2079 service.onCreate();
2080 mServices.put(data.token, service);
2081 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002082 ActivityManagerNative.getDefault().serviceDoneExecuting(
2083 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 } catch (RemoteException e) {
2085 // nothing to do.
2086 }
2087 } catch (Exception e) {
2088 if (!mInstrumentation.onException(service, e)) {
2089 throw new RuntimeException(
2090 "Unable to create service " + data.info.name
2091 + ": " + e.toString(), e);
2092 }
2093 }
2094 }
2095
2096 private final void handleBindService(BindServiceData data) {
2097 Service s = mServices.get(data.token);
2098 if (s != null) {
2099 try {
2100 data.intent.setExtrasClassLoader(s.getClassLoader());
2101 try {
2102 if (!data.rebind) {
2103 IBinder binder = s.onBind(data.intent);
2104 ActivityManagerNative.getDefault().publishService(
2105 data.token, data.intent, binder);
2106 } else {
2107 s.onRebind(data.intent);
2108 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002109 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002111 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 } catch (RemoteException ex) {
2113 }
2114 } catch (Exception e) {
2115 if (!mInstrumentation.onException(s, e)) {
2116 throw new RuntimeException(
2117 "Unable to bind to service " + s
2118 + " with " + data.intent + ": " + e.toString(), e);
2119 }
2120 }
2121 }
2122 }
2123
2124 private final void handleUnbindService(BindServiceData data) {
2125 Service s = mServices.get(data.token);
2126 if (s != null) {
2127 try {
2128 data.intent.setExtrasClassLoader(s.getClassLoader());
2129 boolean doRebind = s.onUnbind(data.intent);
2130 try {
2131 if (doRebind) {
2132 ActivityManagerNative.getDefault().unbindFinished(
2133 data.token, data.intent, doRebind);
2134 } else {
2135 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002136 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 }
2138 } catch (RemoteException ex) {
2139 }
2140 } catch (Exception e) {
2141 if (!mInstrumentation.onException(s, e)) {
2142 throw new RuntimeException(
2143 "Unable to unbind to service " + s
2144 + " with " + data.intent + ": " + e.toString(), e);
2145 }
2146 }
2147 }
2148 }
2149
Dianne Hackborn625ac272010-09-17 18:29:22 -07002150 private void handleDumpService(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002151 Service s = mServices.get(info.token);
2152 if (s != null) {
2153 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2154 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2155 pw.flush();
2156 try {
2157 info.fd.close();
2158 } catch (IOException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 }
2160 }
2161 }
2162
Dianne Hackborn625ac272010-09-17 18:29:22 -07002163 private void handleDumpActivity(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002164 ActivityClientRecord r = mActivities.get(info.token);
2165 if (r != null && r.activity != null) {
2166 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2167 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2168 pw.flush();
2169 try {
2170 info.fd.close();
2171 } catch (IOException e) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07002172 }
2173 }
2174 }
2175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 private final void handleServiceArgs(ServiceArgsData data) {
2177 Service s = mServices.get(data.token);
2178 if (s != null) {
2179 try {
2180 if (data.args != null) {
2181 data.args.setExtrasClassLoader(s.getClassLoader());
2182 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002183 int res;
2184 if (!data.taskRemoved) {
2185 res = s.onStartCommand(data.args, data.flags, data.startId);
2186 } else {
2187 s.onTaskRemoved(data.args);
2188 res = Service.START_TASK_REMOVED_COMPLETE;
2189 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002190
2191 QueuedWork.waitToFinish();
2192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002194 ActivityManagerNative.getDefault().serviceDoneExecuting(
2195 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 } catch (RemoteException e) {
2197 // nothing to do.
2198 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002199 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 } catch (Exception e) {
2201 if (!mInstrumentation.onException(s, e)) {
2202 throw new RuntimeException(
2203 "Unable to start service " + s
2204 + " with " + data.args + ": " + e.toString(), e);
2205 }
2206 }
2207 }
2208 }
2209
2210 private final void handleStopService(IBinder token) {
2211 Service s = mServices.remove(token);
2212 if (s != null) {
2213 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002214 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 s.onDestroy();
2216 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002217 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002219 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002221
2222 QueuedWork.waitToFinish();
2223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002225 ActivityManagerNative.getDefault().serviceDoneExecuting(
2226 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 } catch (RemoteException e) {
2228 // nothing to do.
2229 }
2230 } catch (Exception e) {
2231 if (!mInstrumentation.onException(s, e)) {
2232 throw new RuntimeException(
2233 "Unable to stop service " + s
2234 + ": " + e.toString(), e);
2235 }
2236 }
2237 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002238 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 }
2240
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002241 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002243 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002244 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 + " finished=" + r.activity.mFinished);
2246 if (r != null && !r.activity.mFinished) {
2247 if (clearHide) {
2248 r.hideForNow = false;
2249 r.activity.mStartedActivity = false;
2250 }
2251 try {
2252 if (r.pendingIntents != null) {
2253 deliverNewIntents(r, r.pendingIntents);
2254 r.pendingIntents = null;
2255 }
2256 if (r.pendingResults != null) {
2257 deliverResults(r, r.pendingResults);
2258 r.pendingResults = null;
2259 }
2260 r.activity.performResume();
2261
Bob Leee5408332009-09-04 18:31:17 -07002262 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 r.paused = false;
2266 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 r.state = null;
2268 } catch (Exception e) {
2269 if (!mInstrumentation.onException(r.activity, e)) {
2270 throw new RuntimeException(
2271 "Unable to resume activity "
2272 + r.intent.getComponent().toShortString()
2273 + ": " + e.toString(), e);
2274 }
2275 }
2276 }
2277 return r;
2278 }
2279
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002280 final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
2281 if (r.mPendingRemoveWindow != null) {
2282 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2283 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2284 if (wtoken != null) {
2285 WindowManagerImpl.getDefault().closeAll(wtoken,
2286 r.activity.getClass().getName(), "Activity");
2287 }
2288 }
2289 r.mPendingRemoveWindow = null;
2290 r.mPendingRemoveWindowManager = null;
2291 }
2292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2294 // If we are getting ready to gc after going to the background, well
2295 // we are back active so skip it.
2296 unscheduleGcIdler();
2297
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002298 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299
2300 if (r != null) {
2301 final Activity a = r.activity;
2302
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002303 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 TAG, "Resume " + r + " started activity: " +
2305 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2306 + ", finished: " + a.mFinished);
2307
2308 final int forwardBit = isForward ?
2309 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 // If the window hasn't yet been added to the window manager,
2312 // and this guy didn't finish itself or start another activity,
2313 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002314 boolean willBeVisible = !a.mStartedActivity;
2315 if (!willBeVisible) {
2316 try {
2317 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2318 a.getActivityToken());
2319 } catch (RemoteException e) {
2320 }
2321 }
2322 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 r.window = r.activity.getWindow();
2324 View decor = r.window.getDecorView();
2325 decor.setVisibility(View.INVISIBLE);
2326 ViewManager wm = a.getWindowManager();
2327 WindowManager.LayoutParams l = r.window.getAttributes();
2328 a.mDecor = decor;
2329 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2330 l.softInputMode |= forwardBit;
2331 if (a.mVisibleFromClient) {
2332 a.mWindowAdded = true;
2333 wm.addView(decor, l);
2334 }
2335
2336 // If the window has already been added, but during resume
2337 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002338 // window visible.
2339 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002340 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 TAG, "Launch " + r + " mStartedActivity set");
2342 r.hideForNow = true;
2343 }
2344
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002345 // Get rid of anything left hanging around.
2346 cleanUpPendingRemoveWindows(r);
2347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 // The window is now visible if it has been added, we are not
2349 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002350 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002351 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002353 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002354 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 performConfigurationChanged(r.activity, r.newConfig);
2356 r.newConfig = null;
2357 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002358 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 + isForward);
2360 WindowManager.LayoutParams l = r.window.getAttributes();
2361 if ((l.softInputMode
2362 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2363 != forwardBit) {
2364 l.softInputMode = (l.softInputMode
2365 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2366 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002367 if (r.activity.mVisibleFromClient) {
2368 ViewManager wm = a.getWindowManager();
2369 View decor = r.window.getDecorView();
2370 wm.updateViewLayout(decor, l);
2371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 }
2373 r.activity.mVisibleFromServer = true;
2374 mNumVisibleActivities++;
2375 if (r.activity.mVisibleFromClient) {
2376 r.activity.makeVisible();
2377 }
2378 }
2379
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002380 if (!r.onlyLocalRequest) {
2381 r.nextIdle = mNewActivities;
2382 mNewActivities = r;
2383 if (localLOGV) Slog.v(
2384 TAG, "Scheduling idle handler for " + r);
2385 Looper.myQueue().addIdleHandler(new Idler());
2386 }
2387 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388
2389 } else {
2390 // If an exception was thrown when trying to resume, then
2391 // just end this activity.
2392 try {
2393 ActivityManagerNative.getDefault()
2394 .finishActivity(token, Activity.RESULT_CANCELED, null);
2395 } catch (RemoteException ex) {
2396 }
2397 }
2398 }
2399
2400 private int mThumbnailWidth = -1;
2401 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002402 private Bitmap mAvailThumbnailBitmap = null;
2403 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002405 private final Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002406 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002408 if (thumbnail == null) {
2409 int w = mThumbnailWidth;
2410 int h;
2411 if (w < 0) {
2412 Resources res = r.activity.getResources();
2413 mThumbnailHeight = h =
2414 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002416 mThumbnailWidth = w =
2417 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2418 } else {
2419 h = mThumbnailHeight;
2420 }
2421
2422 // On platforms where we don't want thumbnails, set dims to (0,0)
2423 if ((w > 0) && (h > 0)) {
2424 thumbnail = Bitmap.createBitmap(w, h, THUMBNAIL_FORMAT);
2425 thumbnail.eraseColor(0);
2426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 }
2428
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002429 if (thumbnail != null) {
2430 Canvas cv = mThumbnailCanvas;
2431 if (cv == null) {
2432 mThumbnailCanvas = cv = new Canvas();
2433 }
2434
2435 cv.setBitmap(thumbnail);
2436 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2437 mAvailThumbnailBitmap = thumbnail;
2438 thumbnail = null;
2439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 } catch (Exception e) {
2443 if (!mInstrumentation.onException(r.activity, e)) {
2444 throw new RuntimeException(
2445 "Unable to create thumbnail of "
2446 + r.intent.getComponent().toShortString()
2447 + ": " + e.toString(), e);
2448 }
2449 thumbnail = null;
2450 }
2451
2452 return thumbnail;
2453 }
2454
2455 private final void handlePauseActivity(IBinder token, boolean finished,
2456 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002457 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002459 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 if (userLeaving) {
2461 performUserLeavingActivity(r);
2462 }
Bob Leee5408332009-09-04 18:31:17 -07002463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002465 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002467 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002468 if (r.isPreHoneycomb()) {
2469 QueuedWork.waitToFinish();
2470 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 // Tell the activity manager we have paused.
2473 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002474 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 } catch (RemoteException ex) {
2476 }
2477 }
2478 }
2479
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002480 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 mInstrumentation.callActivityOnUserLeaving(r.activity);
2482 }
2483
2484 final Bundle performPauseActivity(IBinder token, boolean finished,
2485 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002486 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 return r != null ? performPauseActivity(r, finished, saveState) : null;
2488 }
2489
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002490 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 boolean saveState) {
2492 if (r.paused) {
2493 if (r.activity.mFinished) {
2494 // If we are finishing, we won't call onResume() in certain cases.
2495 // So here we likewise don't want to call onPause() if the activity
2496 // isn't resumed.
2497 return null;
2498 }
2499 RuntimeException e = new RuntimeException(
2500 "Performing pause of activity that is not resumed: "
2501 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002502 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 }
2504 Bundle state = null;
2505 if (finished) {
2506 r.activity.mFinished = true;
2507 }
2508 try {
2509 // Next have the activity save its current state and managed dialogs...
2510 if (!r.activity.mFinished && saveState) {
2511 state = new Bundle();
2512 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2513 r.state = state;
2514 }
2515 // Now we are idle.
2516 r.activity.mCalled = false;
2517 mInstrumentation.callActivityOnPause(r.activity);
2518 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2519 if (!r.activity.mCalled) {
2520 throw new SuperNotCalledException(
2521 "Activity " + r.intent.getComponent().toShortString() +
2522 " did not call through to super.onPause()");
2523 }
2524
2525 } catch (SuperNotCalledException e) {
2526 throw e;
2527
2528 } catch (Exception e) {
2529 if (!mInstrumentation.onException(r.activity, e)) {
2530 throw new RuntimeException(
2531 "Unable to pause activity "
2532 + r.intent.getComponent().toShortString()
2533 + ": " + e.toString(), e);
2534 }
2535 }
2536 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002537
2538 // Notify any outstanding on paused listeners
2539 ArrayList<OnActivityPausedListener> listeners;
2540 synchronized (mOnPauseListeners) {
2541 listeners = mOnPauseListeners.remove(r.activity);
2542 }
2543 int size = (listeners != null ? listeners.size() : 0);
2544 for (int i = 0; i < size; i++) {
2545 listeners.get(i).onPaused(r.activity);
2546 }
2547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 return state;
2549 }
2550
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002551 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002552 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002553 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 }
2555
2556 private static class StopInfo {
2557 Bitmap thumbnail;
2558 CharSequence description;
2559 }
2560
2561 private final class ProviderRefCount {
2562 public int count;
2563 ProviderRefCount(int pCount) {
2564 count = pCount;
2565 }
2566 }
2567
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002568 /**
2569 * Core implementation of stopping an activity. Note this is a little
2570 * tricky because the server's meaning of stop is slightly different
2571 * than our client -- for the server, stop means to save state and give
2572 * it the result when it is done, but the window may still be visible.
2573 * For the client, we want to call onStop()/onStart() to indicate when
2574 * the activity's UI visibillity changes.
2575 */
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002576 private final void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002577 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002578 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002579 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 if (r != null) {
2581 if (!keepShown && r.stopped) {
2582 if (r.activity.mFinished) {
2583 // If we are finishing, we won't call onResume() in certain
2584 // cases. So here we likewise don't want to call onStop()
2585 // if the activity isn't resumed.
2586 return;
2587 }
2588 RuntimeException e = new RuntimeException(
2589 "Performing stop of activity that is not resumed: "
2590 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002591 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 }
2593
2594 if (info != null) {
2595 try {
2596 // First create a thumbnail for the activity...
Jim Miller0b2a6d02010-07-13 18:01:29 -07002597 info.thumbnail = createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 info.description = r.activity.onCreateDescription();
2599 } catch (Exception e) {
2600 if (!mInstrumentation.onException(r.activity, e)) {
2601 throw new RuntimeException(
2602 "Unable to save state of activity "
2603 + r.intent.getComponent().toShortString()
2604 + ": " + e.toString(), e);
2605 }
2606 }
2607 }
2608
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002609 // Next have the activity save its current state and managed dialogs...
2610 if (!r.activity.mFinished && saveState) {
2611 if (r.state == null) {
2612 state = new Bundle();
2613 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2614 r.state = state;
2615 } else {
2616 state = r.state;
2617 }
2618 }
2619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 if (!keepShown) {
2621 try {
2622 // Now we are idle.
2623 r.activity.performStop();
2624 } catch (Exception e) {
2625 if (!mInstrumentation.onException(r.activity, e)) {
2626 throw new RuntimeException(
2627 "Unable to stop activity "
2628 + r.intent.getComponent().toShortString()
2629 + ": " + e.toString(), e);
2630 }
2631 }
2632 r.stopped = true;
2633 }
2634
2635 r.paused = true;
2636 }
2637 }
2638
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002639 private final void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 View v = r.activity.mDecor;
2641 if (v != null) {
2642 if (show) {
2643 if (!r.activity.mVisibleFromServer) {
2644 r.activity.mVisibleFromServer = true;
2645 mNumVisibleActivities++;
2646 if (r.activity.mVisibleFromClient) {
2647 r.activity.makeVisible();
2648 }
2649 }
2650 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002651 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002652 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 performConfigurationChanged(r.activity, r.newConfig);
2654 r.newConfig = null;
2655 }
2656 } else {
2657 if (r.activity.mVisibleFromServer) {
2658 r.activity.mVisibleFromServer = false;
2659 mNumVisibleActivities--;
2660 v.setVisibility(View.INVISIBLE);
2661 }
2662 }
2663 }
2664 }
2665
2666 private final void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002667 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 r.activity.mConfigChangeFlags |= configChanges;
2669
2670 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002671 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002673 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 TAG, "Finishing stop of " + r + ": show=" + show
2675 + " win=" + r.window);
2676
2677 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07002678
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002679 // Make sure any pending writes are now committed.
2680 if (!r.isPreHoneycomb()) {
2681 QueuedWork.waitToFinish();
2682 }
2683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 // Tell activity manager we have been stopped.
2685 try {
2686 ActivityManagerNative.getDefault().activityStopped(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002687 r.token, r.state, info.thumbnail, info.description);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 } catch (RemoteException ex) {
2689 }
2690 }
2691
2692 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002693 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 if (r.stopped) {
2695 r.activity.performRestart();
2696 r.stopped = false;
2697 }
2698 }
2699
2700 private final void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002701 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08002702
2703 if (r == null) {
2704 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
2705 return;
2706 }
2707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002709 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 } else if (show && r.stopped) {
2711 // If we are getting ready to gc after going to the background, well
2712 // we are back active so skip it.
2713 unscheduleGcIdler();
2714
2715 r.activity.performRestart();
2716 r.stopped = false;
2717 }
2718 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07002719 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 TAG, "Handle window " + r + " visibility: " + show);
2721 updateVisibility(r, show);
2722 }
2723 }
2724
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002725 private final void handleSleeping(IBinder token, boolean sleeping) {
2726 ActivityClientRecord r = mActivities.get(token);
2727
2728 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002729 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002730 return;
2731 }
2732
2733 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002734 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002735 try {
2736 // Now we are idle.
2737 r.activity.performStop();
2738 } catch (Exception e) {
2739 if (!mInstrumentation.onException(r.activity, e)) {
2740 throw new RuntimeException(
2741 "Unable to stop activity "
2742 + r.intent.getComponent().toShortString()
2743 + ": " + e.toString(), e);
2744 }
2745 }
2746 r.stopped = true;
2747 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002748
2749 // Make sure any pending writes are now committed.
2750 if (!r.isPreHoneycomb()) {
2751 QueuedWork.waitToFinish();
2752 }
2753
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002754 // Tell activity manager we slept.
2755 try {
2756 ActivityManagerNative.getDefault().activitySlept(r.token);
2757 } catch (RemoteException ex) {
2758 }
2759 } else {
2760 if (r.stopped && r.activity.mVisibleFromServer) {
2761 r.activity.performRestart();
2762 r.stopped = false;
2763 }
2764 }
2765 }
2766
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002767 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08002768 synchronized (mPackages) {
2769 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002770 }
2771 }
2772
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002773 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
2774 LoadedApk apk = peekPackageInfo(data.pkg, false);
2775 if (apk != null) {
2776 apk.mCompatibilityInfo = data.info;
2777 }
2778 apk = peekPackageInfo(data.pkg, true);
2779 if (apk != null) {
2780 apk.mCompatibilityInfo = data.info;
2781 }
2782 handleConfigurationChanged(mConfiguration, data.info);
2783 }
2784
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002785 private final void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 final int N = results.size();
2787 for (int i=0; i<N; i++) {
2788 ResultInfo ri = results.get(i);
2789 try {
2790 if (ri.mData != null) {
2791 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
2792 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002793 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07002794 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 r.activity.dispatchActivityResult(ri.mResultWho,
2796 ri.mRequestCode, ri.mResultCode, ri.mData);
2797 } catch (Exception e) {
2798 if (!mInstrumentation.onException(r.activity, e)) {
2799 throw new RuntimeException(
2800 "Failure delivering result " + ri + " to activity "
2801 + r.intent.getComponent().toShortString()
2802 + ": " + e.toString(), e);
2803 }
2804 }
2805 }
2806 }
2807
2808 private final void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002809 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002810 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 if (r != null) {
2812 final boolean resumed = !r.paused;
2813 if (!r.activity.mFinished && r.activity.mDecor != null
2814 && r.hideForNow && resumed) {
2815 // We had hidden the activity because it started another
2816 // one... we have gotten a result back and we are not
2817 // paused, so make sure our window is visible.
2818 updateVisibility(r, true);
2819 }
2820 if (resumed) {
2821 try {
2822 // Now we are idle.
2823 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002824 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 mInstrumentation.callActivityOnPause(r.activity);
2826 if (!r.activity.mCalled) {
2827 throw new SuperNotCalledException(
2828 "Activity " + r.intent.getComponent().toShortString()
2829 + " did not call through to super.onPause()");
2830 }
2831 } catch (SuperNotCalledException e) {
2832 throw e;
2833 } catch (Exception e) {
2834 if (!mInstrumentation.onException(r.activity, e)) {
2835 throw new RuntimeException(
2836 "Unable to pause activity "
2837 + r.intent.getComponent().toShortString()
2838 + ": " + e.toString(), e);
2839 }
2840 }
2841 }
2842 deliverResults(r, res.results);
2843 if (resumed) {
2844 mInstrumentation.callActivityOnResume(r.activity);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002845 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 }
2847 }
2848 }
2849
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002850 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 return performDestroyActivity(token, finishing, 0, false);
2852 }
2853
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002854 private final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002856 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002857 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002858 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002860 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 r.activity.mConfigChangeFlags |= configChanges;
2862 if (finishing) {
2863 r.activity.mFinished = true;
2864 }
2865 if (!r.paused) {
2866 try {
2867 r.activity.mCalled = false;
2868 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07002869 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 r.activity.getComponentName().getClassName());
2871 if (!r.activity.mCalled) {
2872 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002873 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 + " did not call through to super.onPause()");
2875 }
2876 } catch (SuperNotCalledException e) {
2877 throw e;
2878 } catch (Exception e) {
2879 if (!mInstrumentation.onException(r.activity, e)) {
2880 throw new RuntimeException(
2881 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002882 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 + ": " + e.toString(), e);
2884 }
2885 }
2886 r.paused = true;
2887 }
2888 if (!r.stopped) {
2889 try {
2890 r.activity.performStop();
2891 } catch (SuperNotCalledException e) {
2892 throw e;
2893 } catch (Exception e) {
2894 if (!mInstrumentation.onException(r.activity, e)) {
2895 throw new RuntimeException(
2896 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002897 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 + ": " + e.toString(), e);
2899 }
2900 }
2901 r.stopped = true;
2902 }
2903 if (getNonConfigInstance) {
2904 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002905 r.lastNonConfigurationInstances
2906 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 } catch (Exception e) {
2908 if (!mInstrumentation.onException(r.activity, e)) {
2909 throw new RuntimeException(
2910 "Unable to retain activity "
2911 + r.intent.getComponent().toShortString()
2912 + ": " + e.toString(), e);
2913 }
2914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 }
2916 try {
2917 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002918 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 if (!r.activity.mCalled) {
2920 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002921 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 " did not call through to super.onDestroy()");
2923 }
2924 if (r.window != null) {
2925 r.window.closeAllPanels();
2926 }
2927 } catch (SuperNotCalledException e) {
2928 throw e;
2929 } catch (Exception e) {
2930 if (!mInstrumentation.onException(r.activity, e)) {
2931 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002932 "Unable to destroy activity " + safeToComponentShortString(r.intent)
2933 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 }
2935 }
2936 }
2937 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002938 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 return r;
2940 }
2941
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002942 private static String safeToComponentShortString(Intent intent) {
2943 ComponentName component = intent.getComponent();
2944 return component == null ? "[Unknown]" : component.toShortString();
2945 }
2946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 private final void handleDestroyActivity(IBinder token, boolean finishing,
2948 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002949 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 configChanges, getNonConfigInstance);
2951 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002952 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 WindowManager wm = r.activity.getWindowManager();
2954 View v = r.activity.mDecor;
2955 if (v != null) {
2956 if (r.activity.mVisibleFromServer) {
2957 mNumVisibleActivities--;
2958 }
2959 IBinder wtoken = v.getWindowToken();
2960 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002961 if (r.onlyLocalRequest) {
2962 // Hold off on removing this until the new activity's
2963 // window is being added.
2964 r.mPendingRemoveWindow = v;
2965 r.mPendingRemoveWindowManager = wm;
2966 } else {
2967 wm.removeViewImmediate(v);
2968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002970 if (wtoken != null && r.mPendingRemoveWindow == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 WindowManagerImpl.getDefault().closeAll(wtoken,
2972 r.activity.getClass().getName(), "Activity");
2973 }
2974 r.activity.mDecor = null;
2975 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002976 if (r.mPendingRemoveWindow == null) {
2977 // If we are delaying the removal of the activity window, then
2978 // we can't clean up all windows here. Note that we can't do
2979 // so later either, which means any windows that aren't closed
2980 // by the app will leak. Well we try to warning them a lot
2981 // about leaking windows, because that is a bug, so if they are
2982 // using this recreate facility then they get to live with leaks.
2983 WindowManagerImpl.getDefault().closeAll(token,
2984 r.activity.getClass().getName(), "Activity");
2985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986
2987 // Mocked out contexts won't be participating in the normal
2988 // process lifecycle, but if we're running with a proper
2989 // ApplicationContext we need to have it tear down things
2990 // cleanly.
2991 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002992 if (c instanceof ContextImpl) {
2993 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994 r.activity.getClass().getName(), "Activity");
2995 }
2996 }
2997 if (finishing) {
2998 try {
2999 ActivityManagerNative.getDefault().activityDestroyed(token);
3000 } catch (RemoteException ex) {
3001 // If the system process has died, it's game over for everyone.
3002 }
3003 }
3004 }
3005
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003006 public final void requestRelaunchActivity(IBinder token,
3007 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3008 int configChanges, boolean notResumed, Configuration config,
3009 boolean fromServer) {
3010 ActivityClientRecord target = null;
3011
3012 synchronized (mPackages) {
3013 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3014 ActivityClientRecord r = mRelaunchingActivities.get(i);
3015 if (r.token == token) {
3016 target = r;
3017 if (pendingResults != null) {
3018 if (r.pendingResults != null) {
3019 r.pendingResults.addAll(pendingResults);
3020 } else {
3021 r.pendingResults = pendingResults;
3022 }
3023 }
3024 if (pendingNewIntents != null) {
3025 if (r.pendingIntents != null) {
3026 r.pendingIntents.addAll(pendingNewIntents);
3027 } else {
3028 r.pendingIntents = pendingNewIntents;
3029 }
3030 }
3031 break;
3032 }
3033 }
3034
3035 if (target == null) {
3036 target = new ActivityClientRecord();
3037 target.token = token;
3038 target.pendingResults = pendingResults;
3039 target.pendingIntents = pendingNewIntents;
3040 if (!fromServer) {
3041 ActivityClientRecord existing = mActivities.get(token);
3042 if (existing != null) {
3043 target.startsNotResumed = existing.paused;
3044 }
3045 target.onlyLocalRequest = true;
3046 }
3047 mRelaunchingActivities.add(target);
3048 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3049 }
3050
3051 if (fromServer) {
3052 target.startsNotResumed = notResumed;
3053 target.onlyLocalRequest = false;
3054 }
3055 if (config != null) {
3056 target.createdConfig = config;
3057 }
3058 target.pendingConfigChanges |= configChanges;
3059 }
3060 }
3061
3062 private final void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 // If we are getting ready to gc after going to the background, well
3064 // we are back active so skip it.
3065 unscheduleGcIdler();
3066
3067 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003068 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070 // First: make sure we have the most recent configuration and most
3071 // recent version of the activity, or skip it if some previous call
3072 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003073 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 int N = mRelaunchingActivities.size();
3075 IBinder token = tmp.token;
3076 tmp = null;
3077 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003078 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 if (r.token == token) {
3080 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003081 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 mRelaunchingActivities.remove(i);
3083 i--;
3084 N--;
3085 }
3086 }
Bob Leee5408332009-09-04 18:31:17 -07003087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003089 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 return;
3091 }
Bob Leee5408332009-09-04 18:31:17 -07003092
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003093 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3094 + tmp.token + " with configChanges=0x"
3095 + Integer.toHexString(configChanges));
3096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 if (mPendingConfiguration != null) {
3098 changedConfig = mPendingConfiguration;
3099 mPendingConfiguration = null;
3100 }
3101 }
Bob Leee5408332009-09-04 18:31:17 -07003102
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003103 if (tmp.createdConfig != null) {
3104 // If the activity manager is passing us its current config,
3105 // assume that is really what we want regardless of what we
3106 // may have pending.
3107 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003108 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3109 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3110 if (changedConfig == null
3111 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3112 changedConfig = tmp.createdConfig;
3113 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003114 }
3115 }
3116
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003117 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003118 + tmp.token + ": changedConfig=" + changedConfig);
3119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 // If there was a pending configuration change, execute it first.
3121 if (changedConfig != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003122 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 }
Bob Leee5408332009-09-04 18:31:17 -07003124
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003125 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003126 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127 if (r == null) {
3128 return;
3129 }
Bob Leee5408332009-09-04 18:31:17 -07003130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003131 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003132 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003133 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003134
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003135 r.activity.mChangingConfigurations = true;
3136
Dianne Hackborne2b04802010-12-09 09:24:55 -08003137 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003139 performPauseActivity(r.token, false, r.isPreHoneycomb());
3140 }
3141 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3142 r.state = new Bundle();
3143 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 }
Bob Leee5408332009-09-04 18:31:17 -07003145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 r.activity = null;
3149 r.window = null;
3150 r.hideForNow = false;
3151 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003152 // Merge any pending results and pending intents; don't just replace them
3153 if (tmp.pendingResults != null) {
3154 if (r.pendingResults == null) {
3155 r.pendingResults = tmp.pendingResults;
3156 } else {
3157 r.pendingResults.addAll(tmp.pendingResults);
3158 }
3159 }
3160 if (tmp.pendingIntents != null) {
3161 if (r.pendingIntents == null) {
3162 r.pendingIntents = tmp.pendingIntents;
3163 } else {
3164 r.pendingIntents.addAll(tmp.pendingIntents);
3165 }
3166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003168
Christopher Tateb70f3df2009-04-07 16:07:59 -07003169 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 }
3171
3172 private final void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003173 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 Bitmap thumbnail = createThumbnailBitmap(r);
3175 CharSequence description = null;
3176 try {
3177 description = r.activity.onCreateDescription();
3178 } catch (Exception e) {
3179 if (!mInstrumentation.onException(r.activity, e)) {
3180 throw new RuntimeException(
3181 "Unable to create description of activity "
3182 + r.intent.getComponent().toShortString()
3183 + ": " + e.toString(), e);
3184 }
3185 }
3186 //System.out.println("Reporting top thumbnail " + thumbnail);
3187 try {
3188 ActivityManagerNative.getDefault().reportThumbnail(
3189 token, thumbnail, description);
3190 } catch (RemoteException ex) {
3191 }
3192 }
3193
3194 ArrayList<ComponentCallbacks> collectComponentCallbacksLocked(
3195 boolean allActivities, Configuration newConfig) {
3196 ArrayList<ComponentCallbacks> callbacks
3197 = new ArrayList<ComponentCallbacks>();
Bob Leee5408332009-09-04 18:31:17 -07003198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 if (mActivities.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003200 Iterator<ActivityClientRecord> it = mActivities.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 while (it.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003202 ActivityClientRecord ar = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 Activity a = ar.activity;
3204 if (a != null) {
3205 if (!ar.activity.mFinished && (allActivities ||
3206 (a != null && !ar.paused))) {
3207 // If the activity is currently resumed, its configuration
3208 // needs to change right now.
3209 callbacks.add(a);
3210 } else if (newConfig != null) {
3211 // Otherwise, we will tell it about the change
3212 // the next time it is resumed or shown. Note that
3213 // the activity manager may, before then, decide the
3214 // activity needs to be destroyed to handle its new
3215 // configuration.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003216 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Setting activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003217 + ar.activityInfo.name + " newConfig=" + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 ar.newConfig = newConfig;
3219 }
3220 }
3221 }
3222 }
3223 if (mServices.size() > 0) {
3224 Iterator<Service> it = mServices.values().iterator();
3225 while (it.hasNext()) {
3226 callbacks.add(it.next());
3227 }
3228 }
3229 synchronized (mProviderMap) {
3230 if (mLocalProviders.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003231 Iterator<ProviderClientRecord> it = mLocalProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 while (it.hasNext()) {
3233 callbacks.add(it.next().mLocalProvider);
3234 }
3235 }
3236 }
3237 final int N = mAllApplications.size();
3238 for (int i=0; i<N; i++) {
3239 callbacks.add(mAllApplications.get(i));
3240 }
Bob Leee5408332009-09-04 18:31:17 -07003241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 return callbacks;
3243 }
Bob Leee5408332009-09-04 18:31:17 -07003244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 private final void performConfigurationChanged(
3246 ComponentCallbacks cb, Configuration config) {
3247 // Only for Activity objects, check that they actually call up to their
3248 // superclass implementation. ComponentCallbacks is an interface, so
3249 // we check the runtime type and act accordingly.
3250 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3251 if (activity != null) {
3252 activity.mCalled = false;
3253 }
Bob Leee5408332009-09-04 18:31:17 -07003254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 boolean shouldChangeConfig = false;
3256 if ((activity == null) || (activity.mCurrentConfig == null)) {
3257 shouldChangeConfig = true;
3258 } else {
Bob Leee5408332009-09-04 18:31:17 -07003259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 // If the new config is the same as the config this Activity
3261 // is already running with then don't bother calling
3262 // onConfigurationChanged
3263 int diff = activity.mCurrentConfig.diff(config);
3264 if (diff != 0) {
Bob Leee5408332009-09-04 18:31:17 -07003265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 // If this activity doesn't handle any of the config changes
3267 // then don't bother calling onConfigurationChanged as we're
3268 // going to destroy it.
3269 if ((~activity.mActivityInfo.configChanges & diff) == 0) {
3270 shouldChangeConfig = true;
3271 }
3272 }
3273 }
Bob Leee5408332009-09-04 18:31:17 -07003274
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003275 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003276 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277 if (shouldChangeConfig) {
3278 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003280 if (activity != null) {
3281 if (!activity.mCalled) {
3282 throw new SuperNotCalledException(
3283 "Activity " + activity.getLocalClassName() +
3284 " did not call through to super.onConfigurationChanged()");
3285 }
3286 activity.mConfigChangeFlags = 0;
3287 activity.mCurrentConfig = new Configuration(config);
3288 }
3289 }
3290 }
3291
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003292 final boolean applyConfigurationToResourcesLocked(Configuration config,
3293 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003294 if (mResConfiguration == null) {
3295 mResConfiguration = new Configuration();
3296 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003297 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003298 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003299 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003300 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003301 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003302 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003303 DisplayMetrics dm = getDisplayMetricsLocked(true);
Bob Leee5408332009-09-04 18:31:17 -07003304
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003305 // set it for java, this also affects newly created Resources
3306 if (config.locale != null) {
3307 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 }
Bob Leee5408332009-09-04 18:31:17 -07003309
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003310 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003311
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003312 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003313 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003314
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003315 Iterator<WeakReference<Resources>> it =
3316 mActiveResources.values().iterator();
3317 //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3318 // mActiveResources.entrySet().iterator();
3319 while (it.hasNext()) {
3320 WeakReference<Resources> v = it.next();
3321 Resources r = v.get();
3322 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003323 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003324 + r + " config to: " + config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003325 r.updateConfiguration(config, dm, compat);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003326 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003327 // + " " + r + ": " + r.getConfiguration());
3328 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003329 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003330 it.remove();
3331 }
3332 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003333
3334 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003335 }
3336
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003337 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003338
3339 ArrayList<ComponentCallbacks> callbacks = null;
3340
3341 synchronized (mPackages) {
3342 if (mPendingConfiguration != null) {
3343 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3344 config = mPendingConfiguration;
3345 }
3346 mPendingConfiguration = null;
3347 }
3348
3349 if (config == null) {
3350 return;
3351 }
3352
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003353 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003354 + config);
3355
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003356 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 if (mConfiguration == null) {
3359 mConfiguration = new Configuration();
3360 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003361 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003362 return;
3363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 mConfiguration.updateFrom(config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003365 if (compat != null) {
3366 // Can't do this here, because it causes us to report the
3367 // comatible config back to the am as the current config
3368 // of the activity, and much unhappiness results.
3369 //compat.applyToConfiguration(mConfiguration);
3370 }
Bob Leee5408332009-09-04 18:31:17 -07003371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 callbacks = collectComponentCallbacksLocked(false, config);
3373 }
Bob Leee5408332009-09-04 18:31:17 -07003374
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003375 if (callbacks != null) {
3376 final int N = callbacks.size();
3377 for (int i=0; i<N; i++) {
3378 performConfigurationChanged(callbacks.get(i), config);
3379 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 }
3381 }
3382
3383 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003384 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 if (r == null || r.activity == null) {
3386 return;
3387 }
Bob Leee5408332009-09-04 18:31:17 -07003388
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003389 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003390 + r.activityInfo.name);
3391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 performConfigurationChanged(r.activity, mConfiguration);
3393 }
3394
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003395 final void handleProfilerControl(boolean start, ProfilerControlData pcd) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003396 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003397 try {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003398 Debug.startMethodTracing(pcd.path, pcd.fd.getFileDescriptor(),
3399 8 * 1024 * 1024, 0);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003400 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003401 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003402 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003403 } finally {
3404 try {
3405 pcd.fd.close();
3406 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003407 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003408 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003409 }
3410 } else {
3411 Debug.stopMethodTracing();
3412 }
3413 }
Bob Leee5408332009-09-04 18:31:17 -07003414
Andy McFadden824c5102010-07-09 16:26:57 -07003415 final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
3416 if (managed) {
3417 try {
3418 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3419 } catch (IOException e) {
3420 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3421 + " -- can the process access this path?");
3422 } finally {
3423 try {
3424 dhd.fd.close();
3425 } catch (IOException e) {
3426 Slog.w(TAG, "Failure closing profile fd", e);
3427 }
3428 }
3429 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003430 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003431 }
3432 }
3433
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003434 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3435 boolean hasPkgInfo = false;
3436 if (packages != null) {
3437 for (int i=packages.length-1; i>=0; i--) {
3438 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3439 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003440 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003441 ref = mPackages.get(packages[i]);
3442 if (ref != null && ref.get() != null) {
3443 hasPkgInfo = true;
3444 } else {
3445 ref = mResourcePackages.get(packages[i]);
3446 if (ref != null && ref.get() != null) {
3447 hasPkgInfo = true;
3448 }
3449 }
3450 }
3451 mPackages.remove(packages[i]);
3452 mResourcePackages.remove(packages[i]);
3453 }
3454 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003455 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003456 hasPkgInfo);
3457 }
3458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 final void handleLowMemory() {
Brian Carlstromed7e0072011-03-24 13:27:57 -07003460 ArrayList<ComponentCallbacks> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003462 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 callbacks = collectComponentCallbacksLocked(true, null);
3464 }
Bob Leee5408332009-09-04 18:31:17 -07003465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466 final int N = callbacks.size();
3467 for (int i=0; i<N; i++) {
3468 callbacks.get(i).onLowMemory();
3469 }
3470
Chris Tatece229052009-03-25 16:44:52 -07003471 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3472 if (Process.myUid() != Process.SYSTEM_UID) {
3473 int sqliteReleased = SQLiteDatabase.releaseMemory();
3474 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3475 }
Bob Leee5408332009-09-04 18:31:17 -07003476
Mike Reedcaf0df12009-04-27 14:32:05 -04003477 // Ask graphics to free up as much as possible (font/image caches)
3478 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479
3480 BinderInternal.forceGc("mem");
3481 }
3482
3483 private final void handleBindApplication(AppBindData data) {
3484 mBoundApplication = data;
3485 mConfiguration = new Configuration(data.config);
3486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003488 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 android.ddm.DdmHandleAppName.setAppName(data.processName);
3490
Joe Onoratod630f102011-03-17 18:42:26 -07003491 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
3492 // implementation to use the pool executor. Normally, we use the
3493 // serialized executor as the default. This has to happen in the
3494 // main thread so the main looper is set right.
3495 if (data.appInfo.targetSdkVersion <= 12) {
3496 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
3497 }
3498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 /*
3500 * Before spawning a new process, reset the time zone to be the system time zone.
3501 * This needs to be done because the system time zone could have changed after the
3502 * the spawning of this process. Without doing this this process would have the incorrect
3503 * system time zone.
3504 */
3505 TimeZone.setDefault(null);
3506
3507 /*
3508 * Initialize the default locale in this process for the reasons we set the time zone.
3509 */
3510 Locale.setDefault(data.config.locale);
3511
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003512 /*
3513 * Update the system configuration since its preloaded and might not
3514 * reflect configuration changes. The configuration object passed
3515 * in AppBindData can be safely assumed to be up to date
3516 */
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003517 Resources.getSystem().updateConfiguration(mConfiguration,
3518 Resources.getSystem().getDisplayMetrics(), data.compatInfo);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003519
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003520 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003522 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003523 * For system applications on userdebug/eng builds, log stack
3524 * traces of disk and network access to dropbox for analysis.
3525 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07003526 if ((data.appInfo.flags &
3527 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07003528 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
3529 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003530 }
3531
3532 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07003533 * For apps targetting SDK Honeycomb or later, we don't allow
3534 * network usage on the main event loop / UI thread.
3535 *
3536 * Note to those grepping: this is what ultimately throws
3537 * NetworkOnMainThreadException ...
3538 */
3539 if (data.appInfo.targetSdkVersion > 9) {
3540 StrictMode.enableDeathOnNetwork();
3541 }
3542
3543 /**
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003544 * Switch this process to density compatibility mode if needed.
3545 */
3546 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
3547 == 0) {
3548 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3549 }
Bob Leee5408332009-09-04 18:31:17 -07003550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
3552 // XXX should have option to change the port.
3553 Debug.changeDebugPort(8100);
3554 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003555 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 + " is waiting for the debugger on port 8100...");
3557
3558 IActivityManager mgr = ActivityManagerNative.getDefault();
3559 try {
3560 mgr.showWaitingForDebugger(mAppThread, true);
3561 } catch (RemoteException ex) {
3562 }
3563
3564 Debug.waitForDebugger();
3565
3566 try {
3567 mgr.showWaitingForDebugger(mAppThread, false);
3568 } catch (RemoteException ex) {
3569 }
3570
3571 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003572 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003573 + " can be debugged on port 8100...");
3574 }
3575 }
3576
Robert Greenwalt434203a2010-10-11 16:00:27 -07003577 /**
3578 * Initialize the default http proxy in this process for the reasons we set the time zone.
3579 */
3580 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
3581 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
3582 try {
3583 ProxyProperties proxyProperties = service.getProxy();
3584 Proxy.setHttpProxySystemProperty(proxyProperties);
3585 } catch (RemoteException e) {}
3586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003587 if (data.instrumentationName != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08003588 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 appContext.init(data.info, null, this);
3590 InstrumentationInfo ii = null;
3591 try {
3592 ii = appContext.getPackageManager().
3593 getInstrumentationInfo(data.instrumentationName, 0);
3594 } catch (PackageManager.NameNotFoundException e) {
3595 }
3596 if (ii == null) {
3597 throw new RuntimeException(
3598 "Unable to find instrumentation info for: "
3599 + data.instrumentationName);
3600 }
3601
3602 mInstrumentationAppDir = ii.sourceDir;
3603 mInstrumentationAppPackage = ii.packageName;
3604 mInstrumentedAppDir = data.info.getAppDir();
3605
3606 ApplicationInfo instrApp = new ApplicationInfo();
3607 instrApp.packageName = ii.packageName;
3608 instrApp.sourceDir = ii.sourceDir;
3609 instrApp.publicSourceDir = ii.publicSourceDir;
3610 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07003611 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003612 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003613 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08003614 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003615 instrContext.init(pi, null, this);
3616
3617 try {
3618 java.lang.ClassLoader cl = instrContext.getClassLoader();
3619 mInstrumentation = (Instrumentation)
3620 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
3621 } catch (Exception e) {
3622 throw new RuntimeException(
3623 "Unable to instantiate instrumentation "
3624 + data.instrumentationName + ": " + e.toString(), e);
3625 }
3626
3627 mInstrumentation.init(this, instrContext, appContext,
3628 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
3629
3630 if (data.profileFile != null && !ii.handleProfiling) {
3631 data.handlingProfiling = true;
3632 File file = new File(data.profileFile);
3633 file.getParentFile().mkdirs();
3634 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
3635 }
3636
3637 try {
3638 mInstrumentation.onCreate(data.instrumentationArgs);
3639 }
3640 catch (Exception e) {
3641 throw new RuntimeException(
3642 "Exception thrown in onCreate() of "
3643 + data.instrumentationName + ": " + e.toString(), e);
3644 }
3645
3646 } else {
3647 mInstrumentation = new Instrumentation();
3648 }
3649
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003650 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08003651 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003652 }
3653
Christopher Tate181fafa2009-05-14 11:12:14 -07003654 // If the app is being launched for full backup or restore, bring it up in
3655 // a restricted environment with the base application class.
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003656 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657 mInitialApplication = app;
3658
3659 List<ProviderInfo> providers = data.providers;
3660 if (providers != null) {
3661 installContentProviders(app, providers);
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003662 // For process that contains content providers, we want to
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003663 // ensure that the JIT is enabled "at some point".
3664 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 }
3666
3667 try {
3668 mInstrumentation.callApplicationOnCreate(app);
3669 } catch (Exception e) {
3670 if (!mInstrumentation.onException(app, e)) {
3671 throw new RuntimeException(
3672 "Unable to create application " + app.getClass().getName()
3673 + ": " + e.toString(), e);
3674 }
3675 }
3676 }
3677
3678 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
3679 IActivityManager am = ActivityManagerNative.getDefault();
3680 if (mBoundApplication.profileFile != null && mBoundApplication.handlingProfiling) {
3681 Debug.stopMethodTracing();
3682 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003683 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 // + ", app thr: " + mAppThread);
3685 try {
3686 am.finishInstrumentation(mAppThread, resultCode, results);
3687 } catch (RemoteException ex) {
3688 }
3689 }
3690
3691 private final void installContentProviders(
3692 Context context, List<ProviderInfo> providers) {
3693 final ArrayList<IActivityManager.ContentProviderHolder> results =
3694 new ArrayList<IActivityManager.ContentProviderHolder>();
3695
3696 Iterator<ProviderInfo> i = providers.iterator();
3697 while (i.hasNext()) {
3698 ProviderInfo cpi = i.next();
3699 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07003700 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003701 buf.append(cpi.authority);
3702 buf.append(": ");
3703 buf.append(cpi.name);
3704 Log.i(TAG, buf.toString());
3705 IContentProvider cp = installProvider(context, null, cpi, false);
3706 if (cp != null) {
3707 IActivityManager.ContentProviderHolder cph =
3708 new IActivityManager.ContentProviderHolder(cpi);
3709 cph.provider = cp;
3710 results.add(cph);
3711 // Don't ever unload this provider from the process.
3712 synchronized(mProviderMap) {
3713 mProviderRefCountMap.put(cp.asBinder(), new ProviderRefCount(10000));
3714 }
3715 }
3716 }
3717
3718 try {
3719 ActivityManagerNative.getDefault().publishContentProviders(
3720 getApplicationThread(), results);
3721 } catch (RemoteException ex) {
3722 }
3723 }
3724
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003725 private final IContentProvider getExistingProvider(Context context, String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003727 final ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 if (pr != null) {
3729 return pr.mProvider;
3730 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003731 return null;
3732 }
3733 }
3734
3735 private final IContentProvider getProvider(Context context, String name) {
3736 IContentProvider existing = getExistingProvider(context, name);
3737 if (existing != null) {
3738 return existing;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 }
3740
3741 IActivityManager.ContentProviderHolder holder = null;
3742 try {
3743 holder = ActivityManagerNative.getDefault().getContentProvider(
3744 getApplicationThread(), name);
3745 } catch (RemoteException ex) {
3746 }
3747 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003748 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 return null;
3750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003751
3752 IContentProvider prov = installProvider(context, holder.provider,
3753 holder.info, true);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003754 //Slog.i(TAG, "noReleaseNeeded=" + holder.noReleaseNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 if (holder.noReleaseNeeded || holder.provider == null) {
3756 // We are not going to release the provider if it is an external
3757 // provider that doesn't care about being released, or if it is
3758 // a local provider running in this process.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003759 //Slog.i(TAG, "*** NO RELEASE NEEDED");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 synchronized(mProviderMap) {
3761 mProviderRefCountMap.put(prov.asBinder(), new ProviderRefCount(10000));
3762 }
3763 }
3764 return prov;
3765 }
3766
3767 public final IContentProvider acquireProvider(Context c, String name) {
3768 IContentProvider provider = getProvider(c, name);
3769 if(provider == null)
3770 return null;
3771 IBinder jBinder = provider.asBinder();
3772 synchronized(mProviderMap) {
3773 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3774 if(prc == null) {
3775 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
3776 } else {
3777 prc.count++;
3778 } //end else
3779 } //end synchronized
3780 return provider;
3781 }
3782
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003783 public final IContentProvider acquireExistingProvider(Context c, String name) {
3784 IContentProvider provider = getExistingProvider(c, name);
3785 if(provider == null)
3786 return null;
3787 IBinder jBinder = provider.asBinder();
3788 synchronized(mProviderMap) {
3789 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3790 if(prc == null) {
3791 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
3792 } else {
3793 prc.count++;
3794 } //end else
3795 } //end synchronized
3796 return provider;
3797 }
3798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 public final boolean releaseProvider(IContentProvider provider) {
3800 if(provider == null) {
3801 return false;
3802 }
3803 IBinder jBinder = provider.asBinder();
3804 synchronized(mProviderMap) {
3805 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3806 if(prc == null) {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003807 if(localLOGV) Slog.v(TAG, "releaseProvider::Weird shouldn't be here");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808 return false;
3809 } else {
3810 prc.count--;
3811 if(prc.count == 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003812 // Schedule the actual remove asynchronously, since we
3813 // don't know the context this will be called in.
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003814 // TODO: it would be nice to post a delayed message, so
3815 // if we come back and need the same provider quickly
3816 // we will still have it available.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003817 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, provider);
3818 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003819 } //end if
3820 } //end else
3821 } //end synchronized
3822 return true;
3823 }
3824
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003825 final void completeRemoveProvider(IContentProvider provider) {
3826 IBinder jBinder = provider.asBinder();
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003827 String name = null;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003828 synchronized(mProviderMap) {
3829 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3830 if(prc != null && prc.count == 0) {
3831 mProviderRefCountMap.remove(jBinder);
3832 //invoke removeProvider to dereference provider
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003833 name = removeProviderLocked(provider);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003834 }
3835 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003836
3837 if (name != null) {
3838 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003839 if(localLOGV) Slog.v(TAG, "removeProvider::Invoking " +
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003840 "ActivityManagerNative.removeContentProvider(" + name);
3841 ActivityManagerNative.getDefault().removeContentProvider(
3842 getApplicationThread(), name);
3843 } catch (RemoteException e) {
3844 //do nothing content provider object is dead any way
3845 } //end catch
3846 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003847 }
3848
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003849 public final String removeProviderLocked(IContentProvider provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003850 if (provider == null) {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003851 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003852 }
3853 IBinder providerBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003854
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003855 String name = null;
3856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003857 // remove the provider from mProviderMap
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003858 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 while (iter.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003860 ProviderClientRecord pr = iter.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003861 IBinder myBinder = pr.mProvider.asBinder();
3862 if (myBinder == providerBinder) {
3863 //find if its published by this process itself
3864 if(pr.mLocalProvider != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003865 if(localLOGV) Slog.i(TAG, "removeProvider::found local provider returning");
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003866 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003867 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003868 if(localLOGV) Slog.v(TAG, "removeProvider::Not local provider Unlinking " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003869 "death recipient");
3870 //content provider is in another process
3871 myBinder.unlinkToDeath(pr, 0);
3872 iter.remove();
3873 //invoke remove only once for the very first name seen
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003874 if(name == null) {
3875 name = pr.mName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 }
3877 } //end if myBinder
3878 } //end while iter
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003879
3880 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003881 }
3882
3883 final void removeDeadProvider(String name, IContentProvider provider) {
3884 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003885 ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003886 if (pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003887 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003888 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003889 if (removed != null) {
3890 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
3891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 }
3893 }
3894 }
3895
3896 final void removeDeadProviderLocked(String name, IContentProvider provider) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003897 ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 if (pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003899 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003900 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003901 if (removed != null) {
3902 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
3903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 }
3905 }
3906
3907 private final IContentProvider installProvider(Context context,
3908 IContentProvider provider, ProviderInfo info, boolean noisy) {
3909 ContentProvider localProvider = null;
3910 if (provider == null) {
3911 if (noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003912 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003913 + info.name);
3914 }
3915 Context c = null;
3916 ApplicationInfo ai = info.applicationInfo;
3917 if (context.getPackageName().equals(ai.packageName)) {
3918 c = context;
3919 } else if (mInitialApplication != null &&
3920 mInitialApplication.getPackageName().equals(ai.packageName)) {
3921 c = mInitialApplication;
3922 } else {
3923 try {
3924 c = context.createPackageContext(ai.packageName,
3925 Context.CONTEXT_INCLUDE_CODE);
3926 } catch (PackageManager.NameNotFoundException e) {
3927 }
3928 }
3929 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003930 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003931 ai.packageName +
3932 " while loading content provider " +
3933 info.name);
3934 return null;
3935 }
3936 try {
3937 final java.lang.ClassLoader cl = c.getClassLoader();
3938 localProvider = (ContentProvider)cl.
3939 loadClass(info.name).newInstance();
3940 provider = localProvider.getIContentProvider();
3941 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003942 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 info.name + " from sourceDir " +
3944 info.applicationInfo.sourceDir);
3945 return null;
3946 }
Joe Onorato43a17652011-04-06 19:22:23 -07003947 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 TAG, "Instantiating local provider " + info.name);
3949 // XXX Need to create the correct context for this provider.
3950 localProvider.attachInfo(c, info);
3951 } catch (java.lang.Exception e) {
3952 if (!mInstrumentation.onException(null, e)) {
3953 throw new RuntimeException(
3954 "Unable to get provider " + info.name
3955 + ": " + e.toString(), e);
3956 }
3957 return null;
3958 }
3959 } else if (localLOGV) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003960 Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003961 + info.name);
3962 }
3963
3964 synchronized (mProviderMap) {
3965 // Cache the pointer for the remote provider.
3966 String names[] = PATTERN_SEMICOLON.split(info.authority);
3967 for (int i=0; i<names.length; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003968 ProviderClientRecord pr = new ProviderClientRecord(names[i], provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 localProvider);
3970 try {
3971 provider.asBinder().linkToDeath(pr, 0);
3972 mProviderMap.put(names[i], pr);
3973 } catch (RemoteException e) {
3974 return null;
3975 }
3976 }
3977 if (localProvider != null) {
3978 mLocalProviders.put(provider.asBinder(),
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003979 new ProviderClientRecord(null, provider, localProvider));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 }
3981 }
3982
3983 return provider;
3984 }
3985
3986 private final void attach(boolean system) {
3987 sThreadLocal.set(this);
3988 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989 if (!system) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07003990 ViewAncestor.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003991 public void run() {
3992 ensureJitEnabled();
3993 }
3994 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003995 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
3996 RuntimeInit.setApplicationObject(mAppThread.asBinder());
3997 IActivityManager mgr = ActivityManagerNative.getDefault();
3998 try {
3999 mgr.attachApplication(mAppThread);
4000 } catch (RemoteException ex) {
4001 }
4002 } else {
4003 // Don't set application object here -- if the system crashes,
4004 // we can't display an alert, we just want to die die die.
4005 android.ddm.DdmHandleAppName.setAppName("system_process");
4006 try {
4007 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004008 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 context.init(getSystemContext().mPackageInfo, null, this);
4010 Application app = Instrumentation.newApplication(Application.class, context);
4011 mAllApplications.add(app);
4012 mInitialApplication = app;
4013 app.onCreate();
4014 } catch (Exception e) {
4015 throw new RuntimeException(
4016 "Unable to instantiate Application():" + e.toString(), e);
4017 }
4018 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004019
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004020 ViewAncestor.addConfigCallback(new ComponentCallbacks() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004021 public void onConfigurationChanged(Configuration newConfig) {
4022 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004023 // We need to apply this change to the resources
4024 // immediately, because upon returning the view
4025 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004026 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004027 // This actually changed the resources! Tell
4028 // everyone about it.
4029 if (mPendingConfiguration == null ||
4030 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4031 mPendingConfiguration = newConfig;
4032
4033 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4034 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004035 }
4036 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004037 }
4038 public void onLowMemory() {
4039 }
4040 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004041 }
4042
4043 private final void detach()
4044 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004045 sThreadLocal.set(null);
4046 }
4047
4048 public static final ActivityThread systemMain() {
Romain Guy52339202010-09-03 16:04:46 -07004049 HardwareRenderer.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 ActivityThread thread = new ActivityThread();
4051 thread.attach(true);
4052 return thread;
4053 }
4054
4055 public final void installSystemProviders(List providers) {
4056 if (providers != null) {
4057 installContentProviders(mInitialApplication,
4058 (List<ProviderInfo>)providers);
4059 }
4060 }
4061
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004062 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004063 synchronized (mPackages) {
4064 if (mCoreSettings != null) {
4065 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004066 } else {
4067 return defaultValue;
4068 }
4069 }
4070 }
4071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072 public static final void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004073 SamplingProfilerIntegration.start();
4074
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004075 // CloseGuard defaults to true and can be quite spammy. We
4076 // disable it here, but selectively enable it later (via
4077 // StrictMode) on debug builds, but using DropBox, not logs.
4078 CloseGuard.setEnabled(false);
4079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080 Process.setArgV0("<pre-initialized>");
4081
4082 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07004083 if (sMainThreadHandler == null) {
4084 sMainThreadHandler = new Handler();
4085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004086
4087 ActivityThread thread = new ActivityThread();
4088 thread.attach(false);
4089
Dianne Hackborn287952c2010-09-22 22:34:31 -07004090 if (false) {
4091 Looper.myLooper().setMessageLogging(new
4092 LogPrinter(Log.DEBUG, "ActivityThread"));
4093 }
4094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004095 Looper.loop();
4096
4097 if (Process.supportsProcesses()) {
4098 throw new RuntimeException("Main thread loop unexpectedly exited");
4099 }
4100
4101 thread.detach();
Bob Leeeec2f412009-09-10 11:01:24 +02004102 String name = (thread.mInitialApplication != null)
4103 ? thread.mInitialApplication.getPackageName()
4104 : "<unknown>";
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004105 Slog.i(TAG, "Main thread of " + name + " is now exiting");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 }
4107}