blob: b29035d18a875648f204d3eb9006ee7052462fa6 [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;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070021import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import 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;
Romain Guya9582652011-11-10 14:20:10 -080048import android.opengl.GLUtils;
Joe Onoratod630f102011-03-17 18:42:26 -070049import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070050import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.Bundle;
52import android.os.Debug;
53import android.os.Handler;
54import android.os.IBinder;
55import android.os.Looper;
56import android.os.Message;
57import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070058import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Process;
60import android.os.RemoteException;
61import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070062import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.SystemClock;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070064import android.os.Trace;
Amith Yamasani742a6712011-05-04 14:49:28 -070065import android.os.UserId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.util.AndroidRuntimeException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.util.DisplayMetrics;
68import android.util.EventLog;
69import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070070import android.util.LogPrinter;
Jeff Brown6754ba22011-12-14 20:20:01 -080071import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080072import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070074import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.View;
76import android.view.ViewDebug;
77import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070078import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.view.Window;
80import android.view.WindowManager;
81import android.view.WindowManagerImpl;
Jason Samsa6f338c2012-02-24 16:22:16 -080082import android.renderscript.RenderScript;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
84import com.android.internal.os.BinderInternal;
85import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070086import com.android.internal.os.SamplingProfilerIntegration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl;
89
90import java.io.File;
91import java.io.FileDescriptor;
92import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070093import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import java.io.PrintWriter;
95import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -070096import java.net.InetAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import java.util.ArrayList;
98import java.util.HashMap;
99import java.util.Iterator;
100import java.util.List;
101import java.util.Locale;
102import java.util.Map;
103import java.util.TimeZone;
104import java.util.regex.Pattern;
105
Jeff Sharkeye861b422012-03-01 20:59:22 -0800106import libcore.io.IoUtils;
107
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800108import dalvik.system.CloseGuard;
Bob Leee5408332009-09-04 18:31:17 -0700109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110final class SuperNotCalledException extends AndroidRuntimeException {
111 public SuperNotCalledException(String msg) {
112 super(msg);
113 }
114}
115
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700116final class RemoteServiceException extends AndroidRuntimeException {
117 public RemoteServiceException(String msg) {
118 super(msg);
119 }
120}
121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122/**
123 * This manages the execution of the main thread in an
124 * application process, scheduling and executing activities,
125 * broadcasts, and other operations on it as the activity
126 * manager requests.
127 *
128 * {@hide}
129 */
130public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700131 /** @hide */
132 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700133 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700134 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700135 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700136 /** @hide */
137 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700138 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700139 private static final boolean DEBUG_BACKUP = false;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700140 private static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800141 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700142 private static final boolean DEBUG_MEMORY_TRIM = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
144 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
145 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
146 private static final int LOG_ON_PAUSE_CALLED = 30021;
147 private static final int LOG_ON_RESUME_CALLED = 30022;
148
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700149 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700150
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700151 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700153 final ApplicationThread mAppThread = new ApplicationThread();
154 final Looper mLooper = Looper.myLooper();
155 final H mH = new H();
156 final HashMap<IBinder, ActivityClientRecord> mActivities
157 = new HashMap<IBinder, ActivityClientRecord>();
158 // List of new activities (via ActivityRecord.nextIdle) that should
159 // be reported when next we idle.
160 ActivityClientRecord mNewActivities = null;
161 // Number of activities that are currently visible on-screen.
162 int mNumVisibleActivities = 0;
163 final HashMap<IBinder, Service> mServices
164 = new HashMap<IBinder, Service>();
165 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700166 Profiler mProfiler;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700167 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700168 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700169 Configuration mResConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700170 CompatibilityInfo mResCompatibilityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700171 Application mInitialApplication;
172 final ArrayList<Application> mAllApplications
173 = new ArrayList<Application>();
174 // set of instantiated backup agents, keyed by package name
175 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Romain Guy65b345f2011-07-27 18:51:50 -0700176 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700177 Instrumentation mInstrumentation;
178 String mInstrumentationAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700179 String mInstrumentationAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700180 String mInstrumentationAppPackage = null;
181 String mInstrumentedAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700182 String mInstrumentedAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700183 boolean mSystemThread = false;
184 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700186 // These can be accessed by multiple threads; mPackages is the lock.
187 // XXX For now we keep around information about all packages we have
188 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800189 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700190 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800191 // which means this lock gets held while the activity and window managers
192 // holds their own lock. Thus you MUST NEVER call back into the activity manager
193 // or window manager or anything that depends on them while holding this lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700194 final HashMap<String, WeakReference<LoadedApk>> mPackages
195 = new HashMap<String, WeakReference<LoadedApk>>();
196 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
197 = new HashMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700198 final HashMap<CompatibilityInfo, DisplayMetrics> mDisplayMetrics
199 = new HashMap<CompatibilityInfo, DisplayMetrics>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700200 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
201 = new HashMap<ResourcesKey, WeakReference<Resources> >();
202 final ArrayList<ActivityClientRecord> mRelaunchingActivities
203 = new ArrayList<ActivityClientRecord>();
204 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700206 // The lock of mProviderMap protects the following variables.
207 final HashMap<String, ProviderClientRecord> mProviderMap
208 = new HashMap<String, ProviderClientRecord>();
209 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
210 = new HashMap<IBinder, ProviderRefCount>();
211 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
212 = new HashMap<IBinder, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
Jeff Hamilton52d32032011-01-08 15:31:26 -0600214 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
215 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
216
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700217 final GcIdler mGcIdler = new GcIdler();
218 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700220 static Handler sMainThreadHandler; // set once in main()
221
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800222 Bundle mCoreSettings = null;
223
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400224 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700226 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 Intent intent;
228 Bundle state;
229 Activity activity;
230 Window window;
231 Activity parent;
232 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700233 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 boolean paused;
235 boolean stopped;
236 boolean hideForNow;
237 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700238 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700239 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700241 String profileFile;
242 ParcelFileDescriptor profileFd;
243 boolean autoStopProfiler;
244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400246 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700247 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
249 List<ResultInfo> pendingResults;
250 List<Intent> pendingIntents;
251
252 boolean startsNotResumed;
253 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800254 int pendingConfigChanges;
255 boolean onlyLocalRequest;
256
257 View mPendingRemoveWindow;
258 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700260 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 parent = null;
262 embeddedID = null;
263 paused = false;
264 stopped = false;
265 hideForNow = false;
266 nextIdle = null;
267 }
268
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800269 public boolean isPreHoneycomb() {
270 if (activity != null) {
271 return activity.getApplicationInfo().targetSdkVersion
272 < android.os.Build.VERSION_CODES.HONEYCOMB;
273 }
274 return false;
275 }
276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700278 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 return "ActivityRecord{"
280 + Integer.toHexString(System.identityHashCode(this))
281 + " token=" + token + " " + (componentName == null
282 ? "no component name" : componentName.toShortString())
283 + "}";
284 }
285 }
286
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400287 final class ProviderClientRecord implements IBinder.DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 final String mName;
289 final IContentProvider mProvider;
290 final ContentProvider mLocalProvider;
291
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700292 ProviderClientRecord(String name, IContentProvider provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 ContentProvider localProvider) {
294 mName = name;
295 mProvider = provider;
296 mLocalProvider = localProvider;
297 }
298
299 public void binderDied() {
300 removeDeadProvider(mName, mProvider);
301 }
302 }
303
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400304 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 List<Intent> intents;
306 IBinder token;
307 public String toString() {
308 return "NewIntentData{intents=" + intents + " token=" + token + "}";
309 }
310 }
311
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400312 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700313 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
314 boolean ordered, boolean sticky, IBinder token) {
315 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky, token);
316 this.intent = intent;
317 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 Intent intent;
320 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400321 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 public String toString() {
323 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700324 info.packageName + " resultCode=" + getResultCode()
325 + " resultData=" + getResultData() + " resultExtras="
326 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 }
328 }
329
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400330 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700331 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400332 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700333 int backupMode;
334 public String toString() {
335 return "CreateBackupAgentData{appInfo=" + appInfo
336 + " backupAgent=" + appInfo.backupAgentName
337 + " mode=" + backupMode + "}";
338 }
339 }
Bob Leee5408332009-09-04 18:31:17 -0700340
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400341 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 IBinder token;
343 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400344 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 Intent intent;
346 public String toString() {
347 return "CreateServiceData{token=" + token + " className="
348 + info.name + " packageName=" + info.packageName
349 + " intent=" + intent + "}";
350 }
351 }
352
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400353 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 IBinder token;
355 Intent intent;
356 boolean rebind;
357 public String toString() {
358 return "BindServiceData{token=" + token + " intent=" + intent + "}";
359 }
360 }
361
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400362 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700364 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700366 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 Intent args;
368 public String toString() {
369 return "ServiceArgsData{token=" + token + " startId=" + startId
370 + " args=" + args + "}";
371 }
372 }
373
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400374 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700375 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 String processName;
377 ApplicationInfo appInfo;
378 List<ProviderInfo> providers;
379 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 Bundle instrumentationArgs;
381 IInstrumentationWatcher instrumentationWatcher;
382 int debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800383 boolean enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700384 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700385 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400387 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700388
389 /** Initial values for {@link Profiler}. */
390 String initProfileFile;
391 ParcelFileDescriptor initProfileFd;
392 boolean initAutoStopProfiler;
393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 public String toString() {
395 return "AppBindData{appInfo=" + appInfo + "}";
396 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700397 }
398
399 static final class Profiler {
400 String profileFile;
401 ParcelFileDescriptor profileFd;
402 boolean autoStopProfiler;
403 boolean profiling;
404 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700405 public void setProfiler(String file, ParcelFileDescriptor fd) {
406 if (profiling) {
407 if (fd != null) {
408 try {
409 fd.close();
410 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700411 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700412 }
413 }
414 return;
415 }
416 if (profileFd != null) {
417 try {
418 profileFd.close();
419 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700420 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700421 }
422 }
423 profileFile = file;
424 profileFd = fd;
425 }
426 public void startProfiling() {
427 if (profileFd == null || profiling) {
428 return;
429 }
430 try {
431 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
432 8 * 1024 * 1024, 0);
433 profiling = true;
434 } catch (RuntimeException e) {
435 Slog.w(TAG, "Profiling failed on path " + profileFile);
436 try {
437 profileFd.close();
438 profileFd = null;
439 } catch (IOException e2) {
440 Slog.w(TAG, "Failure closing profile fd", e2);
441 }
442 }
443 }
444 public void stopProfiling() {
445 if (profiling) {
446 profiling = false;
447 Debug.stopMethodTracing();
448 if (profileFd != null) {
449 try {
450 profileFd.close();
451 } catch (IOException e) {
452 }
453 }
454 profileFd = null;
455 profileFile = null;
456 }
457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 }
459
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400460 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700461 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700462 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800463 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 }
466
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400467 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 IBinder token;
469 List<ResultInfo> results;
470 public String toString() {
471 return "ResultData{token=" + token + " results" + results + "}";
472 }
473 }
474
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400475 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800476 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 String what;
478 String who;
479 }
480
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400481 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700482 String path;
483 ParcelFileDescriptor fd;
484 }
485
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400486 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700487 String path;
488 ParcelFileDescriptor fd;
489 }
490
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400491 static final class UpdateCompatibilityData {
492 String pkg;
493 CompatibilityInfo info;
494 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700495
Romain Guy65b345f2011-07-27 18:51:50 -0700496 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700497
Romain Guy65b345f2011-07-27 18:51:50 -0700498 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700499 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
500 private static final String ONE_COUNT_COLUMN = "%21s %8d";
501 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700502 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 // Formatting for checkin service - update version if row format changes
505 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700506
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700507 private void updatePendingConfiguration(Configuration config) {
508 synchronized (mPackages) {
509 if (mPendingConfiguration == null ||
510 mPendingConfiguration.isOtherSeqNewer(config)) {
511 mPendingConfiguration = config;
512 }
513 }
514 }
515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 public final void schedulePauseActivity(IBinder token, boolean finished,
517 boolean userLeaving, int configChanges) {
518 queueOrSendMessage(
519 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
520 token,
521 (userLeaving ? 1 : 0),
522 configChanges);
523 }
524
525 public final void scheduleStopActivity(IBinder token, boolean showWindow,
526 int configChanges) {
527 queueOrSendMessage(
528 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
529 token, 0, configChanges);
530 }
531
532 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
533 queueOrSendMessage(
534 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
535 token);
536 }
537
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800538 public final void scheduleSleeping(IBinder token, boolean sleeping) {
539 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
540 }
541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
543 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
544 }
545
546 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
547 ResultData res = new ResultData();
548 res.token = token;
549 res.results = results;
550 queueOrSendMessage(H.SEND_RESULT, res);
551 }
552
553 // we use token to identify this activity without having to send the
554 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700555 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700556 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
557 Bundle state, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700558 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
559 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700560 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561
562 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700563 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 r.intent = intent;
565 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400566 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 r.state = state;
568
569 r.pendingResults = pendingResults;
570 r.pendingIntents = pendingNewIntents;
571
572 r.startsNotResumed = notResumed;
573 r.isForward = isForward;
574
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700575 r.profileFile = profileName;
576 r.profileFd = profileFd;
577 r.autoStopProfiler = autoStopProfiler;
578
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700579 updatePendingConfiguration(curConfig);
580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
582 }
583
584 public final void scheduleRelaunchActivity(IBinder token,
585 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800586 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800587 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
588 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 }
590
591 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
592 NewIntentData data = new NewIntentData();
593 data.intents = intents;
594 data.token = token;
595
596 queueOrSendMessage(H.NEW_INTENT, data);
597 }
598
599 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
600 int configChanges) {
601 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
602 configChanges);
603 }
604
605 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400606 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
607 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700608 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
609 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400611 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 queueOrSendMessage(H.RECEIVER, r);
613 }
614
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400615 public final void scheduleCreateBackupAgent(ApplicationInfo app,
616 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700617 CreateBackupAgentData d = new CreateBackupAgentData();
618 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400619 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700620 d.backupMode = backupMode;
621
622 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
623 }
624
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400625 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
626 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700627 CreateBackupAgentData d = new CreateBackupAgentData();
628 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400629 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700630
631 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
632 }
633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400635 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 CreateServiceData s = new CreateServiceData();
637 s.token = token;
638 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400639 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640
641 queueOrSendMessage(H.CREATE_SERVICE, s);
642 }
643
644 public final void scheduleBindService(IBinder token, Intent intent,
645 boolean rebind) {
646 BindServiceData s = new BindServiceData();
647 s.token = token;
648 s.intent = intent;
649 s.rebind = rebind;
650
Amith Yamasani742a6712011-05-04 14:49:28 -0700651 if (DEBUG_SERVICE)
652 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
653 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 queueOrSendMessage(H.BIND_SERVICE, s);
655 }
656
657 public final void scheduleUnbindService(IBinder token, Intent intent) {
658 BindServiceData s = new BindServiceData();
659 s.token = token;
660 s.intent = intent;
661
662 queueOrSendMessage(H.UNBIND_SERVICE, s);
663 }
664
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700665 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700666 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 ServiceArgsData s = new ServiceArgsData();
668 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700669 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700671 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 s.args = args;
673
674 queueOrSendMessage(H.SERVICE_ARGS, s);
675 }
676
677 public final void scheduleStopService(IBinder token) {
678 queueOrSendMessage(H.STOP_SERVICE, token);
679 }
680
681 public final void bindApplication(String processName,
682 ApplicationInfo appInfo, List<ProviderInfo> providers,
683 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700684 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Siva Velusamy92a8b222012-03-09 16:24:04 -0800686 int debugMode, boolean enableOpenGlTrace, boolean isRestrictedBackupMode,
687 boolean persistent, Configuration config, CompatibilityInfo compatInfo,
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700688 Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689
690 if (services != null) {
691 // Setup the service cache in the ServiceManager
692 ServiceManager.initServiceCache(services);
693 }
694
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800695 setCoreSettings(coreSettings);
696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 AppBindData data = new AppBindData();
698 data.processName = processName;
699 data.appInfo = appInfo;
700 data.providers = providers;
701 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 data.instrumentationArgs = instrumentationArgs;
703 data.instrumentationWatcher = instrumentationWatcher;
704 data.debugMode = debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800705 data.enableOpenGlTrace = enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700706 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700707 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400709 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700710 data.initProfileFile = profileFile;
711 data.initProfileFd = profileFd;
712 data.initAutoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 queueOrSendMessage(H.BIND_APPLICATION, data);
714 }
715
716 public final void scheduleExit() {
717 queueOrSendMessage(H.EXIT_APPLICATION, null);
718 }
719
Christopher Tate5e1ab332009-09-01 20:32:49 -0700720 public final void scheduleSuicide() {
721 queueOrSendMessage(H.SUICIDE, null);
722 }
723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 public void requestThumbnail(IBinder token) {
725 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
726 }
727
728 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700729 updatePendingConfiguration(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
731 }
732
733 public void updateTimeZone() {
734 TimeZone.setDefault(null);
735 }
736
Robert Greenwalt03595d02010-11-02 14:08:23 -0700737 public void clearDnsCache() {
738 // a non-standard API to get this to libcore
739 InetAddress.clearDnsCache();
740 }
741
Robert Greenwalt434203a2010-10-11 16:00:27 -0700742 public void setHttpProxy(String host, String port, String exclList) {
743 Proxy.setHttpProxySystemProperty(host, port, exclList);
744 }
745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 public void processInBackground() {
747 mH.removeMessages(H.GC_WHEN_IDLE);
748 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
749 }
750
751 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700752 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700753 try {
754 data.fd = ParcelFileDescriptor.dup(fd);
755 data.token = servicetoken;
756 data.args = args;
757 queueOrSendMessage(H.DUMP_SERVICE, data);
758 } catch (IOException e) {
759 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 }
761 }
762
763 // This function exists to make sure all receiver dispatching is
764 // correctly ordered, since these are one-way calls and the binder driver
765 // applies transaction ordering per object for such calls.
766 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700767 int resultCode, String dataStr, Bundle extras, boolean ordered,
768 boolean sticky) throws RemoteException {
769 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 }
Bob Leee5408332009-09-04 18:31:17 -0700771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 public void scheduleLowMemory() {
773 queueOrSendMessage(H.LOW_MEMORY, null);
774 }
775
776 public void scheduleActivityConfigurationChanged(IBinder token) {
777 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
778 }
779
Romain Guy7eabe552011-07-21 14:56:34 -0700780 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
781 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700782 ProfilerControlData pcd = new ProfilerControlData();
783 pcd.path = path;
784 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700785 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800786 }
787
Andy McFadden824c5102010-07-09 16:26:57 -0700788 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
789 DumpHeapData dhd = new DumpHeapData();
790 dhd.path = path;
791 dhd.fd = fd;
792 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
793 }
794
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700795 public void setSchedulingGroup(int group) {
796 // Note: do this immediately, since going into the foreground
797 // should happen regardless of what pending work we have to do
798 // and the activity manager will wait for us to report back that
799 // we are done before sending us to the background.
800 try {
801 Process.setProcessGroup(Process.myPid(), group);
802 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800803 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700804 }
805 }
Bob Leee5408332009-09-04 18:31:17 -0700806
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700807 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
808 Debug.getMemoryInfo(outInfo);
809 }
Bob Leee5408332009-09-04 18:31:17 -0700810
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700811 public void dispatchPackageBroadcast(int cmd, String[] packages) {
812 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
813 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700814
815 public void scheduleCrash(String msg) {
816 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
817 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700818
Dianne Hackborn30d71892010-12-11 10:37:55 -0800819 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
820 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700821 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700822 try {
823 data.fd = ParcelFileDescriptor.dup(fd);
824 data.token = activitytoken;
825 data.prefix = prefix;
826 data.args = args;
827 queueOrSendMessage(H.DUMP_ACTIVITY, data);
828 } catch (IOException e) {
829 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700830 }
831 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700832
Marco Nelissen18cb2872011-11-15 11:19:53 -0800833 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
834 String[] args) {
835 DumpComponentInfo data = new DumpComponentInfo();
836 try {
837 data.fd = ParcelFileDescriptor.dup(fd);
838 data.token = providertoken;
839 data.args = args;
840 queueOrSendMessage(H.DUMP_PROVIDER, data);
841 } catch (IOException e) {
842 Slog.w(TAG, "dumpProvider failed", e);
843 }
844 }
845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700847 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
848 boolean all, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700849 FileOutputStream fout = new FileOutputStream(fd);
850 PrintWriter pw = new PrintWriter(fout);
851 try {
Romain Guya998dff2012-03-23 18:58:36 -0700852 return dumpMemInfo(pw, checkin, all);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700853 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700854 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700855 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700856 }
857
Romain Guya998dff2012-03-23 18:58:36 -0700858 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean all) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 long nativeMax = Debug.getNativeHeapSize() / 1024;
860 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
861 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
862
863 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
864 Debug.getMemoryInfo(memInfo);
865
Dianne Hackbornb437e092011-08-05 17:50:29 -0700866 if (!all) {
867 return memInfo;
868 }
869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 Runtime runtime = Runtime.getRuntime();
871
872 long dalvikMax = runtime.totalMemory() / 1024;
873 long dalvikFree = runtime.freeMemory() / 1024;
874 long dalvikAllocated = dalvikMax - dalvikFree;
875 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700876 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700877 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
878 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 int globalAssetCount = AssetManager.getGlobalAssetCount();
880 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
881 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
882 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
883 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700884 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800885 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700888 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 // NOTE: if you change anything significant below, also consider changing
890 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700891 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 // Header
895 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
896 pw.print(Process.myPid()); pw.print(',');
897 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 // Heap info - max
900 pw.print(nativeMax); pw.print(',');
901 pw.print(dalvikMax); pw.print(',');
902 pw.print("N/A,");
903 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 // Heap info - allocated
906 pw.print(nativeAllocated); pw.print(',');
907 pw.print(dalvikAllocated); pw.print(',');
908 pw.print("N/A,");
909 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 // Heap info - free
912 pw.print(nativeFree); pw.print(',');
913 pw.print(dalvikFree); pw.print(',');
914 pw.print("N/A,");
915 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 // Heap info - proportional set size
918 pw.print(memInfo.nativePss); pw.print(',');
919 pw.print(memInfo.dalvikPss); pw.print(',');
920 pw.print(memInfo.otherPss); pw.print(',');
921 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700924 pw.print(memInfo.nativeSharedDirty); pw.print(',');
925 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
926 pw.print(memInfo.otherSharedDirty); pw.print(',');
927 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
928 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700931 pw.print(memInfo.nativePrivateDirty); pw.print(',');
932 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
933 pw.print(memInfo.otherPrivateDirty); pw.print(',');
934 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
935 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 // Object counts
938 pw.print(viewInstanceCount); pw.print(',');
939 pw.print(viewRootInstanceCount); pw.print(',');
940 pw.print(appContextInstanceCount); pw.print(',');
941 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 pw.print(globalAssetCount); pw.print(',');
944 pw.print(globalAssetManagerCount); pw.print(',');
945 pw.print(binderLocalObjectCount); pw.print(',');
946 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 pw.print(binderDeathObjectCount); pw.print(',');
949 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 // SQL
Vasu Noric3849202010-03-09 10:47:25 -0800952 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -0800953 pw.print(stats.memoryUsed / 1024); pw.print(',');
954 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700955 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800956 for (int i = 0; i < stats.dbStats.size(); i++) {
957 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700958 pw.print(','); pw.print(dbStats.dbName);
959 pw.print(','); pw.print(dbStats.pageSize);
960 pw.print(','); pw.print(dbStats.dbSize);
961 pw.print(','); pw.print(dbStats.lookaside);
962 pw.print(','); pw.print(dbStats.cache);
963 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800964 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700965 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700966
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700967 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 }
Bob Leee5408332009-09-04 18:31:17 -0700969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700971 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
972 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
973 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
974 "------");
975 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
976 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
977 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
978 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700980 int otherPss = memInfo.otherPss;
981 int otherSharedDirty = memInfo.otherSharedDirty;
982 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700984 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Romain Guy65b345f2011-07-27 18:51:50 -0700985 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700986 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
987 memInfo.getOtherPrivateDirty(i), "", "", "");
988 otherPss -= memInfo.getOtherPss(i);
989 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
990 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
991 }
992
993 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
994 otherPrivateDirty, "", "", "");
995 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
996 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
997 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
998 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999
1000 pw.println(" ");
1001 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001002 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 viewRootInstanceCount);
1004
1005 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1006 "Activities:", activityInstanceCount);
1007
1008 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1009 "AssetManagers:", globalAssetManagerCount);
1010
1011 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1012 "Proxy Binders:", binderProxyObjectCount);
1013 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
1014
1015 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 // SQLite mem info
1018 pw.println(" ");
1019 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001020 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1021 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1022 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001023 pw.println(" ");
1024 int N = stats.dbStats.size();
1025 if (N > 0) {
1026 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001027 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1028 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001029 for (int i = 0; i < N; i++) {
1030 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001031 printRow(pw, DB_INFO_FORMAT,
1032 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1033 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1034 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1035 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001036 }
1037 }
Bob Leee5408332009-09-04 18:31:17 -07001038
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001039 // Asset details.
1040 String assetAlloc = AssetManager.getAssetAllocations();
1041 if (assetAlloc != null) {
1042 pw.println(" ");
1043 pw.println(" Asset Allocations");
1044 pw.print(assetAlloc);
1045 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001046
1047 return memInfo;
1048 }
1049
1050 @Override
1051 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1052 dumpGraphicsInfo(fd);
Romain Guy65b345f2011-07-27 18:51:50 -07001053 WindowManagerImpl.getDefault().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 }
1055
Jeff Brown6754ba22011-12-14 20:20:01 -08001056 @Override
1057 public void dumpDbInfo(FileDescriptor fd, String[] args) {
1058 PrintWriter pw = new PrintWriter(new FileOutputStream(fd));
1059 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1060 SQLiteDebug.dump(printer, args);
1061 pw.flush();
1062 }
1063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 private void printRow(PrintWriter pw, String format, Object...objs) {
1065 pw.println(String.format(format, objs));
1066 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001067
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001068 public void setCoreSettings(Bundle coreSettings) {
1069 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001070 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001071
1072 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1073 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1074 ucd.pkg = pkg;
1075 ucd.info = info;
1076 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1077 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001078
1079 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001080 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001081 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 }
1084
Romain Guy65b345f2011-07-27 18:51:50 -07001085 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 public static final int LAUNCH_ACTIVITY = 100;
1087 public static final int PAUSE_ACTIVITY = 101;
1088 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1089 public static final int STOP_ACTIVITY_SHOW = 103;
1090 public static final int STOP_ACTIVITY_HIDE = 104;
1091 public static final int SHOW_WINDOW = 105;
1092 public static final int HIDE_WINDOW = 106;
1093 public static final int RESUME_ACTIVITY = 107;
1094 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001095 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 public static final int BIND_APPLICATION = 110;
1097 public static final int EXIT_APPLICATION = 111;
1098 public static final int NEW_INTENT = 112;
1099 public static final int RECEIVER = 113;
1100 public static final int CREATE_SERVICE = 114;
1101 public static final int SERVICE_ARGS = 115;
1102 public static final int STOP_SERVICE = 116;
1103 public static final int REQUEST_THUMBNAIL = 117;
1104 public static final int CONFIGURATION_CHANGED = 118;
1105 public static final int CLEAN_UP_CONTEXT = 119;
1106 public static final int GC_WHEN_IDLE = 120;
1107 public static final int BIND_SERVICE = 121;
1108 public static final int UNBIND_SERVICE = 122;
1109 public static final int DUMP_SERVICE = 123;
1110 public static final int LOW_MEMORY = 124;
1111 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1112 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001113 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001114 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001115 public static final int DESTROY_BACKUP_AGENT = 129;
1116 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001117 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001118 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001119 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001120 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001121 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001122 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001123 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001124 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001125 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001126 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001127 public static final int DUMP_PROVIDER = 141;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001129 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 switch (code) {
1131 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1132 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1133 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1134 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1135 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1136 case SHOW_WINDOW: return "SHOW_WINDOW";
1137 case HIDE_WINDOW: return "HIDE_WINDOW";
1138 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1139 case SEND_RESULT: return "SEND_RESULT";
1140 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1141 case BIND_APPLICATION: return "BIND_APPLICATION";
1142 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1143 case NEW_INTENT: return "NEW_INTENT";
1144 case RECEIVER: return "RECEIVER";
1145 case CREATE_SERVICE: return "CREATE_SERVICE";
1146 case SERVICE_ARGS: return "SERVICE_ARGS";
1147 case STOP_SERVICE: return "STOP_SERVICE";
1148 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1149 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1150 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1151 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1152 case BIND_SERVICE: return "BIND_SERVICE";
1153 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1154 case DUMP_SERVICE: return "DUMP_SERVICE";
1155 case LOW_MEMORY: return "LOW_MEMORY";
1156 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1157 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001158 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001159 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1160 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001161 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001162 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001163 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001164 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001165 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001166 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001167 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001168 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001169 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001170 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001171 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001172 case DUMP_PROVIDER: return "DUMP_PROVIDER";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 }
1174 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001175 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 }
1177 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001178 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 switch (msg.what) {
1180 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001181 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001182 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183
1184 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001185 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001186 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001187 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 } break;
1189 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001190 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001191 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001192 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001193 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 } break;
1195 case PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001196 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001198 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001199 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 break;
1201 case PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001202 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001204 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 break;
1206 case STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001207 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001209 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 break;
1211 case STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001212 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001214 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 break;
1216 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001217 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001219 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 break;
1221 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001222 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001224 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 break;
1226 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001227 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 handleResumeActivity((IBinder)msg.obj, true,
1229 msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001230 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 break;
1232 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001233 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001235 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 break;
1237 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001238 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1240 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001241 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 break;
1243 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001244 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 AppBindData data = (AppBindData)msg.obj;
1246 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001247 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 break;
1249 case EXIT_APPLICATION:
1250 if (mInitialApplication != null) {
1251 mInitialApplication.onTerminate();
1252 }
1253 Looper.myLooper().quit();
1254 break;
1255 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001256 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001258 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 break;
1260 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001261 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001263 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001264 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 break;
1266 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001267 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001269 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 break;
1271 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001272 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001274 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 break;
1276 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001277 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001279 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 break;
1281 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001282 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001284 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 break;
1286 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001287 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001289 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001290 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 break;
1292 case REQUEST_THUMBNAIL:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001293 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "requestThumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 handleRequestThumbnail((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001295 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 break;
1297 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001298 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001299 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001300 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 break;
1302 case CLEAN_UP_CONTEXT:
1303 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1304 cci.context.performFinalCleanup(cci.who, cci.what);
1305 break;
1306 case GC_WHEN_IDLE:
1307 scheduleGcIdler();
1308 break;
1309 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001310 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 break;
1312 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001313 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001315 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 break;
1317 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001318 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001320 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001322 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001323 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001324 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001325 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001326 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001327 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001328 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001329 break;
1330 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001331 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001332 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001333 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001334 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001335 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001336 Process.killProcess(Process.myPid());
1337 break;
1338 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001339 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001340 completeRemoveProvider((IContentProvider)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001341 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001342 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001343 case ENABLE_JIT:
1344 ensureJitEnabled();
1345 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001346 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001347 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001348 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001349 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001350 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001351 case SCHEDULE_CRASH:
1352 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001353 case DUMP_HEAP:
1354 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1355 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001356 case DUMP_ACTIVITY:
1357 handleDumpActivity((DumpComponentInfo)msg.obj);
1358 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001359 case DUMP_PROVIDER:
1360 handleDumpProvider((DumpComponentInfo)msg.obj);
1361 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001362 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001363 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001364 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001365 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001366 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001367 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001368 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001369 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001370 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001371 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001372 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1373 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001374 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001375 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001376 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001377 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001378 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001379 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001381 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 }
Bob Leee5408332009-09-04 18:31:17 -07001383
Brian Carlstromed7e0072011-03-24 13:27:57 -07001384 private void maybeSnapshot() {
1385 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001386 // convert the *private* ActivityThread.PackageInfo to *public* known
1387 // android.content.pm.PackageInfo
1388 String packageName = mBoundApplication.info.mPackageName;
1389 android.content.pm.PackageInfo packageInfo = null;
1390 try {
1391 Context context = getSystemContext();
1392 if(context == null) {
1393 Log.e(TAG, "cannot get a valid context");
1394 return;
1395 }
1396 PackageManager pm = context.getPackageManager();
1397 if(pm == null) {
1398 Log.e(TAG, "cannot get a valid PackageManager");
1399 return;
1400 }
1401 packageInfo = pm.getPackageInfo(
1402 packageName, PackageManager.GET_ACTIVITIES);
1403 } catch (NameNotFoundException e) {
1404 Log.e(TAG, "cannot get package info for " + packageName, e);
1405 }
1406 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001407 }
1408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 }
1410
Romain Guy65b345f2011-07-27 18:51:50 -07001411 private class Idler implements MessageQueue.IdleHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001413 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001414 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001415 if (mBoundApplication != null && mProfiler.profileFd != null
1416 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001417 stopProfiling = true;
1418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 if (a != null) {
1420 mNewActivities = null;
1421 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001422 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001424 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 TAG, "Reporting idle of " + a +
1426 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001427 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 if (a.activity != null && !a.activity.mFinished) {
1429 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001430 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001431 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001433 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 }
1435 }
1436 prev = a;
1437 a = a.nextIdle;
1438 prev.nextIdle = null;
1439 } while (a != null);
1440 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001441 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001442 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001443 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001444 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 return false;
1446 }
1447 }
1448
1449 final class GcIdler implements MessageQueue.IdleHandler {
1450 public final boolean queueIdle() {
1451 doGcIfNeeded();
1452 return false;
1453 }
1454 }
1455
Romain Guy65b345f2011-07-27 18:51:50 -07001456 private static class ResourcesKey {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001457 final private String mResDir;
1458 final private float mScale;
1459 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001460
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001461 ResourcesKey(String resDir, float scale) {
1462 mResDir = resDir;
1463 mScale = scale;
1464 mHash = mResDir.hashCode() << 2 + (int) (mScale * 2);
1465 }
Bob Leee5408332009-09-04 18:31:17 -07001466
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001467 @Override
1468 public int hashCode() {
1469 return mHash;
1470 }
1471
1472 @Override
1473 public boolean equals(Object obj) {
1474 if (!(obj instanceof ResourcesKey)) {
1475 return false;
1476 }
1477 ResourcesKey peer = (ResourcesKey) obj;
1478 return mResDir.equals(peer.mResDir) && mScale == peer.mScale;
1479 }
1480 }
1481
Romain Guy65b345f2011-07-27 18:51:50 -07001482 public static ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001483 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001484 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485
Romain Guy65b345f2011-07-27 18:51:50 -07001486 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001487 ActivityThread am = currentActivityThread();
1488 return (am != null && am.mBoundApplication != null)
1489 ? am.mBoundApplication.processName : null;
1490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491
Romain Guy65b345f2011-07-27 18:51:50 -07001492 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001493 ActivityThread am = currentActivityThread();
1494 return am != null ? am.mInitialApplication : null;
1495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001497 public static IPackageManager getPackageManager() {
1498 if (sPackageManager != null) {
1499 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1500 return sPackageManager;
1501 }
1502 IBinder b = ServiceManager.getService("package");
1503 //Slog.v("PackageManager", "default service binder = " + b);
1504 sPackageManager = IPackageManager.Stub.asInterface(b);
1505 //Slog.v("PackageManager", "default service = " + sPackageManager);
1506 return sPackageManager;
1507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001509 DisplayMetrics getDisplayMetricsLocked(CompatibilityInfo ci, boolean forceUpdate) {
1510 DisplayMetrics dm = mDisplayMetrics.get(ci);
1511 if (dm != null && !forceUpdate) {
1512 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001513 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001514 if (dm == null) {
1515 dm = new DisplayMetrics();
1516 mDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001517 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001518 Display d = WindowManagerImpl.getDefault(ci).getDefaultDisplay();
1519 d.getMetrics(dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001520 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1521 // + metrics.heightPixels + " den=" + metrics.density
1522 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001523 return dm;
1524 }
1525
Romain Guy65b345f2011-07-27 18:51:50 -07001526 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001527 Configuration applyConfigCompatMainThread(Configuration config, CompatibilityInfo compat) {
1528 if (config == null) {
1529 return null;
1530 }
1531 if (compat != null && !compat.supportsScreen()) {
1532 mMainThreadConfig.setTo(config);
1533 config = mMainThreadConfig;
1534 compat.applyToConfiguration(config);
1535 }
1536 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001539 /**
1540 * Creates the top level Resources for applications with the given compatibility info.
1541 *
1542 * @param resDir the resource directory.
1543 * @param compInfo the compability info. It will use the default compatibility info when it's
1544 * null.
1545 */
1546 Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) {
1547 ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale);
1548 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001550 // Resources is app scale dependent.
1551 if (false) {
1552 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1553 + compInfo.applicationScale);
1554 }
1555 WeakReference<Resources> wr = mActiveResources.get(key);
1556 r = wr != null ? wr.get() : null;
1557 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1558 if (r != null && r.getAssets().isUpToDate()) {
1559 if (false) {
1560 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1561 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1562 }
1563 return r;
1564 }
1565 }
1566
1567 //if (r != null) {
1568 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1569 // + r + " " + resDir);
1570 //}
1571
1572 AssetManager assets = new AssetManager();
1573 if (assets.addAssetPath(resDir) == 0) {
1574 return null;
1575 }
1576
1577 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Dianne Hackborn836e2622011-10-04 18:32:39 -07001578 DisplayMetrics metrics = getDisplayMetricsLocked(null, false);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001579 r = new Resources(assets, metrics, getConfiguration(), compInfo);
1580 if (false) {
1581 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1582 + r.getConfiguration() + " appScale="
1583 + r.getCompatibilityInfo().applicationScale);
1584 }
1585
1586 synchronized (mPackages) {
1587 WeakReference<Resources> wr = mActiveResources.get(key);
1588 Resources existing = wr != null ? wr.get() : null;
1589 if (existing != null && existing.getAssets().isUpToDate()) {
1590 // Someone else already created the resources while we were
1591 // unlocked; go ahead and use theirs.
1592 r.getAssets().close();
1593 return existing;
1594 }
1595
1596 // XXX need to remove entries when weak references go away
1597 mActiveResources.put(key, new WeakReference<Resources>(r));
1598 return r;
1599 }
1600 }
1601
1602 /**
1603 * Creates the top level resources for the given package.
1604 */
1605 Resources getTopLevelResources(String resDir, LoadedApk pkgInfo) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001606 return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001607 }
1608
1609 final Handler getHandler() {
1610 return mH;
1611 }
1612
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001613 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1614 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001615 synchronized (mPackages) {
1616 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1618 ref = mPackages.get(packageName);
1619 } else {
1620 ref = mResourcePackages.get(packageName);
1621 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001622 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001623 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001624 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1625 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 if (packageInfo != null && (packageInfo.mResources == null
1627 || packageInfo.mResources.getAssets().isUpToDate())) {
1628 if (packageInfo.isSecurityViolation()
1629 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1630 throw new SecurityException(
1631 "Requesting code from " + packageName
1632 + " to be run in process "
1633 + mBoundApplication.processName
1634 + "/" + mBoundApplication.appInfo.uid);
1635 }
1636 return packageInfo;
1637 }
1638 }
1639
1640 ApplicationInfo ai = null;
1641 try {
1642 ai = getPackageManager().getApplicationInfo(packageName,
Amith Yamasani483f3b02012-03-13 16:08:00 -07001643 PackageManager.GET_SHARED_LIBRARY_FILES, UserId.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001645 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 }
1647
1648 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001649 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 }
1651
1652 return null;
1653 }
1654
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001655 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1656 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1658 boolean securityViolation = includeCode && ai.uid != 0
1659 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Amith Yamasani742a6712011-05-04 14:49:28 -07001660 ? !UserId.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
1661 : true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1663 |Context.CONTEXT_IGNORE_SECURITY))
1664 == Context.CONTEXT_INCLUDE_CODE) {
1665 if (securityViolation) {
1666 String msg = "Requesting code from " + ai.packageName
1667 + " (with uid " + ai.uid + ")";
1668 if (mBoundApplication != null) {
1669 msg = msg + " to be run in process "
1670 + mBoundApplication.processName + " (with uid "
1671 + mBoundApplication.appInfo.uid + ")";
1672 }
1673 throw new SecurityException(msg);
1674 }
1675 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001676 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 }
1678
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001679 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1680 CompatibilityInfo compatInfo) {
1681 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 }
1683
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001684 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1685 synchronized (mPackages) {
1686 WeakReference<LoadedApk> ref;
1687 if (includeCode) {
1688 ref = mPackages.get(packageName);
1689 } else {
1690 ref = mResourcePackages.get(packageName);
1691 }
1692 return ref != null ? ref.get() : null;
1693 }
1694 }
1695
Romain Guy65b345f2011-07-27 18:51:50 -07001696 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1698 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001699 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 if (includeCode) {
1701 ref = mPackages.get(aInfo.packageName);
1702 } else {
1703 ref = mResourcePackages.get(aInfo.packageName);
1704 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001705 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 if (packageInfo == null || (packageInfo.mResources != null
1707 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001708 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 : "Loading resource-only package ") + aInfo.packageName
1710 + " (in " + (mBoundApplication != null
1711 ? mBoundApplication.processName : null)
1712 + ")");
1713 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001714 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 securityViolation, includeCode &&
1716 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1717 if (includeCode) {
1718 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001719 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 } else {
1721 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001722 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 }
1724 }
1725 return packageInfo;
1726 }
1727 }
1728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 ActivityThread() {
1730 }
1731
1732 public ApplicationThread getApplicationThread()
1733 {
1734 return mAppThread;
1735 }
1736
1737 public Instrumentation getInstrumentation()
1738 {
1739 return mInstrumentation;
1740 }
1741
1742 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001743 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 }
1745
1746 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001747 return mProfiler != null && mProfiler.profileFile != null
1748 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 }
1750
1751 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001752 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 }
1754
1755 public Looper getLooper() {
1756 return mLooper;
1757 }
1758
1759 public Application getApplication() {
1760 return mInitialApplication;
1761 }
Bob Leee5408332009-09-04 18:31:17 -07001762
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001763 public String getProcessName() {
1764 return mBoundApplication.processName;
1765 }
Bob Leee5408332009-09-04 18:31:17 -07001766
Dianne Hackborn21556372010-02-04 16:34:40 -08001767 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 synchronized (this) {
1769 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001770 ContextImpl context =
1771 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001772 LoadedApk info = new LoadedApk(this, "android", context, null,
1773 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 context.init(info, null, this);
1775 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001776 getConfiguration(), getDisplayMetricsLocked(
1777 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001779 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 // + ": " + context.getResources().getConfiguration());
1781 }
1782 }
1783 return mSystemContext;
1784 }
1785
Mike Cleron432b7132009-09-24 15:28:29 -07001786 public void installSystemApplicationInfo(ApplicationInfo info) {
1787 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001788 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001789 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001790 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001791
1792 // give ourselves a default profiler
1793 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001794 }
1795 }
1796
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001797 void ensureJitEnabled() {
1798 if (!mJitEnabled) {
1799 mJitEnabled = true;
1800 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1801 }
1802 }
1803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 void scheduleGcIdler() {
1805 if (!mGcIdlerScheduled) {
1806 mGcIdlerScheduled = true;
1807 Looper.myQueue().addIdleHandler(mGcIdler);
1808 }
1809 mH.removeMessages(H.GC_WHEN_IDLE);
1810 }
1811
1812 void unscheduleGcIdler() {
1813 if (mGcIdlerScheduled) {
1814 mGcIdlerScheduled = false;
1815 Looper.myQueue().removeIdleHandler(mGcIdler);
1816 }
1817 mH.removeMessages(H.GC_WHEN_IDLE);
1818 }
1819
1820 void doGcIfNeeded() {
1821 mGcIdlerScheduled = false;
1822 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001823 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 // + "m now=" + now);
1825 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001826 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 BinderInternal.forceGc("bg");
1828 }
1829 }
1830
Jeff Hamilton52d32032011-01-08 15:31:26 -06001831 public void registerOnActivityPausedListener(Activity activity,
1832 OnActivityPausedListener listener) {
1833 synchronized (mOnPauseListeners) {
1834 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1835 if (list == null) {
1836 list = new ArrayList<OnActivityPausedListener>();
1837 mOnPauseListeners.put(activity, list);
1838 }
1839 list.add(listener);
1840 }
1841 }
1842
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001843 public void unregisterOnActivityPausedListener(Activity activity,
1844 OnActivityPausedListener listener) {
1845 synchronized (mOnPauseListeners) {
1846 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1847 if (list != null) {
1848 list.remove(listener);
1849 }
1850 }
1851 }
1852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 public final ActivityInfo resolveActivityInfo(Intent intent) {
1854 ActivityInfo aInfo = intent.resolveActivityInfo(
1855 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1856 if (aInfo == null) {
1857 // Throw an exception.
1858 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07001859 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 }
1861 return aInfo;
1862 }
Bob Leee5408332009-09-04 18:31:17 -07001863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001866 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001867 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001869 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 r.intent = intent;
1871 r.state = state;
1872 r.parent = parent;
1873 r.embeddedID = id;
1874 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001875 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 if (localLOGV) {
1877 ComponentName compname = intent.getComponent();
1878 String name;
1879 if (compname != null) {
1880 name = compname.toShortString();
1881 } else {
1882 name = "(Intent " + intent + ").getComponent() returned null";
1883 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001884 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 + ", comp=" + name
1886 + ", token=" + token);
1887 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001888 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 }
1890
1891 public final Activity getActivity(IBinder token) {
1892 return mActivities.get(token).activity;
1893 }
1894
1895 public final void sendActivityResult(
1896 IBinder token, String id, int requestCode,
1897 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001898 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001899 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1901 list.add(new ResultInfo(id, requestCode, resultCode, data));
1902 mAppThread.scheduleSendResult(token, list);
1903 }
1904
1905 // if the thread hasn't started yet, we don't have the handler, so just
1906 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07001907 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 queueOrSendMessage(what, obj, 0, 0);
1909 }
1910
Romain Guy65b345f2011-07-27 18:51:50 -07001911 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 queueOrSendMessage(what, obj, arg1, 0);
1913 }
1914
Romain Guy65b345f2011-07-27 18:51:50 -07001915 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001917 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1919 + ": " + arg1 + " / " + obj);
1920 Message msg = Message.obtain();
1921 msg.what = what;
1922 msg.obj = obj;
1923 msg.arg1 = arg1;
1924 msg.arg2 = arg2;
1925 mH.sendMessage(msg);
1926 }
1927 }
1928
Dianne Hackborn21556372010-02-04 16:34:40 -08001929 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 String what) {
1931 ContextCleanupInfo cci = new ContextCleanupInfo();
1932 cci.context = context;
1933 cci.who = who;
1934 cci.what = what;
1935 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1936 }
1937
Romain Guy65b345f2011-07-27 18:51:50 -07001938 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1940
1941 ActivityInfo aInfo = r.activityInfo;
1942 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001943 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 Context.CONTEXT_INCLUDE_CODE);
1945 }
Bob Leee5408332009-09-04 18:31:17 -07001946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 ComponentName component = r.intent.getComponent();
1948 if (component == null) {
1949 component = r.intent.resolveActivity(
1950 mInitialApplication.getPackageManager());
1951 r.intent.setComponent(component);
1952 }
1953
1954 if (r.activityInfo.targetActivity != null) {
1955 component = new ComponentName(r.activityInfo.packageName,
1956 r.activityInfo.targetActivity);
1957 }
1958
1959 Activity activity = null;
1960 try {
1961 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
1962 activity = mInstrumentation.newActivity(
1963 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08001964 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 r.intent.setExtrasClassLoader(cl);
1966 if (r.state != null) {
1967 r.state.setClassLoader(cl);
1968 }
1969 } catch (Exception e) {
1970 if (!mInstrumentation.onException(activity, e)) {
1971 throw new RuntimeException(
1972 "Unable to instantiate activity " + component
1973 + ": " + e.toString(), e);
1974 }
1975 }
1976
1977 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001978 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001979
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001980 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
1981 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 TAG, r + ": app=" + app
1983 + ", appName=" + app.getPackageName()
1984 + ", pkg=" + r.packageInfo.getPackageName()
1985 + ", comp=" + r.intent.getComponent().toShortString()
1986 + ", dir=" + r.packageInfo.getAppDir());
1987
1988 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001989 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 appContext.init(r.packageInfo, r.token, this);
1991 appContext.setOuterContext(activity);
1992 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001993 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001994 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07001995 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001996 activity.attach(appContext, this, getInstrumentation(), r.token,
1997 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001998 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07001999
Christopher Tateb70f3df2009-04-07 16:07:59 -07002000 if (customIntent != null) {
2001 activity.mIntent = customIntent;
2002 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002003 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 activity.mStartedActivity = false;
2005 int theme = r.activityInfo.getThemeResource();
2006 if (theme != 0) {
2007 activity.setTheme(theme);
2008 }
2009
2010 activity.mCalled = false;
2011 mInstrumentation.callActivityOnCreate(activity, r.state);
2012 if (!activity.mCalled) {
2013 throw new SuperNotCalledException(
2014 "Activity " + r.intent.getComponent().toShortString() +
2015 " did not call through to super.onCreate()");
2016 }
2017 r.activity = activity;
2018 r.stopped = true;
2019 if (!r.activity.mFinished) {
2020 activity.performStart();
2021 r.stopped = false;
2022 }
2023 if (!r.activity.mFinished) {
2024 if (r.state != null) {
2025 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2026 }
2027 }
2028 if (!r.activity.mFinished) {
2029 activity.mCalled = false;
2030 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2031 if (!activity.mCalled) {
2032 throw new SuperNotCalledException(
2033 "Activity " + r.intent.getComponent().toShortString() +
2034 " did not call through to super.onPostCreate()");
2035 }
2036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 }
2038 r.paused = true;
2039
2040 mActivities.put(r.token, r);
2041
2042 } catch (SuperNotCalledException e) {
2043 throw e;
2044
2045 } catch (Exception e) {
2046 if (!mInstrumentation.onException(activity, e)) {
2047 throw new RuntimeException(
2048 "Unable to start activity " + component
2049 + ": " + e.toString(), e);
2050 }
2051 }
2052
2053 return activity;
2054 }
2055
Romain Guy65b345f2011-07-27 18:51:50 -07002056 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 // If we are getting ready to gc after going to the background, well
2058 // we are back active so skip it.
2059 unscheduleGcIdler();
2060
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002061 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002062 mProfiler.setProfiler(r.profileFile, r.profileFd);
2063 mProfiler.startProfiling();
2064 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002065 }
2066
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002067 // Make sure we are running with the most recent config.
2068 handleConfigurationChanged(null, null);
2069
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002070 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07002072 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073
2074 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002075 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002076 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 handleResumeActivity(r.token, false, r.isForward);
2078
2079 if (!r.activity.mFinished && r.startsNotResumed) {
2080 // The activity manager actually wants this one to start out
2081 // paused, because it needs to be visible but isn't in the
2082 // foreground. We accomplish this by going through the
2083 // normal startup (because activities expect to go through
2084 // onResume() the first time they run, before their window
2085 // is displayed), and then pausing it. However, in this case
2086 // we do -not- need to do the full pause cycle (of freezing
2087 // and such) because the activity manager assumes it can just
2088 // retain the current state it has.
2089 try {
2090 r.activity.mCalled = false;
2091 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002092 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002093 // we need to be created again. But we only do this
2094 // for pre-Honeycomb apps, which always save their state
2095 // when pausing, so we can not have them save their state
2096 // when restarting from a paused state. For HC and later,
2097 // we want to (and can) let the state be saved as the normal
2098 // part of stopping the activity.
2099 if (r.isPreHoneycomb()) {
2100 r.state = oldState;
2101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 if (!r.activity.mCalled) {
2103 throw new SuperNotCalledException(
2104 "Activity " + r.intent.getComponent().toShortString() +
2105 " did not call through to super.onPause()");
2106 }
2107
2108 } catch (SuperNotCalledException e) {
2109 throw e;
2110
2111 } catch (Exception e) {
2112 if (!mInstrumentation.onException(r.activity, e)) {
2113 throw new RuntimeException(
2114 "Unable to pause activity "
2115 + r.intent.getComponent().toShortString()
2116 + ": " + e.toString(), e);
2117 }
2118 }
2119 r.paused = true;
2120 }
2121 } else {
2122 // If there was an error, for any reason, tell the activity
2123 // manager to stop us.
2124 try {
2125 ActivityManagerNative.getDefault()
2126 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2127 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002128 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 }
2130 }
2131 }
2132
Romain Guy65b345f2011-07-27 18:51:50 -07002133 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 List<Intent> intents) {
2135 final int N = intents.size();
2136 for (int i=0; i<N; i++) {
2137 Intent intent = intents.get(i);
2138 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002139 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2141 }
2142 }
2143
2144 public final void performNewIntents(IBinder token,
2145 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002146 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 if (r != null) {
2148 final boolean resumed = !r.paused;
2149 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002150 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 mInstrumentation.callActivityOnPause(r.activity);
2152 }
2153 deliverNewIntents(r, intents);
2154 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002155 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002156 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 }
2158 }
2159 }
Bob Leee5408332009-09-04 18:31:17 -07002160
Romain Guy65b345f2011-07-27 18:51:50 -07002161 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 performNewIntents(data.token, data.intents);
2163 }
2164
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002165 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2166
2167 /**
2168 * Return the Intent that's currently being handled by a
2169 * BroadcastReceiver on this thread, or null if none.
2170 * @hide
2171 */
2172 public static Intent getIntentBeingBroadcast() {
2173 return sCurrentBroadcastIntent.get();
2174 }
2175
Romain Guy65b345f2011-07-27 18:51:50 -07002176 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 // If we are getting ready to gc after going to the background, well
2178 // we are back active so skip it.
2179 unscheduleGcIdler();
2180
2181 String component = data.intent.getComponent().getClassName();
2182
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002183 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002184 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185
2186 IActivityManager mgr = ActivityManagerNative.getDefault();
2187
Romain Guy65b345f2011-07-27 18:51:50 -07002188 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 try {
2190 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2191 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002192 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2194 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002195 if (DEBUG_BROADCAST) Slog.i(TAG,
2196 "Finishing failed broadcast to " + data.intent.getComponent());
2197 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 throw new RuntimeException(
2199 "Unable to instantiate receiver " + component
2200 + ": " + e.toString(), e);
2201 }
2202
2203 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002204 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002205
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002206 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 TAG, "Performing receive of " + data.intent
2208 + ": app=" + app
2209 + ", appName=" + app.getPackageName()
2210 + ", pkg=" + packageInfo.getPackageName()
2211 + ", comp=" + data.intent.getComponent().toShortString()
2212 + ", dir=" + packageInfo.getAppDir());
2213
Dianne Hackborn21556372010-02-04 16:34:40 -08002214 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002215 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002216 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 receiver.onReceive(context.getReceiverRestrictedContext(),
2218 data.intent);
2219 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002220 if (DEBUG_BROADCAST) Slog.i(TAG,
2221 "Finishing failed broadcast to " + data.intent.getComponent());
2222 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 if (!mInstrumentation.onException(receiver, e)) {
2224 throw new RuntimeException(
2225 "Unable to start receiver " + component
2226 + ": " + e.toString(), e);
2227 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002228 } finally {
2229 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 }
2231
Dianne Hackborne829fef2010-10-26 17:44:01 -07002232 if (receiver.getPendingResult() != null) {
2233 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 }
2235 }
2236
Christopher Tate181fafa2009-05-14 11:12:14 -07002237 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002238 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002239 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002240
2241 // no longer idle; we have backup work to do
2242 unscheduleGcIdler();
2243
2244 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002245 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002246 String packageName = packageInfo.mPackageName;
2247 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002248 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002249 + " already exists");
2250 return;
2251 }
Bob Leee5408332009-09-04 18:31:17 -07002252
Christopher Tate181fafa2009-05-14 11:12:14 -07002253 BackupAgent agent = null;
2254 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002255
Christopher Tate79ec80d2011-06-24 14:58:49 -07002256 // full backup operation but no app-supplied agent? use the default implementation
2257 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2258 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002259 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002260 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002261
Christopher Tate181fafa2009-05-14 11:12:14 -07002262 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002263 IBinder binder = null;
2264 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002265 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2266
Christopher Tated1475e02009-07-09 15:36:17 -07002267 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002268 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002269
2270 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002271 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002272 context.init(packageInfo, null, this);
2273 context.setOuterContext(agent);
2274 agent.attach(context);
2275
2276 agent.onCreate();
2277 binder = agent.onBind();
2278 mBackupAgents.put(packageName, agent);
2279 } catch (Exception e) {
2280 // If this is during restore, fail silently; otherwise go
2281 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002282 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002283 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2284 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002285 throw e;
2286 }
2287 // falling through with 'binder' still null
2288 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002289
2290 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002291 try {
2292 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2293 } catch (RemoteException e) {
2294 // nothing to do.
2295 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002296 } catch (Exception e) {
2297 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002298 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002299 }
2300 }
2301
2302 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002303 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002304 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002305
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002306 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002307 String packageName = packageInfo.mPackageName;
2308 BackupAgent agent = mBackupAgents.get(packageName);
2309 if (agent != null) {
2310 try {
2311 agent.onDestroy();
2312 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002313 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002314 e.printStackTrace();
2315 }
2316 mBackupAgents.remove(packageName);
2317 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002318 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002319 }
2320 }
2321
Romain Guy65b345f2011-07-27 18:51:50 -07002322 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 // If we are getting ready to gc after going to the background, well
2324 // we are back active so skip it.
2325 unscheduleGcIdler();
2326
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002327 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002328 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 Service service = null;
2330 try {
2331 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2332 service = (Service) cl.loadClass(data.info.name).newInstance();
2333 } catch (Exception e) {
2334 if (!mInstrumentation.onException(service, e)) {
2335 throw new RuntimeException(
2336 "Unable to instantiate service " + data.info.name
2337 + ": " + e.toString(), e);
2338 }
2339 }
2340
2341 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002342 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343
Dianne Hackborn21556372010-02-04 16:34:40 -08002344 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 context.init(packageInfo, null, this);
2346
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002347 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 context.setOuterContext(service);
2349 service.attach(context, this, data.info.name, data.token, app,
2350 ActivityManagerNative.getDefault());
2351 service.onCreate();
2352 mServices.put(data.token, service);
2353 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002354 ActivityManagerNative.getDefault().serviceDoneExecuting(
2355 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 } catch (RemoteException e) {
2357 // nothing to do.
2358 }
2359 } catch (Exception e) {
2360 if (!mInstrumentation.onException(service, e)) {
2361 throw new RuntimeException(
2362 "Unable to create service " + data.info.name
2363 + ": " + e.toString(), e);
2364 }
2365 }
2366 }
2367
Romain Guy65b345f2011-07-27 18:51:50 -07002368 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002370 if (DEBUG_SERVICE)
2371 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 if (s != null) {
2373 try {
2374 data.intent.setExtrasClassLoader(s.getClassLoader());
2375 try {
2376 if (!data.rebind) {
2377 IBinder binder = s.onBind(data.intent);
2378 ActivityManagerNative.getDefault().publishService(
2379 data.token, data.intent, binder);
2380 } else {
2381 s.onRebind(data.intent);
2382 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002383 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002385 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386 } catch (RemoteException ex) {
2387 }
2388 } catch (Exception e) {
2389 if (!mInstrumentation.onException(s, e)) {
2390 throw new RuntimeException(
2391 "Unable to bind to service " + s
2392 + " with " + data.intent + ": " + e.toString(), e);
2393 }
2394 }
2395 }
2396 }
2397
Romain Guy65b345f2011-07-27 18:51:50 -07002398 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 Service s = mServices.get(data.token);
2400 if (s != null) {
2401 try {
2402 data.intent.setExtrasClassLoader(s.getClassLoader());
2403 boolean doRebind = s.onUnbind(data.intent);
2404 try {
2405 if (doRebind) {
2406 ActivityManagerNative.getDefault().unbindFinished(
2407 data.token, data.intent, doRebind);
2408 } else {
2409 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002410 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 }
2412 } catch (RemoteException ex) {
2413 }
2414 } catch (Exception e) {
2415 if (!mInstrumentation.onException(s, e)) {
2416 throw new RuntimeException(
2417 "Unable to unbind to service " + s
2418 + " with " + data.intent + ": " + e.toString(), e);
2419 }
2420 }
2421 }
2422 }
2423
Dianne Hackborn625ac272010-09-17 18:29:22 -07002424 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002425 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2426 try {
2427 Service s = mServices.get(info.token);
2428 if (s != null) {
2429 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2430 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2431 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002433 } finally {
2434 IoUtils.closeQuietly(info.fd);
2435 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 }
2437 }
2438
Dianne Hackborn625ac272010-09-17 18:29:22 -07002439 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002440 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2441 try {
2442 ActivityClientRecord r = mActivities.get(info.token);
2443 if (r != null && r.activity != null) {
2444 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2445 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2446 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002447 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002448 } finally {
2449 IoUtils.closeQuietly(info.fd);
2450 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002451 }
2452 }
2453
Marco Nelissen18cb2872011-11-15 11:19:53 -08002454 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002455 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2456 try {
2457 ProviderClientRecord r = mLocalProviders.get(info.token);
2458 if (r != null && r.mLocalProvider != null) {
2459 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2460 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2461 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002462 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002463 } finally {
2464 IoUtils.closeQuietly(info.fd);
2465 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002466 }
2467 }
2468
Romain Guy65b345f2011-07-27 18:51:50 -07002469 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 Service s = mServices.get(data.token);
2471 if (s != null) {
2472 try {
2473 if (data.args != null) {
2474 data.args.setExtrasClassLoader(s.getClassLoader());
2475 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002476 int res;
2477 if (!data.taskRemoved) {
2478 res = s.onStartCommand(data.args, data.flags, data.startId);
2479 } else {
2480 s.onTaskRemoved(data.args);
2481 res = Service.START_TASK_REMOVED_COMPLETE;
2482 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002483
2484 QueuedWork.waitToFinish();
2485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002487 ActivityManagerNative.getDefault().serviceDoneExecuting(
2488 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 } catch (RemoteException e) {
2490 // nothing to do.
2491 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002492 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 } catch (Exception e) {
2494 if (!mInstrumentation.onException(s, e)) {
2495 throw new RuntimeException(
2496 "Unable to start service " + s
2497 + " with " + data.args + ": " + e.toString(), e);
2498 }
2499 }
2500 }
2501 }
2502
Romain Guy65b345f2011-07-27 18:51:50 -07002503 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 Service s = mServices.remove(token);
2505 if (s != null) {
2506 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002507 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 s.onDestroy();
2509 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002510 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002512 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002514
2515 QueuedWork.waitToFinish();
2516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002518 ActivityManagerNative.getDefault().serviceDoneExecuting(
2519 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 } catch (RemoteException e) {
2521 // nothing to do.
2522 }
2523 } catch (Exception e) {
2524 if (!mInstrumentation.onException(s, e)) {
2525 throw new RuntimeException(
2526 "Unable to stop service " + s
2527 + ": " + e.toString(), e);
2528 }
2529 }
2530 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002531 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 }
2533
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002534 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002536 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002537 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 + " finished=" + r.activity.mFinished);
2539 if (r != null && !r.activity.mFinished) {
2540 if (clearHide) {
2541 r.hideForNow = false;
2542 r.activity.mStartedActivity = false;
2543 }
2544 try {
2545 if (r.pendingIntents != null) {
2546 deliverNewIntents(r, r.pendingIntents);
2547 r.pendingIntents = null;
2548 }
2549 if (r.pendingResults != null) {
2550 deliverResults(r, r.pendingResults);
2551 r.pendingResults = null;
2552 }
2553 r.activity.performResume();
2554
Bob Leee5408332009-09-04 18:31:17 -07002555 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 r.paused = false;
2559 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 r.state = null;
2561 } catch (Exception e) {
2562 if (!mInstrumentation.onException(r.activity, e)) {
2563 throw new RuntimeException(
2564 "Unable to resume activity "
2565 + r.intent.getComponent().toShortString()
2566 + ": " + e.toString(), e);
2567 }
2568 }
2569 }
2570 return r;
2571 }
2572
Romain Guya998dff2012-03-23 18:58:36 -07002573 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002574 if (r.mPendingRemoveWindow != null) {
2575 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2576 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2577 if (wtoken != null) {
2578 WindowManagerImpl.getDefault().closeAll(wtoken,
2579 r.activity.getClass().getName(), "Activity");
2580 }
2581 }
2582 r.mPendingRemoveWindow = null;
2583 r.mPendingRemoveWindowManager = null;
2584 }
2585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2587 // If we are getting ready to gc after going to the background, well
2588 // we are back active so skip it.
2589 unscheduleGcIdler();
2590
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002591 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592
2593 if (r != null) {
2594 final Activity a = r.activity;
2595
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002596 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 TAG, "Resume " + r + " started activity: " +
2598 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2599 + ", finished: " + a.mFinished);
2600
2601 final int forwardBit = isForward ?
2602 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 // If the window hasn't yet been added to the window manager,
2605 // and this guy didn't finish itself or start another activity,
2606 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002607 boolean willBeVisible = !a.mStartedActivity;
2608 if (!willBeVisible) {
2609 try {
2610 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2611 a.getActivityToken());
2612 } catch (RemoteException e) {
2613 }
2614 }
2615 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 r.window = r.activity.getWindow();
2617 View decor = r.window.getDecorView();
2618 decor.setVisibility(View.INVISIBLE);
2619 ViewManager wm = a.getWindowManager();
2620 WindowManager.LayoutParams l = r.window.getAttributes();
2621 a.mDecor = decor;
2622 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2623 l.softInputMode |= forwardBit;
2624 if (a.mVisibleFromClient) {
2625 a.mWindowAdded = true;
2626 wm.addView(decor, l);
2627 }
2628
2629 // If the window has already been added, but during resume
2630 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002631 // window visible.
2632 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002633 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 TAG, "Launch " + r + " mStartedActivity set");
2635 r.hideForNow = true;
2636 }
2637
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002638 // Get rid of anything left hanging around.
2639 cleanUpPendingRemoveWindows(r);
2640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 // The window is now visible if it has been added, we are not
2642 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002643 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002644 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002646 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002647 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07002649 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 r.newConfig = null;
2651 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002652 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 + isForward);
2654 WindowManager.LayoutParams l = r.window.getAttributes();
2655 if ((l.softInputMode
2656 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2657 != forwardBit) {
2658 l.softInputMode = (l.softInputMode
2659 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2660 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002661 if (r.activity.mVisibleFromClient) {
2662 ViewManager wm = a.getWindowManager();
2663 View decor = r.window.getDecorView();
2664 wm.updateViewLayout(decor, l);
2665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 }
2667 r.activity.mVisibleFromServer = true;
2668 mNumVisibleActivities++;
2669 if (r.activity.mVisibleFromClient) {
2670 r.activity.makeVisible();
2671 }
2672 }
2673
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002674 if (!r.onlyLocalRequest) {
2675 r.nextIdle = mNewActivities;
2676 mNewActivities = r;
2677 if (localLOGV) Slog.v(
2678 TAG, "Scheduling idle handler for " + r);
2679 Looper.myQueue().addIdleHandler(new Idler());
2680 }
2681 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682
2683 } else {
2684 // If an exception was thrown when trying to resume, then
2685 // just end this activity.
2686 try {
2687 ActivityManagerNative.getDefault()
2688 .finishActivity(token, Activity.RESULT_CANCELED, null);
2689 } catch (RemoteException ex) {
2690 }
2691 }
2692 }
2693
2694 private int mThumbnailWidth = -1;
2695 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002696 private Bitmap mAvailThumbnailBitmap = null;
2697 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698
Romain Guy65b345f2011-07-27 18:51:50 -07002699 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002700 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002702 if (thumbnail == null) {
2703 int w = mThumbnailWidth;
2704 int h;
2705 if (w < 0) {
2706 Resources res = r.activity.getResources();
2707 mThumbnailHeight = h =
2708 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002710 mThumbnailWidth = w =
2711 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2712 } else {
2713 h = mThumbnailHeight;
2714 }
2715
2716 // On platforms where we don't want thumbnails, set dims to (0,0)
2717 if ((w > 0) && (h > 0)) {
2718 thumbnail = Bitmap.createBitmap(w, h, THUMBNAIL_FORMAT);
2719 thumbnail.eraseColor(0);
2720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 }
2722
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002723 if (thumbnail != null) {
2724 Canvas cv = mThumbnailCanvas;
2725 if (cv == null) {
2726 mThumbnailCanvas = cv = new Canvas();
2727 }
2728
2729 cv.setBitmap(thumbnail);
2730 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2731 mAvailThumbnailBitmap = thumbnail;
2732 thumbnail = null;
2733 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002734 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 } catch (Exception e) {
2738 if (!mInstrumentation.onException(r.activity, e)) {
2739 throw new RuntimeException(
2740 "Unable to create thumbnail of "
2741 + r.intent.getComponent().toShortString()
2742 + ": " + e.toString(), e);
2743 }
2744 thumbnail = null;
2745 }
2746
2747 return thumbnail;
2748 }
2749
Romain Guy65b345f2011-07-27 18:51:50 -07002750 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002752 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002754 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 if (userLeaving) {
2756 performUserLeavingActivity(r);
2757 }
Bob Leee5408332009-09-04 18:31:17 -07002758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002760 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002762 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002763 if (r.isPreHoneycomb()) {
2764 QueuedWork.waitToFinish();
2765 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 // Tell the activity manager we have paused.
2768 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002769 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 } catch (RemoteException ex) {
2771 }
2772 }
2773 }
2774
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002775 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 mInstrumentation.callActivityOnUserLeaving(r.activity);
2777 }
2778
2779 final Bundle performPauseActivity(IBinder token, boolean finished,
2780 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002781 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 return r != null ? performPauseActivity(r, finished, saveState) : null;
2783 }
2784
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002785 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 boolean saveState) {
2787 if (r.paused) {
2788 if (r.activity.mFinished) {
2789 // If we are finishing, we won't call onResume() in certain cases.
2790 // So here we likewise don't want to call onPause() if the activity
2791 // isn't resumed.
2792 return null;
2793 }
2794 RuntimeException e = new RuntimeException(
2795 "Performing pause of activity that is not resumed: "
2796 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002797 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 }
2799 Bundle state = null;
2800 if (finished) {
2801 r.activity.mFinished = true;
2802 }
2803 try {
2804 // Next have the activity save its current state and managed dialogs...
2805 if (!r.activity.mFinished && saveState) {
2806 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002807 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2809 r.state = state;
2810 }
2811 // Now we are idle.
2812 r.activity.mCalled = false;
2813 mInstrumentation.callActivityOnPause(r.activity);
2814 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2815 if (!r.activity.mCalled) {
2816 throw new SuperNotCalledException(
2817 "Activity " + r.intent.getComponent().toShortString() +
2818 " did not call through to super.onPause()");
2819 }
2820
2821 } catch (SuperNotCalledException e) {
2822 throw e;
2823
2824 } catch (Exception e) {
2825 if (!mInstrumentation.onException(r.activity, e)) {
2826 throw new RuntimeException(
2827 "Unable to pause activity "
2828 + r.intent.getComponent().toShortString()
2829 + ": " + e.toString(), e);
2830 }
2831 }
2832 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002833
2834 // Notify any outstanding on paused listeners
2835 ArrayList<OnActivityPausedListener> listeners;
2836 synchronized (mOnPauseListeners) {
2837 listeners = mOnPauseListeners.remove(r.activity);
2838 }
2839 int size = (listeners != null ? listeners.size() : 0);
2840 for (int i = 0; i < size; i++) {
2841 listeners.get(i).onPaused(r.activity);
2842 }
2843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 return state;
2845 }
2846
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002847 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002848 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002849 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 }
2851
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002852 private static class StopInfo implements Runnable {
2853 ActivityClientRecord activity;
2854 Bundle state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 Bitmap thumbnail;
2856 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002857
2858 @Override public void run() {
2859 // Tell activity manager we have been stopped.
2860 try {
2861 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
2862 ActivityManagerNative.getDefault().activityStopped(
2863 activity.token, state, thumbnail, description);
2864 } catch (RemoteException ex) {
2865 }
2866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 }
2868
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002869 private static final class ProviderRefCount {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 public int count;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 ProviderRefCount(int pCount) {
2873 count = pCount;
2874 }
2875 }
2876
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002877 /**
2878 * Core implementation of stopping an activity. Note this is a little
2879 * tricky because the server's meaning of stop is slightly different
2880 * than our client -- for the server, stop means to save state and give
2881 * it the result when it is done, but the window may still be visible.
2882 * For the client, we want to call onStop()/onStart() to indicate when
2883 * the activity's UI visibillity changes.
2884 */
Romain Guy65b345f2011-07-27 18:51:50 -07002885 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002886 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002887 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002888 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 if (r != null) {
2890 if (!keepShown && r.stopped) {
2891 if (r.activity.mFinished) {
2892 // If we are finishing, we won't call onResume() in certain
2893 // cases. So here we likewise don't want to call onStop()
2894 // if the activity isn't resumed.
2895 return;
2896 }
2897 RuntimeException e = new RuntimeException(
2898 "Performing stop of activity that is not resumed: "
2899 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002900 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 }
2902
2903 if (info != null) {
2904 try {
2905 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002906 // For now, don't create the thumbnail here; we are
2907 // doing that by doing a screen snapshot.
2908 info.thumbnail = null; //createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 info.description = r.activity.onCreateDescription();
2910 } catch (Exception e) {
2911 if (!mInstrumentation.onException(r.activity, e)) {
2912 throw new RuntimeException(
2913 "Unable to save state of activity "
2914 + r.intent.getComponent().toShortString()
2915 + ": " + e.toString(), e);
2916 }
2917 }
2918 }
2919
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002920 // Next have the activity save its current state and managed dialogs...
2921 if (!r.activity.mFinished && saveState) {
2922 if (r.state == null) {
2923 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002924 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002925 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2926 r.state = state;
2927 } else {
2928 state = r.state;
2929 }
2930 }
2931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 if (!keepShown) {
2933 try {
2934 // Now we are idle.
2935 r.activity.performStop();
2936 } catch (Exception e) {
2937 if (!mInstrumentation.onException(r.activity, e)) {
2938 throw new RuntimeException(
2939 "Unable to stop activity "
2940 + r.intent.getComponent().toShortString()
2941 + ": " + e.toString(), e);
2942 }
2943 }
2944 r.stopped = true;
2945 }
2946
2947 r.paused = true;
2948 }
2949 }
2950
Romain Guy65b345f2011-07-27 18:51:50 -07002951 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 View v = r.activity.mDecor;
2953 if (v != null) {
2954 if (show) {
2955 if (!r.activity.mVisibleFromServer) {
2956 r.activity.mVisibleFromServer = true;
2957 mNumVisibleActivities++;
2958 if (r.activity.mVisibleFromClient) {
2959 r.activity.makeVisible();
2960 }
2961 }
2962 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002963 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002964 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07002966 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 r.newConfig = null;
2968 }
2969 } else {
2970 if (r.activity.mVisibleFromServer) {
2971 r.activity.mVisibleFromServer = false;
2972 mNumVisibleActivities--;
2973 v.setVisibility(View.INVISIBLE);
2974 }
2975 }
2976 }
2977 }
2978
Romain Guy65b345f2011-07-27 18:51:50 -07002979 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002980 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 r.activity.mConfigChangeFlags |= configChanges;
2982
2983 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002984 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002986 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987 TAG, "Finishing stop of " + r + ": show=" + show
2988 + " win=" + r.window);
2989
2990 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07002991
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002992 // Make sure any pending writes are now committed.
2993 if (!r.isPreHoneycomb()) {
2994 QueuedWork.waitToFinish();
2995 }
2996
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002997 // Schedule the call to tell the activity manager we have
2998 // stopped. We don't do this immediately, because we want to
2999 // have a chance for any other pending work (in particular memory
3000 // trim requests) to complete before you tell the activity
3001 // manager to proceed and allow us to go fully into the background.
3002 info.activity = r;
3003 info.state = r.state;
3004 mH.post(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005 }
3006
3007 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003008 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 if (r.stopped) {
3010 r.activity.performRestart();
3011 r.stopped = false;
3012 }
3013 }
3014
Romain Guy65b345f2011-07-27 18:51:50 -07003015 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003016 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003017
3018 if (r == null) {
3019 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3020 return;
3021 }
3022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003024 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 } else if (show && r.stopped) {
3026 // If we are getting ready to gc after going to the background, well
3027 // we are back active so skip it.
3028 unscheduleGcIdler();
3029
3030 r.activity.performRestart();
3031 r.stopped = false;
3032 }
3033 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003034 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 TAG, "Handle window " + r + " visibility: " + show);
3036 updateVisibility(r, show);
3037 }
3038 }
3039
Romain Guy65b345f2011-07-27 18:51:50 -07003040 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003041 ActivityClientRecord r = mActivities.get(token);
3042
3043 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003044 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003045 return;
3046 }
3047
3048 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003049 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003050 try {
3051 // Now we are idle.
3052 r.activity.performStop();
3053 } catch (Exception e) {
3054 if (!mInstrumentation.onException(r.activity, e)) {
3055 throw new RuntimeException(
3056 "Unable to stop activity "
3057 + r.intent.getComponent().toShortString()
3058 + ": " + e.toString(), e);
3059 }
3060 }
3061 r.stopped = true;
3062 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003063
3064 // Make sure any pending writes are now committed.
3065 if (!r.isPreHoneycomb()) {
3066 QueuedWork.waitToFinish();
3067 }
3068
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003069 // Tell activity manager we slept.
3070 try {
3071 ActivityManagerNative.getDefault().activitySlept(r.token);
3072 } catch (RemoteException ex) {
3073 }
3074 } else {
3075 if (r.stopped && r.activity.mVisibleFromServer) {
3076 r.activity.performRestart();
3077 r.stopped = false;
3078 }
3079 }
3080 }
3081
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003082 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003083 synchronized (mPackages) {
3084 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003085 }
3086 }
3087
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003088 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3089 LoadedApk apk = peekPackageInfo(data.pkg, false);
3090 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003091 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003092 }
3093 apk = peekPackageInfo(data.pkg, true);
3094 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003095 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003096 }
3097 handleConfigurationChanged(mConfiguration, data.info);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003098 WindowManagerImpl.getDefault().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003099 }
3100
Romain Guy65b345f2011-07-27 18:51:50 -07003101 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 final int N = results.size();
3103 for (int i=0; i<N; i++) {
3104 ResultInfo ri = results.get(i);
3105 try {
3106 if (ri.mData != null) {
3107 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
3108 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003109 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003110 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 r.activity.dispatchActivityResult(ri.mResultWho,
3112 ri.mRequestCode, ri.mResultCode, ri.mData);
3113 } catch (Exception e) {
3114 if (!mInstrumentation.onException(r.activity, e)) {
3115 throw new RuntimeException(
3116 "Failure delivering result " + ri + " to activity "
3117 + r.intent.getComponent().toShortString()
3118 + ": " + e.toString(), e);
3119 }
3120 }
3121 }
3122 }
3123
Romain Guy65b345f2011-07-27 18:51:50 -07003124 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003125 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003126 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127 if (r != null) {
3128 final boolean resumed = !r.paused;
3129 if (!r.activity.mFinished && r.activity.mDecor != null
3130 && r.hideForNow && resumed) {
3131 // We had hidden the activity because it started another
3132 // one... we have gotten a result back and we are not
3133 // paused, so make sure our window is visible.
3134 updateVisibility(r, true);
3135 }
3136 if (resumed) {
3137 try {
3138 // Now we are idle.
3139 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003140 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 mInstrumentation.callActivityOnPause(r.activity);
3142 if (!r.activity.mCalled) {
3143 throw new SuperNotCalledException(
3144 "Activity " + r.intent.getComponent().toShortString()
3145 + " did not call through to super.onPause()");
3146 }
3147 } catch (SuperNotCalledException e) {
3148 throw e;
3149 } catch (Exception e) {
3150 if (!mInstrumentation.onException(r.activity, e)) {
3151 throw new RuntimeException(
3152 "Unable to pause activity "
3153 + r.intent.getComponent().toShortString()
3154 + ": " + e.toString(), e);
3155 }
3156 }
3157 }
3158 deliverResults(r, res.results);
3159 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003160 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003161 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 }
3163 }
3164 }
3165
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003166 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 return performDestroyActivity(token, finishing, 0, false);
3168 }
3169
Romain Guy65b345f2011-07-27 18:51:50 -07003170 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003172 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003173 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003174 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003176 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 r.activity.mConfigChangeFlags |= configChanges;
3178 if (finishing) {
3179 r.activity.mFinished = true;
3180 }
3181 if (!r.paused) {
3182 try {
3183 r.activity.mCalled = false;
3184 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07003185 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 r.activity.getComponentName().getClassName());
3187 if (!r.activity.mCalled) {
3188 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003189 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190 + " did not call through to super.onPause()");
3191 }
3192 } catch (SuperNotCalledException e) {
3193 throw e;
3194 } catch (Exception e) {
3195 if (!mInstrumentation.onException(r.activity, e)) {
3196 throw new RuntimeException(
3197 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003198 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 + ": " + e.toString(), e);
3200 }
3201 }
3202 r.paused = true;
3203 }
3204 if (!r.stopped) {
3205 try {
3206 r.activity.performStop();
3207 } catch (SuperNotCalledException e) {
3208 throw e;
3209 } catch (Exception e) {
3210 if (!mInstrumentation.onException(r.activity, e)) {
3211 throw new RuntimeException(
3212 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003213 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 + ": " + e.toString(), e);
3215 }
3216 }
3217 r.stopped = true;
3218 }
3219 if (getNonConfigInstance) {
3220 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003221 r.lastNonConfigurationInstances
3222 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 } catch (Exception e) {
3224 if (!mInstrumentation.onException(r.activity, e)) {
3225 throw new RuntimeException(
3226 "Unable to retain activity "
3227 + r.intent.getComponent().toShortString()
3228 + ": " + e.toString(), e);
3229 }
3230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 }
3232 try {
3233 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003234 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 if (!r.activity.mCalled) {
3236 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003237 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 " did not call through to super.onDestroy()");
3239 }
3240 if (r.window != null) {
3241 r.window.closeAllPanels();
3242 }
3243 } catch (SuperNotCalledException e) {
3244 throw e;
3245 } catch (Exception e) {
3246 if (!mInstrumentation.onException(r.activity, e)) {
3247 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003248 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3249 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 }
3251 }
3252 }
3253 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003254 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 return r;
3256 }
3257
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003258 private static String safeToComponentShortString(Intent intent) {
3259 ComponentName component = intent.getComponent();
3260 return component == null ? "[Unknown]" : component.toShortString();
3261 }
3262
Romain Guy65b345f2011-07-27 18:51:50 -07003263 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003265 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 configChanges, getNonConfigInstance);
3267 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003268 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269 WindowManager wm = r.activity.getWindowManager();
3270 View v = r.activity.mDecor;
3271 if (v != null) {
3272 if (r.activity.mVisibleFromServer) {
3273 mNumVisibleActivities--;
3274 }
3275 IBinder wtoken = v.getWindowToken();
3276 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003277 if (r.onlyLocalRequest) {
3278 // Hold off on removing this until the new activity's
3279 // window is being added.
3280 r.mPendingRemoveWindow = v;
3281 r.mPendingRemoveWindowManager = wm;
3282 } else {
3283 wm.removeViewImmediate(v);
3284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003285 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003286 if (wtoken != null && r.mPendingRemoveWindow == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 WindowManagerImpl.getDefault().closeAll(wtoken,
3288 r.activity.getClass().getName(), "Activity");
3289 }
3290 r.activity.mDecor = null;
3291 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003292 if (r.mPendingRemoveWindow == null) {
3293 // If we are delaying the removal of the activity window, then
3294 // we can't clean up all windows here. Note that we can't do
3295 // so later either, which means any windows that aren't closed
3296 // by the app will leak. Well we try to warning them a lot
3297 // about leaking windows, because that is a bug, so if they are
3298 // using this recreate facility then they get to live with leaks.
3299 WindowManagerImpl.getDefault().closeAll(token,
3300 r.activity.getClass().getName(), "Activity");
3301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302
3303 // Mocked out contexts won't be participating in the normal
3304 // process lifecycle, but if we're running with a proper
3305 // ApplicationContext we need to have it tear down things
3306 // cleanly.
3307 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003308 if (c instanceof ContextImpl) {
3309 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 r.activity.getClass().getName(), "Activity");
3311 }
3312 }
3313 if (finishing) {
3314 try {
3315 ActivityManagerNative.getDefault().activityDestroyed(token);
3316 } catch (RemoteException ex) {
3317 // If the system process has died, it's game over for everyone.
3318 }
3319 }
3320 }
3321
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003322 public final void requestRelaunchActivity(IBinder token,
3323 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3324 int configChanges, boolean notResumed, Configuration config,
3325 boolean fromServer) {
3326 ActivityClientRecord target = null;
3327
3328 synchronized (mPackages) {
3329 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3330 ActivityClientRecord r = mRelaunchingActivities.get(i);
3331 if (r.token == token) {
3332 target = r;
3333 if (pendingResults != null) {
3334 if (r.pendingResults != null) {
3335 r.pendingResults.addAll(pendingResults);
3336 } else {
3337 r.pendingResults = pendingResults;
3338 }
3339 }
3340 if (pendingNewIntents != null) {
3341 if (r.pendingIntents != null) {
3342 r.pendingIntents.addAll(pendingNewIntents);
3343 } else {
3344 r.pendingIntents = pendingNewIntents;
3345 }
3346 }
3347 break;
3348 }
3349 }
3350
3351 if (target == null) {
3352 target = new ActivityClientRecord();
3353 target.token = token;
3354 target.pendingResults = pendingResults;
3355 target.pendingIntents = pendingNewIntents;
3356 if (!fromServer) {
3357 ActivityClientRecord existing = mActivities.get(token);
3358 if (existing != null) {
3359 target.startsNotResumed = existing.paused;
3360 }
3361 target.onlyLocalRequest = true;
3362 }
3363 mRelaunchingActivities.add(target);
3364 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3365 }
3366
3367 if (fromServer) {
3368 target.startsNotResumed = notResumed;
3369 target.onlyLocalRequest = false;
3370 }
3371 if (config != null) {
3372 target.createdConfig = config;
3373 }
3374 target.pendingConfigChanges |= configChanges;
3375 }
3376 }
3377
Romain Guy65b345f2011-07-27 18:51:50 -07003378 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 // If we are getting ready to gc after going to the background, well
3380 // we are back active so skip it.
3381 unscheduleGcIdler();
3382
3383 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003384 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 // First: make sure we have the most recent configuration and most
3387 // recent version of the activity, or skip it if some previous call
3388 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003389 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 int N = mRelaunchingActivities.size();
3391 IBinder token = tmp.token;
3392 tmp = null;
3393 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003394 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003395 if (r.token == token) {
3396 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003397 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 mRelaunchingActivities.remove(i);
3399 i--;
3400 N--;
3401 }
3402 }
Bob Leee5408332009-09-04 18:31:17 -07003403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003405 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003406 return;
3407 }
Bob Leee5408332009-09-04 18:31:17 -07003408
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003409 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3410 + tmp.token + " with configChanges=0x"
3411 + Integer.toHexString(configChanges));
3412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 if (mPendingConfiguration != null) {
3414 changedConfig = mPendingConfiguration;
3415 mPendingConfiguration = null;
3416 }
3417 }
Bob Leee5408332009-09-04 18:31:17 -07003418
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003419 if (tmp.createdConfig != null) {
3420 // If the activity manager is passing us its current config,
3421 // assume that is really what we want regardless of what we
3422 // may have pending.
3423 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003424 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3425 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3426 if (changedConfig == null
3427 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3428 changedConfig = tmp.createdConfig;
3429 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003430 }
3431 }
3432
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003433 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003434 + tmp.token + ": changedConfig=" + changedConfig);
3435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 // If there was a pending configuration change, execute it first.
3437 if (changedConfig != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003438 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 }
Bob Leee5408332009-09-04 18:31:17 -07003440
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003441 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003442 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443 if (r == null) {
3444 return;
3445 }
Bob Leee5408332009-09-04 18:31:17 -07003446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003448 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003449 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003450
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003451 r.activity.mChangingConfigurations = true;
3452
Dianne Hackborne2b04802010-12-09 09:24:55 -08003453 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003454 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003455 performPauseActivity(r.token, false, r.isPreHoneycomb());
3456 }
3457 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3458 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003459 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003460 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 }
Bob Leee5408332009-09-04 18:31:17 -07003462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 r.activity = null;
3466 r.window = null;
3467 r.hideForNow = false;
3468 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003469 // Merge any pending results and pending intents; don't just replace them
3470 if (tmp.pendingResults != null) {
3471 if (r.pendingResults == null) {
3472 r.pendingResults = tmp.pendingResults;
3473 } else {
3474 r.pendingResults.addAll(tmp.pendingResults);
3475 }
3476 }
3477 if (tmp.pendingIntents != null) {
3478 if (r.pendingIntents == null) {
3479 r.pendingIntents = tmp.pendingIntents;
3480 } else {
3481 r.pendingIntents.addAll(tmp.pendingIntents);
3482 }
3483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003485
Christopher Tateb70f3df2009-04-07 16:07:59 -07003486 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 }
3488
Romain Guy65b345f2011-07-27 18:51:50 -07003489 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003490 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 Bitmap thumbnail = createThumbnailBitmap(r);
3492 CharSequence description = null;
3493 try {
3494 description = r.activity.onCreateDescription();
3495 } catch (Exception e) {
3496 if (!mInstrumentation.onException(r.activity, e)) {
3497 throw new RuntimeException(
3498 "Unable to create description of activity "
3499 + r.intent.getComponent().toShortString()
3500 + ": " + e.toString(), e);
3501 }
3502 }
3503 //System.out.println("Reporting top thumbnail " + thumbnail);
3504 try {
3505 ActivityManagerNative.getDefault().reportThumbnail(
3506 token, thumbnail, description);
3507 } catch (RemoteException ex) {
3508 }
3509 }
3510
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003511 ArrayList<ComponentCallbacks2> collectComponentCallbacksLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003512 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003513 ArrayList<ComponentCallbacks2> callbacks
3514 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003516 if (mActivities.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003517 for (ActivityClientRecord ar : mActivities.values()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 Activity a = ar.activity;
3519 if (a != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003520 Configuration thisConfig = applyConfigCompatMainThread(newConfig,
3521 ar.packageInfo.mCompatibilityInfo.getIfNeeded());
Romain Guya998dff2012-03-23 18:58:36 -07003522 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 // If the activity is currently resumed, its configuration
3524 // needs to change right now.
3525 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003526 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 // Otherwise, we will tell it about the change
3528 // the next time it is resumed or shown. Note that
3529 // the activity manager may, before then, decide the
3530 // activity needs to be destroyed to handle its new
3531 // configuration.
Romain Guya998dff2012-03-23 18:58:36 -07003532 if (DEBUG_CONFIGURATION) {
3533 Slog.v(TAG, "Setting activity "
3534 + ar.activityInfo.name + " newConfig=" + thisConfig);
3535 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003536 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 }
3538 }
3539 }
3540 }
3541 if (mServices.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003542 for (Service service : mServices.values()) {
3543 callbacks.add(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 }
3545 }
3546 synchronized (mProviderMap) {
3547 if (mLocalProviders.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003548 for (ProviderClientRecord providerClientRecord : mLocalProviders.values()) {
3549 callbacks.add(providerClientRecord.mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 }
3551 }
3552 }
3553 final int N = mAllApplications.size();
3554 for (int i=0; i<N; i++) {
3555 callbacks.add(mAllApplications.get(i));
3556 }
Bob Leee5408332009-09-04 18:31:17 -07003557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 return callbacks;
3559 }
Bob Leee5408332009-09-04 18:31:17 -07003560
Romain Guya998dff2012-03-23 18:58:36 -07003561 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003563 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003564 // we check the runtime type and act accordingly.
3565 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3566 if (activity != null) {
3567 activity.mCalled = false;
3568 }
Bob Leee5408332009-09-04 18:31:17 -07003569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 boolean shouldChangeConfig = false;
3571 if ((activity == null) || (activity.mCurrentConfig == null)) {
3572 shouldChangeConfig = true;
3573 } else {
Bob Leee5408332009-09-04 18:31:17 -07003574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 // If the new config is the same as the config this Activity
3576 // is already running with then don't bother calling
3577 // onConfigurationChanged
3578 int diff = activity.mCurrentConfig.diff(config);
3579 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580 // If this activity doesn't handle any of the config changes
3581 // then don't bother calling onConfigurationChanged as we're
3582 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003583 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 shouldChangeConfig = true;
3585 }
3586 }
3587 }
Bob Leee5408332009-09-04 18:31:17 -07003588
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003589 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003590 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003591 if (shouldChangeConfig) {
3592 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 if (activity != null) {
3595 if (!activity.mCalled) {
3596 throw new SuperNotCalledException(
3597 "Activity " + activity.getLocalClassName() +
3598 " did not call through to super.onConfigurationChanged()");
3599 }
3600 activity.mConfigChangeFlags = 0;
3601 activity.mCurrentConfig = new Configuration(config);
3602 }
3603 }
3604 }
3605
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003606 public final void applyConfigurationToResources(Configuration config) {
3607 synchronized (mPackages) {
3608 applyConfigurationToResourcesLocked(config, null);
3609 }
3610 }
3611
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003612 final boolean applyConfigurationToResourcesLocked(Configuration config,
3613 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003614 if (mResConfiguration == null) {
3615 mResConfiguration = new Configuration();
3616 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003617 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003618 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003619 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003620 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003621 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003622 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003623 DisplayMetrics dm = getDisplayMetricsLocked(null, true);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003624
3625 if (compat != null && (mResCompatibilityInfo == null ||
3626 !mResCompatibilityInfo.equals(compat))) {
3627 mResCompatibilityInfo = compat;
3628 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3629 | ActivityInfo.CONFIG_SCREEN_SIZE
3630 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3631 }
Bob Leee5408332009-09-04 18:31:17 -07003632
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003633 // set it for java, this also affects newly created Resources
3634 if (config.locale != null) {
3635 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003636 }
Bob Leee5408332009-09-04 18:31:17 -07003637
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003638 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003639
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003640 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003641 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003642
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003643 Iterator<WeakReference<Resources>> it =
3644 mActiveResources.values().iterator();
3645 //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3646 // mActiveResources.entrySet().iterator();
3647 while (it.hasNext()) {
3648 WeakReference<Resources> v = it.next();
3649 Resources r = v.get();
3650 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003651 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003652 + r + " config to: " + config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003653 r.updateConfiguration(config, dm, compat);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003654 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003655 // + " " + r + ": " + r.getConfiguration());
3656 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003657 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003658 it.remove();
3659 }
3660 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003661
3662 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003663 }
Dianne Hackborn836e2622011-10-04 18:32:39 -07003664
3665 final Configuration applyCompatConfiguration() {
3666 Configuration config = mConfiguration;
3667 if (mCompatConfiguration == null) {
3668 mCompatConfiguration = new Configuration();
3669 }
3670 mCompatConfiguration.setTo(mConfiguration);
3671 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
3672 mResCompatibilityInfo.applyToConfiguration(mCompatConfiguration);
3673 config = mCompatConfiguration;
3674 }
3675 return config;
3676 }
3677
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003678 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003679
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003680 ArrayList<ComponentCallbacks2> callbacks = null;
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003681 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003682
3683 synchronized (mPackages) {
3684 if (mPendingConfiguration != null) {
3685 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3686 config = mPendingConfiguration;
3687 }
3688 mPendingConfiguration = null;
3689 }
3690
3691 if (config == null) {
3692 return;
3693 }
3694
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003695 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003696 + config);
3697
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003698 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 if (mConfiguration == null) {
3701 mConfiguration = new Configuration();
3702 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003703 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003704 return;
3705 }
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003706 configDiff = mConfiguration.diff(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003707 mConfiguration.updateFrom(config);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003708 config = applyCompatConfiguration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 callbacks = collectComponentCallbacksLocked(false, config);
3710 }
Romain Guy65b345f2011-07-27 18:51:50 -07003711
3712 // Cleanup hardware accelerated stuff
3713 WindowManagerImpl.getDefault().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003714
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003715 freeTextLayoutCachesIfNeeded(configDiff);
3716
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003717 if (callbacks != null) {
3718 final int N = callbacks.size();
3719 for (int i=0; i<N; i++) {
3720 performConfigurationChanged(callbacks.get(i), config);
3721 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 }
3723 }
3724
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003725 final void freeTextLayoutCachesIfNeeded(int configDiff) {
3726 if (configDiff != 0) {
3727 // Ask text layout engine to free its caches if there is a locale change
3728 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
3729 if (hasLocaleConfigChange) {
3730 Canvas.freeTextLayoutCaches();
3731 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
3732 }
3733 }
3734 }
3735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003736 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003737 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003738 if (r == null || r.activity == null) {
3739 return;
3740 }
Bob Leee5408332009-09-04 18:31:17 -07003741
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003742 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003743 + r.activityInfo.name);
3744
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003745 performConfigurationChanged(r.activity, mCompatConfiguration);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003746
3747 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 }
3749
Romain Guy7eabe552011-07-21 14:56:34 -07003750 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003751 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003752 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003753 switch (profileType) {
3754 case 1:
3755 ViewDebug.startLooperProfiling(pcd.path, pcd.fd.getFileDescriptor());
3756 break;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003757 default:
3758 mProfiler.setProfiler(pcd.path, pcd.fd);
3759 mProfiler.autoStopProfiler = false;
3760 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003761 break;
3762 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003763 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003764 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003765 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003766 } finally {
3767 try {
3768 pcd.fd.close();
3769 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003770 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003771 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003772 }
3773 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003774 switch (profileType) {
3775 case 1:
3776 ViewDebug.stopLooperProfiling();
3777 break;
3778 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003779 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003780 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003781 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003782 }
3783 }
Bob Leee5408332009-09-04 18:31:17 -07003784
Romain Guya998dff2012-03-23 18:58:36 -07003785 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07003786 if (managed) {
3787 try {
3788 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3789 } catch (IOException e) {
3790 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3791 + " -- can the process access this path?");
3792 } finally {
3793 try {
3794 dhd.fd.close();
3795 } catch (IOException e) {
3796 Slog.w(TAG, "Failure closing profile fd", e);
3797 }
3798 }
3799 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003800 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003801 }
3802 }
3803
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003804 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3805 boolean hasPkgInfo = false;
3806 if (packages != null) {
3807 for (int i=packages.length-1; i>=0; i--) {
3808 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3809 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003810 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003811 ref = mPackages.get(packages[i]);
3812 if (ref != null && ref.get() != null) {
3813 hasPkgInfo = true;
3814 } else {
3815 ref = mResourcePackages.get(packages[i]);
3816 if (ref != null && ref.get() != null) {
3817 hasPkgInfo = true;
3818 }
3819 }
3820 }
3821 mPackages.remove(packages[i]);
3822 mResourcePackages.remove(packages[i]);
3823 }
3824 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003825 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003826 hasPkgInfo);
3827 }
3828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829 final void handleLowMemory() {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003830 ArrayList<ComponentCallbacks2> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003831
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003832 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 callbacks = collectComponentCallbacksLocked(true, null);
3834 }
Bob Leee5408332009-09-04 18:31:17 -07003835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 final int N = callbacks.size();
3837 for (int i=0; i<N; i++) {
3838 callbacks.get(i).onLowMemory();
3839 }
3840
Chris Tatece229052009-03-25 16:44:52 -07003841 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3842 if (Process.myUid() != Process.SYSTEM_UID) {
3843 int sqliteReleased = SQLiteDatabase.releaseMemory();
3844 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3845 }
Bob Leee5408332009-09-04 18:31:17 -07003846
Mike Reedcaf0df12009-04-27 14:32:05 -04003847 // Ask graphics to free up as much as possible (font/image caches)
3848 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003850 // Ask text layout engine to free also as much as possible
3851 Canvas.freeTextLayoutCaches();
3852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853 BinderInternal.forceGc("mem");
3854 }
3855
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003856 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003857 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003858
Romain Guy19f86e82012-04-23 15:19:07 -07003859 final WindowManagerImpl windowManager = WindowManagerImpl.getDefault();
3860 windowManager.startTrimMemory(level);
3861
3862 ArrayList<ComponentCallbacks2> callbacks;
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003863 synchronized (mPackages) {
3864 callbacks = collectComponentCallbacksLocked(true, null);
3865 }
3866
3867 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07003868 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003869 callbacks.get(i).onTrimMemory(level);
3870 }
Romain Guy19f86e82012-04-23 15:19:07 -07003871
3872 windowManager.endTrimMemory();
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003873 }
3874
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003875 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003876 if (Process.isIsolated()) {
3877 // Isolated processes aren't going to do UI.
3878 return;
3879 }
Romain Guya9582652011-11-10 14:20:10 -08003880 try {
3881 int uid = Process.myUid();
3882 String[] packages = getPackageManager().getPackagesForUid(uid);
3883
3884 // If there are several packages in this application we won't
3885 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003886 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003887 HardwareRenderer.setupDiskCache(cacheDir);
3888 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08003889 }
3890 } catch (RemoteException e) {
3891 // Ignore
3892 }
3893 }
3894
Romain Guy65b345f2011-07-27 18:51:50 -07003895 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003896 mBoundApplication = data;
3897 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003898 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003900 mProfiler = new Profiler();
3901 mProfiler.profileFile = data.initProfileFile;
3902 mProfiler.profileFd = data.initProfileFd;
3903 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
3904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003906 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 android.ddm.DdmHandleAppName.setAppName(data.processName);
3908
Dianne Hackborn5d927c22011-09-02 12:22:18 -07003909 if (data.persistent) {
3910 // Persistent processes on low-memory devices do not get to
3911 // use hardware accelerated drawing, since this can add too much
3912 // overhead to the process.
3913 Display display = WindowManagerImpl.getDefault().getDefaultDisplay();
3914 if (!ActivityManager.isHighEndGfx(display)) {
3915 HardwareRenderer.disable(false);
3916 }
3917 }
Romain Guya9582652011-11-10 14:20:10 -08003918
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003919 if (mProfiler.profileFd != null) {
3920 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003921 }
3922
Joe Onoratod630f102011-03-17 18:42:26 -07003923 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
3924 // implementation to use the pool executor. Normally, we use the
3925 // serialized executor as the default. This has to happen in the
3926 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07003927 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07003928 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
3929 }
3930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003931 /*
3932 * Before spawning a new process, reset the time zone to be the system time zone.
3933 * This needs to be done because the system time zone could have changed after the
3934 * the spawning of this process. Without doing this this process would have the incorrect
3935 * system time zone.
3936 */
3937 TimeZone.setDefault(null);
3938
3939 /*
3940 * Initialize the default locale in this process for the reasons we set the time zone.
3941 */
3942 Locale.setDefault(data.config.locale);
3943
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003944 /*
3945 * Update the system configuration since its preloaded and might not
3946 * reflect configuration changes. The configuration object passed
3947 * in AppBindData can be safely assumed to be up to date
3948 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003949 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003950 applyCompatConfiguration();
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003951
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003952 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003953
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003954 final ContextImpl appContext = new ContextImpl();
3955 appContext.init(data.info, null, this);
3956 final File cacheDir = appContext.getCacheDir();
3957
3958 // Provide a usable directory for temporary files
3959 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
3960
3961 setupGraphicsSupport(data.info, cacheDir);
3962
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003963 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003964 * For system applications on userdebug/eng builds, log stack
3965 * traces of disk and network access to dropbox for analysis.
3966 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07003967 if ((data.appInfo.flags &
3968 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07003969 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
3970 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003971 }
3972
3973 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07003974 * For apps targetting SDK Honeycomb or later, we don't allow
3975 * network usage on the main event loop / UI thread.
3976 *
3977 * Note to those grepping: this is what ultimately throws
3978 * NetworkOnMainThreadException ...
3979 */
3980 if (data.appInfo.targetSdkVersion > 9) {
3981 StrictMode.enableDeathOnNetwork();
3982 }
3983
3984 /**
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003985 * Switch this process to density compatibility mode if needed.
3986 */
3987 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
3988 == 0) {
3989 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3990 }
Bob Leee5408332009-09-04 18:31:17 -07003991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003992 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
3993 // XXX should have option to change the port.
3994 Debug.changeDebugPort(8100);
3995 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003996 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003997 + " is waiting for the debugger on port 8100...");
3998
3999 IActivityManager mgr = ActivityManagerNative.getDefault();
4000 try {
4001 mgr.showWaitingForDebugger(mAppThread, true);
4002 } catch (RemoteException ex) {
4003 }
4004
4005 Debug.waitForDebugger();
4006
4007 try {
4008 mgr.showWaitingForDebugger(mAppThread, false);
4009 } catch (RemoteException ex) {
4010 }
4011
4012 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004013 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004014 + " can be debugged on port 8100...");
4015 }
4016 }
4017
Siva Velusamy92a8b222012-03-09 16:24:04 -08004018 // Enable OpenGL tracing if required
4019 if (data.enableOpenGlTrace) {
4020 GLUtils.enableTracing();
4021 }
4022
Robert Greenwalt434203a2010-10-11 16:00:27 -07004023 /**
4024 * Initialize the default http proxy in this process for the reasons we set the time zone.
4025 */
4026 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004027 if (b != null) {
4028 // In pre-boot mode (doing initial launch to collect password), not
4029 // all system is up. This includes the connectivity service, so don't
4030 // crash if we can't get it.
4031 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4032 try {
4033 ProxyProperties proxyProperties = service.getProxy();
4034 Proxy.setHttpProxySystemProperty(proxyProperties);
4035 } catch (RemoteException e) {}
4036 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004038 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 InstrumentationInfo ii = null;
4040 try {
4041 ii = appContext.getPackageManager().
4042 getInstrumentationInfo(data.instrumentationName, 0);
4043 } catch (PackageManager.NameNotFoundException e) {
4044 }
4045 if (ii == null) {
4046 throw new RuntimeException(
4047 "Unable to find instrumentation info for: "
4048 + data.instrumentationName);
4049 }
4050
4051 mInstrumentationAppDir = ii.sourceDir;
Brian Carlstromd893a892012-04-01 21:30:26 -07004052 mInstrumentationAppLibraryDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 mInstrumentationAppPackage = ii.packageName;
4054 mInstrumentedAppDir = data.info.getAppDir();
Brian Carlstromd893a892012-04-01 21:30:26 -07004055 mInstrumentedAppLibraryDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056
4057 ApplicationInfo instrApp = new ApplicationInfo();
4058 instrApp.packageName = ii.packageName;
4059 instrApp.sourceDir = ii.sourceDir;
4060 instrApp.publicSourceDir = ii.publicSourceDir;
4061 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004062 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004063 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08004065 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 instrContext.init(pi, null, this);
4067
4068 try {
4069 java.lang.ClassLoader cl = instrContext.getClassLoader();
4070 mInstrumentation = (Instrumentation)
4071 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4072 } catch (Exception e) {
4073 throw new RuntimeException(
4074 "Unable to instantiate instrumentation "
4075 + data.instrumentationName + ": " + e.toString(), e);
4076 }
4077
4078 mInstrumentation.init(this, instrContext, appContext,
4079 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
4080
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004081 if (mProfiler.profileFile != null && !ii.handleProfiling
4082 && mProfiler.profileFd == null) {
4083 mProfiler.handlingProfiling = true;
4084 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085 file.getParentFile().mkdirs();
4086 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4087 }
4088
4089 try {
4090 mInstrumentation.onCreate(data.instrumentationArgs);
4091 }
4092 catch (Exception e) {
4093 throw new RuntimeException(
4094 "Exception thrown in onCreate() of "
4095 + data.instrumentationName + ": " + e.toString(), e);
4096 }
4097
4098 } else {
4099 mInstrumentation = new Instrumentation();
4100 }
4101
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004102 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004103 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004104 }
4105
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004106 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004107 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004108 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004109 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004111 // If the app is being launched for full backup or restore, bring it up in
4112 // a restricted environment with the base application class.
4113 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4114 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004115
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004116 // don't bring up providers in restricted mode; they may depend on the
4117 // app's custom Application class
4118 if (!data.restrictedBackupMode) {
4119 List<ProviderInfo> providers = data.providers;
4120 if (providers != null) {
4121 installContentProviders(app, providers);
4122 // For process that contains content providers, we want to
4123 // ensure that the JIT is enabled "at some point".
4124 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4125 }
4126 }
4127
4128 try {
4129 mInstrumentation.callApplicationOnCreate(app);
4130 } catch (Exception e) {
4131 if (!mInstrumentation.onException(app, e)) {
4132 throw new RuntimeException(
4133 "Unable to create application " + app.getClass().getName()
4134 + ": " + e.toString(), e);
4135 }
4136 }
4137 } finally {
4138 StrictMode.setThreadPolicy(savedPolicy);
4139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140 }
4141
4142 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4143 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004144 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4145 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 Debug.stopMethodTracing();
4147 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004148 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149 // + ", app thr: " + mAppThread);
4150 try {
4151 am.finishInstrumentation(mAppThread, resultCode, results);
4152 } catch (RemoteException ex) {
4153 }
4154 }
4155
Romain Guy65b345f2011-07-27 18:51:50 -07004156 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004157 Context context, List<ProviderInfo> providers) {
4158 final ArrayList<IActivityManager.ContentProviderHolder> results =
4159 new ArrayList<IActivityManager.ContentProviderHolder>();
4160
Romain Guya998dff2012-03-23 18:58:36 -07004161 for (ProviderInfo cpi : providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07004163 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004164 buf.append(cpi.authority);
4165 buf.append(": ");
4166 buf.append(cpi.name);
4167 Log.i(TAG, buf.toString());
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004168 IContentProvider cp = installProvider(context, null, cpi,
4169 false /*noisy*/, true /*noReleaseNeeded*/);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170 if (cp != null) {
4171 IActivityManager.ContentProviderHolder cph =
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004172 new IActivityManager.ContentProviderHolder(cpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 cph.provider = cp;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004174 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 }
4177 }
4178
4179 try {
4180 ActivityManagerNative.getDefault().publishContentProviders(
4181 getApplicationThread(), results);
4182 } catch (RemoteException ex) {
4183 }
4184 }
4185
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004186 public final IContentProvider acquireProvider(Context c, String name) {
4187 IContentProvider provider = acquireExistingProvider(c, name);
4188 if (provider != null) {
4189 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 }
4191
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004192 // There is a possible race here. Another thread may try to acquire
4193 // the same provider at the same time. When this happens, we want to ensure
4194 // that the first one wins.
4195 // Note that we cannot hold the lock while acquiring and installing the
4196 // provider since it might take a long time to run and it could also potentially
4197 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 IActivityManager.ContentProviderHolder holder = null;
4199 try {
4200 holder = ActivityManagerNative.getDefault().getContentProvider(
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004201 getApplicationThread(), name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202 } catch (RemoteException ex) {
4203 }
4204 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004205 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004206 return null;
4207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004208
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004209 // Install provider will increment the reference count for us, and break
4210 // any ties in the race.
4211 provider = installProvider(c, holder.provider, holder.info,
4212 true /*noisy*/, holder.noReleaseNeeded);
4213 if (holder.provider != null && provider != holder.provider) {
4214 if (localLOGV) {
4215 Slog.v(TAG, "acquireProvider: lost the race, releasing extraneous "
4216 + "reference to the content provider");
4217 }
4218 try {
4219 ActivityManagerNative.getDefault().removeContentProvider(
4220 getApplicationThread(), name);
4221 } catch (RemoteException ex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222 }
4223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 return provider;
4225 }
4226
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004227 public final IContentProvider acquireExistingProvider(Context c, String name) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004228 synchronized (mProviderMap) {
4229 ProviderClientRecord pr = mProviderMap.get(name);
4230 if (pr == null) {
4231 return null;
4232 }
4233
4234 IContentProvider provider = pr.mProvider;
4235 IBinder jBinder = provider.asBinder();
4236
4237 // Only increment the ref count if we have one. If we don't then the
4238 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004239 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004240 if (prc != null) {
4241 prc.count += 1;
4242 if (prc.count == 1) {
4243 if (localLOGV) {
4244 Slog.v(TAG, "acquireExistingProvider: "
4245 + "snatched provider from the jaws of death");
4246 }
4247 // Because the provider previously had a reference count of zero,
4248 // it was scheduled to be removed. Cancel that.
4249 mH.removeMessages(H.REMOVE_PROVIDER, provider);
4250 }
4251 }
4252 return provider;
4253 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004254 }
4255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004256 public final boolean releaseProvider(IContentProvider provider) {
4257 if(provider == null) {
4258 return false;
4259 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004261 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004262 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004263 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004264 if (prc == null) {
4265 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004266 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004267 }
4268
4269 if (prc.count == 0) {
4270 if (localLOGV) Slog.v(TAG, "releaseProvider: ref count already 0, how?");
4271 return false;
4272 }
4273
4274 prc.count -= 1;
4275 if (prc.count == 0) {
4276 // Schedule the actual remove asynchronously, since we don't know the context
4277 // this will be called in.
4278 // TODO: it would be nice to post a delayed message, so
4279 // if we come back and need the same provider quickly
4280 // we will still have it available.
4281 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, provider);
4282 mH.sendMessage(msg);
4283 }
4284 return true;
4285 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 }
4287
Dianne Hackborn652b6d12012-05-09 18:18:40 -07004288 public final IContentProvider acquireUnstableProvider(Context c, String name) {
4289 return acquireProvider(c, name);
4290 }
4291
4292 public final boolean releaseUnstableProvider(IContentProvider provider) {
4293 return releaseProvider(provider);
4294 }
4295
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004296 final void completeRemoveProvider(IContentProvider provider) {
4297 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004298 String remoteProviderName = null;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004299 synchronized(mProviderMap) {
4300 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004301 if (prc == null) {
4302 // Either no release is needed (so we shouldn't be here) or the
4303 // provider was already released.
4304 if (localLOGV) Slog.v(TAG, "completeRemoveProvider: release not needed");
4305 return;
4306 }
4307
4308 if (prc.count != 0) {
4309 // There was a race! Some other client managed to acquire
4310 // the provider before the removal was completed.
4311 // Abort the removal. We will do it later.
4312 if (localLOGV) Slog.v(TAG, "completeRemoveProvider: lost the race, "
4313 + "provider still in use");
4314 return;
4315 }
4316
4317 mProviderRefCountMap.remove(jBinder);
4318
4319 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
4320 while (iter.hasNext()) {
4321 ProviderClientRecord pr = iter.next();
4322 IBinder myBinder = pr.mProvider.asBinder();
4323 if (myBinder == jBinder) {
4324 iter.remove();
4325 if (pr.mLocalProvider == null) {
4326 myBinder.unlinkToDeath(pr, 0);
4327 if (remoteProviderName == null) {
4328 remoteProviderName = pr.mName;
4329 }
4330 }
4331 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004332 }
4333 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004334
4335 if (remoteProviderName != null) {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004336 try {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004337 if (localLOGV) {
4338 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4339 + "removeContentProvider(" + remoteProviderName + ")");
4340 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004341 ActivityManagerNative.getDefault().removeContentProvider(
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004342 getApplicationThread(), remoteProviderName);
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004343 } catch (RemoteException e) {
4344 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004345 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004346 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004348
4349 final void removeDeadProvider(String name, IContentProvider provider) {
4350 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004351 ProviderClientRecord pr = mProviderMap.get(name);
Jeff Brown2a4c5ac2011-08-23 11:58:26 -07004352 if (pr != null && pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004353 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004354 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004355 if (removed != null) {
4356 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
4357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 }
4359 }
4360 }
4361
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004362 /**
4363 * Installs the provider.
4364 *
4365 * Providers that are local to the process or that come from the system server
4366 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4367 * Other remote providers are reference counted. The initial reference count
4368 * for all reference counted providers is one. Providers that are not reference
4369 * counted do not have a reference count (at all).
4370 *
4371 * This method detects when a provider has already been installed. When this happens,
4372 * it increments the reference count of the existing provider (if appropriate)
4373 * and returns the existing provider. This can happen due to concurrent
4374 * attempts to acquire the same provider.
4375 */
Romain Guy65b345f2011-07-27 18:51:50 -07004376 private IContentProvider installProvider(Context context,
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004377 IContentProvider provider, ProviderInfo info,
4378 boolean noisy, boolean noReleaseNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 ContentProvider localProvider = null;
4380 if (provider == null) {
4381 if (noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004382 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383 + info.name);
4384 }
4385 Context c = null;
4386 ApplicationInfo ai = info.applicationInfo;
4387 if (context.getPackageName().equals(ai.packageName)) {
4388 c = context;
4389 } else if (mInitialApplication != null &&
4390 mInitialApplication.getPackageName().equals(ai.packageName)) {
4391 c = mInitialApplication;
4392 } else {
4393 try {
4394 c = context.createPackageContext(ai.packageName,
4395 Context.CONTEXT_INCLUDE_CODE);
4396 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004397 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004398 }
4399 }
4400 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004401 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 ai.packageName +
4403 " while loading content provider " +
4404 info.name);
4405 return null;
4406 }
4407 try {
4408 final java.lang.ClassLoader cl = c.getClassLoader();
4409 localProvider = (ContentProvider)cl.
4410 loadClass(info.name).newInstance();
4411 provider = localProvider.getIContentProvider();
4412 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004413 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 info.name + " from sourceDir " +
4415 info.applicationInfo.sourceDir);
4416 return null;
4417 }
Joe Onorato43a17652011-04-06 19:22:23 -07004418 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 TAG, "Instantiating local provider " + info.name);
4420 // XXX Need to create the correct context for this provider.
4421 localProvider.attachInfo(c, info);
4422 } catch (java.lang.Exception e) {
4423 if (!mInstrumentation.onException(null, e)) {
4424 throw new RuntimeException(
4425 "Unable to get provider " + info.name
4426 + ": " + e.toString(), e);
4427 }
4428 return null;
4429 }
4430 } else if (localLOGV) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004431 Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004432 + info.name);
4433 }
4434
4435 synchronized (mProviderMap) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004436 // There is a possibility that this thread raced with another thread to
4437 // add the provider. If we find another thread got there first then we
4438 // just get out of the way and return the original provider.
4439 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004440 String names[] = PATTERN_SEMICOLON.split(info.authority);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004441 for (int i = 0; i < names.length; i++) {
4442 ProviderClientRecord pr = mProviderMap.get(names[i]);
4443 if (pr != null) {
4444 if (localLOGV) {
4445 Slog.v(TAG, "installProvider: lost the race, "
4446 + "using existing named provider");
4447 }
4448 provider = pr.mProvider;
4449 } else {
4450 pr = new ProviderClientRecord(names[i], provider, localProvider);
4451 if (localProvider == null) {
4452 try {
4453 jBinder.linkToDeath(pr, 0);
4454 } catch (RemoteException e) {
4455 // Provider already dead. Bail out of here without making
4456 // any changes to the provider map or other data structures.
4457 return null;
4458 }
4459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004460 mProviderMap.put(names[i], pr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004461 }
4462 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004464 if (localProvider != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004465 ProviderClientRecord pr = mLocalProviders.get(jBinder);
4466 if (pr != null) {
4467 if (localLOGV) {
4468 Slog.v(TAG, "installProvider: lost the race, "
4469 + "using existing local provider");
4470 }
4471 provider = pr.mProvider;
4472 } else {
4473 pr = new ProviderClientRecord(null, provider, localProvider);
4474 mLocalProviders.put(jBinder, pr);
4475 }
4476 }
4477
4478 if (!noReleaseNeeded) {
4479 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4480 if (prc != null) {
4481 if (localLOGV) {
4482 Slog.v(TAG, "installProvider: lost the race, incrementing ref count");
4483 }
4484 prc.count += 1;
4485 if (prc.count == 1) {
4486 if (localLOGV) {
4487 Slog.v(TAG, "installProvider: "
4488 + "snatched provider from the jaws of death");
4489 }
4490 // Because the provider previously had a reference count of zero,
4491 // it was scheduled to be removed. Cancel that.
4492 mH.removeMessages(H.REMOVE_PROVIDER, provider);
4493 }
4494 } else {
4495 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
4496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004497 }
4498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004499 return provider;
4500 }
4501
Romain Guy65b345f2011-07-27 18:51:50 -07004502 private void attach(boolean system) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503 sThreadLocal.set(this);
4504 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004506 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004507 public void run() {
4508 ensureJitEnabled();
4509 }
4510 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004511 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4512 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4513 IActivityManager mgr = ActivityManagerNative.getDefault();
4514 try {
4515 mgr.attachApplication(mAppThread);
4516 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004517 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004518 }
4519 } else {
4520 // Don't set application object here -- if the system crashes,
4521 // we can't display an alert, we just want to die die die.
4522 android.ddm.DdmHandleAppName.setAppName("system_process");
4523 try {
4524 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004525 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004526 context.init(getSystemContext().mPackageInfo, null, this);
4527 Application app = Instrumentation.newApplication(Application.class, context);
4528 mAllApplications.add(app);
4529 mInitialApplication = app;
4530 app.onCreate();
4531 } catch (Exception e) {
4532 throw new RuntimeException(
4533 "Unable to instantiate Application():" + e.toString(), e);
4534 }
4535 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004536
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004537 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004538 public void onConfigurationChanged(Configuration newConfig) {
4539 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004540 // We need to apply this change to the resources
4541 // immediately, because upon returning the view
4542 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004543 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004544 // This actually changed the resources! Tell
4545 // everyone about it.
4546 if (mPendingConfiguration == null ||
4547 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4548 mPendingConfiguration = newConfig;
4549
4550 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4551 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004552 }
4553 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004554 }
4555 public void onLowMemory() {
4556 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004557 public void onTrimMemory(int level) {
4558 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004559 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004560 }
4561
Romain Guy5e9120d2012-01-30 12:17:22 -08004562 public static ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004563 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004564 ActivityThread thread = new ActivityThread();
4565 thread.attach(true);
4566 return thread;
4567 }
4568
Jeff Brown10e89712011-07-08 18:52:57 -07004569 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004570 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004571 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004572 }
4573 }
4574
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004575 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004576 synchronized (mPackages) {
4577 if (mCoreSettings != null) {
4578 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004579 } else {
4580 return defaultValue;
4581 }
4582 }
4583 }
4584
Romain Guy65b345f2011-07-27 18:51:50 -07004585 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004586 SamplingProfilerIntegration.start();
4587
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004588 // CloseGuard defaults to true and can be quite spammy. We
4589 // disable it here, but selectively enable it later (via
4590 // StrictMode) on debug builds, but using DropBox, not logs.
4591 CloseGuard.setEnabled(false);
4592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004593 Process.setArgV0("<pre-initialized>");
4594
4595 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07004596 if (sMainThreadHandler == null) {
4597 sMainThreadHandler = new Handler();
4598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004599
4600 ActivityThread thread = new ActivityThread();
4601 thread.attach(false);
4602
Romain Guy5e9120d2012-01-30 12:17:22 -08004603 AsyncTask.init();
4604
Dianne Hackborn287952c2010-09-22 22:34:31 -07004605 if (false) {
4606 Looper.myLooper().setMessageLogging(new
4607 LogPrinter(Log.DEBUG, "ActivityThread"));
4608 }
4609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004610 Looper.loop();
4611
Jeff Brown10e89712011-07-08 18:52:57 -07004612 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004613 }
4614}