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