blob: 8be8bb8281984666277d58e546dc336cd9ddc4d8 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Christopher Tate45281862010-03-05 15:46:30 -080019import android.app.backup.BackupAgent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.BroadcastReceiver;
21import android.content.ComponentCallbacks;
22import android.content.ComponentName;
23import android.content.ContentProvider;
24import android.content.Context;
25import android.content.IContentProvider;
26import android.content.Intent;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070027import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.pm.ActivityInfo;
29import android.content.pm.ApplicationInfo;
30import android.content.pm.IPackageManager;
31import android.content.pm.InstrumentationInfo;
32import android.content.pm.PackageManager;
Sen Hubde75702010-05-28 01:54:03 -070033import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.pm.ProviderInfo;
35import android.content.pm.ServiceInfo;
36import android.content.res.AssetManager;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -070037import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.res.Configuration;
39import android.content.res.Resources;
40import android.database.sqlite.SQLiteDatabase;
41import android.database.sqlite.SQLiteDebug;
Vasu Noric3849202010-03-09 10:47:25 -080042import android.database.sqlite.SQLiteDebug.DbStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.graphics.Bitmap;
44import android.graphics.Canvas;
Robert Greenwalt434203a2010-10-11 16:00:27 -070045import android.net.IConnectivityManager;
46import android.net.Proxy;
47import android.net.ProxyProperties;
Joe Onoratod630f102011-03-17 18:42:26 -070048import android.os.AsyncTask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.Bundle;
50import android.os.Debug;
51import android.os.Handler;
52import android.os.IBinder;
53import android.os.Looper;
54import android.os.Message;
55import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070056import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.os.Process;
58import android.os.RemoteException;
59import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070060import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.SystemClock;
62import android.util.AndroidRuntimeException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.util.DisplayMetrics;
64import android.util.EventLog;
65import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070066import android.util.LogPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080067import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070069import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.view.View;
71import android.view.ViewDebug;
72import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070073import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.view.Window;
75import android.view.WindowManager;
76import android.view.WindowManagerImpl;
77
78import com.android.internal.os.BinderInternal;
79import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070080import com.android.internal.os.SamplingProfilerIntegration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
82import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl;
83
84import java.io.File;
85import java.io.FileDescriptor;
86import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070087import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.io.PrintWriter;
89import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -070090import java.net.InetAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import java.util.ArrayList;
92import java.util.HashMap;
93import java.util.Iterator;
94import java.util.List;
95import java.util.Locale;
96import java.util.Map;
97import java.util.TimeZone;
98import java.util.regex.Pattern;
99
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800100import dalvik.system.CloseGuard;
Bob Leee5408332009-09-04 18:31:17 -0700101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102final class SuperNotCalledException extends AndroidRuntimeException {
103 public SuperNotCalledException(String msg) {
104 super(msg);
105 }
106}
107
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700108final class RemoteServiceException extends AndroidRuntimeException {
109 public RemoteServiceException(String msg) {
110 super(msg);
111 }
112}
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114/**
115 * This manages the execution of the main thread in an
116 * application process, scheduling and executing activities,
117 * broadcasts, and other operations on it as the activity
118 * manager requests.
119 *
120 * {@hide}
121 */
122public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700123 /** @hide */
124 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700125 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 private static final boolean DEBUG = false;
Joe Onorato43a17652011-04-06 19:22:23 -0700127 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700128 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700129 /** @hide */
130 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700131 private static final boolean DEBUG_RESULTS = false;
Christopher Tate4a627c72011-04-01 14:43:32 -0700132 private static final boolean DEBUG_BACKUP = true;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700133 private static final boolean DEBUG_CONFIGURATION = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
135 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
136 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
137 private static final int LOG_ON_PAUSE_CALLED = 30021;
138 private static final int LOG_ON_RESUME_CALLED = 30022;
139
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700140 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700141
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700142 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700144 final ApplicationThread mAppThread = new ApplicationThread();
145 final Looper mLooper = Looper.myLooper();
146 final H mH = new H();
147 final HashMap<IBinder, ActivityClientRecord> mActivities
148 = new HashMap<IBinder, ActivityClientRecord>();
149 // List of new activities (via ActivityRecord.nextIdle) that should
150 // be reported when next we idle.
151 ActivityClientRecord mNewActivities = null;
152 // Number of activities that are currently visible on-screen.
153 int mNumVisibleActivities = 0;
154 final HashMap<IBinder, Service> mServices
155 = new HashMap<IBinder, Service>();
156 AppBindData mBoundApplication;
157 Configuration mConfiguration;
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;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700348 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700350 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 Intent args;
352 public String toString() {
353 return "ServiceArgsData{token=" + token + " startId=" + startId
354 + " args=" + args + "}";
355 }
356 }
357
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400358 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700359 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 String processName;
361 ApplicationInfo appInfo;
362 List<ProviderInfo> providers;
363 ComponentName instrumentationName;
364 String profileFile;
365 Bundle instrumentationArgs;
366 IInstrumentationWatcher instrumentationWatcher;
367 int debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700368 boolean restrictedBackupMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400370 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 boolean handlingProfiling;
372 public String toString() {
373 return "AppBindData{appInfo=" + appInfo + "}";
374 }
375 }
376
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400377 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700378 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700379 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800380 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
383
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400384 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 IBinder token;
386 List<ResultInfo> results;
387 public String toString() {
388 return "ResultData{token=" + token + " results" + results + "}";
389 }
390 }
391
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400392 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800393 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 String what;
395 String who;
396 }
397
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400398 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700399 String path;
400 ParcelFileDescriptor fd;
401 }
402
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400403 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700404 String path;
405 ParcelFileDescriptor fd;
406 }
407
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400408 static final class UpdateCompatibilityData {
409 String pkg;
410 CompatibilityInfo info;
411 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700412
Chet Haase9c1e23b2011-03-24 10:51:31 -0700413 native private void dumpGraphicsInfo(FileDescriptor fd);
414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 private final class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700416 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
417 private static final String ONE_COUNT_COLUMN = "%21s %8d";
418 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
419 private static final String TWO_COUNT_COLUMNS_DB = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700420 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 // Formatting for checkin service - update version if row format changes
423 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 public final void schedulePauseActivity(IBinder token, boolean finished,
426 boolean userLeaving, int configChanges) {
427 queueOrSendMessage(
428 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
429 token,
430 (userLeaving ? 1 : 0),
431 configChanges);
432 }
433
434 public final void scheduleStopActivity(IBinder token, boolean showWindow,
435 int configChanges) {
436 queueOrSendMessage(
437 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
438 token, 0, configChanges);
439 }
440
441 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
442 queueOrSendMessage(
443 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
444 token);
445 }
446
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800447 public final void scheduleSleeping(IBinder token, boolean sleeping) {
448 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
449 }
450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
452 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
453 }
454
455 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
456 ResultData res = new ResultData();
457 res.token = token;
458 res.results = results;
459 queueOrSendMessage(H.SEND_RESULT, res);
460 }
461
462 // we use token to identify this activity without having to send the
463 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700464 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400465 ActivityInfo info, CompatibilityInfo compatInfo, Bundle state,
466 List<ResultInfo> pendingResults,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700468 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469
470 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700471 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 r.intent = intent;
473 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400474 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 r.state = state;
476
477 r.pendingResults = pendingResults;
478 r.pendingIntents = pendingNewIntents;
479
480 r.startsNotResumed = notResumed;
481 r.isForward = isForward;
482
483 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
484 }
485
486 public final void scheduleRelaunchActivity(IBinder token,
487 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800488 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800489 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
490 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 }
492
493 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
494 NewIntentData data = new NewIntentData();
495 data.intents = intents;
496 data.token = token;
497
498 queueOrSendMessage(H.NEW_INTENT, data);
499 }
500
501 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
502 int configChanges) {
503 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
504 configChanges);
505 }
506
507 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400508 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
509 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700510 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
511 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400513 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 queueOrSendMessage(H.RECEIVER, r);
515 }
516
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400517 public final void scheduleCreateBackupAgent(ApplicationInfo app,
518 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700519 CreateBackupAgentData d = new CreateBackupAgentData();
520 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400521 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700522 d.backupMode = backupMode;
523
524 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
525 }
526
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400527 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
528 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700529 CreateBackupAgentData d = new CreateBackupAgentData();
530 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400531 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700532
533 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
534 }
535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400537 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 CreateServiceData s = new CreateServiceData();
539 s.token = token;
540 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400541 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542
543 queueOrSendMessage(H.CREATE_SERVICE, s);
544 }
545
546 public final void scheduleBindService(IBinder token, Intent intent,
547 boolean rebind) {
548 BindServiceData s = new BindServiceData();
549 s.token = token;
550 s.intent = intent;
551 s.rebind = rebind;
552
553 queueOrSendMessage(H.BIND_SERVICE, s);
554 }
555
556 public final void scheduleUnbindService(IBinder token, Intent intent) {
557 BindServiceData s = new BindServiceData();
558 s.token = token;
559 s.intent = intent;
560
561 queueOrSendMessage(H.UNBIND_SERVICE, s);
562 }
563
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700564 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700565 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 ServiceArgsData s = new ServiceArgsData();
567 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700568 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700570 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 s.args = args;
572
573 queueOrSendMessage(H.SERVICE_ARGS, s);
574 }
575
576 public final void scheduleStopService(IBinder token) {
577 queueOrSendMessage(H.STOP_SERVICE, token);
578 }
579
580 public final void bindApplication(String processName,
581 ApplicationInfo appInfo, List<ProviderInfo> providers,
582 ComponentName instrumentationName, String profileFile,
583 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Christopher Tate181fafa2009-05-14 11:12:14 -0700584 int debugMode, boolean isRestrictedBackupMode, Configuration config,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400585 CompatibilityInfo compatInfo, Map<String, IBinder> services,
586 Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587
588 if (services != null) {
589 // Setup the service cache in the ServiceManager
590 ServiceManager.initServiceCache(services);
591 }
592
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800593 setCoreSettings(coreSettings);
594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 AppBindData data = new AppBindData();
596 data.processName = processName;
597 data.appInfo = appInfo;
598 data.providers = providers;
599 data.instrumentationName = instrumentationName;
600 data.profileFile = profileFile;
601 data.instrumentationArgs = instrumentationArgs;
602 data.instrumentationWatcher = instrumentationWatcher;
603 data.debugMode = debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700604 data.restrictedBackupMode = isRestrictedBackupMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400606 data.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 queueOrSendMessage(H.BIND_APPLICATION, data);
608 }
609
610 public final void scheduleExit() {
611 queueOrSendMessage(H.EXIT_APPLICATION, null);
612 }
613
Christopher Tate5e1ab332009-09-01 20:32:49 -0700614 public final void scheduleSuicide() {
615 queueOrSendMessage(H.SUICIDE, null);
616 }
617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 public void requestThumbnail(IBinder token) {
619 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
620 }
621
622 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800623 synchronized (mPackages) {
624 if (mPendingConfiguration == null ||
625 mPendingConfiguration.isOtherSeqNewer(config)) {
626 mPendingConfiguration = config;
627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 }
629 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
630 }
631
632 public void updateTimeZone() {
633 TimeZone.setDefault(null);
634 }
635
Robert Greenwalt03595d02010-11-02 14:08:23 -0700636 public void clearDnsCache() {
637 // a non-standard API to get this to libcore
638 InetAddress.clearDnsCache();
639 }
640
Robert Greenwalt434203a2010-10-11 16:00:27 -0700641 public void setHttpProxy(String host, String port, String exclList) {
642 Proxy.setHttpProxySystemProperty(host, port, exclList);
643 }
644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 public void processInBackground() {
646 mH.removeMessages(H.GC_WHEN_IDLE);
647 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
648 }
649
650 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700651 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700652 try {
653 data.fd = ParcelFileDescriptor.dup(fd);
654 data.token = servicetoken;
655 data.args = args;
656 queueOrSendMessage(H.DUMP_SERVICE, data);
657 } catch (IOException e) {
658 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 }
660 }
661
662 // This function exists to make sure all receiver dispatching is
663 // correctly ordered, since these are one-way calls and the binder driver
664 // applies transaction ordering per object for such calls.
665 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700666 int resultCode, String dataStr, Bundle extras, boolean ordered,
667 boolean sticky) throws RemoteException {
668 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 }
Bob Leee5408332009-09-04 18:31:17 -0700670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 public void scheduleLowMemory() {
672 queueOrSendMessage(H.LOW_MEMORY, null);
673 }
674
675 public void scheduleActivityConfigurationChanged(IBinder token) {
676 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
677 }
678
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700679 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd) {
680 ProfilerControlData pcd = new ProfilerControlData();
681 pcd.path = path;
682 pcd.fd = fd;
683 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800684 }
685
Andy McFadden824c5102010-07-09 16:26:57 -0700686 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
687 DumpHeapData dhd = new DumpHeapData();
688 dhd.path = path;
689 dhd.fd = fd;
690 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
691 }
692
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700693 public void setSchedulingGroup(int group) {
694 // Note: do this immediately, since going into the foreground
695 // should happen regardless of what pending work we have to do
696 // and the activity manager will wait for us to report back that
697 // we are done before sending us to the background.
698 try {
699 Process.setProcessGroup(Process.myPid(), group);
700 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800701 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700702 }
703 }
Bob Leee5408332009-09-04 18:31:17 -0700704
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700705 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
706 Debug.getMemoryInfo(outInfo);
707 }
Bob Leee5408332009-09-04 18:31:17 -0700708
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700709 public void dispatchPackageBroadcast(int cmd, String[] packages) {
710 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
711 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700712
713 public void scheduleCrash(String msg) {
714 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
715 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700716
Dianne Hackborn30d71892010-12-11 10:37:55 -0800717 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
718 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700719 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700720 try {
721 data.fd = ParcelFileDescriptor.dup(fd);
722 data.token = activitytoken;
723 data.prefix = prefix;
724 data.args = args;
725 queueOrSendMessage(H.DUMP_ACTIVITY, data);
726 } catch (IOException e) {
727 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700728 }
729 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 @Override
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700732 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, String[] args) {
733 FileOutputStream fout = new FileOutputStream(fd);
734 PrintWriter pw = new PrintWriter(fout);
735 try {
736 return dumpMemInfo(fd, pw, args);
737 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700738 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700739 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700740 }
741
742 private Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, PrintWriter pw, String[] args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 long nativeMax = Debug.getNativeHeapSize() / 1024;
744 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
745 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
746
747 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
748 Debug.getMemoryInfo(memInfo);
749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 Runtime runtime = Runtime.getRuntime();
751
752 long dalvikMax = runtime.totalMemory() / 1024;
753 long dalvikFree = runtime.freeMemory() / 1024;
754 long dalvikAllocated = dalvikMax - dalvikFree;
755 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Joe Onoratoc6cc0f82011-04-12 11:53:13 -0700756 long viewRootInstanceCount = ViewDebug.getViewAncestorInstanceCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700757 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
758 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 int globalAssetCount = AssetManager.getGlobalAssetCount();
760 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
761 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
762 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
763 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700764 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 long sqliteAllocated = SQLiteDebug.getHeapAllocatedSize() / 1024;
Vasu Noric3849202010-03-09 10:47:25 -0800766 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 // Check to see if we were called by checkin server. If so, print terse format.
769 boolean doCheckinFormat = false;
770 if (args != null) {
771 for (String arg : args) {
772 if ("-c".equals(arg)) doCheckinFormat = true;
773 }
774 }
Bob Leee5408332009-09-04 18:31:17 -0700775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 // For checkin, we print one long comma-separated list of values
777 if (doCheckinFormat) {
778 // NOTE: if you change anything significant below, also consider changing
779 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700780 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 // Header
784 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
785 pw.print(Process.myPid()); pw.print(',');
786 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 // Heap info - max
789 pw.print(nativeMax); pw.print(',');
790 pw.print(dalvikMax); pw.print(',');
791 pw.print("N/A,");
792 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 // Heap info - allocated
795 pw.print(nativeAllocated); pw.print(',');
796 pw.print(dalvikAllocated); pw.print(',');
797 pw.print("N/A,");
798 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 // Heap info - free
801 pw.print(nativeFree); pw.print(',');
802 pw.print(dalvikFree); pw.print(',');
803 pw.print("N/A,");
804 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 // Heap info - proportional set size
807 pw.print(memInfo.nativePss); pw.print(',');
808 pw.print(memInfo.dalvikPss); pw.print(',');
809 pw.print(memInfo.otherPss); pw.print(',');
810 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700813 pw.print(memInfo.nativeSharedDirty); pw.print(',');
814 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
815 pw.print(memInfo.otherSharedDirty); pw.print(',');
816 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
817 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700820 pw.print(memInfo.nativePrivateDirty); pw.print(',');
821 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
822 pw.print(memInfo.otherPrivateDirty); pw.print(',');
823 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
824 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 // Object counts
827 pw.print(viewInstanceCount); pw.print(',');
828 pw.print(viewRootInstanceCount); pw.print(',');
829 pw.print(appContextInstanceCount); pw.print(',');
830 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 pw.print(globalAssetCount); pw.print(',');
833 pw.print(globalAssetManagerCount); pw.print(',');
834 pw.print(binderLocalObjectCount); pw.print(',');
835 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 pw.print(binderDeathObjectCount); pw.print(',');
838 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 // SQL
841 pw.print(sqliteAllocated); pw.print(',');
Vasu Noric3849202010-03-09 10:47:25 -0800842 pw.print(stats.memoryUsed / 1024); pw.print(',');
843 pw.print(stats.pageCacheOverflo / 1024); pw.print(',');
844 pw.print(stats.largestMemAlloc / 1024); pw.print(',');
845 for (int i = 0; i < stats.dbStats.size(); i++) {
846 DbStats dbStats = stats.dbStats.get(i);
847 printRow(pw, DB_INFO_FORMAT, dbStats.pageSize, dbStats.dbSize,
Vasu Nori90a367262010-04-12 12:49:09 -0700848 dbStats.lookaside, dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -0800849 pw.print(',');
850 }
Bob Leee5408332009-09-04 18:31:17 -0700851
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700852 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 }
Bob Leee5408332009-09-04 18:31:17 -0700854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700856 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
857 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
858 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
859 "------");
860 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
861 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
862 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
863 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700865 int otherPss = memInfo.otherPss;
866 int otherSharedDirty = memInfo.otherSharedDirty;
867 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700869 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
870 printRow(pw, HEAP_COLUMN, memInfo.getOtherLabel(i),
871 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
872 memInfo.getOtherPrivateDirty(i), "", "", "");
873 otherPss -= memInfo.getOtherPss(i);
874 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
875 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
876 }
877
878 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
879 otherPrivateDirty, "", "", "");
880 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
881 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
882 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
883 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884
885 pw.println(" ");
886 pw.println(" Objects");
Joe Onoratoc6cc0f82011-04-12 11:53:13 -0700887 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewAncestors:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 viewRootInstanceCount);
889
890 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
891 "Activities:", activityInstanceCount);
892
893 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
894 "AssetManagers:", globalAssetManagerCount);
895
896 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
897 "Proxy Binders:", binderProxyObjectCount);
898 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
899
900 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -0700901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 // SQLite mem info
903 pw.println(" ");
904 pw.println(" SQL");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700905 printRow(pw, TWO_COUNT_COLUMNS_DB, "heap:", sqliteAllocated, "MEMORY_USED:",
Vasu Noric3849202010-03-09 10:47:25 -0800906 stats.memoryUsed / 1024);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700907 printRow(pw, TWO_COUNT_COLUMNS_DB, "PAGECACHE_OVERFLOW:",
908 stats.pageCacheOverflo / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800909 pw.println(" ");
910 int N = stats.dbStats.size();
911 if (N > 0) {
912 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700913 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
914 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -0800915 for (int i = 0; i < N; i++) {
916 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700917 printRow(pw, DB_INFO_FORMAT,
918 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
919 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
920 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
921 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -0800922 }
923 }
Bob Leee5408332009-09-04 18:31:17 -0700924
Dianne Hackborn82e1ee92009-08-11 18:56:41 -0700925 // Asset details.
926 String assetAlloc = AssetManager.getAssetAllocations();
927 if (assetAlloc != null) {
928 pw.println(" ");
929 pw.println(" Asset Allocations");
930 pw.print(assetAlloc);
931 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700932
933 return memInfo;
934 }
935
936 @Override
937 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
938 dumpGraphicsInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 }
940
941 private void printRow(PrintWriter pw, String format, Object...objs) {
942 pw.println(String.format(format, objs));
943 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800944
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800945 public void setCoreSettings(Bundle coreSettings) {
946 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800947 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400948
949 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
950 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
951 ucd.pkg = pkg;
952 ucd.info = info;
953 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
954 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700955
956 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -0700957 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 }
960
961 private final class H extends Handler {
962 public static final int LAUNCH_ACTIVITY = 100;
963 public static final int PAUSE_ACTIVITY = 101;
964 public static final int PAUSE_ACTIVITY_FINISHING= 102;
965 public static final int STOP_ACTIVITY_SHOW = 103;
966 public static final int STOP_ACTIVITY_HIDE = 104;
967 public static final int SHOW_WINDOW = 105;
968 public static final int HIDE_WINDOW = 106;
969 public static final int RESUME_ACTIVITY = 107;
970 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -0700971 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 public static final int BIND_APPLICATION = 110;
973 public static final int EXIT_APPLICATION = 111;
974 public static final int NEW_INTENT = 112;
975 public static final int RECEIVER = 113;
976 public static final int CREATE_SERVICE = 114;
977 public static final int SERVICE_ARGS = 115;
978 public static final int STOP_SERVICE = 116;
979 public static final int REQUEST_THUMBNAIL = 117;
980 public static final int CONFIGURATION_CHANGED = 118;
981 public static final int CLEAN_UP_CONTEXT = 119;
982 public static final int GC_WHEN_IDLE = 120;
983 public static final int BIND_SERVICE = 121;
984 public static final int UNBIND_SERVICE = 122;
985 public static final int DUMP_SERVICE = 123;
986 public static final int LOW_MEMORY = 124;
987 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
988 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800989 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -0700990 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -0700991 public static final int DESTROY_BACKUP_AGENT = 129;
992 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700993 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800994 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700995 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700996 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -0700997 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700998 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800999 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001000 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001001 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001002 public static final int TRIM_MEMORY = 140;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001004 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 switch (code) {
1006 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1007 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1008 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1009 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1010 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1011 case SHOW_WINDOW: return "SHOW_WINDOW";
1012 case HIDE_WINDOW: return "HIDE_WINDOW";
1013 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1014 case SEND_RESULT: return "SEND_RESULT";
1015 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1016 case BIND_APPLICATION: return "BIND_APPLICATION";
1017 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1018 case NEW_INTENT: return "NEW_INTENT";
1019 case RECEIVER: return "RECEIVER";
1020 case CREATE_SERVICE: return "CREATE_SERVICE";
1021 case SERVICE_ARGS: return "SERVICE_ARGS";
1022 case STOP_SERVICE: return "STOP_SERVICE";
1023 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1024 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1025 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1026 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1027 case BIND_SERVICE: return "BIND_SERVICE";
1028 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1029 case DUMP_SERVICE: return "DUMP_SERVICE";
1030 case LOW_MEMORY: return "LOW_MEMORY";
1031 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1032 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001033 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001034 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1035 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001036 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001037 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001038 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001039 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001040 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001041 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001042 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001043 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001044 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001045 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001046 case TRIM_MEMORY: return "TRIM_MEMORY";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 }
1048 }
1049 return "(unknown)";
1050 }
1051 public void handleMessage(Message msg) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001052 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 switch (msg.what) {
1054 case LAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001055 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056
1057 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001058 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001059 handleLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 } break;
1061 case RELAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001062 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001063 handleRelaunchActivity(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 } break;
1065 case PAUSE_ACTIVITY:
1066 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001067 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 break;
1069 case PAUSE_ACTIVITY_FINISHING:
1070 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
1071 break;
1072 case STOP_ACTIVITY_SHOW:
1073 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
1074 break;
1075 case STOP_ACTIVITY_HIDE:
1076 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
1077 break;
1078 case SHOW_WINDOW:
1079 handleWindowVisibility((IBinder)msg.obj, true);
1080 break;
1081 case HIDE_WINDOW:
1082 handleWindowVisibility((IBinder)msg.obj, false);
1083 break;
1084 case RESUME_ACTIVITY:
1085 handleResumeActivity((IBinder)msg.obj, true,
1086 msg.arg1 != 0);
1087 break;
1088 case SEND_RESULT:
1089 handleSendResult((ResultData)msg.obj);
1090 break;
1091 case DESTROY_ACTIVITY:
1092 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1093 msg.arg2, false);
1094 break;
1095 case BIND_APPLICATION:
1096 AppBindData data = (AppBindData)msg.obj;
1097 handleBindApplication(data);
1098 break;
1099 case EXIT_APPLICATION:
1100 if (mInitialApplication != null) {
1101 mInitialApplication.onTerminate();
1102 }
1103 Looper.myLooper().quit();
1104 break;
1105 case NEW_INTENT:
1106 handleNewIntent((NewIntentData)msg.obj);
1107 break;
1108 case RECEIVER:
1109 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001110 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 break;
1112 case CREATE_SERVICE:
1113 handleCreateService((CreateServiceData)msg.obj);
1114 break;
1115 case BIND_SERVICE:
1116 handleBindService((BindServiceData)msg.obj);
1117 break;
1118 case UNBIND_SERVICE:
1119 handleUnbindService((BindServiceData)msg.obj);
1120 break;
1121 case SERVICE_ARGS:
1122 handleServiceArgs((ServiceArgsData)msg.obj);
1123 break;
1124 case STOP_SERVICE:
1125 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001126 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 break;
1128 case REQUEST_THUMBNAIL:
1129 handleRequestThumbnail((IBinder)msg.obj);
1130 break;
1131 case CONFIGURATION_CHANGED:
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001132 handleConfigurationChanged((Configuration)msg.obj, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 break;
1134 case CLEAN_UP_CONTEXT:
1135 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1136 cci.context.performFinalCleanup(cci.who, cci.what);
1137 break;
1138 case GC_WHEN_IDLE:
1139 scheduleGcIdler();
1140 break;
1141 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001142 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 break;
1144 case LOW_MEMORY:
1145 handleLowMemory();
1146 break;
1147 case ACTIVITY_CONFIGURATION_CHANGED:
1148 handleActivityConfigurationChanged((IBinder)msg.obj);
1149 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001150 case PROFILER_CONTROL:
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001151 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001152 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001153 case CREATE_BACKUP_AGENT:
1154 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
1155 break;
1156 case DESTROY_BACKUP_AGENT:
1157 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
1158 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001159 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001160 Process.killProcess(Process.myPid());
1161 break;
1162 case REMOVE_PROVIDER:
1163 completeRemoveProvider((IContentProvider)msg.obj);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001164 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001165 case ENABLE_JIT:
1166 ensureJitEnabled();
1167 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001168 case DISPATCH_PACKAGE_BROADCAST:
1169 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
1170 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001171 case SCHEDULE_CRASH:
1172 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001173 case DUMP_HEAP:
1174 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1175 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001176 case DUMP_ACTIVITY:
1177 handleDumpActivity((DumpComponentInfo)msg.obj);
1178 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001179 case SLEEPING:
1180 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
1181 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001182 case SET_CORE_SETTINGS:
1183 handleSetCoreSettings((Bundle) msg.obj);
1184 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001185 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1186 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001187 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001188 case TRIM_MEMORY:
1189 handleTrimMemory(msg.arg1);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001190 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07001192 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 }
Bob Leee5408332009-09-04 18:31:17 -07001194
Brian Carlstromed7e0072011-03-24 13:27:57 -07001195 private void maybeSnapshot() {
1196 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001197 // convert the *private* ActivityThread.PackageInfo to *public* known
1198 // android.content.pm.PackageInfo
1199 String packageName = mBoundApplication.info.mPackageName;
1200 android.content.pm.PackageInfo packageInfo = null;
1201 try {
1202 Context context = getSystemContext();
1203 if(context == null) {
1204 Log.e(TAG, "cannot get a valid context");
1205 return;
1206 }
1207 PackageManager pm = context.getPackageManager();
1208 if(pm == null) {
1209 Log.e(TAG, "cannot get a valid PackageManager");
1210 return;
1211 }
1212 packageInfo = pm.getPackageInfo(
1213 packageName, PackageManager.GET_ACTIVITIES);
1214 } catch (NameNotFoundException e) {
1215 Log.e(TAG, "cannot get package info for " + packageName, e);
1216 }
1217 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001218 }
1219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 }
1221
1222 private final class Idler implements MessageQueue.IdleHandler {
1223 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001224 ActivityClientRecord a = mNewActivities;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 if (a != null) {
1226 mNewActivities = null;
1227 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001228 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001230 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 TAG, "Reporting idle of " + a +
1232 " finished=" +
1233 (a.activity != null ? a.activity.mFinished : false));
1234 if (a.activity != null && !a.activity.mFinished) {
1235 try {
Dianne Hackborne88846e2009-09-30 21:34:25 -07001236 am.activityIdle(a.token, a.createdConfig);
1237 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 } catch (RemoteException ex) {
1239 }
1240 }
1241 prev = a;
1242 a = a.nextIdle;
1243 prev.nextIdle = null;
1244 } while (a != null);
1245 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001246 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 return false;
1248 }
1249 }
1250
1251 final class GcIdler implements MessageQueue.IdleHandler {
1252 public final boolean queueIdle() {
1253 doGcIfNeeded();
1254 return false;
1255 }
1256 }
1257
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001258 private final static class ResourcesKey {
1259 final private String mResDir;
1260 final private float mScale;
1261 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001262
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001263 ResourcesKey(String resDir, float scale) {
1264 mResDir = resDir;
1265 mScale = scale;
1266 mHash = mResDir.hashCode() << 2 + (int) (mScale * 2);
1267 }
Bob Leee5408332009-09-04 18:31:17 -07001268
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001269 @Override
1270 public int hashCode() {
1271 return mHash;
1272 }
1273
1274 @Override
1275 public boolean equals(Object obj) {
1276 if (!(obj instanceof ResourcesKey)) {
1277 return false;
1278 }
1279 ResourcesKey peer = (ResourcesKey) obj;
1280 return mResDir.equals(peer.mResDir) && mScale == peer.mScale;
1281 }
1282 }
1283
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001284 public static final ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001285 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001288 public static final String currentPackageName() {
1289 ActivityThread am = currentActivityThread();
1290 return (am != null && am.mBoundApplication != null)
1291 ? am.mBoundApplication.processName : null;
1292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001294 public static final Application currentApplication() {
1295 ActivityThread am = currentActivityThread();
1296 return am != null ? am.mInitialApplication : null;
1297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001299 public static IPackageManager getPackageManager() {
1300 if (sPackageManager != null) {
1301 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1302 return sPackageManager;
1303 }
1304 IBinder b = ServiceManager.getService("package");
1305 //Slog.v("PackageManager", "default service binder = " + b);
1306 sPackageManager = IPackageManager.Stub.asInterface(b);
1307 //Slog.v("PackageManager", "default service = " + sPackageManager);
1308 return sPackageManager;
1309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001311 DisplayMetrics getDisplayMetricsLocked(CompatibilityInfo ci, boolean forceUpdate) {
1312 DisplayMetrics dm = mDisplayMetrics.get(ci);
1313 if (dm != null && !forceUpdate) {
1314 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001315 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001316 if (dm == null) {
1317 dm = new DisplayMetrics();
1318 mDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001319 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001320 Display d = WindowManagerImpl.getDefault(ci).getDefaultDisplay();
1321 d.getMetrics(dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001322 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1323 // + metrics.heightPixels + " den=" + metrics.density
1324 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001325 return dm;
1326 }
1327
1328 static Configuration applyConfigCompat(Configuration config, CompatibilityInfo compat) {
1329 if (config == null) {
1330 return null;
1331 }
1332 if (compat != null && !compat.supportsScreen()) {
1333 config = new Configuration(config);
1334 compat.applyToConfiguration(config);
1335 }
1336 return config;
1337 }
1338
1339 private final Configuration mMainThreadConfig = new Configuration();
1340 Configuration applyConfigCompatMainThread(Configuration config, CompatibilityInfo compat) {
1341 if (config == null) {
1342 return null;
1343 }
1344 if (compat != null && !compat.supportsScreen()) {
1345 mMainThreadConfig.setTo(config);
1346 config = mMainThreadConfig;
1347 compat.applyToConfiguration(config);
1348 }
1349 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001350 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001352 /**
1353 * Creates the top level Resources for applications with the given compatibility info.
1354 *
1355 * @param resDir the resource directory.
1356 * @param compInfo the compability info. It will use the default compatibility info when it's
1357 * null.
1358 */
1359 Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) {
1360 ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale);
1361 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001363 // Resources is app scale dependent.
1364 if (false) {
1365 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1366 + compInfo.applicationScale);
1367 }
1368 WeakReference<Resources> wr = mActiveResources.get(key);
1369 r = wr != null ? wr.get() : null;
1370 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1371 if (r != null && r.getAssets().isUpToDate()) {
1372 if (false) {
1373 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1374 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1375 }
1376 return r;
1377 }
1378 }
1379
1380 //if (r != null) {
1381 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1382 // + r + " " + resDir);
1383 //}
1384
1385 AssetManager assets = new AssetManager();
1386 if (assets.addAssetPath(resDir) == 0) {
1387 return null;
1388 }
1389
1390 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001391 DisplayMetrics metrics = getDisplayMetricsLocked(compInfo, false);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001392 r = new Resources(assets, metrics, getConfiguration(), compInfo);
1393 if (false) {
1394 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1395 + r.getConfiguration() + " appScale="
1396 + r.getCompatibilityInfo().applicationScale);
1397 }
1398
1399 synchronized (mPackages) {
1400 WeakReference<Resources> wr = mActiveResources.get(key);
1401 Resources existing = wr != null ? wr.get() : null;
1402 if (existing != null && existing.getAssets().isUpToDate()) {
1403 // Someone else already created the resources while we were
1404 // unlocked; go ahead and use theirs.
1405 r.getAssets().close();
1406 return existing;
1407 }
1408
1409 // XXX need to remove entries when weak references go away
1410 mActiveResources.put(key, new WeakReference<Resources>(r));
1411 return r;
1412 }
1413 }
1414
1415 /**
1416 * Creates the top level resources for the given package.
1417 */
1418 Resources getTopLevelResources(String resDir, LoadedApk pkgInfo) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001419 return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001420 }
1421
1422 final Handler getHandler() {
1423 return mH;
1424 }
1425
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001426 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1427 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001428 synchronized (mPackages) {
1429 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1431 ref = mPackages.get(packageName);
1432 } else {
1433 ref = mResourcePackages.get(packageName);
1434 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001435 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001436 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001437 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1438 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 if (packageInfo != null && (packageInfo.mResources == null
1440 || packageInfo.mResources.getAssets().isUpToDate())) {
1441 if (packageInfo.isSecurityViolation()
1442 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1443 throw new SecurityException(
1444 "Requesting code from " + packageName
1445 + " to be run in process "
1446 + mBoundApplication.processName
1447 + "/" + mBoundApplication.appInfo.uid);
1448 }
1449 return packageInfo;
1450 }
1451 }
1452
1453 ApplicationInfo ai = null;
1454 try {
1455 ai = getPackageManager().getApplicationInfo(packageName,
1456 PackageManager.GET_SHARED_LIBRARY_FILES);
1457 } catch (RemoteException e) {
1458 }
1459
1460 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001461 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 }
1463
1464 return null;
1465 }
1466
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001467 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1468 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1470 boolean securityViolation = includeCode && ai.uid != 0
1471 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
1472 ? ai.uid != mBoundApplication.appInfo.uid : true);
1473 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1474 |Context.CONTEXT_IGNORE_SECURITY))
1475 == Context.CONTEXT_INCLUDE_CODE) {
1476 if (securityViolation) {
1477 String msg = "Requesting code from " + ai.packageName
1478 + " (with uid " + ai.uid + ")";
1479 if (mBoundApplication != null) {
1480 msg = msg + " to be run in process "
1481 + mBoundApplication.processName + " (with uid "
1482 + mBoundApplication.appInfo.uid + ")";
1483 }
1484 throw new SecurityException(msg);
1485 }
1486 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001487 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 }
1489
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001490 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1491 CompatibilityInfo compatInfo) {
1492 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 }
1494
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001495 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1496 synchronized (mPackages) {
1497 WeakReference<LoadedApk> ref;
1498 if (includeCode) {
1499 ref = mPackages.get(packageName);
1500 } else {
1501 ref = mResourcePackages.get(packageName);
1502 }
1503 return ref != null ? ref.get() : null;
1504 }
1505 }
1506
1507 private final LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1509 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001510 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 if (includeCode) {
1512 ref = mPackages.get(aInfo.packageName);
1513 } else {
1514 ref = mResourcePackages.get(aInfo.packageName);
1515 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001516 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 if (packageInfo == null || (packageInfo.mResources != null
1518 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001519 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 : "Loading resource-only package ") + aInfo.packageName
1521 + " (in " + (mBoundApplication != null
1522 ? mBoundApplication.processName : null)
1523 + ")");
1524 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001525 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 securityViolation, includeCode &&
1527 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1528 if (includeCode) {
1529 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001530 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 } else {
1532 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001533 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 }
1535 }
1536 return packageInfo;
1537 }
1538 }
1539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 ActivityThread() {
1541 }
1542
1543 public ApplicationThread getApplicationThread()
1544 {
1545 return mAppThread;
1546 }
1547
1548 public Instrumentation getInstrumentation()
1549 {
1550 return mInstrumentation;
1551 }
1552
1553 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001554 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 }
1556
1557 public boolean isProfiling() {
1558 return mBoundApplication != null && mBoundApplication.profileFile != null;
1559 }
1560
1561 public String getProfileFilePath() {
1562 return mBoundApplication.profileFile;
1563 }
1564
1565 public Looper getLooper() {
1566 return mLooper;
1567 }
1568
1569 public Application getApplication() {
1570 return mInitialApplication;
1571 }
Bob Leee5408332009-09-04 18:31:17 -07001572
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001573 public String getProcessName() {
1574 return mBoundApplication.processName;
1575 }
Bob Leee5408332009-09-04 18:31:17 -07001576
Dianne Hackborn21556372010-02-04 16:34:40 -08001577 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 synchronized (this) {
1579 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001580 ContextImpl context =
1581 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001582 LoadedApk info = new LoadedApk(this, "android", context, null,
1583 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 context.init(info, null, this);
1585 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001586 getConfiguration(), getDisplayMetricsLocked(
1587 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001589 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 // + ": " + context.getResources().getConfiguration());
1591 }
1592 }
1593 return mSystemContext;
1594 }
1595
Mike Cleron432b7132009-09-24 15:28:29 -07001596 public void installSystemApplicationInfo(ApplicationInfo info) {
1597 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001598 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001599 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001600 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Mike Cleron432b7132009-09-24 15:28:29 -07001601 }
1602 }
1603
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001604 void ensureJitEnabled() {
1605 if (!mJitEnabled) {
1606 mJitEnabled = true;
1607 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1608 }
1609 }
1610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 void scheduleGcIdler() {
1612 if (!mGcIdlerScheduled) {
1613 mGcIdlerScheduled = true;
1614 Looper.myQueue().addIdleHandler(mGcIdler);
1615 }
1616 mH.removeMessages(H.GC_WHEN_IDLE);
1617 }
1618
1619 void unscheduleGcIdler() {
1620 if (mGcIdlerScheduled) {
1621 mGcIdlerScheduled = false;
1622 Looper.myQueue().removeIdleHandler(mGcIdler);
1623 }
1624 mH.removeMessages(H.GC_WHEN_IDLE);
1625 }
1626
1627 void doGcIfNeeded() {
1628 mGcIdlerScheduled = false;
1629 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001630 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 // + "m now=" + now);
1632 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001633 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 BinderInternal.forceGc("bg");
1635 }
1636 }
1637
Jeff Hamilton52d32032011-01-08 15:31:26 -06001638 public void registerOnActivityPausedListener(Activity activity,
1639 OnActivityPausedListener listener) {
1640 synchronized (mOnPauseListeners) {
1641 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1642 if (list == null) {
1643 list = new ArrayList<OnActivityPausedListener>();
1644 mOnPauseListeners.put(activity, list);
1645 }
1646 list.add(listener);
1647 }
1648 }
1649
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001650 public void unregisterOnActivityPausedListener(Activity activity,
1651 OnActivityPausedListener listener) {
1652 synchronized (mOnPauseListeners) {
1653 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1654 if (list != null) {
1655 list.remove(listener);
1656 }
1657 }
1658 }
1659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 public final ActivityInfo resolveActivityInfo(Intent intent) {
1661 ActivityInfo aInfo = intent.resolveActivityInfo(
1662 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1663 if (aInfo == null) {
1664 // Throw an exception.
1665 Instrumentation.checkStartActivityResult(
1666 IActivityManager.START_CLASS_NOT_FOUND, intent);
1667 }
1668 return aInfo;
1669 }
Bob Leee5408332009-09-04 18:31:17 -07001670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001673 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001674 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001676 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 r.intent = intent;
1678 r.state = state;
1679 r.parent = parent;
1680 r.embeddedID = id;
1681 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001682 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 if (localLOGV) {
1684 ComponentName compname = intent.getComponent();
1685 String name;
1686 if (compname != null) {
1687 name = compname.toShortString();
1688 } else {
1689 name = "(Intent " + intent + ").getComponent() returned null";
1690 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001691 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 + ", comp=" + name
1693 + ", token=" + token);
1694 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001695 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 }
1697
1698 public final Activity getActivity(IBinder token) {
1699 return mActivities.get(token).activity;
1700 }
1701
1702 public final void sendActivityResult(
1703 IBinder token, String id, int requestCode,
1704 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001705 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001706 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1708 list.add(new ResultInfo(id, requestCode, resultCode, data));
1709 mAppThread.scheduleSendResult(token, list);
1710 }
1711
1712 // if the thread hasn't started yet, we don't have the handler, so just
1713 // save the messages until we're ready.
1714 private final void queueOrSendMessage(int what, Object obj) {
1715 queueOrSendMessage(what, obj, 0, 0);
1716 }
1717
1718 private final void queueOrSendMessage(int what, Object obj, int arg1) {
1719 queueOrSendMessage(what, obj, arg1, 0);
1720 }
1721
1722 private final void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
1723 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001724 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1726 + ": " + arg1 + " / " + obj);
1727 Message msg = Message.obtain();
1728 msg.what = what;
1729 msg.obj = obj;
1730 msg.arg1 = arg1;
1731 msg.arg2 = arg2;
1732 mH.sendMessage(msg);
1733 }
1734 }
1735
Dianne Hackborn21556372010-02-04 16:34:40 -08001736 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 String what) {
1738 ContextCleanupInfo cci = new ContextCleanupInfo();
1739 cci.context = context;
1740 cci.who = who;
1741 cci.what = what;
1742 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1743 }
1744
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001745 private final Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1747
1748 ActivityInfo aInfo = r.activityInfo;
1749 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001750 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 Context.CONTEXT_INCLUDE_CODE);
1752 }
Bob Leee5408332009-09-04 18:31:17 -07001753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 ComponentName component = r.intent.getComponent();
1755 if (component == null) {
1756 component = r.intent.resolveActivity(
1757 mInitialApplication.getPackageManager());
1758 r.intent.setComponent(component);
1759 }
1760
1761 if (r.activityInfo.targetActivity != null) {
1762 component = new ComponentName(r.activityInfo.packageName,
1763 r.activityInfo.targetActivity);
1764 }
1765
1766 Activity activity = null;
1767 try {
1768 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
1769 activity = mInstrumentation.newActivity(
1770 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08001771 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 r.intent.setExtrasClassLoader(cl);
1773 if (r.state != null) {
1774 r.state.setClassLoader(cl);
1775 }
1776 } catch (Exception e) {
1777 if (!mInstrumentation.onException(activity, e)) {
1778 throw new RuntimeException(
1779 "Unable to instantiate activity " + component
1780 + ": " + e.toString(), e);
1781 }
1782 }
1783
1784 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001785 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001786
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001787 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
1788 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 TAG, r + ": app=" + app
1790 + ", appName=" + app.getPackageName()
1791 + ", pkg=" + r.packageInfo.getPackageName()
1792 + ", comp=" + r.intent.getComponent().toShortString()
1793 + ", dir=" + r.packageInfo.getAppDir());
1794
1795 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001796 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 appContext.init(r.packageInfo, r.token, this);
1798 appContext.setOuterContext(activity);
1799 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001800 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001801 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07001802 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001803 activity.attach(appContext, this, getInstrumentation(), r.token,
1804 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001805 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07001806
Christopher Tateb70f3df2009-04-07 16:07:59 -07001807 if (customIntent != null) {
1808 activity.mIntent = customIntent;
1809 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001810 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 activity.mStartedActivity = false;
1812 int theme = r.activityInfo.getThemeResource();
1813 if (theme != 0) {
1814 activity.setTheme(theme);
1815 }
1816
1817 activity.mCalled = false;
1818 mInstrumentation.callActivityOnCreate(activity, r.state);
1819 if (!activity.mCalled) {
1820 throw new SuperNotCalledException(
1821 "Activity " + r.intent.getComponent().toShortString() +
1822 " did not call through to super.onCreate()");
1823 }
1824 r.activity = activity;
1825 r.stopped = true;
1826 if (!r.activity.mFinished) {
1827 activity.performStart();
1828 r.stopped = false;
1829 }
1830 if (!r.activity.mFinished) {
1831 if (r.state != null) {
1832 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
1833 }
1834 }
1835 if (!r.activity.mFinished) {
1836 activity.mCalled = false;
1837 mInstrumentation.callActivityOnPostCreate(activity, r.state);
1838 if (!activity.mCalled) {
1839 throw new SuperNotCalledException(
1840 "Activity " + r.intent.getComponent().toShortString() +
1841 " did not call through to super.onPostCreate()");
1842 }
1843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 }
1845 r.paused = true;
1846
1847 mActivities.put(r.token, r);
1848
1849 } catch (SuperNotCalledException e) {
1850 throw e;
1851
1852 } catch (Exception e) {
1853 if (!mInstrumentation.onException(activity, e)) {
1854 throw new RuntimeException(
1855 "Unable to start activity " + component
1856 + ": " + e.toString(), e);
1857 }
1858 }
1859
1860 return activity;
1861 }
1862
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001863 private final void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 // If we are getting ready to gc after going to the background, well
1865 // we are back active so skip it.
1866 unscheduleGcIdler();
1867
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001868 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001870 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871
1872 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08001873 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001874 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 handleResumeActivity(r.token, false, r.isForward);
1876
1877 if (!r.activity.mFinished && r.startsNotResumed) {
1878 // The activity manager actually wants this one to start out
1879 // paused, because it needs to be visible but isn't in the
1880 // foreground. We accomplish this by going through the
1881 // normal startup (because activities expect to go through
1882 // onResume() the first time they run, before their window
1883 // is displayed), and then pausing it. However, in this case
1884 // we do -not- need to do the full pause cycle (of freezing
1885 // and such) because the activity manager assumes it can just
1886 // retain the current state it has.
1887 try {
1888 r.activity.mCalled = false;
1889 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001890 // We need to keep around the original state, in case
1891 // we need to be created again.
1892 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 if (!r.activity.mCalled) {
1894 throw new SuperNotCalledException(
1895 "Activity " + r.intent.getComponent().toShortString() +
1896 " did not call through to super.onPause()");
1897 }
1898
1899 } catch (SuperNotCalledException e) {
1900 throw e;
1901
1902 } catch (Exception e) {
1903 if (!mInstrumentation.onException(r.activity, e)) {
1904 throw new RuntimeException(
1905 "Unable to pause activity "
1906 + r.intent.getComponent().toShortString()
1907 + ": " + e.toString(), e);
1908 }
1909 }
1910 r.paused = true;
1911 }
1912 } else {
1913 // If there was an error, for any reason, tell the activity
1914 // manager to stop us.
1915 try {
1916 ActivityManagerNative.getDefault()
1917 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
1918 } catch (RemoteException ex) {
1919 }
1920 }
1921 }
1922
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001923 private final void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 List<Intent> intents) {
1925 final int N = intents.size();
1926 for (int i=0; i<N; i++) {
1927 Intent intent = intents.get(i);
1928 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001929 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
1931 }
1932 }
1933
1934 public final void performNewIntents(IBinder token,
1935 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001936 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 if (r != null) {
1938 final boolean resumed = !r.paused;
1939 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001940 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 mInstrumentation.callActivityOnPause(r.activity);
1942 }
1943 deliverNewIntents(r, intents);
1944 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001945 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001946 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 }
1948 }
1949 }
Bob Leee5408332009-09-04 18:31:17 -07001950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 private final void handleNewIntent(NewIntentData data) {
1952 performNewIntents(data.token, data.intents);
1953 }
1954
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07001955 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
1956
1957 /**
1958 * Return the Intent that's currently being handled by a
1959 * BroadcastReceiver on this thread, or null if none.
1960 * @hide
1961 */
1962 public static Intent getIntentBeingBroadcast() {
1963 return sCurrentBroadcastIntent.get();
1964 }
1965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 private final void handleReceiver(ReceiverData data) {
1967 // If we are getting ready to gc after going to the background, well
1968 // we are back active so skip it.
1969 unscheduleGcIdler();
1970
1971 String component = data.intent.getComponent().getClassName();
1972
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001973 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001974 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975
1976 IActivityManager mgr = ActivityManagerNative.getDefault();
1977
1978 BroadcastReceiver receiver = null;
1979 try {
1980 java.lang.ClassLoader cl = packageInfo.getClassLoader();
1981 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07001982 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
1984 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07001985 if (DEBUG_BROADCAST) Slog.i(TAG,
1986 "Finishing failed broadcast to " + data.intent.getComponent());
1987 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 throw new RuntimeException(
1989 "Unable to instantiate receiver " + component
1990 + ": " + e.toString(), e);
1991 }
1992
1993 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001994 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001995
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001996 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 TAG, "Performing receive of " + data.intent
1998 + ": app=" + app
1999 + ", appName=" + app.getPackageName()
2000 + ", pkg=" + packageInfo.getPackageName()
2001 + ", comp=" + data.intent.getComponent().toShortString()
2002 + ", dir=" + packageInfo.getAppDir());
2003
Dianne Hackborn21556372010-02-04 16:34:40 -08002004 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002005 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002006 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 receiver.onReceive(context.getReceiverRestrictedContext(),
2008 data.intent);
2009 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002010 if (DEBUG_BROADCAST) Slog.i(TAG,
2011 "Finishing failed broadcast to " + data.intent.getComponent());
2012 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 if (!mInstrumentation.onException(receiver, e)) {
2014 throw new RuntimeException(
2015 "Unable to start receiver " + component
2016 + ": " + e.toString(), e);
2017 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002018 } finally {
2019 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 }
2021
Dianne Hackborne829fef2010-10-26 17:44:01 -07002022 if (receiver.getPendingResult() != null) {
2023 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 }
2025 }
2026
Christopher Tate181fafa2009-05-14 11:12:14 -07002027 // Instantiate a BackupAgent and tell it that it's alive
2028 private final void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002029 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002030
2031 // no longer idle; we have backup work to do
2032 unscheduleGcIdler();
2033
2034 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002035 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002036 String packageName = packageInfo.mPackageName;
2037 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002038 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002039 + " already exists");
2040 return;
2041 }
Bob Leee5408332009-09-04 18:31:17 -07002042
Christopher Tate181fafa2009-05-14 11:12:14 -07002043 BackupAgent agent = null;
2044 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002045
Christopher Tate79ec80d2011-06-24 14:58:49 -07002046 // full backup operation but no app-supplied agent? use the default implementation
2047 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2048 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002049 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002050 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002051
Christopher Tate181fafa2009-05-14 11:12:14 -07002052 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002053 IBinder binder = null;
2054 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002055 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2056
Christopher Tated1475e02009-07-09 15:36:17 -07002057 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002058 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002059
2060 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002061 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002062 context.init(packageInfo, null, this);
2063 context.setOuterContext(agent);
2064 agent.attach(context);
2065
2066 agent.onCreate();
2067 binder = agent.onBind();
2068 mBackupAgents.put(packageName, agent);
2069 } catch (Exception e) {
2070 // If this is during restore, fail silently; otherwise go
2071 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002072 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002073 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2074 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002075 throw e;
2076 }
2077 // falling through with 'binder' still null
2078 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002079
2080 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002081 try {
2082 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2083 } catch (RemoteException e) {
2084 // nothing to do.
2085 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002086 } catch (Exception e) {
2087 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002088 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002089 }
2090 }
2091
2092 // Tear down a BackupAgent
2093 private final void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002094 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002095
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002096 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002097 String packageName = packageInfo.mPackageName;
2098 BackupAgent agent = mBackupAgents.get(packageName);
2099 if (agent != null) {
2100 try {
2101 agent.onDestroy();
2102 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002103 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002104 e.printStackTrace();
2105 }
2106 mBackupAgents.remove(packageName);
2107 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002108 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002109 }
2110 }
2111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 private final void handleCreateService(CreateServiceData data) {
2113 // If we are getting ready to gc after going to the background, well
2114 // we are back active so skip it.
2115 unscheduleGcIdler();
2116
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002117 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002118 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 Service service = null;
2120 try {
2121 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2122 service = (Service) cl.loadClass(data.info.name).newInstance();
2123 } catch (Exception e) {
2124 if (!mInstrumentation.onException(service, e)) {
2125 throw new RuntimeException(
2126 "Unable to instantiate service " + data.info.name
2127 + ": " + e.toString(), e);
2128 }
2129 }
2130
2131 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002132 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133
Dianne Hackborn21556372010-02-04 16:34:40 -08002134 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 context.init(packageInfo, null, this);
2136
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002137 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 context.setOuterContext(service);
2139 service.attach(context, this, data.info.name, data.token, app,
2140 ActivityManagerNative.getDefault());
2141 service.onCreate();
2142 mServices.put(data.token, service);
2143 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002144 ActivityManagerNative.getDefault().serviceDoneExecuting(
2145 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 } catch (RemoteException e) {
2147 // nothing to do.
2148 }
2149 } catch (Exception e) {
2150 if (!mInstrumentation.onException(service, e)) {
2151 throw new RuntimeException(
2152 "Unable to create service " + data.info.name
2153 + ": " + e.toString(), e);
2154 }
2155 }
2156 }
2157
2158 private final void handleBindService(BindServiceData data) {
2159 Service s = mServices.get(data.token);
2160 if (s != null) {
2161 try {
2162 data.intent.setExtrasClassLoader(s.getClassLoader());
2163 try {
2164 if (!data.rebind) {
2165 IBinder binder = s.onBind(data.intent);
2166 ActivityManagerNative.getDefault().publishService(
2167 data.token, data.intent, binder);
2168 } else {
2169 s.onRebind(data.intent);
2170 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002171 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002173 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 } catch (RemoteException ex) {
2175 }
2176 } catch (Exception e) {
2177 if (!mInstrumentation.onException(s, e)) {
2178 throw new RuntimeException(
2179 "Unable to bind to service " + s
2180 + " with " + data.intent + ": " + e.toString(), e);
2181 }
2182 }
2183 }
2184 }
2185
2186 private final void handleUnbindService(BindServiceData data) {
2187 Service s = mServices.get(data.token);
2188 if (s != null) {
2189 try {
2190 data.intent.setExtrasClassLoader(s.getClassLoader());
2191 boolean doRebind = s.onUnbind(data.intent);
2192 try {
2193 if (doRebind) {
2194 ActivityManagerNative.getDefault().unbindFinished(
2195 data.token, data.intent, doRebind);
2196 } else {
2197 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002198 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 }
2200 } catch (RemoteException ex) {
2201 }
2202 } catch (Exception e) {
2203 if (!mInstrumentation.onException(s, e)) {
2204 throw new RuntimeException(
2205 "Unable to unbind to service " + s
2206 + " with " + data.intent + ": " + e.toString(), e);
2207 }
2208 }
2209 }
2210 }
2211
Dianne Hackborn625ac272010-09-17 18:29:22 -07002212 private void handleDumpService(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002213 Service s = mServices.get(info.token);
2214 if (s != null) {
2215 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2216 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2217 pw.flush();
2218 try {
2219 info.fd.close();
2220 } catch (IOException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 }
2222 }
2223 }
2224
Dianne Hackborn625ac272010-09-17 18:29:22 -07002225 private void handleDumpActivity(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002226 ActivityClientRecord r = mActivities.get(info.token);
2227 if (r != null && r.activity != null) {
2228 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2229 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2230 pw.flush();
2231 try {
2232 info.fd.close();
2233 } catch (IOException e) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07002234 }
2235 }
2236 }
2237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 private final void handleServiceArgs(ServiceArgsData data) {
2239 Service s = mServices.get(data.token);
2240 if (s != null) {
2241 try {
2242 if (data.args != null) {
2243 data.args.setExtrasClassLoader(s.getClassLoader());
2244 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002245 int res;
2246 if (!data.taskRemoved) {
2247 res = s.onStartCommand(data.args, data.flags, data.startId);
2248 } else {
2249 s.onTaskRemoved(data.args);
2250 res = Service.START_TASK_REMOVED_COMPLETE;
2251 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002252
2253 QueuedWork.waitToFinish();
2254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002256 ActivityManagerNative.getDefault().serviceDoneExecuting(
2257 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 } catch (RemoteException e) {
2259 // nothing to do.
2260 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002261 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 } catch (Exception e) {
2263 if (!mInstrumentation.onException(s, e)) {
2264 throw new RuntimeException(
2265 "Unable to start service " + s
2266 + " with " + data.args + ": " + e.toString(), e);
2267 }
2268 }
2269 }
2270 }
2271
2272 private final void handleStopService(IBinder token) {
2273 Service s = mServices.remove(token);
2274 if (s != null) {
2275 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002276 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 s.onDestroy();
2278 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002279 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002281 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002283
2284 QueuedWork.waitToFinish();
2285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002287 ActivityManagerNative.getDefault().serviceDoneExecuting(
2288 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 } catch (RemoteException e) {
2290 // nothing to do.
2291 }
2292 } catch (Exception e) {
2293 if (!mInstrumentation.onException(s, e)) {
2294 throw new RuntimeException(
2295 "Unable to stop service " + s
2296 + ": " + e.toString(), e);
2297 }
2298 }
2299 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002300 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 }
2302
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002303 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002305 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002306 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 + " finished=" + r.activity.mFinished);
2308 if (r != null && !r.activity.mFinished) {
2309 if (clearHide) {
2310 r.hideForNow = false;
2311 r.activity.mStartedActivity = false;
2312 }
2313 try {
2314 if (r.pendingIntents != null) {
2315 deliverNewIntents(r, r.pendingIntents);
2316 r.pendingIntents = null;
2317 }
2318 if (r.pendingResults != null) {
2319 deliverResults(r, r.pendingResults);
2320 r.pendingResults = null;
2321 }
2322 r.activity.performResume();
2323
Bob Leee5408332009-09-04 18:31:17 -07002324 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 r.paused = false;
2328 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 r.state = null;
2330 } catch (Exception e) {
2331 if (!mInstrumentation.onException(r.activity, e)) {
2332 throw new RuntimeException(
2333 "Unable to resume activity "
2334 + r.intent.getComponent().toShortString()
2335 + ": " + e.toString(), e);
2336 }
2337 }
2338 }
2339 return r;
2340 }
2341
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002342 final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
2343 if (r.mPendingRemoveWindow != null) {
2344 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2345 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2346 if (wtoken != null) {
2347 WindowManagerImpl.getDefault().closeAll(wtoken,
2348 r.activity.getClass().getName(), "Activity");
2349 }
2350 }
2351 r.mPendingRemoveWindow = null;
2352 r.mPendingRemoveWindowManager = null;
2353 }
2354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2356 // If we are getting ready to gc after going to the background, well
2357 // we are back active so skip it.
2358 unscheduleGcIdler();
2359
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002360 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361
2362 if (r != null) {
2363 final Activity a = r.activity;
2364
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002365 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 TAG, "Resume " + r + " started activity: " +
2367 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2368 + ", finished: " + a.mFinished);
2369
2370 final int forwardBit = isForward ?
2371 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 // If the window hasn't yet been added to the window manager,
2374 // and this guy didn't finish itself or start another activity,
2375 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002376 boolean willBeVisible = !a.mStartedActivity;
2377 if (!willBeVisible) {
2378 try {
2379 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2380 a.getActivityToken());
2381 } catch (RemoteException e) {
2382 }
2383 }
2384 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 r.window = r.activity.getWindow();
2386 View decor = r.window.getDecorView();
2387 decor.setVisibility(View.INVISIBLE);
2388 ViewManager wm = a.getWindowManager();
2389 WindowManager.LayoutParams l = r.window.getAttributes();
2390 a.mDecor = decor;
2391 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2392 l.softInputMode |= forwardBit;
2393 if (a.mVisibleFromClient) {
2394 a.mWindowAdded = true;
2395 wm.addView(decor, l);
2396 }
2397
2398 // If the window has already been added, but during resume
2399 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002400 // window visible.
2401 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002402 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 TAG, "Launch " + r + " mStartedActivity set");
2404 r.hideForNow = true;
2405 }
2406
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002407 // Get rid of anything left hanging around.
2408 cleanUpPendingRemoveWindows(r);
2409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 // The window is now visible if it has been added, we are not
2411 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002412 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002413 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002415 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002416 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 performConfigurationChanged(r.activity, r.newConfig);
2418 r.newConfig = null;
2419 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002420 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 + isForward);
2422 WindowManager.LayoutParams l = r.window.getAttributes();
2423 if ((l.softInputMode
2424 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2425 != forwardBit) {
2426 l.softInputMode = (l.softInputMode
2427 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2428 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002429 if (r.activity.mVisibleFromClient) {
2430 ViewManager wm = a.getWindowManager();
2431 View decor = r.window.getDecorView();
2432 wm.updateViewLayout(decor, l);
2433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 }
2435 r.activity.mVisibleFromServer = true;
2436 mNumVisibleActivities++;
2437 if (r.activity.mVisibleFromClient) {
2438 r.activity.makeVisible();
2439 }
2440 }
2441
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002442 if (!r.onlyLocalRequest) {
2443 r.nextIdle = mNewActivities;
2444 mNewActivities = r;
2445 if (localLOGV) Slog.v(
2446 TAG, "Scheduling idle handler for " + r);
2447 Looper.myQueue().addIdleHandler(new Idler());
2448 }
2449 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450
2451 } else {
2452 // If an exception was thrown when trying to resume, then
2453 // just end this activity.
2454 try {
2455 ActivityManagerNative.getDefault()
2456 .finishActivity(token, Activity.RESULT_CANCELED, null);
2457 } catch (RemoteException ex) {
2458 }
2459 }
2460 }
2461
2462 private int mThumbnailWidth = -1;
2463 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002464 private Bitmap mAvailThumbnailBitmap = null;
2465 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002467 private final Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002468 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002470 if (thumbnail == null) {
2471 int w = mThumbnailWidth;
2472 int h;
2473 if (w < 0) {
2474 Resources res = r.activity.getResources();
2475 mThumbnailHeight = h =
2476 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002478 mThumbnailWidth = w =
2479 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2480 } else {
2481 h = mThumbnailHeight;
2482 }
2483
2484 // On platforms where we don't want thumbnails, set dims to (0,0)
2485 if ((w > 0) && (h > 0)) {
2486 thumbnail = Bitmap.createBitmap(w, h, THUMBNAIL_FORMAT);
2487 thumbnail.eraseColor(0);
2488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 }
2490
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002491 if (thumbnail != null) {
2492 Canvas cv = mThumbnailCanvas;
2493 if (cv == null) {
2494 mThumbnailCanvas = cv = new Canvas();
2495 }
2496
2497 cv.setBitmap(thumbnail);
2498 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2499 mAvailThumbnailBitmap = thumbnail;
2500 thumbnail = null;
2501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 } catch (Exception e) {
2505 if (!mInstrumentation.onException(r.activity, e)) {
2506 throw new RuntimeException(
2507 "Unable to create thumbnail of "
2508 + r.intent.getComponent().toShortString()
2509 + ": " + e.toString(), e);
2510 }
2511 thumbnail = null;
2512 }
2513
2514 return thumbnail;
2515 }
2516
2517 private final void handlePauseActivity(IBinder token, boolean finished,
2518 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002519 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002521 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 if (userLeaving) {
2523 performUserLeavingActivity(r);
2524 }
Bob Leee5408332009-09-04 18:31:17 -07002525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002527 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002529 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002530 if (r.isPreHoneycomb()) {
2531 QueuedWork.waitToFinish();
2532 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 // Tell the activity manager we have paused.
2535 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002536 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 } catch (RemoteException ex) {
2538 }
2539 }
2540 }
2541
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002542 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 mInstrumentation.callActivityOnUserLeaving(r.activity);
2544 }
2545
2546 final Bundle performPauseActivity(IBinder token, boolean finished,
2547 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002548 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 return r != null ? performPauseActivity(r, finished, saveState) : null;
2550 }
2551
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002552 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 boolean saveState) {
2554 if (r.paused) {
2555 if (r.activity.mFinished) {
2556 // If we are finishing, we won't call onResume() in certain cases.
2557 // So here we likewise don't want to call onPause() if the activity
2558 // isn't resumed.
2559 return null;
2560 }
2561 RuntimeException e = new RuntimeException(
2562 "Performing pause of activity that is not resumed: "
2563 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002564 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 }
2566 Bundle state = null;
2567 if (finished) {
2568 r.activity.mFinished = true;
2569 }
2570 try {
2571 // Next have the activity save its current state and managed dialogs...
2572 if (!r.activity.mFinished && saveState) {
2573 state = new Bundle();
2574 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2575 r.state = state;
2576 }
2577 // Now we are idle.
2578 r.activity.mCalled = false;
2579 mInstrumentation.callActivityOnPause(r.activity);
2580 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2581 if (!r.activity.mCalled) {
2582 throw new SuperNotCalledException(
2583 "Activity " + r.intent.getComponent().toShortString() +
2584 " did not call through to super.onPause()");
2585 }
2586
2587 } catch (SuperNotCalledException e) {
2588 throw e;
2589
2590 } catch (Exception e) {
2591 if (!mInstrumentation.onException(r.activity, e)) {
2592 throw new RuntimeException(
2593 "Unable to pause activity "
2594 + r.intent.getComponent().toShortString()
2595 + ": " + e.toString(), e);
2596 }
2597 }
2598 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002599
2600 // Notify any outstanding on paused listeners
2601 ArrayList<OnActivityPausedListener> listeners;
2602 synchronized (mOnPauseListeners) {
2603 listeners = mOnPauseListeners.remove(r.activity);
2604 }
2605 int size = (listeners != null ? listeners.size() : 0);
2606 for (int i = 0; i < size; i++) {
2607 listeners.get(i).onPaused(r.activity);
2608 }
2609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 return state;
2611 }
2612
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002613 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002614 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002615 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 }
2617
2618 private static class StopInfo {
2619 Bitmap thumbnail;
2620 CharSequence description;
2621 }
2622
2623 private final class ProviderRefCount {
2624 public int count;
2625 ProviderRefCount(int pCount) {
2626 count = pCount;
2627 }
2628 }
2629
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002630 /**
2631 * Core implementation of stopping an activity. Note this is a little
2632 * tricky because the server's meaning of stop is slightly different
2633 * than our client -- for the server, stop means to save state and give
2634 * it the result when it is done, but the window may still be visible.
2635 * For the client, we want to call onStop()/onStart() to indicate when
2636 * the activity's UI visibillity changes.
2637 */
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002638 private final void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002639 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002640 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002641 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 if (r != null) {
2643 if (!keepShown && r.stopped) {
2644 if (r.activity.mFinished) {
2645 // If we are finishing, we won't call onResume() in certain
2646 // cases. So here we likewise don't want to call onStop()
2647 // if the activity isn't resumed.
2648 return;
2649 }
2650 RuntimeException e = new RuntimeException(
2651 "Performing stop of activity that is not resumed: "
2652 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002653 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 }
2655
2656 if (info != null) {
2657 try {
2658 // First create a thumbnail for the activity...
Jim Miller0b2a6d02010-07-13 18:01:29 -07002659 info.thumbnail = createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 info.description = r.activity.onCreateDescription();
2661 } catch (Exception e) {
2662 if (!mInstrumentation.onException(r.activity, e)) {
2663 throw new RuntimeException(
2664 "Unable to save state of activity "
2665 + r.intent.getComponent().toShortString()
2666 + ": " + e.toString(), e);
2667 }
2668 }
2669 }
2670
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002671 // Next have the activity save its current state and managed dialogs...
2672 if (!r.activity.mFinished && saveState) {
2673 if (r.state == null) {
2674 state = new Bundle();
2675 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2676 r.state = state;
2677 } else {
2678 state = r.state;
2679 }
2680 }
2681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 if (!keepShown) {
2683 try {
2684 // Now we are idle.
2685 r.activity.performStop();
2686 } catch (Exception e) {
2687 if (!mInstrumentation.onException(r.activity, e)) {
2688 throw new RuntimeException(
2689 "Unable to stop activity "
2690 + r.intent.getComponent().toShortString()
2691 + ": " + e.toString(), e);
2692 }
2693 }
2694 r.stopped = true;
2695 }
2696
2697 r.paused = true;
2698 }
2699 }
2700
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002701 private final void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 View v = r.activity.mDecor;
2703 if (v != null) {
2704 if (show) {
2705 if (!r.activity.mVisibleFromServer) {
2706 r.activity.mVisibleFromServer = true;
2707 mNumVisibleActivities++;
2708 if (r.activity.mVisibleFromClient) {
2709 r.activity.makeVisible();
2710 }
2711 }
2712 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002713 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002714 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 performConfigurationChanged(r.activity, r.newConfig);
2716 r.newConfig = null;
2717 }
2718 } else {
2719 if (r.activity.mVisibleFromServer) {
2720 r.activity.mVisibleFromServer = false;
2721 mNumVisibleActivities--;
2722 v.setVisibility(View.INVISIBLE);
2723 }
2724 }
2725 }
2726 }
2727
2728 private final void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002729 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 r.activity.mConfigChangeFlags |= configChanges;
2731
2732 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002733 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002735 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 TAG, "Finishing stop of " + r + ": show=" + show
2737 + " win=" + r.window);
2738
2739 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07002740
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002741 // Make sure any pending writes are now committed.
2742 if (!r.isPreHoneycomb()) {
2743 QueuedWork.waitToFinish();
2744 }
2745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 // Tell activity manager we have been stopped.
2747 try {
2748 ActivityManagerNative.getDefault().activityStopped(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002749 r.token, r.state, info.thumbnail, info.description);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 } catch (RemoteException ex) {
2751 }
2752 }
2753
2754 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002755 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 if (r.stopped) {
2757 r.activity.performRestart();
2758 r.stopped = false;
2759 }
2760 }
2761
2762 private final void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002763 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08002764
2765 if (r == null) {
2766 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
2767 return;
2768 }
2769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002771 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 } else if (show && r.stopped) {
2773 // If we are getting ready to gc after going to the background, well
2774 // we are back active so skip it.
2775 unscheduleGcIdler();
2776
2777 r.activity.performRestart();
2778 r.stopped = false;
2779 }
2780 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07002781 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 TAG, "Handle window " + r + " visibility: " + show);
2783 updateVisibility(r, show);
2784 }
2785 }
2786
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002787 private final void handleSleeping(IBinder token, boolean sleeping) {
2788 ActivityClientRecord r = mActivities.get(token);
2789
2790 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002791 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002792 return;
2793 }
2794
2795 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002796 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002797 try {
2798 // Now we are idle.
2799 r.activity.performStop();
2800 } catch (Exception e) {
2801 if (!mInstrumentation.onException(r.activity, e)) {
2802 throw new RuntimeException(
2803 "Unable to stop activity "
2804 + r.intent.getComponent().toShortString()
2805 + ": " + e.toString(), e);
2806 }
2807 }
2808 r.stopped = true;
2809 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002810
2811 // Make sure any pending writes are now committed.
2812 if (!r.isPreHoneycomb()) {
2813 QueuedWork.waitToFinish();
2814 }
2815
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002816 // Tell activity manager we slept.
2817 try {
2818 ActivityManagerNative.getDefault().activitySlept(r.token);
2819 } catch (RemoteException ex) {
2820 }
2821 } else {
2822 if (r.stopped && r.activity.mVisibleFromServer) {
2823 r.activity.performRestart();
2824 r.stopped = false;
2825 }
2826 }
2827 }
2828
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002829 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08002830 synchronized (mPackages) {
2831 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002832 }
2833 }
2834
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002835 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
2836 LoadedApk apk = peekPackageInfo(data.pkg, false);
2837 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002838 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002839 }
2840 apk = peekPackageInfo(data.pkg, true);
2841 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002842 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002843 }
2844 handleConfigurationChanged(mConfiguration, data.info);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002845 WindowManagerImpl.getDefault().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002846 }
2847
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002848 private final void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 final int N = results.size();
2850 for (int i=0; i<N; i++) {
2851 ResultInfo ri = results.get(i);
2852 try {
2853 if (ri.mData != null) {
2854 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
2855 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002856 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07002857 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 r.activity.dispatchActivityResult(ri.mResultWho,
2859 ri.mRequestCode, ri.mResultCode, ri.mData);
2860 } catch (Exception e) {
2861 if (!mInstrumentation.onException(r.activity, e)) {
2862 throw new RuntimeException(
2863 "Failure delivering result " + ri + " to activity "
2864 + r.intent.getComponent().toShortString()
2865 + ": " + e.toString(), e);
2866 }
2867 }
2868 }
2869 }
2870
2871 private final void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002872 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002873 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 if (r != null) {
2875 final boolean resumed = !r.paused;
2876 if (!r.activity.mFinished && r.activity.mDecor != null
2877 && r.hideForNow && resumed) {
2878 // We had hidden the activity because it started another
2879 // one... we have gotten a result back and we are not
2880 // paused, so make sure our window is visible.
2881 updateVisibility(r, true);
2882 }
2883 if (resumed) {
2884 try {
2885 // Now we are idle.
2886 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002887 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 mInstrumentation.callActivityOnPause(r.activity);
2889 if (!r.activity.mCalled) {
2890 throw new SuperNotCalledException(
2891 "Activity " + r.intent.getComponent().toShortString()
2892 + " did not call through to super.onPause()");
2893 }
2894 } catch (SuperNotCalledException e) {
2895 throw e;
2896 } catch (Exception e) {
2897 if (!mInstrumentation.onException(r.activity, e)) {
2898 throw new RuntimeException(
2899 "Unable to pause activity "
2900 + r.intent.getComponent().toShortString()
2901 + ": " + e.toString(), e);
2902 }
2903 }
2904 }
2905 deliverResults(r, res.results);
2906 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002907 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002908 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 }
2910 }
2911 }
2912
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002913 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 return performDestroyActivity(token, finishing, 0, false);
2915 }
2916
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002917 private final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002919 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002920 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002921 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002923 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 r.activity.mConfigChangeFlags |= configChanges;
2925 if (finishing) {
2926 r.activity.mFinished = true;
2927 }
2928 if (!r.paused) {
2929 try {
2930 r.activity.mCalled = false;
2931 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07002932 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 r.activity.getComponentName().getClassName());
2934 if (!r.activity.mCalled) {
2935 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002936 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 + " did not call through to super.onPause()");
2938 }
2939 } catch (SuperNotCalledException e) {
2940 throw e;
2941 } catch (Exception e) {
2942 if (!mInstrumentation.onException(r.activity, e)) {
2943 throw new RuntimeException(
2944 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002945 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 + ": " + e.toString(), e);
2947 }
2948 }
2949 r.paused = true;
2950 }
2951 if (!r.stopped) {
2952 try {
2953 r.activity.performStop();
2954 } catch (SuperNotCalledException e) {
2955 throw e;
2956 } catch (Exception e) {
2957 if (!mInstrumentation.onException(r.activity, e)) {
2958 throw new RuntimeException(
2959 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002960 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 + ": " + e.toString(), e);
2962 }
2963 }
2964 r.stopped = true;
2965 }
2966 if (getNonConfigInstance) {
2967 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002968 r.lastNonConfigurationInstances
2969 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 } catch (Exception e) {
2971 if (!mInstrumentation.onException(r.activity, e)) {
2972 throw new RuntimeException(
2973 "Unable to retain activity "
2974 + r.intent.getComponent().toShortString()
2975 + ": " + e.toString(), e);
2976 }
2977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 }
2979 try {
2980 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002981 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 if (!r.activity.mCalled) {
2983 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002984 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 " did not call through to super.onDestroy()");
2986 }
2987 if (r.window != null) {
2988 r.window.closeAllPanels();
2989 }
2990 } catch (SuperNotCalledException e) {
2991 throw e;
2992 } catch (Exception e) {
2993 if (!mInstrumentation.onException(r.activity, e)) {
2994 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002995 "Unable to destroy activity " + safeToComponentShortString(r.intent)
2996 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 }
2998 }
2999 }
3000 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003001 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 return r;
3003 }
3004
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003005 private static String safeToComponentShortString(Intent intent) {
3006 ComponentName component = intent.getComponent();
3007 return component == null ? "[Unknown]" : component.toShortString();
3008 }
3009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 private final void handleDestroyActivity(IBinder token, boolean finishing,
3011 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003012 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 configChanges, getNonConfigInstance);
3014 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003015 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 WindowManager wm = r.activity.getWindowManager();
3017 View v = r.activity.mDecor;
3018 if (v != null) {
3019 if (r.activity.mVisibleFromServer) {
3020 mNumVisibleActivities--;
3021 }
3022 IBinder wtoken = v.getWindowToken();
3023 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003024 if (r.onlyLocalRequest) {
3025 // Hold off on removing this until the new activity's
3026 // window is being added.
3027 r.mPendingRemoveWindow = v;
3028 r.mPendingRemoveWindowManager = wm;
3029 } else {
3030 wm.removeViewImmediate(v);
3031 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003033 if (wtoken != null && r.mPendingRemoveWindow == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 WindowManagerImpl.getDefault().closeAll(wtoken,
3035 r.activity.getClass().getName(), "Activity");
3036 }
3037 r.activity.mDecor = null;
3038 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003039 if (r.mPendingRemoveWindow == null) {
3040 // If we are delaying the removal of the activity window, then
3041 // we can't clean up all windows here. Note that we can't do
3042 // so later either, which means any windows that aren't closed
3043 // by the app will leak. Well we try to warning them a lot
3044 // about leaking windows, because that is a bug, so if they are
3045 // using this recreate facility then they get to live with leaks.
3046 WindowManagerImpl.getDefault().closeAll(token,
3047 r.activity.getClass().getName(), "Activity");
3048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049
3050 // Mocked out contexts won't be participating in the normal
3051 // process lifecycle, but if we're running with a proper
3052 // ApplicationContext we need to have it tear down things
3053 // cleanly.
3054 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003055 if (c instanceof ContextImpl) {
3056 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 r.activity.getClass().getName(), "Activity");
3058 }
3059 }
3060 if (finishing) {
3061 try {
3062 ActivityManagerNative.getDefault().activityDestroyed(token);
3063 } catch (RemoteException ex) {
3064 // If the system process has died, it's game over for everyone.
3065 }
3066 }
3067 }
3068
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003069 public final void requestRelaunchActivity(IBinder token,
3070 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3071 int configChanges, boolean notResumed, Configuration config,
3072 boolean fromServer) {
3073 ActivityClientRecord target = null;
3074
3075 synchronized (mPackages) {
3076 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3077 ActivityClientRecord r = mRelaunchingActivities.get(i);
3078 if (r.token == token) {
3079 target = r;
3080 if (pendingResults != null) {
3081 if (r.pendingResults != null) {
3082 r.pendingResults.addAll(pendingResults);
3083 } else {
3084 r.pendingResults = pendingResults;
3085 }
3086 }
3087 if (pendingNewIntents != null) {
3088 if (r.pendingIntents != null) {
3089 r.pendingIntents.addAll(pendingNewIntents);
3090 } else {
3091 r.pendingIntents = pendingNewIntents;
3092 }
3093 }
3094 break;
3095 }
3096 }
3097
3098 if (target == null) {
3099 target = new ActivityClientRecord();
3100 target.token = token;
3101 target.pendingResults = pendingResults;
3102 target.pendingIntents = pendingNewIntents;
3103 if (!fromServer) {
3104 ActivityClientRecord existing = mActivities.get(token);
3105 if (existing != null) {
3106 target.startsNotResumed = existing.paused;
3107 }
3108 target.onlyLocalRequest = true;
3109 }
3110 mRelaunchingActivities.add(target);
3111 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3112 }
3113
3114 if (fromServer) {
3115 target.startsNotResumed = notResumed;
3116 target.onlyLocalRequest = false;
3117 }
3118 if (config != null) {
3119 target.createdConfig = config;
3120 }
3121 target.pendingConfigChanges |= configChanges;
3122 }
3123 }
3124
3125 private final void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 // If we are getting ready to gc after going to the background, well
3127 // we are back active so skip it.
3128 unscheduleGcIdler();
3129
3130 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003131 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 // First: make sure we have the most recent configuration and most
3134 // recent version of the activity, or skip it if some previous call
3135 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003136 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 int N = mRelaunchingActivities.size();
3138 IBinder token = tmp.token;
3139 tmp = null;
3140 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003141 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 if (r.token == token) {
3143 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003144 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 mRelaunchingActivities.remove(i);
3146 i--;
3147 N--;
3148 }
3149 }
Bob Leee5408332009-09-04 18:31:17 -07003150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003152 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 return;
3154 }
Bob Leee5408332009-09-04 18:31:17 -07003155
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003156 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3157 + tmp.token + " with configChanges=0x"
3158 + Integer.toHexString(configChanges));
3159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160 if (mPendingConfiguration != null) {
3161 changedConfig = mPendingConfiguration;
3162 mPendingConfiguration = null;
3163 }
3164 }
Bob Leee5408332009-09-04 18:31:17 -07003165
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003166 if (tmp.createdConfig != null) {
3167 // If the activity manager is passing us its current config,
3168 // assume that is really what we want regardless of what we
3169 // may have pending.
3170 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003171 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3172 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3173 if (changedConfig == null
3174 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3175 changedConfig = tmp.createdConfig;
3176 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003177 }
3178 }
3179
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003180 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003181 + tmp.token + ": changedConfig=" + changedConfig);
3182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 // If there was a pending configuration change, execute it first.
3184 if (changedConfig != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003185 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 }
Bob Leee5408332009-09-04 18:31:17 -07003187
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003188 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003189 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190 if (r == null) {
3191 return;
3192 }
Bob Leee5408332009-09-04 18:31:17 -07003193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003195 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003196 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003197
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003198 r.activity.mChangingConfigurations = true;
3199
Dianne Hackborne2b04802010-12-09 09:24:55 -08003200 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003202 performPauseActivity(r.token, false, r.isPreHoneycomb());
3203 }
3204 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3205 r.state = new Bundle();
3206 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 }
Bob Leee5408332009-09-04 18:31:17 -07003208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 r.activity = null;
3212 r.window = null;
3213 r.hideForNow = false;
3214 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003215 // Merge any pending results and pending intents; don't just replace them
3216 if (tmp.pendingResults != null) {
3217 if (r.pendingResults == null) {
3218 r.pendingResults = tmp.pendingResults;
3219 } else {
3220 r.pendingResults.addAll(tmp.pendingResults);
3221 }
3222 }
3223 if (tmp.pendingIntents != null) {
3224 if (r.pendingIntents == null) {
3225 r.pendingIntents = tmp.pendingIntents;
3226 } else {
3227 r.pendingIntents.addAll(tmp.pendingIntents);
3228 }
3229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003231
Christopher Tateb70f3df2009-04-07 16:07:59 -07003232 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 }
3234
3235 private final void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003236 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 Bitmap thumbnail = createThumbnailBitmap(r);
3238 CharSequence description = null;
3239 try {
3240 description = r.activity.onCreateDescription();
3241 } catch (Exception e) {
3242 if (!mInstrumentation.onException(r.activity, e)) {
3243 throw new RuntimeException(
3244 "Unable to create description of activity "
3245 + r.intent.getComponent().toShortString()
3246 + ": " + e.toString(), e);
3247 }
3248 }
3249 //System.out.println("Reporting top thumbnail " + thumbnail);
3250 try {
3251 ActivityManagerNative.getDefault().reportThumbnail(
3252 token, thumbnail, description);
3253 } catch (RemoteException ex) {
3254 }
3255 }
3256
3257 ArrayList<ComponentCallbacks> collectComponentCallbacksLocked(
3258 boolean allActivities, Configuration newConfig) {
3259 ArrayList<ComponentCallbacks> callbacks
3260 = new ArrayList<ComponentCallbacks>();
Bob Leee5408332009-09-04 18:31:17 -07003261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262 if (mActivities.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003263 Iterator<ActivityClientRecord> it = mActivities.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 while (it.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003265 ActivityClientRecord ar = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 Activity a = ar.activity;
3267 if (a != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003268 Configuration thisConfig = applyConfigCompatMainThread(newConfig,
3269 ar.packageInfo.mCompatibilityInfo.getIfNeeded());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 if (!ar.activity.mFinished && (allActivities ||
3271 (a != null && !ar.paused))) {
3272 // If the activity is currently resumed, its configuration
3273 // needs to change right now.
3274 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003275 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 // Otherwise, we will tell it about the change
3277 // the next time it is resumed or shown. Note that
3278 // the activity manager may, before then, decide the
3279 // activity needs to be destroyed to handle its new
3280 // configuration.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003281 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Setting activity "
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003282 + ar.activityInfo.name + " newConfig=" + thisConfig);
3283 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 }
3285 }
3286 }
3287 }
3288 if (mServices.size() > 0) {
3289 Iterator<Service> it = mServices.values().iterator();
3290 while (it.hasNext()) {
3291 callbacks.add(it.next());
3292 }
3293 }
3294 synchronized (mProviderMap) {
3295 if (mLocalProviders.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003296 Iterator<ProviderClientRecord> it = mLocalProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 while (it.hasNext()) {
3298 callbacks.add(it.next().mLocalProvider);
3299 }
3300 }
3301 }
3302 final int N = mAllApplications.size();
3303 for (int i=0; i<N; i++) {
3304 callbacks.add(mAllApplications.get(i));
3305 }
Bob Leee5408332009-09-04 18:31:17 -07003306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307 return callbacks;
3308 }
Bob Leee5408332009-09-04 18:31:17 -07003309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 private final void performConfigurationChanged(
3311 ComponentCallbacks cb, Configuration config) {
3312 // Only for Activity objects, check that they actually call up to their
3313 // superclass implementation. ComponentCallbacks is an interface, so
3314 // we check the runtime type and act accordingly.
3315 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3316 if (activity != null) {
3317 activity.mCalled = false;
3318 }
Bob Leee5408332009-09-04 18:31:17 -07003319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 boolean shouldChangeConfig = false;
3321 if ((activity == null) || (activity.mCurrentConfig == null)) {
3322 shouldChangeConfig = true;
3323 } else {
Bob Leee5408332009-09-04 18:31:17 -07003324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 // If the new config is the same as the config this Activity
3326 // is already running with then don't bother calling
3327 // onConfigurationChanged
3328 int diff = activity.mCurrentConfig.diff(config);
3329 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003330 // If this activity doesn't handle any of the config changes
3331 // then don't bother calling onConfigurationChanged as we're
3332 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003333 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 shouldChangeConfig = true;
3335 }
3336 }
3337 }
Bob Leee5408332009-09-04 18:31:17 -07003338
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003339 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003340 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 if (shouldChangeConfig) {
3342 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 if (activity != null) {
3345 if (!activity.mCalled) {
3346 throw new SuperNotCalledException(
3347 "Activity " + activity.getLocalClassName() +
3348 " did not call through to super.onConfigurationChanged()");
3349 }
3350 activity.mConfigChangeFlags = 0;
3351 activity.mCurrentConfig = new Configuration(config);
3352 }
3353 }
3354 }
3355
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003356 public final void applyConfigurationToResources(Configuration config) {
3357 synchronized (mPackages) {
3358 applyConfigurationToResourcesLocked(config, null);
3359 }
3360 }
3361
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003362 final boolean applyConfigurationToResourcesLocked(Configuration config,
3363 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003364 if (mResConfiguration == null) {
3365 mResConfiguration = new Configuration();
3366 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003367 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003368 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003369 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003370 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003371 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003372 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003373 DisplayMetrics dm = getDisplayMetricsLocked(compat, true);
3374
3375 if (compat != null && (mResCompatibilityInfo == null ||
3376 !mResCompatibilityInfo.equals(compat))) {
3377 mResCompatibilityInfo = compat;
3378 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3379 | ActivityInfo.CONFIG_SCREEN_SIZE
3380 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3381 }
Bob Leee5408332009-09-04 18:31:17 -07003382
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003383 // set it for java, this also affects newly created Resources
3384 if (config.locale != null) {
3385 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 }
Bob Leee5408332009-09-04 18:31:17 -07003387
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003388 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003389
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003390 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003391 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003392
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003393 Iterator<WeakReference<Resources>> it =
3394 mActiveResources.values().iterator();
3395 //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3396 // mActiveResources.entrySet().iterator();
3397 while (it.hasNext()) {
3398 WeakReference<Resources> v = it.next();
3399 Resources r = v.get();
3400 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003401 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003402 + r + " config to: " + config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003403 r.updateConfiguration(config, dm, compat);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003404 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003405 // + " " + r + ": " + r.getConfiguration());
3406 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003407 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003408 it.remove();
3409 }
3410 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003411
3412 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003413 }
3414
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003415 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003416
3417 ArrayList<ComponentCallbacks> callbacks = null;
3418
3419 synchronized (mPackages) {
3420 if (mPendingConfiguration != null) {
3421 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3422 config = mPendingConfiguration;
3423 }
3424 mPendingConfiguration = null;
3425 }
3426
3427 if (config == null) {
3428 return;
3429 }
3430
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003431 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003432 + config);
3433
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003434 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 if (mConfiguration == null) {
3437 mConfiguration = new Configuration();
3438 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003439 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003440 return;
3441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 mConfiguration.updateFrom(config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003443 if (mCompatConfiguration == null) {
3444 mCompatConfiguration = new Configuration();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003445 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003446 mCompatConfiguration.setTo(mConfiguration);
3447 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
3448 mResCompatibilityInfo.applyToConfiguration(mCompatConfiguration);
3449 config = mCompatConfiguration;
3450 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 callbacks = collectComponentCallbacksLocked(false, config);
3452 }
Bob Leee5408332009-09-04 18:31:17 -07003453
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003454 if (callbacks != null) {
3455 final int N = callbacks.size();
3456 for (int i=0; i<N; i++) {
3457 performConfigurationChanged(callbacks.get(i), config);
3458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 }
3460 }
3461
3462 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003463 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 if (r == null || r.activity == null) {
3465 return;
3466 }
Bob Leee5408332009-09-04 18:31:17 -07003467
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003468 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003469 + r.activityInfo.name);
3470
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003471 performConfigurationChanged(r.activity, mCompatConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472 }
3473
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003474 final void handleProfilerControl(boolean start, ProfilerControlData pcd) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003475 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003476 try {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003477 Debug.startMethodTracing(pcd.path, pcd.fd.getFileDescriptor(),
3478 8 * 1024 * 1024, 0);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003479 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003480 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003481 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003482 } finally {
3483 try {
3484 pcd.fd.close();
3485 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003486 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003487 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003488 }
3489 } else {
3490 Debug.stopMethodTracing();
3491 }
3492 }
Bob Leee5408332009-09-04 18:31:17 -07003493
Andy McFadden824c5102010-07-09 16:26:57 -07003494 final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
3495 if (managed) {
3496 try {
3497 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3498 } catch (IOException e) {
3499 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3500 + " -- can the process access this path?");
3501 } finally {
3502 try {
3503 dhd.fd.close();
3504 } catch (IOException e) {
3505 Slog.w(TAG, "Failure closing profile fd", e);
3506 }
3507 }
3508 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003509 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003510 }
3511 }
3512
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003513 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3514 boolean hasPkgInfo = false;
3515 if (packages != null) {
3516 for (int i=packages.length-1; i>=0; i--) {
3517 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3518 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003519 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003520 ref = mPackages.get(packages[i]);
3521 if (ref != null && ref.get() != null) {
3522 hasPkgInfo = true;
3523 } else {
3524 ref = mResourcePackages.get(packages[i]);
3525 if (ref != null && ref.get() != null) {
3526 hasPkgInfo = true;
3527 }
3528 }
3529 }
3530 mPackages.remove(packages[i]);
3531 mResourcePackages.remove(packages[i]);
3532 }
3533 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003534 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003535 hasPkgInfo);
3536 }
3537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 final void handleLowMemory() {
Brian Carlstromed7e0072011-03-24 13:27:57 -07003539 ArrayList<ComponentCallbacks> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003541 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 callbacks = collectComponentCallbacksLocked(true, null);
3543 }
Bob Leee5408332009-09-04 18:31:17 -07003544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545 final int N = callbacks.size();
3546 for (int i=0; i<N; i++) {
3547 callbacks.get(i).onLowMemory();
3548 }
3549
Chris Tatece229052009-03-25 16:44:52 -07003550 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3551 if (Process.myUid() != Process.SYSTEM_UID) {
3552 int sqliteReleased = SQLiteDatabase.releaseMemory();
3553 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3554 }
Bob Leee5408332009-09-04 18:31:17 -07003555
Mike Reedcaf0df12009-04-27 14:32:05 -04003556 // Ask graphics to free up as much as possible (font/image caches)
3557 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558
3559 BinderInternal.forceGc("mem");
3560 }
3561
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003562 final void handleTrimMemory(int level) {
Romain Guybdf76092011-07-18 15:00:43 -07003563 WindowManagerImpl.getDefault().trimMemory(level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003564 }
3565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 private final void handleBindApplication(AppBindData data) {
3567 mBoundApplication = data;
3568 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003569 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003572 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003573 android.ddm.DdmHandleAppName.setAppName(data.processName);
3574
Joe Onoratod630f102011-03-17 18:42:26 -07003575 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
3576 // implementation to use the pool executor. Normally, we use the
3577 // serialized executor as the default. This has to happen in the
3578 // main thread so the main looper is set right.
3579 if (data.appInfo.targetSdkVersion <= 12) {
3580 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
3581 }
3582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 /*
3584 * Before spawning a new process, reset the time zone to be the system time zone.
3585 * This needs to be done because the system time zone could have changed after the
3586 * the spawning of this process. Without doing this this process would have the incorrect
3587 * system time zone.
3588 */
3589 TimeZone.setDefault(null);
3590
3591 /*
3592 * Initialize the default locale in this process for the reasons we set the time zone.
3593 */
3594 Locale.setDefault(data.config.locale);
3595
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003596 /*
3597 * Update the system configuration since its preloaded and might not
3598 * reflect configuration changes. The configuration object passed
3599 * in AppBindData can be safely assumed to be up to date
3600 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003601 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003602
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003603 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003604
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003605 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003606 * For system applications on userdebug/eng builds, log stack
3607 * traces of disk and network access to dropbox for analysis.
3608 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07003609 if ((data.appInfo.flags &
3610 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07003611 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
3612 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003613 }
3614
3615 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07003616 * For apps targetting SDK Honeycomb or later, we don't allow
3617 * network usage on the main event loop / UI thread.
3618 *
3619 * Note to those grepping: this is what ultimately throws
3620 * NetworkOnMainThreadException ...
3621 */
3622 if (data.appInfo.targetSdkVersion > 9) {
3623 StrictMode.enableDeathOnNetwork();
3624 }
3625
3626 /**
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003627 * Switch this process to density compatibility mode if needed.
3628 */
3629 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
3630 == 0) {
3631 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3632 }
Bob Leee5408332009-09-04 18:31:17 -07003633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
3635 // XXX should have option to change the port.
3636 Debug.changeDebugPort(8100);
3637 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003638 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 + " is waiting for the debugger on port 8100...");
3640
3641 IActivityManager mgr = ActivityManagerNative.getDefault();
3642 try {
3643 mgr.showWaitingForDebugger(mAppThread, true);
3644 } catch (RemoteException ex) {
3645 }
3646
3647 Debug.waitForDebugger();
3648
3649 try {
3650 mgr.showWaitingForDebugger(mAppThread, false);
3651 } catch (RemoteException ex) {
3652 }
3653
3654 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003655 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003656 + " can be debugged on port 8100...");
3657 }
3658 }
3659
Robert Greenwalt434203a2010-10-11 16:00:27 -07003660 /**
3661 * Initialize the default http proxy in this process for the reasons we set the time zone.
3662 */
3663 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
3664 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
3665 try {
3666 ProxyProperties proxyProperties = service.getProxy();
3667 Proxy.setHttpProxySystemProperty(proxyProperties);
3668 } catch (RemoteException e) {}
3669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 if (data.instrumentationName != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08003671 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 appContext.init(data.info, null, this);
3673 InstrumentationInfo ii = null;
3674 try {
3675 ii = appContext.getPackageManager().
3676 getInstrumentationInfo(data.instrumentationName, 0);
3677 } catch (PackageManager.NameNotFoundException e) {
3678 }
3679 if (ii == null) {
3680 throw new RuntimeException(
3681 "Unable to find instrumentation info for: "
3682 + data.instrumentationName);
3683 }
3684
3685 mInstrumentationAppDir = ii.sourceDir;
3686 mInstrumentationAppPackage = ii.packageName;
3687 mInstrumentedAppDir = data.info.getAppDir();
3688
3689 ApplicationInfo instrApp = new ApplicationInfo();
3690 instrApp.packageName = ii.packageName;
3691 instrApp.sourceDir = ii.sourceDir;
3692 instrApp.publicSourceDir = ii.publicSourceDir;
3693 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07003694 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003695 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08003697 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003698 instrContext.init(pi, null, this);
3699
3700 try {
3701 java.lang.ClassLoader cl = instrContext.getClassLoader();
3702 mInstrumentation = (Instrumentation)
3703 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
3704 } catch (Exception e) {
3705 throw new RuntimeException(
3706 "Unable to instantiate instrumentation "
3707 + data.instrumentationName + ": " + e.toString(), e);
3708 }
3709
3710 mInstrumentation.init(this, instrContext, appContext,
3711 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
3712
3713 if (data.profileFile != null && !ii.handleProfiling) {
3714 data.handlingProfiling = true;
3715 File file = new File(data.profileFile);
3716 file.getParentFile().mkdirs();
3717 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
3718 }
3719
3720 try {
3721 mInstrumentation.onCreate(data.instrumentationArgs);
3722 }
3723 catch (Exception e) {
3724 throw new RuntimeException(
3725 "Exception thrown in onCreate() of "
3726 + data.instrumentationName + ": " + e.toString(), e);
3727 }
3728
3729 } else {
3730 mInstrumentation = new Instrumentation();
3731 }
3732
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003733 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08003734 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003735 }
3736
Christopher Tate181fafa2009-05-14 11:12:14 -07003737 // If the app is being launched for full backup or restore, bring it up in
3738 // a restricted environment with the base application class.
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003739 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 mInitialApplication = app;
3741
Christopher Tate75a99702011-05-18 16:28:19 -07003742 // don't bring up providers in restricted mode; they may depend on the
3743 // app's custom Application class
3744 if (!data.restrictedBackupMode){
3745 List<ProviderInfo> providers = data.providers;
3746 if (providers != null) {
3747 installContentProviders(app, providers);
3748 // For process that contains content providers, we want to
3749 // ensure that the JIT is enabled "at some point".
3750 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
3751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003752 }
3753
3754 try {
3755 mInstrumentation.callApplicationOnCreate(app);
3756 } catch (Exception e) {
3757 if (!mInstrumentation.onException(app, e)) {
3758 throw new RuntimeException(
3759 "Unable to create application " + app.getClass().getName()
3760 + ": " + e.toString(), e);
3761 }
3762 }
3763 }
3764
3765 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
3766 IActivityManager am = ActivityManagerNative.getDefault();
3767 if (mBoundApplication.profileFile != null && mBoundApplication.handlingProfiling) {
3768 Debug.stopMethodTracing();
3769 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003770 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 // + ", app thr: " + mAppThread);
3772 try {
3773 am.finishInstrumentation(mAppThread, resultCode, results);
3774 } catch (RemoteException ex) {
3775 }
3776 }
3777
3778 private final void installContentProviders(
3779 Context context, List<ProviderInfo> providers) {
3780 final ArrayList<IActivityManager.ContentProviderHolder> results =
3781 new ArrayList<IActivityManager.ContentProviderHolder>();
3782
3783 Iterator<ProviderInfo> i = providers.iterator();
3784 while (i.hasNext()) {
3785 ProviderInfo cpi = i.next();
3786 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07003787 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003788 buf.append(cpi.authority);
3789 buf.append(": ");
3790 buf.append(cpi.name);
3791 Log.i(TAG, buf.toString());
3792 IContentProvider cp = installProvider(context, null, cpi, false);
3793 if (cp != null) {
3794 IActivityManager.ContentProviderHolder cph =
3795 new IActivityManager.ContentProviderHolder(cpi);
3796 cph.provider = cp;
3797 results.add(cph);
3798 // Don't ever unload this provider from the process.
3799 synchronized(mProviderMap) {
3800 mProviderRefCountMap.put(cp.asBinder(), new ProviderRefCount(10000));
3801 }
3802 }
3803 }
3804
3805 try {
3806 ActivityManagerNative.getDefault().publishContentProviders(
3807 getApplicationThread(), results);
3808 } catch (RemoteException ex) {
3809 }
3810 }
3811
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003812 private final IContentProvider getExistingProvider(Context context, String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003813 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003814 final ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 if (pr != null) {
3816 return pr.mProvider;
3817 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003818 return null;
3819 }
3820 }
3821
3822 private final IContentProvider getProvider(Context context, String name) {
3823 IContentProvider existing = getExistingProvider(context, name);
3824 if (existing != null) {
3825 return existing;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 }
3827
3828 IActivityManager.ContentProviderHolder holder = null;
3829 try {
3830 holder = ActivityManagerNative.getDefault().getContentProvider(
3831 getApplicationThread(), name);
3832 } catch (RemoteException ex) {
3833 }
3834 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003835 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 return null;
3837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003838
3839 IContentProvider prov = installProvider(context, holder.provider,
3840 holder.info, true);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003841 //Slog.i(TAG, "noReleaseNeeded=" + holder.noReleaseNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003842 if (holder.noReleaseNeeded || holder.provider == null) {
3843 // We are not going to release the provider if it is an external
3844 // provider that doesn't care about being released, or if it is
3845 // a local provider running in this process.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003846 //Slog.i(TAG, "*** NO RELEASE NEEDED");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003847 synchronized(mProviderMap) {
3848 mProviderRefCountMap.put(prov.asBinder(), new ProviderRefCount(10000));
3849 }
3850 }
3851 return prov;
3852 }
3853
3854 public final IContentProvider acquireProvider(Context c, String name) {
3855 IContentProvider provider = getProvider(c, name);
3856 if(provider == null)
3857 return null;
3858 IBinder jBinder = provider.asBinder();
3859 synchronized(mProviderMap) {
3860 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3861 if(prc == null) {
3862 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
3863 } else {
3864 prc.count++;
3865 } //end else
3866 } //end synchronized
3867 return provider;
3868 }
3869
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003870 public final IContentProvider acquireExistingProvider(Context c, String name) {
3871 IContentProvider provider = getExistingProvider(c, name);
3872 if(provider == null)
3873 return null;
3874 IBinder jBinder = provider.asBinder();
3875 synchronized(mProviderMap) {
3876 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3877 if(prc == null) {
3878 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
3879 } else {
3880 prc.count++;
3881 } //end else
3882 } //end synchronized
3883 return provider;
3884 }
3885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003886 public final boolean releaseProvider(IContentProvider provider) {
3887 if(provider == null) {
3888 return false;
3889 }
3890 IBinder jBinder = provider.asBinder();
3891 synchronized(mProviderMap) {
3892 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3893 if(prc == null) {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003894 if(localLOGV) Slog.v(TAG, "releaseProvider::Weird shouldn't be here");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003895 return false;
3896 } else {
3897 prc.count--;
3898 if(prc.count == 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003899 // Schedule the actual remove asynchronously, since we
3900 // don't know the context this will be called in.
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003901 // TODO: it would be nice to post a delayed message, so
3902 // if we come back and need the same provider quickly
3903 // we will still have it available.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003904 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, provider);
3905 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003906 } //end if
3907 } //end else
3908 } //end synchronized
3909 return true;
3910 }
3911
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003912 final void completeRemoveProvider(IContentProvider provider) {
3913 IBinder jBinder = provider.asBinder();
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003914 String name = null;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003915 synchronized(mProviderMap) {
3916 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3917 if(prc != null && prc.count == 0) {
3918 mProviderRefCountMap.remove(jBinder);
3919 //invoke removeProvider to dereference provider
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003920 name = removeProviderLocked(provider);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003921 }
3922 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003923
3924 if (name != null) {
3925 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003926 if(localLOGV) Slog.v(TAG, "removeProvider::Invoking " +
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003927 "ActivityManagerNative.removeContentProvider(" + name);
3928 ActivityManagerNative.getDefault().removeContentProvider(
3929 getApplicationThread(), name);
3930 } catch (RemoteException e) {
3931 //do nothing content provider object is dead any way
3932 } //end catch
3933 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003934 }
3935
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003936 public final String removeProviderLocked(IContentProvider provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 if (provider == null) {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003938 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 }
3940 IBinder providerBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003942 String name = null;
3943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003944 // remove the provider from mProviderMap
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003945 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003946 while (iter.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003947 ProviderClientRecord pr = iter.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 IBinder myBinder = pr.mProvider.asBinder();
3949 if (myBinder == providerBinder) {
3950 //find if its published by this process itself
3951 if(pr.mLocalProvider != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003952 if(localLOGV) Slog.i(TAG, "removeProvider::found local provider returning");
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003953 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003955 if(localLOGV) Slog.v(TAG, "removeProvider::Not local provider Unlinking " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003956 "death recipient");
3957 //content provider is in another process
3958 myBinder.unlinkToDeath(pr, 0);
3959 iter.remove();
3960 //invoke remove only once for the very first name seen
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003961 if(name == null) {
3962 name = pr.mName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 }
3964 } //end if myBinder
3965 } //end while iter
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003966
3967 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003968 }
3969
3970 final void removeDeadProvider(String name, IContentProvider provider) {
3971 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003972 ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 if (pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003974 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003975 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003976 if (removed != null) {
3977 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
3978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 }
3980 }
3981 }
3982
3983 final void removeDeadProviderLocked(String name, IContentProvider provider) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003984 ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003985 if (pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003986 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003987 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003988 if (removed != null) {
3989 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
3990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003991 }
3992 }
3993
3994 private final IContentProvider installProvider(Context context,
3995 IContentProvider provider, ProviderInfo info, boolean noisy) {
3996 ContentProvider localProvider = null;
3997 if (provider == null) {
3998 if (noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003999 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004000 + info.name);
4001 }
4002 Context c = null;
4003 ApplicationInfo ai = info.applicationInfo;
4004 if (context.getPackageName().equals(ai.packageName)) {
4005 c = context;
4006 } else if (mInitialApplication != null &&
4007 mInitialApplication.getPackageName().equals(ai.packageName)) {
4008 c = mInitialApplication;
4009 } else {
4010 try {
4011 c = context.createPackageContext(ai.packageName,
4012 Context.CONTEXT_INCLUDE_CODE);
4013 } catch (PackageManager.NameNotFoundException e) {
4014 }
4015 }
4016 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004017 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004018 ai.packageName +
4019 " while loading content provider " +
4020 info.name);
4021 return null;
4022 }
4023 try {
4024 final java.lang.ClassLoader cl = c.getClassLoader();
4025 localProvider = (ContentProvider)cl.
4026 loadClass(info.name).newInstance();
4027 provider = localProvider.getIContentProvider();
4028 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004029 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 info.name + " from sourceDir " +
4031 info.applicationInfo.sourceDir);
4032 return null;
4033 }
Joe Onorato43a17652011-04-06 19:22:23 -07004034 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004035 TAG, "Instantiating local provider " + info.name);
4036 // XXX Need to create the correct context for this provider.
4037 localProvider.attachInfo(c, info);
4038 } catch (java.lang.Exception e) {
4039 if (!mInstrumentation.onException(null, e)) {
4040 throw new RuntimeException(
4041 "Unable to get provider " + info.name
4042 + ": " + e.toString(), e);
4043 }
4044 return null;
4045 }
4046 } else if (localLOGV) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004047 Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 + info.name);
4049 }
4050
4051 synchronized (mProviderMap) {
4052 // Cache the pointer for the remote provider.
4053 String names[] = PATTERN_SEMICOLON.split(info.authority);
4054 for (int i=0; i<names.length; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004055 ProviderClientRecord pr = new ProviderClientRecord(names[i], provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 localProvider);
4057 try {
4058 provider.asBinder().linkToDeath(pr, 0);
4059 mProviderMap.put(names[i], pr);
4060 } catch (RemoteException e) {
4061 return null;
4062 }
4063 }
4064 if (localProvider != null) {
4065 mLocalProviders.put(provider.asBinder(),
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004066 new ProviderClientRecord(null, provider, localProvider));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 }
4068 }
4069
4070 return provider;
4071 }
4072
4073 private final void attach(boolean system) {
4074 sThreadLocal.set(this);
4075 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004076 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004077 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004078 public void run() {
4079 ensureJitEnabled();
4080 }
4081 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4083 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4084 IActivityManager mgr = ActivityManagerNative.getDefault();
4085 try {
4086 mgr.attachApplication(mAppThread);
4087 } catch (RemoteException ex) {
4088 }
4089 } else {
4090 // Don't set application object here -- if the system crashes,
4091 // we can't display an alert, we just want to die die die.
4092 android.ddm.DdmHandleAppName.setAppName("system_process");
4093 try {
4094 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004095 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 context.init(getSystemContext().mPackageInfo, null, this);
4097 Application app = Instrumentation.newApplication(Application.class, context);
4098 mAllApplications.add(app);
4099 mInitialApplication = app;
4100 app.onCreate();
4101 } catch (Exception e) {
4102 throw new RuntimeException(
4103 "Unable to instantiate Application():" + e.toString(), e);
4104 }
4105 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004106
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004107 ViewRootImpl.addConfigCallback(new ComponentCallbacks() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004108 public void onConfigurationChanged(Configuration newConfig) {
4109 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004110 // We need to apply this change to the resources
4111 // immediately, because upon returning the view
4112 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004113 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004114 // This actually changed the resources! Tell
4115 // everyone about it.
4116 if (mPendingConfiguration == null ||
4117 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4118 mPendingConfiguration = newConfig;
4119
4120 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4121 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004122 }
4123 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004124 }
4125 public void onLowMemory() {
4126 }
4127 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 }
4129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004130 public static final ActivityThread systemMain() {
Romain Guy52339202010-09-03 16:04:46 -07004131 HardwareRenderer.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 ActivityThread thread = new ActivityThread();
4133 thread.attach(true);
4134 return thread;
4135 }
4136
Jeff Brown10e89712011-07-08 18:52:57 -07004137 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004139 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140 }
4141 }
4142
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004143 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004144 synchronized (mPackages) {
4145 if (mCoreSettings != null) {
4146 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004147 } else {
4148 return defaultValue;
4149 }
4150 }
4151 }
4152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004153 public static final void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004154 SamplingProfilerIntegration.start();
4155
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004156 // CloseGuard defaults to true and can be quite spammy. We
4157 // disable it here, but selectively enable it later (via
4158 // StrictMode) on debug builds, but using DropBox, not logs.
4159 CloseGuard.setEnabled(false);
4160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004161 Process.setArgV0("<pre-initialized>");
4162
4163 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07004164 if (sMainThreadHandler == null) {
4165 sMainThreadHandler = new Handler();
4166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004167
4168 ActivityThread thread = new ActivityThread();
4169 thread.attach(false);
4170
Dianne Hackborn287952c2010-09-22 22:34:31 -07004171 if (false) {
4172 Looper.myLooper().setMessageLogging(new
4173 LogPrinter(Log.DEBUG, "ActivityThread"));
4174 }
4175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 Looper.loop();
4177
Jeff Brown10e89712011-07-08 18:52:57 -07004178 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 }
4180}