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