blob: b7e0683a9fc61478099ed65dd27bc581ec9eea89 [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;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700143 private static final boolean DEBUG_PROVIDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
145 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
146 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
147 private static final int LOG_ON_PAUSE_CALLED = 30021;
148 private static final int LOG_ON_RESUME_CALLED = 30022;
149
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700150 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700151
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700152 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700154 final ApplicationThread mAppThread = new ApplicationThread();
155 final Looper mLooper = Looper.myLooper();
156 final H mH = new H();
157 final HashMap<IBinder, ActivityClientRecord> mActivities
158 = new HashMap<IBinder, ActivityClientRecord>();
159 // List of new activities (via ActivityRecord.nextIdle) that should
160 // be reported when next we idle.
161 ActivityClientRecord mNewActivities = null;
162 // Number of activities that are currently visible on-screen.
163 int mNumVisibleActivities = 0;
164 final HashMap<IBinder, Service> mServices
165 = new HashMap<IBinder, Service>();
166 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700167 Profiler mProfiler;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700168 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700169 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700170 Configuration mResConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700171 CompatibilityInfo mResCompatibilityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700172 Application mInitialApplication;
173 final ArrayList<Application> mAllApplications
174 = new ArrayList<Application>();
175 // set of instantiated backup agents, keyed by package name
176 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Romain Guy65b345f2011-07-27 18:51:50 -0700177 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700178 Instrumentation mInstrumentation;
179 String mInstrumentationAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700180 String mInstrumentationAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700181 String mInstrumentationAppPackage = null;
182 String mInstrumentedAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700183 String mInstrumentedAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700184 boolean mSystemThread = false;
185 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700187 // These can be accessed by multiple threads; mPackages is the lock.
188 // XXX For now we keep around information about all packages we have
189 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800190 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700191 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800192 // which means this lock gets held while the activity and window managers
193 // holds their own lock. Thus you MUST NEVER call back into the activity manager
194 // or window manager or anything that depends on them while holding this lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700195 final HashMap<String, WeakReference<LoadedApk>> mPackages
196 = new HashMap<String, WeakReference<LoadedApk>>();
197 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
198 = new HashMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700199 final HashMap<CompatibilityInfo, DisplayMetrics> mDisplayMetrics
200 = new HashMap<CompatibilityInfo, DisplayMetrics>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700201 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
202 = new HashMap<ResourcesKey, WeakReference<Resources> >();
203 final ArrayList<ActivityClientRecord> mRelaunchingActivities
204 = new ArrayList<ActivityClientRecord>();
205 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700207 // The lock of mProviderMap protects the following variables.
208 final HashMap<String, ProviderClientRecord> mProviderMap
209 = new HashMap<String, ProviderClientRecord>();
210 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
211 = new HashMap<IBinder, ProviderRefCount>();
212 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
213 = new HashMap<IBinder, ProviderClientRecord>();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700214 final HashMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
215 = new HashMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216
Jeff Hamilton52d32032011-01-08 15:31:26 -0600217 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
218 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
219
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700220 final GcIdler mGcIdler = new GcIdler();
221 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700223 static Handler sMainThreadHandler; // set once in main()
224
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800225 Bundle mCoreSettings = null;
226
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400227 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700229 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 Intent intent;
231 Bundle state;
232 Activity activity;
233 Window window;
234 Activity parent;
235 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700236 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 boolean paused;
238 boolean stopped;
239 boolean hideForNow;
240 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700241 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700242 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700244 String profileFile;
245 ParcelFileDescriptor profileFd;
246 boolean autoStopProfiler;
247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400249 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700250 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251
252 List<ResultInfo> pendingResults;
253 List<Intent> pendingIntents;
254
255 boolean startsNotResumed;
256 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800257 int pendingConfigChanges;
258 boolean onlyLocalRequest;
259
260 View mPendingRemoveWindow;
261 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700263 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 parent = null;
265 embeddedID = null;
266 paused = false;
267 stopped = false;
268 hideForNow = false;
269 nextIdle = null;
270 }
271
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800272 public boolean isPreHoneycomb() {
273 if (activity != null) {
274 return activity.getApplicationInfo().targetSdkVersion
275 < android.os.Build.VERSION_CODES.HONEYCOMB;
276 }
277 return false;
278 }
279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700281 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 return "ActivityRecord{"
283 + Integer.toHexString(System.identityHashCode(this))
284 + " token=" + token + " " + (componentName == null
285 ? "no component name" : componentName.toShortString())
286 + "}";
287 }
288 }
289
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700290 final class ProviderClientRecord {
291 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 final IContentProvider mProvider;
293 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700294 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700296 ProviderClientRecord(String[] names, IContentProvider provider,
297 ContentProvider localProvider,
298 IActivityManager.ContentProviderHolder holder) {
299 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 mProvider = provider;
301 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700302 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 }
304 }
305
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400306 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 List<Intent> intents;
308 IBinder token;
309 public String toString() {
310 return "NewIntentData{intents=" + intents + " token=" + token + "}";
311 }
312 }
313
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400314 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700315 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
316 boolean ordered, boolean sticky, IBinder token) {
317 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky, token);
318 this.intent = intent;
319 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 Intent intent;
322 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400323 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 public String toString() {
325 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700326 info.packageName + " resultCode=" + getResultCode()
327 + " resultData=" + getResultData() + " resultExtras="
328 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 }
330 }
331
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400332 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700333 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400334 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700335 int backupMode;
336 public String toString() {
337 return "CreateBackupAgentData{appInfo=" + appInfo
338 + " backupAgent=" + appInfo.backupAgentName
339 + " mode=" + backupMode + "}";
340 }
341 }
Bob Leee5408332009-09-04 18:31:17 -0700342
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400343 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 IBinder token;
345 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400346 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 Intent intent;
348 public String toString() {
349 return "CreateServiceData{token=" + token + " className="
350 + info.name + " packageName=" + info.packageName
351 + " intent=" + intent + "}";
352 }
353 }
354
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400355 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 IBinder token;
357 Intent intent;
358 boolean rebind;
359 public String toString() {
360 return "BindServiceData{token=" + token + " intent=" + intent + "}";
361 }
362 }
363
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400364 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700366 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700368 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 Intent args;
370 public String toString() {
371 return "ServiceArgsData{token=" + token + " startId=" + startId
372 + " args=" + args + "}";
373 }
374 }
375
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400376 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700377 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 String processName;
379 ApplicationInfo appInfo;
380 List<ProviderInfo> providers;
381 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 Bundle instrumentationArgs;
383 IInstrumentationWatcher instrumentationWatcher;
384 int debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800385 boolean enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700386 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700387 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400389 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700390
391 /** Initial values for {@link Profiler}. */
392 String initProfileFile;
393 ParcelFileDescriptor initProfileFd;
394 boolean initAutoStopProfiler;
395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 public String toString() {
397 return "AppBindData{appInfo=" + appInfo + "}";
398 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700399 }
400
401 static final class Profiler {
402 String profileFile;
403 ParcelFileDescriptor profileFd;
404 boolean autoStopProfiler;
405 boolean profiling;
406 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700407 public void setProfiler(String file, ParcelFileDescriptor fd) {
408 if (profiling) {
409 if (fd != null) {
410 try {
411 fd.close();
412 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700413 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700414 }
415 }
416 return;
417 }
418 if (profileFd != null) {
419 try {
420 profileFd.close();
421 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700422 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700423 }
424 }
425 profileFile = file;
426 profileFd = fd;
427 }
428 public void startProfiling() {
429 if (profileFd == null || profiling) {
430 return;
431 }
432 try {
433 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
434 8 * 1024 * 1024, 0);
435 profiling = true;
436 } catch (RuntimeException e) {
437 Slog.w(TAG, "Profiling failed on path " + profileFile);
438 try {
439 profileFd.close();
440 profileFd = null;
441 } catch (IOException e2) {
442 Slog.w(TAG, "Failure closing profile fd", e2);
443 }
444 }
445 }
446 public void stopProfiling() {
447 if (profiling) {
448 profiling = false;
449 Debug.stopMethodTracing();
450 if (profileFd != null) {
451 try {
452 profileFd.close();
453 } catch (IOException e) {
454 }
455 }
456 profileFd = null;
457 profileFile = null;
458 }
459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
461
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400462 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700463 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700464 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800465 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 }
468
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400469 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 IBinder token;
471 List<ResultInfo> results;
472 public String toString() {
473 return "ResultData{token=" + token + " results" + results + "}";
474 }
475 }
476
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400477 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800478 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 String what;
480 String who;
481 }
482
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400483 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700484 String path;
485 ParcelFileDescriptor fd;
486 }
487
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400488 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700489 String path;
490 ParcelFileDescriptor fd;
491 }
492
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400493 static final class UpdateCompatibilityData {
494 String pkg;
495 CompatibilityInfo info;
496 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700497
Romain Guy65b345f2011-07-27 18:51:50 -0700498 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700499
Romain Guy65b345f2011-07-27 18:51:50 -0700500 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700501 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
502 private static final String ONE_COUNT_COLUMN = "%21s %8d";
503 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700504 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 // Formatting for checkin service - update version if row format changes
507 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700508
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700509 private void updatePendingConfiguration(Configuration config) {
510 synchronized (mPackages) {
511 if (mPendingConfiguration == null ||
512 mPendingConfiguration.isOtherSeqNewer(config)) {
513 mPendingConfiguration = config;
514 }
515 }
516 }
517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 public final void schedulePauseActivity(IBinder token, boolean finished,
519 boolean userLeaving, int configChanges) {
520 queueOrSendMessage(
521 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
522 token,
523 (userLeaving ? 1 : 0),
524 configChanges);
525 }
526
527 public final void scheduleStopActivity(IBinder token, boolean showWindow,
528 int configChanges) {
529 queueOrSendMessage(
530 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
531 token, 0, configChanges);
532 }
533
534 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
535 queueOrSendMessage(
536 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
537 token);
538 }
539
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800540 public final void scheduleSleeping(IBinder token, boolean sleeping) {
541 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
542 }
543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
545 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
546 }
547
548 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
549 ResultData res = new ResultData();
550 res.token = token;
551 res.results = results;
552 queueOrSendMessage(H.SEND_RESULT, res);
553 }
554
555 // we use token to identify this activity without having to send the
556 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700557 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700558 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
559 Bundle state, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700560 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
561 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700562 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563
564 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700565 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 r.intent = intent;
567 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400568 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 r.state = state;
570
571 r.pendingResults = pendingResults;
572 r.pendingIntents = pendingNewIntents;
573
574 r.startsNotResumed = notResumed;
575 r.isForward = isForward;
576
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700577 r.profileFile = profileName;
578 r.profileFd = profileFd;
579 r.autoStopProfiler = autoStopProfiler;
580
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700581 updatePendingConfiguration(curConfig);
582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
584 }
585
586 public final void scheduleRelaunchActivity(IBinder token,
587 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800588 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800589 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
590 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 }
592
593 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
594 NewIntentData data = new NewIntentData();
595 data.intents = intents;
596 data.token = token;
597
598 queueOrSendMessage(H.NEW_INTENT, data);
599 }
600
601 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
602 int configChanges) {
603 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
604 configChanges);
605 }
606
607 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400608 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
609 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700610 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
611 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400613 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 queueOrSendMessage(H.RECEIVER, r);
615 }
616
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400617 public final void scheduleCreateBackupAgent(ApplicationInfo app,
618 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700619 CreateBackupAgentData d = new CreateBackupAgentData();
620 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400621 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700622 d.backupMode = backupMode;
623
624 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
625 }
626
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400627 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
628 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700629 CreateBackupAgentData d = new CreateBackupAgentData();
630 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400631 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700632
633 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
634 }
635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400637 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 CreateServiceData s = new CreateServiceData();
639 s.token = token;
640 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400641 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642
643 queueOrSendMessage(H.CREATE_SERVICE, s);
644 }
645
646 public final void scheduleBindService(IBinder token, Intent intent,
647 boolean rebind) {
648 BindServiceData s = new BindServiceData();
649 s.token = token;
650 s.intent = intent;
651 s.rebind = rebind;
652
Amith Yamasani742a6712011-05-04 14:49:28 -0700653 if (DEBUG_SERVICE)
654 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
655 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 queueOrSendMessage(H.BIND_SERVICE, s);
657 }
658
659 public final void scheduleUnbindService(IBinder token, Intent intent) {
660 BindServiceData s = new BindServiceData();
661 s.token = token;
662 s.intent = intent;
663
664 queueOrSendMessage(H.UNBIND_SERVICE, s);
665 }
666
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700667 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700668 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 ServiceArgsData s = new ServiceArgsData();
670 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700671 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700673 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 s.args = args;
675
676 queueOrSendMessage(H.SERVICE_ARGS, s);
677 }
678
679 public final void scheduleStopService(IBinder token) {
680 queueOrSendMessage(H.STOP_SERVICE, token);
681 }
682
683 public final void bindApplication(String processName,
684 ApplicationInfo appInfo, List<ProviderInfo> providers,
685 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700686 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Siva Velusamy92a8b222012-03-09 16:24:04 -0800688 int debugMode, boolean enableOpenGlTrace, boolean isRestrictedBackupMode,
689 boolean persistent, Configuration config, CompatibilityInfo compatInfo,
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700690 Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691
692 if (services != null) {
693 // Setup the service cache in the ServiceManager
694 ServiceManager.initServiceCache(services);
695 }
696
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800697 setCoreSettings(coreSettings);
698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 AppBindData data = new AppBindData();
700 data.processName = processName;
701 data.appInfo = appInfo;
702 data.providers = providers;
703 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 data.instrumentationArgs = instrumentationArgs;
705 data.instrumentationWatcher = instrumentationWatcher;
706 data.debugMode = debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800707 data.enableOpenGlTrace = enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700708 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700709 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400711 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700712 data.initProfileFile = profileFile;
713 data.initProfileFd = profileFd;
714 data.initAutoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 queueOrSendMessage(H.BIND_APPLICATION, data);
716 }
717
718 public final void scheduleExit() {
719 queueOrSendMessage(H.EXIT_APPLICATION, null);
720 }
721
Christopher Tate5e1ab332009-09-01 20:32:49 -0700722 public final void scheduleSuicide() {
723 queueOrSendMessage(H.SUICIDE, null);
724 }
725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 public void requestThumbnail(IBinder token) {
727 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
728 }
729
730 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700731 updatePendingConfiguration(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
733 }
734
735 public void updateTimeZone() {
736 TimeZone.setDefault(null);
737 }
738
Robert Greenwalt03595d02010-11-02 14:08:23 -0700739 public void clearDnsCache() {
740 // a non-standard API to get this to libcore
741 InetAddress.clearDnsCache();
742 }
743
Robert Greenwalt434203a2010-10-11 16:00:27 -0700744 public void setHttpProxy(String host, String port, String exclList) {
745 Proxy.setHttpProxySystemProperty(host, port, exclList);
746 }
747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 public void processInBackground() {
749 mH.removeMessages(H.GC_WHEN_IDLE);
750 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
751 }
752
753 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700754 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700755 try {
756 data.fd = ParcelFileDescriptor.dup(fd);
757 data.token = servicetoken;
758 data.args = args;
759 queueOrSendMessage(H.DUMP_SERVICE, data);
760 } catch (IOException e) {
761 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 }
763 }
764
765 // This function exists to make sure all receiver dispatching is
766 // correctly ordered, since these are one-way calls and the binder driver
767 // applies transaction ordering per object for such calls.
768 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700769 int resultCode, String dataStr, Bundle extras, boolean ordered,
770 boolean sticky) throws RemoteException {
771 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 }
Bob Leee5408332009-09-04 18:31:17 -0700773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 public void scheduleLowMemory() {
775 queueOrSendMessage(H.LOW_MEMORY, null);
776 }
777
778 public void scheduleActivityConfigurationChanged(IBinder token) {
779 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
780 }
781
Romain Guy7eabe552011-07-21 14:56:34 -0700782 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
783 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700784 ProfilerControlData pcd = new ProfilerControlData();
785 pcd.path = path;
786 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700787 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800788 }
789
Andy McFadden824c5102010-07-09 16:26:57 -0700790 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
791 DumpHeapData dhd = new DumpHeapData();
792 dhd.path = path;
793 dhd.fd = fd;
794 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
795 }
796
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700797 public void setSchedulingGroup(int group) {
798 // Note: do this immediately, since going into the foreground
799 // should happen regardless of what pending work we have to do
800 // and the activity manager will wait for us to report back that
801 // we are done before sending us to the background.
802 try {
803 Process.setProcessGroup(Process.myPid(), group);
804 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800805 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700806 }
807 }
Bob Leee5408332009-09-04 18:31:17 -0700808
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700809 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
810 Debug.getMemoryInfo(outInfo);
811 }
Bob Leee5408332009-09-04 18:31:17 -0700812
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700813 public void dispatchPackageBroadcast(int cmd, String[] packages) {
814 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
815 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700816
817 public void scheduleCrash(String msg) {
818 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
819 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700820
Dianne Hackborn30d71892010-12-11 10:37:55 -0800821 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
822 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700823 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700824 try {
825 data.fd = ParcelFileDescriptor.dup(fd);
826 data.token = activitytoken;
827 data.prefix = prefix;
828 data.args = args;
829 queueOrSendMessage(H.DUMP_ACTIVITY, data);
830 } catch (IOException e) {
831 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700832 }
833 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700834
Marco Nelissen18cb2872011-11-15 11:19:53 -0800835 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
836 String[] args) {
837 DumpComponentInfo data = new DumpComponentInfo();
838 try {
839 data.fd = ParcelFileDescriptor.dup(fd);
840 data.token = providertoken;
841 data.args = args;
842 queueOrSendMessage(H.DUMP_PROVIDER, data);
843 } catch (IOException e) {
844 Slog.w(TAG, "dumpProvider failed", e);
845 }
846 }
847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700849 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
850 boolean all, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700851 FileOutputStream fout = new FileOutputStream(fd);
852 PrintWriter pw = new PrintWriter(fout);
853 try {
Romain Guya998dff2012-03-23 18:58:36 -0700854 return dumpMemInfo(pw, checkin, all);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700855 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700856 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700857 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700858 }
859
Romain Guya998dff2012-03-23 18:58:36 -0700860 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean all) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 long nativeMax = Debug.getNativeHeapSize() / 1024;
862 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
863 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
864
865 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
866 Debug.getMemoryInfo(memInfo);
867
Dianne Hackbornb437e092011-08-05 17:50:29 -0700868 if (!all) {
869 return memInfo;
870 }
871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 Runtime runtime = Runtime.getRuntime();
873
874 long dalvikMax = runtime.totalMemory() / 1024;
875 long dalvikFree = runtime.freeMemory() / 1024;
876 long dalvikAllocated = dalvikMax - dalvikFree;
877 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700878 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700879 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
880 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 int globalAssetCount = AssetManager.getGlobalAssetCount();
882 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
883 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
884 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
885 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700886 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800887 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700890 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 // NOTE: if you change anything significant below, also consider changing
892 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700893 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 // Header
897 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
898 pw.print(Process.myPid()); pw.print(',');
899 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 // Heap info - max
902 pw.print(nativeMax); pw.print(',');
903 pw.print(dalvikMax); pw.print(',');
904 pw.print("N/A,");
905 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 // Heap info - allocated
908 pw.print(nativeAllocated); pw.print(',');
909 pw.print(dalvikAllocated); pw.print(',');
910 pw.print("N/A,");
911 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 // Heap info - free
914 pw.print(nativeFree); pw.print(',');
915 pw.print(dalvikFree); pw.print(',');
916 pw.print("N/A,");
917 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 // Heap info - proportional set size
920 pw.print(memInfo.nativePss); pw.print(',');
921 pw.print(memInfo.dalvikPss); pw.print(',');
922 pw.print(memInfo.otherPss); pw.print(',');
923 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700926 pw.print(memInfo.nativeSharedDirty); pw.print(',');
927 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
928 pw.print(memInfo.otherSharedDirty); pw.print(',');
929 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
930 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700933 pw.print(memInfo.nativePrivateDirty); pw.print(',');
934 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
935 pw.print(memInfo.otherPrivateDirty); pw.print(',');
936 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
937 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 // Object counts
940 pw.print(viewInstanceCount); pw.print(',');
941 pw.print(viewRootInstanceCount); pw.print(',');
942 pw.print(appContextInstanceCount); pw.print(',');
943 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 pw.print(globalAssetCount); pw.print(',');
946 pw.print(globalAssetManagerCount); pw.print(',');
947 pw.print(binderLocalObjectCount); pw.print(',');
948 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 pw.print(binderDeathObjectCount); pw.print(',');
951 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 // SQL
Vasu Noric3849202010-03-09 10:47:25 -0800954 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -0800955 pw.print(stats.memoryUsed / 1024); pw.print(',');
956 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700957 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800958 for (int i = 0; i < stats.dbStats.size(); i++) {
959 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700960 pw.print(','); pw.print(dbStats.dbName);
961 pw.print(','); pw.print(dbStats.pageSize);
962 pw.print(','); pw.print(dbStats.dbSize);
963 pw.print(','); pw.print(dbStats.lookaside);
964 pw.print(','); pw.print(dbStats.cache);
965 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800966 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700967 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700968
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700969 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 }
Bob Leee5408332009-09-04 18:31:17 -0700971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700973 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
974 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
975 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
976 "------");
977 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
978 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
979 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
980 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700982 int otherPss = memInfo.otherPss;
983 int otherSharedDirty = memInfo.otherSharedDirty;
984 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700986 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Romain Guy65b345f2011-07-27 18:51:50 -0700987 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700988 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
989 memInfo.getOtherPrivateDirty(i), "", "", "");
990 otherPss -= memInfo.getOtherPss(i);
991 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
992 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
993 }
994
995 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
996 otherPrivateDirty, "", "", "");
997 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
998 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
999 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
1000 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001
1002 pw.println(" ");
1003 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001004 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 viewRootInstanceCount);
1006
1007 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1008 "Activities:", activityInstanceCount);
1009
1010 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1011 "AssetManagers:", globalAssetManagerCount);
1012
1013 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1014 "Proxy Binders:", binderProxyObjectCount);
1015 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
1016
1017 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 // SQLite mem info
1020 pw.println(" ");
1021 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001022 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1023 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1024 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001025 pw.println(" ");
1026 int N = stats.dbStats.size();
1027 if (N > 0) {
1028 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001029 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1030 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001031 for (int i = 0; i < N; i++) {
1032 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001033 printRow(pw, DB_INFO_FORMAT,
1034 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1035 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1036 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1037 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001038 }
1039 }
Bob Leee5408332009-09-04 18:31:17 -07001040
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001041 // Asset details.
1042 String assetAlloc = AssetManager.getAssetAllocations();
1043 if (assetAlloc != null) {
1044 pw.println(" ");
1045 pw.println(" Asset Allocations");
1046 pw.print(assetAlloc);
1047 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001048
1049 return memInfo;
1050 }
1051
1052 @Override
1053 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1054 dumpGraphicsInfo(fd);
Romain Guy65b345f2011-07-27 18:51:50 -07001055 WindowManagerImpl.getDefault().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 }
1057
Jeff Brown6754ba22011-12-14 20:20:01 -08001058 @Override
1059 public void dumpDbInfo(FileDescriptor fd, String[] args) {
1060 PrintWriter pw = new PrintWriter(new FileOutputStream(fd));
1061 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1062 SQLiteDebug.dump(printer, args);
1063 pw.flush();
1064 }
1065
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001066 @Override
1067 public void unstableProviderDied(IBinder provider) {
1068 queueOrSendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
1069 }
1070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 private void printRow(PrintWriter pw, String format, Object...objs) {
1072 pw.println(String.format(format, objs));
1073 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001074
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001075 public void setCoreSettings(Bundle coreSettings) {
1076 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001077 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001078
1079 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1080 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1081 ucd.pkg = pkg;
1082 ucd.info = info;
1083 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1084 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001085
1086 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001087 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001088 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 }
1091
Romain Guy65b345f2011-07-27 18:51:50 -07001092 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 public static final int LAUNCH_ACTIVITY = 100;
1094 public static final int PAUSE_ACTIVITY = 101;
1095 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1096 public static final int STOP_ACTIVITY_SHOW = 103;
1097 public static final int STOP_ACTIVITY_HIDE = 104;
1098 public static final int SHOW_WINDOW = 105;
1099 public static final int HIDE_WINDOW = 106;
1100 public static final int RESUME_ACTIVITY = 107;
1101 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001102 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 public static final int BIND_APPLICATION = 110;
1104 public static final int EXIT_APPLICATION = 111;
1105 public static final int NEW_INTENT = 112;
1106 public static final int RECEIVER = 113;
1107 public static final int CREATE_SERVICE = 114;
1108 public static final int SERVICE_ARGS = 115;
1109 public static final int STOP_SERVICE = 116;
1110 public static final int REQUEST_THUMBNAIL = 117;
1111 public static final int CONFIGURATION_CHANGED = 118;
1112 public static final int CLEAN_UP_CONTEXT = 119;
1113 public static final int GC_WHEN_IDLE = 120;
1114 public static final int BIND_SERVICE = 121;
1115 public static final int UNBIND_SERVICE = 122;
1116 public static final int DUMP_SERVICE = 123;
1117 public static final int LOW_MEMORY = 124;
1118 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1119 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001120 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001121 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001122 public static final int DESTROY_BACKUP_AGENT = 129;
1123 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001124 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001125 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001126 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001127 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001128 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001129 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001130 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001131 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001132 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001133 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001134 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001135 public static final int UNSTABLE_PROVIDER_DIED = 142;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001137 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 switch (code) {
1139 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1140 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1141 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1142 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1143 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1144 case SHOW_WINDOW: return "SHOW_WINDOW";
1145 case HIDE_WINDOW: return "HIDE_WINDOW";
1146 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1147 case SEND_RESULT: return "SEND_RESULT";
1148 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1149 case BIND_APPLICATION: return "BIND_APPLICATION";
1150 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1151 case NEW_INTENT: return "NEW_INTENT";
1152 case RECEIVER: return "RECEIVER";
1153 case CREATE_SERVICE: return "CREATE_SERVICE";
1154 case SERVICE_ARGS: return "SERVICE_ARGS";
1155 case STOP_SERVICE: return "STOP_SERVICE";
1156 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1157 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1158 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1159 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1160 case BIND_SERVICE: return "BIND_SERVICE";
1161 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1162 case DUMP_SERVICE: return "DUMP_SERVICE";
1163 case LOW_MEMORY: return "LOW_MEMORY";
1164 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1165 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001166 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001167 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1168 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001169 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001170 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001171 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001172 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001173 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001174 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001175 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001176 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001177 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001178 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001179 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001180 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001181 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
1183 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001184 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 }
1186 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001187 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 switch (msg.what) {
1189 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001190 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001191 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192
1193 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001194 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001195 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001196 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 } break;
1198 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001199 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001200 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001201 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001202 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 } break;
1204 case PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001205 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001207 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001208 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 break;
1210 case PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001211 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001213 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 break;
1215 case STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001216 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001218 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 break;
1220 case STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001221 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001223 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 break;
1225 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001226 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001228 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 break;
1230 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001231 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001233 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 break;
1235 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001236 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 handleResumeActivity((IBinder)msg.obj, true,
1238 msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001239 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 break;
1241 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001242 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001244 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 break;
1246 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001247 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1249 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001250 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 break;
1252 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001253 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 AppBindData data = (AppBindData)msg.obj;
1255 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001256 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 break;
1258 case EXIT_APPLICATION:
1259 if (mInitialApplication != null) {
1260 mInitialApplication.onTerminate();
1261 }
1262 Looper.myLooper().quit();
1263 break;
1264 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001265 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001267 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 break;
1269 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001270 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001272 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001273 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 break;
1275 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001276 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001278 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 break;
1280 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001281 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001283 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 break;
1285 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001286 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001288 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 break;
1290 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001291 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001293 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 break;
1295 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001296 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001298 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001299 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 break;
1301 case REQUEST_THUMBNAIL:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001302 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "requestThumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 handleRequestThumbnail((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001304 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 break;
1306 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001307 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001308 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001309 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 break;
1311 case CLEAN_UP_CONTEXT:
1312 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1313 cci.context.performFinalCleanup(cci.who, cci.what);
1314 break;
1315 case GC_WHEN_IDLE:
1316 scheduleGcIdler();
1317 break;
1318 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001319 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 break;
1321 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001322 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001324 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 break;
1326 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001327 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001329 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001331 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001332 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001333 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001334 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001335 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001336 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001337 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001338 break;
1339 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001340 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001341 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001342 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001343 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001344 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001345 Process.killProcess(Process.myPid());
1346 break;
1347 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001348 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001349 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001350 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001351 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001352 case ENABLE_JIT:
1353 ensureJitEnabled();
1354 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001355 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001356 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001357 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001358 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001359 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001360 case SCHEDULE_CRASH:
1361 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001362 case DUMP_HEAP:
1363 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1364 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001365 case DUMP_ACTIVITY:
1366 handleDumpActivity((DumpComponentInfo)msg.obj);
1367 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001368 case DUMP_PROVIDER:
1369 handleDumpProvider((DumpComponentInfo)msg.obj);
1370 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001371 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001372 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001373 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001374 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001375 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001376 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001377 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001378 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001379 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001380 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001381 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1382 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001383 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001384 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001385 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001386 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001387 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001388 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001389 case UNSTABLE_PROVIDER_DIED:
1390 handleUnstableProviderDied((IBinder)msg.obj, false);
1391 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001393 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 }
Bob Leee5408332009-09-04 18:31:17 -07001395
Brian Carlstromed7e0072011-03-24 13:27:57 -07001396 private void maybeSnapshot() {
1397 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001398 // convert the *private* ActivityThread.PackageInfo to *public* known
1399 // android.content.pm.PackageInfo
1400 String packageName = mBoundApplication.info.mPackageName;
1401 android.content.pm.PackageInfo packageInfo = null;
1402 try {
1403 Context context = getSystemContext();
1404 if(context == null) {
1405 Log.e(TAG, "cannot get a valid context");
1406 return;
1407 }
1408 PackageManager pm = context.getPackageManager();
1409 if(pm == null) {
1410 Log.e(TAG, "cannot get a valid PackageManager");
1411 return;
1412 }
1413 packageInfo = pm.getPackageInfo(
1414 packageName, PackageManager.GET_ACTIVITIES);
1415 } catch (NameNotFoundException e) {
1416 Log.e(TAG, "cannot get package info for " + packageName, e);
1417 }
1418 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001419 }
1420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 }
1422
Romain Guy65b345f2011-07-27 18:51:50 -07001423 private class Idler implements MessageQueue.IdleHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001425 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001426 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001427 if (mBoundApplication != null && mProfiler.profileFd != null
1428 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001429 stopProfiling = true;
1430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 if (a != null) {
1432 mNewActivities = null;
1433 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001434 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001436 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 TAG, "Reporting idle of " + a +
1438 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001439 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 if (a.activity != null && !a.activity.mFinished) {
1441 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001442 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001443 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001445 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 }
1447 }
1448 prev = a;
1449 a = a.nextIdle;
1450 prev.nextIdle = null;
1451 } while (a != null);
1452 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001453 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001454 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001455 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001456 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 return false;
1458 }
1459 }
1460
1461 final class GcIdler implements MessageQueue.IdleHandler {
1462 public final boolean queueIdle() {
1463 doGcIfNeeded();
1464 return false;
1465 }
1466 }
1467
Romain Guy65b345f2011-07-27 18:51:50 -07001468 private static class ResourcesKey {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001469 final private String mResDir;
1470 final private float mScale;
1471 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001472
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001473 ResourcesKey(String resDir, float scale) {
1474 mResDir = resDir;
1475 mScale = scale;
1476 mHash = mResDir.hashCode() << 2 + (int) (mScale * 2);
1477 }
Bob Leee5408332009-09-04 18:31:17 -07001478
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001479 @Override
1480 public int hashCode() {
1481 return mHash;
1482 }
1483
1484 @Override
1485 public boolean equals(Object obj) {
1486 if (!(obj instanceof ResourcesKey)) {
1487 return false;
1488 }
1489 ResourcesKey peer = (ResourcesKey) obj;
1490 return mResDir.equals(peer.mResDir) && mScale == peer.mScale;
1491 }
1492 }
1493
Romain Guy65b345f2011-07-27 18:51:50 -07001494 public static ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001495 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497
Romain Guy65b345f2011-07-27 18:51:50 -07001498 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001499 ActivityThread am = currentActivityThread();
1500 return (am != null && am.mBoundApplication != null)
1501 ? am.mBoundApplication.processName : null;
1502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503
Romain Guy65b345f2011-07-27 18:51:50 -07001504 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001505 ActivityThread am = currentActivityThread();
1506 return am != null ? am.mInitialApplication : null;
1507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001509 public static IPackageManager getPackageManager() {
1510 if (sPackageManager != null) {
1511 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1512 return sPackageManager;
1513 }
1514 IBinder b = ServiceManager.getService("package");
1515 //Slog.v("PackageManager", "default service binder = " + b);
1516 sPackageManager = IPackageManager.Stub.asInterface(b);
1517 //Slog.v("PackageManager", "default service = " + sPackageManager);
1518 return sPackageManager;
1519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001521 DisplayMetrics getDisplayMetricsLocked(CompatibilityInfo ci, boolean forceUpdate) {
1522 DisplayMetrics dm = mDisplayMetrics.get(ci);
1523 if (dm != null && !forceUpdate) {
1524 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001525 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001526 if (dm == null) {
1527 dm = new DisplayMetrics();
1528 mDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001529 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001530 Display d = WindowManagerImpl.getDefault(ci).getDefaultDisplay();
1531 d.getMetrics(dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001532 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1533 // + metrics.heightPixels + " den=" + metrics.density
1534 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001535 return dm;
1536 }
1537
Romain Guy65b345f2011-07-27 18:51:50 -07001538 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001539 Configuration applyConfigCompatMainThread(Configuration config, CompatibilityInfo compat) {
1540 if (config == null) {
1541 return null;
1542 }
1543 if (compat != null && !compat.supportsScreen()) {
1544 mMainThreadConfig.setTo(config);
1545 config = mMainThreadConfig;
1546 compat.applyToConfiguration(config);
1547 }
1548 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001551 /**
1552 * Creates the top level Resources for applications with the given compatibility info.
1553 *
1554 * @param resDir the resource directory.
1555 * @param compInfo the compability info. It will use the default compatibility info when it's
1556 * null.
1557 */
1558 Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) {
1559 ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale);
1560 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001562 // Resources is app scale dependent.
1563 if (false) {
1564 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1565 + compInfo.applicationScale);
1566 }
1567 WeakReference<Resources> wr = mActiveResources.get(key);
1568 r = wr != null ? wr.get() : null;
1569 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1570 if (r != null && r.getAssets().isUpToDate()) {
1571 if (false) {
1572 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1573 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1574 }
1575 return r;
1576 }
1577 }
1578
1579 //if (r != null) {
1580 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1581 // + r + " " + resDir);
1582 //}
1583
1584 AssetManager assets = new AssetManager();
1585 if (assets.addAssetPath(resDir) == 0) {
1586 return null;
1587 }
1588
1589 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Dianne Hackborn836e2622011-10-04 18:32:39 -07001590 DisplayMetrics metrics = getDisplayMetricsLocked(null, false);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001591 r = new Resources(assets, metrics, getConfiguration(), compInfo);
1592 if (false) {
1593 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1594 + r.getConfiguration() + " appScale="
1595 + r.getCompatibilityInfo().applicationScale);
1596 }
1597
1598 synchronized (mPackages) {
1599 WeakReference<Resources> wr = mActiveResources.get(key);
1600 Resources existing = wr != null ? wr.get() : null;
1601 if (existing != null && existing.getAssets().isUpToDate()) {
1602 // Someone else already created the resources while we were
1603 // unlocked; go ahead and use theirs.
1604 r.getAssets().close();
1605 return existing;
1606 }
1607
1608 // XXX need to remove entries when weak references go away
1609 mActiveResources.put(key, new WeakReference<Resources>(r));
1610 return r;
1611 }
1612 }
1613
1614 /**
1615 * Creates the top level resources for the given package.
1616 */
1617 Resources getTopLevelResources(String resDir, LoadedApk pkgInfo) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001618 return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001619 }
1620
1621 final Handler getHandler() {
1622 return mH;
1623 }
1624
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001625 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1626 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001627 synchronized (mPackages) {
1628 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1630 ref = mPackages.get(packageName);
1631 } else {
1632 ref = mResourcePackages.get(packageName);
1633 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001634 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001635 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001636 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1637 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 if (packageInfo != null && (packageInfo.mResources == null
1639 || packageInfo.mResources.getAssets().isUpToDate())) {
1640 if (packageInfo.isSecurityViolation()
1641 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1642 throw new SecurityException(
1643 "Requesting code from " + packageName
1644 + " to be run in process "
1645 + mBoundApplication.processName
1646 + "/" + mBoundApplication.appInfo.uid);
1647 }
1648 return packageInfo;
1649 }
1650 }
1651
1652 ApplicationInfo ai = null;
1653 try {
1654 ai = getPackageManager().getApplicationInfo(packageName,
Amith Yamasani483f3b02012-03-13 16:08:00 -07001655 PackageManager.GET_SHARED_LIBRARY_FILES, UserId.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001657 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
1659
1660 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001661 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 }
1663
1664 return null;
1665 }
1666
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001667 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1668 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1670 boolean securityViolation = includeCode && ai.uid != 0
1671 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Amith Yamasani742a6712011-05-04 14:49:28 -07001672 ? !UserId.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
1673 : true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1675 |Context.CONTEXT_IGNORE_SECURITY))
1676 == Context.CONTEXT_INCLUDE_CODE) {
1677 if (securityViolation) {
1678 String msg = "Requesting code from " + ai.packageName
1679 + " (with uid " + ai.uid + ")";
1680 if (mBoundApplication != null) {
1681 msg = msg + " to be run in process "
1682 + mBoundApplication.processName + " (with uid "
1683 + mBoundApplication.appInfo.uid + ")";
1684 }
1685 throw new SecurityException(msg);
1686 }
1687 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001688 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 }
1690
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001691 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1692 CompatibilityInfo compatInfo) {
1693 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 }
1695
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001696 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1697 synchronized (mPackages) {
1698 WeakReference<LoadedApk> ref;
1699 if (includeCode) {
1700 ref = mPackages.get(packageName);
1701 } else {
1702 ref = mResourcePackages.get(packageName);
1703 }
1704 return ref != null ? ref.get() : null;
1705 }
1706 }
1707
Romain Guy65b345f2011-07-27 18:51:50 -07001708 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1710 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001711 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 if (includeCode) {
1713 ref = mPackages.get(aInfo.packageName);
1714 } else {
1715 ref = mResourcePackages.get(aInfo.packageName);
1716 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001717 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 if (packageInfo == null || (packageInfo.mResources != null
1719 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001720 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 : "Loading resource-only package ") + aInfo.packageName
1722 + " (in " + (mBoundApplication != null
1723 ? mBoundApplication.processName : null)
1724 + ")");
1725 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001726 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 securityViolation, includeCode &&
1728 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1729 if (includeCode) {
1730 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001731 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 } else {
1733 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001734 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 }
1736 }
1737 return packageInfo;
1738 }
1739 }
1740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 ActivityThread() {
1742 }
1743
1744 public ApplicationThread getApplicationThread()
1745 {
1746 return mAppThread;
1747 }
1748
1749 public Instrumentation getInstrumentation()
1750 {
1751 return mInstrumentation;
1752 }
1753
1754 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001755 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 }
1757
1758 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001759 return mProfiler != null && mProfiler.profileFile != null
1760 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 }
1762
1763 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001764 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 }
1766
1767 public Looper getLooper() {
1768 return mLooper;
1769 }
1770
1771 public Application getApplication() {
1772 return mInitialApplication;
1773 }
Bob Leee5408332009-09-04 18:31:17 -07001774
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001775 public String getProcessName() {
1776 return mBoundApplication.processName;
1777 }
Bob Leee5408332009-09-04 18:31:17 -07001778
Dianne Hackborn21556372010-02-04 16:34:40 -08001779 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 synchronized (this) {
1781 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001782 ContextImpl context =
1783 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001784 LoadedApk info = new LoadedApk(this, "android", context, null,
1785 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 context.init(info, null, this);
1787 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001788 getConfiguration(), getDisplayMetricsLocked(
1789 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001791 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 // + ": " + context.getResources().getConfiguration());
1793 }
1794 }
1795 return mSystemContext;
1796 }
1797
Mike Cleron432b7132009-09-24 15:28:29 -07001798 public void installSystemApplicationInfo(ApplicationInfo info) {
1799 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001800 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001801 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001802 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001803
1804 // give ourselves a default profiler
1805 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001806 }
1807 }
1808
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001809 void ensureJitEnabled() {
1810 if (!mJitEnabled) {
1811 mJitEnabled = true;
1812 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1813 }
1814 }
1815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 void scheduleGcIdler() {
1817 if (!mGcIdlerScheduled) {
1818 mGcIdlerScheduled = true;
1819 Looper.myQueue().addIdleHandler(mGcIdler);
1820 }
1821 mH.removeMessages(H.GC_WHEN_IDLE);
1822 }
1823
1824 void unscheduleGcIdler() {
1825 if (mGcIdlerScheduled) {
1826 mGcIdlerScheduled = false;
1827 Looper.myQueue().removeIdleHandler(mGcIdler);
1828 }
1829 mH.removeMessages(H.GC_WHEN_IDLE);
1830 }
1831
1832 void doGcIfNeeded() {
1833 mGcIdlerScheduled = false;
1834 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001835 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 // + "m now=" + now);
1837 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001838 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 BinderInternal.forceGc("bg");
1840 }
1841 }
1842
Jeff Hamilton52d32032011-01-08 15:31:26 -06001843 public void registerOnActivityPausedListener(Activity activity,
1844 OnActivityPausedListener listener) {
1845 synchronized (mOnPauseListeners) {
1846 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1847 if (list == null) {
1848 list = new ArrayList<OnActivityPausedListener>();
1849 mOnPauseListeners.put(activity, list);
1850 }
1851 list.add(listener);
1852 }
1853 }
1854
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001855 public void unregisterOnActivityPausedListener(Activity activity,
1856 OnActivityPausedListener listener) {
1857 synchronized (mOnPauseListeners) {
1858 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1859 if (list != null) {
1860 list.remove(listener);
1861 }
1862 }
1863 }
1864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 public final ActivityInfo resolveActivityInfo(Intent intent) {
1866 ActivityInfo aInfo = intent.resolveActivityInfo(
1867 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1868 if (aInfo == null) {
1869 // Throw an exception.
1870 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07001871 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 }
1873 return aInfo;
1874 }
Bob Leee5408332009-09-04 18:31:17 -07001875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001878 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001879 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001881 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 r.intent = intent;
1883 r.state = state;
1884 r.parent = parent;
1885 r.embeddedID = id;
1886 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001887 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 if (localLOGV) {
1889 ComponentName compname = intent.getComponent();
1890 String name;
1891 if (compname != null) {
1892 name = compname.toShortString();
1893 } else {
1894 name = "(Intent " + intent + ").getComponent() returned null";
1895 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001896 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 + ", comp=" + name
1898 + ", token=" + token);
1899 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001900 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 }
1902
1903 public final Activity getActivity(IBinder token) {
1904 return mActivities.get(token).activity;
1905 }
1906
1907 public final void sendActivityResult(
1908 IBinder token, String id, int requestCode,
1909 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001910 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001911 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1913 list.add(new ResultInfo(id, requestCode, resultCode, data));
1914 mAppThread.scheduleSendResult(token, list);
1915 }
1916
1917 // if the thread hasn't started yet, we don't have the handler, so just
1918 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07001919 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 queueOrSendMessage(what, obj, 0, 0);
1921 }
1922
Romain Guy65b345f2011-07-27 18:51:50 -07001923 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 queueOrSendMessage(what, obj, arg1, 0);
1925 }
1926
Romain Guy65b345f2011-07-27 18:51:50 -07001927 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001929 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1931 + ": " + arg1 + " / " + obj);
1932 Message msg = Message.obtain();
1933 msg.what = what;
1934 msg.obj = obj;
1935 msg.arg1 = arg1;
1936 msg.arg2 = arg2;
1937 mH.sendMessage(msg);
1938 }
1939 }
1940
Dianne Hackborn21556372010-02-04 16:34:40 -08001941 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 String what) {
1943 ContextCleanupInfo cci = new ContextCleanupInfo();
1944 cci.context = context;
1945 cci.who = who;
1946 cci.what = what;
1947 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1948 }
1949
Romain Guy65b345f2011-07-27 18:51:50 -07001950 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1952
1953 ActivityInfo aInfo = r.activityInfo;
1954 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001955 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 Context.CONTEXT_INCLUDE_CODE);
1957 }
Bob Leee5408332009-09-04 18:31:17 -07001958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 ComponentName component = r.intent.getComponent();
1960 if (component == null) {
1961 component = r.intent.resolveActivity(
1962 mInitialApplication.getPackageManager());
1963 r.intent.setComponent(component);
1964 }
1965
1966 if (r.activityInfo.targetActivity != null) {
1967 component = new ComponentName(r.activityInfo.packageName,
1968 r.activityInfo.targetActivity);
1969 }
1970
1971 Activity activity = null;
1972 try {
1973 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
1974 activity = mInstrumentation.newActivity(
1975 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08001976 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 r.intent.setExtrasClassLoader(cl);
1978 if (r.state != null) {
1979 r.state.setClassLoader(cl);
1980 }
1981 } catch (Exception e) {
1982 if (!mInstrumentation.onException(activity, e)) {
1983 throw new RuntimeException(
1984 "Unable to instantiate activity " + component
1985 + ": " + e.toString(), e);
1986 }
1987 }
1988
1989 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001990 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001991
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001992 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
1993 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 TAG, r + ": app=" + app
1995 + ", appName=" + app.getPackageName()
1996 + ", pkg=" + r.packageInfo.getPackageName()
1997 + ", comp=" + r.intent.getComponent().toShortString()
1998 + ", dir=" + r.packageInfo.getAppDir());
1999
2000 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08002001 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 appContext.init(r.packageInfo, r.token, this);
2003 appContext.setOuterContext(activity);
2004 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002005 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002006 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002007 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002008 activity.attach(appContext, this, getInstrumentation(), r.token,
2009 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002010 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07002011
Christopher Tateb70f3df2009-04-07 16:07:59 -07002012 if (customIntent != null) {
2013 activity.mIntent = customIntent;
2014 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002015 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 activity.mStartedActivity = false;
2017 int theme = r.activityInfo.getThemeResource();
2018 if (theme != 0) {
2019 activity.setTheme(theme);
2020 }
2021
2022 activity.mCalled = false;
2023 mInstrumentation.callActivityOnCreate(activity, r.state);
2024 if (!activity.mCalled) {
2025 throw new SuperNotCalledException(
2026 "Activity " + r.intent.getComponent().toShortString() +
2027 " did not call through to super.onCreate()");
2028 }
2029 r.activity = activity;
2030 r.stopped = true;
2031 if (!r.activity.mFinished) {
2032 activity.performStart();
2033 r.stopped = false;
2034 }
2035 if (!r.activity.mFinished) {
2036 if (r.state != null) {
2037 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2038 }
2039 }
2040 if (!r.activity.mFinished) {
2041 activity.mCalled = false;
2042 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2043 if (!activity.mCalled) {
2044 throw new SuperNotCalledException(
2045 "Activity " + r.intent.getComponent().toShortString() +
2046 " did not call through to super.onPostCreate()");
2047 }
2048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 }
2050 r.paused = true;
2051
2052 mActivities.put(r.token, r);
2053
2054 } catch (SuperNotCalledException e) {
2055 throw e;
2056
2057 } catch (Exception e) {
2058 if (!mInstrumentation.onException(activity, e)) {
2059 throw new RuntimeException(
2060 "Unable to start activity " + component
2061 + ": " + e.toString(), e);
2062 }
2063 }
2064
2065 return activity;
2066 }
2067
Romain Guy65b345f2011-07-27 18:51:50 -07002068 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 // If we are getting ready to gc after going to the background, well
2070 // we are back active so skip it.
2071 unscheduleGcIdler();
2072
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002073 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002074 mProfiler.setProfiler(r.profileFile, r.profileFd);
2075 mProfiler.startProfiling();
2076 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002077 }
2078
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002079 // Make sure we are running with the most recent config.
2080 handleConfigurationChanged(null, null);
2081
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002082 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07002084 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085
2086 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002087 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002088 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 handleResumeActivity(r.token, false, r.isForward);
2090
2091 if (!r.activity.mFinished && r.startsNotResumed) {
2092 // The activity manager actually wants this one to start out
2093 // paused, because it needs to be visible but isn't in the
2094 // foreground. We accomplish this by going through the
2095 // normal startup (because activities expect to go through
2096 // onResume() the first time they run, before their window
2097 // is displayed), and then pausing it. However, in this case
2098 // we do -not- need to do the full pause cycle (of freezing
2099 // and such) because the activity manager assumes it can just
2100 // retain the current state it has.
2101 try {
2102 r.activity.mCalled = false;
2103 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002104 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002105 // we need to be created again. But we only do this
2106 // for pre-Honeycomb apps, which always save their state
2107 // when pausing, so we can not have them save their state
2108 // when restarting from a paused state. For HC and later,
2109 // we want to (and can) let the state be saved as the normal
2110 // part of stopping the activity.
2111 if (r.isPreHoneycomb()) {
2112 r.state = oldState;
2113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 if (!r.activity.mCalled) {
2115 throw new SuperNotCalledException(
2116 "Activity " + r.intent.getComponent().toShortString() +
2117 " did not call through to super.onPause()");
2118 }
2119
2120 } catch (SuperNotCalledException e) {
2121 throw e;
2122
2123 } catch (Exception e) {
2124 if (!mInstrumentation.onException(r.activity, e)) {
2125 throw new RuntimeException(
2126 "Unable to pause activity "
2127 + r.intent.getComponent().toShortString()
2128 + ": " + e.toString(), e);
2129 }
2130 }
2131 r.paused = true;
2132 }
2133 } else {
2134 // If there was an error, for any reason, tell the activity
2135 // manager to stop us.
2136 try {
2137 ActivityManagerNative.getDefault()
2138 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2139 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002140 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 }
2142 }
2143 }
2144
Romain Guy65b345f2011-07-27 18:51:50 -07002145 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 List<Intent> intents) {
2147 final int N = intents.size();
2148 for (int i=0; i<N; i++) {
2149 Intent intent = intents.get(i);
2150 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002151 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2153 }
2154 }
2155
2156 public final void performNewIntents(IBinder token,
2157 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002158 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 if (r != null) {
2160 final boolean resumed = !r.paused;
2161 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002162 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 mInstrumentation.callActivityOnPause(r.activity);
2164 }
2165 deliverNewIntents(r, intents);
2166 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002167 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002168 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 }
2170 }
2171 }
Bob Leee5408332009-09-04 18:31:17 -07002172
Romain Guy65b345f2011-07-27 18:51:50 -07002173 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 performNewIntents(data.token, data.intents);
2175 }
2176
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002177 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2178
2179 /**
2180 * Return the Intent that's currently being handled by a
2181 * BroadcastReceiver on this thread, or null if none.
2182 * @hide
2183 */
2184 public static Intent getIntentBeingBroadcast() {
2185 return sCurrentBroadcastIntent.get();
2186 }
2187
Romain Guy65b345f2011-07-27 18:51:50 -07002188 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 // If we are getting ready to gc after going to the background, well
2190 // we are back active so skip it.
2191 unscheduleGcIdler();
2192
2193 String component = data.intent.getComponent().getClassName();
2194
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002195 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002196 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197
2198 IActivityManager mgr = ActivityManagerNative.getDefault();
2199
Romain Guy65b345f2011-07-27 18:51:50 -07002200 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 try {
2202 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2203 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002204 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2206 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002207 if (DEBUG_BROADCAST) Slog.i(TAG,
2208 "Finishing failed broadcast to " + data.intent.getComponent());
2209 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 throw new RuntimeException(
2211 "Unable to instantiate receiver " + component
2212 + ": " + e.toString(), e);
2213 }
2214
2215 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002216 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002217
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002218 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 TAG, "Performing receive of " + data.intent
2220 + ": app=" + app
2221 + ", appName=" + app.getPackageName()
2222 + ", pkg=" + packageInfo.getPackageName()
2223 + ", comp=" + data.intent.getComponent().toShortString()
2224 + ", dir=" + packageInfo.getAppDir());
2225
Dianne Hackborn21556372010-02-04 16:34:40 -08002226 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002227 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002228 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 receiver.onReceive(context.getReceiverRestrictedContext(),
2230 data.intent);
2231 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002232 if (DEBUG_BROADCAST) Slog.i(TAG,
2233 "Finishing failed broadcast to " + data.intent.getComponent());
2234 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 if (!mInstrumentation.onException(receiver, e)) {
2236 throw new RuntimeException(
2237 "Unable to start receiver " + component
2238 + ": " + e.toString(), e);
2239 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002240 } finally {
2241 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 }
2243
Dianne Hackborne829fef2010-10-26 17:44:01 -07002244 if (receiver.getPendingResult() != null) {
2245 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 }
2247 }
2248
Christopher Tate181fafa2009-05-14 11:12:14 -07002249 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002250 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002251 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002252
2253 // no longer idle; we have backup work to do
2254 unscheduleGcIdler();
2255
2256 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002257 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002258 String packageName = packageInfo.mPackageName;
2259 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002260 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002261 + " already exists");
2262 return;
2263 }
Bob Leee5408332009-09-04 18:31:17 -07002264
Christopher Tate181fafa2009-05-14 11:12:14 -07002265 BackupAgent agent = null;
2266 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002267
Christopher Tate79ec80d2011-06-24 14:58:49 -07002268 // full backup operation but no app-supplied agent? use the default implementation
2269 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2270 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002271 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002272 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002273
Christopher Tate181fafa2009-05-14 11:12:14 -07002274 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002275 IBinder binder = null;
2276 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002277 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2278
Christopher Tated1475e02009-07-09 15:36:17 -07002279 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002280 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002281
2282 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002283 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002284 context.init(packageInfo, null, this);
2285 context.setOuterContext(agent);
2286 agent.attach(context);
2287
2288 agent.onCreate();
2289 binder = agent.onBind();
2290 mBackupAgents.put(packageName, agent);
2291 } catch (Exception e) {
2292 // If this is during restore, fail silently; otherwise go
2293 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002294 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002295 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2296 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002297 throw e;
2298 }
2299 // falling through with 'binder' still null
2300 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002301
2302 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002303 try {
2304 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2305 } catch (RemoteException e) {
2306 // nothing to do.
2307 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002308 } catch (Exception e) {
2309 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002310 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002311 }
2312 }
2313
2314 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002315 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002316 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002317
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002318 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002319 String packageName = packageInfo.mPackageName;
2320 BackupAgent agent = mBackupAgents.get(packageName);
2321 if (agent != null) {
2322 try {
2323 agent.onDestroy();
2324 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002325 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002326 e.printStackTrace();
2327 }
2328 mBackupAgents.remove(packageName);
2329 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002330 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002331 }
2332 }
2333
Romain Guy65b345f2011-07-27 18:51:50 -07002334 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 // If we are getting ready to gc after going to the background, well
2336 // we are back active so skip it.
2337 unscheduleGcIdler();
2338
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002339 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002340 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 Service service = null;
2342 try {
2343 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2344 service = (Service) cl.loadClass(data.info.name).newInstance();
2345 } catch (Exception e) {
2346 if (!mInstrumentation.onException(service, e)) {
2347 throw new RuntimeException(
2348 "Unable to instantiate service " + data.info.name
2349 + ": " + e.toString(), e);
2350 }
2351 }
2352
2353 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002354 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355
Dianne Hackborn21556372010-02-04 16:34:40 -08002356 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 context.init(packageInfo, null, this);
2358
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002359 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 context.setOuterContext(service);
2361 service.attach(context, this, data.info.name, data.token, app,
2362 ActivityManagerNative.getDefault());
2363 service.onCreate();
2364 mServices.put(data.token, service);
2365 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002366 ActivityManagerNative.getDefault().serviceDoneExecuting(
2367 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 } catch (RemoteException e) {
2369 // nothing to do.
2370 }
2371 } catch (Exception e) {
2372 if (!mInstrumentation.onException(service, e)) {
2373 throw new RuntimeException(
2374 "Unable to create service " + data.info.name
2375 + ": " + e.toString(), e);
2376 }
2377 }
2378 }
2379
Romain Guy65b345f2011-07-27 18:51:50 -07002380 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002382 if (DEBUG_SERVICE)
2383 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 if (s != null) {
2385 try {
2386 data.intent.setExtrasClassLoader(s.getClassLoader());
2387 try {
2388 if (!data.rebind) {
2389 IBinder binder = s.onBind(data.intent);
2390 ActivityManagerNative.getDefault().publishService(
2391 data.token, data.intent, binder);
2392 } else {
2393 s.onRebind(data.intent);
2394 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002395 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002397 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 } catch (RemoteException ex) {
2399 }
2400 } catch (Exception e) {
2401 if (!mInstrumentation.onException(s, e)) {
2402 throw new RuntimeException(
2403 "Unable to bind to service " + s
2404 + " with " + data.intent + ": " + e.toString(), e);
2405 }
2406 }
2407 }
2408 }
2409
Romain Guy65b345f2011-07-27 18:51:50 -07002410 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 Service s = mServices.get(data.token);
2412 if (s != null) {
2413 try {
2414 data.intent.setExtrasClassLoader(s.getClassLoader());
2415 boolean doRebind = s.onUnbind(data.intent);
2416 try {
2417 if (doRebind) {
2418 ActivityManagerNative.getDefault().unbindFinished(
2419 data.token, data.intent, doRebind);
2420 } else {
2421 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002422 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 }
2424 } catch (RemoteException ex) {
2425 }
2426 } catch (Exception e) {
2427 if (!mInstrumentation.onException(s, e)) {
2428 throw new RuntimeException(
2429 "Unable to unbind to service " + s
2430 + " with " + data.intent + ": " + e.toString(), e);
2431 }
2432 }
2433 }
2434 }
2435
Dianne Hackborn625ac272010-09-17 18:29:22 -07002436 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002437 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2438 try {
2439 Service s = mServices.get(info.token);
2440 if (s != null) {
2441 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2442 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2443 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002445 } finally {
2446 IoUtils.closeQuietly(info.fd);
2447 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 }
2449 }
2450
Dianne Hackborn625ac272010-09-17 18:29:22 -07002451 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002452 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2453 try {
2454 ActivityClientRecord r = mActivities.get(info.token);
2455 if (r != null && r.activity != null) {
2456 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2457 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2458 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002459 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002460 } finally {
2461 IoUtils.closeQuietly(info.fd);
2462 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002463 }
2464 }
2465
Marco Nelissen18cb2872011-11-15 11:19:53 -08002466 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002467 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2468 try {
2469 ProviderClientRecord r = mLocalProviders.get(info.token);
2470 if (r != null && r.mLocalProvider != null) {
2471 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2472 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2473 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002474 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002475 } finally {
2476 IoUtils.closeQuietly(info.fd);
2477 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002478 }
2479 }
2480
Romain Guy65b345f2011-07-27 18:51:50 -07002481 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 Service s = mServices.get(data.token);
2483 if (s != null) {
2484 try {
2485 if (data.args != null) {
2486 data.args.setExtrasClassLoader(s.getClassLoader());
2487 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002488 int res;
2489 if (!data.taskRemoved) {
2490 res = s.onStartCommand(data.args, data.flags, data.startId);
2491 } else {
2492 s.onTaskRemoved(data.args);
2493 res = Service.START_TASK_REMOVED_COMPLETE;
2494 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002495
2496 QueuedWork.waitToFinish();
2497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002499 ActivityManagerNative.getDefault().serviceDoneExecuting(
2500 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 } catch (RemoteException e) {
2502 // nothing to do.
2503 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002504 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 } catch (Exception e) {
2506 if (!mInstrumentation.onException(s, e)) {
2507 throw new RuntimeException(
2508 "Unable to start service " + s
2509 + " with " + data.args + ": " + e.toString(), e);
2510 }
2511 }
2512 }
2513 }
2514
Romain Guy65b345f2011-07-27 18:51:50 -07002515 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 Service s = mServices.remove(token);
2517 if (s != null) {
2518 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002519 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 s.onDestroy();
2521 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002522 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002524 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002526
2527 QueuedWork.waitToFinish();
2528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002530 ActivityManagerNative.getDefault().serviceDoneExecuting(
2531 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 } catch (RemoteException e) {
2533 // nothing to do.
2534 }
2535 } catch (Exception e) {
2536 if (!mInstrumentation.onException(s, e)) {
2537 throw new RuntimeException(
2538 "Unable to stop service " + s
2539 + ": " + e.toString(), e);
2540 }
2541 }
2542 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002543 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 }
2545
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002546 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002548 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002549 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 + " finished=" + r.activity.mFinished);
2551 if (r != null && !r.activity.mFinished) {
2552 if (clearHide) {
2553 r.hideForNow = false;
2554 r.activity.mStartedActivity = false;
2555 }
2556 try {
2557 if (r.pendingIntents != null) {
2558 deliverNewIntents(r, r.pendingIntents);
2559 r.pendingIntents = null;
2560 }
2561 if (r.pendingResults != null) {
2562 deliverResults(r, r.pendingResults);
2563 r.pendingResults = null;
2564 }
2565 r.activity.performResume();
2566
Bob Leee5408332009-09-04 18:31:17 -07002567 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 r.paused = false;
2571 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 r.state = null;
2573 } catch (Exception e) {
2574 if (!mInstrumentation.onException(r.activity, e)) {
2575 throw new RuntimeException(
2576 "Unable to resume activity "
2577 + r.intent.getComponent().toShortString()
2578 + ": " + e.toString(), e);
2579 }
2580 }
2581 }
2582 return r;
2583 }
2584
Romain Guya998dff2012-03-23 18:58:36 -07002585 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002586 if (r.mPendingRemoveWindow != null) {
2587 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2588 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2589 if (wtoken != null) {
2590 WindowManagerImpl.getDefault().closeAll(wtoken,
2591 r.activity.getClass().getName(), "Activity");
2592 }
2593 }
2594 r.mPendingRemoveWindow = null;
2595 r.mPendingRemoveWindowManager = null;
2596 }
2597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2599 // If we are getting ready to gc after going to the background, well
2600 // we are back active so skip it.
2601 unscheduleGcIdler();
2602
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002603 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604
2605 if (r != null) {
2606 final Activity a = r.activity;
2607
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002608 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 TAG, "Resume " + r + " started activity: " +
2610 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2611 + ", finished: " + a.mFinished);
2612
2613 final int forwardBit = isForward ?
2614 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 // If the window hasn't yet been added to the window manager,
2617 // and this guy didn't finish itself or start another activity,
2618 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002619 boolean willBeVisible = !a.mStartedActivity;
2620 if (!willBeVisible) {
2621 try {
2622 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2623 a.getActivityToken());
2624 } catch (RemoteException e) {
2625 }
2626 }
2627 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 r.window = r.activity.getWindow();
2629 View decor = r.window.getDecorView();
2630 decor.setVisibility(View.INVISIBLE);
2631 ViewManager wm = a.getWindowManager();
2632 WindowManager.LayoutParams l = r.window.getAttributes();
2633 a.mDecor = decor;
2634 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2635 l.softInputMode |= forwardBit;
2636 if (a.mVisibleFromClient) {
2637 a.mWindowAdded = true;
2638 wm.addView(decor, l);
2639 }
2640
2641 // If the window has already been added, but during resume
2642 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002643 // window visible.
2644 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002645 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 TAG, "Launch " + r + " mStartedActivity set");
2647 r.hideForNow = true;
2648 }
2649
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002650 // Get rid of anything left hanging around.
2651 cleanUpPendingRemoveWindows(r);
2652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 // The window is now visible if it has been added, we are not
2654 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002655 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002656 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002658 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002659 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07002661 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 r.newConfig = null;
2663 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002664 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 + isForward);
2666 WindowManager.LayoutParams l = r.window.getAttributes();
2667 if ((l.softInputMode
2668 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2669 != forwardBit) {
2670 l.softInputMode = (l.softInputMode
2671 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2672 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002673 if (r.activity.mVisibleFromClient) {
2674 ViewManager wm = a.getWindowManager();
2675 View decor = r.window.getDecorView();
2676 wm.updateViewLayout(decor, l);
2677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 }
2679 r.activity.mVisibleFromServer = true;
2680 mNumVisibleActivities++;
2681 if (r.activity.mVisibleFromClient) {
2682 r.activity.makeVisible();
2683 }
2684 }
2685
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002686 if (!r.onlyLocalRequest) {
2687 r.nextIdle = mNewActivities;
2688 mNewActivities = r;
2689 if (localLOGV) Slog.v(
2690 TAG, "Scheduling idle handler for " + r);
2691 Looper.myQueue().addIdleHandler(new Idler());
2692 }
2693 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694
2695 } else {
2696 // If an exception was thrown when trying to resume, then
2697 // just end this activity.
2698 try {
2699 ActivityManagerNative.getDefault()
2700 .finishActivity(token, Activity.RESULT_CANCELED, null);
2701 } catch (RemoteException ex) {
2702 }
2703 }
2704 }
2705
2706 private int mThumbnailWidth = -1;
2707 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002708 private Bitmap mAvailThumbnailBitmap = null;
2709 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710
Romain Guy65b345f2011-07-27 18:51:50 -07002711 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002712 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002714 if (thumbnail == null) {
2715 int w = mThumbnailWidth;
2716 int h;
2717 if (w < 0) {
2718 Resources res = r.activity.getResources();
2719 mThumbnailHeight = h =
2720 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002722 mThumbnailWidth = w =
2723 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2724 } else {
2725 h = mThumbnailHeight;
2726 }
2727
2728 // On platforms where we don't want thumbnails, set dims to (0,0)
2729 if ((w > 0) && (h > 0)) {
2730 thumbnail = Bitmap.createBitmap(w, h, THUMBNAIL_FORMAT);
2731 thumbnail.eraseColor(0);
2732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 }
2734
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002735 if (thumbnail != null) {
2736 Canvas cv = mThumbnailCanvas;
2737 if (cv == null) {
2738 mThumbnailCanvas = cv = new Canvas();
2739 }
2740
2741 cv.setBitmap(thumbnail);
2742 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2743 mAvailThumbnailBitmap = thumbnail;
2744 thumbnail = null;
2745 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002746 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 } catch (Exception e) {
2750 if (!mInstrumentation.onException(r.activity, e)) {
2751 throw new RuntimeException(
2752 "Unable to create thumbnail of "
2753 + r.intent.getComponent().toShortString()
2754 + ": " + e.toString(), e);
2755 }
2756 thumbnail = null;
2757 }
2758
2759 return thumbnail;
2760 }
2761
Romain Guy65b345f2011-07-27 18:51:50 -07002762 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002764 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002766 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 if (userLeaving) {
2768 performUserLeavingActivity(r);
2769 }
Bob Leee5408332009-09-04 18:31:17 -07002770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002772 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002774 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002775 if (r.isPreHoneycomb()) {
2776 QueuedWork.waitToFinish();
2777 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 // Tell the activity manager we have paused.
2780 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002781 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 } catch (RemoteException ex) {
2783 }
2784 }
2785 }
2786
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002787 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 mInstrumentation.callActivityOnUserLeaving(r.activity);
2789 }
2790
2791 final Bundle performPauseActivity(IBinder token, boolean finished,
2792 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002793 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 return r != null ? performPauseActivity(r, finished, saveState) : null;
2795 }
2796
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002797 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 boolean saveState) {
2799 if (r.paused) {
2800 if (r.activity.mFinished) {
2801 // If we are finishing, we won't call onResume() in certain cases.
2802 // So here we likewise don't want to call onPause() if the activity
2803 // isn't resumed.
2804 return null;
2805 }
2806 RuntimeException e = new RuntimeException(
2807 "Performing pause of activity that is not resumed: "
2808 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002809 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 }
2811 Bundle state = null;
2812 if (finished) {
2813 r.activity.mFinished = true;
2814 }
2815 try {
2816 // Next have the activity save its current state and managed dialogs...
2817 if (!r.activity.mFinished && saveState) {
2818 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002819 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2821 r.state = state;
2822 }
2823 // Now we are idle.
2824 r.activity.mCalled = false;
2825 mInstrumentation.callActivityOnPause(r.activity);
2826 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2827 if (!r.activity.mCalled) {
2828 throw new SuperNotCalledException(
2829 "Activity " + r.intent.getComponent().toShortString() +
2830 " did not call through to super.onPause()");
2831 }
2832
2833 } catch (SuperNotCalledException e) {
2834 throw e;
2835
2836 } catch (Exception e) {
2837 if (!mInstrumentation.onException(r.activity, e)) {
2838 throw new RuntimeException(
2839 "Unable to pause activity "
2840 + r.intent.getComponent().toShortString()
2841 + ": " + e.toString(), e);
2842 }
2843 }
2844 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002845
2846 // Notify any outstanding on paused listeners
2847 ArrayList<OnActivityPausedListener> listeners;
2848 synchronized (mOnPauseListeners) {
2849 listeners = mOnPauseListeners.remove(r.activity);
2850 }
2851 int size = (listeners != null ? listeners.size() : 0);
2852 for (int i = 0; i < size; i++) {
2853 listeners.get(i).onPaused(r.activity);
2854 }
2855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 return state;
2857 }
2858
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002859 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002860 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002861 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 }
2863
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002864 private static class StopInfo implements Runnable {
2865 ActivityClientRecord activity;
2866 Bundle state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 Bitmap thumbnail;
2868 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002869
2870 @Override public void run() {
2871 // Tell activity manager we have been stopped.
2872 try {
2873 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
2874 ActivityManagerNative.getDefault().activityStopped(
2875 activity.token, state, thumbnail, description);
2876 } catch (RemoteException ex) {
2877 }
2878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 }
2880
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002881 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002882 public final IActivityManager.ContentProviderHolder holder;
2883 public final ProviderClientRecord client;
2884 public int stableCount;
2885 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002886
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002887 // When this is set, the stable and unstable ref counts are 0 and
2888 // we have a pending operation scheduled to remove the ref count
2889 // from the activity manager. On the activity manager we are still
2890 // holding an unstable ref, though it is not reflected in the counts
2891 // here.
2892 public boolean removePending;
2893
2894 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
2895 ProviderClientRecord inClient, int sCount, int uCount) {
2896 holder = inHolder;
2897 client = inClient;
2898 stableCount = sCount;
2899 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 }
2901 }
2902
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002903 /**
2904 * Core implementation of stopping an activity. Note this is a little
2905 * tricky because the server's meaning of stop is slightly different
2906 * than our client -- for the server, stop means to save state and give
2907 * it the result when it is done, but the window may still be visible.
2908 * For the client, we want to call onStop()/onStart() to indicate when
2909 * the activity's UI visibillity changes.
2910 */
Romain Guy65b345f2011-07-27 18:51:50 -07002911 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002912 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002913 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002914 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 if (r != null) {
2916 if (!keepShown && r.stopped) {
2917 if (r.activity.mFinished) {
2918 // If we are finishing, we won't call onResume() in certain
2919 // cases. So here we likewise don't want to call onStop()
2920 // if the activity isn't resumed.
2921 return;
2922 }
2923 RuntimeException e = new RuntimeException(
2924 "Performing stop of activity that is not resumed: "
2925 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002926 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 }
2928
2929 if (info != null) {
2930 try {
2931 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002932 // For now, don't create the thumbnail here; we are
2933 // doing that by doing a screen snapshot.
2934 info.thumbnail = null; //createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 info.description = r.activity.onCreateDescription();
2936 } catch (Exception e) {
2937 if (!mInstrumentation.onException(r.activity, e)) {
2938 throw new RuntimeException(
2939 "Unable to save state of activity "
2940 + r.intent.getComponent().toShortString()
2941 + ": " + e.toString(), e);
2942 }
2943 }
2944 }
2945
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002946 // Next have the activity save its current state and managed dialogs...
2947 if (!r.activity.mFinished && saveState) {
2948 if (r.state == null) {
2949 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002950 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002951 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2952 r.state = state;
2953 } else {
2954 state = r.state;
2955 }
2956 }
2957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 if (!keepShown) {
2959 try {
2960 // Now we are idle.
2961 r.activity.performStop();
2962 } catch (Exception e) {
2963 if (!mInstrumentation.onException(r.activity, e)) {
2964 throw new RuntimeException(
2965 "Unable to stop activity "
2966 + r.intent.getComponent().toShortString()
2967 + ": " + e.toString(), e);
2968 }
2969 }
2970 r.stopped = true;
2971 }
2972
2973 r.paused = true;
2974 }
2975 }
2976
Romain Guy65b345f2011-07-27 18:51:50 -07002977 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 View v = r.activity.mDecor;
2979 if (v != null) {
2980 if (show) {
2981 if (!r.activity.mVisibleFromServer) {
2982 r.activity.mVisibleFromServer = true;
2983 mNumVisibleActivities++;
2984 if (r.activity.mVisibleFromClient) {
2985 r.activity.makeVisible();
2986 }
2987 }
2988 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002989 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002990 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07002992 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 r.newConfig = null;
2994 }
2995 } else {
2996 if (r.activity.mVisibleFromServer) {
2997 r.activity.mVisibleFromServer = false;
2998 mNumVisibleActivities--;
2999 v.setVisibility(View.INVISIBLE);
3000 }
3001 }
3002 }
3003 }
3004
Romain Guy65b345f2011-07-27 18:51:50 -07003005 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003006 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 r.activity.mConfigChangeFlags |= configChanges;
3008
3009 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003010 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003012 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 TAG, "Finishing stop of " + r + ": show=" + show
3014 + " win=" + r.window);
3015
3016 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003017
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003018 // Make sure any pending writes are now committed.
3019 if (!r.isPreHoneycomb()) {
3020 QueuedWork.waitToFinish();
3021 }
3022
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003023 // Schedule the call to tell the activity manager we have
3024 // stopped. We don't do this immediately, because we want to
3025 // have a chance for any other pending work (in particular memory
3026 // trim requests) to complete before you tell the activity
3027 // manager to proceed and allow us to go fully into the background.
3028 info.activity = r;
3029 info.state = r.state;
3030 mH.post(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 }
3032
3033 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003034 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 if (r.stopped) {
3036 r.activity.performRestart();
3037 r.stopped = false;
3038 }
3039 }
3040
Romain Guy65b345f2011-07-27 18:51:50 -07003041 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003042 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003043
3044 if (r == null) {
3045 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3046 return;
3047 }
3048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003050 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 } else if (show && r.stopped) {
3052 // If we are getting ready to gc after going to the background, well
3053 // we are back active so skip it.
3054 unscheduleGcIdler();
3055
3056 r.activity.performRestart();
3057 r.stopped = false;
3058 }
3059 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003060 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 TAG, "Handle window " + r + " visibility: " + show);
3062 updateVisibility(r, show);
3063 }
3064 }
3065
Romain Guy65b345f2011-07-27 18:51:50 -07003066 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003067 ActivityClientRecord r = mActivities.get(token);
3068
3069 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003070 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003071 return;
3072 }
3073
3074 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003075 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003076 try {
3077 // Now we are idle.
3078 r.activity.performStop();
3079 } catch (Exception e) {
3080 if (!mInstrumentation.onException(r.activity, e)) {
3081 throw new RuntimeException(
3082 "Unable to stop activity "
3083 + r.intent.getComponent().toShortString()
3084 + ": " + e.toString(), e);
3085 }
3086 }
3087 r.stopped = true;
3088 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003089
3090 // Make sure any pending writes are now committed.
3091 if (!r.isPreHoneycomb()) {
3092 QueuedWork.waitToFinish();
3093 }
3094
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003095 // Tell activity manager we slept.
3096 try {
3097 ActivityManagerNative.getDefault().activitySlept(r.token);
3098 } catch (RemoteException ex) {
3099 }
3100 } else {
3101 if (r.stopped && r.activity.mVisibleFromServer) {
3102 r.activity.performRestart();
3103 r.stopped = false;
3104 }
3105 }
3106 }
3107
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003108 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003109 synchronized (mPackages) {
3110 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003111 }
3112 }
3113
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003114 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3115 LoadedApk apk = peekPackageInfo(data.pkg, false);
3116 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003117 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003118 }
3119 apk = peekPackageInfo(data.pkg, true);
3120 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003121 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003122 }
3123 handleConfigurationChanged(mConfiguration, data.info);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003124 WindowManagerImpl.getDefault().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003125 }
3126
Romain Guy65b345f2011-07-27 18:51:50 -07003127 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003128 final int N = results.size();
3129 for (int i=0; i<N; i++) {
3130 ResultInfo ri = results.get(i);
3131 try {
3132 if (ri.mData != null) {
3133 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
3134 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003135 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003136 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 r.activity.dispatchActivityResult(ri.mResultWho,
3138 ri.mRequestCode, ri.mResultCode, ri.mData);
3139 } catch (Exception e) {
3140 if (!mInstrumentation.onException(r.activity, e)) {
3141 throw new RuntimeException(
3142 "Failure delivering result " + ri + " to activity "
3143 + r.intent.getComponent().toShortString()
3144 + ": " + e.toString(), e);
3145 }
3146 }
3147 }
3148 }
3149
Romain Guy65b345f2011-07-27 18:51:50 -07003150 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003151 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003152 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 if (r != null) {
3154 final boolean resumed = !r.paused;
3155 if (!r.activity.mFinished && r.activity.mDecor != null
3156 && r.hideForNow && resumed) {
3157 // We had hidden the activity because it started another
3158 // one... we have gotten a result back and we are not
3159 // paused, so make sure our window is visible.
3160 updateVisibility(r, true);
3161 }
3162 if (resumed) {
3163 try {
3164 // Now we are idle.
3165 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003166 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 mInstrumentation.callActivityOnPause(r.activity);
3168 if (!r.activity.mCalled) {
3169 throw new SuperNotCalledException(
3170 "Activity " + r.intent.getComponent().toShortString()
3171 + " did not call through to super.onPause()");
3172 }
3173 } catch (SuperNotCalledException e) {
3174 throw e;
3175 } catch (Exception e) {
3176 if (!mInstrumentation.onException(r.activity, e)) {
3177 throw new RuntimeException(
3178 "Unable to pause activity "
3179 + r.intent.getComponent().toShortString()
3180 + ": " + e.toString(), e);
3181 }
3182 }
3183 }
3184 deliverResults(r, res.results);
3185 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003186 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003187 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 }
3189 }
3190 }
3191
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003192 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 return performDestroyActivity(token, finishing, 0, false);
3194 }
3195
Romain Guy65b345f2011-07-27 18:51:50 -07003196 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003198 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003199 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003200 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003202 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 r.activity.mConfigChangeFlags |= configChanges;
3204 if (finishing) {
3205 r.activity.mFinished = true;
3206 }
3207 if (!r.paused) {
3208 try {
3209 r.activity.mCalled = false;
3210 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07003211 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003212 r.activity.getComponentName().getClassName());
3213 if (!r.activity.mCalled) {
3214 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003215 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 + " did not call through to super.onPause()");
3217 }
3218 } catch (SuperNotCalledException e) {
3219 throw e;
3220 } catch (Exception e) {
3221 if (!mInstrumentation.onException(r.activity, e)) {
3222 throw new RuntimeException(
3223 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003224 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 + ": " + e.toString(), e);
3226 }
3227 }
3228 r.paused = true;
3229 }
3230 if (!r.stopped) {
3231 try {
3232 r.activity.performStop();
3233 } catch (SuperNotCalledException e) {
3234 throw e;
3235 } catch (Exception e) {
3236 if (!mInstrumentation.onException(r.activity, e)) {
3237 throw new RuntimeException(
3238 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003239 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 + ": " + e.toString(), e);
3241 }
3242 }
3243 r.stopped = true;
3244 }
3245 if (getNonConfigInstance) {
3246 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003247 r.lastNonConfigurationInstances
3248 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 } catch (Exception e) {
3250 if (!mInstrumentation.onException(r.activity, e)) {
3251 throw new RuntimeException(
3252 "Unable to retain activity "
3253 + r.intent.getComponent().toShortString()
3254 + ": " + e.toString(), e);
3255 }
3256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 }
3258 try {
3259 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003260 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 if (!r.activity.mCalled) {
3262 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003263 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 " did not call through to super.onDestroy()");
3265 }
3266 if (r.window != null) {
3267 r.window.closeAllPanels();
3268 }
3269 } catch (SuperNotCalledException e) {
3270 throw e;
3271 } catch (Exception e) {
3272 if (!mInstrumentation.onException(r.activity, e)) {
3273 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003274 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3275 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 }
3277 }
3278 }
3279 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003280 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 return r;
3282 }
3283
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003284 private static String safeToComponentShortString(Intent intent) {
3285 ComponentName component = intent.getComponent();
3286 return component == null ? "[Unknown]" : component.toShortString();
3287 }
3288
Romain Guy65b345f2011-07-27 18:51:50 -07003289 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003291 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 configChanges, getNonConfigInstance);
3293 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003294 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 WindowManager wm = r.activity.getWindowManager();
3296 View v = r.activity.mDecor;
3297 if (v != null) {
3298 if (r.activity.mVisibleFromServer) {
3299 mNumVisibleActivities--;
3300 }
3301 IBinder wtoken = v.getWindowToken();
3302 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003303 if (r.onlyLocalRequest) {
3304 // Hold off on removing this until the new activity's
3305 // window is being added.
3306 r.mPendingRemoveWindow = v;
3307 r.mPendingRemoveWindowManager = wm;
3308 } else {
3309 wm.removeViewImmediate(v);
3310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003312 if (wtoken != null && r.mPendingRemoveWindow == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 WindowManagerImpl.getDefault().closeAll(wtoken,
3314 r.activity.getClass().getName(), "Activity");
3315 }
3316 r.activity.mDecor = null;
3317 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003318 if (r.mPendingRemoveWindow == null) {
3319 // If we are delaying the removal of the activity window, then
3320 // we can't clean up all windows here. Note that we can't do
3321 // so later either, which means any windows that aren't closed
3322 // by the app will leak. Well we try to warning them a lot
3323 // about leaking windows, because that is a bug, so if they are
3324 // using this recreate facility then they get to live with leaks.
3325 WindowManagerImpl.getDefault().closeAll(token,
3326 r.activity.getClass().getName(), "Activity");
3327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328
3329 // Mocked out contexts won't be participating in the normal
3330 // process lifecycle, but if we're running with a proper
3331 // ApplicationContext we need to have it tear down things
3332 // cleanly.
3333 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003334 if (c instanceof ContextImpl) {
3335 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 r.activity.getClass().getName(), "Activity");
3337 }
3338 }
3339 if (finishing) {
3340 try {
3341 ActivityManagerNative.getDefault().activityDestroyed(token);
3342 } catch (RemoteException ex) {
3343 // If the system process has died, it's game over for everyone.
3344 }
3345 }
3346 }
3347
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003348 public final void requestRelaunchActivity(IBinder token,
3349 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3350 int configChanges, boolean notResumed, Configuration config,
3351 boolean fromServer) {
3352 ActivityClientRecord target = null;
3353
3354 synchronized (mPackages) {
3355 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3356 ActivityClientRecord r = mRelaunchingActivities.get(i);
3357 if (r.token == token) {
3358 target = r;
3359 if (pendingResults != null) {
3360 if (r.pendingResults != null) {
3361 r.pendingResults.addAll(pendingResults);
3362 } else {
3363 r.pendingResults = pendingResults;
3364 }
3365 }
3366 if (pendingNewIntents != null) {
3367 if (r.pendingIntents != null) {
3368 r.pendingIntents.addAll(pendingNewIntents);
3369 } else {
3370 r.pendingIntents = pendingNewIntents;
3371 }
3372 }
3373 break;
3374 }
3375 }
3376
3377 if (target == null) {
3378 target = new ActivityClientRecord();
3379 target.token = token;
3380 target.pendingResults = pendingResults;
3381 target.pendingIntents = pendingNewIntents;
3382 if (!fromServer) {
3383 ActivityClientRecord existing = mActivities.get(token);
3384 if (existing != null) {
3385 target.startsNotResumed = existing.paused;
3386 }
3387 target.onlyLocalRequest = true;
3388 }
3389 mRelaunchingActivities.add(target);
3390 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3391 }
3392
3393 if (fromServer) {
3394 target.startsNotResumed = notResumed;
3395 target.onlyLocalRequest = false;
3396 }
3397 if (config != null) {
3398 target.createdConfig = config;
3399 }
3400 target.pendingConfigChanges |= configChanges;
3401 }
3402 }
3403
Romain Guy65b345f2011-07-27 18:51:50 -07003404 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405 // If we are getting ready to gc after going to the background, well
3406 // we are back active so skip it.
3407 unscheduleGcIdler();
3408
3409 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003410 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412 // First: make sure we have the most recent configuration and most
3413 // recent version of the activity, or skip it if some previous call
3414 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003415 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 int N = mRelaunchingActivities.size();
3417 IBinder token = tmp.token;
3418 tmp = null;
3419 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003420 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 if (r.token == token) {
3422 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003423 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 mRelaunchingActivities.remove(i);
3425 i--;
3426 N--;
3427 }
3428 }
Bob Leee5408332009-09-04 18:31:17 -07003429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003431 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 return;
3433 }
Bob Leee5408332009-09-04 18:31:17 -07003434
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003435 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3436 + tmp.token + " with configChanges=0x"
3437 + Integer.toHexString(configChanges));
3438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 if (mPendingConfiguration != null) {
3440 changedConfig = mPendingConfiguration;
3441 mPendingConfiguration = null;
3442 }
3443 }
Bob Leee5408332009-09-04 18:31:17 -07003444
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003445 if (tmp.createdConfig != null) {
3446 // If the activity manager is passing us its current config,
3447 // assume that is really what we want regardless of what we
3448 // may have pending.
3449 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003450 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3451 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3452 if (changedConfig == null
3453 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3454 changedConfig = tmp.createdConfig;
3455 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003456 }
3457 }
3458
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003459 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003460 + tmp.token + ": changedConfig=" + changedConfig);
3461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 // If there was a pending configuration change, execute it first.
3463 if (changedConfig != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003464 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 }
Bob Leee5408332009-09-04 18:31:17 -07003466
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003467 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003468 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003469 if (r == null) {
3470 return;
3471 }
Bob Leee5408332009-09-04 18:31:17 -07003472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003474 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003475 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003476
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003477 r.activity.mChangingConfigurations = true;
3478
Dianne Hackborne2b04802010-12-09 09:24:55 -08003479 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003480 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003481 performPauseActivity(r.token, false, r.isPreHoneycomb());
3482 }
3483 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3484 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003485 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003486 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 }
Bob Leee5408332009-09-04 18:31:17 -07003488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 r.activity = null;
3492 r.window = null;
3493 r.hideForNow = false;
3494 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003495 // Merge any pending results and pending intents; don't just replace them
3496 if (tmp.pendingResults != null) {
3497 if (r.pendingResults == null) {
3498 r.pendingResults = tmp.pendingResults;
3499 } else {
3500 r.pendingResults.addAll(tmp.pendingResults);
3501 }
3502 }
3503 if (tmp.pendingIntents != null) {
3504 if (r.pendingIntents == null) {
3505 r.pendingIntents = tmp.pendingIntents;
3506 } else {
3507 r.pendingIntents.addAll(tmp.pendingIntents);
3508 }
3509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003510 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003511
Christopher Tateb70f3df2009-04-07 16:07:59 -07003512 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 }
3514
Romain Guy65b345f2011-07-27 18:51:50 -07003515 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003516 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 Bitmap thumbnail = createThumbnailBitmap(r);
3518 CharSequence description = null;
3519 try {
3520 description = r.activity.onCreateDescription();
3521 } catch (Exception e) {
3522 if (!mInstrumentation.onException(r.activity, e)) {
3523 throw new RuntimeException(
3524 "Unable to create description of activity "
3525 + r.intent.getComponent().toShortString()
3526 + ": " + e.toString(), e);
3527 }
3528 }
3529 //System.out.println("Reporting top thumbnail " + thumbnail);
3530 try {
3531 ActivityManagerNative.getDefault().reportThumbnail(
3532 token, thumbnail, description);
3533 } catch (RemoteException ex) {
3534 }
3535 }
3536
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003537 ArrayList<ComponentCallbacks2> collectComponentCallbacksLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003539 ArrayList<ComponentCallbacks2> callbacks
3540 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 if (mActivities.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003543 for (ActivityClientRecord ar : mActivities.values()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 Activity a = ar.activity;
3545 if (a != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003546 Configuration thisConfig = applyConfigCompatMainThread(newConfig,
3547 ar.packageInfo.mCompatibilityInfo.getIfNeeded());
Romain Guya998dff2012-03-23 18:58:36 -07003548 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 // If the activity is currently resumed, its configuration
3550 // needs to change right now.
3551 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003552 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 // Otherwise, we will tell it about the change
3554 // the next time it is resumed or shown. Note that
3555 // the activity manager may, before then, decide the
3556 // activity needs to be destroyed to handle its new
3557 // configuration.
Romain Guya998dff2012-03-23 18:58:36 -07003558 if (DEBUG_CONFIGURATION) {
3559 Slog.v(TAG, "Setting activity "
3560 + ar.activityInfo.name + " newConfig=" + thisConfig);
3561 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003562 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 }
3564 }
3565 }
3566 }
3567 if (mServices.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003568 for (Service service : mServices.values()) {
3569 callbacks.add(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 }
3571 }
3572 synchronized (mProviderMap) {
3573 if (mLocalProviders.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003574 for (ProviderClientRecord providerClientRecord : mLocalProviders.values()) {
3575 callbacks.add(providerClientRecord.mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003576 }
3577 }
3578 }
3579 final int N = mAllApplications.size();
3580 for (int i=0; i<N; i++) {
3581 callbacks.add(mAllApplications.get(i));
3582 }
Bob Leee5408332009-09-04 18:31:17 -07003583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 return callbacks;
3585 }
Bob Leee5408332009-09-04 18:31:17 -07003586
Romain Guya998dff2012-03-23 18:58:36 -07003587 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003589 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003590 // we check the runtime type and act accordingly.
3591 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3592 if (activity != null) {
3593 activity.mCalled = false;
3594 }
Bob Leee5408332009-09-04 18:31:17 -07003595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003596 boolean shouldChangeConfig = false;
3597 if ((activity == null) || (activity.mCurrentConfig == null)) {
3598 shouldChangeConfig = true;
3599 } else {
Bob Leee5408332009-09-04 18:31:17 -07003600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003601 // If the new config is the same as the config this Activity
3602 // is already running with then don't bother calling
3603 // onConfigurationChanged
3604 int diff = activity.mCurrentConfig.diff(config);
3605 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003606 // If this activity doesn't handle any of the config changes
3607 // then don't bother calling onConfigurationChanged as we're
3608 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003609 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003610 shouldChangeConfig = true;
3611 }
3612 }
3613 }
Bob Leee5408332009-09-04 18:31:17 -07003614
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003615 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003616 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003617 if (shouldChangeConfig) {
3618 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003620 if (activity != null) {
3621 if (!activity.mCalled) {
3622 throw new SuperNotCalledException(
3623 "Activity " + activity.getLocalClassName() +
3624 " did not call through to super.onConfigurationChanged()");
3625 }
3626 activity.mConfigChangeFlags = 0;
3627 activity.mCurrentConfig = new Configuration(config);
3628 }
3629 }
3630 }
3631
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003632 public final void applyConfigurationToResources(Configuration config) {
3633 synchronized (mPackages) {
3634 applyConfigurationToResourcesLocked(config, null);
3635 }
3636 }
3637
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003638 final boolean applyConfigurationToResourcesLocked(Configuration config,
3639 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003640 if (mResConfiguration == null) {
3641 mResConfiguration = new Configuration();
3642 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003643 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003644 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003645 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003646 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003647 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003648 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003649 DisplayMetrics dm = getDisplayMetricsLocked(null, true);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003650
3651 if (compat != null && (mResCompatibilityInfo == null ||
3652 !mResCompatibilityInfo.equals(compat))) {
3653 mResCompatibilityInfo = compat;
3654 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3655 | ActivityInfo.CONFIG_SCREEN_SIZE
3656 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3657 }
Bob Leee5408332009-09-04 18:31:17 -07003658
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003659 // set it for java, this also affects newly created Resources
3660 if (config.locale != null) {
3661 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003662 }
Bob Leee5408332009-09-04 18:31:17 -07003663
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003664 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003665
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003666 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003667 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003668
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003669 Iterator<WeakReference<Resources>> it =
3670 mActiveResources.values().iterator();
3671 //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3672 // mActiveResources.entrySet().iterator();
3673 while (it.hasNext()) {
3674 WeakReference<Resources> v = it.next();
3675 Resources r = v.get();
3676 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003677 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003678 + r + " config to: " + config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003679 r.updateConfiguration(config, dm, compat);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003680 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003681 // + " " + r + ": " + r.getConfiguration());
3682 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003683 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003684 it.remove();
3685 }
3686 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003687
3688 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003689 }
Dianne Hackborn836e2622011-10-04 18:32:39 -07003690
3691 final Configuration applyCompatConfiguration() {
3692 Configuration config = mConfiguration;
3693 if (mCompatConfiguration == null) {
3694 mCompatConfiguration = new Configuration();
3695 }
3696 mCompatConfiguration.setTo(mConfiguration);
3697 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
3698 mResCompatibilityInfo.applyToConfiguration(mCompatConfiguration);
3699 config = mCompatConfiguration;
3700 }
3701 return config;
3702 }
3703
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003704 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003705
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003706 ArrayList<ComponentCallbacks2> callbacks = null;
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003707 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003708
3709 synchronized (mPackages) {
3710 if (mPendingConfiguration != null) {
3711 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3712 config = mPendingConfiguration;
3713 }
3714 mPendingConfiguration = null;
3715 }
3716
3717 if (config == null) {
3718 return;
3719 }
3720
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003721 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003722 + config);
3723
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003724 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726 if (mConfiguration == null) {
3727 mConfiguration = new Configuration();
3728 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003729 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003730 return;
3731 }
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003732 configDiff = mConfiguration.diff(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 mConfiguration.updateFrom(config);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003734 config = applyCompatConfiguration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003735 callbacks = collectComponentCallbacksLocked(false, config);
3736 }
Romain Guy65b345f2011-07-27 18:51:50 -07003737
3738 // Cleanup hardware accelerated stuff
3739 WindowManagerImpl.getDefault().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003740
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003741 freeTextLayoutCachesIfNeeded(configDiff);
3742
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003743 if (callbacks != null) {
3744 final int N = callbacks.size();
3745 for (int i=0; i<N; i++) {
3746 performConfigurationChanged(callbacks.get(i), config);
3747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 }
3749 }
3750
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003751 final void freeTextLayoutCachesIfNeeded(int configDiff) {
3752 if (configDiff != 0) {
3753 // Ask text layout engine to free its caches if there is a locale change
3754 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
3755 if (hasLocaleConfigChange) {
3756 Canvas.freeTextLayoutCaches();
3757 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
3758 }
3759 }
3760 }
3761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003762 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003763 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003764 if (r == null || r.activity == null) {
3765 return;
3766 }
Bob Leee5408332009-09-04 18:31:17 -07003767
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003768 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003769 + r.activityInfo.name);
3770
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003771 performConfigurationChanged(r.activity, mCompatConfiguration);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003772
3773 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003774 }
3775
Romain Guy7eabe552011-07-21 14:56:34 -07003776 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003777 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003778 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003779 switch (profileType) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003780 default:
3781 mProfiler.setProfiler(pcd.path, pcd.fd);
3782 mProfiler.autoStopProfiler = false;
3783 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003784 break;
3785 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003786 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003787 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003788 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003789 } finally {
3790 try {
3791 pcd.fd.close();
3792 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003793 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003794 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003795 }
3796 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003797 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07003798 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003799 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003800 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003801 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003802 }
3803 }
Bob Leee5408332009-09-04 18:31:17 -07003804
Romain Guya998dff2012-03-23 18:58:36 -07003805 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07003806 if (managed) {
3807 try {
3808 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3809 } catch (IOException e) {
3810 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3811 + " -- can the process access this path?");
3812 } finally {
3813 try {
3814 dhd.fd.close();
3815 } catch (IOException e) {
3816 Slog.w(TAG, "Failure closing profile fd", e);
3817 }
3818 }
3819 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003820 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003821 }
3822 }
3823
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003824 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3825 boolean hasPkgInfo = false;
3826 if (packages != null) {
3827 for (int i=packages.length-1; i>=0; i--) {
3828 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3829 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003830 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003831 ref = mPackages.get(packages[i]);
3832 if (ref != null && ref.get() != null) {
3833 hasPkgInfo = true;
3834 } else {
3835 ref = mResourcePackages.get(packages[i]);
3836 if (ref != null && ref.get() != null) {
3837 hasPkgInfo = true;
3838 }
3839 }
3840 }
3841 mPackages.remove(packages[i]);
3842 mResourcePackages.remove(packages[i]);
3843 }
3844 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003845 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003846 hasPkgInfo);
3847 }
3848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849 final void handleLowMemory() {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003850 ArrayList<ComponentCallbacks2> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003851
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003852 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853 callbacks = collectComponentCallbacksLocked(true, null);
3854 }
Bob Leee5408332009-09-04 18:31:17 -07003855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003856 final int N = callbacks.size();
3857 for (int i=0; i<N; i++) {
3858 callbacks.get(i).onLowMemory();
3859 }
3860
Chris Tatece229052009-03-25 16:44:52 -07003861 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3862 if (Process.myUid() != Process.SYSTEM_UID) {
3863 int sqliteReleased = SQLiteDatabase.releaseMemory();
3864 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3865 }
Bob Leee5408332009-09-04 18:31:17 -07003866
Mike Reedcaf0df12009-04-27 14:32:05 -04003867 // Ask graphics to free up as much as possible (font/image caches)
3868 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003869
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003870 // Ask text layout engine to free also as much as possible
3871 Canvas.freeTextLayoutCaches();
3872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003873 BinderInternal.forceGc("mem");
3874 }
3875
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003876 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003877 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003878
Romain Guy19f86e82012-04-23 15:19:07 -07003879 final WindowManagerImpl windowManager = WindowManagerImpl.getDefault();
3880 windowManager.startTrimMemory(level);
3881
3882 ArrayList<ComponentCallbacks2> callbacks;
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003883 synchronized (mPackages) {
3884 callbacks = collectComponentCallbacksLocked(true, null);
3885 }
3886
3887 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07003888 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003889 callbacks.get(i).onTrimMemory(level);
3890 }
Romain Guy19f86e82012-04-23 15:19:07 -07003891
3892 windowManager.endTrimMemory();
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003893 }
3894
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003895 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003896 if (Process.isIsolated()) {
3897 // Isolated processes aren't going to do UI.
3898 return;
3899 }
Romain Guya9582652011-11-10 14:20:10 -08003900 try {
3901 int uid = Process.myUid();
3902 String[] packages = getPackageManager().getPackagesForUid(uid);
3903
3904 // If there are several packages in this application we won't
3905 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003906 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003907 HardwareRenderer.setupDiskCache(cacheDir);
3908 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08003909 }
3910 } catch (RemoteException e) {
3911 // Ignore
3912 }
3913 }
3914
Romain Guy65b345f2011-07-27 18:51:50 -07003915 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 mBoundApplication = data;
3917 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003918 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003920 mProfiler = new Profiler();
3921 mProfiler.profileFile = data.initProfileFile;
3922 mProfiler.profileFd = data.initProfileFd;
3923 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
3924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003925 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003926 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003927 android.ddm.DdmHandleAppName.setAppName(data.processName);
3928
Dianne Hackborn5d927c22011-09-02 12:22:18 -07003929 if (data.persistent) {
3930 // Persistent processes on low-memory devices do not get to
3931 // use hardware accelerated drawing, since this can add too much
3932 // overhead to the process.
3933 Display display = WindowManagerImpl.getDefault().getDefaultDisplay();
3934 if (!ActivityManager.isHighEndGfx(display)) {
3935 HardwareRenderer.disable(false);
3936 }
3937 }
Romain Guya9582652011-11-10 14:20:10 -08003938
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003939 if (mProfiler.profileFd != null) {
3940 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003941 }
3942
Joe Onoratod630f102011-03-17 18:42:26 -07003943 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
3944 // implementation to use the pool executor. Normally, we use the
3945 // serialized executor as the default. This has to happen in the
3946 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07003947 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07003948 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
3949 }
3950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003951 /*
3952 * Before spawning a new process, reset the time zone to be the system time zone.
3953 * This needs to be done because the system time zone could have changed after the
3954 * the spawning of this process. Without doing this this process would have the incorrect
3955 * system time zone.
3956 */
3957 TimeZone.setDefault(null);
3958
3959 /*
3960 * Initialize the default locale in this process for the reasons we set the time zone.
3961 */
3962 Locale.setDefault(data.config.locale);
3963
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003964 /*
3965 * Update the system configuration since its preloaded and might not
3966 * reflect configuration changes. The configuration object passed
3967 * in AppBindData can be safely assumed to be up to date
3968 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003969 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003970 applyCompatConfiguration();
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003971
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003972 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003974 final ContextImpl appContext = new ContextImpl();
3975 appContext.init(data.info, null, this);
3976 final File cacheDir = appContext.getCacheDir();
3977
3978 // Provide a usable directory for temporary files
3979 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
3980
3981 setupGraphicsSupport(data.info, cacheDir);
3982
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003983 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003984 * For system applications on userdebug/eng builds, log stack
3985 * traces of disk and network access to dropbox for analysis.
3986 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07003987 if ((data.appInfo.flags &
3988 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07003989 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
3990 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003991 }
3992
3993 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07003994 * For apps targetting SDK Honeycomb or later, we don't allow
3995 * network usage on the main event loop / UI thread.
3996 *
3997 * Note to those grepping: this is what ultimately throws
3998 * NetworkOnMainThreadException ...
3999 */
4000 if (data.appInfo.targetSdkVersion > 9) {
4001 StrictMode.enableDeathOnNetwork();
4002 }
4003
4004 /**
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004005 * Switch this process to density compatibility mode if needed.
4006 */
4007 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4008 == 0) {
4009 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4010 }
Bob Leee5408332009-09-04 18:31:17 -07004011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004012 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4013 // XXX should have option to change the port.
4014 Debug.changeDebugPort(8100);
4015 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004016 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 + " is waiting for the debugger on port 8100...");
4018
4019 IActivityManager mgr = ActivityManagerNative.getDefault();
4020 try {
4021 mgr.showWaitingForDebugger(mAppThread, true);
4022 } catch (RemoteException ex) {
4023 }
4024
4025 Debug.waitForDebugger();
4026
4027 try {
4028 mgr.showWaitingForDebugger(mAppThread, false);
4029 } catch (RemoteException ex) {
4030 }
4031
4032 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004033 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 + " can be debugged on port 8100...");
4035 }
4036 }
4037
Siva Velusamy92a8b222012-03-09 16:24:04 -08004038 // Enable OpenGL tracing if required
4039 if (data.enableOpenGlTrace) {
4040 GLUtils.enableTracing();
4041 }
4042
Robert Greenwalt434203a2010-10-11 16:00:27 -07004043 /**
4044 * Initialize the default http proxy in this process for the reasons we set the time zone.
4045 */
4046 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004047 if (b != null) {
4048 // In pre-boot mode (doing initial launch to collect password), not
4049 // all system is up. This includes the connectivity service, so don't
4050 // crash if we can't get it.
4051 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4052 try {
4053 ProxyProperties proxyProperties = service.getProxy();
4054 Proxy.setHttpProxySystemProperty(proxyProperties);
4055 } catch (RemoteException e) {}
4056 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 InstrumentationInfo ii = null;
4060 try {
4061 ii = appContext.getPackageManager().
4062 getInstrumentationInfo(data.instrumentationName, 0);
4063 } catch (PackageManager.NameNotFoundException e) {
4064 }
4065 if (ii == null) {
4066 throw new RuntimeException(
4067 "Unable to find instrumentation info for: "
4068 + data.instrumentationName);
4069 }
4070
4071 mInstrumentationAppDir = ii.sourceDir;
Brian Carlstromd893a892012-04-01 21:30:26 -07004072 mInstrumentationAppLibraryDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004073 mInstrumentationAppPackage = ii.packageName;
4074 mInstrumentedAppDir = data.info.getAppDir();
Brian Carlstromd893a892012-04-01 21:30:26 -07004075 mInstrumentedAppLibraryDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004076
4077 ApplicationInfo instrApp = new ApplicationInfo();
4078 instrApp.packageName = ii.packageName;
4079 instrApp.sourceDir = ii.sourceDir;
4080 instrApp.publicSourceDir = ii.publicSourceDir;
4081 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004082 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004083 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08004085 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004086 instrContext.init(pi, null, this);
4087
4088 try {
4089 java.lang.ClassLoader cl = instrContext.getClassLoader();
4090 mInstrumentation = (Instrumentation)
4091 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4092 } catch (Exception e) {
4093 throw new RuntimeException(
4094 "Unable to instantiate instrumentation "
4095 + data.instrumentationName + ": " + e.toString(), e);
4096 }
4097
4098 mInstrumentation.init(this, instrContext, appContext,
4099 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
4100
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004101 if (mProfiler.profileFile != null && !ii.handleProfiling
4102 && mProfiler.profileFd == null) {
4103 mProfiler.handlingProfiling = true;
4104 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105 file.getParentFile().mkdirs();
4106 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4107 }
4108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004109 } else {
4110 mInstrumentation = new Instrumentation();
4111 }
4112
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004113 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004114 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004115 }
4116
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004117 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004118 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004119 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004120 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004122 // If the app is being launched for full backup or restore, bring it up in
4123 // a restricted environment with the base application class.
4124 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4125 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004126
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004127 // don't bring up providers in restricted mode; they may depend on the
4128 // app's custom Application class
4129 if (!data.restrictedBackupMode) {
4130 List<ProviderInfo> providers = data.providers;
4131 if (providers != null) {
4132 installContentProviders(app, providers);
4133 // For process that contains content providers, we want to
4134 // ensure that the JIT is enabled "at some point".
4135 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4136 }
4137 }
4138
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004139 // Do this after providers, since instrumentation tests generally start their
4140 // test thread at this point, and we don't want that racing.
4141 try {
4142 mInstrumentation.onCreate(data.instrumentationArgs);
4143 }
4144 catch (Exception e) {
4145 throw new RuntimeException(
4146 "Exception thrown in onCreate() of "
4147 + data.instrumentationName + ": " + e.toString(), e);
4148 }
4149
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004150 try {
4151 mInstrumentation.callApplicationOnCreate(app);
4152 } catch (Exception e) {
4153 if (!mInstrumentation.onException(app, e)) {
4154 throw new RuntimeException(
4155 "Unable to create application " + app.getClass().getName()
4156 + ": " + e.toString(), e);
4157 }
4158 }
4159 } finally {
4160 StrictMode.setThreadPolicy(savedPolicy);
4161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 }
4163
4164 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4165 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004166 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4167 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 Debug.stopMethodTracing();
4169 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004170 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 // + ", app thr: " + mAppThread);
4172 try {
4173 am.finishInstrumentation(mAppThread, resultCode, results);
4174 } catch (RemoteException ex) {
4175 }
4176 }
4177
Romain Guy65b345f2011-07-27 18:51:50 -07004178 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 Context context, List<ProviderInfo> providers) {
4180 final ArrayList<IActivityManager.ContentProviderHolder> results =
4181 new ArrayList<IActivityManager.ContentProviderHolder>();
4182
Romain Guya998dff2012-03-23 18:58:36 -07004183 for (ProviderInfo cpi : providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004184 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07004185 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 buf.append(cpi.authority);
4187 buf.append(": ");
4188 buf.append(cpi.name);
4189 Log.i(TAG, buf.toString());
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004190 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
4191 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
4192 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004193 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004194 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 }
4196 }
4197
4198 try {
4199 ActivityManagerNative.getDefault().publishContentProviders(
4200 getApplicationThread(), results);
4201 } catch (RemoteException ex) {
4202 }
4203 }
4204
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004205 public final IContentProvider acquireProvider(Context c, String name, boolean stable) {
4206 IContentProvider provider = acquireExistingProvider(c, name, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004207 if (provider != null) {
4208 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004209 }
4210
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004211 // There is a possible race here. Another thread may try to acquire
4212 // the same provider at the same time. When this happens, we want to ensure
4213 // that the first one wins.
4214 // Note that we cannot hold the lock while acquiring and installing the
4215 // provider since it might take a long time to run and it could also potentially
4216 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004217 IActivityManager.ContentProviderHolder holder = null;
4218 try {
4219 holder = ActivityManagerNative.getDefault().getContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004220 getApplicationThread(), name, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 } catch (RemoteException ex) {
4222 }
4223 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004224 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 return null;
4226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004228 // Install provider will increment the reference count for us, and break
4229 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004230 holder = installProvider(c, holder, holder.info,
4231 true /*noisy*/, holder.noReleaseNeeded, stable);
4232 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233 }
4234
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004235 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
4236 if (stable) {
4237 prc.stableCount += 1;
4238 if (prc.stableCount == 1) {
4239 // We are acquiring a new stable reference on the provider.
4240 int unstableDelta;
4241 if (prc.removePending) {
4242 // We have a pending remove operation, which is holding the
4243 // last unstable reference. At this point we are converting
4244 // that unstable reference to our new stable reference.
4245 unstableDelta = -1;
4246 // Cancel the removal of the provider.
4247 if (DEBUG_PROVIDER) {
4248 Slog.v(TAG, "incProviderRef: stable "
4249 + "snatched provider from the jaws of death");
4250 }
4251 prc.removePending = false;
4252 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4253 } else {
4254 unstableDelta = 0;
4255 }
4256 try {
4257 if (DEBUG_PROVIDER) {
4258 Slog.v(TAG, "incProviderRef Now stable - "
4259 + prc.holder.info.name + ": unstableDelta="
4260 + unstableDelta);
4261 }
4262 ActivityManagerNative.getDefault().refContentProvider(
4263 prc.holder.connection, 1, unstableDelta);
4264 } catch (RemoteException e) {
4265 //do nothing content provider object is dead any way
4266 }
4267 }
4268 } else {
4269 prc.unstableCount += 1;
4270 if (prc.unstableCount == 1) {
4271 // We are acquiring a new unstable reference on the provider.
4272 if (prc.removePending) {
4273 // Oh look, we actually have a remove pending for the
4274 // provider, which is still holding the last unstable
4275 // reference. We just need to cancel that to take new
4276 // ownership of the reference.
4277 if (DEBUG_PROVIDER) {
4278 Slog.v(TAG, "incProviderRef: unstable "
4279 + "snatched provider from the jaws of death");
4280 }
4281 prc.removePending = false;
4282 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4283 } else {
4284 // First unstable ref, increment our count in the
4285 // activity manager.
4286 try {
4287 if (DEBUG_PROVIDER) {
4288 Slog.v(TAG, "incProviderRef: Now unstable - "
4289 + prc.holder.info.name);
4290 }
4291 ActivityManagerNative.getDefault().refContentProvider(
4292 prc.holder.connection, 0, 1);
4293 } catch (RemoteException e) {
4294 //do nothing content provider object is dead any way
4295 }
4296 }
4297 }
4298 }
4299 }
4300
4301 public final IContentProvider acquireExistingProvider(Context c, String name,
4302 boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004303 synchronized (mProviderMap) {
4304 ProviderClientRecord pr = mProviderMap.get(name);
4305 if (pr == null) {
4306 return null;
4307 }
4308
4309 IContentProvider provider = pr.mProvider;
4310 IBinder jBinder = provider.asBinder();
4311
4312 // Only increment the ref count if we have one. If we don't then the
4313 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004314 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004315 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004316 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004317 }
4318 return provider;
4319 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004320 }
4321
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004322 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
4323 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 return false;
4325 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004328 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004330 if (prc == null) {
4331 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004332 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004333 }
4334
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004335 boolean lastRef = false;
4336 if (stable) {
4337 if (prc.stableCount == 0) {
4338 if (DEBUG_PROVIDER) Slog.v(TAG,
4339 "releaseProvider: stable ref count already 0, how?");
4340 return false;
4341 }
4342 prc.stableCount -= 1;
4343 if (prc.stableCount == 0) {
4344 // What we do at this point depends on whether there are
4345 // any unstable refs left: if there are, we just tell the
4346 // activity manager to decrement its stable count; if there
4347 // aren't, we need to enqueue this provider to be removed,
4348 // and convert to holding a single unstable ref while
4349 // doing so.
4350 lastRef = prc.unstableCount == 0;
4351 try {
4352 if (DEBUG_PROVIDER) {
4353 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
4354 + lastRef + " - " + prc.holder.info.name);
4355 }
4356 ActivityManagerNative.getDefault().refContentProvider(
4357 prc.holder.connection, -1, lastRef ? 1 : 0);
4358 } catch (RemoteException e) {
4359 //do nothing content provider object is dead any way
4360 }
4361 }
4362 } else {
4363 if (prc.unstableCount == 0) {
4364 if (DEBUG_PROVIDER) Slog.v(TAG,
4365 "releaseProvider: unstable ref count already 0, how?");
4366 return false;
4367 }
4368 prc.unstableCount -= 1;
4369 if (prc.unstableCount == 0) {
4370 // If this is the last reference, we need to enqueue
4371 // this provider to be removed instead of telling the
4372 // activity manager to remove it at this point.
4373 lastRef = prc.stableCount == 0;
4374 if (!lastRef) {
4375 try {
4376 if (DEBUG_PROVIDER) {
4377 Slog.v(TAG, "releaseProvider: No longer unstable - "
4378 + prc.holder.info.name);
4379 }
4380 ActivityManagerNative.getDefault().refContentProvider(
4381 prc.holder.connection, 0, -1);
4382 } catch (RemoteException e) {
4383 //do nothing content provider object is dead any way
4384 }
4385 }
4386 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004387 }
4388
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004389 if (lastRef) {
4390 if (!prc.removePending) {
4391 // Schedule the actual remove asynchronously, since we don't know the context
4392 // this will be called in.
4393 // TODO: it would be nice to post a delayed message, so
4394 // if we come back and need the same provider quickly
4395 // we will still have it available.
4396 if (DEBUG_PROVIDER) {
4397 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
4398 + prc.holder.info.name);
4399 }
4400 prc.removePending = true;
4401 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
4402 mH.sendMessage(msg);
4403 } else {
4404 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
4405 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004406 }
4407 return true;
4408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004409 }
4410
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004411 final void completeRemoveProvider(ProviderRefCount prc) {
4412 synchronized (mProviderMap) {
4413 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004414 // There was a race! Some other client managed to acquire
4415 // the provider before the removal was completed.
4416 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004417 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004418 + "provider still in use");
4419 return;
4420 }
4421
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004422 final IBinder jBinder = prc.holder.provider.asBinder();
4423 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
4424 if (existingPrc == prc) {
4425 mProviderRefCountMap.remove(jBinder);
4426 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004427
4428 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
4429 while (iter.hasNext()) {
4430 ProviderClientRecord pr = iter.next();
4431 IBinder myBinder = pr.mProvider.asBinder();
4432 if (myBinder == jBinder) {
4433 iter.remove();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004434 }
4435 }
4436 }
4437
4438 try {
4439 if (DEBUG_PROVIDER) {
4440 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4441 + "removeContentProvider(" + prc.holder.info.name + ")");
4442 }
4443 ActivityManagerNative.getDefault().removeContentProvider(
4444 prc.holder.connection, false);
4445 } catch (RemoteException e) {
4446 //do nothing content provider object is dead any way
4447 }
4448 }
4449
4450 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
4451 synchronized(mProviderMap) {
4452 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4453 if (prc != null) {
4454 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
4455 + provider + " " + prc.holder.info.name);
4456 mProviderRefCountMap.remove(provider);
4457 if (prc.client != null && prc.client.mNames != null) {
4458 for (String name : prc.client.mNames) {
4459 ProviderClientRecord pr = mProviderMap.get(name);
4460 if (pr != null && pr.mProvider.asBinder() == provider) {
4461 Slog.i(TAG, "Removing dead content provider: " + name);
4462 mProviderMap.remove(name);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004463 }
4464 }
4465 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004466 if (fromClient) {
4467 // We found out about this due to execution in our client
4468 // code. Tell the activity manager about it now, to ensure
4469 // that the next time we go to do anything with the provider
4470 // it knows it is dead (so we don't race with its death
4471 // notification).
4472 try {
4473 ActivityManagerNative.getDefault().unstableProviderDied(
4474 prc.holder.connection);
4475 } catch (RemoteException e) {
4476 //do nothing content provider object is dead any way
4477 }
4478 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004479 }
4480 }
4481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004482
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004483 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
4484 ContentProvider localProvider,IActivityManager.ContentProviderHolder holder) {
4485 String names[] = PATTERN_SEMICOLON.split(holder.info.authority);
4486 ProviderClientRecord pcr = new ProviderClientRecord(names, provider,
4487 localProvider, holder);
4488 for (int i = 0; i < names.length; i++) {
4489 ProviderClientRecord existing = mProviderMap.get(names[i]);
4490 if (existing != null) {
4491 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
4492 + " already published as " + names[i]);
4493 } else {
4494 mProviderMap.put(names[i], pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004495 }
4496 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004497 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004498 }
4499
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004500 /**
4501 * Installs the provider.
4502 *
4503 * Providers that are local to the process or that come from the system server
4504 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4505 * Other remote providers are reference counted. The initial reference count
4506 * for all reference counted providers is one. Providers that are not reference
4507 * counted do not have a reference count (at all).
4508 *
4509 * This method detects when a provider has already been installed. When this happens,
4510 * it increments the reference count of the existing provider (if appropriate)
4511 * and returns the existing provider. This can happen due to concurrent
4512 * attempts to acquire the same provider.
4513 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004514 private IActivityManager.ContentProviderHolder installProvider(Context context,
4515 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
4516 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004517 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004518 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07004519 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004520 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004521 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522 + info.name);
4523 }
4524 Context c = null;
4525 ApplicationInfo ai = info.applicationInfo;
4526 if (context.getPackageName().equals(ai.packageName)) {
4527 c = context;
4528 } else if (mInitialApplication != null &&
4529 mInitialApplication.getPackageName().equals(ai.packageName)) {
4530 c = mInitialApplication;
4531 } else {
4532 try {
4533 c = context.createPackageContext(ai.packageName,
4534 Context.CONTEXT_INCLUDE_CODE);
4535 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004536 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004537 }
4538 }
4539 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004540 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 ai.packageName +
4542 " while loading content provider " +
4543 info.name);
4544 return null;
4545 }
4546 try {
4547 final java.lang.ClassLoader cl = c.getClassLoader();
4548 localProvider = (ContentProvider)cl.
4549 loadClass(info.name).newInstance();
4550 provider = localProvider.getIContentProvider();
4551 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004552 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004553 info.name + " from sourceDir " +
4554 info.applicationInfo.sourceDir);
4555 return null;
4556 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004557 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004558 TAG, "Instantiating local provider " + info.name);
4559 // XXX Need to create the correct context for this provider.
4560 localProvider.attachInfo(c, info);
4561 } catch (java.lang.Exception e) {
4562 if (!mInstrumentation.onException(null, e)) {
4563 throw new RuntimeException(
4564 "Unable to get provider " + info.name
4565 + ": " + e.toString(), e);
4566 }
4567 return null;
4568 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004569 } else {
4570 provider = holder.provider;
4571 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004572 + info.name);
4573 }
4574
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004575 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004576
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004577 synchronized (mProviderMap) {
4578 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
4579 + " / " + info.name);
4580 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004582 ComponentName cname = new ComponentName(info.packageName, info.name);
4583 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004584 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004585 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004586 Slog.v(TAG, "installProvider: lost the race, "
4587 + "using existing local provider");
4588 }
4589 provider = pr.mProvider;
4590 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004591 holder = new IActivityManager.ContentProviderHolder(info);
4592 holder.provider = provider;
4593 holder.noReleaseNeeded = true;
4594 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004595 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004596 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004597 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004598 retHolder = pr.mHolder;
4599 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004600 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4601 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004602 if (DEBUG_PROVIDER) {
4603 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004604 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004605 // We need to transfer our new reference to the existing
4606 // ref count, releasing the old one... but only if
4607 // release is needed (that is, it is not running in the
4608 // system process).
4609 if (!noReleaseNeeded) {
4610 incProviderRefLocked(prc, stable);
4611 try {
4612 ActivityManagerNative.getDefault().removeContentProvider(
4613 holder.connection, stable);
4614 } catch (RemoteException e) {
4615 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004616 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004617 }
4618 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004619 ProviderClientRecord client = installProviderAuthoritiesLocked(
4620 provider, localProvider, holder);
4621 if (noReleaseNeeded) {
4622 prc = new ProviderRefCount(holder, client, 1000, 1000);
4623 } else {
4624 prc = stable
4625 ? new ProviderRefCount(holder, client, 1, 0)
4626 : new ProviderRefCount(holder, client, 0, 1);
4627 }
4628 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004629 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004630 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004631 }
4632 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004633
4634 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004635 }
4636
Romain Guy65b345f2011-07-27 18:51:50 -07004637 private void attach(boolean system) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004638 sThreadLocal.set(this);
4639 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004640 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004641 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004642 public void run() {
4643 ensureJitEnabled();
4644 }
4645 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004646 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4647 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4648 IActivityManager mgr = ActivityManagerNative.getDefault();
4649 try {
4650 mgr.attachApplication(mAppThread);
4651 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004652 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004653 }
4654 } else {
4655 // Don't set application object here -- if the system crashes,
4656 // we can't display an alert, we just want to die die die.
4657 android.ddm.DdmHandleAppName.setAppName("system_process");
4658 try {
4659 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004660 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004661 context.init(getSystemContext().mPackageInfo, null, this);
4662 Application app = Instrumentation.newApplication(Application.class, context);
4663 mAllApplications.add(app);
4664 mInitialApplication = app;
4665 app.onCreate();
4666 } catch (Exception e) {
4667 throw new RuntimeException(
4668 "Unable to instantiate Application():" + e.toString(), e);
4669 }
4670 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004671
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004672 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004673 public void onConfigurationChanged(Configuration newConfig) {
4674 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004675 // We need to apply this change to the resources
4676 // immediately, because upon returning the view
4677 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004678 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004679 // This actually changed the resources! Tell
4680 // everyone about it.
4681 if (mPendingConfiguration == null ||
4682 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4683 mPendingConfiguration = newConfig;
4684
4685 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4686 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004687 }
4688 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004689 }
4690 public void onLowMemory() {
4691 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004692 public void onTrimMemory(int level) {
4693 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004694 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004695 }
4696
Romain Guy5e9120d2012-01-30 12:17:22 -08004697 public static ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004698 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 ActivityThread thread = new ActivityThread();
4700 thread.attach(true);
4701 return thread;
4702 }
4703
Jeff Brown10e89712011-07-08 18:52:57 -07004704 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004705 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004706 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 }
4708 }
4709
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004710 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004711 synchronized (mPackages) {
4712 if (mCoreSettings != null) {
4713 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004714 } else {
4715 return defaultValue;
4716 }
4717 }
4718 }
4719
Romain Guy65b345f2011-07-27 18:51:50 -07004720 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004721 SamplingProfilerIntegration.start();
4722
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004723 // CloseGuard defaults to true and can be quite spammy. We
4724 // disable it here, but selectively enable it later (via
4725 // StrictMode) on debug builds, but using DropBox, not logs.
4726 CloseGuard.setEnabled(false);
4727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004728 Process.setArgV0("<pre-initialized>");
4729
4730 Looper.prepareMainLooper();
4731
4732 ActivityThread thread = new ActivityThread();
4733 thread.attach(false);
4734
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07004735 if (sMainThreadHandler == null) {
4736 sMainThreadHandler = thread.getHandler();
4737 }
4738
Romain Guy5e9120d2012-01-30 12:17:22 -08004739 AsyncTask.init();
4740
Dianne Hackborn287952c2010-09-22 22:34:31 -07004741 if (false) {
4742 Looper.myLooper().setMessageLogging(new
4743 LogPrinter(Log.DEBUG, "ActivityThread"));
4744 }
4745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004746 Looper.loop();
4747
Jeff Brown10e89712011-07-08 18:52:57 -07004748 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004749 }
4750}