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