The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app; |
| 18 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.ComponentCallbacks; |
| 21 | import android.content.ComponentName; |
| 22 | import android.content.ContentProvider; |
| 23 | import android.content.Context; |
| 24 | import android.content.IContentProvider; |
| 25 | import android.content.Intent; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 26 | import android.content.IIntentReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.content.ServiceConnection; |
| 28 | import android.content.pm.ActivityInfo; |
| 29 | import android.content.pm.ApplicationInfo; |
| 30 | import android.content.pm.IPackageManager; |
| 31 | import android.content.pm.InstrumentationInfo; |
| 32 | import android.content.pm.PackageManager; |
| 33 | import android.content.pm.ProviderInfo; |
| 34 | import android.content.pm.ServiceInfo; |
Mitsuru Oshima | 12172ba | 2009-06-12 15:50:03 -0700 | [diff] [blame] | 35 | import android.content.pm.PackageParser.Component; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.content.res.AssetManager; |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 37 | import android.content.res.CompatibilityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.content.res.Configuration; |
| 39 | import android.content.res.Resources; |
| 40 | import android.database.sqlite.SQLiteDatabase; |
| 41 | import android.database.sqlite.SQLiteDebug; |
| 42 | import android.graphics.Bitmap; |
| 43 | import android.graphics.Canvas; |
| 44 | import android.net.http.AndroidHttpClient; |
| 45 | import android.os.Bundle; |
| 46 | import android.os.Debug; |
| 47 | import android.os.Handler; |
| 48 | import android.os.IBinder; |
| 49 | import android.os.Looper; |
| 50 | import android.os.Message; |
| 51 | import android.os.MessageQueue; |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 52 | import android.os.ParcelFileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.os.Process; |
| 54 | import android.os.RemoteException; |
| 55 | import android.os.ServiceManager; |
| 56 | import android.os.SystemClock; |
| 57 | import android.util.AndroidRuntimeException; |
| 58 | import android.util.Config; |
| 59 | import android.util.DisplayMetrics; |
| 60 | import android.util.EventLog; |
| 61 | import android.util.Log; |
| 62 | import android.view.Display; |
| 63 | import android.view.View; |
| 64 | import android.view.ViewDebug; |
| 65 | import android.view.ViewManager; |
| 66 | import android.view.Window; |
| 67 | import android.view.WindowManager; |
| 68 | import android.view.WindowManagerImpl; |
| 69 | |
| 70 | import com.android.internal.os.BinderInternal; |
| 71 | import com.android.internal.os.RuntimeInit; |
| 72 | import com.android.internal.util.ArrayUtils; |
| 73 | |
| 74 | import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl; |
| 75 | |
| 76 | import java.io.File; |
| 77 | import java.io.FileDescriptor; |
| 78 | import java.io.FileOutputStream; |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 79 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | import java.io.PrintWriter; |
| 81 | import java.lang.ref.WeakReference; |
| 82 | import java.util.ArrayList; |
| 83 | import java.util.HashMap; |
| 84 | import java.util.Iterator; |
| 85 | import java.util.List; |
| 86 | import java.util.Locale; |
| 87 | import java.util.Map; |
| 88 | import java.util.TimeZone; |
| 89 | import java.util.regex.Pattern; |
| 90 | |
| 91 | final class IntentReceiverLeaked extends AndroidRuntimeException { |
| 92 | public IntentReceiverLeaked(String msg) { |
| 93 | super(msg); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | final class ServiceConnectionLeaked extends AndroidRuntimeException { |
| 98 | public ServiceConnectionLeaked(String msg) { |
| 99 | super(msg); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | final class SuperNotCalledException extends AndroidRuntimeException { |
| 104 | public SuperNotCalledException(String msg) { |
| 105 | super(msg); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * This manages the execution of the main thread in an |
| 111 | * application process, scheduling and executing activities, |
| 112 | * broadcasts, and other operations on it as the activity |
| 113 | * manager requests. |
| 114 | * |
| 115 | * {@hide} |
| 116 | */ |
| 117 | public final class ActivityThread { |
| 118 | private static final String TAG = "ActivityThread"; |
| 119 | private static final boolean DEBUG = false; |
| 120 | private static final boolean localLOGV = DEBUG ? Config.LOGD : Config.LOGV; |
| 121 | private static final boolean DEBUG_BROADCAST = false; |
Chris Tate | 8a7dc17 | 2009-03-24 20:11:42 -0700 | [diff] [blame] | 122 | private static final boolean DEBUG_RESULTS = false; |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 123 | private static final boolean DEBUG_BACKUP = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | private static final long MIN_TIME_BETWEEN_GCS = 5*1000; |
| 125 | private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";"); |
| 126 | private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003; |
| 127 | private static final int LOG_ON_PAUSE_CALLED = 30021; |
| 128 | private static final int LOG_ON_RESUME_CALLED = 30022; |
| 129 | |
| 130 | |
| 131 | public static final ActivityThread currentActivityThread() { |
| 132 | return (ActivityThread)sThreadLocal.get(); |
| 133 | } |
| 134 | |
| 135 | public static final String currentPackageName() |
| 136 | { |
| 137 | ActivityThread am = currentActivityThread(); |
| 138 | return (am != null && am.mBoundApplication != null) |
| 139 | ? am.mBoundApplication.processName : null; |
| 140 | } |
| 141 | |
| 142 | public static IPackageManager getPackageManager() { |
| 143 | if (sPackageManager != null) { |
| 144 | //Log.v("PackageManager", "returning cur default = " + sPackageManager); |
| 145 | return sPackageManager; |
| 146 | } |
| 147 | IBinder b = ServiceManager.getService("package"); |
| 148 | //Log.v("PackageManager", "default service binder = " + b); |
| 149 | sPackageManager = IPackageManager.Stub.asInterface(b); |
| 150 | //Log.v("PackageManager", "default service = " + sPackageManager); |
| 151 | return sPackageManager; |
| 152 | } |
| 153 | |
| 154 | DisplayMetrics getDisplayMetricsLocked(boolean forceUpdate) { |
| 155 | if (mDisplayMetrics != null && !forceUpdate) { |
| 156 | return mDisplayMetrics; |
| 157 | } |
| 158 | if (mDisplay == null) { |
| 159 | WindowManager wm = WindowManagerImpl.getDefault(); |
| 160 | mDisplay = wm.getDefaultDisplay(); |
| 161 | } |
| 162 | DisplayMetrics metrics = mDisplayMetrics = new DisplayMetrics(); |
| 163 | mDisplay.getMetrics(metrics); |
| 164 | //Log.i("foo", "New metrics: w=" + metrics.widthPixels + " h=" |
| 165 | // + metrics.heightPixels + " den=" + metrics.density |
| 166 | // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi); |
| 167 | return metrics; |
| 168 | } |
| 169 | |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 170 | /** |
| 171 | * Creates the top level Resources for applications with the given compatibility info. |
| 172 | * |
| 173 | * @param resDir the resource directory. |
| 174 | * @param compInfo the compability info. It will use the default compatibility info when it's |
| 175 | * null. |
| 176 | */ |
| 177 | Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | synchronized (mPackages) { |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 179 | // Resources is app scale dependent. |
| 180 | ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale); |
| 181 | //Log.w(TAG, "getTopLevelResources: " + resDir); |
| 182 | WeakReference<Resources> wr = mActiveResources.get(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | Resources r = wr != null ? wr.get() : null; |
| 184 | if (r != null && r.getAssets().isUpToDate()) { |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 185 | //Log.w(TAG, "Returning cached resources " + r + " " + resDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | return r; |
| 187 | } |
| 188 | |
| 189 | //if (r != null) { |
| 190 | // Log.w(TAG, "Throwing away out-of-date resources!!!! " |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 191 | // + r + " " + resDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | //} |
| 193 | |
| 194 | AssetManager assets = new AssetManager(); |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 195 | if (assets.addAssetPath(resDir) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | return null; |
| 197 | } |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 198 | |
| 199 | //Log.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics); |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 200 | DisplayMetrics metrics = getDisplayMetricsLocked(false); |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 201 | r = new Resources(assets, metrics, getConfiguration(), compInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | //Log.i(TAG, "Created app resources " + r + ": " + r.getConfiguration()); |
| 203 | // XXX need to remove entries when weak references go away |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 204 | mActiveResources.put(key, new WeakReference<Resources>(r)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | return r; |
| 206 | } |
| 207 | } |
| 208 | |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 209 | /** |
| 210 | * Creates the top level resources for the given package. |
| 211 | */ |
| 212 | Resources getTopLevelResources(String resDir, PackageInfo pkgInfo) { |
| 213 | return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo); |
| 214 | } |
| 215 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | final Handler getHandler() { |
| 217 | return mH; |
| 218 | } |
| 219 | |
| 220 | public final static class PackageInfo { |
| 221 | |
| 222 | private final ActivityThread mActivityThread; |
| 223 | private final ApplicationInfo mApplicationInfo; |
| 224 | private final String mPackageName; |
| 225 | private final String mAppDir; |
| 226 | private final String mResDir; |
| 227 | private final String[] mSharedLibraries; |
| 228 | private final String mDataDir; |
| 229 | private final File mDataDirFile; |
| 230 | private final ClassLoader mBaseClassLoader; |
| 231 | private final boolean mSecurityViolation; |
| 232 | private final boolean mIncludeCode; |
| 233 | private Resources mResources; |
| 234 | private ClassLoader mClassLoader; |
| 235 | private Application mApplication; |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 236 | private CompatibilityInfo mCompatibilityInfo; |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 237 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | private final HashMap<Context, HashMap<BroadcastReceiver, ReceiverDispatcher>> mReceivers |
| 239 | = new HashMap<Context, HashMap<BroadcastReceiver, ReceiverDispatcher>>(); |
| 240 | private final HashMap<Context, HashMap<BroadcastReceiver, ReceiverDispatcher>> mUnregisteredReceivers |
| 241 | = new HashMap<Context, HashMap<BroadcastReceiver, ReceiverDispatcher>>(); |
| 242 | private final HashMap<Context, HashMap<ServiceConnection, ServiceDispatcher>> mServices |
| 243 | = new HashMap<Context, HashMap<ServiceConnection, ServiceDispatcher>>(); |
| 244 | private final HashMap<Context, HashMap<ServiceConnection, ServiceDispatcher>> mUnboundServices |
| 245 | = new HashMap<Context, HashMap<ServiceConnection, ServiceDispatcher>>(); |
| 246 | |
| 247 | int mClientCount = 0; |
| 248 | |
| 249 | public PackageInfo(ActivityThread activityThread, ApplicationInfo aInfo, |
| 250 | ActivityThread mainThread, ClassLoader baseLoader, |
| 251 | boolean securityViolation, boolean includeCode) { |
| 252 | mActivityThread = activityThread; |
| 253 | mApplicationInfo = aInfo; |
| 254 | mPackageName = aInfo.packageName; |
| 255 | mAppDir = aInfo.sourceDir; |
| 256 | mResDir = aInfo.uid == Process.myUid() ? aInfo.sourceDir |
| 257 | : aInfo.publicSourceDir; |
| 258 | mSharedLibraries = aInfo.sharedLibraryFiles; |
| 259 | mDataDir = aInfo.dataDir; |
| 260 | mDataDirFile = mDataDir != null ? new File(mDataDir) : null; |
| 261 | mBaseClassLoader = baseLoader; |
| 262 | mSecurityViolation = securityViolation; |
| 263 | mIncludeCode = includeCode; |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 264 | mCompatibilityInfo = new CompatibilityInfo(aInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | |
| 266 | if (mAppDir == null) { |
| 267 | if (mSystemContext == null) { |
| 268 | mSystemContext = |
| 269 | ApplicationContext.createSystemContext(mainThread); |
| 270 | mSystemContext.getResources().updateConfiguration( |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 271 | mainThread.getConfiguration(), |
| 272 | mainThread.getDisplayMetricsLocked(false)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | //Log.i(TAG, "Created system resources " |
| 274 | // + mSystemContext.getResources() + ": " |
| 275 | // + mSystemContext.getResources().getConfiguration()); |
| 276 | } |
| 277 | mClassLoader = mSystemContext.getClassLoader(); |
| 278 | mResources = mSystemContext.getResources(); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | public PackageInfo(ActivityThread activityThread, String name, |
| 283 | Context systemContext) { |
| 284 | mActivityThread = activityThread; |
| 285 | mApplicationInfo = new ApplicationInfo(); |
| 286 | mApplicationInfo.packageName = name; |
| 287 | mPackageName = name; |
| 288 | mAppDir = null; |
| 289 | mResDir = null; |
| 290 | mSharedLibraries = null; |
| 291 | mDataDir = null; |
| 292 | mDataDirFile = null; |
| 293 | mBaseClassLoader = null; |
| 294 | mSecurityViolation = false; |
| 295 | mIncludeCode = true; |
| 296 | mClassLoader = systemContext.getClassLoader(); |
| 297 | mResources = systemContext.getResources(); |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 298 | mCompatibilityInfo = new CompatibilityInfo(mApplicationInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | public String getPackageName() { |
| 302 | return mPackageName; |
| 303 | } |
| 304 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 305 | public ApplicationInfo getApplicationInfo() { |
| 306 | return mApplicationInfo; |
| 307 | } |
| 308 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | public boolean isSecurityViolation() { |
| 310 | return mSecurityViolation; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Gets the array of shared libraries that are listed as |
| 315 | * used by the given package. |
| 316 | * |
| 317 | * @param packageName the name of the package (note: not its |
| 318 | * file name) |
| 319 | * @return null-ok; the array of shared libraries, each one |
| 320 | * a fully-qualified path |
| 321 | */ |
| 322 | private static String[] getLibrariesFor(String packageName) { |
| 323 | ApplicationInfo ai = null; |
| 324 | try { |
| 325 | ai = getPackageManager().getApplicationInfo(packageName, |
| 326 | PackageManager.GET_SHARED_LIBRARY_FILES); |
| 327 | } catch (RemoteException e) { |
| 328 | throw new AssertionError(e); |
| 329 | } |
| 330 | |
| 331 | if (ai == null) { |
| 332 | return null; |
| 333 | } |
| 334 | |
| 335 | return ai.sharedLibraryFiles; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Combines two arrays (of library names) such that they are |
| 340 | * concatenated in order but are devoid of duplicates. The |
| 341 | * result is a single string with the names of the libraries |
| 342 | * separated by colons, or <code>null</code> if both lists |
| 343 | * were <code>null</code> or empty. |
| 344 | * |
| 345 | * @param list1 null-ok; the first list |
| 346 | * @param list2 null-ok; the second list |
| 347 | * @return null-ok; the combination |
| 348 | */ |
| 349 | private static String combineLibs(String[] list1, String[] list2) { |
| 350 | StringBuilder result = new StringBuilder(300); |
| 351 | boolean first = true; |
| 352 | |
| 353 | if (list1 != null) { |
| 354 | for (String s : list1) { |
| 355 | if (first) { |
| 356 | first = false; |
| 357 | } else { |
| 358 | result.append(':'); |
| 359 | } |
| 360 | result.append(s); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // Only need to check for duplicates if list1 was non-empty. |
| 365 | boolean dupCheck = !first; |
| 366 | |
| 367 | if (list2 != null) { |
| 368 | for (String s : list2) { |
| 369 | if (dupCheck && ArrayUtils.contains(list1, s)) { |
| 370 | continue; |
| 371 | } |
| 372 | |
| 373 | if (first) { |
| 374 | first = false; |
| 375 | } else { |
| 376 | result.append(':'); |
| 377 | } |
| 378 | result.append(s); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | return result.toString(); |
| 383 | } |
| 384 | |
| 385 | public ClassLoader getClassLoader() { |
| 386 | synchronized (this) { |
| 387 | if (mClassLoader != null) { |
| 388 | return mClassLoader; |
| 389 | } |
| 390 | |
| 391 | if (mIncludeCode && !mPackageName.equals("android")) { |
| 392 | String zip = mAppDir; |
| 393 | |
| 394 | /* |
| 395 | * The following is a bit of a hack to inject |
| 396 | * instrumentation into the system: If the app |
| 397 | * being started matches one of the instrumentation names, |
| 398 | * then we combine both the "instrumentation" and |
| 399 | * "instrumented" app into the path, along with the |
| 400 | * concatenation of both apps' shared library lists. |
| 401 | */ |
| 402 | |
| 403 | String instrumentationAppDir = |
| 404 | mActivityThread.mInstrumentationAppDir; |
| 405 | String instrumentationAppPackage = |
| 406 | mActivityThread.mInstrumentationAppPackage; |
| 407 | String instrumentedAppDir = |
| 408 | mActivityThread.mInstrumentedAppDir; |
| 409 | String[] instrumentationLibs = null; |
| 410 | |
| 411 | if (mAppDir.equals(instrumentationAppDir) |
| 412 | || mAppDir.equals(instrumentedAppDir)) { |
| 413 | zip = instrumentationAppDir + ":" + instrumentedAppDir; |
| 414 | if (! instrumentedAppDir.equals(instrumentationAppDir)) { |
| 415 | instrumentationLibs = |
| 416 | getLibrariesFor(instrumentationAppPackage); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | if ((mSharedLibraries != null) || |
| 421 | (instrumentationLibs != null)) { |
| 422 | zip = |
| 423 | combineLibs(mSharedLibraries, instrumentationLibs) |
| 424 | + ':' + zip; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * With all the combination done (if necessary, actually |
| 429 | * create the class loader. |
| 430 | */ |
| 431 | |
| 432 | if (localLOGV) Log.v(TAG, "Class path: " + zip); |
| 433 | |
| 434 | mClassLoader = |
| 435 | ApplicationLoaders.getDefault().getClassLoader( |
| 436 | zip, mDataDir, mBaseClassLoader); |
| 437 | } else { |
| 438 | if (mBaseClassLoader == null) { |
| 439 | mClassLoader = ClassLoader.getSystemClassLoader(); |
| 440 | } else { |
| 441 | mClassLoader = mBaseClassLoader; |
| 442 | } |
| 443 | } |
| 444 | return mClassLoader; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | public String getAppDir() { |
| 449 | return mAppDir; |
| 450 | } |
| 451 | |
| 452 | public String getResDir() { |
| 453 | return mResDir; |
| 454 | } |
| 455 | |
| 456 | public String getDataDir() { |
| 457 | return mDataDir; |
| 458 | } |
| 459 | |
| 460 | public File getDataDirFile() { |
| 461 | return mDataDirFile; |
| 462 | } |
| 463 | |
| 464 | public AssetManager getAssets(ActivityThread mainThread) { |
| 465 | return getResources(mainThread).getAssets(); |
| 466 | } |
| 467 | |
| 468 | public Resources getResources(ActivityThread mainThread) { |
| 469 | if (mResources == null) { |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 470 | mResources = mainThread.getTopLevelResources(mResDir, this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | } |
| 472 | return mResources; |
| 473 | } |
| 474 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 475 | public Application makeApplication(boolean forceDefaultAppClass) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | if (mApplication != null) { |
| 477 | return mApplication; |
| 478 | } |
| 479 | |
| 480 | Application app = null; |
| 481 | |
| 482 | String appClass = mApplicationInfo.className; |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 483 | if (forceDefaultAppClass || (appClass == null)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | appClass = "android.app.Application"; |
| 485 | } |
| 486 | |
| 487 | try { |
| 488 | java.lang.ClassLoader cl = getClassLoader(); |
| 489 | ApplicationContext appContext = new ApplicationContext(); |
| 490 | appContext.init(this, null, mActivityThread); |
| 491 | app = mActivityThread.mInstrumentation.newApplication( |
| 492 | cl, appClass, appContext); |
| 493 | appContext.setOuterContext(app); |
| 494 | } catch (Exception e) { |
| 495 | if (!mActivityThread.mInstrumentation.onException(app, e)) { |
| 496 | throw new RuntimeException( |
| 497 | "Unable to instantiate application " + appClass |
| 498 | + ": " + e.toString(), e); |
| 499 | } |
| 500 | } |
| 501 | mActivityThread.mAllApplications.add(app); |
| 502 | return mApplication = app; |
| 503 | } |
| 504 | |
| 505 | public void removeContextRegistrations(Context context, |
| 506 | String who, String what) { |
| 507 | HashMap<BroadcastReceiver, ReceiverDispatcher> rmap = |
| 508 | mReceivers.remove(context); |
| 509 | if (rmap != null) { |
| 510 | Iterator<ReceiverDispatcher> it = rmap.values().iterator(); |
| 511 | while (it.hasNext()) { |
| 512 | ReceiverDispatcher rd = it.next(); |
| 513 | IntentReceiverLeaked leak = new IntentReceiverLeaked( |
| 514 | what + " " + who + " has leaked IntentReceiver " |
| 515 | + rd.getIntentReceiver() + " that was " + |
| 516 | "originally registered here. Are you missing a " + |
| 517 | "call to unregisterReceiver()?"); |
| 518 | leak.setStackTrace(rd.getLocation().getStackTrace()); |
| 519 | Log.e(TAG, leak.getMessage(), leak); |
| 520 | try { |
| 521 | ActivityManagerNative.getDefault().unregisterReceiver( |
| 522 | rd.getIIntentReceiver()); |
| 523 | } catch (RemoteException e) { |
| 524 | // system crashed, nothing we can do |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | mUnregisteredReceivers.remove(context); |
| 529 | //Log.i(TAG, "Receiver registrations: " + mReceivers); |
| 530 | HashMap<ServiceConnection, ServiceDispatcher> smap = |
| 531 | mServices.remove(context); |
| 532 | if (smap != null) { |
| 533 | Iterator<ServiceDispatcher> it = smap.values().iterator(); |
| 534 | while (it.hasNext()) { |
| 535 | ServiceDispatcher sd = it.next(); |
| 536 | ServiceConnectionLeaked leak = new ServiceConnectionLeaked( |
| 537 | what + " " + who + " has leaked ServiceConnection " |
| 538 | + sd.getServiceConnection() + " that was originally bound here"); |
| 539 | leak.setStackTrace(sd.getLocation().getStackTrace()); |
| 540 | Log.e(TAG, leak.getMessage(), leak); |
| 541 | try { |
| 542 | ActivityManagerNative.getDefault().unbindService( |
| 543 | sd.getIServiceConnection()); |
| 544 | } catch (RemoteException e) { |
| 545 | // system crashed, nothing we can do |
| 546 | } |
| 547 | sd.doForget(); |
| 548 | } |
| 549 | } |
| 550 | mUnboundServices.remove(context); |
| 551 | //Log.i(TAG, "Service registrations: " + mServices); |
| 552 | } |
| 553 | |
| 554 | public IIntentReceiver getReceiverDispatcher(BroadcastReceiver r, |
| 555 | Context context, Handler handler, |
| 556 | Instrumentation instrumentation, boolean registered) { |
| 557 | synchronized (mReceivers) { |
| 558 | ReceiverDispatcher rd = null; |
| 559 | HashMap<BroadcastReceiver, ReceiverDispatcher> map = null; |
| 560 | if (registered) { |
| 561 | map = mReceivers.get(context); |
| 562 | if (map != null) { |
| 563 | rd = map.get(r); |
| 564 | } |
| 565 | } |
| 566 | if (rd == null) { |
| 567 | rd = new ReceiverDispatcher(r, context, handler, |
| 568 | instrumentation, registered); |
| 569 | if (registered) { |
| 570 | if (map == null) { |
| 571 | map = new HashMap<BroadcastReceiver, ReceiverDispatcher>(); |
| 572 | mReceivers.put(context, map); |
| 573 | } |
| 574 | map.put(r, rd); |
| 575 | } |
| 576 | } else { |
| 577 | rd.validate(context, handler); |
| 578 | } |
| 579 | return rd.getIIntentReceiver(); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | public IIntentReceiver forgetReceiverDispatcher(Context context, |
| 584 | BroadcastReceiver r) { |
| 585 | synchronized (mReceivers) { |
| 586 | HashMap<BroadcastReceiver, ReceiverDispatcher> map = mReceivers.get(context); |
| 587 | ReceiverDispatcher rd = null; |
| 588 | if (map != null) { |
| 589 | rd = map.get(r); |
| 590 | if (rd != null) { |
| 591 | map.remove(r); |
| 592 | if (map.size() == 0) { |
| 593 | mReceivers.remove(context); |
| 594 | } |
| 595 | if (r.getDebugUnregister()) { |
| 596 | HashMap<BroadcastReceiver, ReceiverDispatcher> holder |
| 597 | = mUnregisteredReceivers.get(context); |
| 598 | if (holder == null) { |
| 599 | holder = new HashMap<BroadcastReceiver, ReceiverDispatcher>(); |
| 600 | mUnregisteredReceivers.put(context, holder); |
| 601 | } |
| 602 | RuntimeException ex = new IllegalArgumentException( |
| 603 | "Originally unregistered here:"); |
| 604 | ex.fillInStackTrace(); |
| 605 | rd.setUnregisterLocation(ex); |
| 606 | holder.put(r, rd); |
| 607 | } |
| 608 | return rd.getIIntentReceiver(); |
| 609 | } |
| 610 | } |
| 611 | HashMap<BroadcastReceiver, ReceiverDispatcher> holder |
| 612 | = mUnregisteredReceivers.get(context); |
| 613 | if (holder != null) { |
| 614 | rd = holder.get(r); |
| 615 | if (rd != null) { |
| 616 | RuntimeException ex = rd.getUnregisterLocation(); |
| 617 | throw new IllegalArgumentException( |
| 618 | "Unregistering Receiver " + r |
| 619 | + " that was already unregistered", ex); |
| 620 | } |
| 621 | } |
| 622 | if (context == null) { |
| 623 | throw new IllegalStateException("Unbinding Receiver " + r |
| 624 | + " from Context that is no longer in use: " + context); |
| 625 | } else { |
| 626 | throw new IllegalArgumentException("Receiver not registered: " + r); |
| 627 | } |
| 628 | |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | static final class ReceiverDispatcher { |
| 633 | |
| 634 | final static class InnerReceiver extends IIntentReceiver.Stub { |
| 635 | final WeakReference<ReceiverDispatcher> mDispatcher; |
| 636 | final ReceiverDispatcher mStrongRef; |
| 637 | |
| 638 | InnerReceiver(ReceiverDispatcher rd, boolean strong) { |
| 639 | mDispatcher = new WeakReference<ReceiverDispatcher>(rd); |
| 640 | mStrongRef = strong ? rd : null; |
| 641 | } |
| 642 | public void performReceive(Intent intent, int resultCode, |
| 643 | String data, Bundle extras, boolean ordered) { |
| 644 | ReceiverDispatcher rd = mDispatcher.get(); |
| 645 | if (DEBUG_BROADCAST) { |
| 646 | int seq = intent.getIntExtra("seq", -1); |
| 647 | Log.i(TAG, "Receiving broadcast " + intent.getAction() + " seq=" + seq |
| 648 | + " to " + rd); |
| 649 | } |
| 650 | if (rd != null) { |
| 651 | rd.performReceive(intent, resultCode, data, extras, ordered); |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | final IIntentReceiver.Stub mIIntentReceiver; |
| 657 | final BroadcastReceiver mReceiver; |
| 658 | final Context mContext; |
| 659 | final Handler mActivityThread; |
| 660 | final Instrumentation mInstrumentation; |
| 661 | final boolean mRegistered; |
| 662 | final IntentReceiverLeaked mLocation; |
| 663 | RuntimeException mUnregisterLocation; |
| 664 | |
| 665 | final class Args implements Runnable { |
| 666 | private Intent mCurIntent; |
| 667 | private int mCurCode; |
| 668 | private String mCurData; |
| 669 | private Bundle mCurMap; |
| 670 | private boolean mCurOrdered; |
| 671 | |
| 672 | public void run() { |
| 673 | BroadcastReceiver receiver = mReceiver; |
| 674 | if (DEBUG_BROADCAST) { |
| 675 | int seq = mCurIntent.getIntExtra("seq", -1); |
| 676 | Log.i(TAG, "Dispathing broadcast " + mCurIntent.getAction() + " seq=" + seq |
| 677 | + " to " + mReceiver); |
| 678 | } |
| 679 | if (receiver == null) { |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | IActivityManager mgr = ActivityManagerNative.getDefault(); |
| 684 | Intent intent = mCurIntent; |
| 685 | mCurIntent = null; |
| 686 | try { |
| 687 | ClassLoader cl = mReceiver.getClass().getClassLoader(); |
| 688 | intent.setExtrasClassLoader(cl); |
| 689 | if (mCurMap != null) { |
| 690 | mCurMap.setClassLoader(cl); |
| 691 | } |
| 692 | receiver.setOrderedHint(true); |
| 693 | receiver.setResult(mCurCode, mCurData, mCurMap); |
| 694 | receiver.clearAbortBroadcast(); |
| 695 | receiver.setOrderedHint(mCurOrdered); |
| 696 | receiver.onReceive(mContext, intent); |
| 697 | } catch (Exception e) { |
| 698 | if (mRegistered && mCurOrdered) { |
| 699 | try { |
| 700 | mgr.finishReceiver(mIIntentReceiver, |
| 701 | mCurCode, mCurData, mCurMap, false); |
| 702 | } catch (RemoteException ex) { |
| 703 | } |
| 704 | } |
| 705 | if (mInstrumentation == null || |
| 706 | !mInstrumentation.onException(mReceiver, e)) { |
| 707 | throw new RuntimeException( |
| 708 | "Error receiving broadcast " + intent |
| 709 | + " in " + mReceiver, e); |
| 710 | } |
| 711 | } |
| 712 | if (mRegistered && mCurOrdered) { |
| 713 | try { |
| 714 | mgr.finishReceiver(mIIntentReceiver, |
| 715 | receiver.getResultCode(), |
| 716 | receiver.getResultData(), |
| 717 | receiver.getResultExtras(false), |
| 718 | receiver.getAbortBroadcast()); |
| 719 | } catch (RemoteException ex) { |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | ReceiverDispatcher(BroadcastReceiver receiver, Context context, |
| 726 | Handler activityThread, Instrumentation instrumentation, |
| 727 | boolean registered) { |
| 728 | if (activityThread == null) { |
| 729 | throw new NullPointerException("Handler must not be null"); |
| 730 | } |
| 731 | |
| 732 | mIIntentReceiver = new InnerReceiver(this, !registered); |
| 733 | mReceiver = receiver; |
| 734 | mContext = context; |
| 735 | mActivityThread = activityThread; |
| 736 | mInstrumentation = instrumentation; |
| 737 | mRegistered = registered; |
| 738 | mLocation = new IntentReceiverLeaked(null); |
| 739 | mLocation.fillInStackTrace(); |
| 740 | } |
| 741 | |
| 742 | void validate(Context context, Handler activityThread) { |
| 743 | if (mContext != context) { |
| 744 | throw new IllegalStateException( |
| 745 | "Receiver " + mReceiver + |
| 746 | " registered with differing Context (was " + |
| 747 | mContext + " now " + context + ")"); |
| 748 | } |
| 749 | if (mActivityThread != activityThread) { |
| 750 | throw new IllegalStateException( |
| 751 | "Receiver " + mReceiver + |
| 752 | " registered with differing handler (was " + |
| 753 | mActivityThread + " now " + activityThread + ")"); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | IntentReceiverLeaked getLocation() { |
| 758 | return mLocation; |
| 759 | } |
| 760 | |
| 761 | BroadcastReceiver getIntentReceiver() { |
| 762 | return mReceiver; |
| 763 | } |
| 764 | |
| 765 | IIntentReceiver getIIntentReceiver() { |
| 766 | return mIIntentReceiver; |
| 767 | } |
| 768 | |
| 769 | void setUnregisterLocation(RuntimeException ex) { |
| 770 | mUnregisterLocation = ex; |
| 771 | } |
| 772 | |
| 773 | RuntimeException getUnregisterLocation() { |
| 774 | return mUnregisterLocation; |
| 775 | } |
| 776 | |
| 777 | public void performReceive(Intent intent, int resultCode, |
| 778 | String data, Bundle extras, boolean ordered) { |
| 779 | if (DEBUG_BROADCAST) { |
| 780 | int seq = intent.getIntExtra("seq", -1); |
| 781 | Log.i(TAG, "Enqueueing broadcast " + intent.getAction() + " seq=" + seq |
| 782 | + " to " + mReceiver); |
| 783 | } |
| 784 | Args args = new Args(); |
| 785 | args.mCurIntent = intent; |
| 786 | args.mCurCode = resultCode; |
| 787 | args.mCurData = data; |
| 788 | args.mCurMap = extras; |
| 789 | args.mCurOrdered = ordered; |
| 790 | if (!mActivityThread.post(args)) { |
| 791 | if (mRegistered) { |
| 792 | IActivityManager mgr = ActivityManagerNative.getDefault(); |
| 793 | try { |
| 794 | mgr.finishReceiver(mIIntentReceiver, args.mCurCode, |
| 795 | args.mCurData, args.mCurMap, false); |
| 796 | } catch (RemoteException ex) { |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | } |
| 803 | |
| 804 | public final IServiceConnection getServiceDispatcher(ServiceConnection c, |
| 805 | Context context, Handler handler, int flags) { |
| 806 | synchronized (mServices) { |
| 807 | ServiceDispatcher sd = null; |
| 808 | HashMap<ServiceConnection, ServiceDispatcher> map = mServices.get(context); |
| 809 | if (map != null) { |
| 810 | sd = map.get(c); |
| 811 | } |
| 812 | if (sd == null) { |
| 813 | sd = new ServiceDispatcher(c, context, handler, flags); |
| 814 | if (map == null) { |
| 815 | map = new HashMap<ServiceConnection, ServiceDispatcher>(); |
| 816 | mServices.put(context, map); |
| 817 | } |
| 818 | map.put(c, sd); |
| 819 | } else { |
| 820 | sd.validate(context, handler); |
| 821 | } |
| 822 | return sd.getIServiceConnection(); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | public final IServiceConnection forgetServiceDispatcher(Context context, |
| 827 | ServiceConnection c) { |
| 828 | synchronized (mServices) { |
| 829 | HashMap<ServiceConnection, ServiceDispatcher> map |
| 830 | = mServices.get(context); |
| 831 | ServiceDispatcher sd = null; |
| 832 | if (map != null) { |
| 833 | sd = map.get(c); |
| 834 | if (sd != null) { |
| 835 | map.remove(c); |
| 836 | sd.doForget(); |
| 837 | if (map.size() == 0) { |
| 838 | mServices.remove(context); |
| 839 | } |
| 840 | if ((sd.getFlags()&Context.BIND_DEBUG_UNBIND) != 0) { |
| 841 | HashMap<ServiceConnection, ServiceDispatcher> holder |
| 842 | = mUnboundServices.get(context); |
| 843 | if (holder == null) { |
| 844 | holder = new HashMap<ServiceConnection, ServiceDispatcher>(); |
| 845 | mUnboundServices.put(context, holder); |
| 846 | } |
| 847 | RuntimeException ex = new IllegalArgumentException( |
| 848 | "Originally unbound here:"); |
| 849 | ex.fillInStackTrace(); |
| 850 | sd.setUnbindLocation(ex); |
| 851 | holder.put(c, sd); |
| 852 | } |
| 853 | return sd.getIServiceConnection(); |
| 854 | } |
| 855 | } |
| 856 | HashMap<ServiceConnection, ServiceDispatcher> holder |
| 857 | = mUnboundServices.get(context); |
| 858 | if (holder != null) { |
| 859 | sd = holder.get(c); |
| 860 | if (sd != null) { |
| 861 | RuntimeException ex = sd.getUnbindLocation(); |
| 862 | throw new IllegalArgumentException( |
| 863 | "Unbinding Service " + c |
| 864 | + " that was already unbound", ex); |
| 865 | } |
| 866 | } |
| 867 | if (context == null) { |
| 868 | throw new IllegalStateException("Unbinding Service " + c |
| 869 | + " from Context that is no longer in use: " + context); |
| 870 | } else { |
| 871 | throw new IllegalArgumentException("Service not registered: " + c); |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | static final class ServiceDispatcher { |
| 877 | private final InnerConnection mIServiceConnection; |
| 878 | private final ServiceConnection mConnection; |
| 879 | private final Context mContext; |
| 880 | private final Handler mActivityThread; |
| 881 | private final ServiceConnectionLeaked mLocation; |
| 882 | private final int mFlags; |
| 883 | |
| 884 | private RuntimeException mUnbindLocation; |
| 885 | |
| 886 | private boolean mDied; |
| 887 | |
| 888 | private static class ConnectionInfo { |
| 889 | IBinder binder; |
| 890 | IBinder.DeathRecipient deathMonitor; |
| 891 | } |
| 892 | |
| 893 | private static class InnerConnection extends IServiceConnection.Stub { |
| 894 | final WeakReference<ServiceDispatcher> mDispatcher; |
| 895 | |
| 896 | InnerConnection(ServiceDispatcher sd) { |
| 897 | mDispatcher = new WeakReference<ServiceDispatcher>(sd); |
| 898 | } |
| 899 | |
| 900 | public void connected(ComponentName name, IBinder service) throws RemoteException { |
| 901 | ServiceDispatcher sd = mDispatcher.get(); |
| 902 | if (sd != null) { |
| 903 | sd.connected(name, service); |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | private final HashMap<ComponentName, ConnectionInfo> mActiveConnections |
| 909 | = new HashMap<ComponentName, ConnectionInfo>(); |
| 910 | |
| 911 | ServiceDispatcher(ServiceConnection conn, |
| 912 | Context context, Handler activityThread, int flags) { |
| 913 | mIServiceConnection = new InnerConnection(this); |
| 914 | mConnection = conn; |
| 915 | mContext = context; |
| 916 | mActivityThread = activityThread; |
| 917 | mLocation = new ServiceConnectionLeaked(null); |
| 918 | mLocation.fillInStackTrace(); |
| 919 | mFlags = flags; |
| 920 | } |
| 921 | |
| 922 | void validate(Context context, Handler activityThread) { |
| 923 | if (mContext != context) { |
| 924 | throw new RuntimeException( |
| 925 | "ServiceConnection " + mConnection + |
| 926 | " registered with differing Context (was " + |
| 927 | mContext + " now " + context + ")"); |
| 928 | } |
| 929 | if (mActivityThread != activityThread) { |
| 930 | throw new RuntimeException( |
| 931 | "ServiceConnection " + mConnection + |
| 932 | " registered with differing handler (was " + |
| 933 | mActivityThread + " now " + activityThread + ")"); |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | void doForget() { |
| 938 | synchronized(this) { |
| 939 | Iterator<ConnectionInfo> it = mActiveConnections.values().iterator(); |
| 940 | while (it.hasNext()) { |
| 941 | ConnectionInfo ci = it.next(); |
| 942 | ci.binder.unlinkToDeath(ci.deathMonitor, 0); |
| 943 | } |
| 944 | mActiveConnections.clear(); |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | ServiceConnectionLeaked getLocation() { |
| 949 | return mLocation; |
| 950 | } |
| 951 | |
| 952 | ServiceConnection getServiceConnection() { |
| 953 | return mConnection; |
| 954 | } |
| 955 | |
| 956 | IServiceConnection getIServiceConnection() { |
| 957 | return mIServiceConnection; |
| 958 | } |
| 959 | |
| 960 | int getFlags() { |
| 961 | return mFlags; |
| 962 | } |
| 963 | |
| 964 | void setUnbindLocation(RuntimeException ex) { |
| 965 | mUnbindLocation = ex; |
| 966 | } |
| 967 | |
| 968 | RuntimeException getUnbindLocation() { |
| 969 | return mUnbindLocation; |
| 970 | } |
| 971 | |
| 972 | public void connected(ComponentName name, IBinder service) { |
| 973 | if (mActivityThread != null) { |
| 974 | mActivityThread.post(new RunConnection(name, service, 0)); |
| 975 | } else { |
| 976 | doConnected(name, service); |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | public void death(ComponentName name, IBinder service) { |
| 981 | ConnectionInfo old; |
| 982 | |
| 983 | synchronized (this) { |
| 984 | mDied = true; |
| 985 | old = mActiveConnections.remove(name); |
| 986 | if (old == null || old.binder != service) { |
| 987 | // Death for someone different than who we last |
| 988 | // reported... just ignore it. |
| 989 | return; |
| 990 | } |
| 991 | old.binder.unlinkToDeath(old.deathMonitor, 0); |
| 992 | } |
| 993 | |
| 994 | if (mActivityThread != null) { |
| 995 | mActivityThread.post(new RunConnection(name, service, 1)); |
| 996 | } else { |
| 997 | doDeath(name, service); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | public void doConnected(ComponentName name, IBinder service) { |
| 1002 | ConnectionInfo old; |
| 1003 | ConnectionInfo info; |
| 1004 | |
| 1005 | synchronized (this) { |
| 1006 | old = mActiveConnections.get(name); |
| 1007 | if (old != null && old.binder == service) { |
| 1008 | // Huh, already have this one. Oh well! |
| 1009 | return; |
| 1010 | } |
| 1011 | |
| 1012 | if (service != null) { |
| 1013 | // A new service is being connected... set it all up. |
| 1014 | mDied = false; |
| 1015 | info = new ConnectionInfo(); |
| 1016 | info.binder = service; |
| 1017 | info.deathMonitor = new DeathMonitor(name, service); |
| 1018 | try { |
| 1019 | service.linkToDeath(info.deathMonitor, 0); |
| 1020 | mActiveConnections.put(name, info); |
| 1021 | } catch (RemoteException e) { |
| 1022 | // This service was dead before we got it... just |
| 1023 | // don't do anything with it. |
| 1024 | mActiveConnections.remove(name); |
| 1025 | return; |
| 1026 | } |
| 1027 | |
| 1028 | } else { |
| 1029 | // The named service is being disconnected... clean up. |
| 1030 | mActiveConnections.remove(name); |
| 1031 | } |
| 1032 | |
| 1033 | if (old != null) { |
| 1034 | old.binder.unlinkToDeath(old.deathMonitor, 0); |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | // If there was an old service, it is not disconnected. |
| 1039 | if (old != null) { |
| 1040 | mConnection.onServiceDisconnected(name); |
| 1041 | } |
| 1042 | // If there is a new service, it is now connected. |
| 1043 | if (service != null) { |
| 1044 | mConnection.onServiceConnected(name, service); |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | public void doDeath(ComponentName name, IBinder service) { |
| 1049 | mConnection.onServiceDisconnected(name); |
| 1050 | } |
| 1051 | |
| 1052 | private final class RunConnection implements Runnable { |
| 1053 | RunConnection(ComponentName name, IBinder service, int command) { |
| 1054 | mName = name; |
| 1055 | mService = service; |
| 1056 | mCommand = command; |
| 1057 | } |
| 1058 | |
| 1059 | public void run() { |
| 1060 | if (mCommand == 0) { |
| 1061 | doConnected(mName, mService); |
| 1062 | } else if (mCommand == 1) { |
| 1063 | doDeath(mName, mService); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | final ComponentName mName; |
| 1068 | final IBinder mService; |
| 1069 | final int mCommand; |
| 1070 | } |
| 1071 | |
| 1072 | private final class DeathMonitor implements IBinder.DeathRecipient |
| 1073 | { |
| 1074 | DeathMonitor(ComponentName name, IBinder service) { |
| 1075 | mName = name; |
| 1076 | mService = service; |
| 1077 | } |
| 1078 | |
| 1079 | public void binderDied() { |
| 1080 | death(mName, mService); |
| 1081 | } |
| 1082 | |
| 1083 | final ComponentName mName; |
| 1084 | final IBinder mService; |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | private static ApplicationContext mSystemContext = null; |
| 1090 | |
| 1091 | private static final class ActivityRecord { |
| 1092 | IBinder token; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 1093 | int ident; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1094 | Intent intent; |
| 1095 | Bundle state; |
| 1096 | Activity activity; |
| 1097 | Window window; |
| 1098 | Activity parent; |
| 1099 | String embeddedID; |
| 1100 | Object lastNonConfigurationInstance; |
| 1101 | HashMap<String,Object> lastNonConfigurationChildInstances; |
| 1102 | boolean paused; |
| 1103 | boolean stopped; |
| 1104 | boolean hideForNow; |
| 1105 | Configuration newConfig; |
| 1106 | ActivityRecord nextIdle; |
| 1107 | |
| 1108 | ActivityInfo activityInfo; |
| 1109 | PackageInfo packageInfo; |
| 1110 | |
| 1111 | List<ResultInfo> pendingResults; |
| 1112 | List<Intent> pendingIntents; |
| 1113 | |
| 1114 | boolean startsNotResumed; |
| 1115 | boolean isForward; |
| 1116 | |
| 1117 | ActivityRecord() { |
| 1118 | parent = null; |
| 1119 | embeddedID = null; |
| 1120 | paused = false; |
| 1121 | stopped = false; |
| 1122 | hideForNow = false; |
| 1123 | nextIdle = null; |
| 1124 | } |
| 1125 | |
| 1126 | public String toString() { |
| 1127 | ComponentName componentName = intent.getComponent(); |
| 1128 | return "ActivityRecord{" |
| 1129 | + Integer.toHexString(System.identityHashCode(this)) |
| 1130 | + " token=" + token + " " + (componentName == null |
| 1131 | ? "no component name" : componentName.toShortString()) |
| 1132 | + "}"; |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | private final class ProviderRecord implements IBinder.DeathRecipient { |
| 1137 | final String mName; |
| 1138 | final IContentProvider mProvider; |
| 1139 | final ContentProvider mLocalProvider; |
| 1140 | |
| 1141 | ProviderRecord(String name, IContentProvider provider, |
| 1142 | ContentProvider localProvider) { |
| 1143 | mName = name; |
| 1144 | mProvider = provider; |
| 1145 | mLocalProvider = localProvider; |
| 1146 | } |
| 1147 | |
| 1148 | public void binderDied() { |
| 1149 | removeDeadProvider(mName, mProvider); |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | private static final class NewIntentData { |
| 1154 | List<Intent> intents; |
| 1155 | IBinder token; |
| 1156 | public String toString() { |
| 1157 | return "NewIntentData{intents=" + intents + " token=" + token + "}"; |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | private static final class ReceiverData { |
| 1162 | Intent intent; |
| 1163 | ActivityInfo info; |
| 1164 | int resultCode; |
| 1165 | String resultData; |
| 1166 | Bundle resultExtras; |
| 1167 | boolean sync; |
| 1168 | boolean resultAbort; |
| 1169 | public String toString() { |
| 1170 | return "ReceiverData{intent=" + intent + " packageName=" + |
| 1171 | info.packageName + " resultCode=" + resultCode |
| 1172 | + " resultData=" + resultData + " resultExtras=" + resultExtras + "}"; |
| 1173 | } |
| 1174 | } |
| 1175 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1176 | private static final class CreateBackupAgentData { |
| 1177 | ApplicationInfo appInfo; |
| 1178 | int backupMode; |
| 1179 | public String toString() { |
| 1180 | return "CreateBackupAgentData{appInfo=" + appInfo |
| 1181 | + " backupAgent=" + appInfo.backupAgentName |
| 1182 | + " mode=" + backupMode + "}"; |
| 1183 | } |
| 1184 | } |
| 1185 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1186 | private static final class CreateServiceData { |
| 1187 | IBinder token; |
| 1188 | ServiceInfo info; |
| 1189 | Intent intent; |
| 1190 | public String toString() { |
| 1191 | return "CreateServiceData{token=" + token + " className=" |
| 1192 | + info.name + " packageName=" + info.packageName |
| 1193 | + " intent=" + intent + "}"; |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | private static final class BindServiceData { |
| 1198 | IBinder token; |
| 1199 | Intent intent; |
| 1200 | boolean rebind; |
| 1201 | public String toString() { |
| 1202 | return "BindServiceData{token=" + token + " intent=" + intent + "}"; |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | private static final class ServiceArgsData { |
| 1207 | IBinder token; |
| 1208 | int startId; |
| 1209 | Intent args; |
| 1210 | public String toString() { |
| 1211 | return "ServiceArgsData{token=" + token + " startId=" + startId |
| 1212 | + " args=" + args + "}"; |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | private static final class AppBindData { |
| 1217 | PackageInfo info; |
| 1218 | String processName; |
| 1219 | ApplicationInfo appInfo; |
| 1220 | List<ProviderInfo> providers; |
| 1221 | ComponentName instrumentationName; |
| 1222 | String profileFile; |
| 1223 | Bundle instrumentationArgs; |
| 1224 | IInstrumentationWatcher instrumentationWatcher; |
| 1225 | int debugMode; |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1226 | boolean restrictedBackupMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1227 | Configuration config; |
| 1228 | boolean handlingProfiling; |
| 1229 | public String toString() { |
| 1230 | return "AppBindData{appInfo=" + appInfo + "}"; |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | private static final class DumpServiceInfo { |
| 1235 | FileDescriptor fd; |
| 1236 | IBinder service; |
| 1237 | String[] args; |
| 1238 | boolean dumped; |
| 1239 | } |
| 1240 | |
| 1241 | private static final class ResultData { |
| 1242 | IBinder token; |
| 1243 | List<ResultInfo> results; |
| 1244 | public String toString() { |
| 1245 | return "ResultData{token=" + token + " results" + results + "}"; |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | private static final class ContextCleanupInfo { |
| 1250 | ApplicationContext context; |
| 1251 | String what; |
| 1252 | String who; |
| 1253 | } |
| 1254 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 1255 | private static final class ProfilerControlData { |
| 1256 | String path; |
| 1257 | ParcelFileDescriptor fd; |
| 1258 | } |
| 1259 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1260 | private final class ApplicationThread extends ApplicationThreadNative { |
| 1261 | private static final String HEAP_COLUMN = "%17s %8s %8s %8s %8s"; |
| 1262 | private static final String ONE_COUNT_COLUMN = "%17s %8d"; |
| 1263 | private static final String TWO_COUNT_COLUMNS = "%17s %8d %17s %8d"; |
| 1264 | |
| 1265 | // Formatting for checkin service - update version if row format changes |
| 1266 | private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1; |
| 1267 | |
| 1268 | public final void schedulePauseActivity(IBinder token, boolean finished, |
| 1269 | boolean userLeaving, int configChanges) { |
| 1270 | queueOrSendMessage( |
| 1271 | finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY, |
| 1272 | token, |
| 1273 | (userLeaving ? 1 : 0), |
| 1274 | configChanges); |
| 1275 | } |
| 1276 | |
| 1277 | public final void scheduleStopActivity(IBinder token, boolean showWindow, |
| 1278 | int configChanges) { |
| 1279 | queueOrSendMessage( |
| 1280 | showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE, |
| 1281 | token, 0, configChanges); |
| 1282 | } |
| 1283 | |
| 1284 | public final void scheduleWindowVisibility(IBinder token, boolean showWindow) { |
| 1285 | queueOrSendMessage( |
| 1286 | showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW, |
| 1287 | token); |
| 1288 | } |
| 1289 | |
| 1290 | public final void scheduleResumeActivity(IBinder token, boolean isForward) { |
| 1291 | queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0); |
| 1292 | } |
| 1293 | |
| 1294 | public final void scheduleSendResult(IBinder token, List<ResultInfo> results) { |
| 1295 | ResultData res = new ResultData(); |
| 1296 | res.token = token; |
| 1297 | res.results = results; |
| 1298 | queueOrSendMessage(H.SEND_RESULT, res); |
| 1299 | } |
| 1300 | |
| 1301 | // we use token to identify this activity without having to send the |
| 1302 | // activity itself back to the activity manager. (matters more with ipc) |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 1303 | public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1304 | ActivityInfo info, Bundle state, List<ResultInfo> pendingResults, |
| 1305 | List<Intent> pendingNewIntents, boolean notResumed, boolean isForward) { |
| 1306 | ActivityRecord r = new ActivityRecord(); |
| 1307 | |
| 1308 | r.token = token; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 1309 | r.ident = ident; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1310 | r.intent = intent; |
| 1311 | r.activityInfo = info; |
| 1312 | r.state = state; |
| 1313 | |
| 1314 | r.pendingResults = pendingResults; |
| 1315 | r.pendingIntents = pendingNewIntents; |
| 1316 | |
| 1317 | r.startsNotResumed = notResumed; |
| 1318 | r.isForward = isForward; |
| 1319 | |
| 1320 | queueOrSendMessage(H.LAUNCH_ACTIVITY, r); |
| 1321 | } |
| 1322 | |
| 1323 | public final void scheduleRelaunchActivity(IBinder token, |
| 1324 | List<ResultInfo> pendingResults, List<Intent> pendingNewIntents, |
| 1325 | int configChanges, boolean notResumed) { |
| 1326 | ActivityRecord r = new ActivityRecord(); |
| 1327 | |
| 1328 | r.token = token; |
| 1329 | r.pendingResults = pendingResults; |
| 1330 | r.pendingIntents = pendingNewIntents; |
| 1331 | r.startsNotResumed = notResumed; |
| 1332 | |
| 1333 | synchronized (mRelaunchingActivities) { |
| 1334 | mRelaunchingActivities.add(r); |
| 1335 | } |
| 1336 | |
| 1337 | queueOrSendMessage(H.RELAUNCH_ACTIVITY, r, configChanges); |
| 1338 | } |
| 1339 | |
| 1340 | public final void scheduleNewIntent(List<Intent> intents, IBinder token) { |
| 1341 | NewIntentData data = new NewIntentData(); |
| 1342 | data.intents = intents; |
| 1343 | data.token = token; |
| 1344 | |
| 1345 | queueOrSendMessage(H.NEW_INTENT, data); |
| 1346 | } |
| 1347 | |
| 1348 | public final void scheduleDestroyActivity(IBinder token, boolean finishing, |
| 1349 | int configChanges) { |
| 1350 | queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0, |
| 1351 | configChanges); |
| 1352 | } |
| 1353 | |
| 1354 | public final void scheduleReceiver(Intent intent, ActivityInfo info, |
| 1355 | int resultCode, String data, Bundle extras, boolean sync) { |
| 1356 | ReceiverData r = new ReceiverData(); |
| 1357 | |
| 1358 | r.intent = intent; |
| 1359 | r.info = info; |
| 1360 | r.resultCode = resultCode; |
| 1361 | r.resultData = data; |
| 1362 | r.resultExtras = extras; |
| 1363 | r.sync = sync; |
| 1364 | |
| 1365 | queueOrSendMessage(H.RECEIVER, r); |
| 1366 | } |
| 1367 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1368 | public final void scheduleCreateBackupAgent(ApplicationInfo app, int backupMode) { |
| 1369 | CreateBackupAgentData d = new CreateBackupAgentData(); |
| 1370 | d.appInfo = app; |
| 1371 | d.backupMode = backupMode; |
| 1372 | |
| 1373 | queueOrSendMessage(H.CREATE_BACKUP_AGENT, d); |
| 1374 | } |
| 1375 | |
| 1376 | public final void scheduleDestroyBackupAgent(ApplicationInfo app) { |
| 1377 | CreateBackupAgentData d = new CreateBackupAgentData(); |
| 1378 | d.appInfo = app; |
| 1379 | |
| 1380 | queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d); |
| 1381 | } |
| 1382 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1383 | public final void scheduleCreateService(IBinder token, |
| 1384 | ServiceInfo info) { |
| 1385 | CreateServiceData s = new CreateServiceData(); |
| 1386 | s.token = token; |
| 1387 | s.info = info; |
| 1388 | |
| 1389 | queueOrSendMessage(H.CREATE_SERVICE, s); |
| 1390 | } |
| 1391 | |
| 1392 | public final void scheduleBindService(IBinder token, Intent intent, |
| 1393 | boolean rebind) { |
| 1394 | BindServiceData s = new BindServiceData(); |
| 1395 | s.token = token; |
| 1396 | s.intent = intent; |
| 1397 | s.rebind = rebind; |
| 1398 | |
| 1399 | queueOrSendMessage(H.BIND_SERVICE, s); |
| 1400 | } |
| 1401 | |
| 1402 | public final void scheduleUnbindService(IBinder token, Intent intent) { |
| 1403 | BindServiceData s = new BindServiceData(); |
| 1404 | s.token = token; |
| 1405 | s.intent = intent; |
| 1406 | |
| 1407 | queueOrSendMessage(H.UNBIND_SERVICE, s); |
| 1408 | } |
| 1409 | |
| 1410 | public final void scheduleServiceArgs(IBinder token, int startId, |
| 1411 | Intent args) { |
| 1412 | ServiceArgsData s = new ServiceArgsData(); |
| 1413 | s.token = token; |
| 1414 | s.startId = startId; |
| 1415 | s.args = args; |
| 1416 | |
| 1417 | queueOrSendMessage(H.SERVICE_ARGS, s); |
| 1418 | } |
| 1419 | |
| 1420 | public final void scheduleStopService(IBinder token) { |
| 1421 | queueOrSendMessage(H.STOP_SERVICE, token); |
| 1422 | } |
| 1423 | |
| 1424 | public final void bindApplication(String processName, |
| 1425 | ApplicationInfo appInfo, List<ProviderInfo> providers, |
| 1426 | ComponentName instrumentationName, String profileFile, |
| 1427 | Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher, |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1428 | int debugMode, boolean isRestrictedBackupMode, Configuration config, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1429 | Map<String, IBinder> services) { |
| 1430 | Process.setArgV0(processName); |
| 1431 | |
| 1432 | if (services != null) { |
| 1433 | // Setup the service cache in the ServiceManager |
| 1434 | ServiceManager.initServiceCache(services); |
| 1435 | } |
| 1436 | |
| 1437 | AppBindData data = new AppBindData(); |
| 1438 | data.processName = processName; |
| 1439 | data.appInfo = appInfo; |
| 1440 | data.providers = providers; |
| 1441 | data.instrumentationName = instrumentationName; |
| 1442 | data.profileFile = profileFile; |
| 1443 | data.instrumentationArgs = instrumentationArgs; |
| 1444 | data.instrumentationWatcher = instrumentationWatcher; |
| 1445 | data.debugMode = debugMode; |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1446 | data.restrictedBackupMode = isRestrictedBackupMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1447 | data.config = config; |
| 1448 | queueOrSendMessage(H.BIND_APPLICATION, data); |
| 1449 | } |
| 1450 | |
| 1451 | public final void scheduleExit() { |
| 1452 | queueOrSendMessage(H.EXIT_APPLICATION, null); |
| 1453 | } |
| 1454 | |
| 1455 | public void requestThumbnail(IBinder token) { |
| 1456 | queueOrSendMessage(H.REQUEST_THUMBNAIL, token); |
| 1457 | } |
| 1458 | |
| 1459 | public void scheduleConfigurationChanged(Configuration config) { |
| 1460 | synchronized (mRelaunchingActivities) { |
| 1461 | mPendingConfiguration = config; |
| 1462 | } |
| 1463 | queueOrSendMessage(H.CONFIGURATION_CHANGED, config); |
| 1464 | } |
| 1465 | |
| 1466 | public void updateTimeZone() { |
| 1467 | TimeZone.setDefault(null); |
| 1468 | } |
| 1469 | |
| 1470 | public void processInBackground() { |
| 1471 | mH.removeMessages(H.GC_WHEN_IDLE); |
| 1472 | mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE)); |
| 1473 | } |
| 1474 | |
| 1475 | public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) { |
| 1476 | DumpServiceInfo data = new DumpServiceInfo(); |
| 1477 | data.fd = fd; |
| 1478 | data.service = servicetoken; |
| 1479 | data.args = args; |
| 1480 | data.dumped = false; |
| 1481 | queueOrSendMessage(H.DUMP_SERVICE, data); |
| 1482 | synchronized (data) { |
| 1483 | while (!data.dumped) { |
| 1484 | try { |
| 1485 | data.wait(); |
| 1486 | } catch (InterruptedException e) { |
| 1487 | // no need to do anything here, we will keep waiting until |
| 1488 | // dumped is set |
| 1489 | } |
| 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | // This function exists to make sure all receiver dispatching is |
| 1495 | // correctly ordered, since these are one-way calls and the binder driver |
| 1496 | // applies transaction ordering per object for such calls. |
| 1497 | public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent, |
| 1498 | int resultCode, String dataStr, Bundle extras, boolean ordered) |
| 1499 | throws RemoteException { |
| 1500 | receiver.performReceive(intent, resultCode, dataStr, extras, ordered); |
| 1501 | } |
| 1502 | |
| 1503 | public void scheduleLowMemory() { |
| 1504 | queueOrSendMessage(H.LOW_MEMORY, null); |
| 1505 | } |
| 1506 | |
| 1507 | public void scheduleActivityConfigurationChanged(IBinder token) { |
| 1508 | queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token); |
| 1509 | } |
| 1510 | |
| 1511 | public void requestPss() { |
| 1512 | try { |
| 1513 | ActivityManagerNative.getDefault().reportPss(this, |
| 1514 | (int)Process.getPss(Process.myPid())); |
| 1515 | } catch (RemoteException e) { |
| 1516 | } |
| 1517 | } |
| 1518 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 1519 | public void profilerControl(boolean start, String path, ParcelFileDescriptor fd) { |
| 1520 | ProfilerControlData pcd = new ProfilerControlData(); |
| 1521 | pcd.path = path; |
| 1522 | pcd.fd = fd; |
| 1523 | queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 1524 | } |
| 1525 | |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 1526 | public void setSchedulingGroup(int group) { |
| 1527 | // Note: do this immediately, since going into the foreground |
| 1528 | // should happen regardless of what pending work we have to do |
| 1529 | // and the activity manager will wait for us to report back that |
| 1530 | // we are done before sending us to the background. |
| 1531 | try { |
| 1532 | Process.setProcessGroup(Process.myPid(), group); |
| 1533 | } catch (Exception e) { |
| 1534 | Log.w(TAG, "Failed setting process group to " + group, e); |
| 1535 | } |
| 1536 | } |
| 1537 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1538 | @Override |
| 1539 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1540 | long nativeMax = Debug.getNativeHeapSize() / 1024; |
| 1541 | long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024; |
| 1542 | long nativeFree = Debug.getNativeHeapFreeSize() / 1024; |
| 1543 | |
| 1544 | Debug.MemoryInfo memInfo = new Debug.MemoryInfo(); |
| 1545 | Debug.getMemoryInfo(memInfo); |
| 1546 | |
| 1547 | final int nativeShared = memInfo.nativeSharedDirty; |
| 1548 | final int dalvikShared = memInfo.dalvikSharedDirty; |
| 1549 | final int otherShared = memInfo.otherSharedDirty; |
| 1550 | |
| 1551 | final int nativePrivate = memInfo.nativePrivateDirty; |
| 1552 | final int dalvikPrivate = memInfo.dalvikPrivateDirty; |
| 1553 | final int otherPrivate = memInfo.otherPrivateDirty; |
| 1554 | |
| 1555 | Runtime runtime = Runtime.getRuntime(); |
| 1556 | |
| 1557 | long dalvikMax = runtime.totalMemory() / 1024; |
| 1558 | long dalvikFree = runtime.freeMemory() / 1024; |
| 1559 | long dalvikAllocated = dalvikMax - dalvikFree; |
| 1560 | long viewInstanceCount = ViewDebug.getViewInstanceCount(); |
| 1561 | long viewRootInstanceCount = ViewDebug.getViewRootInstanceCount(); |
| 1562 | long appContextInstanceCount = ApplicationContext.getInstanceCount(); |
| 1563 | long activityInstanceCount = Activity.getInstanceCount(); |
| 1564 | int globalAssetCount = AssetManager.getGlobalAssetCount(); |
| 1565 | int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount(); |
| 1566 | int binderLocalObjectCount = Debug.getBinderLocalObjectCount(); |
| 1567 | int binderProxyObjectCount = Debug.getBinderProxyObjectCount(); |
| 1568 | int binderDeathObjectCount = Debug.getBinderDeathObjectCount(); |
| 1569 | int openSslSocketCount = OpenSSLSocketImpl.getInstanceCount(); |
| 1570 | long sqliteAllocated = SQLiteDebug.getHeapAllocatedSize() / 1024; |
| 1571 | SQLiteDebug.PagerStats stats = new SQLiteDebug.PagerStats(); |
| 1572 | SQLiteDebug.getPagerStats(stats); |
| 1573 | |
| 1574 | // Check to see if we were called by checkin server. If so, print terse format. |
| 1575 | boolean doCheckinFormat = false; |
| 1576 | if (args != null) { |
| 1577 | for (String arg : args) { |
| 1578 | if ("-c".equals(arg)) doCheckinFormat = true; |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | // For checkin, we print one long comma-separated list of values |
| 1583 | if (doCheckinFormat) { |
| 1584 | // NOTE: if you change anything significant below, also consider changing |
| 1585 | // ACTIVITY_THREAD_CHECKIN_VERSION. |
| 1586 | String processName = (mBoundApplication != null) |
| 1587 | ? mBoundApplication.processName : "unknown"; |
| 1588 | |
| 1589 | // Header |
| 1590 | pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(','); |
| 1591 | pw.print(Process.myPid()); pw.print(','); |
| 1592 | pw.print(processName); pw.print(','); |
| 1593 | |
| 1594 | // Heap info - max |
| 1595 | pw.print(nativeMax); pw.print(','); |
| 1596 | pw.print(dalvikMax); pw.print(','); |
| 1597 | pw.print("N/A,"); |
| 1598 | pw.print(nativeMax + dalvikMax); pw.print(','); |
| 1599 | |
| 1600 | // Heap info - allocated |
| 1601 | pw.print(nativeAllocated); pw.print(','); |
| 1602 | pw.print(dalvikAllocated); pw.print(','); |
| 1603 | pw.print("N/A,"); |
| 1604 | pw.print(nativeAllocated + dalvikAllocated); pw.print(','); |
| 1605 | |
| 1606 | // Heap info - free |
| 1607 | pw.print(nativeFree); pw.print(','); |
| 1608 | pw.print(dalvikFree); pw.print(','); |
| 1609 | pw.print("N/A,"); |
| 1610 | pw.print(nativeFree + dalvikFree); pw.print(','); |
| 1611 | |
| 1612 | // Heap info - proportional set size |
| 1613 | pw.print(memInfo.nativePss); pw.print(','); |
| 1614 | pw.print(memInfo.dalvikPss); pw.print(','); |
| 1615 | pw.print(memInfo.otherPss); pw.print(','); |
| 1616 | pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(','); |
| 1617 | |
| 1618 | // Heap info - shared |
| 1619 | pw.print(nativeShared); pw.print(','); |
| 1620 | pw.print(dalvikShared); pw.print(','); |
| 1621 | pw.print(otherShared); pw.print(','); |
| 1622 | pw.print(nativeShared + dalvikShared + otherShared); pw.print(','); |
| 1623 | |
| 1624 | // Heap info - private |
| 1625 | pw.print(nativePrivate); pw.print(','); |
| 1626 | pw.print(dalvikPrivate); pw.print(','); |
| 1627 | pw.print(otherPrivate); pw.print(','); |
| 1628 | pw.print(nativePrivate + dalvikPrivate + otherPrivate); pw.print(','); |
| 1629 | |
| 1630 | // Object counts |
| 1631 | pw.print(viewInstanceCount); pw.print(','); |
| 1632 | pw.print(viewRootInstanceCount); pw.print(','); |
| 1633 | pw.print(appContextInstanceCount); pw.print(','); |
| 1634 | pw.print(activityInstanceCount); pw.print(','); |
| 1635 | |
| 1636 | pw.print(globalAssetCount); pw.print(','); |
| 1637 | pw.print(globalAssetManagerCount); pw.print(','); |
| 1638 | pw.print(binderLocalObjectCount); pw.print(','); |
| 1639 | pw.print(binderProxyObjectCount); pw.print(','); |
| 1640 | |
| 1641 | pw.print(binderDeathObjectCount); pw.print(','); |
| 1642 | pw.print(openSslSocketCount); pw.print(','); |
| 1643 | |
| 1644 | // SQL |
| 1645 | pw.print(sqliteAllocated); pw.print(','); |
| 1646 | pw.print(stats.databaseBytes / 1024); pw.print(','); |
| 1647 | pw.print(stats.numPagers); pw.print(','); |
| 1648 | pw.print((stats.totalBytes - stats.referencedBytes) / 1024); pw.print(','); |
| 1649 | pw.print(stats.referencedBytes / 1024); pw.print('\n'); |
| 1650 | |
| 1651 | return; |
| 1652 | } |
| 1653 | |
| 1654 | // otherwise, show human-readable format |
| 1655 | printRow(pw, HEAP_COLUMN, "", "native", "dalvik", "other", "total"); |
| 1656 | printRow(pw, HEAP_COLUMN, "size:", nativeMax, dalvikMax, "N/A", nativeMax + dalvikMax); |
| 1657 | printRow(pw, HEAP_COLUMN, "allocated:", nativeAllocated, dalvikAllocated, "N/A", |
| 1658 | nativeAllocated + dalvikAllocated); |
| 1659 | printRow(pw, HEAP_COLUMN, "free:", nativeFree, dalvikFree, "N/A", |
| 1660 | nativeFree + dalvikFree); |
| 1661 | |
| 1662 | printRow(pw, HEAP_COLUMN, "(Pss):", memInfo.nativePss, memInfo.dalvikPss, |
| 1663 | memInfo.otherPss, memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); |
| 1664 | |
| 1665 | printRow(pw, HEAP_COLUMN, "(shared dirty):", nativeShared, dalvikShared, otherShared, |
| 1666 | nativeShared + dalvikShared + otherShared); |
| 1667 | printRow(pw, HEAP_COLUMN, "(priv dirty):", nativePrivate, dalvikPrivate, otherPrivate, |
| 1668 | nativePrivate + dalvikPrivate + otherPrivate); |
| 1669 | |
| 1670 | pw.println(" "); |
| 1671 | pw.println(" Objects"); |
| 1672 | printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRoots:", |
| 1673 | viewRootInstanceCount); |
| 1674 | |
| 1675 | printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount, |
| 1676 | "Activities:", activityInstanceCount); |
| 1677 | |
| 1678 | printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount, |
| 1679 | "AssetManagers:", globalAssetManagerCount); |
| 1680 | |
| 1681 | printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount, |
| 1682 | "Proxy Binders:", binderProxyObjectCount); |
| 1683 | printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount); |
| 1684 | |
| 1685 | printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount); |
| 1686 | |
| 1687 | // SQLite mem info |
| 1688 | pw.println(" "); |
| 1689 | pw.println(" SQL"); |
| 1690 | printRow(pw, TWO_COUNT_COLUMNS, "heap:", sqliteAllocated, "dbFiles:", |
| 1691 | stats.databaseBytes / 1024); |
| 1692 | printRow(pw, TWO_COUNT_COLUMNS, "numPagers:", stats.numPagers, "inactivePageKB:", |
| 1693 | (stats.totalBytes - stats.referencedBytes) / 1024); |
| 1694 | printRow(pw, ONE_COUNT_COLUMN, "activePageKB:", stats.referencedBytes / 1024); |
| 1695 | } |
| 1696 | |
| 1697 | private void printRow(PrintWriter pw, String format, Object...objs) { |
| 1698 | pw.println(String.format(format, objs)); |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | private final class H extends Handler { |
| 1703 | public static final int LAUNCH_ACTIVITY = 100; |
| 1704 | public static final int PAUSE_ACTIVITY = 101; |
| 1705 | public static final int PAUSE_ACTIVITY_FINISHING= 102; |
| 1706 | public static final int STOP_ACTIVITY_SHOW = 103; |
| 1707 | public static final int STOP_ACTIVITY_HIDE = 104; |
| 1708 | public static final int SHOW_WINDOW = 105; |
| 1709 | public static final int HIDE_WINDOW = 106; |
| 1710 | public static final int RESUME_ACTIVITY = 107; |
| 1711 | public static final int SEND_RESULT = 108; |
| 1712 | public static final int DESTROY_ACTIVITY = 109; |
| 1713 | public static final int BIND_APPLICATION = 110; |
| 1714 | public static final int EXIT_APPLICATION = 111; |
| 1715 | public static final int NEW_INTENT = 112; |
| 1716 | public static final int RECEIVER = 113; |
| 1717 | public static final int CREATE_SERVICE = 114; |
| 1718 | public static final int SERVICE_ARGS = 115; |
| 1719 | public static final int STOP_SERVICE = 116; |
| 1720 | public static final int REQUEST_THUMBNAIL = 117; |
| 1721 | public static final int CONFIGURATION_CHANGED = 118; |
| 1722 | public static final int CLEAN_UP_CONTEXT = 119; |
| 1723 | public static final int GC_WHEN_IDLE = 120; |
| 1724 | public static final int BIND_SERVICE = 121; |
| 1725 | public static final int UNBIND_SERVICE = 122; |
| 1726 | public static final int DUMP_SERVICE = 123; |
| 1727 | public static final int LOW_MEMORY = 124; |
| 1728 | public static final int ACTIVITY_CONFIGURATION_CHANGED = 125; |
| 1729 | public static final int RELAUNCH_ACTIVITY = 126; |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 1730 | public static final int PROFILER_CONTROL = 127; |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1731 | public static final int CREATE_BACKUP_AGENT = 128; |
| 1732 | public static final int DESTROY_BACKUP_AGENT = 129; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1733 | String codeToString(int code) { |
| 1734 | if (localLOGV) { |
| 1735 | switch (code) { |
| 1736 | case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY"; |
| 1737 | case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY"; |
| 1738 | case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING"; |
| 1739 | case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW"; |
| 1740 | case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE"; |
| 1741 | case SHOW_WINDOW: return "SHOW_WINDOW"; |
| 1742 | case HIDE_WINDOW: return "HIDE_WINDOW"; |
| 1743 | case RESUME_ACTIVITY: return "RESUME_ACTIVITY"; |
| 1744 | case SEND_RESULT: return "SEND_RESULT"; |
| 1745 | case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY"; |
| 1746 | case BIND_APPLICATION: return "BIND_APPLICATION"; |
| 1747 | case EXIT_APPLICATION: return "EXIT_APPLICATION"; |
| 1748 | case NEW_INTENT: return "NEW_INTENT"; |
| 1749 | case RECEIVER: return "RECEIVER"; |
| 1750 | case CREATE_SERVICE: return "CREATE_SERVICE"; |
| 1751 | case SERVICE_ARGS: return "SERVICE_ARGS"; |
| 1752 | case STOP_SERVICE: return "STOP_SERVICE"; |
| 1753 | case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL"; |
| 1754 | case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED"; |
| 1755 | case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT"; |
| 1756 | case GC_WHEN_IDLE: return "GC_WHEN_IDLE"; |
| 1757 | case BIND_SERVICE: return "BIND_SERVICE"; |
| 1758 | case UNBIND_SERVICE: return "UNBIND_SERVICE"; |
| 1759 | case DUMP_SERVICE: return "DUMP_SERVICE"; |
| 1760 | case LOW_MEMORY: return "LOW_MEMORY"; |
| 1761 | case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED"; |
| 1762 | case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY"; |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 1763 | case PROFILER_CONTROL: return "PROFILER_CONTROL"; |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1764 | case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT"; |
| 1765 | case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1766 | } |
| 1767 | } |
| 1768 | return "(unknown)"; |
| 1769 | } |
| 1770 | public void handleMessage(Message msg) { |
| 1771 | switch (msg.what) { |
| 1772 | case LAUNCH_ACTIVITY: { |
| 1773 | ActivityRecord r = (ActivityRecord)msg.obj; |
| 1774 | |
| 1775 | r.packageInfo = getPackageInfoNoCheck( |
| 1776 | r.activityInfo.applicationInfo); |
Christopher Tate | b70f3df | 2009-04-07 16:07:59 -0700 | [diff] [blame] | 1777 | handleLaunchActivity(r, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1778 | } break; |
| 1779 | case RELAUNCH_ACTIVITY: { |
| 1780 | ActivityRecord r = (ActivityRecord)msg.obj; |
| 1781 | handleRelaunchActivity(r, msg.arg1); |
| 1782 | } break; |
| 1783 | case PAUSE_ACTIVITY: |
| 1784 | handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2); |
| 1785 | break; |
| 1786 | case PAUSE_ACTIVITY_FINISHING: |
| 1787 | handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2); |
| 1788 | break; |
| 1789 | case STOP_ACTIVITY_SHOW: |
| 1790 | handleStopActivity((IBinder)msg.obj, true, msg.arg2); |
| 1791 | break; |
| 1792 | case STOP_ACTIVITY_HIDE: |
| 1793 | handleStopActivity((IBinder)msg.obj, false, msg.arg2); |
| 1794 | break; |
| 1795 | case SHOW_WINDOW: |
| 1796 | handleWindowVisibility((IBinder)msg.obj, true); |
| 1797 | break; |
| 1798 | case HIDE_WINDOW: |
| 1799 | handleWindowVisibility((IBinder)msg.obj, false); |
| 1800 | break; |
| 1801 | case RESUME_ACTIVITY: |
| 1802 | handleResumeActivity((IBinder)msg.obj, true, |
| 1803 | msg.arg1 != 0); |
| 1804 | break; |
| 1805 | case SEND_RESULT: |
| 1806 | handleSendResult((ResultData)msg.obj); |
| 1807 | break; |
| 1808 | case DESTROY_ACTIVITY: |
| 1809 | handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0, |
| 1810 | msg.arg2, false); |
| 1811 | break; |
| 1812 | case BIND_APPLICATION: |
| 1813 | AppBindData data = (AppBindData)msg.obj; |
| 1814 | handleBindApplication(data); |
| 1815 | break; |
| 1816 | case EXIT_APPLICATION: |
| 1817 | if (mInitialApplication != null) { |
| 1818 | mInitialApplication.onTerminate(); |
| 1819 | } |
| 1820 | Looper.myLooper().quit(); |
| 1821 | break; |
| 1822 | case NEW_INTENT: |
| 1823 | handleNewIntent((NewIntentData)msg.obj); |
| 1824 | break; |
| 1825 | case RECEIVER: |
| 1826 | handleReceiver((ReceiverData)msg.obj); |
| 1827 | break; |
| 1828 | case CREATE_SERVICE: |
| 1829 | handleCreateService((CreateServiceData)msg.obj); |
| 1830 | break; |
| 1831 | case BIND_SERVICE: |
| 1832 | handleBindService((BindServiceData)msg.obj); |
| 1833 | break; |
| 1834 | case UNBIND_SERVICE: |
| 1835 | handleUnbindService((BindServiceData)msg.obj); |
| 1836 | break; |
| 1837 | case SERVICE_ARGS: |
| 1838 | handleServiceArgs((ServiceArgsData)msg.obj); |
| 1839 | break; |
| 1840 | case STOP_SERVICE: |
| 1841 | handleStopService((IBinder)msg.obj); |
| 1842 | break; |
| 1843 | case REQUEST_THUMBNAIL: |
| 1844 | handleRequestThumbnail((IBinder)msg.obj); |
| 1845 | break; |
| 1846 | case CONFIGURATION_CHANGED: |
| 1847 | handleConfigurationChanged((Configuration)msg.obj); |
| 1848 | break; |
| 1849 | case CLEAN_UP_CONTEXT: |
| 1850 | ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj; |
| 1851 | cci.context.performFinalCleanup(cci.who, cci.what); |
| 1852 | break; |
| 1853 | case GC_WHEN_IDLE: |
| 1854 | scheduleGcIdler(); |
| 1855 | break; |
| 1856 | case DUMP_SERVICE: |
| 1857 | handleDumpService((DumpServiceInfo)msg.obj); |
| 1858 | break; |
| 1859 | case LOW_MEMORY: |
| 1860 | handleLowMemory(); |
| 1861 | break; |
| 1862 | case ACTIVITY_CONFIGURATION_CHANGED: |
| 1863 | handleActivityConfigurationChanged((IBinder)msg.obj); |
| 1864 | break; |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 1865 | case PROFILER_CONTROL: |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 1866 | handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 1867 | break; |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1868 | case CREATE_BACKUP_AGENT: |
| 1869 | handleCreateBackupAgent((CreateBackupAgentData)msg.obj); |
| 1870 | break; |
| 1871 | case DESTROY_BACKUP_AGENT: |
| 1872 | handleDestroyBackupAgent((CreateBackupAgentData)msg.obj); |
| 1873 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1874 | } |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | private final class Idler implements MessageQueue.IdleHandler { |
| 1879 | public final boolean queueIdle() { |
| 1880 | ActivityRecord a = mNewActivities; |
| 1881 | if (a != null) { |
| 1882 | mNewActivities = null; |
| 1883 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 1884 | ActivityRecord prev; |
| 1885 | do { |
| 1886 | if (localLOGV) Log.v( |
| 1887 | TAG, "Reporting idle of " + a + |
| 1888 | " finished=" + |
| 1889 | (a.activity != null ? a.activity.mFinished : false)); |
| 1890 | if (a.activity != null && !a.activity.mFinished) { |
| 1891 | try { |
| 1892 | am.activityIdle(a.token); |
| 1893 | } catch (RemoteException ex) { |
| 1894 | } |
| 1895 | } |
| 1896 | prev = a; |
| 1897 | a = a.nextIdle; |
| 1898 | prev.nextIdle = null; |
| 1899 | } while (a != null); |
| 1900 | } |
| 1901 | return false; |
| 1902 | } |
| 1903 | } |
| 1904 | |
| 1905 | final class GcIdler implements MessageQueue.IdleHandler { |
| 1906 | public final boolean queueIdle() { |
| 1907 | doGcIfNeeded(); |
| 1908 | return false; |
| 1909 | } |
| 1910 | } |
| 1911 | |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 1912 | private final static class ResourcesKey { |
| 1913 | final private String mResDir; |
| 1914 | final private float mScale; |
| 1915 | final private int mHash; |
| 1916 | |
| 1917 | ResourcesKey(String resDir, float scale) { |
| 1918 | mResDir = resDir; |
| 1919 | mScale = scale; |
| 1920 | mHash = mResDir.hashCode() << 2 + (int) (mScale * 2); |
| 1921 | } |
| 1922 | |
| 1923 | @Override |
| 1924 | public int hashCode() { |
| 1925 | return mHash; |
| 1926 | } |
| 1927 | |
| 1928 | @Override |
| 1929 | public boolean equals(Object obj) { |
| 1930 | if (!(obj instanceof ResourcesKey)) { |
| 1931 | return false; |
| 1932 | } |
| 1933 | ResourcesKey peer = (ResourcesKey) obj; |
| 1934 | return mResDir.equals(peer.mResDir) && mScale == peer.mScale; |
| 1935 | } |
| 1936 | } |
| 1937 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1938 | static IPackageManager sPackageManager; |
| 1939 | |
| 1940 | final ApplicationThread mAppThread = new ApplicationThread(); |
| 1941 | final Looper mLooper = Looper.myLooper(); |
| 1942 | final H mH = new H(); |
| 1943 | final HashMap<IBinder, ActivityRecord> mActivities |
| 1944 | = new HashMap<IBinder, ActivityRecord>(); |
| 1945 | // List of new activities (via ActivityRecord.nextIdle) that should |
| 1946 | // be reported when next we idle. |
| 1947 | ActivityRecord mNewActivities = null; |
| 1948 | // Number of activities that are currently visible on-screen. |
| 1949 | int mNumVisibleActivities = 0; |
| 1950 | final HashMap<IBinder, Service> mServices |
| 1951 | = new HashMap<IBinder, Service>(); |
| 1952 | AppBindData mBoundApplication; |
| 1953 | Configuration mConfiguration; |
| 1954 | Application mInitialApplication; |
| 1955 | final ArrayList<Application> mAllApplications |
| 1956 | = new ArrayList<Application>(); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1957 | // set of instantiated backup agents, keyed by package name |
| 1958 | final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1959 | static final ThreadLocal sThreadLocal = new ThreadLocal(); |
| 1960 | Instrumentation mInstrumentation; |
| 1961 | String mInstrumentationAppDir = null; |
| 1962 | String mInstrumentationAppPackage = null; |
| 1963 | String mInstrumentedAppDir = null; |
| 1964 | boolean mSystemThread = false; |
| 1965 | |
| 1966 | /** |
| 1967 | * Activities that are enqueued to be relaunched. This list is accessed |
| 1968 | * by multiple threads, so you must synchronize on it when accessing it. |
| 1969 | */ |
| 1970 | final ArrayList<ActivityRecord> mRelaunchingActivities |
| 1971 | = new ArrayList<ActivityRecord>(); |
| 1972 | Configuration mPendingConfiguration = null; |
| 1973 | |
| 1974 | // These can be accessed by multiple threads; mPackages is the lock. |
| 1975 | // XXX For now we keep around information about all packages we have |
| 1976 | // seen, not removing entries from this map. |
| 1977 | final HashMap<String, WeakReference<PackageInfo>> mPackages |
| 1978 | = new HashMap<String, WeakReference<PackageInfo>>(); |
| 1979 | final HashMap<String, WeakReference<PackageInfo>> mResourcePackages |
| 1980 | = new HashMap<String, WeakReference<PackageInfo>>(); |
| 1981 | Display mDisplay = null; |
| 1982 | DisplayMetrics mDisplayMetrics = null; |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 1983 | HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources |
| 1984 | = new HashMap<ResourcesKey, WeakReference<Resources> >(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1985 | |
| 1986 | // The lock of mProviderMap protects the following variables. |
| 1987 | final HashMap<String, ProviderRecord> mProviderMap |
| 1988 | = new HashMap<String, ProviderRecord>(); |
| 1989 | final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap |
| 1990 | = new HashMap<IBinder, ProviderRefCount>(); |
| 1991 | final HashMap<IBinder, ProviderRecord> mLocalProviders |
| 1992 | = new HashMap<IBinder, ProviderRecord>(); |
| 1993 | |
| 1994 | final GcIdler mGcIdler = new GcIdler(); |
| 1995 | boolean mGcIdlerScheduled = false; |
| 1996 | |
| 1997 | public final PackageInfo getPackageInfo(String packageName, int flags) { |
| 1998 | synchronized (mPackages) { |
| 1999 | WeakReference<PackageInfo> ref; |
| 2000 | if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) { |
| 2001 | ref = mPackages.get(packageName); |
| 2002 | } else { |
| 2003 | ref = mResourcePackages.get(packageName); |
| 2004 | } |
| 2005 | PackageInfo packageInfo = ref != null ? ref.get() : null; |
| 2006 | //Log.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo); |
| 2007 | if (packageInfo != null && (packageInfo.mResources == null |
| 2008 | || packageInfo.mResources.getAssets().isUpToDate())) { |
| 2009 | if (packageInfo.isSecurityViolation() |
| 2010 | && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) { |
| 2011 | throw new SecurityException( |
| 2012 | "Requesting code from " + packageName |
| 2013 | + " to be run in process " |
| 2014 | + mBoundApplication.processName |
| 2015 | + "/" + mBoundApplication.appInfo.uid); |
| 2016 | } |
| 2017 | return packageInfo; |
| 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | ApplicationInfo ai = null; |
| 2022 | try { |
| 2023 | ai = getPackageManager().getApplicationInfo(packageName, |
| 2024 | PackageManager.GET_SHARED_LIBRARY_FILES); |
| 2025 | } catch (RemoteException e) { |
| 2026 | } |
| 2027 | |
| 2028 | if (ai != null) { |
| 2029 | return getPackageInfo(ai, flags); |
| 2030 | } |
| 2031 | |
| 2032 | return null; |
| 2033 | } |
| 2034 | |
| 2035 | public final PackageInfo getPackageInfo(ApplicationInfo ai, int flags) { |
| 2036 | boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0; |
| 2037 | boolean securityViolation = includeCode && ai.uid != 0 |
| 2038 | && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null |
| 2039 | ? ai.uid != mBoundApplication.appInfo.uid : true); |
| 2040 | if ((flags&(Context.CONTEXT_INCLUDE_CODE |
| 2041 | |Context.CONTEXT_IGNORE_SECURITY)) |
| 2042 | == Context.CONTEXT_INCLUDE_CODE) { |
| 2043 | if (securityViolation) { |
| 2044 | String msg = "Requesting code from " + ai.packageName |
| 2045 | + " (with uid " + ai.uid + ")"; |
| 2046 | if (mBoundApplication != null) { |
| 2047 | msg = msg + " to be run in process " |
| 2048 | + mBoundApplication.processName + " (with uid " |
| 2049 | + mBoundApplication.appInfo.uid + ")"; |
| 2050 | } |
| 2051 | throw new SecurityException(msg); |
| 2052 | } |
| 2053 | } |
| 2054 | return getPackageInfo(ai, null, securityViolation, includeCode); |
| 2055 | } |
| 2056 | |
| 2057 | public final PackageInfo getPackageInfoNoCheck(ApplicationInfo ai) { |
| 2058 | return getPackageInfo(ai, null, false, true); |
| 2059 | } |
| 2060 | |
| 2061 | private final PackageInfo getPackageInfo(ApplicationInfo aInfo, |
| 2062 | ClassLoader baseLoader, boolean securityViolation, boolean includeCode) { |
| 2063 | synchronized (mPackages) { |
| 2064 | WeakReference<PackageInfo> ref; |
| 2065 | if (includeCode) { |
| 2066 | ref = mPackages.get(aInfo.packageName); |
| 2067 | } else { |
| 2068 | ref = mResourcePackages.get(aInfo.packageName); |
| 2069 | } |
| 2070 | PackageInfo packageInfo = ref != null ? ref.get() : null; |
| 2071 | if (packageInfo == null || (packageInfo.mResources != null |
| 2072 | && !packageInfo.mResources.getAssets().isUpToDate())) { |
| 2073 | if (localLOGV) Log.v(TAG, (includeCode ? "Loading code package " |
| 2074 | : "Loading resource-only package ") + aInfo.packageName |
| 2075 | + " (in " + (mBoundApplication != null |
| 2076 | ? mBoundApplication.processName : null) |
| 2077 | + ")"); |
| 2078 | packageInfo = |
| 2079 | new PackageInfo(this, aInfo, this, baseLoader, |
| 2080 | securityViolation, includeCode && |
| 2081 | (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0); |
| 2082 | if (includeCode) { |
| 2083 | mPackages.put(aInfo.packageName, |
| 2084 | new WeakReference<PackageInfo>(packageInfo)); |
| 2085 | } else { |
| 2086 | mResourcePackages.put(aInfo.packageName, |
| 2087 | new WeakReference<PackageInfo>(packageInfo)); |
| 2088 | } |
| 2089 | } |
| 2090 | return packageInfo; |
| 2091 | } |
| 2092 | } |
| 2093 | |
| 2094 | public final boolean hasPackageInfo(String packageName) { |
| 2095 | synchronized (mPackages) { |
| 2096 | WeakReference<PackageInfo> ref; |
| 2097 | ref = mPackages.get(packageName); |
| 2098 | if (ref != null && ref.get() != null) { |
| 2099 | return true; |
| 2100 | } |
| 2101 | ref = mResourcePackages.get(packageName); |
| 2102 | if (ref != null && ref.get() != null) { |
| 2103 | return true; |
| 2104 | } |
| 2105 | return false; |
| 2106 | } |
| 2107 | } |
| 2108 | |
| 2109 | ActivityThread() { |
| 2110 | } |
| 2111 | |
| 2112 | public ApplicationThread getApplicationThread() |
| 2113 | { |
| 2114 | return mAppThread; |
| 2115 | } |
| 2116 | |
| 2117 | public Instrumentation getInstrumentation() |
| 2118 | { |
| 2119 | return mInstrumentation; |
| 2120 | } |
| 2121 | |
| 2122 | public Configuration getConfiguration() { |
| 2123 | return mConfiguration; |
| 2124 | } |
| 2125 | |
| 2126 | public boolean isProfiling() { |
| 2127 | return mBoundApplication != null && mBoundApplication.profileFile != null; |
| 2128 | } |
| 2129 | |
| 2130 | public String getProfileFilePath() { |
| 2131 | return mBoundApplication.profileFile; |
| 2132 | } |
| 2133 | |
| 2134 | public Looper getLooper() { |
| 2135 | return mLooper; |
| 2136 | } |
| 2137 | |
| 2138 | public Application getApplication() { |
| 2139 | return mInitialApplication; |
| 2140 | } |
| 2141 | |
Dianne Hackborn | d97c7ad | 2009-06-19 11:37:35 -0700 | [diff] [blame] | 2142 | public String getProcessName() { |
| 2143 | return mBoundApplication.processName; |
| 2144 | } |
| 2145 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2146 | public ApplicationContext getSystemContext() { |
| 2147 | synchronized (this) { |
| 2148 | if (mSystemContext == null) { |
| 2149 | ApplicationContext context = |
| 2150 | ApplicationContext.createSystemContext(this); |
| 2151 | PackageInfo info = new PackageInfo(this, "android", context); |
| 2152 | context.init(info, null, this); |
| 2153 | context.getResources().updateConfiguration( |
| 2154 | getConfiguration(), getDisplayMetricsLocked(false)); |
| 2155 | mSystemContext = context; |
| 2156 | //Log.i(TAG, "Created system resources " + context.getResources() |
| 2157 | // + ": " + context.getResources().getConfiguration()); |
| 2158 | } |
| 2159 | } |
| 2160 | return mSystemContext; |
| 2161 | } |
| 2162 | |
| 2163 | void scheduleGcIdler() { |
| 2164 | if (!mGcIdlerScheduled) { |
| 2165 | mGcIdlerScheduled = true; |
| 2166 | Looper.myQueue().addIdleHandler(mGcIdler); |
| 2167 | } |
| 2168 | mH.removeMessages(H.GC_WHEN_IDLE); |
| 2169 | } |
| 2170 | |
| 2171 | void unscheduleGcIdler() { |
| 2172 | if (mGcIdlerScheduled) { |
| 2173 | mGcIdlerScheduled = false; |
| 2174 | Looper.myQueue().removeIdleHandler(mGcIdler); |
| 2175 | } |
| 2176 | mH.removeMessages(H.GC_WHEN_IDLE); |
| 2177 | } |
| 2178 | |
| 2179 | void doGcIfNeeded() { |
| 2180 | mGcIdlerScheduled = false; |
| 2181 | final long now = SystemClock.uptimeMillis(); |
| 2182 | //Log.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime() |
| 2183 | // + "m now=" + now); |
| 2184 | if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) { |
| 2185 | //Log.i(TAG, "**** WE DO, WE DO WANT TO GC!"); |
| 2186 | BinderInternal.forceGc("bg"); |
| 2187 | } |
| 2188 | } |
| 2189 | |
| 2190 | public final ActivityInfo resolveActivityInfo(Intent intent) { |
| 2191 | ActivityInfo aInfo = intent.resolveActivityInfo( |
| 2192 | mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES); |
| 2193 | if (aInfo == null) { |
| 2194 | // Throw an exception. |
| 2195 | Instrumentation.checkStartActivityResult( |
| 2196 | IActivityManager.START_CLASS_NOT_FOUND, intent); |
| 2197 | } |
| 2198 | return aInfo; |
| 2199 | } |
| 2200 | |
| 2201 | public final Activity startActivityNow(Activity parent, String id, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2202 | Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state, |
| 2203 | Object lastNonConfigurationInstance) { |
| 2204 | ActivityRecord r = new ActivityRecord(); |
| 2205 | r.token = token; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2206 | r.ident = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2207 | r.intent = intent; |
| 2208 | r.state = state; |
| 2209 | r.parent = parent; |
| 2210 | r.embeddedID = id; |
| 2211 | r.activityInfo = activityInfo; |
| 2212 | r.lastNonConfigurationInstance = lastNonConfigurationInstance; |
| 2213 | if (localLOGV) { |
| 2214 | ComponentName compname = intent.getComponent(); |
| 2215 | String name; |
| 2216 | if (compname != null) { |
| 2217 | name = compname.toShortString(); |
| 2218 | } else { |
| 2219 | name = "(Intent " + intent + ").getComponent() returned null"; |
| 2220 | } |
| 2221 | Log.v(TAG, "Performing launch: action=" + intent.getAction() |
| 2222 | + ", comp=" + name |
| 2223 | + ", token=" + token); |
| 2224 | } |
Christopher Tate | b70f3df | 2009-04-07 16:07:59 -0700 | [diff] [blame] | 2225 | return performLaunchActivity(r, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | public final Activity getActivity(IBinder token) { |
| 2229 | return mActivities.get(token).activity; |
| 2230 | } |
| 2231 | |
| 2232 | public final void sendActivityResult( |
| 2233 | IBinder token, String id, int requestCode, |
| 2234 | int resultCode, Intent data) { |
Chris Tate | 8a7dc17 | 2009-03-24 20:11:42 -0700 | [diff] [blame] | 2235 | if (DEBUG_RESULTS) Log.v(TAG, "sendActivityResult: id=" + id |
| 2236 | + " req=" + requestCode + " res=" + resultCode + " data=" + data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2237 | ArrayList<ResultInfo> list = new ArrayList<ResultInfo>(); |
| 2238 | list.add(new ResultInfo(id, requestCode, resultCode, data)); |
| 2239 | mAppThread.scheduleSendResult(token, list); |
| 2240 | } |
| 2241 | |
| 2242 | // if the thread hasn't started yet, we don't have the handler, so just |
| 2243 | // save the messages until we're ready. |
| 2244 | private final void queueOrSendMessage(int what, Object obj) { |
| 2245 | queueOrSendMessage(what, obj, 0, 0); |
| 2246 | } |
| 2247 | |
| 2248 | private final void queueOrSendMessage(int what, Object obj, int arg1) { |
| 2249 | queueOrSendMessage(what, obj, arg1, 0); |
| 2250 | } |
| 2251 | |
| 2252 | private final void queueOrSendMessage(int what, Object obj, int arg1, int arg2) { |
| 2253 | synchronized (this) { |
| 2254 | if (localLOGV) Log.v( |
| 2255 | TAG, "SCHEDULE " + what + " " + mH.codeToString(what) |
| 2256 | + ": " + arg1 + " / " + obj); |
| 2257 | Message msg = Message.obtain(); |
| 2258 | msg.what = what; |
| 2259 | msg.obj = obj; |
| 2260 | msg.arg1 = arg1; |
| 2261 | msg.arg2 = arg2; |
| 2262 | mH.sendMessage(msg); |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | final void scheduleContextCleanup(ApplicationContext context, String who, |
| 2267 | String what) { |
| 2268 | ContextCleanupInfo cci = new ContextCleanupInfo(); |
| 2269 | cci.context = context; |
| 2270 | cci.who = who; |
| 2271 | cci.what = what; |
| 2272 | queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci); |
| 2273 | } |
| 2274 | |
Christopher Tate | b70f3df | 2009-04-07 16:07:59 -0700 | [diff] [blame] | 2275 | private final Activity performLaunchActivity(ActivityRecord r, Intent customIntent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2276 | // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")"); |
| 2277 | |
| 2278 | ActivityInfo aInfo = r.activityInfo; |
| 2279 | if (r.packageInfo == null) { |
| 2280 | r.packageInfo = getPackageInfo(aInfo.applicationInfo, |
| 2281 | Context.CONTEXT_INCLUDE_CODE); |
| 2282 | } |
| 2283 | |
| 2284 | ComponentName component = r.intent.getComponent(); |
| 2285 | if (component == null) { |
| 2286 | component = r.intent.resolveActivity( |
| 2287 | mInitialApplication.getPackageManager()); |
| 2288 | r.intent.setComponent(component); |
| 2289 | } |
| 2290 | |
| 2291 | if (r.activityInfo.targetActivity != null) { |
| 2292 | component = new ComponentName(r.activityInfo.packageName, |
| 2293 | r.activityInfo.targetActivity); |
| 2294 | } |
| 2295 | |
| 2296 | Activity activity = null; |
| 2297 | try { |
| 2298 | java.lang.ClassLoader cl = r.packageInfo.getClassLoader(); |
| 2299 | activity = mInstrumentation.newActivity( |
| 2300 | cl, component.getClassName(), r.intent); |
| 2301 | r.intent.setExtrasClassLoader(cl); |
| 2302 | if (r.state != null) { |
| 2303 | r.state.setClassLoader(cl); |
| 2304 | } |
| 2305 | } catch (Exception e) { |
| 2306 | if (!mInstrumentation.onException(activity, e)) { |
| 2307 | throw new RuntimeException( |
| 2308 | "Unable to instantiate activity " + component |
| 2309 | + ": " + e.toString(), e); |
| 2310 | } |
| 2311 | } |
| 2312 | |
| 2313 | try { |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 2314 | Application app = r.packageInfo.makeApplication(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2315 | |
| 2316 | if (localLOGV) Log.v(TAG, "Performing launch of " + r); |
| 2317 | if (localLOGV) Log.v( |
| 2318 | TAG, r + ": app=" + app |
| 2319 | + ", appName=" + app.getPackageName() |
| 2320 | + ", pkg=" + r.packageInfo.getPackageName() |
| 2321 | + ", comp=" + r.intent.getComponent().toShortString() |
| 2322 | + ", dir=" + r.packageInfo.getAppDir()); |
| 2323 | |
| 2324 | if (activity != null) { |
| 2325 | ApplicationContext appContext = new ApplicationContext(); |
| 2326 | appContext.init(r.packageInfo, r.token, this); |
| 2327 | appContext.setOuterContext(activity); |
| 2328 | CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager()); |
| 2329 | Configuration config = new Configuration(mConfiguration); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2330 | activity.attach(appContext, this, getInstrumentation(), r.token, |
| 2331 | r.ident, app, r.intent, r.activityInfo, title, r.parent, |
| 2332 | r.embeddedID, r.lastNonConfigurationInstance, |
| 2333 | r.lastNonConfigurationChildInstances, config); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2334 | |
Christopher Tate | b70f3df | 2009-04-07 16:07:59 -0700 | [diff] [blame] | 2335 | if (customIntent != null) { |
| 2336 | activity.mIntent = customIntent; |
| 2337 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2338 | r.lastNonConfigurationInstance = null; |
| 2339 | r.lastNonConfigurationChildInstances = null; |
| 2340 | activity.mStartedActivity = false; |
| 2341 | int theme = r.activityInfo.getThemeResource(); |
| 2342 | if (theme != 0) { |
| 2343 | activity.setTheme(theme); |
| 2344 | } |
| 2345 | |
| 2346 | activity.mCalled = false; |
| 2347 | mInstrumentation.callActivityOnCreate(activity, r.state); |
| 2348 | if (!activity.mCalled) { |
| 2349 | throw new SuperNotCalledException( |
| 2350 | "Activity " + r.intent.getComponent().toShortString() + |
| 2351 | " did not call through to super.onCreate()"); |
| 2352 | } |
| 2353 | r.activity = activity; |
| 2354 | r.stopped = true; |
| 2355 | if (!r.activity.mFinished) { |
| 2356 | activity.performStart(); |
| 2357 | r.stopped = false; |
| 2358 | } |
| 2359 | if (!r.activity.mFinished) { |
| 2360 | if (r.state != null) { |
| 2361 | mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state); |
| 2362 | } |
| 2363 | } |
| 2364 | if (!r.activity.mFinished) { |
| 2365 | activity.mCalled = false; |
| 2366 | mInstrumentation.callActivityOnPostCreate(activity, r.state); |
| 2367 | if (!activity.mCalled) { |
| 2368 | throw new SuperNotCalledException( |
| 2369 | "Activity " + r.intent.getComponent().toShortString() + |
| 2370 | " did not call through to super.onPostCreate()"); |
| 2371 | } |
| 2372 | } |
| 2373 | r.state = null; |
| 2374 | } |
| 2375 | r.paused = true; |
| 2376 | |
| 2377 | mActivities.put(r.token, r); |
| 2378 | |
| 2379 | } catch (SuperNotCalledException e) { |
| 2380 | throw e; |
| 2381 | |
| 2382 | } catch (Exception e) { |
| 2383 | if (!mInstrumentation.onException(activity, e)) { |
| 2384 | throw new RuntimeException( |
| 2385 | "Unable to start activity " + component |
| 2386 | + ": " + e.toString(), e); |
| 2387 | } |
| 2388 | } |
| 2389 | |
| 2390 | return activity; |
| 2391 | } |
| 2392 | |
Christopher Tate | b70f3df | 2009-04-07 16:07:59 -0700 | [diff] [blame] | 2393 | private final void handleLaunchActivity(ActivityRecord r, Intent customIntent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2394 | // If we are getting ready to gc after going to the background, well |
| 2395 | // we are back active so skip it. |
| 2396 | unscheduleGcIdler(); |
| 2397 | |
| 2398 | if (localLOGV) Log.v( |
| 2399 | TAG, "Handling launch of " + r); |
Christopher Tate | b70f3df | 2009-04-07 16:07:59 -0700 | [diff] [blame] | 2400 | Activity a = performLaunchActivity(r, customIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2401 | |
| 2402 | if (a != null) { |
| 2403 | handleResumeActivity(r.token, false, r.isForward); |
| 2404 | |
| 2405 | if (!r.activity.mFinished && r.startsNotResumed) { |
| 2406 | // The activity manager actually wants this one to start out |
| 2407 | // paused, because it needs to be visible but isn't in the |
| 2408 | // foreground. We accomplish this by going through the |
| 2409 | // normal startup (because activities expect to go through |
| 2410 | // onResume() the first time they run, before their window |
| 2411 | // is displayed), and then pausing it. However, in this case |
| 2412 | // we do -not- need to do the full pause cycle (of freezing |
| 2413 | // and such) because the activity manager assumes it can just |
| 2414 | // retain the current state it has. |
| 2415 | try { |
| 2416 | r.activity.mCalled = false; |
| 2417 | mInstrumentation.callActivityOnPause(r.activity); |
| 2418 | if (!r.activity.mCalled) { |
| 2419 | throw new SuperNotCalledException( |
| 2420 | "Activity " + r.intent.getComponent().toShortString() + |
| 2421 | " did not call through to super.onPause()"); |
| 2422 | } |
| 2423 | |
| 2424 | } catch (SuperNotCalledException e) { |
| 2425 | throw e; |
| 2426 | |
| 2427 | } catch (Exception e) { |
| 2428 | if (!mInstrumentation.onException(r.activity, e)) { |
| 2429 | throw new RuntimeException( |
| 2430 | "Unable to pause activity " |
| 2431 | + r.intent.getComponent().toShortString() |
| 2432 | + ": " + e.toString(), e); |
| 2433 | } |
| 2434 | } |
| 2435 | r.paused = true; |
| 2436 | } |
| 2437 | } else { |
| 2438 | // If there was an error, for any reason, tell the activity |
| 2439 | // manager to stop us. |
| 2440 | try { |
| 2441 | ActivityManagerNative.getDefault() |
| 2442 | .finishActivity(r.token, Activity.RESULT_CANCELED, null); |
| 2443 | } catch (RemoteException ex) { |
| 2444 | } |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | private final void deliverNewIntents(ActivityRecord r, |
| 2449 | List<Intent> intents) { |
| 2450 | final int N = intents.size(); |
| 2451 | for (int i=0; i<N; i++) { |
| 2452 | Intent intent = intents.get(i); |
| 2453 | intent.setExtrasClassLoader(r.activity.getClassLoader()); |
| 2454 | mInstrumentation.callActivityOnNewIntent(r.activity, intent); |
| 2455 | } |
| 2456 | } |
| 2457 | |
| 2458 | public final void performNewIntents(IBinder token, |
| 2459 | List<Intent> intents) { |
| 2460 | ActivityRecord r = mActivities.get(token); |
| 2461 | if (r != null) { |
| 2462 | final boolean resumed = !r.paused; |
| 2463 | if (resumed) { |
| 2464 | mInstrumentation.callActivityOnPause(r.activity); |
| 2465 | } |
| 2466 | deliverNewIntents(r, intents); |
| 2467 | if (resumed) { |
| 2468 | mInstrumentation.callActivityOnResume(r.activity); |
| 2469 | } |
| 2470 | } |
| 2471 | } |
| 2472 | |
| 2473 | private final void handleNewIntent(NewIntentData data) { |
| 2474 | performNewIntents(data.token, data.intents); |
| 2475 | } |
| 2476 | |
| 2477 | private final void handleReceiver(ReceiverData data) { |
| 2478 | // If we are getting ready to gc after going to the background, well |
| 2479 | // we are back active so skip it. |
| 2480 | unscheduleGcIdler(); |
| 2481 | |
| 2482 | String component = data.intent.getComponent().getClassName(); |
| 2483 | |
| 2484 | PackageInfo packageInfo = getPackageInfoNoCheck( |
| 2485 | data.info.applicationInfo); |
| 2486 | |
| 2487 | IActivityManager mgr = ActivityManagerNative.getDefault(); |
| 2488 | |
| 2489 | BroadcastReceiver receiver = null; |
| 2490 | try { |
| 2491 | java.lang.ClassLoader cl = packageInfo.getClassLoader(); |
| 2492 | data.intent.setExtrasClassLoader(cl); |
| 2493 | if (data.resultExtras != null) { |
| 2494 | data.resultExtras.setClassLoader(cl); |
| 2495 | } |
| 2496 | receiver = (BroadcastReceiver)cl.loadClass(component).newInstance(); |
| 2497 | } catch (Exception e) { |
| 2498 | try { |
| 2499 | mgr.finishReceiver(mAppThread.asBinder(), data.resultCode, |
| 2500 | data.resultData, data.resultExtras, data.resultAbort); |
| 2501 | } catch (RemoteException ex) { |
| 2502 | } |
| 2503 | throw new RuntimeException( |
| 2504 | "Unable to instantiate receiver " + component |
| 2505 | + ": " + e.toString(), e); |
| 2506 | } |
| 2507 | |
| 2508 | try { |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 2509 | Application app = packageInfo.makeApplication(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2510 | |
| 2511 | if (localLOGV) Log.v( |
| 2512 | TAG, "Performing receive of " + data.intent |
| 2513 | + ": app=" + app |
| 2514 | + ", appName=" + app.getPackageName() |
| 2515 | + ", pkg=" + packageInfo.getPackageName() |
| 2516 | + ", comp=" + data.intent.getComponent().toShortString() |
| 2517 | + ", dir=" + packageInfo.getAppDir()); |
| 2518 | |
| 2519 | ApplicationContext context = (ApplicationContext)app.getBaseContext(); |
| 2520 | receiver.setOrderedHint(true); |
| 2521 | receiver.setResult(data.resultCode, data.resultData, |
| 2522 | data.resultExtras); |
| 2523 | receiver.setOrderedHint(data.sync); |
| 2524 | receiver.onReceive(context.getReceiverRestrictedContext(), |
| 2525 | data.intent); |
| 2526 | } catch (Exception e) { |
| 2527 | try { |
| 2528 | mgr.finishReceiver(mAppThread.asBinder(), data.resultCode, |
| 2529 | data.resultData, data.resultExtras, data.resultAbort); |
| 2530 | } catch (RemoteException ex) { |
| 2531 | } |
| 2532 | if (!mInstrumentation.onException(receiver, e)) { |
| 2533 | throw new RuntimeException( |
| 2534 | "Unable to start receiver " + component |
| 2535 | + ": " + e.toString(), e); |
| 2536 | } |
| 2537 | } |
| 2538 | |
| 2539 | try { |
| 2540 | if (data.sync) { |
| 2541 | mgr.finishReceiver( |
| 2542 | mAppThread.asBinder(), receiver.getResultCode(), |
| 2543 | receiver.getResultData(), receiver.getResultExtras(false), |
| 2544 | receiver.getAbortBroadcast()); |
| 2545 | } else { |
| 2546 | mgr.finishReceiver(mAppThread.asBinder(), 0, null, null, false); |
| 2547 | } |
| 2548 | } catch (RemoteException ex) { |
| 2549 | } |
| 2550 | } |
| 2551 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 2552 | // Instantiate a BackupAgent and tell it that it's alive |
| 2553 | private final void handleCreateBackupAgent(CreateBackupAgentData data) { |
| 2554 | if (DEBUG_BACKUP) Log.v(TAG, "handleCreateBackupAgent: " + data); |
| 2555 | |
| 2556 | // no longer idle; we have backup work to do |
| 2557 | unscheduleGcIdler(); |
| 2558 | |
| 2559 | // instantiate the BackupAgent class named in the manifest |
| 2560 | PackageInfo packageInfo = getPackageInfoNoCheck(data.appInfo); |
| 2561 | String packageName = packageInfo.mPackageName; |
| 2562 | if (mBackupAgents.get(packageName) != null) { |
| 2563 | Log.d(TAG, "BackupAgent " + " for " + packageName |
| 2564 | + " already exists"); |
| 2565 | return; |
| 2566 | } |
| 2567 | |
| 2568 | BackupAgent agent = null; |
| 2569 | String classname = data.appInfo.backupAgentName; |
| 2570 | if (classname == null) { |
| 2571 | if (data.backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) { |
| 2572 | Log.e(TAG, "Attempted incremental backup but no defined agent for " |
| 2573 | + packageName); |
| 2574 | return; |
| 2575 | } |
| 2576 | classname = "android.app.FullBackupAgent"; |
| 2577 | } |
| 2578 | try { |
Christopher Tate | d1475e0 | 2009-07-09 15:36:17 -0700 | [diff] [blame] | 2579 | IBinder binder = null; |
| 2580 | try { |
| 2581 | java.lang.ClassLoader cl = packageInfo.getClassLoader(); |
| 2582 | agent = (BackupAgent) cl.loadClass(data.appInfo.backupAgentName).newInstance(); |
| 2583 | |
| 2584 | // set up the agent's context |
| 2585 | if (DEBUG_BACKUP) Log.v(TAG, "Initializing BackupAgent " |
| 2586 | + data.appInfo.backupAgentName); |
| 2587 | |
| 2588 | ApplicationContext context = new ApplicationContext(); |
| 2589 | context.init(packageInfo, null, this); |
| 2590 | context.setOuterContext(agent); |
| 2591 | agent.attach(context); |
| 2592 | |
| 2593 | agent.onCreate(); |
| 2594 | binder = agent.onBind(); |
| 2595 | mBackupAgents.put(packageName, agent); |
| 2596 | } catch (Exception e) { |
| 2597 | // If this is during restore, fail silently; otherwise go |
| 2598 | // ahead and let the user see the crash. |
| 2599 | Log.e(TAG, "Agent threw during creation: " + e); |
| 2600 | if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE) { |
| 2601 | throw e; |
| 2602 | } |
| 2603 | // falling through with 'binder' still null |
| 2604 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 2605 | |
| 2606 | // tell the OS that we're live now |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 2607 | try { |
| 2608 | ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder); |
| 2609 | } catch (RemoteException e) { |
| 2610 | // nothing to do. |
| 2611 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 2612 | } catch (Exception e) { |
| 2613 | throw new RuntimeException("Unable to create BackupAgent " |
| 2614 | + data.appInfo.backupAgentName + ": " + e.toString(), e); |
| 2615 | } |
| 2616 | } |
| 2617 | |
| 2618 | // Tear down a BackupAgent |
| 2619 | private final void handleDestroyBackupAgent(CreateBackupAgentData data) { |
| 2620 | if (DEBUG_BACKUP) Log.v(TAG, "handleDestroyBackupAgent: " + data); |
| 2621 | |
| 2622 | PackageInfo packageInfo = getPackageInfoNoCheck(data.appInfo); |
| 2623 | String packageName = packageInfo.mPackageName; |
| 2624 | BackupAgent agent = mBackupAgents.get(packageName); |
| 2625 | if (agent != null) { |
| 2626 | try { |
| 2627 | agent.onDestroy(); |
| 2628 | } catch (Exception e) { |
| 2629 | Log.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo); |
| 2630 | e.printStackTrace(); |
| 2631 | } |
| 2632 | mBackupAgents.remove(packageName); |
| 2633 | } else { |
| 2634 | Log.w(TAG, "Attempt to destroy unknown backup agent " + data); |
| 2635 | } |
| 2636 | } |
| 2637 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2638 | private final void handleCreateService(CreateServiceData data) { |
| 2639 | // If we are getting ready to gc after going to the background, well |
| 2640 | // we are back active so skip it. |
| 2641 | unscheduleGcIdler(); |
| 2642 | |
| 2643 | PackageInfo packageInfo = getPackageInfoNoCheck( |
| 2644 | data.info.applicationInfo); |
| 2645 | Service service = null; |
| 2646 | try { |
| 2647 | java.lang.ClassLoader cl = packageInfo.getClassLoader(); |
| 2648 | service = (Service) cl.loadClass(data.info.name).newInstance(); |
| 2649 | } catch (Exception e) { |
| 2650 | if (!mInstrumentation.onException(service, e)) { |
| 2651 | throw new RuntimeException( |
| 2652 | "Unable to instantiate service " + data.info.name |
| 2653 | + ": " + e.toString(), e); |
| 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | try { |
| 2658 | if (localLOGV) Log.v(TAG, "Creating service " + data.info.name); |
| 2659 | |
| 2660 | ApplicationContext context = new ApplicationContext(); |
| 2661 | context.init(packageInfo, null, this); |
| 2662 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 2663 | Application app = packageInfo.makeApplication(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2664 | context.setOuterContext(service); |
| 2665 | service.attach(context, this, data.info.name, data.token, app, |
| 2666 | ActivityManagerNative.getDefault()); |
| 2667 | service.onCreate(); |
| 2668 | mServices.put(data.token, service); |
| 2669 | try { |
| 2670 | ActivityManagerNative.getDefault().serviceDoneExecuting(data.token); |
| 2671 | } catch (RemoteException e) { |
| 2672 | // nothing to do. |
| 2673 | } |
| 2674 | } catch (Exception e) { |
| 2675 | if (!mInstrumentation.onException(service, e)) { |
| 2676 | throw new RuntimeException( |
| 2677 | "Unable to create service " + data.info.name |
| 2678 | + ": " + e.toString(), e); |
| 2679 | } |
| 2680 | } |
| 2681 | } |
| 2682 | |
| 2683 | private final void handleBindService(BindServiceData data) { |
| 2684 | Service s = mServices.get(data.token); |
| 2685 | if (s != null) { |
| 2686 | try { |
| 2687 | data.intent.setExtrasClassLoader(s.getClassLoader()); |
| 2688 | try { |
| 2689 | if (!data.rebind) { |
| 2690 | IBinder binder = s.onBind(data.intent); |
| 2691 | ActivityManagerNative.getDefault().publishService( |
| 2692 | data.token, data.intent, binder); |
| 2693 | } else { |
| 2694 | s.onRebind(data.intent); |
| 2695 | ActivityManagerNative.getDefault().serviceDoneExecuting( |
| 2696 | data.token); |
| 2697 | } |
| 2698 | } catch (RemoteException ex) { |
| 2699 | } |
| 2700 | } catch (Exception e) { |
| 2701 | if (!mInstrumentation.onException(s, e)) { |
| 2702 | throw new RuntimeException( |
| 2703 | "Unable to bind to service " + s |
| 2704 | + " with " + data.intent + ": " + e.toString(), e); |
| 2705 | } |
| 2706 | } |
| 2707 | } |
| 2708 | } |
| 2709 | |
| 2710 | private final void handleUnbindService(BindServiceData data) { |
| 2711 | Service s = mServices.get(data.token); |
| 2712 | if (s != null) { |
| 2713 | try { |
| 2714 | data.intent.setExtrasClassLoader(s.getClassLoader()); |
| 2715 | boolean doRebind = s.onUnbind(data.intent); |
| 2716 | try { |
| 2717 | if (doRebind) { |
| 2718 | ActivityManagerNative.getDefault().unbindFinished( |
| 2719 | data.token, data.intent, doRebind); |
| 2720 | } else { |
| 2721 | ActivityManagerNative.getDefault().serviceDoneExecuting( |
| 2722 | data.token); |
| 2723 | } |
| 2724 | } catch (RemoteException ex) { |
| 2725 | } |
| 2726 | } catch (Exception e) { |
| 2727 | if (!mInstrumentation.onException(s, e)) { |
| 2728 | throw new RuntimeException( |
| 2729 | "Unable to unbind to service " + s |
| 2730 | + " with " + data.intent + ": " + e.toString(), e); |
| 2731 | } |
| 2732 | } |
| 2733 | } |
| 2734 | } |
| 2735 | |
| 2736 | private void handleDumpService(DumpServiceInfo info) { |
| 2737 | try { |
| 2738 | Service s = mServices.get(info.service); |
| 2739 | if (s != null) { |
| 2740 | PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd)); |
| 2741 | s.dump(info.fd, pw, info.args); |
| 2742 | pw.close(); |
| 2743 | } |
| 2744 | } finally { |
| 2745 | synchronized (info) { |
| 2746 | info.dumped = true; |
| 2747 | info.notifyAll(); |
| 2748 | } |
| 2749 | } |
| 2750 | } |
| 2751 | |
| 2752 | private final void handleServiceArgs(ServiceArgsData data) { |
| 2753 | Service s = mServices.get(data.token); |
| 2754 | if (s != null) { |
| 2755 | try { |
| 2756 | if (data.args != null) { |
| 2757 | data.args.setExtrasClassLoader(s.getClassLoader()); |
| 2758 | } |
| 2759 | s.onStart(data.args, data.startId); |
| 2760 | try { |
| 2761 | ActivityManagerNative.getDefault().serviceDoneExecuting(data.token); |
| 2762 | } catch (RemoteException e) { |
| 2763 | // nothing to do. |
| 2764 | } |
| 2765 | } catch (Exception e) { |
| 2766 | if (!mInstrumentation.onException(s, e)) { |
| 2767 | throw new RuntimeException( |
| 2768 | "Unable to start service " + s |
| 2769 | + " with " + data.args + ": " + e.toString(), e); |
| 2770 | } |
| 2771 | } |
| 2772 | } |
| 2773 | } |
| 2774 | |
| 2775 | private final void handleStopService(IBinder token) { |
| 2776 | Service s = mServices.remove(token); |
| 2777 | if (s != null) { |
| 2778 | try { |
| 2779 | if (localLOGV) Log.v(TAG, "Destroying service " + s); |
| 2780 | s.onDestroy(); |
| 2781 | Context context = s.getBaseContext(); |
| 2782 | if (context instanceof ApplicationContext) { |
| 2783 | final String who = s.getClassName(); |
| 2784 | ((ApplicationContext) context).scheduleFinalCleanup(who, "Service"); |
| 2785 | } |
| 2786 | try { |
| 2787 | ActivityManagerNative.getDefault().serviceDoneExecuting(token); |
| 2788 | } catch (RemoteException e) { |
| 2789 | // nothing to do. |
| 2790 | } |
| 2791 | } catch (Exception e) { |
| 2792 | if (!mInstrumentation.onException(s, e)) { |
| 2793 | throw new RuntimeException( |
| 2794 | "Unable to stop service " + s |
| 2795 | + ": " + e.toString(), e); |
| 2796 | } |
| 2797 | } |
| 2798 | } |
| 2799 | //Log.i(TAG, "Running services: " + mServices); |
| 2800 | } |
| 2801 | |
| 2802 | public final ActivityRecord performResumeActivity(IBinder token, |
| 2803 | boolean clearHide) { |
| 2804 | ActivityRecord r = mActivities.get(token); |
| 2805 | if (localLOGV) Log.v(TAG, "Performing resume of " + r |
| 2806 | + " finished=" + r.activity.mFinished); |
| 2807 | if (r != null && !r.activity.mFinished) { |
| 2808 | if (clearHide) { |
| 2809 | r.hideForNow = false; |
| 2810 | r.activity.mStartedActivity = false; |
| 2811 | } |
| 2812 | try { |
| 2813 | if (r.pendingIntents != null) { |
| 2814 | deliverNewIntents(r, r.pendingIntents); |
| 2815 | r.pendingIntents = null; |
| 2816 | } |
| 2817 | if (r.pendingResults != null) { |
| 2818 | deliverResults(r, r.pendingResults); |
| 2819 | r.pendingResults = null; |
| 2820 | } |
| 2821 | r.activity.performResume(); |
| 2822 | |
| 2823 | EventLog.writeEvent(LOG_ON_RESUME_CALLED, |
| 2824 | r.activity.getComponentName().getClassName()); |
| 2825 | |
| 2826 | r.paused = false; |
| 2827 | r.stopped = false; |
| 2828 | if (r.activity.mStartedActivity) { |
| 2829 | r.hideForNow = true; |
| 2830 | } |
| 2831 | r.state = null; |
| 2832 | } catch (Exception e) { |
| 2833 | if (!mInstrumentation.onException(r.activity, e)) { |
| 2834 | throw new RuntimeException( |
| 2835 | "Unable to resume activity " |
| 2836 | + r.intent.getComponent().toShortString() |
| 2837 | + ": " + e.toString(), e); |
| 2838 | } |
| 2839 | } |
| 2840 | } |
| 2841 | return r; |
| 2842 | } |
| 2843 | |
| 2844 | final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) { |
| 2845 | // If we are getting ready to gc after going to the background, well |
| 2846 | // we are back active so skip it. |
| 2847 | unscheduleGcIdler(); |
| 2848 | |
| 2849 | ActivityRecord r = performResumeActivity(token, clearHide); |
| 2850 | |
| 2851 | if (r != null) { |
| 2852 | final Activity a = r.activity; |
| 2853 | |
| 2854 | if (localLOGV) Log.v( |
| 2855 | TAG, "Resume " + r + " started activity: " + |
| 2856 | a.mStartedActivity + ", hideForNow: " + r.hideForNow |
| 2857 | + ", finished: " + a.mFinished); |
| 2858 | |
| 2859 | final int forwardBit = isForward ? |
| 2860 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0; |
| 2861 | |
| 2862 | // If the window hasn't yet been added to the window manager, |
| 2863 | // and this guy didn't finish itself or start another activity, |
| 2864 | // then go ahead and add the window. |
| 2865 | if (r.window == null && !a.mFinished && !a.mStartedActivity) { |
| 2866 | r.window = r.activity.getWindow(); |
| 2867 | View decor = r.window.getDecorView(); |
| 2868 | decor.setVisibility(View.INVISIBLE); |
| 2869 | ViewManager wm = a.getWindowManager(); |
| 2870 | WindowManager.LayoutParams l = r.window.getAttributes(); |
| 2871 | a.mDecor = decor; |
| 2872 | l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION; |
| 2873 | l.softInputMode |= forwardBit; |
| 2874 | if (a.mVisibleFromClient) { |
| 2875 | a.mWindowAdded = true; |
| 2876 | wm.addView(decor, l); |
| 2877 | } |
| 2878 | |
| 2879 | // If the window has already been added, but during resume |
| 2880 | // we started another activity, then don't yet make the |
| 2881 | // window visisble. |
| 2882 | } else if (a.mStartedActivity) { |
| 2883 | if (localLOGV) Log.v( |
| 2884 | TAG, "Launch " + r + " mStartedActivity set"); |
| 2885 | r.hideForNow = true; |
| 2886 | } |
| 2887 | |
| 2888 | // The window is now visible if it has been added, we are not |
| 2889 | // simply finishing, and we are not starting another activity. |
| 2890 | if (!r.activity.mFinished && r.activity.mDecor != null |
| 2891 | && !r.hideForNow) { |
| 2892 | if (r.newConfig != null) { |
| 2893 | performConfigurationChanged(r.activity, r.newConfig); |
| 2894 | r.newConfig = null; |
| 2895 | } |
| 2896 | if (localLOGV) Log.v(TAG, "Resuming " + r + " with isForward=" |
| 2897 | + isForward); |
| 2898 | WindowManager.LayoutParams l = r.window.getAttributes(); |
| 2899 | if ((l.softInputMode |
| 2900 | & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) |
| 2901 | != forwardBit) { |
| 2902 | l.softInputMode = (l.softInputMode |
| 2903 | & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)) |
| 2904 | | forwardBit; |
| 2905 | ViewManager wm = a.getWindowManager(); |
| 2906 | View decor = r.window.getDecorView(); |
| 2907 | wm.updateViewLayout(decor, l); |
| 2908 | } |
| 2909 | r.activity.mVisibleFromServer = true; |
| 2910 | mNumVisibleActivities++; |
| 2911 | if (r.activity.mVisibleFromClient) { |
| 2912 | r.activity.makeVisible(); |
| 2913 | } |
| 2914 | } |
| 2915 | |
| 2916 | r.nextIdle = mNewActivities; |
| 2917 | mNewActivities = r; |
| 2918 | if (localLOGV) Log.v( |
| 2919 | TAG, "Scheduling idle handler for " + r); |
| 2920 | Looper.myQueue().addIdleHandler(new Idler()); |
| 2921 | |
| 2922 | } else { |
| 2923 | // If an exception was thrown when trying to resume, then |
| 2924 | // just end this activity. |
| 2925 | try { |
| 2926 | ActivityManagerNative.getDefault() |
| 2927 | .finishActivity(token, Activity.RESULT_CANCELED, null); |
| 2928 | } catch (RemoteException ex) { |
| 2929 | } |
| 2930 | } |
| 2931 | } |
| 2932 | |
| 2933 | private int mThumbnailWidth = -1; |
| 2934 | private int mThumbnailHeight = -1; |
| 2935 | |
| 2936 | private final Bitmap createThumbnailBitmap(ActivityRecord r) { |
| 2937 | Bitmap thumbnail = null; |
| 2938 | try { |
| 2939 | int w = mThumbnailWidth; |
| 2940 | int h; |
| 2941 | if (w < 0) { |
| 2942 | Resources res = r.activity.getResources(); |
| 2943 | mThumbnailHeight = h = |
| 2944 | res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height); |
| 2945 | |
| 2946 | mThumbnailWidth = w = |
| 2947 | res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width); |
| 2948 | } else { |
| 2949 | h = mThumbnailHeight; |
| 2950 | } |
| 2951 | |
| 2952 | // XXX Only set hasAlpha if needed? |
| 2953 | thumbnail = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565); |
| 2954 | thumbnail.eraseColor(0); |
| 2955 | Canvas cv = new Canvas(thumbnail); |
| 2956 | if (!r.activity.onCreateThumbnail(thumbnail, cv)) { |
| 2957 | thumbnail = null; |
| 2958 | } |
| 2959 | } catch (Exception e) { |
| 2960 | if (!mInstrumentation.onException(r.activity, e)) { |
| 2961 | throw new RuntimeException( |
| 2962 | "Unable to create thumbnail of " |
| 2963 | + r.intent.getComponent().toShortString() |
| 2964 | + ": " + e.toString(), e); |
| 2965 | } |
| 2966 | thumbnail = null; |
| 2967 | } |
| 2968 | |
| 2969 | return thumbnail; |
| 2970 | } |
| 2971 | |
| 2972 | private final void handlePauseActivity(IBinder token, boolean finished, |
| 2973 | boolean userLeaving, int configChanges) { |
| 2974 | ActivityRecord r = mActivities.get(token); |
| 2975 | if (r != null) { |
| 2976 | //Log.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r); |
| 2977 | if (userLeaving) { |
| 2978 | performUserLeavingActivity(r); |
| 2979 | } |
| 2980 | |
| 2981 | r.activity.mConfigChangeFlags |= configChanges; |
| 2982 | Bundle state = performPauseActivity(token, finished, true); |
| 2983 | |
| 2984 | // Tell the activity manager we have paused. |
| 2985 | try { |
| 2986 | ActivityManagerNative.getDefault().activityPaused(token, state); |
| 2987 | } catch (RemoteException ex) { |
| 2988 | } |
| 2989 | } |
| 2990 | } |
| 2991 | |
| 2992 | final void performUserLeavingActivity(ActivityRecord r) { |
| 2993 | mInstrumentation.callActivityOnUserLeaving(r.activity); |
| 2994 | } |
| 2995 | |
| 2996 | final Bundle performPauseActivity(IBinder token, boolean finished, |
| 2997 | boolean saveState) { |
| 2998 | ActivityRecord r = mActivities.get(token); |
| 2999 | return r != null ? performPauseActivity(r, finished, saveState) : null; |
| 3000 | } |
| 3001 | |
| 3002 | final Bundle performPauseActivity(ActivityRecord r, boolean finished, |
| 3003 | boolean saveState) { |
| 3004 | if (r.paused) { |
| 3005 | if (r.activity.mFinished) { |
| 3006 | // If we are finishing, we won't call onResume() in certain cases. |
| 3007 | // So here we likewise don't want to call onPause() if the activity |
| 3008 | // isn't resumed. |
| 3009 | return null; |
| 3010 | } |
| 3011 | RuntimeException e = new RuntimeException( |
| 3012 | "Performing pause of activity that is not resumed: " |
| 3013 | + r.intent.getComponent().toShortString()); |
| 3014 | Log.e(TAG, e.getMessage(), e); |
| 3015 | } |
| 3016 | Bundle state = null; |
| 3017 | if (finished) { |
| 3018 | r.activity.mFinished = true; |
| 3019 | } |
| 3020 | try { |
| 3021 | // Next have the activity save its current state and managed dialogs... |
| 3022 | if (!r.activity.mFinished && saveState) { |
| 3023 | state = new Bundle(); |
| 3024 | mInstrumentation.callActivityOnSaveInstanceState(r.activity, state); |
| 3025 | r.state = state; |
| 3026 | } |
| 3027 | // Now we are idle. |
| 3028 | r.activity.mCalled = false; |
| 3029 | mInstrumentation.callActivityOnPause(r.activity); |
| 3030 | EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName()); |
| 3031 | if (!r.activity.mCalled) { |
| 3032 | throw new SuperNotCalledException( |
| 3033 | "Activity " + r.intent.getComponent().toShortString() + |
| 3034 | " did not call through to super.onPause()"); |
| 3035 | } |
| 3036 | |
| 3037 | } catch (SuperNotCalledException e) { |
| 3038 | throw e; |
| 3039 | |
| 3040 | } catch (Exception e) { |
| 3041 | if (!mInstrumentation.onException(r.activity, e)) { |
| 3042 | throw new RuntimeException( |
| 3043 | "Unable to pause activity " |
| 3044 | + r.intent.getComponent().toShortString() |
| 3045 | + ": " + e.toString(), e); |
| 3046 | } |
| 3047 | } |
| 3048 | r.paused = true; |
| 3049 | return state; |
| 3050 | } |
| 3051 | |
| 3052 | final void performStopActivity(IBinder token) { |
| 3053 | ActivityRecord r = mActivities.get(token); |
| 3054 | performStopActivityInner(r, null, false); |
| 3055 | } |
| 3056 | |
| 3057 | private static class StopInfo { |
| 3058 | Bitmap thumbnail; |
| 3059 | CharSequence description; |
| 3060 | } |
| 3061 | |
| 3062 | private final class ProviderRefCount { |
| 3063 | public int count; |
| 3064 | ProviderRefCount(int pCount) { |
| 3065 | count = pCount; |
| 3066 | } |
| 3067 | } |
| 3068 | |
| 3069 | private final void performStopActivityInner(ActivityRecord r, |
| 3070 | StopInfo info, boolean keepShown) { |
| 3071 | if (localLOGV) Log.v(TAG, "Performing stop of " + r); |
| 3072 | if (r != null) { |
| 3073 | if (!keepShown && r.stopped) { |
| 3074 | if (r.activity.mFinished) { |
| 3075 | // If we are finishing, we won't call onResume() in certain |
| 3076 | // cases. So here we likewise don't want to call onStop() |
| 3077 | // if the activity isn't resumed. |
| 3078 | return; |
| 3079 | } |
| 3080 | RuntimeException e = new RuntimeException( |
| 3081 | "Performing stop of activity that is not resumed: " |
| 3082 | + r.intent.getComponent().toShortString()); |
| 3083 | Log.e(TAG, e.getMessage(), e); |
| 3084 | } |
| 3085 | |
| 3086 | if (info != null) { |
| 3087 | try { |
| 3088 | // First create a thumbnail for the activity... |
| 3089 | //info.thumbnail = createThumbnailBitmap(r); |
| 3090 | info.description = r.activity.onCreateDescription(); |
| 3091 | } catch (Exception e) { |
| 3092 | if (!mInstrumentation.onException(r.activity, e)) { |
| 3093 | throw new RuntimeException( |
| 3094 | "Unable to save state of activity " |
| 3095 | + r.intent.getComponent().toShortString() |
| 3096 | + ": " + e.toString(), e); |
| 3097 | } |
| 3098 | } |
| 3099 | } |
| 3100 | |
| 3101 | if (!keepShown) { |
| 3102 | try { |
| 3103 | // Now we are idle. |
| 3104 | r.activity.performStop(); |
| 3105 | } catch (Exception e) { |
| 3106 | if (!mInstrumentation.onException(r.activity, e)) { |
| 3107 | throw new RuntimeException( |
| 3108 | "Unable to stop activity " |
| 3109 | + r.intent.getComponent().toShortString() |
| 3110 | + ": " + e.toString(), e); |
| 3111 | } |
| 3112 | } |
| 3113 | r.stopped = true; |
| 3114 | } |
| 3115 | |
| 3116 | r.paused = true; |
| 3117 | } |
| 3118 | } |
| 3119 | |
| 3120 | private final void updateVisibility(ActivityRecord r, boolean show) { |
| 3121 | View v = r.activity.mDecor; |
| 3122 | if (v != null) { |
| 3123 | if (show) { |
| 3124 | if (!r.activity.mVisibleFromServer) { |
| 3125 | r.activity.mVisibleFromServer = true; |
| 3126 | mNumVisibleActivities++; |
| 3127 | if (r.activity.mVisibleFromClient) { |
| 3128 | r.activity.makeVisible(); |
| 3129 | } |
| 3130 | } |
| 3131 | if (r.newConfig != null) { |
| 3132 | performConfigurationChanged(r.activity, r.newConfig); |
| 3133 | r.newConfig = null; |
| 3134 | } |
| 3135 | } else { |
| 3136 | if (r.activity.mVisibleFromServer) { |
| 3137 | r.activity.mVisibleFromServer = false; |
| 3138 | mNumVisibleActivities--; |
| 3139 | v.setVisibility(View.INVISIBLE); |
| 3140 | } |
| 3141 | } |
| 3142 | } |
| 3143 | } |
| 3144 | |
| 3145 | private final void handleStopActivity(IBinder token, boolean show, int configChanges) { |
| 3146 | ActivityRecord r = mActivities.get(token); |
| 3147 | r.activity.mConfigChangeFlags |= configChanges; |
| 3148 | |
| 3149 | StopInfo info = new StopInfo(); |
| 3150 | performStopActivityInner(r, info, show); |
| 3151 | |
| 3152 | if (localLOGV) Log.v( |
| 3153 | TAG, "Finishing stop of " + r + ": show=" + show |
| 3154 | + " win=" + r.window); |
| 3155 | |
| 3156 | updateVisibility(r, show); |
| 3157 | |
| 3158 | // Tell activity manager we have been stopped. |
| 3159 | try { |
| 3160 | ActivityManagerNative.getDefault().activityStopped( |
| 3161 | r.token, info.thumbnail, info.description); |
| 3162 | } catch (RemoteException ex) { |
| 3163 | } |
| 3164 | } |
| 3165 | |
| 3166 | final void performRestartActivity(IBinder token) { |
| 3167 | ActivityRecord r = mActivities.get(token); |
| 3168 | if (r.stopped) { |
| 3169 | r.activity.performRestart(); |
| 3170 | r.stopped = false; |
| 3171 | } |
| 3172 | } |
| 3173 | |
| 3174 | private final void handleWindowVisibility(IBinder token, boolean show) { |
| 3175 | ActivityRecord r = mActivities.get(token); |
| 3176 | if (!show && !r.stopped) { |
| 3177 | performStopActivityInner(r, null, show); |
| 3178 | } else if (show && r.stopped) { |
| 3179 | // If we are getting ready to gc after going to the background, well |
| 3180 | // we are back active so skip it. |
| 3181 | unscheduleGcIdler(); |
| 3182 | |
| 3183 | r.activity.performRestart(); |
| 3184 | r.stopped = false; |
| 3185 | } |
| 3186 | if (r.activity.mDecor != null) { |
| 3187 | if (Config.LOGV) Log.v( |
| 3188 | TAG, "Handle window " + r + " visibility: " + show); |
| 3189 | updateVisibility(r, show); |
| 3190 | } |
| 3191 | } |
| 3192 | |
| 3193 | private final void deliverResults(ActivityRecord r, List<ResultInfo> results) { |
| 3194 | final int N = results.size(); |
| 3195 | for (int i=0; i<N; i++) { |
| 3196 | ResultInfo ri = results.get(i); |
| 3197 | try { |
| 3198 | if (ri.mData != null) { |
| 3199 | ri.mData.setExtrasClassLoader(r.activity.getClassLoader()); |
| 3200 | } |
Chris Tate | 8a7dc17 | 2009-03-24 20:11:42 -0700 | [diff] [blame] | 3201 | if (DEBUG_RESULTS) Log.v(TAG, |
| 3202 | "Delivering result to activity " + r + " : " + ri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3203 | r.activity.dispatchActivityResult(ri.mResultWho, |
| 3204 | ri.mRequestCode, ri.mResultCode, ri.mData); |
| 3205 | } catch (Exception e) { |
| 3206 | if (!mInstrumentation.onException(r.activity, e)) { |
| 3207 | throw new RuntimeException( |
| 3208 | "Failure delivering result " + ri + " to activity " |
| 3209 | + r.intent.getComponent().toShortString() |
| 3210 | + ": " + e.toString(), e); |
| 3211 | } |
| 3212 | } |
| 3213 | } |
| 3214 | } |
| 3215 | |
| 3216 | private final void handleSendResult(ResultData res) { |
| 3217 | ActivityRecord r = mActivities.get(res.token); |
Chris Tate | 8a7dc17 | 2009-03-24 20:11:42 -0700 | [diff] [blame] | 3218 | if (DEBUG_RESULTS) Log.v(TAG, "Handling send result to " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3219 | if (r != null) { |
| 3220 | final boolean resumed = !r.paused; |
| 3221 | if (!r.activity.mFinished && r.activity.mDecor != null |
| 3222 | && r.hideForNow && resumed) { |
| 3223 | // We had hidden the activity because it started another |
| 3224 | // one... we have gotten a result back and we are not |
| 3225 | // paused, so make sure our window is visible. |
| 3226 | updateVisibility(r, true); |
| 3227 | } |
| 3228 | if (resumed) { |
| 3229 | try { |
| 3230 | // Now we are idle. |
| 3231 | r.activity.mCalled = false; |
| 3232 | mInstrumentation.callActivityOnPause(r.activity); |
| 3233 | if (!r.activity.mCalled) { |
| 3234 | throw new SuperNotCalledException( |
| 3235 | "Activity " + r.intent.getComponent().toShortString() |
| 3236 | + " did not call through to super.onPause()"); |
| 3237 | } |
| 3238 | } catch (SuperNotCalledException e) { |
| 3239 | throw e; |
| 3240 | } catch (Exception e) { |
| 3241 | if (!mInstrumentation.onException(r.activity, e)) { |
| 3242 | throw new RuntimeException( |
| 3243 | "Unable to pause activity " |
| 3244 | + r.intent.getComponent().toShortString() |
| 3245 | + ": " + e.toString(), e); |
| 3246 | } |
| 3247 | } |
| 3248 | } |
| 3249 | deliverResults(r, res.results); |
| 3250 | if (resumed) { |
| 3251 | mInstrumentation.callActivityOnResume(r.activity); |
| 3252 | } |
| 3253 | } |
| 3254 | } |
| 3255 | |
| 3256 | public final ActivityRecord performDestroyActivity(IBinder token, boolean finishing) { |
| 3257 | return performDestroyActivity(token, finishing, 0, false); |
| 3258 | } |
| 3259 | |
| 3260 | private final ActivityRecord performDestroyActivity(IBinder token, boolean finishing, |
| 3261 | int configChanges, boolean getNonConfigInstance) { |
| 3262 | ActivityRecord r = mActivities.get(token); |
| 3263 | if (localLOGV) Log.v(TAG, "Performing finish of " + r); |
| 3264 | if (r != null) { |
| 3265 | r.activity.mConfigChangeFlags |= configChanges; |
| 3266 | if (finishing) { |
| 3267 | r.activity.mFinished = true; |
| 3268 | } |
| 3269 | if (!r.paused) { |
| 3270 | try { |
| 3271 | r.activity.mCalled = false; |
| 3272 | mInstrumentation.callActivityOnPause(r.activity); |
| 3273 | EventLog.writeEvent(LOG_ON_PAUSE_CALLED, |
| 3274 | r.activity.getComponentName().getClassName()); |
| 3275 | if (!r.activity.mCalled) { |
| 3276 | throw new SuperNotCalledException( |
Mitsuru Oshima | d9aef73 | 2009-06-16 20:20:50 -0700 | [diff] [blame] | 3277 | "Activity " + safeToComponentShortString(r.intent) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3278 | + " did not call through to super.onPause()"); |
| 3279 | } |
| 3280 | } catch (SuperNotCalledException e) { |
| 3281 | throw e; |
| 3282 | } catch (Exception e) { |
| 3283 | if (!mInstrumentation.onException(r.activity, e)) { |
| 3284 | throw new RuntimeException( |
| 3285 | "Unable to pause activity " |
Mitsuru Oshima | d9aef73 | 2009-06-16 20:20:50 -0700 | [diff] [blame] | 3286 | + safeToComponentShortString(r.intent) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3287 | + ": " + e.toString(), e); |
| 3288 | } |
| 3289 | } |
| 3290 | r.paused = true; |
| 3291 | } |
| 3292 | if (!r.stopped) { |
| 3293 | try { |
| 3294 | r.activity.performStop(); |
| 3295 | } catch (SuperNotCalledException e) { |
| 3296 | throw e; |
| 3297 | } catch (Exception e) { |
| 3298 | if (!mInstrumentation.onException(r.activity, e)) { |
| 3299 | throw new RuntimeException( |
| 3300 | "Unable to stop activity " |
Mitsuru Oshima | d9aef73 | 2009-06-16 20:20:50 -0700 | [diff] [blame] | 3301 | + safeToComponentShortString(r.intent) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3302 | + ": " + e.toString(), e); |
| 3303 | } |
| 3304 | } |
| 3305 | r.stopped = true; |
| 3306 | } |
| 3307 | if (getNonConfigInstance) { |
| 3308 | try { |
| 3309 | r.lastNonConfigurationInstance |
| 3310 | = r.activity.onRetainNonConfigurationInstance(); |
| 3311 | } catch (Exception e) { |
| 3312 | if (!mInstrumentation.onException(r.activity, e)) { |
| 3313 | throw new RuntimeException( |
| 3314 | "Unable to retain activity " |
| 3315 | + r.intent.getComponent().toShortString() |
| 3316 | + ": " + e.toString(), e); |
| 3317 | } |
| 3318 | } |
| 3319 | try { |
| 3320 | r.lastNonConfigurationChildInstances |
| 3321 | = r.activity.onRetainNonConfigurationChildInstances(); |
| 3322 | } catch (Exception e) { |
| 3323 | if (!mInstrumentation.onException(r.activity, e)) { |
| 3324 | throw new RuntimeException( |
| 3325 | "Unable to retain child activities " |
Mitsuru Oshima | d9aef73 | 2009-06-16 20:20:50 -0700 | [diff] [blame] | 3326 | + safeToComponentShortString(r.intent) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3327 | + ": " + e.toString(), e); |
| 3328 | } |
| 3329 | } |
| 3330 | |
| 3331 | } |
| 3332 | try { |
| 3333 | r.activity.mCalled = false; |
| 3334 | r.activity.onDestroy(); |
| 3335 | if (!r.activity.mCalled) { |
| 3336 | throw new SuperNotCalledException( |
Mitsuru Oshima | d9aef73 | 2009-06-16 20:20:50 -0700 | [diff] [blame] | 3337 | "Activity " + safeToComponentShortString(r.intent) + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3338 | " did not call through to super.onDestroy()"); |
| 3339 | } |
| 3340 | if (r.window != null) { |
| 3341 | r.window.closeAllPanels(); |
| 3342 | } |
| 3343 | } catch (SuperNotCalledException e) { |
| 3344 | throw e; |
| 3345 | } catch (Exception e) { |
| 3346 | if (!mInstrumentation.onException(r.activity, e)) { |
| 3347 | throw new RuntimeException( |
Mitsuru Oshima | d9aef73 | 2009-06-16 20:20:50 -0700 | [diff] [blame] | 3348 | "Unable to destroy activity " + safeToComponentShortString(r.intent) |
| 3349 | + ": " + e.toString(), e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3350 | } |
| 3351 | } |
| 3352 | } |
| 3353 | mActivities.remove(token); |
| 3354 | |
| 3355 | return r; |
| 3356 | } |
| 3357 | |
Mitsuru Oshima | d9aef73 | 2009-06-16 20:20:50 -0700 | [diff] [blame] | 3358 | private static String safeToComponentShortString(Intent intent) { |
| 3359 | ComponentName component = intent.getComponent(); |
| 3360 | return component == null ? "[Unknown]" : component.toShortString(); |
| 3361 | } |
| 3362 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3363 | private final void handleDestroyActivity(IBinder token, boolean finishing, |
| 3364 | int configChanges, boolean getNonConfigInstance) { |
| 3365 | ActivityRecord r = performDestroyActivity(token, finishing, |
| 3366 | configChanges, getNonConfigInstance); |
| 3367 | if (r != null) { |
| 3368 | WindowManager wm = r.activity.getWindowManager(); |
| 3369 | View v = r.activity.mDecor; |
| 3370 | if (v != null) { |
| 3371 | if (r.activity.mVisibleFromServer) { |
| 3372 | mNumVisibleActivities--; |
| 3373 | } |
| 3374 | IBinder wtoken = v.getWindowToken(); |
| 3375 | if (r.activity.mWindowAdded) { |
| 3376 | wm.removeViewImmediate(v); |
| 3377 | } |
| 3378 | if (wtoken != null) { |
| 3379 | WindowManagerImpl.getDefault().closeAll(wtoken, |
| 3380 | r.activity.getClass().getName(), "Activity"); |
| 3381 | } |
| 3382 | r.activity.mDecor = null; |
| 3383 | } |
| 3384 | WindowManagerImpl.getDefault().closeAll(token, |
| 3385 | r.activity.getClass().getName(), "Activity"); |
| 3386 | |
| 3387 | // Mocked out contexts won't be participating in the normal |
| 3388 | // process lifecycle, but if we're running with a proper |
| 3389 | // ApplicationContext we need to have it tear down things |
| 3390 | // cleanly. |
| 3391 | Context c = r.activity.getBaseContext(); |
| 3392 | if (c instanceof ApplicationContext) { |
| 3393 | ((ApplicationContext) c).scheduleFinalCleanup( |
| 3394 | r.activity.getClass().getName(), "Activity"); |
| 3395 | } |
| 3396 | } |
| 3397 | if (finishing) { |
| 3398 | try { |
| 3399 | ActivityManagerNative.getDefault().activityDestroyed(token); |
| 3400 | } catch (RemoteException ex) { |
| 3401 | // If the system process has died, it's game over for everyone. |
| 3402 | } |
| 3403 | } |
| 3404 | } |
| 3405 | |
| 3406 | private final void handleRelaunchActivity(ActivityRecord tmp, int configChanges) { |
| 3407 | // If we are getting ready to gc after going to the background, well |
| 3408 | // we are back active so skip it. |
| 3409 | unscheduleGcIdler(); |
| 3410 | |
| 3411 | Configuration changedConfig = null; |
| 3412 | |
| 3413 | // First: make sure we have the most recent configuration and most |
| 3414 | // recent version of the activity, or skip it if some previous call |
| 3415 | // had taken a more recent version. |
| 3416 | synchronized (mRelaunchingActivities) { |
| 3417 | int N = mRelaunchingActivities.size(); |
| 3418 | IBinder token = tmp.token; |
| 3419 | tmp = null; |
| 3420 | for (int i=0; i<N; i++) { |
| 3421 | ActivityRecord r = mRelaunchingActivities.get(i); |
| 3422 | if (r.token == token) { |
| 3423 | tmp = r; |
| 3424 | mRelaunchingActivities.remove(i); |
| 3425 | i--; |
| 3426 | N--; |
| 3427 | } |
| 3428 | } |
| 3429 | |
| 3430 | if (tmp == null) { |
| 3431 | return; |
| 3432 | } |
| 3433 | |
| 3434 | if (mPendingConfiguration != null) { |
| 3435 | changedConfig = mPendingConfiguration; |
| 3436 | mPendingConfiguration = null; |
| 3437 | } |
| 3438 | } |
| 3439 | |
| 3440 | // If there was a pending configuration change, execute it first. |
| 3441 | if (changedConfig != null) { |
| 3442 | handleConfigurationChanged(changedConfig); |
| 3443 | } |
| 3444 | |
| 3445 | ActivityRecord r = mActivities.get(tmp.token); |
| 3446 | if (localLOGV) Log.v(TAG, "Handling relaunch of " + r); |
| 3447 | if (r == null) { |
| 3448 | return; |
| 3449 | } |
| 3450 | |
| 3451 | r.activity.mConfigChangeFlags |= configChanges; |
Christopher Tate | b70f3df | 2009-04-07 16:07:59 -0700 | [diff] [blame] | 3452 | Intent currentIntent = r.activity.mIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3453 | |
| 3454 | Bundle savedState = null; |
| 3455 | if (!r.paused) { |
| 3456 | savedState = performPauseActivity(r.token, false, true); |
| 3457 | } |
| 3458 | |
| 3459 | handleDestroyActivity(r.token, false, configChanges, true); |
| 3460 | |
| 3461 | r.activity = null; |
| 3462 | r.window = null; |
| 3463 | r.hideForNow = false; |
| 3464 | r.nextIdle = null; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3465 | // Merge any pending results and pending intents; don't just replace them |
| 3466 | if (tmp.pendingResults != null) { |
| 3467 | if (r.pendingResults == null) { |
| 3468 | r.pendingResults = tmp.pendingResults; |
| 3469 | } else { |
| 3470 | r.pendingResults.addAll(tmp.pendingResults); |
| 3471 | } |
| 3472 | } |
| 3473 | if (tmp.pendingIntents != null) { |
| 3474 | if (r.pendingIntents == null) { |
| 3475 | r.pendingIntents = tmp.pendingIntents; |
| 3476 | } else { |
| 3477 | r.pendingIntents.addAll(tmp.pendingIntents); |
| 3478 | } |
| 3479 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3480 | r.startsNotResumed = tmp.startsNotResumed; |
| 3481 | if (savedState != null) { |
| 3482 | r.state = savedState; |
| 3483 | } |
| 3484 | |
Christopher Tate | b70f3df | 2009-04-07 16:07:59 -0700 | [diff] [blame] | 3485 | handleLaunchActivity(r, currentIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | private final void handleRequestThumbnail(IBinder token) { |
| 3489 | ActivityRecord r = mActivities.get(token); |
| 3490 | Bitmap thumbnail = createThumbnailBitmap(r); |
| 3491 | CharSequence description = null; |
| 3492 | try { |
| 3493 | description = r.activity.onCreateDescription(); |
| 3494 | } catch (Exception e) { |
| 3495 | if (!mInstrumentation.onException(r.activity, e)) { |
| 3496 | throw new RuntimeException( |
| 3497 | "Unable to create description of activity " |
| 3498 | + r.intent.getComponent().toShortString() |
| 3499 | + ": " + e.toString(), e); |
| 3500 | } |
| 3501 | } |
| 3502 | //System.out.println("Reporting top thumbnail " + thumbnail); |
| 3503 | try { |
| 3504 | ActivityManagerNative.getDefault().reportThumbnail( |
| 3505 | token, thumbnail, description); |
| 3506 | } catch (RemoteException ex) { |
| 3507 | } |
| 3508 | } |
| 3509 | |
| 3510 | ArrayList<ComponentCallbacks> collectComponentCallbacksLocked( |
| 3511 | boolean allActivities, Configuration newConfig) { |
| 3512 | ArrayList<ComponentCallbacks> callbacks |
| 3513 | = new ArrayList<ComponentCallbacks>(); |
| 3514 | |
| 3515 | if (mActivities.size() > 0) { |
| 3516 | Iterator<ActivityRecord> it = mActivities.values().iterator(); |
| 3517 | while (it.hasNext()) { |
| 3518 | ActivityRecord ar = it.next(); |
| 3519 | Activity a = ar.activity; |
| 3520 | if (a != null) { |
| 3521 | if (!ar.activity.mFinished && (allActivities || |
| 3522 | (a != null && !ar.paused))) { |
| 3523 | // If the activity is currently resumed, its configuration |
| 3524 | // needs to change right now. |
| 3525 | callbacks.add(a); |
| 3526 | } else if (newConfig != null) { |
| 3527 | // Otherwise, we will tell it about the change |
| 3528 | // the next time it is resumed or shown. Note that |
| 3529 | // the activity manager may, before then, decide the |
| 3530 | // activity needs to be destroyed to handle its new |
| 3531 | // configuration. |
| 3532 | ar.newConfig = newConfig; |
| 3533 | } |
| 3534 | } |
| 3535 | } |
| 3536 | } |
| 3537 | if (mServices.size() > 0) { |
| 3538 | Iterator<Service> it = mServices.values().iterator(); |
| 3539 | while (it.hasNext()) { |
| 3540 | callbacks.add(it.next()); |
| 3541 | } |
| 3542 | } |
| 3543 | synchronized (mProviderMap) { |
| 3544 | if (mLocalProviders.size() > 0) { |
| 3545 | Iterator<ProviderRecord> it = mLocalProviders.values().iterator(); |
| 3546 | while (it.hasNext()) { |
| 3547 | callbacks.add(it.next().mLocalProvider); |
| 3548 | } |
| 3549 | } |
| 3550 | } |
| 3551 | final int N = mAllApplications.size(); |
| 3552 | for (int i=0; i<N; i++) { |
| 3553 | callbacks.add(mAllApplications.get(i)); |
| 3554 | } |
| 3555 | |
| 3556 | return callbacks; |
| 3557 | } |
| 3558 | |
| 3559 | private final void performConfigurationChanged( |
| 3560 | ComponentCallbacks cb, Configuration config) { |
| 3561 | // Only for Activity objects, check that they actually call up to their |
| 3562 | // superclass implementation. ComponentCallbacks is an interface, so |
| 3563 | // we check the runtime type and act accordingly. |
| 3564 | Activity activity = (cb instanceof Activity) ? (Activity) cb : null; |
| 3565 | if (activity != null) { |
| 3566 | activity.mCalled = false; |
| 3567 | } |
| 3568 | |
| 3569 | boolean shouldChangeConfig = false; |
| 3570 | if ((activity == null) || (activity.mCurrentConfig == null)) { |
| 3571 | shouldChangeConfig = true; |
| 3572 | } else { |
| 3573 | |
| 3574 | // If the new config is the same as the config this Activity |
| 3575 | // is already running with then don't bother calling |
| 3576 | // onConfigurationChanged |
| 3577 | int diff = activity.mCurrentConfig.diff(config); |
| 3578 | if (diff != 0) { |
| 3579 | |
| 3580 | // If this activity doesn't handle any of the config changes |
| 3581 | // then don't bother calling onConfigurationChanged as we're |
| 3582 | // going to destroy it. |
| 3583 | if ((~activity.mActivityInfo.configChanges & diff) == 0) { |
| 3584 | shouldChangeConfig = true; |
| 3585 | } |
| 3586 | } |
| 3587 | } |
| 3588 | |
| 3589 | if (shouldChangeConfig) { |
| 3590 | cb.onConfigurationChanged(config); |
| 3591 | |
| 3592 | if (activity != null) { |
| 3593 | if (!activity.mCalled) { |
| 3594 | throw new SuperNotCalledException( |
| 3595 | "Activity " + activity.getLocalClassName() + |
| 3596 | " did not call through to super.onConfigurationChanged()"); |
| 3597 | } |
| 3598 | activity.mConfigChangeFlags = 0; |
| 3599 | activity.mCurrentConfig = new Configuration(config); |
| 3600 | } |
| 3601 | } |
| 3602 | } |
| 3603 | |
| 3604 | final void handleConfigurationChanged(Configuration config) { |
| 3605 | |
| 3606 | synchronized (mRelaunchingActivities) { |
| 3607 | if (mPendingConfiguration != null) { |
| 3608 | config = mPendingConfiguration; |
| 3609 | mPendingConfiguration = null; |
| 3610 | } |
| 3611 | } |
| 3612 | |
| 3613 | ArrayList<ComponentCallbacks> callbacks |
| 3614 | = new ArrayList<ComponentCallbacks>(); |
| 3615 | |
| 3616 | synchronized(mPackages) { |
| 3617 | if (mConfiguration == null) { |
| 3618 | mConfiguration = new Configuration(); |
| 3619 | } |
| 3620 | mConfiguration.updateFrom(config); |
| 3621 | DisplayMetrics dm = getDisplayMetricsLocked(true); |
| 3622 | |
| 3623 | // set it for java, this also affects newly created Resources |
| 3624 | if (config.locale != null) { |
| 3625 | Locale.setDefault(config.locale); |
| 3626 | } |
| 3627 | |
| 3628 | Resources.updateSystemConfiguration(config, null); |
| 3629 | |
| 3630 | ApplicationContext.ApplicationPackageManager.configurationChanged(); |
| 3631 | //Log.i(TAG, "Configuration changed in " + currentPackageName()); |
| 3632 | { |
| 3633 | Iterator<WeakReference<Resources>> it = |
| 3634 | mActiveResources.values().iterator(); |
| 3635 | //Iterator<Map.Entry<String, WeakReference<Resources>>> it = |
| 3636 | // mActiveResources.entrySet().iterator(); |
| 3637 | while (it.hasNext()) { |
| 3638 | WeakReference<Resources> v = it.next(); |
| 3639 | Resources r = v.get(); |
| 3640 | if (r != null) { |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 3641 | r.updateConfiguration(config, dm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3642 | //Log.i(TAG, "Updated app resources " + v.getKey() |
| 3643 | // + " " + r + ": " + r.getConfiguration()); |
| 3644 | } else { |
| 3645 | //Log.i(TAG, "Removing old resources " + v.getKey()); |
| 3646 | it.remove(); |
| 3647 | } |
| 3648 | } |
| 3649 | } |
| 3650 | |
| 3651 | callbacks = collectComponentCallbacksLocked(false, config); |
| 3652 | } |
| 3653 | |
| 3654 | final int N = callbacks.size(); |
| 3655 | for (int i=0; i<N; i++) { |
| 3656 | performConfigurationChanged(callbacks.get(i), config); |
| 3657 | } |
| 3658 | } |
| 3659 | |
| 3660 | final void handleActivityConfigurationChanged(IBinder token) { |
| 3661 | ActivityRecord r = mActivities.get(token); |
| 3662 | if (r == null || r.activity == null) { |
| 3663 | return; |
| 3664 | } |
| 3665 | |
| 3666 | performConfigurationChanged(r.activity, mConfiguration); |
| 3667 | } |
| 3668 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 3669 | final void handleProfilerControl(boolean start, ProfilerControlData pcd) { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 3670 | if (start) { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 3671 | try { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 3672 | Debug.startMethodTracing(pcd.path, pcd.fd.getFileDescriptor(), |
| 3673 | 8 * 1024 * 1024, 0); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 3674 | } catch (RuntimeException e) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 3675 | Log.w(TAG, "Profiling failed on path " + pcd.path |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 3676 | + " -- can the process access this path?"); |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 3677 | } finally { |
| 3678 | try { |
| 3679 | pcd.fd.close(); |
| 3680 | } catch (IOException e) { |
| 3681 | Log.w(TAG, "Failure closing profile fd", e); |
| 3682 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 3683 | } |
| 3684 | } else { |
| 3685 | Debug.stopMethodTracing(); |
| 3686 | } |
| 3687 | } |
| 3688 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3689 | final void handleLowMemory() { |
| 3690 | ArrayList<ComponentCallbacks> callbacks |
| 3691 | = new ArrayList<ComponentCallbacks>(); |
| 3692 | |
| 3693 | synchronized(mPackages) { |
| 3694 | callbacks = collectComponentCallbacksLocked(true, null); |
| 3695 | } |
| 3696 | |
| 3697 | final int N = callbacks.size(); |
| 3698 | for (int i=0; i<N; i++) { |
| 3699 | callbacks.get(i).onLowMemory(); |
| 3700 | } |
| 3701 | |
Chris Tate | ce22905 | 2009-03-25 16:44:52 -0700 | [diff] [blame] | 3702 | // Ask SQLite to free up as much memory as it can, mostly from its page caches. |
| 3703 | if (Process.myUid() != Process.SYSTEM_UID) { |
| 3704 | int sqliteReleased = SQLiteDatabase.releaseMemory(); |
| 3705 | EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased); |
| 3706 | } |
Mike Reed | caf0df1 | 2009-04-27 14:32:05 -0400 | [diff] [blame] | 3707 | |
| 3708 | // Ask graphics to free up as much as possible (font/image caches) |
| 3709 | Canvas.freeCaches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3710 | |
| 3711 | BinderInternal.forceGc("mem"); |
| 3712 | } |
| 3713 | |
| 3714 | private final void handleBindApplication(AppBindData data) { |
| 3715 | mBoundApplication = data; |
| 3716 | mConfiguration = new Configuration(data.config); |
| 3717 | |
| 3718 | // We now rely on this being set by zygote. |
| 3719 | //Process.setGid(data.appInfo.gid); |
| 3720 | //Process.setUid(data.appInfo.uid); |
| 3721 | |
| 3722 | // send up app name; do this *before* waiting for debugger |
| 3723 | android.ddm.DdmHandleAppName.setAppName(data.processName); |
| 3724 | |
| 3725 | /* |
| 3726 | * Before spawning a new process, reset the time zone to be the system time zone. |
| 3727 | * This needs to be done because the system time zone could have changed after the |
| 3728 | * the spawning of this process. Without doing this this process would have the incorrect |
| 3729 | * system time zone. |
| 3730 | */ |
| 3731 | TimeZone.setDefault(null); |
| 3732 | |
| 3733 | /* |
| 3734 | * Initialize the default locale in this process for the reasons we set the time zone. |
| 3735 | */ |
| 3736 | Locale.setDefault(data.config.locale); |
| 3737 | |
Suchi Amalapurapu | c984329 | 2009-06-24 17:02:25 -0700 | [diff] [blame] | 3738 | /* |
| 3739 | * Update the system configuration since its preloaded and might not |
| 3740 | * reflect configuration changes. The configuration object passed |
| 3741 | * in AppBindData can be safely assumed to be up to date |
| 3742 | */ |
| 3743 | Resources.getSystem().updateConfiguration(mConfiguration, null); |
| 3744 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3745 | data.info = getPackageInfoNoCheck(data.appInfo); |
| 3746 | |
| 3747 | if (data.debugMode != IApplicationThread.DEBUG_OFF) { |
| 3748 | // XXX should have option to change the port. |
| 3749 | Debug.changeDebugPort(8100); |
| 3750 | if (data.debugMode == IApplicationThread.DEBUG_WAIT) { |
| 3751 | Log.w(TAG, "Application " + data.info.getPackageName() |
| 3752 | + " is waiting for the debugger on port 8100..."); |
| 3753 | |
| 3754 | IActivityManager mgr = ActivityManagerNative.getDefault(); |
| 3755 | try { |
| 3756 | mgr.showWaitingForDebugger(mAppThread, true); |
| 3757 | } catch (RemoteException ex) { |
| 3758 | } |
| 3759 | |
| 3760 | Debug.waitForDebugger(); |
| 3761 | |
| 3762 | try { |
| 3763 | mgr.showWaitingForDebugger(mAppThread, false); |
| 3764 | } catch (RemoteException ex) { |
| 3765 | } |
| 3766 | |
| 3767 | } else { |
| 3768 | Log.w(TAG, "Application " + data.info.getPackageName() |
| 3769 | + " can be debugged on port 8100..."); |
| 3770 | } |
| 3771 | } |
| 3772 | |
| 3773 | if (data.instrumentationName != null) { |
| 3774 | ApplicationContext appContext = new ApplicationContext(); |
| 3775 | appContext.init(data.info, null, this); |
| 3776 | InstrumentationInfo ii = null; |
| 3777 | try { |
| 3778 | ii = appContext.getPackageManager(). |
| 3779 | getInstrumentationInfo(data.instrumentationName, 0); |
| 3780 | } catch (PackageManager.NameNotFoundException e) { |
| 3781 | } |
| 3782 | if (ii == null) { |
| 3783 | throw new RuntimeException( |
| 3784 | "Unable to find instrumentation info for: " |
| 3785 | + data.instrumentationName); |
| 3786 | } |
| 3787 | |
| 3788 | mInstrumentationAppDir = ii.sourceDir; |
| 3789 | mInstrumentationAppPackage = ii.packageName; |
| 3790 | mInstrumentedAppDir = data.info.getAppDir(); |
| 3791 | |
| 3792 | ApplicationInfo instrApp = new ApplicationInfo(); |
| 3793 | instrApp.packageName = ii.packageName; |
| 3794 | instrApp.sourceDir = ii.sourceDir; |
| 3795 | instrApp.publicSourceDir = ii.publicSourceDir; |
| 3796 | instrApp.dataDir = ii.dataDir; |
| 3797 | PackageInfo pi = getPackageInfo(instrApp, |
| 3798 | appContext.getClassLoader(), false, true); |
| 3799 | ApplicationContext instrContext = new ApplicationContext(); |
| 3800 | instrContext.init(pi, null, this); |
| 3801 | |
| 3802 | try { |
| 3803 | java.lang.ClassLoader cl = instrContext.getClassLoader(); |
| 3804 | mInstrumentation = (Instrumentation) |
| 3805 | cl.loadClass(data.instrumentationName.getClassName()).newInstance(); |
| 3806 | } catch (Exception e) { |
| 3807 | throw new RuntimeException( |
| 3808 | "Unable to instantiate instrumentation " |
| 3809 | + data.instrumentationName + ": " + e.toString(), e); |
| 3810 | } |
| 3811 | |
| 3812 | mInstrumentation.init(this, instrContext, appContext, |
| 3813 | new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher); |
| 3814 | |
| 3815 | if (data.profileFile != null && !ii.handleProfiling) { |
| 3816 | data.handlingProfiling = true; |
| 3817 | File file = new File(data.profileFile); |
| 3818 | file.getParentFile().mkdirs(); |
| 3819 | Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024); |
| 3820 | } |
| 3821 | |
| 3822 | try { |
| 3823 | mInstrumentation.onCreate(data.instrumentationArgs); |
| 3824 | } |
| 3825 | catch (Exception e) { |
| 3826 | throw new RuntimeException( |
| 3827 | "Exception thrown in onCreate() of " |
| 3828 | + data.instrumentationName + ": " + e.toString(), e); |
| 3829 | } |
| 3830 | |
| 3831 | } else { |
| 3832 | mInstrumentation = new Instrumentation(); |
| 3833 | } |
| 3834 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3835 | // If the app is being launched for full backup or restore, bring it up in |
| 3836 | // a restricted environment with the base application class. |
| 3837 | Application app = data.info.makeApplication(data.restrictedBackupMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3838 | mInitialApplication = app; |
| 3839 | |
| 3840 | List<ProviderInfo> providers = data.providers; |
| 3841 | if (providers != null) { |
| 3842 | installContentProviders(app, providers); |
| 3843 | } |
| 3844 | |
| 3845 | try { |
| 3846 | mInstrumentation.callApplicationOnCreate(app); |
| 3847 | } catch (Exception e) { |
| 3848 | if (!mInstrumentation.onException(app, e)) { |
| 3849 | throw new RuntimeException( |
| 3850 | "Unable to create application " + app.getClass().getName() |
| 3851 | + ": " + e.toString(), e); |
| 3852 | } |
| 3853 | } |
| 3854 | } |
| 3855 | |
| 3856 | /*package*/ final void finishInstrumentation(int resultCode, Bundle results) { |
| 3857 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 3858 | if (mBoundApplication.profileFile != null && mBoundApplication.handlingProfiling) { |
| 3859 | Debug.stopMethodTracing(); |
| 3860 | } |
| 3861 | //Log.i(TAG, "am: " + ActivityManagerNative.getDefault() |
| 3862 | // + ", app thr: " + mAppThread); |
| 3863 | try { |
| 3864 | am.finishInstrumentation(mAppThread, resultCode, results); |
| 3865 | } catch (RemoteException ex) { |
| 3866 | } |
| 3867 | } |
| 3868 | |
| 3869 | private final void installContentProviders( |
| 3870 | Context context, List<ProviderInfo> providers) { |
| 3871 | final ArrayList<IActivityManager.ContentProviderHolder> results = |
| 3872 | new ArrayList<IActivityManager.ContentProviderHolder>(); |
| 3873 | |
| 3874 | Iterator<ProviderInfo> i = providers.iterator(); |
| 3875 | while (i.hasNext()) { |
| 3876 | ProviderInfo cpi = i.next(); |
| 3877 | StringBuilder buf = new StringBuilder(128); |
| 3878 | buf.append("Publishing provider "); |
| 3879 | buf.append(cpi.authority); |
| 3880 | buf.append(": "); |
| 3881 | buf.append(cpi.name); |
| 3882 | Log.i(TAG, buf.toString()); |
| 3883 | IContentProvider cp = installProvider(context, null, cpi, false); |
| 3884 | if (cp != null) { |
| 3885 | IActivityManager.ContentProviderHolder cph = |
| 3886 | new IActivityManager.ContentProviderHolder(cpi); |
| 3887 | cph.provider = cp; |
| 3888 | results.add(cph); |
| 3889 | // Don't ever unload this provider from the process. |
| 3890 | synchronized(mProviderMap) { |
| 3891 | mProviderRefCountMap.put(cp.asBinder(), new ProviderRefCount(10000)); |
| 3892 | } |
| 3893 | } |
| 3894 | } |
| 3895 | |
| 3896 | try { |
| 3897 | ActivityManagerNative.getDefault().publishContentProviders( |
| 3898 | getApplicationThread(), results); |
| 3899 | } catch (RemoteException ex) { |
| 3900 | } |
| 3901 | } |
| 3902 | |
| 3903 | private final IContentProvider getProvider(Context context, String name) { |
| 3904 | synchronized(mProviderMap) { |
| 3905 | final ProviderRecord pr = mProviderMap.get(name); |
| 3906 | if (pr != null) { |
| 3907 | return pr.mProvider; |
| 3908 | } |
| 3909 | } |
| 3910 | |
| 3911 | IActivityManager.ContentProviderHolder holder = null; |
| 3912 | try { |
| 3913 | holder = ActivityManagerNative.getDefault().getContentProvider( |
| 3914 | getApplicationThread(), name); |
| 3915 | } catch (RemoteException ex) { |
| 3916 | } |
| 3917 | if (holder == null) { |
| 3918 | Log.e(TAG, "Failed to find provider info for " + name); |
| 3919 | return null; |
| 3920 | } |
| 3921 | if (holder.permissionFailure != null) { |
| 3922 | throw new SecurityException("Permission " + holder.permissionFailure |
| 3923 | + " required for provider " + name); |
| 3924 | } |
| 3925 | |
| 3926 | IContentProvider prov = installProvider(context, holder.provider, |
| 3927 | holder.info, true); |
| 3928 | //Log.i(TAG, "noReleaseNeeded=" + holder.noReleaseNeeded); |
| 3929 | if (holder.noReleaseNeeded || holder.provider == null) { |
| 3930 | // We are not going to release the provider if it is an external |
| 3931 | // provider that doesn't care about being released, or if it is |
| 3932 | // a local provider running in this process. |
| 3933 | //Log.i(TAG, "*** NO RELEASE NEEDED"); |
| 3934 | synchronized(mProviderMap) { |
| 3935 | mProviderRefCountMap.put(prov.asBinder(), new ProviderRefCount(10000)); |
| 3936 | } |
| 3937 | } |
| 3938 | return prov; |
| 3939 | } |
| 3940 | |
| 3941 | public final IContentProvider acquireProvider(Context c, String name) { |
| 3942 | IContentProvider provider = getProvider(c, name); |
| 3943 | if(provider == null) |
| 3944 | return null; |
| 3945 | IBinder jBinder = provider.asBinder(); |
| 3946 | synchronized(mProviderMap) { |
| 3947 | ProviderRefCount prc = mProviderRefCountMap.get(jBinder); |
| 3948 | if(prc == null) { |
| 3949 | mProviderRefCountMap.put(jBinder, new ProviderRefCount(1)); |
| 3950 | } else { |
| 3951 | prc.count++; |
| 3952 | } //end else |
| 3953 | } //end synchronized |
| 3954 | return provider; |
| 3955 | } |
| 3956 | |
| 3957 | public final boolean releaseProvider(IContentProvider provider) { |
| 3958 | if(provider == null) { |
| 3959 | return false; |
| 3960 | } |
| 3961 | IBinder jBinder = provider.asBinder(); |
| 3962 | synchronized(mProviderMap) { |
| 3963 | ProviderRefCount prc = mProviderRefCountMap.get(jBinder); |
| 3964 | if(prc == null) { |
| 3965 | if(localLOGV) Log.v(TAG, "releaseProvider::Weird shouldnt be here"); |
| 3966 | return false; |
| 3967 | } else { |
| 3968 | prc.count--; |
| 3969 | if(prc.count == 0) { |
| 3970 | mProviderRefCountMap.remove(jBinder); |
| 3971 | //invoke removeProvider to dereference provider |
| 3972 | removeProviderLocked(provider); |
| 3973 | } //end if |
| 3974 | } //end else |
| 3975 | } //end synchronized |
| 3976 | return true; |
| 3977 | } |
| 3978 | |
| 3979 | public final void removeProviderLocked(IContentProvider provider) { |
| 3980 | if (provider == null) { |
| 3981 | return; |
| 3982 | } |
| 3983 | IBinder providerBinder = provider.asBinder(); |
| 3984 | boolean amRemoveFlag = false; |
| 3985 | |
| 3986 | // remove the provider from mProviderMap |
| 3987 | Iterator<ProviderRecord> iter = mProviderMap.values().iterator(); |
| 3988 | while (iter.hasNext()) { |
| 3989 | ProviderRecord pr = iter.next(); |
| 3990 | IBinder myBinder = pr.mProvider.asBinder(); |
| 3991 | if (myBinder == providerBinder) { |
| 3992 | //find if its published by this process itself |
| 3993 | if(pr.mLocalProvider != null) { |
| 3994 | if(localLOGV) Log.i(TAG, "removeProvider::found local provider returning"); |
| 3995 | return; |
| 3996 | } |
| 3997 | if(localLOGV) Log.v(TAG, "removeProvider::Not local provider Unlinking " + |
| 3998 | "death recipient"); |
| 3999 | //content provider is in another process |
| 4000 | myBinder.unlinkToDeath(pr, 0); |
| 4001 | iter.remove(); |
| 4002 | //invoke remove only once for the very first name seen |
| 4003 | if(!amRemoveFlag) { |
| 4004 | try { |
| 4005 | if(localLOGV) Log.v(TAG, "removeProvider::Invoking " + |
| 4006 | "ActivityManagerNative.removeContentProvider("+pr.mName); |
| 4007 | ActivityManagerNative.getDefault().removeContentProvider(getApplicationThread(), pr.mName); |
| 4008 | amRemoveFlag = true; |
| 4009 | } catch (RemoteException e) { |
| 4010 | //do nothing content provider object is dead any way |
| 4011 | } //end catch |
| 4012 | } |
| 4013 | } //end if myBinder |
| 4014 | } //end while iter |
| 4015 | } |
| 4016 | |
| 4017 | final void removeDeadProvider(String name, IContentProvider provider) { |
| 4018 | synchronized(mProviderMap) { |
| 4019 | ProviderRecord pr = mProviderMap.get(name); |
| 4020 | if (pr.mProvider.asBinder() == provider.asBinder()) { |
| 4021 | Log.i(TAG, "Removing dead content provider: " + name); |
Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 4022 | ProviderRecord removed = mProviderMap.remove(name); |
| 4023 | if (removed != null) { |
| 4024 | removed.mProvider.asBinder().unlinkToDeath(removed, 0); |
| 4025 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4026 | } |
| 4027 | } |
| 4028 | } |
| 4029 | |
| 4030 | final void removeDeadProviderLocked(String name, IContentProvider provider) { |
| 4031 | ProviderRecord pr = mProviderMap.get(name); |
| 4032 | if (pr.mProvider.asBinder() == provider.asBinder()) { |
| 4033 | Log.i(TAG, "Removing dead content provider: " + name); |
Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 4034 | ProviderRecord removed = mProviderMap.remove(name); |
| 4035 | if (removed != null) { |
| 4036 | removed.mProvider.asBinder().unlinkToDeath(removed, 0); |
| 4037 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4038 | } |
| 4039 | } |
| 4040 | |
| 4041 | private final IContentProvider installProvider(Context context, |
| 4042 | IContentProvider provider, ProviderInfo info, boolean noisy) { |
| 4043 | ContentProvider localProvider = null; |
| 4044 | if (provider == null) { |
| 4045 | if (noisy) { |
| 4046 | Log.d(TAG, "Loading provider " + info.authority + ": " |
| 4047 | + info.name); |
| 4048 | } |
| 4049 | Context c = null; |
| 4050 | ApplicationInfo ai = info.applicationInfo; |
| 4051 | if (context.getPackageName().equals(ai.packageName)) { |
| 4052 | c = context; |
| 4053 | } else if (mInitialApplication != null && |
| 4054 | mInitialApplication.getPackageName().equals(ai.packageName)) { |
| 4055 | c = mInitialApplication; |
| 4056 | } else { |
| 4057 | try { |
| 4058 | c = context.createPackageContext(ai.packageName, |
| 4059 | Context.CONTEXT_INCLUDE_CODE); |
| 4060 | } catch (PackageManager.NameNotFoundException e) { |
| 4061 | } |
| 4062 | } |
| 4063 | if (c == null) { |
| 4064 | Log.w(TAG, "Unable to get context for package " + |
| 4065 | ai.packageName + |
| 4066 | " while loading content provider " + |
| 4067 | info.name); |
| 4068 | return null; |
| 4069 | } |
| 4070 | try { |
| 4071 | final java.lang.ClassLoader cl = c.getClassLoader(); |
| 4072 | localProvider = (ContentProvider)cl. |
| 4073 | loadClass(info.name).newInstance(); |
| 4074 | provider = localProvider.getIContentProvider(); |
| 4075 | if (provider == null) { |
| 4076 | Log.e(TAG, "Failed to instantiate class " + |
| 4077 | info.name + " from sourceDir " + |
| 4078 | info.applicationInfo.sourceDir); |
| 4079 | return null; |
| 4080 | } |
| 4081 | if (Config.LOGV) Log.v( |
| 4082 | TAG, "Instantiating local provider " + info.name); |
| 4083 | // XXX Need to create the correct context for this provider. |
| 4084 | localProvider.attachInfo(c, info); |
| 4085 | } catch (java.lang.Exception e) { |
| 4086 | if (!mInstrumentation.onException(null, e)) { |
| 4087 | throw new RuntimeException( |
| 4088 | "Unable to get provider " + info.name |
| 4089 | + ": " + e.toString(), e); |
| 4090 | } |
| 4091 | return null; |
| 4092 | } |
| 4093 | } else if (localLOGV) { |
| 4094 | Log.v(TAG, "Installing external provider " + info.authority + ": " |
| 4095 | + info.name); |
| 4096 | } |
| 4097 | |
| 4098 | synchronized (mProviderMap) { |
| 4099 | // Cache the pointer for the remote provider. |
| 4100 | String names[] = PATTERN_SEMICOLON.split(info.authority); |
| 4101 | for (int i=0; i<names.length; i++) { |
| 4102 | ProviderRecord pr = new ProviderRecord(names[i], provider, |
| 4103 | localProvider); |
| 4104 | try { |
| 4105 | provider.asBinder().linkToDeath(pr, 0); |
| 4106 | mProviderMap.put(names[i], pr); |
| 4107 | } catch (RemoteException e) { |
| 4108 | return null; |
| 4109 | } |
| 4110 | } |
| 4111 | if (localProvider != null) { |
| 4112 | mLocalProviders.put(provider.asBinder(), |
| 4113 | new ProviderRecord(null, provider, localProvider)); |
| 4114 | } |
| 4115 | } |
| 4116 | |
| 4117 | return provider; |
| 4118 | } |
| 4119 | |
| 4120 | private final void attach(boolean system) { |
| 4121 | sThreadLocal.set(this); |
| 4122 | mSystemThread = system; |
| 4123 | AndroidHttpClient.setThreadBlocked(true); |
| 4124 | if (!system) { |
| 4125 | android.ddm.DdmHandleAppName.setAppName("<pre-initialized>"); |
| 4126 | RuntimeInit.setApplicationObject(mAppThread.asBinder()); |
| 4127 | IActivityManager mgr = ActivityManagerNative.getDefault(); |
| 4128 | try { |
| 4129 | mgr.attachApplication(mAppThread); |
| 4130 | } catch (RemoteException ex) { |
| 4131 | } |
| 4132 | } else { |
| 4133 | // Don't set application object here -- if the system crashes, |
| 4134 | // we can't display an alert, we just want to die die die. |
| 4135 | android.ddm.DdmHandleAppName.setAppName("system_process"); |
| 4136 | try { |
| 4137 | mInstrumentation = new Instrumentation(); |
| 4138 | ApplicationContext context = new ApplicationContext(); |
| 4139 | context.init(getSystemContext().mPackageInfo, null, this); |
| 4140 | Application app = Instrumentation.newApplication(Application.class, context); |
| 4141 | mAllApplications.add(app); |
| 4142 | mInitialApplication = app; |
| 4143 | app.onCreate(); |
| 4144 | } catch (Exception e) { |
| 4145 | throw new RuntimeException( |
| 4146 | "Unable to instantiate Application():" + e.toString(), e); |
| 4147 | } |
| 4148 | } |
| 4149 | } |
| 4150 | |
| 4151 | private final void detach() |
| 4152 | { |
| 4153 | AndroidHttpClient.setThreadBlocked(false); |
| 4154 | sThreadLocal.set(null); |
| 4155 | } |
| 4156 | |
| 4157 | public static final ActivityThread systemMain() { |
| 4158 | ActivityThread thread = new ActivityThread(); |
| 4159 | thread.attach(true); |
| 4160 | return thread; |
| 4161 | } |
| 4162 | |
| 4163 | public final void installSystemProviders(List providers) { |
| 4164 | if (providers != null) { |
| 4165 | installContentProviders(mInitialApplication, |
| 4166 | (List<ProviderInfo>)providers); |
| 4167 | } |
| 4168 | } |
| 4169 | |
| 4170 | public static final void main(String[] args) { |
| 4171 | Process.setArgV0("<pre-initialized>"); |
| 4172 | |
| 4173 | Looper.prepareMainLooper(); |
| 4174 | |
| 4175 | ActivityThread thread = new ActivityThread(); |
| 4176 | thread.attach(false); |
| 4177 | |
| 4178 | Looper.loop(); |
| 4179 | |
| 4180 | if (Process.supportsProcesses()) { |
| 4181 | throw new RuntimeException("Main thread loop unexpectedly exited"); |
| 4182 | } |
| 4183 | |
| 4184 | thread.detach(); |
| 4185 | String name; |
| 4186 | if (thread.mInitialApplication != null) name = thread.mInitialApplication.getPackageName(); |
| 4187 | else name = "<unknown>"; |
| 4188 | Log.i(TAG, "Main thread of " + name + " is now exiting"); |
| 4189 | } |
| 4190 | } |