blob: 67e88396a8830177c2361838cc13edfb73251425 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.os.Bundle;
49import android.os.Debug;
50import android.os.Handler;
51import android.os.IBinder;
52import android.os.Looper;
53import android.os.Message;
54import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070055import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.Process;
57import android.os.RemoteException;
58import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070059import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.SystemClock;
61import android.util.AndroidRuntimeException;
62import android.util.Config;
63import android.util.DisplayMetrics;
64import android.util.EventLog;
65import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070066import android.util.LogPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080067import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070069import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.view.View;
71import android.view.ViewDebug;
72import android.view.ViewManager;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -080073import android.view.ViewRoot;
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;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700127 static final boolean localLOGV = DEBUG ? Config.LOGD : Config.LOGV;
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;
341 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700342 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 Intent args;
344 public String toString() {
345 return "ServiceArgsData{token=" + token + " startId=" + startId
346 + " args=" + args + "}";
347 }
348 }
349
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400350 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700351 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 String processName;
353 ApplicationInfo appInfo;
354 List<ProviderInfo> providers;
355 ComponentName instrumentationName;
356 String profileFile;
357 Bundle instrumentationArgs;
358 IInstrumentationWatcher instrumentationWatcher;
359 int debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700360 boolean restrictedBackupMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400362 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 boolean handlingProfiling;
364 public String toString() {
365 return "AppBindData{appInfo=" + appInfo + "}";
366 }
367 }
368
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400369 static final class DumpComponentInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 FileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700371 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800372 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 String[] args;
374 boolean dumped;
375 }
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 }
405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 private final class ApplicationThread extends ApplicationThreadNative {
407 private static final String HEAP_COLUMN = "%17s %8s %8s %8s %8s";
408 private static final String ONE_COUNT_COLUMN = "%17s %8d";
409 private static final String TWO_COUNT_COLUMNS = "%17s %8d %17s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700410 private static final String TWO_COUNT_COLUMNS_DB = "%20s %8d %20s %8d";
411 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 // Formatting for checkin service - update version if row format changes
414 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 public final void schedulePauseActivity(IBinder token, boolean finished,
417 boolean userLeaving, int configChanges) {
418 queueOrSendMessage(
419 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
420 token,
421 (userLeaving ? 1 : 0),
422 configChanges);
423 }
424
425 public final void scheduleStopActivity(IBinder token, boolean showWindow,
426 int configChanges) {
427 queueOrSendMessage(
428 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
429 token, 0, configChanges);
430 }
431
432 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
433 queueOrSendMessage(
434 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
435 token);
436 }
437
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800438 public final void scheduleSleeping(IBinder token, boolean sleeping) {
439 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
440 }
441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
443 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
444 }
445
446 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
447 ResultData res = new ResultData();
448 res.token = token;
449 res.results = results;
450 queueOrSendMessage(H.SEND_RESULT, res);
451 }
452
453 // we use token to identify this activity without having to send the
454 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700455 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400456 ActivityInfo info, CompatibilityInfo compatInfo, Bundle state,
457 List<ResultInfo> pendingResults,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700459 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460
461 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700462 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 r.intent = intent;
464 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400465 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 r.state = state;
467
468 r.pendingResults = pendingResults;
469 r.pendingIntents = pendingNewIntents;
470
471 r.startsNotResumed = notResumed;
472 r.isForward = isForward;
473
474 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
475 }
476
477 public final void scheduleRelaunchActivity(IBinder token,
478 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800479 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800480 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
481 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 }
483
484 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
485 NewIntentData data = new NewIntentData();
486 data.intents = intents;
487 data.token = token;
488
489 queueOrSendMessage(H.NEW_INTENT, data);
490 }
491
492 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
493 int configChanges) {
494 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
495 configChanges);
496 }
497
498 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400499 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
500 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700501 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
502 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400504 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 queueOrSendMessage(H.RECEIVER, r);
506 }
507
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400508 public final void scheduleCreateBackupAgent(ApplicationInfo app,
509 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700510 CreateBackupAgentData d = new CreateBackupAgentData();
511 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400512 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700513 d.backupMode = backupMode;
514
515 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
516 }
517
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400518 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
519 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700520 CreateBackupAgentData d = new CreateBackupAgentData();
521 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400522 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700523
524 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
525 }
526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400528 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 CreateServiceData s = new CreateServiceData();
530 s.token = token;
531 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400532 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533
534 queueOrSendMessage(H.CREATE_SERVICE, s);
535 }
536
537 public final void scheduleBindService(IBinder token, Intent intent,
538 boolean rebind) {
539 BindServiceData s = new BindServiceData();
540 s.token = token;
541 s.intent = intent;
542 s.rebind = rebind;
543
544 queueOrSendMessage(H.BIND_SERVICE, s);
545 }
546
547 public final void scheduleUnbindService(IBinder token, Intent intent) {
548 BindServiceData s = new BindServiceData();
549 s.token = token;
550 s.intent = intent;
551
552 queueOrSendMessage(H.UNBIND_SERVICE, s);
553 }
554
555 public final void scheduleServiceArgs(IBinder token, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700556 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 ServiceArgsData s = new ServiceArgsData();
558 s.token = token;
559 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700560 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 s.args = args;
562
563 queueOrSendMessage(H.SERVICE_ARGS, s);
564 }
565
566 public final void scheduleStopService(IBinder token) {
567 queueOrSendMessage(H.STOP_SERVICE, token);
568 }
569
570 public final void bindApplication(String processName,
571 ApplicationInfo appInfo, List<ProviderInfo> providers,
572 ComponentName instrumentationName, String profileFile,
573 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Christopher Tate181fafa2009-05-14 11:12:14 -0700574 int debugMode, boolean isRestrictedBackupMode, Configuration config,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400575 CompatibilityInfo compatInfo, Map<String, IBinder> services,
576 Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577
578 if (services != null) {
579 // Setup the service cache in the ServiceManager
580 ServiceManager.initServiceCache(services);
581 }
582
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800583 setCoreSettings(coreSettings);
584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 AppBindData data = new AppBindData();
586 data.processName = processName;
587 data.appInfo = appInfo;
588 data.providers = providers;
589 data.instrumentationName = instrumentationName;
590 data.profileFile = profileFile;
591 data.instrumentationArgs = instrumentationArgs;
592 data.instrumentationWatcher = instrumentationWatcher;
593 data.debugMode = debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700594 data.restrictedBackupMode = isRestrictedBackupMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400596 data.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 queueOrSendMessage(H.BIND_APPLICATION, data);
598 }
599
600 public final void scheduleExit() {
601 queueOrSendMessage(H.EXIT_APPLICATION, null);
602 }
603
Christopher Tate5e1ab332009-09-01 20:32:49 -0700604 public final void scheduleSuicide() {
605 queueOrSendMessage(H.SUICIDE, null);
606 }
607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 public void requestThumbnail(IBinder token) {
609 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
610 }
611
612 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800613 synchronized (mPackages) {
614 if (mPendingConfiguration == null ||
615 mPendingConfiguration.isOtherSeqNewer(config)) {
616 mPendingConfiguration = config;
617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 }
619 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
620 }
621
622 public void updateTimeZone() {
623 TimeZone.setDefault(null);
624 }
625
Robert Greenwalt03595d02010-11-02 14:08:23 -0700626 public void clearDnsCache() {
627 // a non-standard API to get this to libcore
628 InetAddress.clearDnsCache();
629 }
630
Robert Greenwalt434203a2010-10-11 16:00:27 -0700631 public void setHttpProxy(String host, String port, String exclList) {
632 Proxy.setHttpProxySystemProperty(host, port, exclList);
633 }
634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 public void processInBackground() {
636 mH.removeMessages(H.GC_WHEN_IDLE);
637 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
638 }
639
640 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700641 DumpComponentInfo data = new DumpComponentInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 data.fd = fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700643 data.token = servicetoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 data.args = args;
645 data.dumped = false;
646 queueOrSendMessage(H.DUMP_SERVICE, data);
647 synchronized (data) {
648 while (!data.dumped) {
649 try {
650 data.wait();
651 } catch (InterruptedException e) {
652 // no need to do anything here, we will keep waiting until
653 // dumped is set
654 }
655 }
656 }
657 }
658
659 // This function exists to make sure all receiver dispatching is
660 // correctly ordered, since these are one-way calls and the binder driver
661 // applies transaction ordering per object for such calls.
662 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700663 int resultCode, String dataStr, Bundle extras, boolean ordered,
664 boolean sticky) throws RemoteException {
665 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 }
Bob Leee5408332009-09-04 18:31:17 -0700667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 public void scheduleLowMemory() {
669 queueOrSendMessage(H.LOW_MEMORY, null);
670 }
671
672 public void scheduleActivityConfigurationChanged(IBinder token) {
673 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
674 }
675
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700676 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd) {
677 ProfilerControlData pcd = new ProfilerControlData();
678 pcd.path = path;
679 pcd.fd = fd;
680 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800681 }
682
Andy McFadden824c5102010-07-09 16:26:57 -0700683 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
684 DumpHeapData dhd = new DumpHeapData();
685 dhd.path = path;
686 dhd.fd = fd;
687 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
688 }
689
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700690 public void setSchedulingGroup(int group) {
691 // Note: do this immediately, since going into the foreground
692 // should happen regardless of what pending work we have to do
693 // and the activity manager will wait for us to report back that
694 // we are done before sending us to the background.
695 try {
696 Process.setProcessGroup(Process.myPid(), group);
697 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800698 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700699 }
700 }
Bob Leee5408332009-09-04 18:31:17 -0700701
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700702 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
703 Debug.getMemoryInfo(outInfo);
704 }
Bob Leee5408332009-09-04 18:31:17 -0700705
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700706 public void dispatchPackageBroadcast(int cmd, String[] packages) {
707 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
708 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700709
710 public void scheduleCrash(String msg) {
711 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
712 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700713
Dianne Hackborn30d71892010-12-11 10:37:55 -0800714 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
715 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700716 DumpComponentInfo data = new DumpComponentInfo();
717 data.fd = fd;
718 data.token = activitytoken;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800719 data.prefix = prefix;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700720 data.args = args;
721 data.dumped = false;
722 queueOrSendMessage(H.DUMP_ACTIVITY, data);
723 synchronized (data) {
724 while (!data.dumped) {
725 try {
726 data.wait();
727 } catch (InterruptedException e) {
728 // no need to do anything here, we will keep waiting until
729 // dumped is set
730 }
731 }
732 }
733 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 @Override
736 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
737 long nativeMax = Debug.getNativeHeapSize() / 1024;
738 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
739 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
740
741 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
742 Debug.getMemoryInfo(memInfo);
743
744 final int nativeShared = memInfo.nativeSharedDirty;
745 final int dalvikShared = memInfo.dalvikSharedDirty;
746 final int otherShared = memInfo.otherSharedDirty;
747
748 final int nativePrivate = memInfo.nativePrivateDirty;
749 final int dalvikPrivate = memInfo.dalvikPrivateDirty;
750 final int otherPrivate = memInfo.otherPrivateDirty;
751
752 Runtime runtime = Runtime.getRuntime();
753
754 long dalvikMax = runtime.totalMemory() / 1024;
755 long dalvikFree = runtime.freeMemory() / 1024;
756 long dalvikAllocated = dalvikMax - dalvikFree;
757 long viewInstanceCount = ViewDebug.getViewInstanceCount();
758 long viewRootInstanceCount = ViewDebug.getViewRootInstanceCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700759 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
760 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 int globalAssetCount = AssetManager.getGlobalAssetCount();
762 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
763 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
764 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
765 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700766 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 long sqliteAllocated = SQLiteDebug.getHeapAllocatedSize() / 1024;
Vasu Noric3849202010-03-09 10:47:25 -0800768 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 // Check to see if we were called by checkin server. If so, print terse format.
771 boolean doCheckinFormat = false;
772 if (args != null) {
773 for (String arg : args) {
774 if ("-c".equals(arg)) doCheckinFormat = true;
775 }
776 }
Bob Leee5408332009-09-04 18:31:17 -0700777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 // For checkin, we print one long comma-separated list of values
779 if (doCheckinFormat) {
780 // NOTE: if you change anything significant below, also consider changing
781 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700782 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 // Header
786 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
787 pw.print(Process.myPid()); pw.print(',');
788 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 // Heap info - max
791 pw.print(nativeMax); pw.print(',');
792 pw.print(dalvikMax); pw.print(',');
793 pw.print("N/A,");
794 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 // Heap info - allocated
797 pw.print(nativeAllocated); pw.print(',');
798 pw.print(dalvikAllocated); pw.print(',');
799 pw.print("N/A,");
800 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 // Heap info - free
803 pw.print(nativeFree); pw.print(',');
804 pw.print(dalvikFree); pw.print(',');
805 pw.print("N/A,");
806 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 // Heap info - proportional set size
809 pw.print(memInfo.nativePss); pw.print(',');
810 pw.print(memInfo.dalvikPss); pw.print(',');
811 pw.print(memInfo.otherPss); pw.print(',');
812 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 // Heap info - shared
Bob Leee5408332009-09-04 18:31:17 -0700815 pw.print(nativeShared); pw.print(',');
816 pw.print(dalvikShared); pw.print(',');
817 pw.print(otherShared); pw.print(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 pw.print(nativeShared + dalvikShared + otherShared); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 // Heap info - private
Bob Leee5408332009-09-04 18:31:17 -0700821 pw.print(nativePrivate); pw.print(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 pw.print(dalvikPrivate); pw.print(',');
823 pw.print(otherPrivate); pw.print(',');
824 pw.print(nativePrivate + dalvikPrivate + otherPrivate); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 // Object counts
827 pw.print(viewInstanceCount); pw.print(',');
828 pw.print(viewRootInstanceCount); pw.print(',');
829 pw.print(appContextInstanceCount); pw.print(',');
830 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 pw.print(globalAssetCount); pw.print(',');
833 pw.print(globalAssetManagerCount); pw.print(',');
834 pw.print(binderLocalObjectCount); pw.print(',');
835 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 pw.print(binderDeathObjectCount); pw.print(',');
838 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 // SQL
841 pw.print(sqliteAllocated); pw.print(',');
Vasu Noric3849202010-03-09 10:47:25 -0800842 pw.print(stats.memoryUsed / 1024); pw.print(',');
843 pw.print(stats.pageCacheOverflo / 1024); pw.print(',');
844 pw.print(stats.largestMemAlloc / 1024); pw.print(',');
845 for (int i = 0; i < stats.dbStats.size(); i++) {
846 DbStats dbStats = stats.dbStats.get(i);
847 printRow(pw, DB_INFO_FORMAT, dbStats.pageSize, dbStats.dbSize,
Vasu Nori90a367262010-04-12 12:49:09 -0700848 dbStats.lookaside, dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -0800849 pw.print(',');
850 }
Bob Leee5408332009-09-04 18:31:17 -0700851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 return;
853 }
Bob Leee5408332009-09-04 18:31:17 -0700854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 // otherwise, show human-readable format
856 printRow(pw, HEAP_COLUMN, "", "native", "dalvik", "other", "total");
857 printRow(pw, HEAP_COLUMN, "size:", nativeMax, dalvikMax, "N/A", nativeMax + dalvikMax);
858 printRow(pw, HEAP_COLUMN, "allocated:", nativeAllocated, dalvikAllocated, "N/A",
859 nativeAllocated + dalvikAllocated);
860 printRow(pw, HEAP_COLUMN, "free:", nativeFree, dalvikFree, "N/A",
861 nativeFree + dalvikFree);
862
863 printRow(pw, HEAP_COLUMN, "(Pss):", memInfo.nativePss, memInfo.dalvikPss,
864 memInfo.otherPss, memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss);
865
866 printRow(pw, HEAP_COLUMN, "(shared dirty):", nativeShared, dalvikShared, otherShared,
867 nativeShared + dalvikShared + otherShared);
868 printRow(pw, HEAP_COLUMN, "(priv dirty):", nativePrivate, dalvikPrivate, otherPrivate,
869 nativePrivate + dalvikPrivate + otherPrivate);
870
871 pw.println(" ");
872 pw.println(" Objects");
873 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRoots:",
874 viewRootInstanceCount);
875
876 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
877 "Activities:", activityInstanceCount);
878
879 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
880 "AssetManagers:", globalAssetManagerCount);
881
882 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
883 "Proxy Binders:", binderProxyObjectCount);
884 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
885
886 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -0700887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 // SQLite mem info
889 pw.println(" ");
890 pw.println(" SQL");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700891 printRow(pw, TWO_COUNT_COLUMNS_DB, "heap:", sqliteAllocated, "MEMORY_USED:",
Vasu Noric3849202010-03-09 10:47:25 -0800892 stats.memoryUsed / 1024);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700893 printRow(pw, TWO_COUNT_COLUMNS_DB, "PAGECACHE_OVERFLOW:",
894 stats.pageCacheOverflo / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800895 pw.println(" ");
896 int N = stats.dbStats.size();
897 if (N > 0) {
898 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700899 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
900 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -0800901 for (int i = 0; i < N; i++) {
902 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700903 printRow(pw, DB_INFO_FORMAT,
904 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
905 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
906 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
907 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -0800908 }
909 }
Bob Leee5408332009-09-04 18:31:17 -0700910
Dianne Hackborn82e1ee92009-08-11 18:56:41 -0700911 // Asset details.
912 String assetAlloc = AssetManager.getAssetAllocations();
913 if (assetAlloc != null) {
914 pw.println(" ");
915 pw.println(" Asset Allocations");
916 pw.print(assetAlloc);
917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 }
919
920 private void printRow(PrintWriter pw, String format, Object...objs) {
921 pw.println(String.format(format, objs));
922 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800923
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800924 public void setCoreSettings(Bundle coreSettings) {
925 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800926 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400927
928 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
929 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
930 ucd.pkg = pkg;
931 ucd.info = info;
932 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 }
935
936 private final class H extends Handler {
937 public static final int LAUNCH_ACTIVITY = 100;
938 public static final int PAUSE_ACTIVITY = 101;
939 public static final int PAUSE_ACTIVITY_FINISHING= 102;
940 public static final int STOP_ACTIVITY_SHOW = 103;
941 public static final int STOP_ACTIVITY_HIDE = 104;
942 public static final int SHOW_WINDOW = 105;
943 public static final int HIDE_WINDOW = 106;
944 public static final int RESUME_ACTIVITY = 107;
945 public static final int SEND_RESULT = 108;
946 public static final int DESTROY_ACTIVITY = 109;
947 public static final int BIND_APPLICATION = 110;
948 public static final int EXIT_APPLICATION = 111;
949 public static final int NEW_INTENT = 112;
950 public static final int RECEIVER = 113;
951 public static final int CREATE_SERVICE = 114;
952 public static final int SERVICE_ARGS = 115;
953 public static final int STOP_SERVICE = 116;
954 public static final int REQUEST_THUMBNAIL = 117;
955 public static final int CONFIGURATION_CHANGED = 118;
956 public static final int CLEAN_UP_CONTEXT = 119;
957 public static final int GC_WHEN_IDLE = 120;
958 public static final int BIND_SERVICE = 121;
959 public static final int UNBIND_SERVICE = 122;
960 public static final int DUMP_SERVICE = 123;
961 public static final int LOW_MEMORY = 124;
962 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
963 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800964 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -0700965 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -0700966 public static final int DESTROY_BACKUP_AGENT = 129;
967 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700968 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800969 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700970 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700971 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -0700972 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700973 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800974 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800975 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400976 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -0700978 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 switch (code) {
980 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
981 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
982 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
983 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
984 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
985 case SHOW_WINDOW: return "SHOW_WINDOW";
986 case HIDE_WINDOW: return "HIDE_WINDOW";
987 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
988 case SEND_RESULT: return "SEND_RESULT";
989 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
990 case BIND_APPLICATION: return "BIND_APPLICATION";
991 case EXIT_APPLICATION: return "EXIT_APPLICATION";
992 case NEW_INTENT: return "NEW_INTENT";
993 case RECEIVER: return "RECEIVER";
994 case CREATE_SERVICE: return "CREATE_SERVICE";
995 case SERVICE_ARGS: return "SERVICE_ARGS";
996 case STOP_SERVICE: return "STOP_SERVICE";
997 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
998 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
999 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1000 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1001 case BIND_SERVICE: return "BIND_SERVICE";
1002 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1003 case DUMP_SERVICE: return "DUMP_SERVICE";
1004 case LOW_MEMORY: return "LOW_MEMORY";
1005 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1006 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001007 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001008 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1009 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001010 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001011 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001012 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001013 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001014 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001015 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001016 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001017 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001018 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001019 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 }
1021 }
1022 return "(unknown)";
1023 }
1024 public void handleMessage(Message msg) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001025 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 switch (msg.what) {
1027 case LAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001028 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029
1030 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001031 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001032 handleLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 } break;
1034 case RELAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001035 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001036 handleRelaunchActivity(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 } break;
1038 case PAUSE_ACTIVITY:
1039 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001040 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 break;
1042 case PAUSE_ACTIVITY_FINISHING:
1043 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
1044 break;
1045 case STOP_ACTIVITY_SHOW:
1046 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
1047 break;
1048 case STOP_ACTIVITY_HIDE:
1049 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
1050 break;
1051 case SHOW_WINDOW:
1052 handleWindowVisibility((IBinder)msg.obj, true);
1053 break;
1054 case HIDE_WINDOW:
1055 handleWindowVisibility((IBinder)msg.obj, false);
1056 break;
1057 case RESUME_ACTIVITY:
1058 handleResumeActivity((IBinder)msg.obj, true,
1059 msg.arg1 != 0);
1060 break;
1061 case SEND_RESULT:
1062 handleSendResult((ResultData)msg.obj);
1063 break;
1064 case DESTROY_ACTIVITY:
1065 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1066 msg.arg2, false);
1067 break;
1068 case BIND_APPLICATION:
1069 AppBindData data = (AppBindData)msg.obj;
1070 handleBindApplication(data);
1071 break;
1072 case EXIT_APPLICATION:
1073 if (mInitialApplication != null) {
1074 mInitialApplication.onTerminate();
1075 }
1076 Looper.myLooper().quit();
1077 break;
1078 case NEW_INTENT:
1079 handleNewIntent((NewIntentData)msg.obj);
1080 break;
1081 case RECEIVER:
1082 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001083 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 break;
1085 case CREATE_SERVICE:
1086 handleCreateService((CreateServiceData)msg.obj);
1087 break;
1088 case BIND_SERVICE:
1089 handleBindService((BindServiceData)msg.obj);
1090 break;
1091 case UNBIND_SERVICE:
1092 handleUnbindService((BindServiceData)msg.obj);
1093 break;
1094 case SERVICE_ARGS:
1095 handleServiceArgs((ServiceArgsData)msg.obj);
1096 break;
1097 case STOP_SERVICE:
1098 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001099 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 break;
1101 case REQUEST_THUMBNAIL:
1102 handleRequestThumbnail((IBinder)msg.obj);
1103 break;
1104 case CONFIGURATION_CHANGED:
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001105 handleConfigurationChanged((Configuration)msg.obj, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 break;
1107 case CLEAN_UP_CONTEXT:
1108 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1109 cci.context.performFinalCleanup(cci.who, cci.what);
1110 break;
1111 case GC_WHEN_IDLE:
1112 scheduleGcIdler();
1113 break;
1114 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001115 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 break;
1117 case LOW_MEMORY:
1118 handleLowMemory();
1119 break;
1120 case ACTIVITY_CONFIGURATION_CHANGED:
1121 handleActivityConfigurationChanged((IBinder)msg.obj);
1122 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001123 case PROFILER_CONTROL:
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001124 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001125 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001126 case CREATE_BACKUP_AGENT:
1127 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
1128 break;
1129 case DESTROY_BACKUP_AGENT:
1130 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
1131 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001132 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001133 Process.killProcess(Process.myPid());
1134 break;
1135 case REMOVE_PROVIDER:
1136 completeRemoveProvider((IContentProvider)msg.obj);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001137 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001138 case ENABLE_JIT:
1139 ensureJitEnabled();
1140 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001141 case DISPATCH_PACKAGE_BROADCAST:
1142 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
1143 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001144 case SCHEDULE_CRASH:
1145 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001146 case DUMP_HEAP:
1147 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1148 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001149 case DUMP_ACTIVITY:
1150 handleDumpActivity((DumpComponentInfo)msg.obj);
1151 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001152 case SLEEPING:
1153 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
1154 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001155 case SET_CORE_SETTINGS:
1156 handleSetCoreSettings((Bundle) msg.obj);
1157 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001158 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1159 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07001161 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 }
Bob Leee5408332009-09-04 18:31:17 -07001163
1164 void maybeSnapshot() {
1165 if (mBoundApplication != null) {
Sen Hubde75702010-05-28 01:54:03 -07001166 // convert the *private* ActivityThread.PackageInfo to *public* known
1167 // android.content.pm.PackageInfo
1168 String packageName = mBoundApplication.info.mPackageName;
1169 android.content.pm.PackageInfo packageInfo = null;
1170 try {
1171 Context context = getSystemContext();
1172 if(context == null) {
1173 Log.e(TAG, "cannot get a valid context");
1174 return;
1175 }
1176 PackageManager pm = context.getPackageManager();
1177 if(pm == null) {
1178 Log.e(TAG, "cannot get a valid PackageManager");
1179 return;
1180 }
1181 packageInfo = pm.getPackageInfo(
1182 packageName, PackageManager.GET_ACTIVITIES);
1183 } catch (NameNotFoundException e) {
1184 Log.e(TAG, "cannot get package info for " + packageName, e);
1185 }
1186 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001187 }
1188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190
1191 private final class Idler implements MessageQueue.IdleHandler {
1192 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001193 ActivityClientRecord a = mNewActivities;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 if (a != null) {
1195 mNewActivities = null;
1196 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001197 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001199 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 TAG, "Reporting idle of " + a +
1201 " finished=" +
1202 (a.activity != null ? a.activity.mFinished : false));
1203 if (a.activity != null && !a.activity.mFinished) {
1204 try {
Dianne Hackborne88846e2009-09-30 21:34:25 -07001205 am.activityIdle(a.token, a.createdConfig);
1206 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 } catch (RemoteException ex) {
1208 }
1209 }
1210 prev = a;
1211 a = a.nextIdle;
1212 prev.nextIdle = null;
1213 } while (a != null);
1214 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001215 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 return false;
1217 }
1218 }
1219
1220 final class GcIdler implements MessageQueue.IdleHandler {
1221 public final boolean queueIdle() {
1222 doGcIfNeeded();
1223 return false;
1224 }
1225 }
1226
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001227 private final static class ResourcesKey {
1228 final private String mResDir;
1229 final private float mScale;
1230 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001231
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001232 ResourcesKey(String resDir, float scale) {
1233 mResDir = resDir;
1234 mScale = scale;
1235 mHash = mResDir.hashCode() << 2 + (int) (mScale * 2);
1236 }
Bob Leee5408332009-09-04 18:31:17 -07001237
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001238 @Override
1239 public int hashCode() {
1240 return mHash;
1241 }
1242
1243 @Override
1244 public boolean equals(Object obj) {
1245 if (!(obj instanceof ResourcesKey)) {
1246 return false;
1247 }
1248 ResourcesKey peer = (ResourcesKey) obj;
1249 return mResDir.equals(peer.mResDir) && mScale == peer.mScale;
1250 }
1251 }
1252
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001253 public static final ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001254 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001257 public static final String currentPackageName() {
1258 ActivityThread am = currentActivityThread();
1259 return (am != null && am.mBoundApplication != null)
1260 ? am.mBoundApplication.processName : null;
1261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001263 public static final Application currentApplication() {
1264 ActivityThread am = currentActivityThread();
1265 return am != null ? am.mInitialApplication : null;
1266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001268 public static IPackageManager getPackageManager() {
1269 if (sPackageManager != null) {
1270 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1271 return sPackageManager;
1272 }
1273 IBinder b = ServiceManager.getService("package");
1274 //Slog.v("PackageManager", "default service binder = " + b);
1275 sPackageManager = IPackageManager.Stub.asInterface(b);
1276 //Slog.v("PackageManager", "default service = " + sPackageManager);
1277 return sPackageManager;
1278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001280 DisplayMetrics getDisplayMetricsLocked(boolean forceUpdate) {
1281 if (mDisplayMetrics != null && !forceUpdate) {
1282 return mDisplayMetrics;
1283 }
1284 if (mDisplay == null) {
1285 WindowManager wm = WindowManagerImpl.getDefault();
1286 mDisplay = wm.getDefaultDisplay();
1287 }
1288 DisplayMetrics metrics = mDisplayMetrics = new DisplayMetrics();
1289 mDisplay.getMetrics(metrics);
1290 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1291 // + metrics.heightPixels + " den=" + metrics.density
1292 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
1293 return metrics;
1294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001296 /**
1297 * Creates the top level Resources for applications with the given compatibility info.
1298 *
1299 * @param resDir the resource directory.
1300 * @param compInfo the compability info. It will use the default compatibility info when it's
1301 * null.
1302 */
1303 Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) {
1304 ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale);
1305 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001307 // Resources is app scale dependent.
1308 if (false) {
1309 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1310 + compInfo.applicationScale);
1311 }
1312 WeakReference<Resources> wr = mActiveResources.get(key);
1313 r = wr != null ? wr.get() : null;
1314 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1315 if (r != null && r.getAssets().isUpToDate()) {
1316 if (false) {
1317 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1318 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1319 }
1320 return r;
1321 }
1322 }
1323
1324 //if (r != null) {
1325 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1326 // + r + " " + resDir);
1327 //}
1328
1329 AssetManager assets = new AssetManager();
1330 if (assets.addAssetPath(resDir) == 0) {
1331 return null;
1332 }
1333
1334 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
1335 DisplayMetrics metrics = getDisplayMetricsLocked(false);
1336 r = new Resources(assets, metrics, getConfiguration(), compInfo);
1337 if (false) {
1338 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1339 + r.getConfiguration() + " appScale="
1340 + r.getCompatibilityInfo().applicationScale);
1341 }
1342
1343 synchronized (mPackages) {
1344 WeakReference<Resources> wr = mActiveResources.get(key);
1345 Resources existing = wr != null ? wr.get() : null;
1346 if (existing != null && existing.getAssets().isUpToDate()) {
1347 // Someone else already created the resources while we were
1348 // unlocked; go ahead and use theirs.
1349 r.getAssets().close();
1350 return existing;
1351 }
1352
1353 // XXX need to remove entries when weak references go away
1354 mActiveResources.put(key, new WeakReference<Resources>(r));
1355 return r;
1356 }
1357 }
1358
1359 /**
1360 * Creates the top level resources for the given package.
1361 */
1362 Resources getTopLevelResources(String resDir, LoadedApk pkgInfo) {
1363 return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo);
1364 }
1365
1366 final Handler getHandler() {
1367 return mH;
1368 }
1369
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001370 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1371 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001372 synchronized (mPackages) {
1373 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1375 ref = mPackages.get(packageName);
1376 } else {
1377 ref = mResourcePackages.get(packageName);
1378 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001379 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001380 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001381 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1382 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 if (packageInfo != null && (packageInfo.mResources == null
1384 || packageInfo.mResources.getAssets().isUpToDate())) {
1385 if (packageInfo.isSecurityViolation()
1386 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1387 throw new SecurityException(
1388 "Requesting code from " + packageName
1389 + " to be run in process "
1390 + mBoundApplication.processName
1391 + "/" + mBoundApplication.appInfo.uid);
1392 }
1393 return packageInfo;
1394 }
1395 }
1396
1397 ApplicationInfo ai = null;
1398 try {
1399 ai = getPackageManager().getApplicationInfo(packageName,
1400 PackageManager.GET_SHARED_LIBRARY_FILES);
1401 } catch (RemoteException e) {
1402 }
1403
1404 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001405 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 }
1407
1408 return null;
1409 }
1410
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001411 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1412 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1414 boolean securityViolation = includeCode && ai.uid != 0
1415 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
1416 ? ai.uid != mBoundApplication.appInfo.uid : true);
1417 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1418 |Context.CONTEXT_IGNORE_SECURITY))
1419 == Context.CONTEXT_INCLUDE_CODE) {
1420 if (securityViolation) {
1421 String msg = "Requesting code from " + ai.packageName
1422 + " (with uid " + ai.uid + ")";
1423 if (mBoundApplication != null) {
1424 msg = msg + " to be run in process "
1425 + mBoundApplication.processName + " (with uid "
1426 + mBoundApplication.appInfo.uid + ")";
1427 }
1428 throw new SecurityException(msg);
1429 }
1430 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001431 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 }
1433
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001434 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1435 CompatibilityInfo compatInfo) {
1436 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 }
1438
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001439 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1440 synchronized (mPackages) {
1441 WeakReference<LoadedApk> ref;
1442 if (includeCode) {
1443 ref = mPackages.get(packageName);
1444 } else {
1445 ref = mResourcePackages.get(packageName);
1446 }
1447 return ref != null ? ref.get() : null;
1448 }
1449 }
1450
1451 private final LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1453 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001454 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 if (includeCode) {
1456 ref = mPackages.get(aInfo.packageName);
1457 } else {
1458 ref = mResourcePackages.get(aInfo.packageName);
1459 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001460 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 if (packageInfo == null || (packageInfo.mResources != null
1462 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001463 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 : "Loading resource-only package ") + aInfo.packageName
1465 + " (in " + (mBoundApplication != null
1466 ? mBoundApplication.processName : null)
1467 + ")");
1468 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001469 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 securityViolation, includeCode &&
1471 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1472 if (includeCode) {
1473 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001474 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 } else {
1476 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001477 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 }
1479 }
1480 return packageInfo;
1481 }
1482 }
1483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 ActivityThread() {
1485 }
1486
1487 public ApplicationThread getApplicationThread()
1488 {
1489 return mAppThread;
1490 }
1491
1492 public Instrumentation getInstrumentation()
1493 {
1494 return mInstrumentation;
1495 }
1496
1497 public Configuration getConfiguration() {
1498 return mConfiguration;
1499 }
1500
1501 public boolean isProfiling() {
1502 return mBoundApplication != null && mBoundApplication.profileFile != null;
1503 }
1504
1505 public String getProfileFilePath() {
1506 return mBoundApplication.profileFile;
1507 }
1508
1509 public Looper getLooper() {
1510 return mLooper;
1511 }
1512
1513 public Application getApplication() {
1514 return mInitialApplication;
1515 }
Bob Leee5408332009-09-04 18:31:17 -07001516
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001517 public String getProcessName() {
1518 return mBoundApplication.processName;
1519 }
Bob Leee5408332009-09-04 18:31:17 -07001520
Dianne Hackborn21556372010-02-04 16:34:40 -08001521 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 synchronized (this) {
1523 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001524 ContextImpl context =
1525 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001526 LoadedApk info = new LoadedApk(this, "android", context, null,
1527 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 context.init(info, null, this);
1529 context.getResources().updateConfiguration(
1530 getConfiguration(), getDisplayMetricsLocked(false));
1531 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001532 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 // + ": " + context.getResources().getConfiguration());
1534 }
1535 }
1536 return mSystemContext;
1537 }
1538
Mike Cleron432b7132009-09-24 15:28:29 -07001539 public void installSystemApplicationInfo(ApplicationInfo info) {
1540 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001541 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001542 context.init(new LoadedApk(this, "android", context, info,
1543 new CompatibilityInfo(info, 0, false)), null, this);
Mike Cleron432b7132009-09-24 15:28:29 -07001544 }
1545 }
1546
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001547 void ensureJitEnabled() {
1548 if (!mJitEnabled) {
1549 mJitEnabled = true;
1550 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1551 }
1552 }
1553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 void scheduleGcIdler() {
1555 if (!mGcIdlerScheduled) {
1556 mGcIdlerScheduled = true;
1557 Looper.myQueue().addIdleHandler(mGcIdler);
1558 }
1559 mH.removeMessages(H.GC_WHEN_IDLE);
1560 }
1561
1562 void unscheduleGcIdler() {
1563 if (mGcIdlerScheduled) {
1564 mGcIdlerScheduled = false;
1565 Looper.myQueue().removeIdleHandler(mGcIdler);
1566 }
1567 mH.removeMessages(H.GC_WHEN_IDLE);
1568 }
1569
1570 void doGcIfNeeded() {
1571 mGcIdlerScheduled = false;
1572 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001573 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 // + "m now=" + now);
1575 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001576 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 BinderInternal.forceGc("bg");
1578 }
1579 }
1580
Jeff Hamilton52d32032011-01-08 15:31:26 -06001581 public void registerOnActivityPausedListener(Activity activity,
1582 OnActivityPausedListener listener) {
1583 synchronized (mOnPauseListeners) {
1584 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1585 if (list == null) {
1586 list = new ArrayList<OnActivityPausedListener>();
1587 mOnPauseListeners.put(activity, list);
1588 }
1589 list.add(listener);
1590 }
1591 }
1592
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001593 public void unregisterOnActivityPausedListener(Activity activity,
1594 OnActivityPausedListener listener) {
1595 synchronized (mOnPauseListeners) {
1596 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1597 if (list != null) {
1598 list.remove(listener);
1599 }
1600 }
1601 }
1602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 public final ActivityInfo resolveActivityInfo(Intent intent) {
1604 ActivityInfo aInfo = intent.resolveActivityInfo(
1605 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1606 if (aInfo == null) {
1607 // Throw an exception.
1608 Instrumentation.checkStartActivityResult(
1609 IActivityManager.START_CLASS_NOT_FOUND, intent);
1610 }
1611 return aInfo;
1612 }
Bob Leee5408332009-09-04 18:31:17 -07001613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001616 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001617 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001619 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 r.intent = intent;
1621 r.state = state;
1622 r.parent = parent;
1623 r.embeddedID = id;
1624 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001625 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 if (localLOGV) {
1627 ComponentName compname = intent.getComponent();
1628 String name;
1629 if (compname != null) {
1630 name = compname.toShortString();
1631 } else {
1632 name = "(Intent " + intent + ").getComponent() returned null";
1633 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001634 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 + ", comp=" + name
1636 + ", token=" + token);
1637 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001638 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 }
1640
1641 public final Activity getActivity(IBinder token) {
1642 return mActivities.get(token).activity;
1643 }
1644
1645 public final void sendActivityResult(
1646 IBinder token, String id, int requestCode,
1647 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001648 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001649 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1651 list.add(new ResultInfo(id, requestCode, resultCode, data));
1652 mAppThread.scheduleSendResult(token, list);
1653 }
1654
1655 // if the thread hasn't started yet, we don't have the handler, so just
1656 // save the messages until we're ready.
1657 private final void queueOrSendMessage(int what, Object obj) {
1658 queueOrSendMessage(what, obj, 0, 0);
1659 }
1660
1661 private final void queueOrSendMessage(int what, Object obj, int arg1) {
1662 queueOrSendMessage(what, obj, arg1, 0);
1663 }
1664
1665 private final void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
1666 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001667 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1669 + ": " + arg1 + " / " + obj);
1670 Message msg = Message.obtain();
1671 msg.what = what;
1672 msg.obj = obj;
1673 msg.arg1 = arg1;
1674 msg.arg2 = arg2;
1675 mH.sendMessage(msg);
1676 }
1677 }
1678
Dianne Hackborn21556372010-02-04 16:34:40 -08001679 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 String what) {
1681 ContextCleanupInfo cci = new ContextCleanupInfo();
1682 cci.context = context;
1683 cci.who = who;
1684 cci.what = what;
1685 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1686 }
1687
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001688 private final Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1690
1691 ActivityInfo aInfo = r.activityInfo;
1692 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001693 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 Context.CONTEXT_INCLUDE_CODE);
1695 }
Bob Leee5408332009-09-04 18:31:17 -07001696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 ComponentName component = r.intent.getComponent();
1698 if (component == null) {
1699 component = r.intent.resolveActivity(
1700 mInitialApplication.getPackageManager());
1701 r.intent.setComponent(component);
1702 }
1703
1704 if (r.activityInfo.targetActivity != null) {
1705 component = new ComponentName(r.activityInfo.packageName,
1706 r.activityInfo.targetActivity);
1707 }
1708
1709 Activity activity = null;
1710 try {
1711 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
1712 activity = mInstrumentation.newActivity(
1713 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08001714 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 r.intent.setExtrasClassLoader(cl);
1716 if (r.state != null) {
1717 r.state.setClassLoader(cl);
1718 }
1719 } catch (Exception e) {
1720 if (!mInstrumentation.onException(activity, e)) {
1721 throw new RuntimeException(
1722 "Unable to instantiate activity " + component
1723 + ": " + e.toString(), e);
1724 }
1725 }
1726
1727 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001728 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001729
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001730 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
1731 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 TAG, r + ": app=" + app
1733 + ", appName=" + app.getPackageName()
1734 + ", pkg=" + r.packageInfo.getPackageName()
1735 + ", comp=" + r.intent.getComponent().toShortString()
1736 + ", dir=" + r.packageInfo.getAppDir());
1737
1738 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001739 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 appContext.init(r.packageInfo, r.token, this);
1741 appContext.setOuterContext(activity);
1742 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
1743 Configuration config = new Configuration(mConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001744 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07001745 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001746 activity.attach(appContext, this, getInstrumentation(), r.token,
1747 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001748 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07001749
Christopher Tateb70f3df2009-04-07 16:07:59 -07001750 if (customIntent != null) {
1751 activity.mIntent = customIntent;
1752 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001753 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 activity.mStartedActivity = false;
1755 int theme = r.activityInfo.getThemeResource();
1756 if (theme != 0) {
1757 activity.setTheme(theme);
1758 }
1759
1760 activity.mCalled = false;
1761 mInstrumentation.callActivityOnCreate(activity, r.state);
1762 if (!activity.mCalled) {
1763 throw new SuperNotCalledException(
1764 "Activity " + r.intent.getComponent().toShortString() +
1765 " did not call through to super.onCreate()");
1766 }
1767 r.activity = activity;
1768 r.stopped = true;
1769 if (!r.activity.mFinished) {
1770 activity.performStart();
1771 r.stopped = false;
1772 }
1773 if (!r.activity.mFinished) {
1774 if (r.state != null) {
1775 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
1776 }
1777 }
1778 if (!r.activity.mFinished) {
1779 activity.mCalled = false;
1780 mInstrumentation.callActivityOnPostCreate(activity, r.state);
1781 if (!activity.mCalled) {
1782 throw new SuperNotCalledException(
1783 "Activity " + r.intent.getComponent().toShortString() +
1784 " did not call through to super.onPostCreate()");
1785 }
1786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 }
1788 r.paused = true;
1789
1790 mActivities.put(r.token, r);
1791
1792 } catch (SuperNotCalledException e) {
1793 throw e;
1794
1795 } catch (Exception e) {
1796 if (!mInstrumentation.onException(activity, e)) {
1797 throw new RuntimeException(
1798 "Unable to start activity " + component
1799 + ": " + e.toString(), e);
1800 }
1801 }
1802
1803 return activity;
1804 }
1805
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001806 private final void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 // If we are getting ready to gc after going to the background, well
1808 // we are back active so skip it.
1809 unscheduleGcIdler();
1810
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001811 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001813 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814
1815 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08001816 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001817 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 handleResumeActivity(r.token, false, r.isForward);
1819
1820 if (!r.activity.mFinished && r.startsNotResumed) {
1821 // The activity manager actually wants this one to start out
1822 // paused, because it needs to be visible but isn't in the
1823 // foreground. We accomplish this by going through the
1824 // normal startup (because activities expect to go through
1825 // onResume() the first time they run, before their window
1826 // is displayed), and then pausing it. However, in this case
1827 // we do -not- need to do the full pause cycle (of freezing
1828 // and such) because the activity manager assumes it can just
1829 // retain the current state it has.
1830 try {
1831 r.activity.mCalled = false;
1832 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001833 // We need to keep around the original state, in case
1834 // we need to be created again.
1835 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 if (!r.activity.mCalled) {
1837 throw new SuperNotCalledException(
1838 "Activity " + r.intent.getComponent().toShortString() +
1839 " did not call through to super.onPause()");
1840 }
1841
1842 } catch (SuperNotCalledException e) {
1843 throw e;
1844
1845 } catch (Exception e) {
1846 if (!mInstrumentation.onException(r.activity, e)) {
1847 throw new RuntimeException(
1848 "Unable to pause activity "
1849 + r.intent.getComponent().toShortString()
1850 + ": " + e.toString(), e);
1851 }
1852 }
1853 r.paused = true;
1854 }
1855 } else {
1856 // If there was an error, for any reason, tell the activity
1857 // manager to stop us.
1858 try {
1859 ActivityManagerNative.getDefault()
1860 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
1861 } catch (RemoteException ex) {
1862 }
1863 }
1864 }
1865
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001866 private final void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 List<Intent> intents) {
1868 final int N = intents.size();
1869 for (int i=0; i<N; i++) {
1870 Intent intent = intents.get(i);
1871 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001872 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
1874 }
1875 }
1876
1877 public final void performNewIntents(IBinder token,
1878 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001879 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 if (r != null) {
1881 final boolean resumed = !r.paused;
1882 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001883 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 mInstrumentation.callActivityOnPause(r.activity);
1885 }
1886 deliverNewIntents(r, intents);
1887 if (resumed) {
1888 mInstrumentation.callActivityOnResume(r.activity);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001889 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 }
1891 }
1892 }
Bob Leee5408332009-09-04 18:31:17 -07001893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 private final void handleNewIntent(NewIntentData data) {
1895 performNewIntents(data.token, data.intents);
1896 }
1897
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07001898 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
1899
1900 /**
1901 * Return the Intent that's currently being handled by a
1902 * BroadcastReceiver on this thread, or null if none.
1903 * @hide
1904 */
1905 public static Intent getIntentBeingBroadcast() {
1906 return sCurrentBroadcastIntent.get();
1907 }
1908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 private final void handleReceiver(ReceiverData data) {
1910 // If we are getting ready to gc after going to the background, well
1911 // we are back active so skip it.
1912 unscheduleGcIdler();
1913
1914 String component = data.intent.getComponent().getClassName();
1915
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001916 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001917 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918
1919 IActivityManager mgr = ActivityManagerNative.getDefault();
1920
1921 BroadcastReceiver receiver = null;
1922 try {
1923 java.lang.ClassLoader cl = packageInfo.getClassLoader();
1924 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07001925 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
1927 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07001928 if (DEBUG_BROADCAST) Slog.i(TAG,
1929 "Finishing failed broadcast to " + data.intent.getComponent());
1930 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 throw new RuntimeException(
1932 "Unable to instantiate receiver " + component
1933 + ": " + e.toString(), e);
1934 }
1935
1936 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001937 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001938
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001939 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 TAG, "Performing receive of " + data.intent
1941 + ": app=" + app
1942 + ", appName=" + app.getPackageName()
1943 + ", pkg=" + packageInfo.getPackageName()
1944 + ", comp=" + data.intent.getComponent().toShortString()
1945 + ", dir=" + packageInfo.getAppDir());
1946
Dianne Hackborn21556372010-02-04 16:34:40 -08001947 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07001948 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07001949 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 receiver.onReceive(context.getReceiverRestrictedContext(),
1951 data.intent);
1952 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07001953 if (DEBUG_BROADCAST) Slog.i(TAG,
1954 "Finishing failed broadcast to " + data.intent.getComponent());
1955 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 if (!mInstrumentation.onException(receiver, e)) {
1957 throw new RuntimeException(
1958 "Unable to start receiver " + component
1959 + ": " + e.toString(), e);
1960 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07001961 } finally {
1962 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 }
1964
Dianne Hackborne829fef2010-10-26 17:44:01 -07001965 if (receiver.getPendingResult() != null) {
1966 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 }
1968 }
1969
Christopher Tate181fafa2009-05-14 11:12:14 -07001970 // Instantiate a BackupAgent and tell it that it's alive
1971 private final void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001972 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07001973
1974 // no longer idle; we have backup work to do
1975 unscheduleGcIdler();
1976
1977 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001978 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07001979 String packageName = packageInfo.mPackageName;
1980 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001981 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07001982 + " already exists");
1983 return;
1984 }
Bob Leee5408332009-09-04 18:31:17 -07001985
Christopher Tate181fafa2009-05-14 11:12:14 -07001986 BackupAgent agent = null;
1987 String classname = data.appInfo.backupAgentName;
1988 if (classname == null) {
1989 if (data.backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08001990 Slog.e(TAG, "Attempted incremental backup but no defined agent for "
Christopher Tate181fafa2009-05-14 11:12:14 -07001991 + packageName);
1992 return;
1993 }
1994 classname = "android.app.FullBackupAgent";
1995 }
1996 try {
Christopher Tated1475e02009-07-09 15:36:17 -07001997 IBinder binder = null;
1998 try {
1999 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2000 agent = (BackupAgent) cl.loadClass(data.appInfo.backupAgentName).newInstance();
2001
2002 // set up the agent's context
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002003 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing BackupAgent "
Christopher Tated1475e02009-07-09 15:36:17 -07002004 + data.appInfo.backupAgentName);
2005
Dianne Hackborn21556372010-02-04 16:34:40 -08002006 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002007 context.init(packageInfo, null, this);
2008 context.setOuterContext(agent);
2009 agent.attach(context);
2010
2011 agent.onCreate();
2012 binder = agent.onBind();
2013 mBackupAgents.put(packageName, agent);
2014 } catch (Exception e) {
2015 // If this is during restore, fail silently; otherwise go
2016 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002017 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tated1475e02009-07-09 15:36:17 -07002018 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE) {
2019 throw e;
2020 }
2021 // falling through with 'binder' still null
2022 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002023
2024 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002025 try {
2026 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2027 } catch (RemoteException e) {
2028 // nothing to do.
2029 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002030 } catch (Exception e) {
2031 throw new RuntimeException("Unable to create BackupAgent "
2032 + data.appInfo.backupAgentName + ": " + e.toString(), e);
2033 }
2034 }
2035
2036 // Tear down a BackupAgent
2037 private final void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002038 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002039
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002040 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002041 String packageName = packageInfo.mPackageName;
2042 BackupAgent agent = mBackupAgents.get(packageName);
2043 if (agent != null) {
2044 try {
2045 agent.onDestroy();
2046 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002047 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002048 e.printStackTrace();
2049 }
2050 mBackupAgents.remove(packageName);
2051 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002052 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002053 }
2054 }
2055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 private final void handleCreateService(CreateServiceData data) {
2057 // If we are getting ready to gc after going to the background, well
2058 // we are back active so skip it.
2059 unscheduleGcIdler();
2060
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002061 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002062 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 Service service = null;
2064 try {
2065 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2066 service = (Service) cl.loadClass(data.info.name).newInstance();
2067 } catch (Exception e) {
2068 if (!mInstrumentation.onException(service, e)) {
2069 throw new RuntimeException(
2070 "Unable to instantiate service " + data.info.name
2071 + ": " + e.toString(), e);
2072 }
2073 }
2074
2075 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002076 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077
Dianne Hackborn21556372010-02-04 16:34:40 -08002078 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 context.init(packageInfo, null, this);
2080
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002081 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 context.setOuterContext(service);
2083 service.attach(context, this, data.info.name, data.token, app,
2084 ActivityManagerNative.getDefault());
2085 service.onCreate();
2086 mServices.put(data.token, service);
2087 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002088 ActivityManagerNative.getDefault().serviceDoneExecuting(
2089 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 } catch (RemoteException e) {
2091 // nothing to do.
2092 }
2093 } catch (Exception e) {
2094 if (!mInstrumentation.onException(service, e)) {
2095 throw new RuntimeException(
2096 "Unable to create service " + data.info.name
2097 + ": " + e.toString(), e);
2098 }
2099 }
2100 }
2101
2102 private final void handleBindService(BindServiceData data) {
2103 Service s = mServices.get(data.token);
2104 if (s != null) {
2105 try {
2106 data.intent.setExtrasClassLoader(s.getClassLoader());
2107 try {
2108 if (!data.rebind) {
2109 IBinder binder = s.onBind(data.intent);
2110 ActivityManagerNative.getDefault().publishService(
2111 data.token, data.intent, binder);
2112 } else {
2113 s.onRebind(data.intent);
2114 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002115 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002117 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 } catch (RemoteException ex) {
2119 }
2120 } catch (Exception e) {
2121 if (!mInstrumentation.onException(s, e)) {
2122 throw new RuntimeException(
2123 "Unable to bind to service " + s
2124 + " with " + data.intent + ": " + e.toString(), e);
2125 }
2126 }
2127 }
2128 }
2129
2130 private final void handleUnbindService(BindServiceData data) {
2131 Service s = mServices.get(data.token);
2132 if (s != null) {
2133 try {
2134 data.intent.setExtrasClassLoader(s.getClassLoader());
2135 boolean doRebind = s.onUnbind(data.intent);
2136 try {
2137 if (doRebind) {
2138 ActivityManagerNative.getDefault().unbindFinished(
2139 data.token, data.intent, doRebind);
2140 } else {
2141 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002142 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 }
2144 } catch (RemoteException ex) {
2145 }
2146 } catch (Exception e) {
2147 if (!mInstrumentation.onException(s, e)) {
2148 throw new RuntimeException(
2149 "Unable to unbind to service " + s
2150 + " with " + data.intent + ": " + e.toString(), e);
2151 }
2152 }
2153 }
2154 }
2155
Dianne Hackborn625ac272010-09-17 18:29:22 -07002156 private void handleDumpService(DumpComponentInfo info) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 try {
Dianne Hackborn625ac272010-09-17 18:29:22 -07002158 Service s = mServices.get(info.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 if (s != null) {
2160 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd));
2161 s.dump(info.fd, pw, info.args);
2162 pw.close();
2163 }
2164 } finally {
2165 synchronized (info) {
2166 info.dumped = true;
2167 info.notifyAll();
2168 }
2169 }
2170 }
2171
Dianne Hackborn625ac272010-09-17 18:29:22 -07002172 private void handleDumpActivity(DumpComponentInfo info) {
2173 try {
2174 ActivityClientRecord r = mActivities.get(info.token);
2175 if (r != null && r.activity != null) {
2176 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd));
Dianne Hackborn30d71892010-12-11 10:37:55 -08002177 r.activity.dump(info.prefix, info.fd, pw, info.args);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002178 pw.close();
2179 }
2180 } finally {
2181 synchronized (info) {
2182 info.dumped = true;
2183 info.notifyAll();
2184 }
2185 }
2186 }
2187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 private final void handleServiceArgs(ServiceArgsData data) {
2189 Service s = mServices.get(data.token);
2190 if (s != null) {
2191 try {
2192 if (data.args != null) {
2193 data.args.setExtrasClassLoader(s.getClassLoader());
2194 }
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002195 int res = s.onStartCommand(data.args, data.flags, data.startId);
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002196
2197 QueuedWork.waitToFinish();
2198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002200 ActivityManagerNative.getDefault().serviceDoneExecuting(
2201 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 } catch (RemoteException e) {
2203 // nothing to do.
2204 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002205 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 } catch (Exception e) {
2207 if (!mInstrumentation.onException(s, e)) {
2208 throw new RuntimeException(
2209 "Unable to start service " + s
2210 + " with " + data.args + ": " + e.toString(), e);
2211 }
2212 }
2213 }
2214 }
2215
2216 private final void handleStopService(IBinder token) {
2217 Service s = mServices.remove(token);
2218 if (s != null) {
2219 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002220 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 s.onDestroy();
2222 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002223 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002225 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002227
2228 QueuedWork.waitToFinish();
2229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002231 ActivityManagerNative.getDefault().serviceDoneExecuting(
2232 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 } catch (RemoteException e) {
2234 // nothing to do.
2235 }
2236 } catch (Exception e) {
2237 if (!mInstrumentation.onException(s, e)) {
2238 throw new RuntimeException(
2239 "Unable to stop service " + s
2240 + ": " + e.toString(), e);
2241 }
2242 }
2243 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002244 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 }
2246
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002247 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002249 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002250 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 + " finished=" + r.activity.mFinished);
2252 if (r != null && !r.activity.mFinished) {
2253 if (clearHide) {
2254 r.hideForNow = false;
2255 r.activity.mStartedActivity = false;
2256 }
2257 try {
2258 if (r.pendingIntents != null) {
2259 deliverNewIntents(r, r.pendingIntents);
2260 r.pendingIntents = null;
2261 }
2262 if (r.pendingResults != null) {
2263 deliverResults(r, r.pendingResults);
2264 r.pendingResults = null;
2265 }
2266 r.activity.performResume();
2267
Bob Leee5408332009-09-04 18:31:17 -07002268 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 r.paused = false;
2272 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 r.state = null;
2274 } catch (Exception e) {
2275 if (!mInstrumentation.onException(r.activity, e)) {
2276 throw new RuntimeException(
2277 "Unable to resume activity "
2278 + r.intent.getComponent().toShortString()
2279 + ": " + e.toString(), e);
2280 }
2281 }
2282 }
2283 return r;
2284 }
2285
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002286 final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
2287 if (r.mPendingRemoveWindow != null) {
2288 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2289 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2290 if (wtoken != null) {
2291 WindowManagerImpl.getDefault().closeAll(wtoken,
2292 r.activity.getClass().getName(), "Activity");
2293 }
2294 }
2295 r.mPendingRemoveWindow = null;
2296 r.mPendingRemoveWindowManager = null;
2297 }
2298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2300 // If we are getting ready to gc after going to the background, well
2301 // we are back active so skip it.
2302 unscheduleGcIdler();
2303
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002304 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305
2306 if (r != null) {
2307 final Activity a = r.activity;
2308
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002309 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 TAG, "Resume " + r + " started activity: " +
2311 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2312 + ", finished: " + a.mFinished);
2313
2314 final int forwardBit = isForward ?
2315 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 // If the window hasn't yet been added to the window manager,
2318 // and this guy didn't finish itself or start another activity,
2319 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002320 boolean willBeVisible = !a.mStartedActivity;
2321 if (!willBeVisible) {
2322 try {
2323 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2324 a.getActivityToken());
2325 } catch (RemoteException e) {
2326 }
2327 }
2328 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 r.window = r.activity.getWindow();
2330 View decor = r.window.getDecorView();
2331 decor.setVisibility(View.INVISIBLE);
2332 ViewManager wm = a.getWindowManager();
2333 WindowManager.LayoutParams l = r.window.getAttributes();
2334 a.mDecor = decor;
2335 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2336 l.softInputMode |= forwardBit;
2337 if (a.mVisibleFromClient) {
2338 a.mWindowAdded = true;
2339 wm.addView(decor, l);
2340 }
2341
2342 // If the window has already been added, but during resume
2343 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002344 // window visible.
2345 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002346 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 TAG, "Launch " + r + " mStartedActivity set");
2348 r.hideForNow = true;
2349 }
2350
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002351 // Get rid of anything left hanging around.
2352 cleanUpPendingRemoveWindows(r);
2353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 // The window is now visible if it has been added, we are not
2355 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002356 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002357 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002359 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002360 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 performConfigurationChanged(r.activity, r.newConfig);
2362 r.newConfig = null;
2363 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002364 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 + isForward);
2366 WindowManager.LayoutParams l = r.window.getAttributes();
2367 if ((l.softInputMode
2368 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2369 != forwardBit) {
2370 l.softInputMode = (l.softInputMode
2371 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2372 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002373 if (r.activity.mVisibleFromClient) {
2374 ViewManager wm = a.getWindowManager();
2375 View decor = r.window.getDecorView();
2376 wm.updateViewLayout(decor, l);
2377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 }
2379 r.activity.mVisibleFromServer = true;
2380 mNumVisibleActivities++;
2381 if (r.activity.mVisibleFromClient) {
2382 r.activity.makeVisible();
2383 }
2384 }
2385
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002386 if (!r.onlyLocalRequest) {
2387 r.nextIdle = mNewActivities;
2388 mNewActivities = r;
2389 if (localLOGV) Slog.v(
2390 TAG, "Scheduling idle handler for " + r);
2391 Looper.myQueue().addIdleHandler(new Idler());
2392 }
2393 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394
2395 } else {
2396 // If an exception was thrown when trying to resume, then
2397 // just end this activity.
2398 try {
2399 ActivityManagerNative.getDefault()
2400 .finishActivity(token, Activity.RESULT_CANCELED, null);
2401 } catch (RemoteException ex) {
2402 }
2403 }
2404 }
2405
2406 private int mThumbnailWidth = -1;
2407 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002408 private Bitmap mAvailThumbnailBitmap = null;
2409 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002411 private final Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002412 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002414 if (thumbnail == null) {
2415 int w = mThumbnailWidth;
2416 int h;
2417 if (w < 0) {
2418 Resources res = r.activity.getResources();
2419 mThumbnailHeight = h =
2420 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002422 mThumbnailWidth = w =
2423 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2424 } else {
2425 h = mThumbnailHeight;
2426 }
2427
2428 // On platforms where we don't want thumbnails, set dims to (0,0)
2429 if ((w > 0) && (h > 0)) {
2430 thumbnail = Bitmap.createBitmap(w, h, THUMBNAIL_FORMAT);
2431 thumbnail.eraseColor(0);
2432 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 }
2434
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002435 if (thumbnail != null) {
2436 Canvas cv = mThumbnailCanvas;
2437 if (cv == null) {
2438 mThumbnailCanvas = cv = new Canvas();
2439 }
2440
2441 cv.setBitmap(thumbnail);
2442 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2443 mAvailThumbnailBitmap = thumbnail;
2444 thumbnail = null;
2445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 } catch (Exception e) {
2449 if (!mInstrumentation.onException(r.activity, e)) {
2450 throw new RuntimeException(
2451 "Unable to create thumbnail of "
2452 + r.intent.getComponent().toShortString()
2453 + ": " + e.toString(), e);
2454 }
2455 thumbnail = null;
2456 }
2457
2458 return thumbnail;
2459 }
2460
2461 private final void handlePauseActivity(IBinder token, boolean finished,
2462 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002463 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002465 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 if (userLeaving) {
2467 performUserLeavingActivity(r);
2468 }
Bob Leee5408332009-09-04 18:31:17 -07002469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002471 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002473 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002474 if (r.isPreHoneycomb()) {
2475 QueuedWork.waitToFinish();
2476 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 // Tell the activity manager we have paused.
2479 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002480 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 } catch (RemoteException ex) {
2482 }
2483 }
2484 }
2485
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002486 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 mInstrumentation.callActivityOnUserLeaving(r.activity);
2488 }
2489
2490 final Bundle performPauseActivity(IBinder token, boolean finished,
2491 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002492 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 return r != null ? performPauseActivity(r, finished, saveState) : null;
2494 }
2495
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002496 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 boolean saveState) {
2498 if (r.paused) {
2499 if (r.activity.mFinished) {
2500 // If we are finishing, we won't call onResume() in certain cases.
2501 // So here we likewise don't want to call onPause() if the activity
2502 // isn't resumed.
2503 return null;
2504 }
2505 RuntimeException e = new RuntimeException(
2506 "Performing pause of activity that is not resumed: "
2507 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002508 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 }
2510 Bundle state = null;
2511 if (finished) {
2512 r.activity.mFinished = true;
2513 }
2514 try {
2515 // Next have the activity save its current state and managed dialogs...
2516 if (!r.activity.mFinished && saveState) {
2517 state = new Bundle();
2518 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2519 r.state = state;
2520 }
2521 // Now we are idle.
2522 r.activity.mCalled = false;
2523 mInstrumentation.callActivityOnPause(r.activity);
2524 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2525 if (!r.activity.mCalled) {
2526 throw new SuperNotCalledException(
2527 "Activity " + r.intent.getComponent().toShortString() +
2528 " did not call through to super.onPause()");
2529 }
2530
2531 } catch (SuperNotCalledException e) {
2532 throw e;
2533
2534 } catch (Exception e) {
2535 if (!mInstrumentation.onException(r.activity, e)) {
2536 throw new RuntimeException(
2537 "Unable to pause activity "
2538 + r.intent.getComponent().toShortString()
2539 + ": " + e.toString(), e);
2540 }
2541 }
2542 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002543
2544 // Notify any outstanding on paused listeners
2545 ArrayList<OnActivityPausedListener> listeners;
2546 synchronized (mOnPauseListeners) {
2547 listeners = mOnPauseListeners.remove(r.activity);
2548 }
2549 int size = (listeners != null ? listeners.size() : 0);
2550 for (int i = 0; i < size; i++) {
2551 listeners.get(i).onPaused(r.activity);
2552 }
2553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 return state;
2555 }
2556
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002557 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002558 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002559 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 }
2561
2562 private static class StopInfo {
2563 Bitmap thumbnail;
2564 CharSequence description;
2565 }
2566
2567 private final class ProviderRefCount {
2568 public int count;
2569 ProviderRefCount(int pCount) {
2570 count = pCount;
2571 }
2572 }
2573
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002574 /**
2575 * Core implementation of stopping an activity. Note this is a little
2576 * tricky because the server's meaning of stop is slightly different
2577 * than our client -- for the server, stop means to save state and give
2578 * it the result when it is done, but the window may still be visible.
2579 * For the client, we want to call onStop()/onStart() to indicate when
2580 * the activity's UI visibillity changes.
2581 */
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002582 private final void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002583 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002584 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002585 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 if (r != null) {
2587 if (!keepShown && r.stopped) {
2588 if (r.activity.mFinished) {
2589 // If we are finishing, we won't call onResume() in certain
2590 // cases. So here we likewise don't want to call onStop()
2591 // if the activity isn't resumed.
2592 return;
2593 }
2594 RuntimeException e = new RuntimeException(
2595 "Performing stop of activity that is not resumed: "
2596 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002597 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 }
2599
2600 if (info != null) {
2601 try {
2602 // First create a thumbnail for the activity...
Jim Miller0b2a6d02010-07-13 18:01:29 -07002603 info.thumbnail = createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 info.description = r.activity.onCreateDescription();
2605 } catch (Exception e) {
2606 if (!mInstrumentation.onException(r.activity, e)) {
2607 throw new RuntimeException(
2608 "Unable to save state of activity "
2609 + r.intent.getComponent().toShortString()
2610 + ": " + e.toString(), e);
2611 }
2612 }
2613 }
2614
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002615 // Next have the activity save its current state and managed dialogs...
2616 if (!r.activity.mFinished && saveState) {
2617 if (r.state == null) {
2618 state = new Bundle();
2619 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2620 r.state = state;
2621 } else {
2622 state = r.state;
2623 }
2624 }
2625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 if (!keepShown) {
2627 try {
2628 // Now we are idle.
2629 r.activity.performStop();
2630 } catch (Exception e) {
2631 if (!mInstrumentation.onException(r.activity, e)) {
2632 throw new RuntimeException(
2633 "Unable to stop activity "
2634 + r.intent.getComponent().toShortString()
2635 + ": " + e.toString(), e);
2636 }
2637 }
2638 r.stopped = true;
2639 }
2640
2641 r.paused = true;
2642 }
2643 }
2644
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002645 private final void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 View v = r.activity.mDecor;
2647 if (v != null) {
2648 if (show) {
2649 if (!r.activity.mVisibleFromServer) {
2650 r.activity.mVisibleFromServer = true;
2651 mNumVisibleActivities++;
2652 if (r.activity.mVisibleFromClient) {
2653 r.activity.makeVisible();
2654 }
2655 }
2656 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002657 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002658 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 performConfigurationChanged(r.activity, r.newConfig);
2660 r.newConfig = null;
2661 }
2662 } else {
2663 if (r.activity.mVisibleFromServer) {
2664 r.activity.mVisibleFromServer = false;
2665 mNumVisibleActivities--;
2666 v.setVisibility(View.INVISIBLE);
2667 }
2668 }
2669 }
2670 }
2671
2672 private final void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002673 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 r.activity.mConfigChangeFlags |= configChanges;
2675
2676 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002677 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002679 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 TAG, "Finishing stop of " + r + ": show=" + show
2681 + " win=" + r.window);
2682
2683 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07002684
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002685 // Make sure any pending writes are now committed.
2686 if (!r.isPreHoneycomb()) {
2687 QueuedWork.waitToFinish();
2688 }
2689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 // Tell activity manager we have been stopped.
2691 try {
2692 ActivityManagerNative.getDefault().activityStopped(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002693 r.token, r.state, info.thumbnail, info.description);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 } catch (RemoteException ex) {
2695 }
2696 }
2697
2698 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002699 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 if (r.stopped) {
2701 r.activity.performRestart();
2702 r.stopped = false;
2703 }
2704 }
2705
2706 private final void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002707 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08002708
2709 if (r == null) {
2710 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
2711 return;
2712 }
2713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002715 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 } else if (show && r.stopped) {
2717 // If we are getting ready to gc after going to the background, well
2718 // we are back active so skip it.
2719 unscheduleGcIdler();
2720
2721 r.activity.performRestart();
2722 r.stopped = false;
2723 }
2724 if (r.activity.mDecor != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002725 if (Config.LOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 TAG, "Handle window " + r + " visibility: " + show);
2727 updateVisibility(r, show);
2728 }
2729 }
2730
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002731 private final void handleSleeping(IBinder token, boolean sleeping) {
2732 ActivityClientRecord r = mActivities.get(token);
2733
2734 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002735 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002736 return;
2737 }
2738
2739 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002740 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002741 try {
2742 // Now we are idle.
2743 r.activity.performStop();
2744 } catch (Exception e) {
2745 if (!mInstrumentation.onException(r.activity, e)) {
2746 throw new RuntimeException(
2747 "Unable to stop activity "
2748 + r.intent.getComponent().toShortString()
2749 + ": " + e.toString(), e);
2750 }
2751 }
2752 r.stopped = true;
2753 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002754
2755 // Make sure any pending writes are now committed.
2756 if (!r.isPreHoneycomb()) {
2757 QueuedWork.waitToFinish();
2758 }
2759
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002760 // Tell activity manager we slept.
2761 try {
2762 ActivityManagerNative.getDefault().activitySlept(r.token);
2763 } catch (RemoteException ex) {
2764 }
2765 } else {
2766 if (r.stopped && r.activity.mVisibleFromServer) {
2767 r.activity.performRestart();
2768 r.stopped = false;
2769 }
2770 }
2771 }
2772
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002773 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08002774 synchronized (mPackages) {
2775 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002776 }
2777 }
2778
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002779 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
2780 LoadedApk apk = peekPackageInfo(data.pkg, false);
2781 if (apk != null) {
2782 apk.mCompatibilityInfo = data.info;
2783 }
2784 apk = peekPackageInfo(data.pkg, true);
2785 if (apk != null) {
2786 apk.mCompatibilityInfo = data.info;
2787 }
2788 handleConfigurationChanged(mConfiguration, data.info);
2789 }
2790
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002791 private final void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 final int N = results.size();
2793 for (int i=0; i<N; i++) {
2794 ResultInfo ri = results.get(i);
2795 try {
2796 if (ri.mData != null) {
2797 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
2798 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002799 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07002800 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 r.activity.dispatchActivityResult(ri.mResultWho,
2802 ri.mRequestCode, ri.mResultCode, ri.mData);
2803 } catch (Exception e) {
2804 if (!mInstrumentation.onException(r.activity, e)) {
2805 throw new RuntimeException(
2806 "Failure delivering result " + ri + " to activity "
2807 + r.intent.getComponent().toShortString()
2808 + ": " + e.toString(), e);
2809 }
2810 }
2811 }
2812 }
2813
2814 private final void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002815 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002816 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 if (r != null) {
2818 final boolean resumed = !r.paused;
2819 if (!r.activity.mFinished && r.activity.mDecor != null
2820 && r.hideForNow && resumed) {
2821 // We had hidden the activity because it started another
2822 // one... we have gotten a result back and we are not
2823 // paused, so make sure our window is visible.
2824 updateVisibility(r, true);
2825 }
2826 if (resumed) {
2827 try {
2828 // Now we are idle.
2829 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002830 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 mInstrumentation.callActivityOnPause(r.activity);
2832 if (!r.activity.mCalled) {
2833 throw new SuperNotCalledException(
2834 "Activity " + r.intent.getComponent().toShortString()
2835 + " did not call through to super.onPause()");
2836 }
2837 } catch (SuperNotCalledException e) {
2838 throw e;
2839 } catch (Exception e) {
2840 if (!mInstrumentation.onException(r.activity, e)) {
2841 throw new RuntimeException(
2842 "Unable to pause activity "
2843 + r.intent.getComponent().toShortString()
2844 + ": " + e.toString(), e);
2845 }
2846 }
2847 }
2848 deliverResults(r, res.results);
2849 if (resumed) {
2850 mInstrumentation.callActivityOnResume(r.activity);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002851 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 }
2853 }
2854 }
2855
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002856 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 return performDestroyActivity(token, finishing, 0, false);
2858 }
2859
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002860 private final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002862 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002863 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002864 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002866 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 r.activity.mConfigChangeFlags |= configChanges;
2868 if (finishing) {
2869 r.activity.mFinished = true;
2870 }
2871 if (!r.paused) {
2872 try {
2873 r.activity.mCalled = false;
2874 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07002875 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 r.activity.getComponentName().getClassName());
2877 if (!r.activity.mCalled) {
2878 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002879 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 + " did not call through to super.onPause()");
2881 }
2882 } catch (SuperNotCalledException e) {
2883 throw e;
2884 } catch (Exception e) {
2885 if (!mInstrumentation.onException(r.activity, e)) {
2886 throw new RuntimeException(
2887 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002888 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 + ": " + e.toString(), e);
2890 }
2891 }
2892 r.paused = true;
2893 }
2894 if (!r.stopped) {
2895 try {
2896 r.activity.performStop();
2897 } catch (SuperNotCalledException e) {
2898 throw e;
2899 } catch (Exception e) {
2900 if (!mInstrumentation.onException(r.activity, e)) {
2901 throw new RuntimeException(
2902 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002903 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 + ": " + e.toString(), e);
2905 }
2906 }
2907 r.stopped = true;
2908 }
2909 if (getNonConfigInstance) {
2910 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002911 r.lastNonConfigurationInstances
2912 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 } catch (Exception e) {
2914 if (!mInstrumentation.onException(r.activity, e)) {
2915 throw new RuntimeException(
2916 "Unable to retain activity "
2917 + r.intent.getComponent().toShortString()
2918 + ": " + e.toString(), e);
2919 }
2920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 }
2922 try {
2923 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002924 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 if (!r.activity.mCalled) {
2926 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002927 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 " did not call through to super.onDestroy()");
2929 }
2930 if (r.window != null) {
2931 r.window.closeAllPanels();
2932 }
2933 } catch (SuperNotCalledException e) {
2934 throw e;
2935 } catch (Exception e) {
2936 if (!mInstrumentation.onException(r.activity, e)) {
2937 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002938 "Unable to destroy activity " + safeToComponentShortString(r.intent)
2939 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 }
2941 }
2942 }
2943 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002944 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 return r;
2946 }
2947
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002948 private static String safeToComponentShortString(Intent intent) {
2949 ComponentName component = intent.getComponent();
2950 return component == null ? "[Unknown]" : component.toShortString();
2951 }
2952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 private final void handleDestroyActivity(IBinder token, boolean finishing,
2954 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002955 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 configChanges, getNonConfigInstance);
2957 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002958 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002959 WindowManager wm = r.activity.getWindowManager();
2960 View v = r.activity.mDecor;
2961 if (v != null) {
2962 if (r.activity.mVisibleFromServer) {
2963 mNumVisibleActivities--;
2964 }
2965 IBinder wtoken = v.getWindowToken();
2966 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002967 if (r.onlyLocalRequest) {
2968 // Hold off on removing this until the new activity's
2969 // window is being added.
2970 r.mPendingRemoveWindow = v;
2971 r.mPendingRemoveWindowManager = wm;
2972 } else {
2973 wm.removeViewImmediate(v);
2974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002976 if (wtoken != null && r.mPendingRemoveWindow == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 WindowManagerImpl.getDefault().closeAll(wtoken,
2978 r.activity.getClass().getName(), "Activity");
2979 }
2980 r.activity.mDecor = null;
2981 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002982 if (r.mPendingRemoveWindow == null) {
2983 // If we are delaying the removal of the activity window, then
2984 // we can't clean up all windows here. Note that we can't do
2985 // so later either, which means any windows that aren't closed
2986 // by the app will leak. Well we try to warning them a lot
2987 // about leaking windows, because that is a bug, so if they are
2988 // using this recreate facility then they get to live with leaks.
2989 WindowManagerImpl.getDefault().closeAll(token,
2990 r.activity.getClass().getName(), "Activity");
2991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992
2993 // Mocked out contexts won't be participating in the normal
2994 // process lifecycle, but if we're running with a proper
2995 // ApplicationContext we need to have it tear down things
2996 // cleanly.
2997 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002998 if (c instanceof ContextImpl) {
2999 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 r.activity.getClass().getName(), "Activity");
3001 }
3002 }
3003 if (finishing) {
3004 try {
3005 ActivityManagerNative.getDefault().activityDestroyed(token);
3006 } catch (RemoteException ex) {
3007 // If the system process has died, it's game over for everyone.
3008 }
3009 }
3010 }
3011
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003012 public final void requestRelaunchActivity(IBinder token,
3013 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3014 int configChanges, boolean notResumed, Configuration config,
3015 boolean fromServer) {
3016 ActivityClientRecord target = null;
3017
3018 synchronized (mPackages) {
3019 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3020 ActivityClientRecord r = mRelaunchingActivities.get(i);
3021 if (r.token == token) {
3022 target = r;
3023 if (pendingResults != null) {
3024 if (r.pendingResults != null) {
3025 r.pendingResults.addAll(pendingResults);
3026 } else {
3027 r.pendingResults = pendingResults;
3028 }
3029 }
3030 if (pendingNewIntents != null) {
3031 if (r.pendingIntents != null) {
3032 r.pendingIntents.addAll(pendingNewIntents);
3033 } else {
3034 r.pendingIntents = pendingNewIntents;
3035 }
3036 }
3037 break;
3038 }
3039 }
3040
3041 if (target == null) {
3042 target = new ActivityClientRecord();
3043 target.token = token;
3044 target.pendingResults = pendingResults;
3045 target.pendingIntents = pendingNewIntents;
3046 if (!fromServer) {
3047 ActivityClientRecord existing = mActivities.get(token);
3048 if (existing != null) {
3049 target.startsNotResumed = existing.paused;
3050 }
3051 target.onlyLocalRequest = true;
3052 }
3053 mRelaunchingActivities.add(target);
3054 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3055 }
3056
3057 if (fromServer) {
3058 target.startsNotResumed = notResumed;
3059 target.onlyLocalRequest = false;
3060 }
3061 if (config != null) {
3062 target.createdConfig = config;
3063 }
3064 target.pendingConfigChanges |= configChanges;
3065 }
3066 }
3067
3068 private final void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 // If we are getting ready to gc after going to the background, well
3070 // we are back active so skip it.
3071 unscheduleGcIdler();
3072
3073 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003074 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 // First: make sure we have the most recent configuration and most
3077 // recent version of the activity, or skip it if some previous call
3078 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003079 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 int N = mRelaunchingActivities.size();
3081 IBinder token = tmp.token;
3082 tmp = null;
3083 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003084 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 if (r.token == token) {
3086 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003087 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 mRelaunchingActivities.remove(i);
3089 i--;
3090 N--;
3091 }
3092 }
Bob Leee5408332009-09-04 18:31:17 -07003093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003095 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 return;
3097 }
Bob Leee5408332009-09-04 18:31:17 -07003098
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003099 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3100 + tmp.token + " with configChanges=0x"
3101 + Integer.toHexString(configChanges));
3102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 if (mPendingConfiguration != null) {
3104 changedConfig = mPendingConfiguration;
3105 mPendingConfiguration = null;
3106 }
3107 }
Bob Leee5408332009-09-04 18:31:17 -07003108
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003109 if (tmp.createdConfig != null) {
3110 // If the activity manager is passing us its current config,
3111 // assume that is really what we want regardless of what we
3112 // may have pending.
3113 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003114 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3115 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3116 if (changedConfig == null
3117 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3118 changedConfig = tmp.createdConfig;
3119 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003120 }
3121 }
3122
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003123 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003124 + tmp.token + ": changedConfig=" + changedConfig);
3125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 // If there was a pending configuration change, execute it first.
3127 if (changedConfig != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003128 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 }
Bob Leee5408332009-09-04 18:31:17 -07003130
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003131 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003132 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 if (r == null) {
3134 return;
3135 }
Bob Leee5408332009-09-04 18:31:17 -07003136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003138 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003139 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003140
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003141 r.activity.mChangingConfigurations = true;
3142
Dianne Hackborne2b04802010-12-09 09:24:55 -08003143 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003145 performPauseActivity(r.token, false, r.isPreHoneycomb());
3146 }
3147 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3148 r.state = new Bundle();
3149 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 }
Bob Leee5408332009-09-04 18:31:17 -07003151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 r.activity = null;
3155 r.window = null;
3156 r.hideForNow = false;
3157 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003158 // Merge any pending results and pending intents; don't just replace them
3159 if (tmp.pendingResults != null) {
3160 if (r.pendingResults == null) {
3161 r.pendingResults = tmp.pendingResults;
3162 } else {
3163 r.pendingResults.addAll(tmp.pendingResults);
3164 }
3165 }
3166 if (tmp.pendingIntents != null) {
3167 if (r.pendingIntents == null) {
3168 r.pendingIntents = tmp.pendingIntents;
3169 } else {
3170 r.pendingIntents.addAll(tmp.pendingIntents);
3171 }
3172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003173 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003174
Christopher Tateb70f3df2009-04-07 16:07:59 -07003175 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 }
3177
3178 private final void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003179 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 Bitmap thumbnail = createThumbnailBitmap(r);
3181 CharSequence description = null;
3182 try {
3183 description = r.activity.onCreateDescription();
3184 } catch (Exception e) {
3185 if (!mInstrumentation.onException(r.activity, e)) {
3186 throw new RuntimeException(
3187 "Unable to create description of activity "
3188 + r.intent.getComponent().toShortString()
3189 + ": " + e.toString(), e);
3190 }
3191 }
3192 //System.out.println("Reporting top thumbnail " + thumbnail);
3193 try {
3194 ActivityManagerNative.getDefault().reportThumbnail(
3195 token, thumbnail, description);
3196 } catch (RemoteException ex) {
3197 }
3198 }
3199
3200 ArrayList<ComponentCallbacks> collectComponentCallbacksLocked(
3201 boolean allActivities, Configuration newConfig) {
3202 ArrayList<ComponentCallbacks> callbacks
3203 = new ArrayList<ComponentCallbacks>();
Bob Leee5408332009-09-04 18:31:17 -07003204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 if (mActivities.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003206 Iterator<ActivityClientRecord> it = mActivities.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 while (it.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003208 ActivityClientRecord ar = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 Activity a = ar.activity;
3210 if (a != null) {
3211 if (!ar.activity.mFinished && (allActivities ||
3212 (a != null && !ar.paused))) {
3213 // If the activity is currently resumed, its configuration
3214 // needs to change right now.
3215 callbacks.add(a);
3216 } else if (newConfig != null) {
3217 // Otherwise, we will tell it about the change
3218 // the next time it is resumed or shown. Note that
3219 // the activity manager may, before then, decide the
3220 // activity needs to be destroyed to handle its new
3221 // configuration.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003222 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Setting activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003223 + ar.activityInfo.name + " newConfig=" + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 ar.newConfig = newConfig;
3225 }
3226 }
3227 }
3228 }
3229 if (mServices.size() > 0) {
3230 Iterator<Service> it = mServices.values().iterator();
3231 while (it.hasNext()) {
3232 callbacks.add(it.next());
3233 }
3234 }
3235 synchronized (mProviderMap) {
3236 if (mLocalProviders.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003237 Iterator<ProviderClientRecord> it = mLocalProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 while (it.hasNext()) {
3239 callbacks.add(it.next().mLocalProvider);
3240 }
3241 }
3242 }
3243 final int N = mAllApplications.size();
3244 for (int i=0; i<N; i++) {
3245 callbacks.add(mAllApplications.get(i));
3246 }
Bob Leee5408332009-09-04 18:31:17 -07003247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 return callbacks;
3249 }
Bob Leee5408332009-09-04 18:31:17 -07003250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 private final void performConfigurationChanged(
3252 ComponentCallbacks cb, Configuration config) {
3253 // Only for Activity objects, check that they actually call up to their
3254 // superclass implementation. ComponentCallbacks is an interface, so
3255 // we check the runtime type and act accordingly.
3256 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3257 if (activity != null) {
3258 activity.mCalled = false;
3259 }
Bob Leee5408332009-09-04 18:31:17 -07003260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 boolean shouldChangeConfig = false;
3262 if ((activity == null) || (activity.mCurrentConfig == null)) {
3263 shouldChangeConfig = true;
3264 } else {
Bob Leee5408332009-09-04 18:31:17 -07003265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 // If the new config is the same as the config this Activity
3267 // is already running with then don't bother calling
3268 // onConfigurationChanged
3269 int diff = activity.mCurrentConfig.diff(config);
3270 if (diff != 0) {
Bob Leee5408332009-09-04 18:31:17 -07003271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 // If this activity doesn't handle any of the config changes
3273 // then don't bother calling onConfigurationChanged as we're
3274 // going to destroy it.
3275 if ((~activity.mActivityInfo.configChanges & diff) == 0) {
3276 shouldChangeConfig = true;
3277 }
3278 }
3279 }
Bob Leee5408332009-09-04 18:31:17 -07003280
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003281 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003282 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 if (shouldChangeConfig) {
3284 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 if (activity != null) {
3287 if (!activity.mCalled) {
3288 throw new SuperNotCalledException(
3289 "Activity " + activity.getLocalClassName() +
3290 " did not call through to super.onConfigurationChanged()");
3291 }
3292 activity.mConfigChangeFlags = 0;
3293 activity.mCurrentConfig = new Configuration(config);
3294 }
3295 }
3296 }
3297
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003298 final boolean applyConfigurationToResourcesLocked(Configuration config,
3299 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003300 if (mResConfiguration == null) {
3301 mResConfiguration = new Configuration();
3302 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003303 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003304 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003305 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003306 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003307 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003308 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003309 DisplayMetrics dm = getDisplayMetricsLocked(true);
Bob Leee5408332009-09-04 18:31:17 -07003310
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003311 // set it for java, this also affects newly created Resources
3312 if (config.locale != null) {
3313 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 }
Bob Leee5408332009-09-04 18:31:17 -07003315
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003316 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003317
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003318 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003319 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003320
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003321 Iterator<WeakReference<Resources>> it =
3322 mActiveResources.values().iterator();
3323 //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3324 // mActiveResources.entrySet().iterator();
3325 while (it.hasNext()) {
3326 WeakReference<Resources> v = it.next();
3327 Resources r = v.get();
3328 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003329 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003330 + r + " config to: " + config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003331 r.updateConfiguration(config, dm, compat);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003332 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003333 // + " " + r + ": " + r.getConfiguration());
3334 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003335 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003336 it.remove();
3337 }
3338 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003339
3340 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003341 }
3342
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003343 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003344
3345 ArrayList<ComponentCallbacks> callbacks = null;
3346
3347 synchronized (mPackages) {
3348 if (mPendingConfiguration != null) {
3349 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3350 config = mPendingConfiguration;
3351 }
3352 mPendingConfiguration = null;
3353 }
3354
3355 if (config == null) {
3356 return;
3357 }
3358
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003359 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003360 + config);
3361
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003362 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 if (mConfiguration == null) {
3365 mConfiguration = new Configuration();
3366 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003367 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003368 return;
3369 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 mConfiguration.updateFrom(config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003371 if (compat != null) {
3372 // Can't do this here, because it causes us to report the
3373 // comatible config back to the am as the current config
3374 // of the activity, and much unhappiness results.
3375 //compat.applyToConfiguration(mConfiguration);
3376 }
Bob Leee5408332009-09-04 18:31:17 -07003377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378 callbacks = collectComponentCallbacksLocked(false, config);
3379 }
Bob Leee5408332009-09-04 18:31:17 -07003380
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003381 if (callbacks != null) {
3382 final int N = callbacks.size();
3383 for (int i=0; i<N; i++) {
3384 performConfigurationChanged(callbacks.get(i), config);
3385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 }
3387 }
3388
3389 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003390 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 if (r == null || r.activity == null) {
3392 return;
3393 }
Bob Leee5408332009-09-04 18:31:17 -07003394
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003395 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003396 + r.activityInfo.name);
3397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 performConfigurationChanged(r.activity, mConfiguration);
3399 }
3400
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003401 final void handleProfilerControl(boolean start, ProfilerControlData pcd) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003402 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003403 try {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003404 Debug.startMethodTracing(pcd.path, pcd.fd.getFileDescriptor(),
3405 8 * 1024 * 1024, 0);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003406 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003407 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003408 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003409 } finally {
3410 try {
3411 pcd.fd.close();
3412 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003413 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003414 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003415 }
3416 } else {
3417 Debug.stopMethodTracing();
3418 }
3419 }
Bob Leee5408332009-09-04 18:31:17 -07003420
Andy McFadden824c5102010-07-09 16:26:57 -07003421 final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
3422 if (managed) {
3423 try {
3424 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3425 } catch (IOException e) {
3426 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3427 + " -- can the process access this path?");
3428 } finally {
3429 try {
3430 dhd.fd.close();
3431 } catch (IOException e) {
3432 Slog.w(TAG, "Failure closing profile fd", e);
3433 }
3434 }
3435 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003436 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003437 }
3438 }
3439
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003440 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3441 boolean hasPkgInfo = false;
3442 if (packages != null) {
3443 for (int i=packages.length-1; i>=0; i--) {
3444 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3445 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003446 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003447 ref = mPackages.get(packages[i]);
3448 if (ref != null && ref.get() != null) {
3449 hasPkgInfo = true;
3450 } else {
3451 ref = mResourcePackages.get(packages[i]);
3452 if (ref != null && ref.get() != null) {
3453 hasPkgInfo = true;
3454 }
3455 }
3456 }
3457 mPackages.remove(packages[i]);
3458 mResourcePackages.remove(packages[i]);
3459 }
3460 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003461 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003462 hasPkgInfo);
3463 }
3464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 final void handleLowMemory() {
3466 ArrayList<ComponentCallbacks> callbacks
3467 = new ArrayList<ComponentCallbacks>();
3468
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003469 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 callbacks = collectComponentCallbacksLocked(true, null);
3471 }
Bob Leee5408332009-09-04 18:31:17 -07003472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 final int N = callbacks.size();
3474 for (int i=0; i<N; i++) {
3475 callbacks.get(i).onLowMemory();
3476 }
3477
Chris Tatece229052009-03-25 16:44:52 -07003478 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3479 if (Process.myUid() != Process.SYSTEM_UID) {
3480 int sqliteReleased = SQLiteDatabase.releaseMemory();
3481 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3482 }
Bob Leee5408332009-09-04 18:31:17 -07003483
Mike Reedcaf0df12009-04-27 14:32:05 -04003484 // Ask graphics to free up as much as possible (font/image caches)
3485 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486
3487 BinderInternal.forceGc("mem");
3488 }
3489
3490 private final void handleBindApplication(AppBindData data) {
3491 mBoundApplication = data;
3492 mConfiguration = new Configuration(data.config);
3493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003495 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 android.ddm.DdmHandleAppName.setAppName(data.processName);
3497
3498 /*
3499 * Before spawning a new process, reset the time zone to be the system time zone.
3500 * This needs to be done because the system time zone could have changed after the
3501 * the spawning of this process. Without doing this this process would have the incorrect
3502 * system time zone.
3503 */
3504 TimeZone.setDefault(null);
3505
3506 /*
3507 * Initialize the default locale in this process for the reasons we set the time zone.
3508 */
3509 Locale.setDefault(data.config.locale);
3510
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003511 /*
3512 * Update the system configuration since its preloaded and might not
3513 * reflect configuration changes. The configuration object passed
3514 * in AppBindData can be safely assumed to be up to date
3515 */
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003516 Resources.getSystem().updateConfiguration(mConfiguration,
3517 Resources.getSystem().getDisplayMetrics(), data.compatInfo);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003518
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003519 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003521 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003522 * For system applications on userdebug/eng builds, log stack
3523 * traces of disk and network access to dropbox for analysis.
3524 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07003525 if ((data.appInfo.flags &
3526 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07003527 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
3528 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003529 }
3530
3531 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07003532 * For apps targetting SDK Honeycomb or later, we don't allow
3533 * network usage on the main event loop / UI thread.
3534 *
3535 * Note to those grepping: this is what ultimately throws
3536 * NetworkOnMainThreadException ...
3537 */
3538 if (data.appInfo.targetSdkVersion > 9) {
3539 StrictMode.enableDeathOnNetwork();
3540 }
3541
3542 /**
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003543 * Switch this process to density compatibility mode if needed.
3544 */
3545 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
3546 == 0) {
3547 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3548 }
Bob Leee5408332009-09-04 18:31:17 -07003549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
3551 // XXX should have option to change the port.
3552 Debug.changeDebugPort(8100);
3553 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003554 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 + " is waiting for the debugger on port 8100...");
3556
3557 IActivityManager mgr = ActivityManagerNative.getDefault();
3558 try {
3559 mgr.showWaitingForDebugger(mAppThread, true);
3560 } catch (RemoteException ex) {
3561 }
3562
3563 Debug.waitForDebugger();
3564
3565 try {
3566 mgr.showWaitingForDebugger(mAppThread, false);
3567 } catch (RemoteException ex) {
3568 }
3569
3570 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003571 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 + " can be debugged on port 8100...");
3573 }
3574 }
3575
Robert Greenwalt434203a2010-10-11 16:00:27 -07003576 /**
3577 * Initialize the default http proxy in this process for the reasons we set the time zone.
3578 */
3579 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
3580 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
3581 try {
3582 ProxyProperties proxyProperties = service.getProxy();
3583 Proxy.setHttpProxySystemProperty(proxyProperties);
3584 } catch (RemoteException e) {}
3585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003586 if (data.instrumentationName != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08003587 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 appContext.init(data.info, null, this);
3589 InstrumentationInfo ii = null;
3590 try {
3591 ii = appContext.getPackageManager().
3592 getInstrumentationInfo(data.instrumentationName, 0);
3593 } catch (PackageManager.NameNotFoundException e) {
3594 }
3595 if (ii == null) {
3596 throw new RuntimeException(
3597 "Unable to find instrumentation info for: "
3598 + data.instrumentationName);
3599 }
3600
3601 mInstrumentationAppDir = ii.sourceDir;
3602 mInstrumentationAppPackage = ii.packageName;
3603 mInstrumentedAppDir = data.info.getAppDir();
3604
3605 ApplicationInfo instrApp = new ApplicationInfo();
3606 instrApp.packageName = ii.packageName;
3607 instrApp.sourceDir = ii.sourceDir;
3608 instrApp.publicSourceDir = ii.publicSourceDir;
3609 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07003610 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003611 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08003613 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003614 instrContext.init(pi, null, this);
3615
3616 try {
3617 java.lang.ClassLoader cl = instrContext.getClassLoader();
3618 mInstrumentation = (Instrumentation)
3619 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
3620 } catch (Exception e) {
3621 throw new RuntimeException(
3622 "Unable to instantiate instrumentation "
3623 + data.instrumentationName + ": " + e.toString(), e);
3624 }
3625
3626 mInstrumentation.init(this, instrContext, appContext,
3627 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
3628
3629 if (data.profileFile != null && !ii.handleProfiling) {
3630 data.handlingProfiling = true;
3631 File file = new File(data.profileFile);
3632 file.getParentFile().mkdirs();
3633 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
3634 }
3635
3636 try {
3637 mInstrumentation.onCreate(data.instrumentationArgs);
3638 }
3639 catch (Exception e) {
3640 throw new RuntimeException(
3641 "Exception thrown in onCreate() of "
3642 + data.instrumentationName + ": " + e.toString(), e);
3643 }
3644
3645 } else {
3646 mInstrumentation = new Instrumentation();
3647 }
3648
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003649 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08003650 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003651 }
3652
Christopher Tate181fafa2009-05-14 11:12:14 -07003653 // If the app is being launched for full backup or restore, bring it up in
3654 // a restricted environment with the base application class.
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003655 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003656 mInitialApplication = app;
3657
3658 List<ProviderInfo> providers = data.providers;
3659 if (providers != null) {
3660 installContentProviders(app, providers);
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003661 // For process that contains content providers, we want to
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003662 // ensure that the JIT is enabled "at some point".
3663 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664 }
3665
3666 try {
3667 mInstrumentation.callApplicationOnCreate(app);
3668 } catch (Exception e) {
3669 if (!mInstrumentation.onException(app, e)) {
3670 throw new RuntimeException(
3671 "Unable to create application " + app.getClass().getName()
3672 + ": " + e.toString(), e);
3673 }
3674 }
3675 }
3676
3677 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
3678 IActivityManager am = ActivityManagerNative.getDefault();
3679 if (mBoundApplication.profileFile != null && mBoundApplication.handlingProfiling) {
3680 Debug.stopMethodTracing();
3681 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003682 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 // + ", app thr: " + mAppThread);
3684 try {
3685 am.finishInstrumentation(mAppThread, resultCode, results);
3686 } catch (RemoteException ex) {
3687 }
3688 }
3689
3690 private final void installContentProviders(
3691 Context context, List<ProviderInfo> providers) {
3692 final ArrayList<IActivityManager.ContentProviderHolder> results =
3693 new ArrayList<IActivityManager.ContentProviderHolder>();
3694
3695 Iterator<ProviderInfo> i = providers.iterator();
3696 while (i.hasNext()) {
3697 ProviderInfo cpi = i.next();
3698 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07003699 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 buf.append(cpi.authority);
3701 buf.append(": ");
3702 buf.append(cpi.name);
3703 Log.i(TAG, buf.toString());
3704 IContentProvider cp = installProvider(context, null, cpi, false);
3705 if (cp != null) {
3706 IActivityManager.ContentProviderHolder cph =
3707 new IActivityManager.ContentProviderHolder(cpi);
3708 cph.provider = cp;
3709 results.add(cph);
3710 // Don't ever unload this provider from the process.
3711 synchronized(mProviderMap) {
3712 mProviderRefCountMap.put(cp.asBinder(), new ProviderRefCount(10000));
3713 }
3714 }
3715 }
3716
3717 try {
3718 ActivityManagerNative.getDefault().publishContentProviders(
3719 getApplicationThread(), results);
3720 } catch (RemoteException ex) {
3721 }
3722 }
3723
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003724 private final IContentProvider getExistingProvider(Context context, String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003726 final ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 if (pr != null) {
3728 return pr.mProvider;
3729 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003730 return null;
3731 }
3732 }
3733
3734 private final IContentProvider getProvider(Context context, String name) {
3735 IContentProvider existing = getExistingProvider(context, name);
3736 if (existing != null) {
3737 return existing;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003738 }
3739
3740 IActivityManager.ContentProviderHolder holder = null;
3741 try {
3742 holder = ActivityManagerNative.getDefault().getContentProvider(
3743 getApplicationThread(), name);
3744 } catch (RemoteException ex) {
3745 }
3746 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003747 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 return null;
3749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750
3751 IContentProvider prov = installProvider(context, holder.provider,
3752 holder.info, true);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003753 //Slog.i(TAG, "noReleaseNeeded=" + holder.noReleaseNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003754 if (holder.noReleaseNeeded || holder.provider == null) {
3755 // We are not going to release the provider if it is an external
3756 // provider that doesn't care about being released, or if it is
3757 // a local provider running in this process.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003758 //Slog.i(TAG, "*** NO RELEASE NEEDED");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 synchronized(mProviderMap) {
3760 mProviderRefCountMap.put(prov.asBinder(), new ProviderRefCount(10000));
3761 }
3762 }
3763 return prov;
3764 }
3765
3766 public final IContentProvider acquireProvider(Context c, String name) {
3767 IContentProvider provider = getProvider(c, name);
3768 if(provider == null)
3769 return null;
3770 IBinder jBinder = provider.asBinder();
3771 synchronized(mProviderMap) {
3772 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3773 if(prc == null) {
3774 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
3775 } else {
3776 prc.count++;
3777 } //end else
3778 } //end synchronized
3779 return provider;
3780 }
3781
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003782 public final IContentProvider acquireExistingProvider(Context c, String name) {
3783 IContentProvider provider = getExistingProvider(c, name);
3784 if(provider == null)
3785 return null;
3786 IBinder jBinder = provider.asBinder();
3787 synchronized(mProviderMap) {
3788 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3789 if(prc == null) {
3790 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
3791 } else {
3792 prc.count++;
3793 } //end else
3794 } //end synchronized
3795 return provider;
3796 }
3797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003798 public final boolean releaseProvider(IContentProvider provider) {
3799 if(provider == null) {
3800 return false;
3801 }
3802 IBinder jBinder = provider.asBinder();
3803 synchronized(mProviderMap) {
3804 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3805 if(prc == null) {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003806 if(localLOGV) Slog.v(TAG, "releaseProvider::Weird shouldn't be here");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 return false;
3808 } else {
3809 prc.count--;
3810 if(prc.count == 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003811 // Schedule the actual remove asynchronously, since we
3812 // don't know the context this will be called in.
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003813 // TODO: it would be nice to post a delayed message, so
3814 // if we come back and need the same provider quickly
3815 // we will still have it available.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003816 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, provider);
3817 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003818 } //end if
3819 } //end else
3820 } //end synchronized
3821 return true;
3822 }
3823
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003824 final void completeRemoveProvider(IContentProvider provider) {
3825 IBinder jBinder = provider.asBinder();
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003826 String name = null;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003827 synchronized(mProviderMap) {
3828 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3829 if(prc != null && prc.count == 0) {
3830 mProviderRefCountMap.remove(jBinder);
3831 //invoke removeProvider to dereference provider
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003832 name = removeProviderLocked(provider);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003833 }
3834 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003835
3836 if (name != null) {
3837 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003838 if(localLOGV) Slog.v(TAG, "removeProvider::Invoking " +
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003839 "ActivityManagerNative.removeContentProvider(" + name);
3840 ActivityManagerNative.getDefault().removeContentProvider(
3841 getApplicationThread(), name);
3842 } catch (RemoteException e) {
3843 //do nothing content provider object is dead any way
3844 } //end catch
3845 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003846 }
3847
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003848 public final String removeProviderLocked(IContentProvider provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849 if (provider == null) {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003850 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003851 }
3852 IBinder providerBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003854 String name = null;
3855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003856 // remove the provider from mProviderMap
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003857 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 while (iter.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003859 ProviderClientRecord pr = iter.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 IBinder myBinder = pr.mProvider.asBinder();
3861 if (myBinder == providerBinder) {
3862 //find if its published by this process itself
3863 if(pr.mLocalProvider != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003864 if(localLOGV) Slog.i(TAG, "removeProvider::found local provider returning");
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003865 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003866 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003867 if(localLOGV) Slog.v(TAG, "removeProvider::Not local provider Unlinking " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 "death recipient");
3869 //content provider is in another process
3870 myBinder.unlinkToDeath(pr, 0);
3871 iter.remove();
3872 //invoke remove only once for the very first name seen
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003873 if(name == null) {
3874 name = pr.mName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 }
3876 } //end if myBinder
3877 } //end while iter
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003878
3879 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003880 }
3881
3882 final void removeDeadProvider(String name, IContentProvider provider) {
3883 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003884 ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 if (pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003886 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003887 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003888 if (removed != null) {
3889 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
3890 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003891 }
3892 }
3893 }
3894
3895 final void removeDeadProviderLocked(String name, IContentProvider provider) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003896 ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897 if (pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003898 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003899 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003900 if (removed != null) {
3901 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
3902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003903 }
3904 }
3905
3906 private final IContentProvider installProvider(Context context,
3907 IContentProvider provider, ProviderInfo info, boolean noisy) {
3908 ContentProvider localProvider = null;
3909 if (provider == null) {
3910 if (noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003911 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 + info.name);
3913 }
3914 Context c = null;
3915 ApplicationInfo ai = info.applicationInfo;
3916 if (context.getPackageName().equals(ai.packageName)) {
3917 c = context;
3918 } else if (mInitialApplication != null &&
3919 mInitialApplication.getPackageName().equals(ai.packageName)) {
3920 c = mInitialApplication;
3921 } else {
3922 try {
3923 c = context.createPackageContext(ai.packageName,
3924 Context.CONTEXT_INCLUDE_CODE);
3925 } catch (PackageManager.NameNotFoundException e) {
3926 }
3927 }
3928 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003929 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003930 ai.packageName +
3931 " while loading content provider " +
3932 info.name);
3933 return null;
3934 }
3935 try {
3936 final java.lang.ClassLoader cl = c.getClassLoader();
3937 localProvider = (ContentProvider)cl.
3938 loadClass(info.name).newInstance();
3939 provider = localProvider.getIContentProvider();
3940 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003941 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003942 info.name + " from sourceDir " +
3943 info.applicationInfo.sourceDir);
3944 return null;
3945 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003946 if (Config.LOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 TAG, "Instantiating local provider " + info.name);
3948 // XXX Need to create the correct context for this provider.
3949 localProvider.attachInfo(c, info);
3950 } catch (java.lang.Exception e) {
3951 if (!mInstrumentation.onException(null, e)) {
3952 throw new RuntimeException(
3953 "Unable to get provider " + info.name
3954 + ": " + e.toString(), e);
3955 }
3956 return null;
3957 }
3958 } else if (localLOGV) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003959 Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 + info.name);
3961 }
3962
3963 synchronized (mProviderMap) {
3964 // Cache the pointer for the remote provider.
3965 String names[] = PATTERN_SEMICOLON.split(info.authority);
3966 for (int i=0; i<names.length; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003967 ProviderClientRecord pr = new ProviderClientRecord(names[i], provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003968 localProvider);
3969 try {
3970 provider.asBinder().linkToDeath(pr, 0);
3971 mProviderMap.put(names[i], pr);
3972 } catch (RemoteException e) {
3973 return null;
3974 }
3975 }
3976 if (localProvider != null) {
3977 mLocalProviders.put(provider.asBinder(),
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003978 new ProviderClientRecord(null, provider, localProvider));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 }
3980 }
3981
3982 return provider;
3983 }
3984
3985 private final void attach(boolean system) {
3986 sThreadLocal.set(this);
3987 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003988 if (!system) {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003989 ViewRoot.addFirstDrawHandler(new Runnable() {
3990 public void run() {
3991 ensureJitEnabled();
3992 }
3993 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003994 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
3995 RuntimeInit.setApplicationObject(mAppThread.asBinder());
3996 IActivityManager mgr = ActivityManagerNative.getDefault();
3997 try {
3998 mgr.attachApplication(mAppThread);
3999 } catch (RemoteException ex) {
4000 }
4001 } else {
4002 // Don't set application object here -- if the system crashes,
4003 // we can't display an alert, we just want to die die die.
4004 android.ddm.DdmHandleAppName.setAppName("system_process");
4005 try {
4006 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004007 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004008 context.init(getSystemContext().mPackageInfo, null, this);
4009 Application app = Instrumentation.newApplication(Application.class, context);
4010 mAllApplications.add(app);
4011 mInitialApplication = app;
4012 app.onCreate();
4013 } catch (Exception e) {
4014 throw new RuntimeException(
4015 "Unable to instantiate Application():" + e.toString(), e);
4016 }
4017 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004018
4019 ViewRoot.addConfigCallback(new ComponentCallbacks() {
4020 public void onConfigurationChanged(Configuration newConfig) {
4021 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004022 // We need to apply this change to the resources
4023 // immediately, because upon returning the view
4024 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004025 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004026 // This actually changed the resources! Tell
4027 // everyone about it.
4028 if (mPendingConfiguration == null ||
4029 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4030 mPendingConfiguration = newConfig;
4031
4032 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4033 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004034 }
4035 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004036 }
4037 public void onLowMemory() {
4038 }
4039 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 }
4041
4042 private final void detach()
4043 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004044 sThreadLocal.set(null);
4045 }
4046
4047 public static final ActivityThread systemMain() {
Romain Guy52339202010-09-03 16:04:46 -07004048 HardwareRenderer.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004049 ActivityThread thread = new ActivityThread();
4050 thread.attach(true);
4051 return thread;
4052 }
4053
4054 public final void installSystemProviders(List providers) {
4055 if (providers != null) {
4056 installContentProviders(mInitialApplication,
4057 (List<ProviderInfo>)providers);
4058 }
4059 }
4060
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004061 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004062 synchronized (mPackages) {
4063 if (mCoreSettings != null) {
4064 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004065 } else {
4066 return defaultValue;
4067 }
4068 }
4069 }
4070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004071 public static final void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004072 SamplingProfilerIntegration.start();
4073
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004074 // CloseGuard defaults to true and can be quite spammy. We
4075 // disable it here, but selectively enable it later (via
4076 // StrictMode) on debug builds, but using DropBox, not logs.
4077 CloseGuard.setEnabled(false);
4078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004079 Process.setArgV0("<pre-initialized>");
4080
4081 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07004082 if (sMainThreadHandler == null) {
4083 sMainThreadHandler = new Handler();
4084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085
4086 ActivityThread thread = new ActivityThread();
4087 thread.attach(false);
4088
Dianne Hackborn287952c2010-09-22 22:34:31 -07004089 if (false) {
4090 Looper.myLooper().setMessageLogging(new
4091 LogPrinter(Log.DEBUG, "ActivityThread"));
4092 }
4093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 Looper.loop();
4095
4096 if (Process.supportsProcesses()) {
4097 throw new RuntimeException("Main thread loop unexpectedly exited");
4098 }
4099
4100 thread.detach();
Bob Leeeec2f412009-09-10 11:01:24 +02004101 String name = (thread.mInitialApplication != null)
4102 ? thread.mInitialApplication.getPackageName()
4103 : "<unknown>";
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004104 Slog.i(TAG, "Main thread of " + name + " is now exiting");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105 }
4106}