blob: 6c63c2ae3ac501a238a125cb1d085700b14b059b [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;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700158 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700159 Configuration mResConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700160 CompatibilityInfo mResCompatibilityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700161 Application mInitialApplication;
162 final ArrayList<Application> mAllApplications
163 = new ArrayList<Application>();
164 // set of instantiated backup agents, keyed by package name
165 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700166 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700167 Instrumentation mInstrumentation;
168 String mInstrumentationAppDir = null;
169 String mInstrumentationAppPackage = null;
170 String mInstrumentedAppDir = null;
171 boolean mSystemThread = false;
172 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700174 // These can be accessed by multiple threads; mPackages is the lock.
175 // XXX For now we keep around information about all packages we have
176 // seen, not removing entries from this map.
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700177 // NOTE: The activity manager in its process needs to call in to
178 // ActivityThread to do things like update resource configurations,
179 // which means this lock gets held while the activity manager holds its
180 // own lock. Thus you MUST NEVER call back into the activity manager
181 // or anything that depends on it while holding this lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700182 final HashMap<String, WeakReference<LoadedApk>> mPackages
183 = new HashMap<String, WeakReference<LoadedApk>>();
184 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
185 = new HashMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700186 final HashMap<CompatibilityInfo, DisplayMetrics> mDisplayMetrics
187 = new HashMap<CompatibilityInfo, DisplayMetrics>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700188 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
189 = new HashMap<ResourcesKey, WeakReference<Resources> >();
190 final ArrayList<ActivityClientRecord> mRelaunchingActivities
191 = new ArrayList<ActivityClientRecord>();
192 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700194 // The lock of mProviderMap protects the following variables.
195 final HashMap<String, ProviderClientRecord> mProviderMap
196 = new HashMap<String, ProviderClientRecord>();
197 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
198 = new HashMap<IBinder, ProviderRefCount>();
199 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
200 = new HashMap<IBinder, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201
Jeff Hamilton52d32032011-01-08 15:31:26 -0600202 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
203 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
204
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700205 final GcIdler mGcIdler = new GcIdler();
206 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700208 static Handler sMainThreadHandler; // set once in main()
209
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800210 Bundle mCoreSettings = null;
211
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400212 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700214 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 Intent intent;
216 Bundle state;
217 Activity activity;
218 Window window;
219 Activity parent;
220 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700221 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 boolean paused;
223 boolean stopped;
224 boolean hideForNow;
225 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700226 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700227 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228
229 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400230 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700231 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232
233 List<ResultInfo> pendingResults;
234 List<Intent> pendingIntents;
235
236 boolean startsNotResumed;
237 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800238 int pendingConfigChanges;
239 boolean onlyLocalRequest;
240
241 View mPendingRemoveWindow;
242 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700244 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 parent = null;
246 embeddedID = null;
247 paused = false;
248 stopped = false;
249 hideForNow = false;
250 nextIdle = null;
251 }
252
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800253 public boolean isPreHoneycomb() {
254 if (activity != null) {
255 return activity.getApplicationInfo().targetSdkVersion
256 < android.os.Build.VERSION_CODES.HONEYCOMB;
257 }
258 return false;
259 }
260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 public String toString() {
262 ComponentName componentName = intent.getComponent();
263 return "ActivityRecord{"
264 + Integer.toHexString(System.identityHashCode(this))
265 + " token=" + token + " " + (componentName == null
266 ? "no component name" : componentName.toShortString())
267 + "}";
268 }
269 }
270
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400271 final class ProviderClientRecord implements IBinder.DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 final String mName;
273 final IContentProvider mProvider;
274 final ContentProvider mLocalProvider;
275
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700276 ProviderClientRecord(String name, IContentProvider provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 ContentProvider localProvider) {
278 mName = name;
279 mProvider = provider;
280 mLocalProvider = localProvider;
281 }
282
283 public void binderDied() {
284 removeDeadProvider(mName, mProvider);
285 }
286 }
287
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400288 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 List<Intent> intents;
290 IBinder token;
291 public String toString() {
292 return "NewIntentData{intents=" + intents + " token=" + token + "}";
293 }
294 }
295
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400296 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700297 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
298 boolean ordered, boolean sticky, IBinder token) {
299 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky, token);
300 this.intent = intent;
301 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 Intent intent;
304 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400305 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 public String toString() {
307 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700308 info.packageName + " resultCode=" + getResultCode()
309 + " resultData=" + getResultData() + " resultExtras="
310 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 }
312 }
313
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400314 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700315 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400316 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700317 int backupMode;
318 public String toString() {
319 return "CreateBackupAgentData{appInfo=" + appInfo
320 + " backupAgent=" + appInfo.backupAgentName
321 + " mode=" + backupMode + "}";
322 }
323 }
Bob Leee5408332009-09-04 18:31:17 -0700324
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400325 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 IBinder token;
327 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400328 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 Intent intent;
330 public String toString() {
331 return "CreateServiceData{token=" + token + " className="
332 + info.name + " packageName=" + info.packageName
333 + " intent=" + intent + "}";
334 }
335 }
336
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400337 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 IBinder token;
339 Intent intent;
340 boolean rebind;
341 public String toString() {
342 return "BindServiceData{token=" + token + " intent=" + intent + "}";
343 }
344 }
345
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400346 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 IBinder token;
348 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700349 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 Intent args;
351 public String toString() {
352 return "ServiceArgsData{token=" + token + " startId=" + startId
353 + " args=" + args + "}";
354 }
355 }
356
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400357 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700358 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 String processName;
360 ApplicationInfo appInfo;
361 List<ProviderInfo> providers;
362 ComponentName instrumentationName;
363 String profileFile;
364 Bundle instrumentationArgs;
365 IInstrumentationWatcher instrumentationWatcher;
366 int debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700367 boolean restrictedBackupMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400369 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 boolean handlingProfiling;
371 public String toString() {
372 return "AppBindData{appInfo=" + appInfo + "}";
373 }
374 }
375
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400376 static final class DumpComponentInfo {
Dianne Hackborn9a849832011-04-07 15:11:57 -0700377 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700378 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800379 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 }
382
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400383 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 IBinder token;
385 List<ResultInfo> results;
386 public String toString() {
387 return "ResultData{token=" + token + " results" + results + "}";
388 }
389 }
390
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400391 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800392 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 String what;
394 String who;
395 }
396
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400397 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700398 String path;
399 ParcelFileDescriptor fd;
400 }
401
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400402 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700403 String path;
404 ParcelFileDescriptor fd;
405 }
406
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400407 static final class UpdateCompatibilityData {
408 String pkg;
409 CompatibilityInfo info;
410 }
411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 private final class ApplicationThread extends ApplicationThreadNative {
413 private static final String HEAP_COLUMN = "%17s %8s %8s %8s %8s";
414 private static final String ONE_COUNT_COLUMN = "%17s %8d";
415 private static final String TWO_COUNT_COLUMNS = "%17s %8d %17s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700416 private static final String TWO_COUNT_COLUMNS_DB = "%20s %8d %20s %8d";
417 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 // Formatting for checkin service - update version if row format changes
420 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 public final void schedulePauseActivity(IBinder token, boolean finished,
423 boolean userLeaving, int configChanges) {
424 queueOrSendMessage(
425 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
426 token,
427 (userLeaving ? 1 : 0),
428 configChanges);
429 }
430
431 public final void scheduleStopActivity(IBinder token, boolean showWindow,
432 int configChanges) {
433 queueOrSendMessage(
434 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
435 token, 0, configChanges);
436 }
437
438 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
439 queueOrSendMessage(
440 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
441 token);
442 }
443
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800444 public final void scheduleSleeping(IBinder token, boolean sleeping) {
445 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
446 }
447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
449 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
450 }
451
452 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
453 ResultData res = new ResultData();
454 res.token = token;
455 res.results = results;
456 queueOrSendMessage(H.SEND_RESULT, res);
457 }
458
459 // we use token to identify this activity without having to send the
460 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700461 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400462 ActivityInfo info, CompatibilityInfo compatInfo, Bundle state,
463 List<ResultInfo> pendingResults,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700465 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466
467 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700468 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 r.intent = intent;
470 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400471 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 r.state = state;
473
474 r.pendingResults = pendingResults;
475 r.pendingIntents = pendingNewIntents;
476
477 r.startsNotResumed = notResumed;
478 r.isForward = isForward;
479
480 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
481 }
482
483 public final void scheduleRelaunchActivity(IBinder token,
484 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800485 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800486 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
487 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 }
489
490 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
491 NewIntentData data = new NewIntentData();
492 data.intents = intents;
493 data.token = token;
494
495 queueOrSendMessage(H.NEW_INTENT, data);
496 }
497
498 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
499 int configChanges) {
500 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
501 configChanges);
502 }
503
504 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400505 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
506 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700507 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
508 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400510 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 queueOrSendMessage(H.RECEIVER, r);
512 }
513
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400514 public final void scheduleCreateBackupAgent(ApplicationInfo app,
515 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700516 CreateBackupAgentData d = new CreateBackupAgentData();
517 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400518 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700519 d.backupMode = backupMode;
520
521 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
522 }
523
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400524 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
525 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700526 CreateBackupAgentData d = new CreateBackupAgentData();
527 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400528 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700529
530 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
531 }
532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400534 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 CreateServiceData s = new CreateServiceData();
536 s.token = token;
537 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400538 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539
540 queueOrSendMessage(H.CREATE_SERVICE, s);
541 }
542
543 public final void scheduleBindService(IBinder token, Intent intent,
544 boolean rebind) {
545 BindServiceData s = new BindServiceData();
546 s.token = token;
547 s.intent = intent;
548 s.rebind = rebind;
549
550 queueOrSendMessage(H.BIND_SERVICE, s);
551 }
552
553 public final void scheduleUnbindService(IBinder token, Intent intent) {
554 BindServiceData s = new BindServiceData();
555 s.token = token;
556 s.intent = intent;
557
558 queueOrSendMessage(H.UNBIND_SERVICE, s);
559 }
560
561 public final void scheduleServiceArgs(IBinder token, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700562 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 ServiceArgsData s = new ServiceArgsData();
564 s.token = token;
565 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700566 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 s.args = args;
568
569 queueOrSendMessage(H.SERVICE_ARGS, s);
570 }
571
572 public final void scheduleStopService(IBinder token) {
573 queueOrSendMessage(H.STOP_SERVICE, token);
574 }
575
576 public final void bindApplication(String processName,
577 ApplicationInfo appInfo, List<ProviderInfo> providers,
578 ComponentName instrumentationName, String profileFile,
579 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Christopher Tate181fafa2009-05-14 11:12:14 -0700580 int debugMode, boolean isRestrictedBackupMode, Configuration config,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400581 CompatibilityInfo compatInfo, Map<String, IBinder> services,
582 Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583
584 if (services != null) {
585 // Setup the service cache in the ServiceManager
586 ServiceManager.initServiceCache(services);
587 }
588
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800589 setCoreSettings(coreSettings);
590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 AppBindData data = new AppBindData();
592 data.processName = processName;
593 data.appInfo = appInfo;
594 data.providers = providers;
595 data.instrumentationName = instrumentationName;
596 data.profileFile = profileFile;
597 data.instrumentationArgs = instrumentationArgs;
598 data.instrumentationWatcher = instrumentationWatcher;
599 data.debugMode = debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700600 data.restrictedBackupMode = isRestrictedBackupMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400602 data.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 queueOrSendMessage(H.BIND_APPLICATION, data);
604 }
605
606 public final void scheduleExit() {
607 queueOrSendMessage(H.EXIT_APPLICATION, null);
608 }
609
Christopher Tate5e1ab332009-09-01 20:32:49 -0700610 public final void scheduleSuicide() {
611 queueOrSendMessage(H.SUICIDE, null);
612 }
613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 public void requestThumbnail(IBinder token) {
615 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
616 }
617
618 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800619 synchronized (mPackages) {
620 if (mPendingConfiguration == null ||
621 mPendingConfiguration.isOtherSeqNewer(config)) {
622 mPendingConfiguration = config;
623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 }
625 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
626 }
627
628 public void updateTimeZone() {
629 TimeZone.setDefault(null);
630 }
631
Robert Greenwalt03595d02010-11-02 14:08:23 -0700632 public void clearDnsCache() {
633 // a non-standard API to get this to libcore
634 InetAddress.clearDnsCache();
635 }
636
Robert Greenwalt434203a2010-10-11 16:00:27 -0700637 public void setHttpProxy(String host, String port, String exclList) {
638 Proxy.setHttpProxySystemProperty(host, port, exclList);
639 }
640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 public void processInBackground() {
642 mH.removeMessages(H.GC_WHEN_IDLE);
643 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
644 }
645
646 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700647 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborn9a849832011-04-07 15:11:57 -0700648 try {
649 data.fd = ParcelFileDescriptor.dup(fd);
650 data.token = servicetoken;
651 data.args = args;
652 queueOrSendMessage(H.DUMP_SERVICE, data);
653 } catch (IOException e) {
654 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 }
656 }
657
658 // This function exists to make sure all receiver dispatching is
659 // correctly ordered, since these are one-way calls and the binder driver
660 // applies transaction ordering per object for such calls.
661 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700662 int resultCode, String dataStr, Bundle extras, boolean ordered,
663 boolean sticky) throws RemoteException {
664 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 }
Bob Leee5408332009-09-04 18:31:17 -0700666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 public void scheduleLowMemory() {
668 queueOrSendMessage(H.LOW_MEMORY, null);
669 }
670
671 public void scheduleActivityConfigurationChanged(IBinder token) {
672 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
673 }
674
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700675 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd) {
676 ProfilerControlData pcd = new ProfilerControlData();
677 pcd.path = path;
678 pcd.fd = fd;
679 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800680 }
681
Andy McFadden824c5102010-07-09 16:26:57 -0700682 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
683 DumpHeapData dhd = new DumpHeapData();
684 dhd.path = path;
685 dhd.fd = fd;
686 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
687 }
688
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700689 public void setSchedulingGroup(int group) {
690 // Note: do this immediately, since going into the foreground
691 // should happen regardless of what pending work we have to do
692 // and the activity manager will wait for us to report back that
693 // we are done before sending us to the background.
694 try {
695 Process.setProcessGroup(Process.myPid(), group);
696 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800697 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700698 }
699 }
Bob Leee5408332009-09-04 18:31:17 -0700700
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700701 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
702 Debug.getMemoryInfo(outInfo);
703 }
Bob Leee5408332009-09-04 18:31:17 -0700704
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700705 public void dispatchPackageBroadcast(int cmd, String[] packages) {
706 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
707 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700708
709 public void scheduleCrash(String msg) {
710 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
711 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700712
Dianne Hackborn30d71892010-12-11 10:37:55 -0800713 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
714 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700715 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborn9a849832011-04-07 15:11:57 -0700716 try {
717 data.fd = ParcelFileDescriptor.dup(fd);
718 data.token = activitytoken;
719 data.prefix = prefix;
720 data.args = args;
721 queueOrSendMessage(H.DUMP_ACTIVITY, data);
722 } catch (IOException e) {
723 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700724 }
725 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 @Override
728 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
729 long nativeMax = Debug.getNativeHeapSize() / 1024;
730 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
731 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
732
733 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
734 Debug.getMemoryInfo(memInfo);
735
736 final int nativeShared = memInfo.nativeSharedDirty;
737 final int dalvikShared = memInfo.dalvikSharedDirty;
738 final int otherShared = memInfo.otherSharedDirty;
739
740 final int nativePrivate = memInfo.nativePrivateDirty;
741 final int dalvikPrivate = memInfo.dalvikPrivateDirty;
742 final int otherPrivate = memInfo.otherPrivateDirty;
743
744 Runtime runtime = Runtime.getRuntime();
745
746 long dalvikMax = runtime.totalMemory() / 1024;
747 long dalvikFree = runtime.freeMemory() / 1024;
748 long dalvikAllocated = dalvikMax - dalvikFree;
749 long viewInstanceCount = ViewDebug.getViewInstanceCount();
750 long viewRootInstanceCount = ViewDebug.getViewRootInstanceCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700751 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
752 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 int globalAssetCount = AssetManager.getGlobalAssetCount();
754 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
755 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
756 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
757 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700758 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 long sqliteAllocated = SQLiteDebug.getHeapAllocatedSize() / 1024;
Vasu Noric3849202010-03-09 10:47:25 -0800760 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 // Check to see if we were called by checkin server. If so, print terse format.
763 boolean doCheckinFormat = false;
764 if (args != null) {
765 for (String arg : args) {
766 if ("-c".equals(arg)) doCheckinFormat = true;
767 }
768 }
Bob Leee5408332009-09-04 18:31:17 -0700769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 // For checkin, we print one long comma-separated list of values
771 if (doCheckinFormat) {
772 // NOTE: if you change anything significant below, also consider changing
773 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700774 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 // Header
778 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
779 pw.print(Process.myPid()); pw.print(',');
780 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 // Heap info - max
783 pw.print(nativeMax); pw.print(',');
784 pw.print(dalvikMax); pw.print(',');
785 pw.print("N/A,");
786 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 // Heap info - allocated
789 pw.print(nativeAllocated); pw.print(',');
790 pw.print(dalvikAllocated); pw.print(',');
791 pw.print("N/A,");
792 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 // Heap info - free
795 pw.print(nativeFree); pw.print(',');
796 pw.print(dalvikFree); pw.print(',');
797 pw.print("N/A,");
798 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 // Heap info - proportional set size
801 pw.print(memInfo.nativePss); pw.print(',');
802 pw.print(memInfo.dalvikPss); pw.print(',');
803 pw.print(memInfo.otherPss); pw.print(',');
804 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 // Heap info - shared
Bob Leee5408332009-09-04 18:31:17 -0700807 pw.print(nativeShared); pw.print(',');
808 pw.print(dalvikShared); pw.print(',');
809 pw.print(otherShared); pw.print(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 pw.print(nativeShared + dalvikShared + otherShared); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 // Heap info - private
Bob Leee5408332009-09-04 18:31:17 -0700813 pw.print(nativePrivate); pw.print(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 pw.print(dalvikPrivate); pw.print(',');
815 pw.print(otherPrivate); pw.print(',');
816 pw.print(nativePrivate + dalvikPrivate + otherPrivate); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 // Object counts
819 pw.print(viewInstanceCount); pw.print(',');
820 pw.print(viewRootInstanceCount); pw.print(',');
821 pw.print(appContextInstanceCount); pw.print(',');
822 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 pw.print(globalAssetCount); pw.print(',');
825 pw.print(globalAssetManagerCount); pw.print(',');
826 pw.print(binderLocalObjectCount); pw.print(',');
827 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 pw.print(binderDeathObjectCount); pw.print(',');
830 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 // SQL
833 pw.print(sqliteAllocated); pw.print(',');
Vasu Noric3849202010-03-09 10:47:25 -0800834 pw.print(stats.memoryUsed / 1024); pw.print(',');
835 pw.print(stats.pageCacheOverflo / 1024); pw.print(',');
836 pw.print(stats.largestMemAlloc / 1024); pw.print(',');
837 for (int i = 0; i < stats.dbStats.size(); i++) {
838 DbStats dbStats = stats.dbStats.get(i);
839 printRow(pw, DB_INFO_FORMAT, dbStats.pageSize, dbStats.dbSize,
Vasu Nori90a367262010-04-12 12:49:09 -0700840 dbStats.lookaside, dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -0800841 pw.print(',');
842 }
Bob Leee5408332009-09-04 18:31:17 -0700843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 return;
845 }
Bob Leee5408332009-09-04 18:31:17 -0700846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 // otherwise, show human-readable format
848 printRow(pw, HEAP_COLUMN, "", "native", "dalvik", "other", "total");
849 printRow(pw, HEAP_COLUMN, "size:", nativeMax, dalvikMax, "N/A", nativeMax + dalvikMax);
850 printRow(pw, HEAP_COLUMN, "allocated:", nativeAllocated, dalvikAllocated, "N/A",
851 nativeAllocated + dalvikAllocated);
852 printRow(pw, HEAP_COLUMN, "free:", nativeFree, dalvikFree, "N/A",
853 nativeFree + dalvikFree);
854
855 printRow(pw, HEAP_COLUMN, "(Pss):", memInfo.nativePss, memInfo.dalvikPss,
856 memInfo.otherPss, memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss);
857
858 printRow(pw, HEAP_COLUMN, "(shared dirty):", nativeShared, dalvikShared, otherShared,
859 nativeShared + dalvikShared + otherShared);
860 printRow(pw, HEAP_COLUMN, "(priv dirty):", nativePrivate, dalvikPrivate, otherPrivate,
861 nativePrivate + dalvikPrivate + otherPrivate);
862
863 pw.println(" ");
864 pw.println(" Objects");
865 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRoots:",
866 viewRootInstanceCount);
867
868 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
869 "Activities:", activityInstanceCount);
870
871 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
872 "AssetManagers:", globalAssetManagerCount);
873
874 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
875 "Proxy Binders:", binderProxyObjectCount);
876 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
877
878 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -0700879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 // SQLite mem info
881 pw.println(" ");
882 pw.println(" SQL");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700883 printRow(pw, TWO_COUNT_COLUMNS_DB, "heap:", sqliteAllocated, "MEMORY_USED:",
Vasu Noric3849202010-03-09 10:47:25 -0800884 stats.memoryUsed / 1024);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700885 printRow(pw, TWO_COUNT_COLUMNS_DB, "PAGECACHE_OVERFLOW:",
886 stats.pageCacheOverflo / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800887 pw.println(" ");
888 int N = stats.dbStats.size();
889 if (N > 0) {
890 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700891 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
892 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -0800893 for (int i = 0; i < N; i++) {
894 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700895 printRow(pw, DB_INFO_FORMAT,
896 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
897 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
898 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
899 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -0800900 }
901 }
Bob Leee5408332009-09-04 18:31:17 -0700902
Dianne Hackborn82e1ee92009-08-11 18:56:41 -0700903 // Asset details.
904 String assetAlloc = AssetManager.getAssetAllocations();
905 if (assetAlloc != null) {
906 pw.println(" ");
907 pw.println(" Asset Allocations");
908 pw.print(assetAlloc);
909 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 }
911
912 private void printRow(PrintWriter pw, String format, Object...objs) {
913 pw.println(String.format(format, objs));
914 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800915
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800916 public void setCoreSettings(Bundle coreSettings) {
917 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800918 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400919
920 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
921 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
922 ucd.pkg = pkg;
923 ucd.info = info;
924 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 }
927
928 private final class H extends Handler {
929 public static final int LAUNCH_ACTIVITY = 100;
930 public static final int PAUSE_ACTIVITY = 101;
931 public static final int PAUSE_ACTIVITY_FINISHING= 102;
932 public static final int STOP_ACTIVITY_SHOW = 103;
933 public static final int STOP_ACTIVITY_HIDE = 104;
934 public static final int SHOW_WINDOW = 105;
935 public static final int HIDE_WINDOW = 106;
936 public static final int RESUME_ACTIVITY = 107;
937 public static final int SEND_RESULT = 108;
938 public static final int DESTROY_ACTIVITY = 109;
939 public static final int BIND_APPLICATION = 110;
940 public static final int EXIT_APPLICATION = 111;
941 public static final int NEW_INTENT = 112;
942 public static final int RECEIVER = 113;
943 public static final int CREATE_SERVICE = 114;
944 public static final int SERVICE_ARGS = 115;
945 public static final int STOP_SERVICE = 116;
946 public static final int REQUEST_THUMBNAIL = 117;
947 public static final int CONFIGURATION_CHANGED = 118;
948 public static final int CLEAN_UP_CONTEXT = 119;
949 public static final int GC_WHEN_IDLE = 120;
950 public static final int BIND_SERVICE = 121;
951 public static final int UNBIND_SERVICE = 122;
952 public static final int DUMP_SERVICE = 123;
953 public static final int LOW_MEMORY = 124;
954 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
955 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800956 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -0700957 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -0700958 public static final int DESTROY_BACKUP_AGENT = 129;
959 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700960 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800961 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700962 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700963 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -0700964 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700965 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800966 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800967 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400968 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -0700970 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 switch (code) {
972 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
973 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
974 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
975 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
976 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
977 case SHOW_WINDOW: return "SHOW_WINDOW";
978 case HIDE_WINDOW: return "HIDE_WINDOW";
979 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
980 case SEND_RESULT: return "SEND_RESULT";
981 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
982 case BIND_APPLICATION: return "BIND_APPLICATION";
983 case EXIT_APPLICATION: return "EXIT_APPLICATION";
984 case NEW_INTENT: return "NEW_INTENT";
985 case RECEIVER: return "RECEIVER";
986 case CREATE_SERVICE: return "CREATE_SERVICE";
987 case SERVICE_ARGS: return "SERVICE_ARGS";
988 case STOP_SERVICE: return "STOP_SERVICE";
989 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
990 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
991 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
992 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
993 case BIND_SERVICE: return "BIND_SERVICE";
994 case UNBIND_SERVICE: return "UNBIND_SERVICE";
995 case DUMP_SERVICE: return "DUMP_SERVICE";
996 case LOW_MEMORY: return "LOW_MEMORY";
997 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
998 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800999 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001000 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1001 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001002 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001003 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001004 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001005 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001006 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001007 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001008 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001009 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001010 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001011 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 }
1013 }
1014 return "(unknown)";
1015 }
1016 public void handleMessage(Message msg) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001017 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 switch (msg.what) {
1019 case LAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001020 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021
1022 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001023 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001024 handleLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 } break;
1026 case RELAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001027 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001028 handleRelaunchActivity(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 } break;
1030 case PAUSE_ACTIVITY:
1031 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001032 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 break;
1034 case PAUSE_ACTIVITY_FINISHING:
1035 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
1036 break;
1037 case STOP_ACTIVITY_SHOW:
1038 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
1039 break;
1040 case STOP_ACTIVITY_HIDE:
1041 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
1042 break;
1043 case SHOW_WINDOW:
1044 handleWindowVisibility((IBinder)msg.obj, true);
1045 break;
1046 case HIDE_WINDOW:
1047 handleWindowVisibility((IBinder)msg.obj, false);
1048 break;
1049 case RESUME_ACTIVITY:
1050 handleResumeActivity((IBinder)msg.obj, true,
1051 msg.arg1 != 0);
1052 break;
1053 case SEND_RESULT:
1054 handleSendResult((ResultData)msg.obj);
1055 break;
1056 case DESTROY_ACTIVITY:
1057 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1058 msg.arg2, false);
1059 break;
1060 case BIND_APPLICATION:
1061 AppBindData data = (AppBindData)msg.obj;
1062 handleBindApplication(data);
1063 break;
1064 case EXIT_APPLICATION:
1065 if (mInitialApplication != null) {
1066 mInitialApplication.onTerminate();
1067 }
1068 Looper.myLooper().quit();
1069 break;
1070 case NEW_INTENT:
1071 handleNewIntent((NewIntentData)msg.obj);
1072 break;
1073 case RECEIVER:
1074 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001075 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 break;
1077 case CREATE_SERVICE:
1078 handleCreateService((CreateServiceData)msg.obj);
1079 break;
1080 case BIND_SERVICE:
1081 handleBindService((BindServiceData)msg.obj);
1082 break;
1083 case UNBIND_SERVICE:
1084 handleUnbindService((BindServiceData)msg.obj);
1085 break;
1086 case SERVICE_ARGS:
1087 handleServiceArgs((ServiceArgsData)msg.obj);
1088 break;
1089 case STOP_SERVICE:
1090 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001091 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 break;
1093 case REQUEST_THUMBNAIL:
1094 handleRequestThumbnail((IBinder)msg.obj);
1095 break;
1096 case CONFIGURATION_CHANGED:
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001097 handleConfigurationChanged((Configuration)msg.obj, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 break;
1099 case CLEAN_UP_CONTEXT:
1100 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1101 cci.context.performFinalCleanup(cci.who, cci.what);
1102 break;
1103 case GC_WHEN_IDLE:
1104 scheduleGcIdler();
1105 break;
1106 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001107 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 break;
1109 case LOW_MEMORY:
1110 handleLowMemory();
1111 break;
1112 case ACTIVITY_CONFIGURATION_CHANGED:
1113 handleActivityConfigurationChanged((IBinder)msg.obj);
1114 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001115 case PROFILER_CONTROL:
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001116 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001117 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001118 case CREATE_BACKUP_AGENT:
1119 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
1120 break;
1121 case DESTROY_BACKUP_AGENT:
1122 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
1123 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001124 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001125 Process.killProcess(Process.myPid());
1126 break;
1127 case REMOVE_PROVIDER:
1128 completeRemoveProvider((IContentProvider)msg.obj);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001129 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001130 case ENABLE_JIT:
1131 ensureJitEnabled();
1132 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001133 case DISPATCH_PACKAGE_BROADCAST:
1134 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
1135 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001136 case SCHEDULE_CRASH:
1137 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001138 case DUMP_HEAP:
1139 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1140 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001141 case DUMP_ACTIVITY:
1142 handleDumpActivity((DumpComponentInfo)msg.obj);
1143 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001144 case SLEEPING:
1145 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
1146 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001147 case SET_CORE_SETTINGS:
1148 handleSetCoreSettings((Bundle) msg.obj);
1149 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001150 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1151 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07001153 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 }
Bob Leee5408332009-09-04 18:31:17 -07001155
1156 void maybeSnapshot() {
1157 if (mBoundApplication != null) {
Sen Hubde75702010-05-28 01:54:03 -07001158 // convert the *private* ActivityThread.PackageInfo to *public* known
1159 // android.content.pm.PackageInfo
1160 String packageName = mBoundApplication.info.mPackageName;
1161 android.content.pm.PackageInfo packageInfo = null;
1162 try {
1163 Context context = getSystemContext();
1164 if(context == null) {
1165 Log.e(TAG, "cannot get a valid context");
1166 return;
1167 }
1168 PackageManager pm = context.getPackageManager();
1169 if(pm == null) {
1170 Log.e(TAG, "cannot get a valid PackageManager");
1171 return;
1172 }
1173 packageInfo = pm.getPackageInfo(
1174 packageName, PackageManager.GET_ACTIVITIES);
1175 } catch (NameNotFoundException e) {
1176 Log.e(TAG, "cannot get package info for " + packageName, e);
1177 }
1178 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001179 }
1180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 }
1182
1183 private final class Idler implements MessageQueue.IdleHandler {
1184 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001185 ActivityClientRecord a = mNewActivities;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 if (a != null) {
1187 mNewActivities = null;
1188 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001189 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001191 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 TAG, "Reporting idle of " + a +
1193 " finished=" +
1194 (a.activity != null ? a.activity.mFinished : false));
1195 if (a.activity != null && !a.activity.mFinished) {
1196 try {
Dianne Hackborne88846e2009-09-30 21:34:25 -07001197 am.activityIdle(a.token, a.createdConfig);
1198 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 } catch (RemoteException ex) {
1200 }
1201 }
1202 prev = a;
1203 a = a.nextIdle;
1204 prev.nextIdle = null;
1205 } while (a != null);
1206 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001207 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 return false;
1209 }
1210 }
1211
1212 final class GcIdler implements MessageQueue.IdleHandler {
1213 public final boolean queueIdle() {
1214 doGcIfNeeded();
1215 return false;
1216 }
1217 }
1218
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001219 private final static class ResourcesKey {
1220 final private String mResDir;
1221 final private float mScale;
1222 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001223
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001224 ResourcesKey(String resDir, float scale) {
1225 mResDir = resDir;
1226 mScale = scale;
1227 mHash = mResDir.hashCode() << 2 + (int) (mScale * 2);
1228 }
Bob Leee5408332009-09-04 18:31:17 -07001229
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001230 @Override
1231 public int hashCode() {
1232 return mHash;
1233 }
1234
1235 @Override
1236 public boolean equals(Object obj) {
1237 if (!(obj instanceof ResourcesKey)) {
1238 return false;
1239 }
1240 ResourcesKey peer = (ResourcesKey) obj;
1241 return mResDir.equals(peer.mResDir) && mScale == peer.mScale;
1242 }
1243 }
1244
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001245 public static final ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001246 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001249 public static final String currentPackageName() {
1250 ActivityThread am = currentActivityThread();
1251 return (am != null && am.mBoundApplication != null)
1252 ? am.mBoundApplication.processName : null;
1253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001255 public static final Application currentApplication() {
1256 ActivityThread am = currentActivityThread();
1257 return am != null ? am.mInitialApplication : null;
1258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001260 public static IPackageManager getPackageManager() {
1261 if (sPackageManager != null) {
1262 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1263 return sPackageManager;
1264 }
1265 IBinder b = ServiceManager.getService("package");
1266 //Slog.v("PackageManager", "default service binder = " + b);
1267 sPackageManager = IPackageManager.Stub.asInterface(b);
1268 //Slog.v("PackageManager", "default service = " + sPackageManager);
1269 return sPackageManager;
1270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001272 DisplayMetrics getDisplayMetricsLocked(CompatibilityInfo ci, boolean forceUpdate) {
1273 DisplayMetrics dm = mDisplayMetrics.get(ci);
1274 if (dm != null && !forceUpdate) {
1275 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001276 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001277 if (dm == null) {
1278 dm = new DisplayMetrics();
1279 mDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001280 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001281 Display d = WindowManagerImpl.getDefault(ci).getDefaultDisplay();
1282 d.getMetrics(dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001283 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1284 // + metrics.heightPixels + " den=" + metrics.density
1285 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001286 return dm;
1287 }
1288
1289 static Configuration applyConfigCompat(Configuration config, CompatibilityInfo compat) {
1290 if (config == null) {
1291 return null;
1292 }
1293 if (compat != null && !compat.supportsScreen()) {
1294 config = new Configuration(config);
1295 compat.applyToConfiguration(config);
1296 }
1297 return config;
1298 }
1299
1300 private final Configuration mMainThreadConfig = new Configuration();
1301 Configuration applyConfigCompatMainThread(Configuration config, CompatibilityInfo compat) {
1302 if (config == null) {
1303 return null;
1304 }
1305 if (compat != null && !compat.supportsScreen()) {
1306 mMainThreadConfig.setTo(config);
1307 config = mMainThreadConfig;
1308 compat.applyToConfiguration(config);
1309 }
1310 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001313 /**
1314 * Creates the top level Resources for applications with the given compatibility info.
1315 *
1316 * @param resDir the resource directory.
1317 * @param compInfo the compability info. It will use the default compatibility info when it's
1318 * null.
1319 */
1320 Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) {
1321 ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale);
1322 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001324 // Resources is app scale dependent.
1325 if (false) {
1326 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1327 + compInfo.applicationScale);
1328 }
1329 WeakReference<Resources> wr = mActiveResources.get(key);
1330 r = wr != null ? wr.get() : null;
1331 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1332 if (r != null && r.getAssets().isUpToDate()) {
1333 if (false) {
1334 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1335 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1336 }
1337 return r;
1338 }
1339 }
1340
1341 //if (r != null) {
1342 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1343 // + r + " " + resDir);
1344 //}
1345
1346 AssetManager assets = new AssetManager();
1347 if (assets.addAssetPath(resDir) == 0) {
1348 return null;
1349 }
1350
1351 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001352 DisplayMetrics metrics = getDisplayMetricsLocked(compInfo, false);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001353 r = new Resources(assets, metrics, getConfiguration(), compInfo);
1354 if (false) {
1355 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1356 + r.getConfiguration() + " appScale="
1357 + r.getCompatibilityInfo().applicationScale);
1358 }
1359
1360 synchronized (mPackages) {
1361 WeakReference<Resources> wr = mActiveResources.get(key);
1362 Resources existing = wr != null ? wr.get() : null;
1363 if (existing != null && existing.getAssets().isUpToDate()) {
1364 // Someone else already created the resources while we were
1365 // unlocked; go ahead and use theirs.
1366 r.getAssets().close();
1367 return existing;
1368 }
1369
1370 // XXX need to remove entries when weak references go away
1371 mActiveResources.put(key, new WeakReference<Resources>(r));
1372 return r;
1373 }
1374 }
1375
1376 /**
1377 * Creates the top level resources for the given package.
1378 */
1379 Resources getTopLevelResources(String resDir, LoadedApk pkgInfo) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001380 return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001381 }
1382
1383 final Handler getHandler() {
1384 return mH;
1385 }
1386
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001387 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1388 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001389 synchronized (mPackages) {
1390 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1392 ref = mPackages.get(packageName);
1393 } else {
1394 ref = mResourcePackages.get(packageName);
1395 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001396 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001397 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001398 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1399 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 if (packageInfo != null && (packageInfo.mResources == null
1401 || packageInfo.mResources.getAssets().isUpToDate())) {
1402 if (packageInfo.isSecurityViolation()
1403 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1404 throw new SecurityException(
1405 "Requesting code from " + packageName
1406 + " to be run in process "
1407 + mBoundApplication.processName
1408 + "/" + mBoundApplication.appInfo.uid);
1409 }
1410 return packageInfo;
1411 }
1412 }
1413
1414 ApplicationInfo ai = null;
1415 try {
1416 ai = getPackageManager().getApplicationInfo(packageName,
1417 PackageManager.GET_SHARED_LIBRARY_FILES);
1418 } catch (RemoteException e) {
1419 }
1420
1421 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001422 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 }
1424
1425 return null;
1426 }
1427
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001428 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1429 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1431 boolean securityViolation = includeCode && ai.uid != 0
1432 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
1433 ? ai.uid != mBoundApplication.appInfo.uid : true);
1434 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1435 |Context.CONTEXT_IGNORE_SECURITY))
1436 == Context.CONTEXT_INCLUDE_CODE) {
1437 if (securityViolation) {
1438 String msg = "Requesting code from " + ai.packageName
1439 + " (with uid " + ai.uid + ")";
1440 if (mBoundApplication != null) {
1441 msg = msg + " to be run in process "
1442 + mBoundApplication.processName + " (with uid "
1443 + mBoundApplication.appInfo.uid + ")";
1444 }
1445 throw new SecurityException(msg);
1446 }
1447 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001448 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 }
1450
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001451 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1452 CompatibilityInfo compatInfo) {
1453 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 }
1455
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001456 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1457 synchronized (mPackages) {
1458 WeakReference<LoadedApk> ref;
1459 if (includeCode) {
1460 ref = mPackages.get(packageName);
1461 } else {
1462 ref = mResourcePackages.get(packageName);
1463 }
1464 return ref != null ? ref.get() : null;
1465 }
1466 }
1467
1468 private final LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1470 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001471 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 if (includeCode) {
1473 ref = mPackages.get(aInfo.packageName);
1474 } else {
1475 ref = mResourcePackages.get(aInfo.packageName);
1476 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001477 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 if (packageInfo == null || (packageInfo.mResources != null
1479 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001480 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 : "Loading resource-only package ") + aInfo.packageName
1482 + " (in " + (mBoundApplication != null
1483 ? mBoundApplication.processName : null)
1484 + ")");
1485 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001486 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 securityViolation, includeCode &&
1488 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1489 if (includeCode) {
1490 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001491 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 } else {
1493 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001494 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 }
1496 }
1497 return packageInfo;
1498 }
1499 }
1500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 ActivityThread() {
1502 }
1503
1504 public ApplicationThread getApplicationThread()
1505 {
1506 return mAppThread;
1507 }
1508
1509 public Instrumentation getInstrumentation()
1510 {
1511 return mInstrumentation;
1512 }
1513
1514 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001515 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 }
1517
1518 public boolean isProfiling() {
1519 return mBoundApplication != null && mBoundApplication.profileFile != null;
1520 }
1521
1522 public String getProfileFilePath() {
1523 return mBoundApplication.profileFile;
1524 }
1525
1526 public Looper getLooper() {
1527 return mLooper;
1528 }
1529
1530 public Application getApplication() {
1531 return mInitialApplication;
1532 }
Bob Leee5408332009-09-04 18:31:17 -07001533
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001534 public String getProcessName() {
1535 return mBoundApplication.processName;
1536 }
Bob Leee5408332009-09-04 18:31:17 -07001537
Dianne Hackborn21556372010-02-04 16:34:40 -08001538 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 synchronized (this) {
1540 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001541 ContextImpl context =
1542 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001543 LoadedApk info = new LoadedApk(this, "android", context, null,
1544 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 context.init(info, null, this);
1546 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001547 getConfiguration(), getDisplayMetricsLocked(
1548 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001550 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 // + ": " + context.getResources().getConfiguration());
1552 }
1553 }
1554 return mSystemContext;
1555 }
1556
Mike Cleron432b7132009-09-24 15:28:29 -07001557 public void installSystemApplicationInfo(ApplicationInfo info) {
1558 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001559 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001560 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001561 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Mike Cleron432b7132009-09-24 15:28:29 -07001562 }
1563 }
1564
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001565 void ensureJitEnabled() {
1566 if (!mJitEnabled) {
1567 mJitEnabled = true;
1568 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1569 }
1570 }
1571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 void scheduleGcIdler() {
1573 if (!mGcIdlerScheduled) {
1574 mGcIdlerScheduled = true;
1575 Looper.myQueue().addIdleHandler(mGcIdler);
1576 }
1577 mH.removeMessages(H.GC_WHEN_IDLE);
1578 }
1579
1580 void unscheduleGcIdler() {
1581 if (mGcIdlerScheduled) {
1582 mGcIdlerScheduled = false;
1583 Looper.myQueue().removeIdleHandler(mGcIdler);
1584 }
1585 mH.removeMessages(H.GC_WHEN_IDLE);
1586 }
1587
1588 void doGcIfNeeded() {
1589 mGcIdlerScheduled = false;
1590 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001591 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 // + "m now=" + now);
1593 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001594 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 BinderInternal.forceGc("bg");
1596 }
1597 }
1598
Jeff Hamilton52d32032011-01-08 15:31:26 -06001599 public void registerOnActivityPausedListener(Activity activity,
1600 OnActivityPausedListener listener) {
1601 synchronized (mOnPauseListeners) {
1602 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1603 if (list == null) {
1604 list = new ArrayList<OnActivityPausedListener>();
1605 mOnPauseListeners.put(activity, list);
1606 }
1607 list.add(listener);
1608 }
1609 }
1610
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001611 public void unregisterOnActivityPausedListener(Activity activity,
1612 OnActivityPausedListener listener) {
1613 synchronized (mOnPauseListeners) {
1614 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1615 if (list != null) {
1616 list.remove(listener);
1617 }
1618 }
1619 }
1620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 public final ActivityInfo resolveActivityInfo(Intent intent) {
1622 ActivityInfo aInfo = intent.resolveActivityInfo(
1623 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1624 if (aInfo == null) {
1625 // Throw an exception.
1626 Instrumentation.checkStartActivityResult(
1627 IActivityManager.START_CLASS_NOT_FOUND, intent);
1628 }
1629 return aInfo;
1630 }
Bob Leee5408332009-09-04 18:31:17 -07001631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001634 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001635 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001637 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 r.intent = intent;
1639 r.state = state;
1640 r.parent = parent;
1641 r.embeddedID = id;
1642 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001643 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 if (localLOGV) {
1645 ComponentName compname = intent.getComponent();
1646 String name;
1647 if (compname != null) {
1648 name = compname.toShortString();
1649 } else {
1650 name = "(Intent " + intent + ").getComponent() returned null";
1651 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001652 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 + ", comp=" + name
1654 + ", token=" + token);
1655 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001656 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 }
1658
1659 public final Activity getActivity(IBinder token) {
1660 return mActivities.get(token).activity;
1661 }
1662
1663 public final void sendActivityResult(
1664 IBinder token, String id, int requestCode,
1665 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001666 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001667 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1669 list.add(new ResultInfo(id, requestCode, resultCode, data));
1670 mAppThread.scheduleSendResult(token, list);
1671 }
1672
1673 // if the thread hasn't started yet, we don't have the handler, so just
1674 // save the messages until we're ready.
1675 private final void queueOrSendMessage(int what, Object obj) {
1676 queueOrSendMessage(what, obj, 0, 0);
1677 }
1678
1679 private final void queueOrSendMessage(int what, Object obj, int arg1) {
1680 queueOrSendMessage(what, obj, arg1, 0);
1681 }
1682
1683 private final void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
1684 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001685 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1687 + ": " + arg1 + " / " + obj);
1688 Message msg = Message.obtain();
1689 msg.what = what;
1690 msg.obj = obj;
1691 msg.arg1 = arg1;
1692 msg.arg2 = arg2;
1693 mH.sendMessage(msg);
1694 }
1695 }
1696
Dianne Hackborn21556372010-02-04 16:34:40 -08001697 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 String what) {
1699 ContextCleanupInfo cci = new ContextCleanupInfo();
1700 cci.context = context;
1701 cci.who = who;
1702 cci.what = what;
1703 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1704 }
1705
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001706 private final Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1708
1709 ActivityInfo aInfo = r.activityInfo;
1710 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001711 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 Context.CONTEXT_INCLUDE_CODE);
1713 }
Bob Leee5408332009-09-04 18:31:17 -07001714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 ComponentName component = r.intent.getComponent();
1716 if (component == null) {
1717 component = r.intent.resolveActivity(
1718 mInitialApplication.getPackageManager());
1719 r.intent.setComponent(component);
1720 }
1721
1722 if (r.activityInfo.targetActivity != null) {
1723 component = new ComponentName(r.activityInfo.packageName,
1724 r.activityInfo.targetActivity);
1725 }
1726
1727 Activity activity = null;
1728 try {
1729 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
1730 activity = mInstrumentation.newActivity(
1731 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08001732 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 r.intent.setExtrasClassLoader(cl);
1734 if (r.state != null) {
1735 r.state.setClassLoader(cl);
1736 }
1737 } catch (Exception e) {
1738 if (!mInstrumentation.onException(activity, e)) {
1739 throw new RuntimeException(
1740 "Unable to instantiate activity " + component
1741 + ": " + e.toString(), e);
1742 }
1743 }
1744
1745 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001746 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001747
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001748 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
1749 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 TAG, r + ": app=" + app
1751 + ", appName=" + app.getPackageName()
1752 + ", pkg=" + r.packageInfo.getPackageName()
1753 + ", comp=" + r.intent.getComponent().toShortString()
1754 + ", dir=" + r.packageInfo.getAppDir());
1755
1756 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001757 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 appContext.init(r.packageInfo, r.token, this);
1759 appContext.setOuterContext(activity);
1760 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001761 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001762 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07001763 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001764 activity.attach(appContext, this, getInstrumentation(), r.token,
1765 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001766 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07001767
Christopher Tateb70f3df2009-04-07 16:07:59 -07001768 if (customIntent != null) {
1769 activity.mIntent = customIntent;
1770 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001771 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 activity.mStartedActivity = false;
1773 int theme = r.activityInfo.getThemeResource();
1774 if (theme != 0) {
1775 activity.setTheme(theme);
1776 }
1777
1778 activity.mCalled = false;
1779 mInstrumentation.callActivityOnCreate(activity, r.state);
1780 if (!activity.mCalled) {
1781 throw new SuperNotCalledException(
1782 "Activity " + r.intent.getComponent().toShortString() +
1783 " did not call through to super.onCreate()");
1784 }
1785 r.activity = activity;
1786 r.stopped = true;
1787 if (!r.activity.mFinished) {
1788 activity.performStart();
1789 r.stopped = false;
1790 }
1791 if (!r.activity.mFinished) {
1792 if (r.state != null) {
1793 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
1794 }
1795 }
1796 if (!r.activity.mFinished) {
1797 activity.mCalled = false;
1798 mInstrumentation.callActivityOnPostCreate(activity, r.state);
1799 if (!activity.mCalled) {
1800 throw new SuperNotCalledException(
1801 "Activity " + r.intent.getComponent().toShortString() +
1802 " did not call through to super.onPostCreate()");
1803 }
1804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 }
1806 r.paused = true;
1807
1808 mActivities.put(r.token, r);
1809
1810 } catch (SuperNotCalledException e) {
1811 throw e;
1812
1813 } catch (Exception e) {
1814 if (!mInstrumentation.onException(activity, e)) {
1815 throw new RuntimeException(
1816 "Unable to start activity " + component
1817 + ": " + e.toString(), e);
1818 }
1819 }
1820
1821 return activity;
1822 }
1823
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001824 private final void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 // If we are getting ready to gc after going to the background, well
1826 // we are back active so skip it.
1827 unscheduleGcIdler();
1828
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001829 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001831 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832
1833 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08001834 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001835 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 handleResumeActivity(r.token, false, r.isForward);
1837
1838 if (!r.activity.mFinished && r.startsNotResumed) {
1839 // The activity manager actually wants this one to start out
1840 // paused, because it needs to be visible but isn't in the
1841 // foreground. We accomplish this by going through the
1842 // normal startup (because activities expect to go through
1843 // onResume() the first time they run, before their window
1844 // is displayed), and then pausing it. However, in this case
1845 // we do -not- need to do the full pause cycle (of freezing
1846 // and such) because the activity manager assumes it can just
1847 // retain the current state it has.
1848 try {
1849 r.activity.mCalled = false;
1850 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001851 // We need to keep around the original state, in case
1852 // we need to be created again.
1853 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 if (!r.activity.mCalled) {
1855 throw new SuperNotCalledException(
1856 "Activity " + r.intent.getComponent().toShortString() +
1857 " did not call through to super.onPause()");
1858 }
1859
1860 } catch (SuperNotCalledException e) {
1861 throw e;
1862
1863 } catch (Exception e) {
1864 if (!mInstrumentation.onException(r.activity, e)) {
1865 throw new RuntimeException(
1866 "Unable to pause activity "
1867 + r.intent.getComponent().toShortString()
1868 + ": " + e.toString(), e);
1869 }
1870 }
1871 r.paused = true;
1872 }
1873 } else {
1874 // If there was an error, for any reason, tell the activity
1875 // manager to stop us.
1876 try {
1877 ActivityManagerNative.getDefault()
1878 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
1879 } catch (RemoteException ex) {
1880 }
1881 }
1882 }
1883
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001884 private final void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 List<Intent> intents) {
1886 final int N = intents.size();
1887 for (int i=0; i<N; i++) {
1888 Intent intent = intents.get(i);
1889 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001890 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
1892 }
1893 }
1894
1895 public final void performNewIntents(IBinder token,
1896 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001897 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 if (r != null) {
1899 final boolean resumed = !r.paused;
1900 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001901 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 mInstrumentation.callActivityOnPause(r.activity);
1903 }
1904 deliverNewIntents(r, intents);
1905 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001906 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001907 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 }
1909 }
1910 }
Bob Leee5408332009-09-04 18:31:17 -07001911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 private final void handleNewIntent(NewIntentData data) {
1913 performNewIntents(data.token, data.intents);
1914 }
1915
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07001916 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
1917
1918 /**
1919 * Return the Intent that's currently being handled by a
1920 * BroadcastReceiver on this thread, or null if none.
1921 * @hide
1922 */
1923 public static Intent getIntentBeingBroadcast() {
1924 return sCurrentBroadcastIntent.get();
1925 }
1926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 private final void handleReceiver(ReceiverData data) {
1928 // If we are getting ready to gc after going to the background, well
1929 // we are back active so skip it.
1930 unscheduleGcIdler();
1931
1932 String component = data.intent.getComponent().getClassName();
1933
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001934 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001935 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936
1937 IActivityManager mgr = ActivityManagerNative.getDefault();
1938
1939 BroadcastReceiver receiver = null;
1940 try {
1941 java.lang.ClassLoader cl = packageInfo.getClassLoader();
1942 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07001943 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
1945 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07001946 if (DEBUG_BROADCAST) Slog.i(TAG,
1947 "Finishing failed broadcast to " + data.intent.getComponent());
1948 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 throw new RuntimeException(
1950 "Unable to instantiate receiver " + component
1951 + ": " + e.toString(), e);
1952 }
1953
1954 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001955 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001956
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001957 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 TAG, "Performing receive of " + data.intent
1959 + ": app=" + app
1960 + ", appName=" + app.getPackageName()
1961 + ", pkg=" + packageInfo.getPackageName()
1962 + ", comp=" + data.intent.getComponent().toShortString()
1963 + ", dir=" + packageInfo.getAppDir());
1964
Dianne Hackborn21556372010-02-04 16:34:40 -08001965 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07001966 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07001967 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 receiver.onReceive(context.getReceiverRestrictedContext(),
1969 data.intent);
1970 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07001971 if (DEBUG_BROADCAST) Slog.i(TAG,
1972 "Finishing failed broadcast to " + data.intent.getComponent());
1973 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 if (!mInstrumentation.onException(receiver, e)) {
1975 throw new RuntimeException(
1976 "Unable to start receiver " + component
1977 + ": " + e.toString(), e);
1978 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07001979 } finally {
1980 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 }
1982
Dianne Hackborne829fef2010-10-26 17:44:01 -07001983 if (receiver.getPendingResult() != null) {
1984 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 }
1986 }
1987
Christopher Tate181fafa2009-05-14 11:12:14 -07001988 // Instantiate a BackupAgent and tell it that it's alive
1989 private final void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001990 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07001991
1992 // no longer idle; we have backup work to do
1993 unscheduleGcIdler();
1994
1995 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001996 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07001997 String packageName = packageInfo.mPackageName;
1998 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001999 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002000 + " already exists");
2001 return;
2002 }
Bob Leee5408332009-09-04 18:31:17 -07002003
Christopher Tate181fafa2009-05-14 11:12:14 -07002004 BackupAgent agent = null;
2005 String classname = data.appInfo.backupAgentName;
2006 if (classname == null) {
2007 if (data.backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002008 Slog.e(TAG, "Attempted incremental backup but no defined agent for "
Christopher Tate181fafa2009-05-14 11:12:14 -07002009 + packageName);
2010 return;
2011 }
2012 classname = "android.app.FullBackupAgent";
2013 }
2014 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002015 IBinder binder = null;
2016 try {
2017 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2018 agent = (BackupAgent) cl.loadClass(data.appInfo.backupAgentName).newInstance();
2019
2020 // set up the agent's context
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002021 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing BackupAgent "
Christopher Tated1475e02009-07-09 15:36:17 -07002022 + data.appInfo.backupAgentName);
2023
Dianne Hackborn21556372010-02-04 16:34:40 -08002024 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002025 context.init(packageInfo, null, this);
2026 context.setOuterContext(agent);
2027 agent.attach(context);
2028
2029 agent.onCreate();
2030 binder = agent.onBind();
2031 mBackupAgents.put(packageName, agent);
2032 } catch (Exception e) {
2033 // If this is during restore, fail silently; otherwise go
2034 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002035 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tated1475e02009-07-09 15:36:17 -07002036 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE) {
2037 throw e;
2038 }
2039 // falling through with 'binder' still null
2040 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002041
2042 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002043 try {
2044 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2045 } catch (RemoteException e) {
2046 // nothing to do.
2047 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002048 } catch (Exception e) {
2049 throw new RuntimeException("Unable to create BackupAgent "
2050 + data.appInfo.backupAgentName + ": " + e.toString(), e);
2051 }
2052 }
2053
2054 // Tear down a BackupAgent
2055 private final void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002056 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002057
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002058 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002059 String packageName = packageInfo.mPackageName;
2060 BackupAgent agent = mBackupAgents.get(packageName);
2061 if (agent != null) {
2062 try {
2063 agent.onDestroy();
2064 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002065 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002066 e.printStackTrace();
2067 }
2068 mBackupAgents.remove(packageName);
2069 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002070 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002071 }
2072 }
2073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 private final void handleCreateService(CreateServiceData data) {
2075 // If we are getting ready to gc after going to the background, well
2076 // we are back active so skip it.
2077 unscheduleGcIdler();
2078
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002079 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002080 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 Service service = null;
2082 try {
2083 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2084 service = (Service) cl.loadClass(data.info.name).newInstance();
2085 } catch (Exception e) {
2086 if (!mInstrumentation.onException(service, e)) {
2087 throw new RuntimeException(
2088 "Unable to instantiate service " + data.info.name
2089 + ": " + e.toString(), e);
2090 }
2091 }
2092
2093 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002094 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095
Dianne Hackborn21556372010-02-04 16:34:40 -08002096 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 context.init(packageInfo, null, this);
2098
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002099 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 context.setOuterContext(service);
2101 service.attach(context, this, data.info.name, data.token, app,
2102 ActivityManagerNative.getDefault());
2103 service.onCreate();
2104 mServices.put(data.token, service);
2105 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002106 ActivityManagerNative.getDefault().serviceDoneExecuting(
2107 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 } catch (RemoteException e) {
2109 // nothing to do.
2110 }
2111 } catch (Exception e) {
2112 if (!mInstrumentation.onException(service, e)) {
2113 throw new RuntimeException(
2114 "Unable to create service " + data.info.name
2115 + ": " + e.toString(), e);
2116 }
2117 }
2118 }
2119
2120 private final void handleBindService(BindServiceData data) {
2121 Service s = mServices.get(data.token);
2122 if (s != null) {
2123 try {
2124 data.intent.setExtrasClassLoader(s.getClassLoader());
2125 try {
2126 if (!data.rebind) {
2127 IBinder binder = s.onBind(data.intent);
2128 ActivityManagerNative.getDefault().publishService(
2129 data.token, data.intent, binder);
2130 } else {
2131 s.onRebind(data.intent);
2132 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002133 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002135 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 } catch (RemoteException ex) {
2137 }
2138 } catch (Exception e) {
2139 if (!mInstrumentation.onException(s, e)) {
2140 throw new RuntimeException(
2141 "Unable to bind to service " + s
2142 + " with " + data.intent + ": " + e.toString(), e);
2143 }
2144 }
2145 }
2146 }
2147
2148 private final void handleUnbindService(BindServiceData data) {
2149 Service s = mServices.get(data.token);
2150 if (s != null) {
2151 try {
2152 data.intent.setExtrasClassLoader(s.getClassLoader());
2153 boolean doRebind = s.onUnbind(data.intent);
2154 try {
2155 if (doRebind) {
2156 ActivityManagerNative.getDefault().unbindFinished(
2157 data.token, data.intent, doRebind);
2158 } else {
2159 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002160 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 }
2162 } catch (RemoteException ex) {
2163 }
2164 } catch (Exception e) {
2165 if (!mInstrumentation.onException(s, e)) {
2166 throw new RuntimeException(
2167 "Unable to unbind to service " + s
2168 + " with " + data.intent + ": " + e.toString(), e);
2169 }
2170 }
2171 }
2172 }
2173
Dianne Hackborn625ac272010-09-17 18:29:22 -07002174 private void handleDumpService(DumpComponentInfo info) {
Dianne Hackborn9a849832011-04-07 15:11:57 -07002175 Service s = mServices.get(info.token);
2176 if (s != null) {
2177 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2178 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2179 pw.flush();
2180 try {
2181 info.fd.close();
2182 } catch (IOException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 }
2184 }
2185 }
2186
Dianne Hackborn625ac272010-09-17 18:29:22 -07002187 private void handleDumpActivity(DumpComponentInfo info) {
Dianne Hackborn9a849832011-04-07 15:11:57 -07002188 ActivityClientRecord r = mActivities.get(info.token);
2189 if (r != null && r.activity != null) {
2190 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2191 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2192 pw.flush();
2193 try {
2194 info.fd.close();
2195 } catch (IOException e) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07002196 }
2197 }
2198 }
2199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 private final void handleServiceArgs(ServiceArgsData data) {
2201 Service s = mServices.get(data.token);
2202 if (s != null) {
2203 try {
2204 if (data.args != null) {
2205 data.args.setExtrasClassLoader(s.getClassLoader());
2206 }
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002207 int res = s.onStartCommand(data.args, data.flags, data.startId);
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002208
2209 QueuedWork.waitToFinish();
2210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002212 ActivityManagerNative.getDefault().serviceDoneExecuting(
2213 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 } catch (RemoteException e) {
2215 // nothing to do.
2216 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002217 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 } catch (Exception e) {
2219 if (!mInstrumentation.onException(s, e)) {
2220 throw new RuntimeException(
2221 "Unable to start service " + s
2222 + " with " + data.args + ": " + e.toString(), e);
2223 }
2224 }
2225 }
2226 }
2227
2228 private final void handleStopService(IBinder token) {
2229 Service s = mServices.remove(token);
2230 if (s != null) {
2231 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002232 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 s.onDestroy();
2234 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002235 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002237 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002239
2240 QueuedWork.waitToFinish();
2241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002243 ActivityManagerNative.getDefault().serviceDoneExecuting(
2244 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 } catch (RemoteException e) {
2246 // nothing to do.
2247 }
2248 } catch (Exception e) {
2249 if (!mInstrumentation.onException(s, e)) {
2250 throw new RuntimeException(
2251 "Unable to stop service " + s
2252 + ": " + e.toString(), e);
2253 }
2254 }
2255 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002256 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 }
2258
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002259 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002261 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002262 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 + " finished=" + r.activity.mFinished);
2264 if (r != null && !r.activity.mFinished) {
2265 if (clearHide) {
2266 r.hideForNow = false;
2267 r.activity.mStartedActivity = false;
2268 }
2269 try {
2270 if (r.pendingIntents != null) {
2271 deliverNewIntents(r, r.pendingIntents);
2272 r.pendingIntents = null;
2273 }
2274 if (r.pendingResults != null) {
2275 deliverResults(r, r.pendingResults);
2276 r.pendingResults = null;
2277 }
2278 r.activity.performResume();
2279
Bob Leee5408332009-09-04 18:31:17 -07002280 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 r.paused = false;
2284 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 r.state = null;
2286 } catch (Exception e) {
2287 if (!mInstrumentation.onException(r.activity, e)) {
2288 throw new RuntimeException(
2289 "Unable to resume activity "
2290 + r.intent.getComponent().toShortString()
2291 + ": " + e.toString(), e);
2292 }
2293 }
2294 }
2295 return r;
2296 }
2297
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002298 final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
2299 if (r.mPendingRemoveWindow != null) {
2300 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2301 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2302 if (wtoken != null) {
2303 WindowManagerImpl.getDefault().closeAll(wtoken,
2304 r.activity.getClass().getName(), "Activity");
2305 }
2306 }
2307 r.mPendingRemoveWindow = null;
2308 r.mPendingRemoveWindowManager = null;
2309 }
2310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2312 // If we are getting ready to gc after going to the background, well
2313 // we are back active so skip it.
2314 unscheduleGcIdler();
2315
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002316 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317
2318 if (r != null) {
2319 final Activity a = r.activity;
2320
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002321 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 TAG, "Resume " + r + " started activity: " +
2323 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2324 + ", finished: " + a.mFinished);
2325
2326 final int forwardBit = isForward ?
2327 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 // If the window hasn't yet been added to the window manager,
2330 // and this guy didn't finish itself or start another activity,
2331 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002332 boolean willBeVisible = !a.mStartedActivity;
2333 if (!willBeVisible) {
2334 try {
2335 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2336 a.getActivityToken());
2337 } catch (RemoteException e) {
2338 }
2339 }
2340 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 r.window = r.activity.getWindow();
2342 View decor = r.window.getDecorView();
2343 decor.setVisibility(View.INVISIBLE);
2344 ViewManager wm = a.getWindowManager();
2345 WindowManager.LayoutParams l = r.window.getAttributes();
2346 a.mDecor = decor;
2347 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2348 l.softInputMode |= forwardBit;
2349 if (a.mVisibleFromClient) {
2350 a.mWindowAdded = true;
2351 wm.addView(decor, l);
2352 }
2353
2354 // If the window has already been added, but during resume
2355 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002356 // window visible.
2357 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002358 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 TAG, "Launch " + r + " mStartedActivity set");
2360 r.hideForNow = true;
2361 }
2362
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002363 // Get rid of anything left hanging around.
2364 cleanUpPendingRemoveWindows(r);
2365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 // The window is now visible if it has been added, we are not
2367 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002368 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002369 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002371 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002372 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 performConfigurationChanged(r.activity, r.newConfig);
2374 r.newConfig = null;
2375 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002376 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 + isForward);
2378 WindowManager.LayoutParams l = r.window.getAttributes();
2379 if ((l.softInputMode
2380 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2381 != forwardBit) {
2382 l.softInputMode = (l.softInputMode
2383 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2384 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002385 if (r.activity.mVisibleFromClient) {
2386 ViewManager wm = a.getWindowManager();
2387 View decor = r.window.getDecorView();
2388 wm.updateViewLayout(decor, l);
2389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 }
2391 r.activity.mVisibleFromServer = true;
2392 mNumVisibleActivities++;
2393 if (r.activity.mVisibleFromClient) {
2394 r.activity.makeVisible();
2395 }
2396 }
2397
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002398 if (!r.onlyLocalRequest) {
2399 r.nextIdle = mNewActivities;
2400 mNewActivities = r;
2401 if (localLOGV) Slog.v(
2402 TAG, "Scheduling idle handler for " + r);
2403 Looper.myQueue().addIdleHandler(new Idler());
2404 }
2405 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406
2407 } else {
2408 // If an exception was thrown when trying to resume, then
2409 // just end this activity.
2410 try {
2411 ActivityManagerNative.getDefault()
2412 .finishActivity(token, Activity.RESULT_CANCELED, null);
2413 } catch (RemoteException ex) {
2414 }
2415 }
2416 }
2417
2418 private int mThumbnailWidth = -1;
2419 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002420 private Bitmap mAvailThumbnailBitmap = null;
2421 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002423 private final Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002424 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002426 if (thumbnail == null) {
2427 int w = mThumbnailWidth;
2428 int h;
2429 if (w < 0) {
2430 Resources res = r.activity.getResources();
2431 mThumbnailHeight = h =
2432 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002434 mThumbnailWidth = w =
2435 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2436 } else {
2437 h = mThumbnailHeight;
2438 }
2439
2440 // On platforms where we don't want thumbnails, set dims to (0,0)
2441 if ((w > 0) && (h > 0)) {
2442 thumbnail = Bitmap.createBitmap(w, h, THUMBNAIL_FORMAT);
2443 thumbnail.eraseColor(0);
2444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 }
2446
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002447 if (thumbnail != null) {
2448 Canvas cv = mThumbnailCanvas;
2449 if (cv == null) {
2450 mThumbnailCanvas = cv = new Canvas();
2451 }
2452
2453 cv.setBitmap(thumbnail);
2454 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2455 mAvailThumbnailBitmap = thumbnail;
2456 thumbnail = null;
2457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 } catch (Exception e) {
2461 if (!mInstrumentation.onException(r.activity, e)) {
2462 throw new RuntimeException(
2463 "Unable to create thumbnail of "
2464 + r.intent.getComponent().toShortString()
2465 + ": " + e.toString(), e);
2466 }
2467 thumbnail = null;
2468 }
2469
2470 return thumbnail;
2471 }
2472
2473 private final void handlePauseActivity(IBinder token, boolean finished,
2474 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002475 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002477 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 if (userLeaving) {
2479 performUserLeavingActivity(r);
2480 }
Bob Leee5408332009-09-04 18:31:17 -07002481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002483 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002485 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002486 if (r.isPreHoneycomb()) {
2487 QueuedWork.waitToFinish();
2488 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 // Tell the activity manager we have paused.
2491 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002492 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 } catch (RemoteException ex) {
2494 }
2495 }
2496 }
2497
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002498 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 mInstrumentation.callActivityOnUserLeaving(r.activity);
2500 }
2501
2502 final Bundle performPauseActivity(IBinder token, boolean finished,
2503 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002504 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 return r != null ? performPauseActivity(r, finished, saveState) : null;
2506 }
2507
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002508 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 boolean saveState) {
2510 if (r.paused) {
2511 if (r.activity.mFinished) {
2512 // If we are finishing, we won't call onResume() in certain cases.
2513 // So here we likewise don't want to call onPause() if the activity
2514 // isn't resumed.
2515 return null;
2516 }
2517 RuntimeException e = new RuntimeException(
2518 "Performing pause of activity that is not resumed: "
2519 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002520 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 }
2522 Bundle state = null;
2523 if (finished) {
2524 r.activity.mFinished = true;
2525 }
2526 try {
2527 // Next have the activity save its current state and managed dialogs...
2528 if (!r.activity.mFinished && saveState) {
2529 state = new Bundle();
2530 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2531 r.state = state;
2532 }
2533 // Now we are idle.
2534 r.activity.mCalled = false;
2535 mInstrumentation.callActivityOnPause(r.activity);
2536 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2537 if (!r.activity.mCalled) {
2538 throw new SuperNotCalledException(
2539 "Activity " + r.intent.getComponent().toShortString() +
2540 " did not call through to super.onPause()");
2541 }
2542
2543 } catch (SuperNotCalledException e) {
2544 throw e;
2545
2546 } catch (Exception e) {
2547 if (!mInstrumentation.onException(r.activity, e)) {
2548 throw new RuntimeException(
2549 "Unable to pause activity "
2550 + r.intent.getComponent().toShortString()
2551 + ": " + e.toString(), e);
2552 }
2553 }
2554 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002555
2556 // Notify any outstanding on paused listeners
2557 ArrayList<OnActivityPausedListener> listeners;
2558 synchronized (mOnPauseListeners) {
2559 listeners = mOnPauseListeners.remove(r.activity);
2560 }
2561 int size = (listeners != null ? listeners.size() : 0);
2562 for (int i = 0; i < size; i++) {
2563 listeners.get(i).onPaused(r.activity);
2564 }
2565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 return state;
2567 }
2568
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002569 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002570 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002571 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 }
2573
2574 private static class StopInfo {
2575 Bitmap thumbnail;
2576 CharSequence description;
2577 }
2578
2579 private final class ProviderRefCount {
2580 public int count;
2581 ProviderRefCount(int pCount) {
2582 count = pCount;
2583 }
2584 }
2585
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002586 /**
2587 * Core implementation of stopping an activity. Note this is a little
2588 * tricky because the server's meaning of stop is slightly different
2589 * than our client -- for the server, stop means to save state and give
2590 * it the result when it is done, but the window may still be visible.
2591 * For the client, we want to call onStop()/onStart() to indicate when
2592 * the activity's UI visibillity changes.
2593 */
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002594 private final void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002595 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002596 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002597 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 if (r != null) {
2599 if (!keepShown && r.stopped) {
2600 if (r.activity.mFinished) {
2601 // If we are finishing, we won't call onResume() in certain
2602 // cases. So here we likewise don't want to call onStop()
2603 // if the activity isn't resumed.
2604 return;
2605 }
2606 RuntimeException e = new RuntimeException(
2607 "Performing stop of activity that is not resumed: "
2608 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002609 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 }
2611
2612 if (info != null) {
2613 try {
2614 // First create a thumbnail for the activity...
Jim Miller0b2a6d02010-07-13 18:01:29 -07002615 info.thumbnail = createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 info.description = r.activity.onCreateDescription();
2617 } catch (Exception e) {
2618 if (!mInstrumentation.onException(r.activity, e)) {
2619 throw new RuntimeException(
2620 "Unable to save state of activity "
2621 + r.intent.getComponent().toShortString()
2622 + ": " + e.toString(), e);
2623 }
2624 }
2625 }
2626
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002627 // Next have the activity save its current state and managed dialogs...
2628 if (!r.activity.mFinished && saveState) {
2629 if (r.state == null) {
2630 state = new Bundle();
2631 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2632 r.state = state;
2633 } else {
2634 state = r.state;
2635 }
2636 }
2637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 if (!keepShown) {
2639 try {
2640 // Now we are idle.
2641 r.activity.performStop();
2642 } catch (Exception e) {
2643 if (!mInstrumentation.onException(r.activity, e)) {
2644 throw new RuntimeException(
2645 "Unable to stop activity "
2646 + r.intent.getComponent().toShortString()
2647 + ": " + e.toString(), e);
2648 }
2649 }
2650 r.stopped = true;
2651 }
2652
2653 r.paused = true;
2654 }
2655 }
2656
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002657 private final void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 View v = r.activity.mDecor;
2659 if (v != null) {
2660 if (show) {
2661 if (!r.activity.mVisibleFromServer) {
2662 r.activity.mVisibleFromServer = true;
2663 mNumVisibleActivities++;
2664 if (r.activity.mVisibleFromClient) {
2665 r.activity.makeVisible();
2666 }
2667 }
2668 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002669 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002670 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 performConfigurationChanged(r.activity, r.newConfig);
2672 r.newConfig = null;
2673 }
2674 } else {
2675 if (r.activity.mVisibleFromServer) {
2676 r.activity.mVisibleFromServer = false;
2677 mNumVisibleActivities--;
2678 v.setVisibility(View.INVISIBLE);
2679 }
2680 }
2681 }
2682 }
2683
2684 private final void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002685 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 r.activity.mConfigChangeFlags |= configChanges;
2687
2688 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002689 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002691 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 TAG, "Finishing stop of " + r + ": show=" + show
2693 + " win=" + r.window);
2694
2695 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07002696
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002697 // Make sure any pending writes are now committed.
2698 if (!r.isPreHoneycomb()) {
2699 QueuedWork.waitToFinish();
2700 }
2701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 // Tell activity manager we have been stopped.
2703 try {
2704 ActivityManagerNative.getDefault().activityStopped(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002705 r.token, r.state, info.thumbnail, info.description);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 } catch (RemoteException ex) {
2707 }
2708 }
2709
2710 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002711 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 if (r.stopped) {
2713 r.activity.performRestart();
2714 r.stopped = false;
2715 }
2716 }
2717
2718 private final void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002719 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08002720
2721 if (r == null) {
2722 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
2723 return;
2724 }
2725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002727 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 } else if (show && r.stopped) {
2729 // If we are getting ready to gc after going to the background, well
2730 // we are back active so skip it.
2731 unscheduleGcIdler();
2732
2733 r.activity.performRestart();
2734 r.stopped = false;
2735 }
2736 if (r.activity.mDecor != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002737 if (Config.LOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 TAG, "Handle window " + r + " visibility: " + show);
2739 updateVisibility(r, show);
2740 }
2741 }
2742
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002743 private final void handleSleeping(IBinder token, boolean sleeping) {
2744 ActivityClientRecord r = mActivities.get(token);
2745
2746 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002747 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002748 return;
2749 }
2750
2751 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002752 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002753 try {
2754 // Now we are idle.
2755 r.activity.performStop();
2756 } catch (Exception e) {
2757 if (!mInstrumentation.onException(r.activity, e)) {
2758 throw new RuntimeException(
2759 "Unable to stop activity "
2760 + r.intent.getComponent().toShortString()
2761 + ": " + e.toString(), e);
2762 }
2763 }
2764 r.stopped = true;
2765 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002766
2767 // Make sure any pending writes are now committed.
2768 if (!r.isPreHoneycomb()) {
2769 QueuedWork.waitToFinish();
2770 }
2771
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002772 // Tell activity manager we slept.
2773 try {
2774 ActivityManagerNative.getDefault().activitySlept(r.token);
2775 } catch (RemoteException ex) {
2776 }
2777 } else {
2778 if (r.stopped && r.activity.mVisibleFromServer) {
2779 r.activity.performRestart();
2780 r.stopped = false;
2781 }
2782 }
2783 }
2784
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002785 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08002786 synchronized (mPackages) {
2787 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002788 }
2789 }
2790
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002791 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
2792 LoadedApk apk = peekPackageInfo(data.pkg, false);
2793 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002794 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002795 }
2796 apk = peekPackageInfo(data.pkg, true);
2797 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002798 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002799 }
2800 handleConfigurationChanged(mConfiguration, data.info);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002801 WindowManagerImpl.getDefault().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002802 }
2803
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002804 private final void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 final int N = results.size();
2806 for (int i=0; i<N; i++) {
2807 ResultInfo ri = results.get(i);
2808 try {
2809 if (ri.mData != null) {
2810 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
2811 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002812 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07002813 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 r.activity.dispatchActivityResult(ri.mResultWho,
2815 ri.mRequestCode, ri.mResultCode, ri.mData);
2816 } catch (Exception e) {
2817 if (!mInstrumentation.onException(r.activity, e)) {
2818 throw new RuntimeException(
2819 "Failure delivering result " + ri + " to activity "
2820 + r.intent.getComponent().toShortString()
2821 + ": " + e.toString(), e);
2822 }
2823 }
2824 }
2825 }
2826
2827 private final void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002828 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002829 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 if (r != null) {
2831 final boolean resumed = !r.paused;
2832 if (!r.activity.mFinished && r.activity.mDecor != null
2833 && r.hideForNow && resumed) {
2834 // We had hidden the activity because it started another
2835 // one... we have gotten a result back and we are not
2836 // paused, so make sure our window is visible.
2837 updateVisibility(r, true);
2838 }
2839 if (resumed) {
2840 try {
2841 // Now we are idle.
2842 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002843 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 mInstrumentation.callActivityOnPause(r.activity);
2845 if (!r.activity.mCalled) {
2846 throw new SuperNotCalledException(
2847 "Activity " + r.intent.getComponent().toShortString()
2848 + " did not call through to super.onPause()");
2849 }
2850 } catch (SuperNotCalledException e) {
2851 throw e;
2852 } catch (Exception e) {
2853 if (!mInstrumentation.onException(r.activity, e)) {
2854 throw new RuntimeException(
2855 "Unable to pause activity "
2856 + r.intent.getComponent().toShortString()
2857 + ": " + e.toString(), e);
2858 }
2859 }
2860 }
2861 deliverResults(r, res.results);
2862 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002863 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002864 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 }
2866 }
2867 }
2868
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002869 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 return performDestroyActivity(token, finishing, 0, false);
2871 }
2872
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002873 private final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002875 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002876 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002877 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002879 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 r.activity.mConfigChangeFlags |= configChanges;
2881 if (finishing) {
2882 r.activity.mFinished = true;
2883 }
2884 if (!r.paused) {
2885 try {
2886 r.activity.mCalled = false;
2887 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07002888 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 r.activity.getComponentName().getClassName());
2890 if (!r.activity.mCalled) {
2891 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002892 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 + " did not call through to super.onPause()");
2894 }
2895 } catch (SuperNotCalledException e) {
2896 throw e;
2897 } catch (Exception e) {
2898 if (!mInstrumentation.onException(r.activity, e)) {
2899 throw new RuntimeException(
2900 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002901 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 + ": " + e.toString(), e);
2903 }
2904 }
2905 r.paused = true;
2906 }
2907 if (!r.stopped) {
2908 try {
2909 r.activity.performStop();
2910 } catch (SuperNotCalledException e) {
2911 throw e;
2912 } catch (Exception e) {
2913 if (!mInstrumentation.onException(r.activity, e)) {
2914 throw new RuntimeException(
2915 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002916 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 + ": " + e.toString(), e);
2918 }
2919 }
2920 r.stopped = true;
2921 }
2922 if (getNonConfigInstance) {
2923 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002924 r.lastNonConfigurationInstances
2925 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 } catch (Exception e) {
2927 if (!mInstrumentation.onException(r.activity, e)) {
2928 throw new RuntimeException(
2929 "Unable to retain activity "
2930 + r.intent.getComponent().toShortString()
2931 + ": " + e.toString(), e);
2932 }
2933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 }
2935 try {
2936 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002937 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 if (!r.activity.mCalled) {
2939 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002940 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 " did not call through to super.onDestroy()");
2942 }
2943 if (r.window != null) {
2944 r.window.closeAllPanels();
2945 }
2946 } catch (SuperNotCalledException e) {
2947 throw e;
2948 } catch (Exception e) {
2949 if (!mInstrumentation.onException(r.activity, e)) {
2950 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002951 "Unable to destroy activity " + safeToComponentShortString(r.intent)
2952 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 }
2954 }
2955 }
2956 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002957 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 return r;
2959 }
2960
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002961 private static String safeToComponentShortString(Intent intent) {
2962 ComponentName component = intent.getComponent();
2963 return component == null ? "[Unknown]" : component.toShortString();
2964 }
2965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 private final void handleDestroyActivity(IBinder token, boolean finishing,
2967 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002968 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 configChanges, getNonConfigInstance);
2970 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002971 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 WindowManager wm = r.activity.getWindowManager();
2973 View v = r.activity.mDecor;
2974 if (v != null) {
2975 if (r.activity.mVisibleFromServer) {
2976 mNumVisibleActivities--;
2977 }
2978 IBinder wtoken = v.getWindowToken();
2979 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002980 if (r.onlyLocalRequest) {
2981 // Hold off on removing this until the new activity's
2982 // window is being added.
2983 r.mPendingRemoveWindow = v;
2984 r.mPendingRemoveWindowManager = wm;
2985 } else {
2986 wm.removeViewImmediate(v);
2987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002989 if (wtoken != null && r.mPendingRemoveWindow == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 WindowManagerImpl.getDefault().closeAll(wtoken,
2991 r.activity.getClass().getName(), "Activity");
2992 }
2993 r.activity.mDecor = null;
2994 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002995 if (r.mPendingRemoveWindow == null) {
2996 // If we are delaying the removal of the activity window, then
2997 // we can't clean up all windows here. Note that we can't do
2998 // so later either, which means any windows that aren't closed
2999 // by the app will leak. Well we try to warning them a lot
3000 // about leaking windows, because that is a bug, so if they are
3001 // using this recreate facility then they get to live with leaks.
3002 WindowManagerImpl.getDefault().closeAll(token,
3003 r.activity.getClass().getName(), "Activity");
3004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005
3006 // Mocked out contexts won't be participating in the normal
3007 // process lifecycle, but if we're running with a proper
3008 // ApplicationContext we need to have it tear down things
3009 // cleanly.
3010 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003011 if (c instanceof ContextImpl) {
3012 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 r.activity.getClass().getName(), "Activity");
3014 }
3015 }
3016 if (finishing) {
3017 try {
3018 ActivityManagerNative.getDefault().activityDestroyed(token);
3019 } catch (RemoteException ex) {
3020 // If the system process has died, it's game over for everyone.
3021 }
3022 }
3023 }
3024
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003025 public final void requestRelaunchActivity(IBinder token,
3026 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3027 int configChanges, boolean notResumed, Configuration config,
3028 boolean fromServer) {
3029 ActivityClientRecord target = null;
3030
3031 synchronized (mPackages) {
3032 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3033 ActivityClientRecord r = mRelaunchingActivities.get(i);
3034 if (r.token == token) {
3035 target = r;
3036 if (pendingResults != null) {
3037 if (r.pendingResults != null) {
3038 r.pendingResults.addAll(pendingResults);
3039 } else {
3040 r.pendingResults = pendingResults;
3041 }
3042 }
3043 if (pendingNewIntents != null) {
3044 if (r.pendingIntents != null) {
3045 r.pendingIntents.addAll(pendingNewIntents);
3046 } else {
3047 r.pendingIntents = pendingNewIntents;
3048 }
3049 }
3050 break;
3051 }
3052 }
3053
3054 if (target == null) {
3055 target = new ActivityClientRecord();
3056 target.token = token;
3057 target.pendingResults = pendingResults;
3058 target.pendingIntents = pendingNewIntents;
3059 if (!fromServer) {
3060 ActivityClientRecord existing = mActivities.get(token);
3061 if (existing != null) {
3062 target.startsNotResumed = existing.paused;
3063 }
3064 target.onlyLocalRequest = true;
3065 }
3066 mRelaunchingActivities.add(target);
3067 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3068 }
3069
3070 if (fromServer) {
3071 target.startsNotResumed = notResumed;
3072 target.onlyLocalRequest = false;
3073 }
3074 if (config != null) {
3075 target.createdConfig = config;
3076 }
3077 target.pendingConfigChanges |= configChanges;
3078 }
3079 }
3080
3081 private final void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 // If we are getting ready to gc after going to the background, well
3083 // we are back active so skip it.
3084 unscheduleGcIdler();
3085
3086 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003087 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 // First: make sure we have the most recent configuration and most
3090 // recent version of the activity, or skip it if some previous call
3091 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003092 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 int N = mRelaunchingActivities.size();
3094 IBinder token = tmp.token;
3095 tmp = null;
3096 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003097 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 if (r.token == token) {
3099 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003100 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 mRelaunchingActivities.remove(i);
3102 i--;
3103 N--;
3104 }
3105 }
Bob Leee5408332009-09-04 18:31:17 -07003106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003108 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 return;
3110 }
Bob Leee5408332009-09-04 18:31:17 -07003111
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003112 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3113 + tmp.token + " with configChanges=0x"
3114 + Integer.toHexString(configChanges));
3115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 if (mPendingConfiguration != null) {
3117 changedConfig = mPendingConfiguration;
3118 mPendingConfiguration = null;
3119 }
3120 }
Bob Leee5408332009-09-04 18:31:17 -07003121
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003122 if (tmp.createdConfig != null) {
3123 // If the activity manager is passing us its current config,
3124 // assume that is really what we want regardless of what we
3125 // may have pending.
3126 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003127 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3128 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3129 if (changedConfig == null
3130 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3131 changedConfig = tmp.createdConfig;
3132 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003133 }
3134 }
3135
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003136 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003137 + tmp.token + ": changedConfig=" + changedConfig);
3138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 // If there was a pending configuration change, execute it first.
3140 if (changedConfig != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003141 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 }
Bob Leee5408332009-09-04 18:31:17 -07003143
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003144 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003145 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146 if (r == null) {
3147 return;
3148 }
Bob Leee5408332009-09-04 18:31:17 -07003149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003151 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003152 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003153
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003154 r.activity.mChangingConfigurations = true;
3155
Dianne Hackborne2b04802010-12-09 09:24:55 -08003156 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003158 performPauseActivity(r.token, false, r.isPreHoneycomb());
3159 }
3160 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3161 r.state = new Bundle();
3162 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 }
Bob Leee5408332009-09-04 18:31:17 -07003164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 r.activity = null;
3168 r.window = null;
3169 r.hideForNow = false;
3170 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003171 // Merge any pending results and pending intents; don't just replace them
3172 if (tmp.pendingResults != null) {
3173 if (r.pendingResults == null) {
3174 r.pendingResults = tmp.pendingResults;
3175 } else {
3176 r.pendingResults.addAll(tmp.pendingResults);
3177 }
3178 }
3179 if (tmp.pendingIntents != null) {
3180 if (r.pendingIntents == null) {
3181 r.pendingIntents = tmp.pendingIntents;
3182 } else {
3183 r.pendingIntents.addAll(tmp.pendingIntents);
3184 }
3185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003187
Christopher Tateb70f3df2009-04-07 16:07:59 -07003188 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 }
3190
3191 private final void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003192 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 Bitmap thumbnail = createThumbnailBitmap(r);
3194 CharSequence description = null;
3195 try {
3196 description = r.activity.onCreateDescription();
3197 } catch (Exception e) {
3198 if (!mInstrumentation.onException(r.activity, e)) {
3199 throw new RuntimeException(
3200 "Unable to create description of activity "
3201 + r.intent.getComponent().toShortString()
3202 + ": " + e.toString(), e);
3203 }
3204 }
3205 //System.out.println("Reporting top thumbnail " + thumbnail);
3206 try {
3207 ActivityManagerNative.getDefault().reportThumbnail(
3208 token, thumbnail, description);
3209 } catch (RemoteException ex) {
3210 }
3211 }
3212
3213 ArrayList<ComponentCallbacks> collectComponentCallbacksLocked(
3214 boolean allActivities, Configuration newConfig) {
3215 ArrayList<ComponentCallbacks> callbacks
3216 = new ArrayList<ComponentCallbacks>();
Bob Leee5408332009-09-04 18:31:17 -07003217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 if (mActivities.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003219 Iterator<ActivityClientRecord> it = mActivities.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 while (it.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003221 ActivityClientRecord ar = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 Activity a = ar.activity;
3223 if (a != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003224 Configuration thisConfig = applyConfigCompatMainThread(newConfig,
3225 ar.packageInfo.mCompatibilityInfo.getIfNeeded());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 if (!ar.activity.mFinished && (allActivities ||
3227 (a != null && !ar.paused))) {
3228 // If the activity is currently resumed, its configuration
3229 // needs to change right now.
3230 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003231 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 // Otherwise, we will tell it about the change
3233 // the next time it is resumed or shown. Note that
3234 // the activity manager may, before then, decide the
3235 // activity needs to be destroyed to handle its new
3236 // configuration.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003237 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Setting activity "
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003238 + ar.activityInfo.name + " newConfig=" + thisConfig);
3239 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 }
3241 }
3242 }
3243 }
3244 if (mServices.size() > 0) {
3245 Iterator<Service> it = mServices.values().iterator();
3246 while (it.hasNext()) {
3247 callbacks.add(it.next());
3248 }
3249 }
3250 synchronized (mProviderMap) {
3251 if (mLocalProviders.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003252 Iterator<ProviderClientRecord> it = mLocalProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 while (it.hasNext()) {
3254 callbacks.add(it.next().mLocalProvider);
3255 }
3256 }
3257 }
3258 final int N = mAllApplications.size();
3259 for (int i=0; i<N; i++) {
3260 callbacks.add(mAllApplications.get(i));
3261 }
Bob Leee5408332009-09-04 18:31:17 -07003262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 return callbacks;
3264 }
Bob Leee5408332009-09-04 18:31:17 -07003265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 private final void performConfigurationChanged(
3267 ComponentCallbacks cb, Configuration config) {
3268 // Only for Activity objects, check that they actually call up to their
3269 // superclass implementation. ComponentCallbacks is an interface, so
3270 // we check the runtime type and act accordingly.
3271 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3272 if (activity != null) {
3273 activity.mCalled = false;
3274 }
Bob Leee5408332009-09-04 18:31:17 -07003275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 boolean shouldChangeConfig = false;
3277 if ((activity == null) || (activity.mCurrentConfig == null)) {
3278 shouldChangeConfig = true;
3279 } else {
Bob Leee5408332009-09-04 18:31:17 -07003280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 // If the new config is the same as the config this Activity
3282 // is already running with then don't bother calling
3283 // onConfigurationChanged
3284 int diff = activity.mCurrentConfig.diff(config);
3285 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 // If this activity doesn't handle any of the config changes
3287 // then don't bother calling onConfigurationChanged as we're
3288 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003289 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 shouldChangeConfig = true;
3291 }
3292 }
3293 }
Bob Leee5408332009-09-04 18:31:17 -07003294
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003295 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003296 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 if (shouldChangeConfig) {
3298 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 if (activity != null) {
3301 if (!activity.mCalled) {
3302 throw new SuperNotCalledException(
3303 "Activity " + activity.getLocalClassName() +
3304 " did not call through to super.onConfigurationChanged()");
3305 }
3306 activity.mConfigChangeFlags = 0;
3307 activity.mCurrentConfig = new Configuration(config);
3308 }
3309 }
3310 }
3311
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003312 public final void applyConfigurationToResources(Configuration config) {
3313 synchronized (mPackages) {
3314 applyConfigurationToResourcesLocked(config, null);
3315 }
3316 }
3317
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003318 final boolean applyConfigurationToResourcesLocked(Configuration config,
3319 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003320 if (mResConfiguration == null) {
3321 mResConfiguration = new Configuration();
3322 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003323 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003324 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003325 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003326 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003327 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003328 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003329 DisplayMetrics dm = getDisplayMetricsLocked(compat, true);
3330
3331 if (compat != null && (mResCompatibilityInfo == null ||
3332 !mResCompatibilityInfo.equals(compat))) {
3333 mResCompatibilityInfo = compat;
3334 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3335 | ActivityInfo.CONFIG_SCREEN_SIZE
3336 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3337 }
Bob Leee5408332009-09-04 18:31:17 -07003338
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003339 // set it for java, this also affects newly created Resources
3340 if (config.locale != null) {
3341 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 }
Bob Leee5408332009-09-04 18:31:17 -07003343
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003344 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003345
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003346 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003347 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003348
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003349 Iterator<WeakReference<Resources>> it =
3350 mActiveResources.values().iterator();
3351 //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3352 // mActiveResources.entrySet().iterator();
3353 while (it.hasNext()) {
3354 WeakReference<Resources> v = it.next();
3355 Resources r = v.get();
3356 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003357 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003358 + r + " config to: " + config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003359 r.updateConfiguration(config, dm, compat);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003360 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003361 // + " " + r + ": " + r.getConfiguration());
3362 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003363 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003364 it.remove();
3365 }
3366 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003367
3368 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003369 }
3370
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003371 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003372
3373 ArrayList<ComponentCallbacks> callbacks = null;
3374
3375 synchronized (mPackages) {
3376 if (mPendingConfiguration != null) {
3377 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3378 config = mPendingConfiguration;
3379 }
3380 mPendingConfiguration = null;
3381 }
3382
3383 if (config == null) {
3384 return;
3385 }
3386
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003387 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003388 + config);
3389
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003390 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 if (mConfiguration == null) {
3393 mConfiguration = new Configuration();
3394 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003395 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003396 return;
3397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 mConfiguration.updateFrom(config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003399 if (mCompatConfiguration == null) {
3400 mCompatConfiguration = new Configuration();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003401 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003402 mCompatConfiguration.setTo(mConfiguration);
3403 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
3404 mResCompatibilityInfo.applyToConfiguration(mCompatConfiguration);
3405 config = mCompatConfiguration;
3406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 callbacks = collectComponentCallbacksLocked(false, config);
3408 }
Bob Leee5408332009-09-04 18:31:17 -07003409
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003410 if (callbacks != null) {
3411 final int N = callbacks.size();
3412 for (int i=0; i<N; i++) {
3413 performConfigurationChanged(callbacks.get(i), config);
3414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003415 }
3416 }
3417
3418 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003419 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003420 if (r == null || r.activity == null) {
3421 return;
3422 }
Bob Leee5408332009-09-04 18:31:17 -07003423
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003424 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003425 + r.activityInfo.name);
3426
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003427 performConfigurationChanged(r.activity, mCompatConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 }
3429
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003430 final void handleProfilerControl(boolean start, ProfilerControlData pcd) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003431 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003432 try {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003433 Debug.startMethodTracing(pcd.path, pcd.fd.getFileDescriptor(),
3434 8 * 1024 * 1024, 0);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003435 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003436 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003437 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003438 } finally {
3439 try {
3440 pcd.fd.close();
3441 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003442 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003443 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003444 }
3445 } else {
3446 Debug.stopMethodTracing();
3447 }
3448 }
Bob Leee5408332009-09-04 18:31:17 -07003449
Andy McFadden824c5102010-07-09 16:26:57 -07003450 final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
3451 if (managed) {
3452 try {
3453 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3454 } catch (IOException e) {
3455 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3456 + " -- can the process access this path?");
3457 } finally {
3458 try {
3459 dhd.fd.close();
3460 } catch (IOException e) {
3461 Slog.w(TAG, "Failure closing profile fd", e);
3462 }
3463 }
3464 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003465 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003466 }
3467 }
3468
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003469 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3470 boolean hasPkgInfo = false;
3471 if (packages != null) {
3472 for (int i=packages.length-1; i>=0; i--) {
3473 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3474 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003475 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003476 ref = mPackages.get(packages[i]);
3477 if (ref != null && ref.get() != null) {
3478 hasPkgInfo = true;
3479 } else {
3480 ref = mResourcePackages.get(packages[i]);
3481 if (ref != null && ref.get() != null) {
3482 hasPkgInfo = true;
3483 }
3484 }
3485 }
3486 mPackages.remove(packages[i]);
3487 mResourcePackages.remove(packages[i]);
3488 }
3489 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003490 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003491 hasPkgInfo);
3492 }
3493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 final void handleLowMemory() {
3495 ArrayList<ComponentCallbacks> callbacks
3496 = new ArrayList<ComponentCallbacks>();
3497
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003498 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 callbacks = collectComponentCallbacksLocked(true, null);
3500 }
Bob Leee5408332009-09-04 18:31:17 -07003501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 final int N = callbacks.size();
3503 for (int i=0; i<N; i++) {
3504 callbacks.get(i).onLowMemory();
3505 }
3506
Chris Tatece229052009-03-25 16:44:52 -07003507 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3508 if (Process.myUid() != Process.SYSTEM_UID) {
3509 int sqliteReleased = SQLiteDatabase.releaseMemory();
3510 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3511 }
Bob Leee5408332009-09-04 18:31:17 -07003512
Mike Reedcaf0df12009-04-27 14:32:05 -04003513 // Ask graphics to free up as much as possible (font/image caches)
3514 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515
3516 BinderInternal.forceGc("mem");
3517 }
3518
3519 private final void handleBindApplication(AppBindData data) {
3520 mBoundApplication = data;
3521 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003522 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003525 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 android.ddm.DdmHandleAppName.setAppName(data.processName);
3527
3528 /*
3529 * Before spawning a new process, reset the time zone to be the system time zone.
3530 * This needs to be done because the system time zone could have changed after the
3531 * the spawning of this process. Without doing this this process would have the incorrect
3532 * system time zone.
3533 */
3534 TimeZone.setDefault(null);
3535
3536 /*
3537 * Initialize the default locale in this process for the reasons we set the time zone.
3538 */
3539 Locale.setDefault(data.config.locale);
3540
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003541 /*
3542 * Update the system configuration since its preloaded and might not
3543 * reflect configuration changes. The configuration object passed
3544 * in AppBindData can be safely assumed to be up to date
3545 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003546 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003547
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003548 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003550 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003551 * For system applications on userdebug/eng builds, log stack
3552 * traces of disk and network access to dropbox for analysis.
3553 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07003554 if ((data.appInfo.flags &
3555 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07003556 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
3557 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003558 }
3559
3560 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07003561 * For apps targetting SDK Honeycomb or later, we don't allow
3562 * network usage on the main event loop / UI thread.
3563 *
3564 * Note to those grepping: this is what ultimately throws
3565 * NetworkOnMainThreadException ...
3566 */
3567 if (data.appInfo.targetSdkVersion > 9) {
3568 StrictMode.enableDeathOnNetwork();
3569 }
3570
3571 /**
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003572 * Switch this process to density compatibility mode if needed.
3573 */
3574 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
3575 == 0) {
3576 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3577 }
Bob Leee5408332009-09-04 18:31:17 -07003578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003579 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
3580 // XXX should have option to change the port.
3581 Debug.changeDebugPort(8100);
3582 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003583 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 + " is waiting for the debugger on port 8100...");
3585
3586 IActivityManager mgr = ActivityManagerNative.getDefault();
3587 try {
3588 mgr.showWaitingForDebugger(mAppThread, true);
3589 } catch (RemoteException ex) {
3590 }
3591
3592 Debug.waitForDebugger();
3593
3594 try {
3595 mgr.showWaitingForDebugger(mAppThread, false);
3596 } catch (RemoteException ex) {
3597 }
3598
3599 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003600 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003601 + " can be debugged on port 8100...");
3602 }
3603 }
3604
Robert Greenwalt434203a2010-10-11 16:00:27 -07003605 /**
3606 * Initialize the default http proxy in this process for the reasons we set the time zone.
3607 */
3608 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
3609 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
3610 try {
3611 ProxyProperties proxyProperties = service.getProxy();
3612 Proxy.setHttpProxySystemProperty(proxyProperties);
3613 } catch (RemoteException e) {}
3614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003615 if (data.instrumentationName != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08003616 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003617 appContext.init(data.info, null, this);
3618 InstrumentationInfo ii = null;
3619 try {
3620 ii = appContext.getPackageManager().
3621 getInstrumentationInfo(data.instrumentationName, 0);
3622 } catch (PackageManager.NameNotFoundException e) {
3623 }
3624 if (ii == null) {
3625 throw new RuntimeException(
3626 "Unable to find instrumentation info for: "
3627 + data.instrumentationName);
3628 }
3629
3630 mInstrumentationAppDir = ii.sourceDir;
3631 mInstrumentationAppPackage = ii.packageName;
3632 mInstrumentedAppDir = data.info.getAppDir();
3633
3634 ApplicationInfo instrApp = new ApplicationInfo();
3635 instrApp.packageName = ii.packageName;
3636 instrApp.sourceDir = ii.sourceDir;
3637 instrApp.publicSourceDir = ii.publicSourceDir;
3638 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07003639 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003640 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003641 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08003642 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003643 instrContext.init(pi, null, this);
3644
3645 try {
3646 java.lang.ClassLoader cl = instrContext.getClassLoader();
3647 mInstrumentation = (Instrumentation)
3648 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
3649 } catch (Exception e) {
3650 throw new RuntimeException(
3651 "Unable to instantiate instrumentation "
3652 + data.instrumentationName + ": " + e.toString(), e);
3653 }
3654
3655 mInstrumentation.init(this, instrContext, appContext,
3656 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
3657
3658 if (data.profileFile != null && !ii.handleProfiling) {
3659 data.handlingProfiling = true;
3660 File file = new File(data.profileFile);
3661 file.getParentFile().mkdirs();
3662 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
3663 }
3664
3665 try {
3666 mInstrumentation.onCreate(data.instrumentationArgs);
3667 }
3668 catch (Exception e) {
3669 throw new RuntimeException(
3670 "Exception thrown in onCreate() of "
3671 + data.instrumentationName + ": " + e.toString(), e);
3672 }
3673
3674 } else {
3675 mInstrumentation = new Instrumentation();
3676 }
3677
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003678 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08003679 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003680 }
3681
Christopher Tate181fafa2009-05-14 11:12:14 -07003682 // If the app is being launched for full backup or restore, bring it up in
3683 // a restricted environment with the base application class.
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003684 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 mInitialApplication = app;
3686
3687 List<ProviderInfo> providers = data.providers;
3688 if (providers != null) {
3689 installContentProviders(app, providers);
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003690 // For process that contains content providers, we want to
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003691 // ensure that the JIT is enabled "at some point".
3692 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 }
3694
3695 try {
3696 mInstrumentation.callApplicationOnCreate(app);
3697 } catch (Exception e) {
3698 if (!mInstrumentation.onException(app, e)) {
3699 throw new RuntimeException(
3700 "Unable to create application " + app.getClass().getName()
3701 + ": " + e.toString(), e);
3702 }
3703 }
3704 }
3705
3706 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
3707 IActivityManager am = ActivityManagerNative.getDefault();
3708 if (mBoundApplication.profileFile != null && mBoundApplication.handlingProfiling) {
3709 Debug.stopMethodTracing();
3710 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003711 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712 // + ", app thr: " + mAppThread);
3713 try {
3714 am.finishInstrumentation(mAppThread, resultCode, results);
3715 } catch (RemoteException ex) {
3716 }
3717 }
3718
3719 private final void installContentProviders(
3720 Context context, List<ProviderInfo> providers) {
3721 final ArrayList<IActivityManager.ContentProviderHolder> results =
3722 new ArrayList<IActivityManager.ContentProviderHolder>();
3723
3724 Iterator<ProviderInfo> i = providers.iterator();
3725 while (i.hasNext()) {
3726 ProviderInfo cpi = i.next();
3727 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07003728 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 buf.append(cpi.authority);
3730 buf.append(": ");
3731 buf.append(cpi.name);
3732 Log.i(TAG, buf.toString());
3733 IContentProvider cp = installProvider(context, null, cpi, false);
3734 if (cp != null) {
3735 IActivityManager.ContentProviderHolder cph =
3736 new IActivityManager.ContentProviderHolder(cpi);
3737 cph.provider = cp;
3738 results.add(cph);
3739 // Don't ever unload this provider from the process.
3740 synchronized(mProviderMap) {
3741 mProviderRefCountMap.put(cp.asBinder(), new ProviderRefCount(10000));
3742 }
3743 }
3744 }
3745
3746 try {
3747 ActivityManagerNative.getDefault().publishContentProviders(
3748 getApplicationThread(), results);
3749 } catch (RemoteException ex) {
3750 }
3751 }
3752
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003753 private final IContentProvider getExistingProvider(Context context, String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003754 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003755 final ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 if (pr != null) {
3757 return pr.mProvider;
3758 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003759 return null;
3760 }
3761 }
3762
3763 private final IContentProvider getProvider(Context context, String name) {
3764 IContentProvider existing = getExistingProvider(context, name);
3765 if (existing != null) {
3766 return existing;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 }
3768
3769 IActivityManager.ContentProviderHolder holder = null;
3770 try {
3771 holder = ActivityManagerNative.getDefault().getContentProvider(
3772 getApplicationThread(), name);
3773 } catch (RemoteException ex) {
3774 }
3775 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003776 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 return null;
3778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779
3780 IContentProvider prov = installProvider(context, holder.provider,
3781 holder.info, true);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003782 //Slog.i(TAG, "noReleaseNeeded=" + holder.noReleaseNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003783 if (holder.noReleaseNeeded || holder.provider == null) {
3784 // We are not going to release the provider if it is an external
3785 // provider that doesn't care about being released, or if it is
3786 // a local provider running in this process.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003787 //Slog.i(TAG, "*** NO RELEASE NEEDED");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003788 synchronized(mProviderMap) {
3789 mProviderRefCountMap.put(prov.asBinder(), new ProviderRefCount(10000));
3790 }
3791 }
3792 return prov;
3793 }
3794
3795 public final IContentProvider acquireProvider(Context c, String name) {
3796 IContentProvider provider = getProvider(c, name);
3797 if(provider == null)
3798 return null;
3799 IBinder jBinder = provider.asBinder();
3800 synchronized(mProviderMap) {
3801 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3802 if(prc == null) {
3803 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
3804 } else {
3805 prc.count++;
3806 } //end else
3807 } //end synchronized
3808 return provider;
3809 }
3810
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003811 public final IContentProvider acquireExistingProvider(Context c, String name) {
3812 IContentProvider provider = getExistingProvider(c, name);
3813 if(provider == null)
3814 return null;
3815 IBinder jBinder = provider.asBinder();
3816 synchronized(mProviderMap) {
3817 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3818 if(prc == null) {
3819 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
3820 } else {
3821 prc.count++;
3822 } //end else
3823 } //end synchronized
3824 return provider;
3825 }
3826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 public final boolean releaseProvider(IContentProvider provider) {
3828 if(provider == null) {
3829 return false;
3830 }
3831 IBinder jBinder = provider.asBinder();
3832 synchronized(mProviderMap) {
3833 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3834 if(prc == null) {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003835 if(localLOGV) Slog.v(TAG, "releaseProvider::Weird shouldn't be here");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 return false;
3837 } else {
3838 prc.count--;
3839 if(prc.count == 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003840 // Schedule the actual remove asynchronously, since we
3841 // don't know the context this will be called in.
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003842 // TODO: it would be nice to post a delayed message, so
3843 // if we come back and need the same provider quickly
3844 // we will still have it available.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003845 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, provider);
3846 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003847 } //end if
3848 } //end else
3849 } //end synchronized
3850 return true;
3851 }
3852
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003853 final void completeRemoveProvider(IContentProvider provider) {
3854 IBinder jBinder = provider.asBinder();
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003855 String name = null;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003856 synchronized(mProviderMap) {
3857 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3858 if(prc != null && prc.count == 0) {
3859 mProviderRefCountMap.remove(jBinder);
3860 //invoke removeProvider to dereference provider
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003861 name = removeProviderLocked(provider);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003862 }
3863 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003864
3865 if (name != null) {
3866 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003867 if(localLOGV) Slog.v(TAG, "removeProvider::Invoking " +
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003868 "ActivityManagerNative.removeContentProvider(" + name);
3869 ActivityManagerNative.getDefault().removeContentProvider(
3870 getApplicationThread(), name);
3871 } catch (RemoteException e) {
3872 //do nothing content provider object is dead any way
3873 } //end catch
3874 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003875 }
3876
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003877 public final String removeProviderLocked(IContentProvider provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 if (provider == null) {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003879 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003880 }
3881 IBinder providerBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003883 String name = null;
3884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 // remove the provider from mProviderMap
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003886 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 while (iter.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003888 ProviderClientRecord pr = iter.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 IBinder myBinder = pr.mProvider.asBinder();
3890 if (myBinder == providerBinder) {
3891 //find if its published by this process itself
3892 if(pr.mLocalProvider != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003893 if(localLOGV) Slog.i(TAG, "removeProvider::found local provider returning");
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003894 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003895 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003896 if(localLOGV) Slog.v(TAG, "removeProvider::Not local provider Unlinking " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897 "death recipient");
3898 //content provider is in another process
3899 myBinder.unlinkToDeath(pr, 0);
3900 iter.remove();
3901 //invoke remove only once for the very first name seen
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003902 if(name == null) {
3903 name = pr.mName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 }
3905 } //end if myBinder
3906 } //end while iter
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003907
3908 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003909 }
3910
3911 final void removeDeadProvider(String name, IContentProvider provider) {
3912 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003913 ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003914 if (pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003915 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003916 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003917 if (removed != null) {
3918 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
3919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 }
3921 }
3922 }
3923
3924 final void removeDeadProviderLocked(String name, IContentProvider provider) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003925 ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003926 if (pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003927 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003928 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003929 if (removed != null) {
3930 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
3931 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003932 }
3933 }
3934
3935 private final IContentProvider installProvider(Context context,
3936 IContentProvider provider, ProviderInfo info, boolean noisy) {
3937 ContentProvider localProvider = null;
3938 if (provider == null) {
3939 if (noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003940 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941 + info.name);
3942 }
3943 Context c = null;
3944 ApplicationInfo ai = info.applicationInfo;
3945 if (context.getPackageName().equals(ai.packageName)) {
3946 c = context;
3947 } else if (mInitialApplication != null &&
3948 mInitialApplication.getPackageName().equals(ai.packageName)) {
3949 c = mInitialApplication;
3950 } else {
3951 try {
3952 c = context.createPackageContext(ai.packageName,
3953 Context.CONTEXT_INCLUDE_CODE);
3954 } catch (PackageManager.NameNotFoundException e) {
3955 }
3956 }
3957 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003958 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003959 ai.packageName +
3960 " while loading content provider " +
3961 info.name);
3962 return null;
3963 }
3964 try {
3965 final java.lang.ClassLoader cl = c.getClassLoader();
3966 localProvider = (ContentProvider)cl.
3967 loadClass(info.name).newInstance();
3968 provider = localProvider.getIContentProvider();
3969 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003970 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 info.name + " from sourceDir " +
3972 info.applicationInfo.sourceDir);
3973 return null;
3974 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003975 if (Config.LOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003976 TAG, "Instantiating local provider " + info.name);
3977 // XXX Need to create the correct context for this provider.
3978 localProvider.attachInfo(c, info);
3979 } catch (java.lang.Exception e) {
3980 if (!mInstrumentation.onException(null, e)) {
3981 throw new RuntimeException(
3982 "Unable to get provider " + info.name
3983 + ": " + e.toString(), e);
3984 }
3985 return null;
3986 }
3987 } else if (localLOGV) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003988 Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989 + info.name);
3990 }
3991
3992 synchronized (mProviderMap) {
3993 // Cache the pointer for the remote provider.
3994 String names[] = PATTERN_SEMICOLON.split(info.authority);
3995 for (int i=0; i<names.length; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003996 ProviderClientRecord pr = new ProviderClientRecord(names[i], provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003997 localProvider);
3998 try {
3999 provider.asBinder().linkToDeath(pr, 0);
4000 mProviderMap.put(names[i], pr);
4001 } catch (RemoteException e) {
4002 return null;
4003 }
4004 }
4005 if (localProvider != null) {
4006 mLocalProviders.put(provider.asBinder(),
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004007 new ProviderClientRecord(null, provider, localProvider));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004008 }
4009 }
4010
4011 return provider;
4012 }
4013
4014 private final void attach(boolean system) {
4015 sThreadLocal.set(this);
4016 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 if (!system) {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004018 ViewRoot.addFirstDrawHandler(new Runnable() {
4019 public void run() {
4020 ensureJitEnabled();
4021 }
4022 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4024 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4025 IActivityManager mgr = ActivityManagerNative.getDefault();
4026 try {
4027 mgr.attachApplication(mAppThread);
4028 } catch (RemoteException ex) {
4029 }
4030 } else {
4031 // Don't set application object here -- if the system crashes,
4032 // we can't display an alert, we just want to die die die.
4033 android.ddm.DdmHandleAppName.setAppName("system_process");
4034 try {
4035 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004036 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004037 context.init(getSystemContext().mPackageInfo, null, this);
4038 Application app = Instrumentation.newApplication(Application.class, context);
4039 mAllApplications.add(app);
4040 mInitialApplication = app;
4041 app.onCreate();
4042 } catch (Exception e) {
4043 throw new RuntimeException(
4044 "Unable to instantiate Application():" + e.toString(), e);
4045 }
4046 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004047
4048 ViewRoot.addConfigCallback(new ComponentCallbacks() {
4049 public void onConfigurationChanged(Configuration newConfig) {
4050 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004051 // We need to apply this change to the resources
4052 // immediately, because upon returning the view
4053 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004054 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004055 // This actually changed the resources! Tell
4056 // everyone about it.
4057 if (mPendingConfiguration == null ||
4058 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4059 mPendingConfiguration = newConfig;
4060
4061 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4062 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004063 }
4064 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004065 }
4066 public void onLowMemory() {
4067 }
4068 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 }
4070
4071 private final void detach()
4072 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004073 sThreadLocal.set(null);
4074 }
4075
4076 public static final ActivityThread systemMain() {
Romain Guy52339202010-09-03 16:04:46 -07004077 HardwareRenderer.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 ActivityThread thread = new ActivityThread();
4079 thread.attach(true);
4080 return thread;
4081 }
4082
4083 public final void installSystemProviders(List providers) {
4084 if (providers != null) {
4085 installContentProviders(mInitialApplication,
4086 (List<ProviderInfo>)providers);
4087 }
4088 }
4089
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004090 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004091 synchronized (mPackages) {
4092 if (mCoreSettings != null) {
4093 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004094 } else {
4095 return defaultValue;
4096 }
4097 }
4098 }
4099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004100 public static final void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004101 SamplingProfilerIntegration.start();
4102
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004103 // CloseGuard defaults to true and can be quite spammy. We
4104 // disable it here, but selectively enable it later (via
4105 // StrictMode) on debug builds, but using DropBox, not logs.
4106 CloseGuard.setEnabled(false);
4107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004108 Process.setArgV0("<pre-initialized>");
4109
4110 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07004111 if (sMainThreadHandler == null) {
4112 sMainThreadHandler = new Handler();
4113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004114
4115 ActivityThread thread = new ActivityThread();
4116 thread.attach(false);
4117
Dianne Hackborn287952c2010-09-22 22:34:31 -07004118 if (false) {
4119 Looper.myLooper().setMessageLogging(new
4120 LogPrinter(Log.DEBUG, "ActivityThread"));
4121 }
4122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004123 Looper.loop();
4124
4125 if (Process.supportsProcesses()) {
4126 throw new RuntimeException("Main thread loop unexpectedly exited");
4127 }
4128
4129 thread.detach();
Bob Leeeec2f412009-09-10 11:01:24 +02004130 String name = (thread.mInitialApplication != null)
4131 ? thread.mInitialApplication.getPackageName()
4132 : "<unknown>";
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004133 Slog.i(TAG, "Main thread of " + name + " is now exiting");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 }
4135}