Merge "Fix for when flash loads cached crossdomain.xml files. Cache the x-permitted-cross-domain-policies header. http://b/issue?id=2519669"
diff --git a/api/current.xml b/api/current.xml
index 58ce3ee..868f6fc 100644
--- a/api/current.xml
+++ b/api/current.xml
@@ -88413,7 +88413,7 @@
  static="true"
  final="false"
  deprecated="not deprecated"
- visibility="private"
+ visibility="public"
 >
 <method name="startDownloadByUri"
  return="long"
@@ -193673,6 +193673,17 @@
  visibility="public"
 >
 </method>
+<method name="getPluginState"
+ return="android.webkit.WebSettings.PluginState"
+ abstract="false"
+ native="false"
+ synchronized="true"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</method>
 <method name="getPluginsEnabled"
  return="boolean"
  abstract="false"
@@ -193680,7 +193691,7 @@
  synchronized="true"
  static="false"
  final="false"
- deprecated="not deprecated"
+ deprecated="deprecated"
  visibility="public"
 >
 </method>
@@ -194206,7 +194217,7 @@
 <parameter name="flag" type="boolean">
 </parameter>
 </method>
-<method name="setPluginsEnabled"
+<method name="setPluginState"
  return="void"
  abstract="false"
  native="false"
@@ -194216,6 +194227,19 @@
  deprecated="not deprecated"
  visibility="public"
 >
+<parameter name="state" type="android.webkit.WebSettings.PluginState">
+</parameter>
+</method>
+<method name="setPluginsEnabled"
+ return="void"
+ abstract="false"
+ native="false"
+ synchronized="true"
+ static="false"
+ final="false"
+ deprecated="deprecated"
+ visibility="public"
+>
 <parameter name="flag" type="boolean">
 </parameter>
 </method>
@@ -194525,6 +194549,39 @@
 >
 </method>
 </class>
+<class name="WebSettings.PluginState"
+ extends="java.lang.Enum"
+ abstract="false"
+ static="true"
+ final="true"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<method name="valueOf"
+ return="android.webkit.WebSettings.PluginState"
+ abstract="false"
+ native="false"
+ synchronized="false"
+ static="true"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="name" type="java.lang.String">
+</parameter>
+</method>
+<method name="values"
+ return="android.webkit.WebSettings.PluginState[]"
+ abstract="false"
+ native="false"
+ synchronized="false"
+ static="true"
+ final="true"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</method>
+</class>
 <class name="WebSettings.RenderPriority"
  extends="java.lang.Enum"
  abstract="false"
diff --git a/core/java/android/app/ActivityManagerNative.java b/core/java/android/app/ActivityManagerNative.java
index 596ca9d..adadfeb 100644
--- a/core/java/android/app/ActivityManagerNative.java
+++ b/core/java/android/app/ActivityManagerNative.java
@@ -168,6 +168,29 @@
             return true;
         }
 
+        case START_ACTIVITY_WITH_CONFIG_TRANSACTION:
+        {
+            data.enforceInterface(IActivityManager.descriptor);
+            IBinder b = data.readStrongBinder();
+            IApplicationThread app = ApplicationThreadNative.asInterface(b);
+            Intent intent = Intent.CREATOR.createFromParcel(data);
+            String resolvedType = data.readString();
+            Uri[] grantedUriPermissions = data.createTypedArray(Uri.CREATOR);
+            int grantedMode = data.readInt();
+            IBinder resultTo = data.readStrongBinder();
+            String resultWho = data.readString();    
+            int requestCode = data.readInt();
+            boolean onlyIfNeeded = data.readInt() != 0;
+            boolean debug = data.readInt() != 0;
+            Configuration config = Configuration.CREATOR.createFromParcel(data);
+            int result = startActivityWithConfig(app, intent, resolvedType,
+                    grantedUriPermissions, grantedMode, resultTo, resultWho,
+                    requestCode, onlyIfNeeded, debug, config);
+            reply.writeNoException();
+            reply.writeInt(result);
+            return true;
+        }
+
         case START_ACTIVITY_INTENT_SENDER_TRANSACTION:
         {
             data.enforceInterface(IActivityManager.descriptor);
@@ -1295,6 +1318,32 @@
         data.recycle();
         return result;
     }
+    public int startActivityWithConfig(IApplicationThread caller, Intent intent,
+            String resolvedType, Uri[] grantedUriPermissions, int grantedMode,
+            IBinder resultTo, String resultWho,
+            int requestCode, boolean onlyIfNeeded,
+            boolean debug, Configuration config) throws RemoteException {
+        Parcel data = Parcel.obtain();
+        Parcel reply = Parcel.obtain();
+        data.writeInterfaceToken(IActivityManager.descriptor);
+        data.writeStrongBinder(caller != null ? caller.asBinder() : null);
+        intent.writeToParcel(data, 0);
+        data.writeString(resolvedType);
+        data.writeTypedArray(grantedUriPermissions, 0);
+        data.writeInt(grantedMode);
+        data.writeStrongBinder(resultTo);
+        data.writeString(resultWho);
+        data.writeInt(requestCode);
+        data.writeInt(onlyIfNeeded ? 1 : 0);
+        data.writeInt(debug ? 1 : 0);
+        config.writeToParcel(data, 0);
+        mRemote.transact(START_ACTIVITY_TRANSACTION, data, reply, 0);
+        reply.readException();
+        int result = reply.readInt();
+        reply.recycle();
+        data.recycle();
+        return result;
+    }
     public int startActivityIntentSender(IApplicationThread caller,
             IntentSender intent, Intent fillInIntent, String resolvedType,
             IBinder resultTo, String resultWho, int requestCode,
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java
index fa6abec..a556a32 100644
--- a/core/java/android/app/ActivityThread.java
+++ b/core/java/android/app/ActivityThread.java
@@ -278,6 +278,10 @@
 
         int mClientCount = 0;
 
+        Application getApplication() {
+            return mApplication;
+        }
+
         public PackageInfo(ActivityThread activityThread, ApplicationInfo aInfo,
                 ActivityThread mainThread, ClassLoader baseLoader,
                 boolean securityViolation, boolean includeCode) {
@@ -648,7 +652,7 @@
             }
             mActivityThread.mAllApplications.add(app);
             mApplication = app;
-            
+
             if (instrumentation != null) {
                 try {
                     instrumentation.callApplicationOnCreate(app);
diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java
index f296f43..4c2b36f 100644
--- a/core/java/android/app/ContextImpl.java
+++ b/core/java/android/app/ContextImpl.java
@@ -260,7 +260,8 @@
     
     @Override
     public Context getApplicationContext() {
-        return mMainThread.getApplication();
+        return (mPackageInfo != null) ?
+                mPackageInfo.getApplication() : mMainThread.getApplication();
     }
     
     @Override
diff --git a/core/java/android/app/IActivityManager.java b/core/java/android/app/IActivityManager.java
index 30feae1..ea0e952 100644
--- a/core/java/android/app/IActivityManager.java
+++ b/core/java/android/app/IActivityManager.java
@@ -88,6 +88,10 @@
             Intent intent, String resolvedType, Uri[] grantedUriPermissions,
             int grantedMode, IBinder resultTo, String resultWho, int requestCode,
             boolean onlyIfNeeded, boolean debug) throws RemoteException;
+    public int startActivityWithConfig(IApplicationThread caller,
+            Intent intent, String resolvedType, Uri[] grantedUriPermissions,
+            int grantedMode, IBinder resultTo, String resultWho, int requestCode,
+            boolean onlyIfNeeded, boolean debug, Configuration newConfig) throws RemoteException;
     public int startActivityIntentSender(IApplicationThread caller,
             IntentSender intent, Intent fillInIntent, String resolvedType,
             IBinder resultTo, String resultWho, int requestCode,
@@ -503,4 +507,5 @@
     int IS_USER_A_MONKEY_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+103;
     int START_ACTIVITY_AND_WAIT_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+104;
     int WILL_ACTIVITY_BE_VISIBLE_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+105;
+    int START_ACTIVITY_WITH_CONFIG_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+106;
 }
diff --git a/core/java/android/content/ContentValues.java b/core/java/android/content/ContentValues.java
index c04625d..75787cd 100644
--- a/core/java/android/content/ContentValues.java
+++ b/core/java/android/content/ContentValues.java
@@ -66,7 +66,7 @@
      * Creates a set of values copied from the given HashMap. This is used
      * by the Parcel unmarshalling code.
      *
-     * @param from the values to start with
+     * @param values the values to start with
      * {@hide}
      */
     private ContentValues(HashMap<String, Object> values) {
@@ -248,7 +248,7 @@
      */
     public String getAsString(String key) {
         Object value = mValues.get(key);
-        return value != null ? mValues.get(key).toString() : null;
+        return value != null ? value.toString() : null;
     }
 
     /**
diff --git a/core/java/android/database/sqlite/SQLiteCursor.java b/core/java/android/database/sqlite/SQLiteCursor.java
index 3f0fcb1..6e5b3e1 100644
--- a/core/java/android/database/sqlite/SQLiteCursor.java
+++ b/core/java/android/database/sqlite/SQLiteCursor.java
@@ -579,10 +579,12 @@
         try {
             // if the cursor hasn't been closed yet, close it first
             if (mWindow != null) {
-                close();
+                int len = mQuery.mSql.length();
                 Log.e(TAG, "Finalizing a Cursor that has not been deactivated or closed. " +
                         "database = " + mDatabase.getPath() + ", table = " + mEditTable +
-                        ", query = " + mQuery.mSql, mStackTrace);
+                        ", query = " + mQuery.mSql.substring(0, (len > 100) ? 100 : len),
+                        mStackTrace);
+                close();
                 SQLiteDebug.notifyActiveCursorFinalized();
             } else {
                 if (Config.LOGV) {
diff --git a/core/java/android/net/Downloads.java b/core/java/android/net/Downloads.java
index 72106c8..3867385 100644
--- a/core/java/android/net/Downloads.java
+++ b/core/java/android/net/Downloads.java
@@ -469,7 +469,7 @@
     /**
      * Base class with common functionality for the various download classes
      */
-    private static class DownloadBase {
+    public static class DownloadBase {
         /** @hide */
         DownloadBase() {}
 
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index 13f8780..2ba38c23 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -3274,6 +3274,14 @@
                 INCALL_POWER_BUTTON_BEHAVIOR_SCREEN_OFF;
 
         /**
+         * The current night mode that has been selected by the user.  Owned
+         * and controlled by UiModeManagerService.  Constants are as per
+         * UiModeManager.
+         * @hide
+         */
+        public static final String UI_NIGHT_MODE = "ui_night_mode";
+        
+        /**
          * @hide
          */
         public static final String[] SETTINGS_TO_BACKUP = {
@@ -3299,7 +3307,8 @@
             MOUNT_PLAY_NOTIFICATION_SND,
             MOUNT_UMS_AUTOSTART,
             MOUNT_UMS_PROMPT,
-            MOUNT_UMS_NOTIFY_ENABLED
+            MOUNT_UMS_NOTIFY_ENABLED,
+            UI_NIGHT_MODE
         };
 
         /**
diff --git a/core/java/android/view/Surface.java b/core/java/android/view/Surface.java
index 183fce3..255e317 100644
--- a/core/java/android/view/Surface.java
+++ b/core/java/android/view/Surface.java
@@ -28,6 +28,7 @@
  */
 public class Surface implements Parcelable {
     private static final String LOG_TAG = "Surface";
+    private static final boolean DEBUG_RELEASE = false;
     
     /* flags used in constructor (keep in sync with ISurfaceComposer.h) */
 
@@ -155,6 +156,9 @@
     // non compatibility mode.
     private Matrix mCompatibleMatrix;
 
+    @SuppressWarnings("unused")
+    private Exception mCreationStack;
+
     /**
      * Exception thrown when a surface couldn't be created or resized
      */
@@ -181,6 +185,9 @@
     public Surface(SurfaceSession s,
             int pid, int display, int w, int h, int format, int flags)
         throws OutOfResourcesException {
+        if (DEBUG_RELEASE) {
+            mCreationStack = new Exception();
+        }
         mCanvas = new CompatibleCanvas();
         init(s,pid,null,display,w,h,format,flags);
     }
@@ -192,6 +199,9 @@
     public Surface(SurfaceSession s,
             int pid, String name, int display, int w, int h, int format, int flags)
         throws OutOfResourcesException {
+        if (DEBUG_RELEASE) {
+            mCreationStack = new Exception();
+        }
         mCanvas = new CompatibleCanvas();
         init(s,pid,name,display,w,h,format,flags);
     }
@@ -202,6 +212,9 @@
      * {@hide}
      */
     public Surface() {
+        if (DEBUG_RELEASE) {
+            mCreationStack = new Exception();
+        }
         mCanvas = new CompatibleCanvas();
     }
 
@@ -407,6 +420,15 @@
     /* no user serviceable parts here ... */
     @Override
     protected void finalize() throws Throwable {
+        if (mSurface != 0 || mSurfaceControl != 0) {
+            if (DEBUG_RELEASE) {
+                Log.w(LOG_TAG, "Surface.finalize() has work. You should have called release() (" 
+                        + mSurface + ", " + mSurfaceControl + ")", mCreationStack);
+            } else {
+                Log.w(LOG_TAG, "Surface.finalize() has work. You should have called release() (" 
+                        + mSurface + ", " + mSurfaceControl + ")");
+            }
+        }
         release();
     }
     
diff --git a/core/java/android/view/SurfaceView.java b/core/java/android/view/SurfaceView.java
index c44854a..7f7d207 100644
--- a/core/java/android/view/SurfaceView.java
+++ b/core/java/android/view/SurfaceView.java
@@ -422,7 +422,7 @@
                 if (visibleChanged && (!visible || mNewSurfaceNeeded)) {
                     reportSurfaceDestroyed();
                 }
-                
+
                 mNewSurfaceNeeded = false;
                 
                 mSurfaceLock.lock();
@@ -470,6 +470,8 @@
                                 c.surfaceChanged(mSurfaceHolder, mFormat, mWidth, mHeight);
                             }
                         }
+                    } else {
+                        mSurface.release();
                     }
                 } finally {
                     mIsCreating = false;
diff --git a/core/java/android/view/ViewRoot.java b/core/java/android/view/ViewRoot.java
index 9df8ac3..74943f3 100644
--- a/core/java/android/view/ViewRoot.java
+++ b/core/java/android/view/ViewRoot.java
@@ -1500,8 +1500,10 @@
                         focus.getFocusedRect(mTempRect);
                         if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Focus " + focus
                                 + ": focusRect=" + mTempRect.toShortString());
-                        ((ViewGroup) mView).offsetDescendantRectToMyCoords(
-                                focus, mTempRect);
+                        if (mView instanceof ViewGroup) {
+                            ((ViewGroup) mView).offsetDescendantRectToMyCoords(
+                                    focus, mTempRect);
+                        }
                         if (DEBUG_INPUT_RESIZE) Log.v(TAG,
                                 "Focus in window: focusRect="
                                 + mTempRect.toShortString()
@@ -2491,8 +2493,12 @@
                                 // of previous focused into the coord system of
                                 // newly focused view
                                 focused.getFocusedRect(mTempRect);
-                                ((ViewGroup) mView).offsetDescendantRectToMyCoords(focused, mTempRect);
-                                ((ViewGroup) mView).offsetRectIntoDescendantCoords(v, mTempRect);
+                                if (mView instanceof ViewGroup) {
+                                    ((ViewGroup) mView).offsetDescendantRectToMyCoords(
+                                            focused, mTempRect);
+                                    ((ViewGroup) mView).offsetRectIntoDescendantCoords(
+                                            v, mTempRect);
+                                }
                                 focusPassed = v.requestFocus(direction, mTempRect);
                             }
 
diff --git a/core/java/android/webkit/ViewManager.java b/core/java/android/webkit/ViewManager.java
index fc5c425..23cf6b8 100644
--- a/core/java/android/webkit/ViewManager.java
+++ b/core/java/android/webkit/ViewManager.java
@@ -28,6 +28,7 @@
     private final ArrayList<ChildView> mChildren = new ArrayList<ChildView>();
     private boolean mHidden;
     private boolean mReadyToDraw;
+    private boolean mZoomInProgress = false;
 
     // Threshold at which a surface is prevented from further increasing in size
     private final int MAX_SURFACE_THRESHOLD;
@@ -39,11 +40,6 @@
         int height;
         View mView; // generic view to show
 
-        /* set to true if the view is a surface and it has exceeded the pixel
-           threshold specified in MAX_SURFACE_THRESHOLD.
-         */
-        boolean isFixedSize = false;
-
         ChildView() {
         }
 
@@ -66,19 +62,17 @@
                     // already attached, just set the new LayoutParams,
                     // otherwise attach the view and add it to the list of
                     // children.
-                    AbsoluteLayout.LayoutParams lp = computeLayout(ChildView.this);
+                    requestLayout(ChildView.this);
 
-                    if (mView.getParent() != null) {
-                        mView.setLayoutParams(lp);
-                    } else {
-                        attachViewOnUIThread(lp);
+                    if (mView.getParent() == null) {
+                        attachViewOnUIThread();
                     }
                 }
             });
         }
 
-        private void attachViewOnUIThread(AbsoluteLayout.LayoutParams lp) {
-            mWebView.addView(mView, lp);
+        private void attachViewOnUIThread() {
+            mWebView.addView(mView);
             mChildren.add(this);
             if (!mReadyToDraw) {
                 mView.setVisibility(View.GONE);
@@ -146,35 +140,87 @@
     /**
      * This should only be called from the UI thread.
      */
-    private AbsoluteLayout.LayoutParams computeLayout(ChildView v) {
+    private void requestLayout(ChildView v) {
 
-        // if the surface has exceed a predefined threshold then fix the size
-        // of the surface.
-        if (!v.isFixedSize && (v.width * v.height) > MAX_SURFACE_THRESHOLD
-                && v.mView instanceof SurfaceView) {
-            ((SurfaceView)v.mView).getHolder().setFixedSize(v.width, v.height);
-            v.isFixedSize = true;
-        }
+        int width = ctvD(v.width);
+        int height = ctvD(v.height);
+        int x = ctvX(v.x);
+        int y = ctvY(v.y);
 
         AbsoluteLayout.LayoutParams lp;
         ViewGroup.LayoutParams layoutParams = v.mView.getLayoutParams();
 
         if (layoutParams instanceof AbsoluteLayout.LayoutParams) {
             lp = (AbsoluteLayout.LayoutParams) layoutParams;
-            lp.width = ctvD(v.width);
-            lp.height = ctvD(v.height);
-            lp.x = ctvX(v.x);
-            lp.y = ctvY(v.y);
+            lp.width = width;
+            lp.height = height;
+            lp.x = x;
+            lp.y = y;
         } else {
-            lp = new AbsoluteLayout.LayoutParams(ctvD(v.width), ctvD(v.height),
-                    ctvX(v.x), ctvY(v.y));
+            lp = new AbsoluteLayout.LayoutParams(width, height, x, y);
         }
-        return lp;
+
+        // apply the layout to the view
+        v.mView.setLayoutParams(lp);
+
+        if(v.mView instanceof SurfaceView) {
+
+            final SurfaceView sView = (SurfaceView) v.mView;
+            boolean exceedThreshold = (width * height) > MAX_SURFACE_THRESHOLD;
+
+            /* If the surface has exceeded a predefined threshold or the webview
+             * is currently zoom then fix the size of the surface.
+             *
+             * NOTE: plugins (e.g. Flash) must not explicitly fix the size of
+             * their surface. The logic below will result in unexpected behavior
+             * for the plugin if they attempt to fix the size of the surface.
+             */
+            if (!sView.isFixedSize() && (exceedThreshold || mZoomInProgress)) {
+                sView.getHolder().setFixedSize(width, height);
+            }
+            else if (sView.isFixedSize() && !exceedThreshold && !mZoomInProgress) {
+                /* The changing of visibility is a hack to get around a bug in
+                 * the framework that causes the surface to revert to the size
+                 * it was prior to being fixed before it redraws using the
+                 * values currently in its layout.
+                 *
+                 * The surface is destroyed when it is set to invisible and then
+                 * recreated at the new dimensions when it is made visible. The
+                 * same destroy/create step occurs without the change in
+                 * visibility, but then exhibits the behavior described in the
+                 * previous paragraph.
+                 */
+                if (sView.getVisibility() == View.VISIBLE) {
+                    sView.setVisibility(View.INVISIBLE);
+                    sView.getHolder().setSizeFromLayout();
+                    sView.setVisibility(View.VISIBLE);
+                } else {
+                    sView.getHolder().setSizeFromLayout();
+                }
+            }
+            else if (sView.isFixedSize() && exceedThreshold) {
+                sView.requestLayout();
+            }
+        }
+    }
+
+    void startZoom() {
+        mZoomInProgress = true;
+        for (ChildView v : mChildren) {
+            requestLayout(v);
+        }
+    }
+
+    void endZoom() {
+        mZoomInProgress = false;
+        for (ChildView v : mChildren) {
+            requestLayout(v);
+        }
     }
 
     void scaleAll() {
         for (ChildView v : mChildren) {
-            v.mView.setLayoutParams(computeLayout(v));
+            requestLayout(v);
         }
     }
 
diff --git a/core/java/android/webkit/WebSettings.java b/core/java/android/webkit/WebSettings.java
index d1da5ea..1b801d4 100644
--- a/core/java/android/webkit/WebSettings.java
+++ b/core/java/android/webkit/WebSettings.java
@@ -120,6 +120,21 @@
         LOW
     }
 
+    /**
+     * The plugin state effects how plugins are treated on a page. ON means
+     * that any object will be loaded even if a plugin does not exist to handle
+     * the content. ON_DEMAND means that if there is a plugin installed that
+     * can handle the content, a placeholder is shown until the user clicks on
+     * the placeholder. Once clicked, the plugin will be enabled on the page.
+     * OFF means that all plugins will be turned off and any fallback content
+     * will be used.
+     */
+    public enum PluginState {
+        ON,
+        ON_DEMAND,
+        OFF
+    }
+
     // WebView associated with this WebSettings.
     private WebView mWebView;
     // BrowserFrame used to access the native frame pointer.
@@ -157,7 +172,7 @@
     private boolean         mBlockNetworkImage = false;
     private boolean         mBlockNetworkLoads;
     private boolean         mJavaScriptEnabled = false;
-    private boolean         mPluginsEnabled = false;
+    private PluginState     mPluginState = PluginState.OFF;
     private boolean         mJavaScriptCanOpenWindowsAutomatically = false;
     private boolean         mUseDoubleTree = false;
     private boolean         mUseWideViewport = false;
@@ -1011,10 +1026,23 @@
     /**
      * Tell the WebView to enable plugins.
      * @param flag True if the WebView should load plugins.
+     * @deprecated This method has been deprecated in favor of
+     *             {@link #setPluginState}
      */
     public synchronized void setPluginsEnabled(boolean flag) {
-        if (mPluginsEnabled != flag) {
-            mPluginsEnabled = flag;
+        setPluginState(PluginState.ON);
+    }
+
+    /**
+     * Tell the WebView to enable, disable, or have plugins on demand. On
+     * demand mode means that if a plugin exists that can handle the embedded
+     * content, a placeholder icon will be shown instead of the plugin. When
+     * the placeholder is clicked, the plugin will be enabled.
+     * @param state One of the PluginState values.
+     */
+    public synchronized void setPluginState(PluginState state) {
+        if (mPluginState != state) {
+            mPluginState = state;
             postSync();
         }
     }
@@ -1176,9 +1204,18 @@
     /**
      * Return true if plugins are enabled.
      * @return True if plugins are enabled.
+     * @deprecated This method has been replaced by {@link #getPluginState}
      */
     public synchronized boolean getPluginsEnabled() {
-        return mPluginsEnabled;
+        return mPluginState == PluginState.ON;
+    }
+
+    /**
+     * Return the current plugin state.
+     * @return A value corresponding to the enum PluginState.
+     */
+    public synchronized PluginState getPluginState() {
+        return mPluginState;
     }
 
     /**
diff --git a/core/java/android/webkit/WebView.java b/core/java/android/webkit/WebView.java
index b5eb5d5..714ae70 100644
--- a/core/java/android/webkit/WebView.java
+++ b/core/java/android/webkit/WebView.java
@@ -1668,7 +1668,7 @@
         }
         nativeClearCursor(); // start next trackball movement from page edge
         if (bottom) {
-            return pinScrollTo(mScrollX, computeVerticalScrollRange(), true, 0);
+            return pinScrollTo(mScrollX, computeRealVerticalScrollRange(), true, 0);
         }
         // Page down.
         int h = getHeight();
@@ -1907,7 +1907,7 @@
     private int pinLocY(int y) {
         if (mInOverScrollMode) return y;
         return pinLoc(y, getViewHeightWithTitle(),
-                      computeVerticalScrollRange() + getTitleHeight());
+                      computeRealVerticalScrollRange() + getTitleHeight());
     }
 
     /**
@@ -2258,8 +2258,7 @@
         return false;
     }
 
-    @Override
-    protected int computeHorizontalScrollRange() {
+    private int computeRealHorizontalScrollRange() {
         if (mDrawHistory) {
             return mHistoryWidth;
         } else {
@@ -2269,7 +2268,27 @@
     }
 
     @Override
-    protected int computeVerticalScrollRange() {
+    protected int computeHorizontalScrollRange() {
+        int range = computeRealHorizontalScrollRange();
+
+        // Adjust reported range if overscrolled to compress the scroll bars
+        final int scrollX = mScrollX;
+        final int overscrollRight = computeMaxScrollX();
+        if (scrollX < 0) {
+            range -= scrollX;
+        } else if (scrollX > overscrollRight) {
+            range += scrollX - overscrollRight;
+        }
+
+        return range;
+    }
+
+    @Override
+    protected int computeHorizontalScrollOffset() {
+        return Math.max(mScrollX, 0);
+    }
+
+    private int computeRealVerticalScrollRange() {
         if (mDrawHistory) {
             return mHistoryHeight;
         } else {
@@ -2279,6 +2298,22 @@
     }
 
     @Override
+    protected int computeVerticalScrollRange() {
+        int range = computeRealVerticalScrollRange();
+
+        // Adjust reported range if overscrolled to compress the scroll bars
+        final int scrollY = mScrollY;
+        final int overscrollBottom = computeMaxScrollY();
+        if (scrollY < 0) {
+            range -= scrollY;
+        } else if (scrollY > overscrollBottom) {
+            range += scrollY - overscrollBottom;
+        }
+
+        return range;
+    }
+
+    @Override
     protected int computeVerticalScrollOffset() {
         return Math.max(mScrollY - getTitleHeight(), 0);
     }
@@ -3134,14 +3169,14 @@
             canvas.save();
             canvas.translate(mScrollX, mScrollY);
             canvas.clipRect(-mScrollX, top - mScrollY,
-                    computeHorizontalScrollRange() - mScrollX, top
-                            + computeVerticalScrollRange() - mScrollY,
+                    computeRealHorizontalScrollRange() - mScrollX, top
+                            + computeRealVerticalScrollRange() - mScrollY,
                     Region.Op.DIFFERENCE);
             canvas.drawPaint(mOverScrollBackground);
             canvas.restore();
             // next clip the region for the content
-            canvas.clipRect(0, top, computeHorizontalScrollRange(), top
-                    + computeVerticalScrollRange());
+            canvas.clipRect(0, top, computeRealHorizontalScrollRange(), top
+                    + computeRealVerticalScrollRange());
         }
         if (mTitleBar != null) {
             canvas.translate(0, (int) mTitleBar.getHeight());
@@ -4195,7 +4230,15 @@
     @Override
     protected void onScrollChanged(int l, int t, int oldl, int oldt) {
         super.onScrollChanged(l, t, oldl, oldt);
-        sendOurVisibleRect();
+        if (!mInOverScrollMode) {
+            sendOurVisibleRect();
+            // update WebKit if visible title bar height changed. The logic is same
+            // as getVisibleTitleHeight.
+            int titleHeight = getTitleHeight();
+            if (Math.max(titleHeight - t, 0) != Math.max(titleHeight - oldt, 0)) {
+                sendViewSizeZoom();
+            }
+        }
     }
 
     @Override
@@ -4264,7 +4307,7 @@
         public DragTrackerHandler(float x, float y, DragTracker proxy) {
             mProxy = proxy;
 
-            int docBottom = computeVerticalScrollRange() + getTitleHeight();
+            int docBottom = computeRealVerticalScrollRange() + getTitleHeight();
             int viewTop = getScrollY();
             int viewBottom = viewTop + getHeight();
 
@@ -4452,6 +4495,9 @@
             if (inEditingMode() && nativeFocusCandidateIsPassword()) {
                 mWebTextView.setInPassword(false);
             }
+
+            mViewManager.startZoom();
+
             return true;
         }
 
@@ -4485,6 +4531,8 @@
             mConfirmMove = true;
             startTouch(detector.getFocusX(), detector.getFocusY(),
                     mLastTouchTime);
+
+            mViewManager.endZoom();
         }
 
         public boolean onScale(ScaleGestureDetector detector) {
@@ -5351,11 +5399,11 @@
     }
 
     private int computeMaxScrollX() {
-        return Math.max(computeHorizontalScrollRange() - getViewWidth(), 0);
+        return Math.max(computeRealHorizontalScrollRange() - getViewWidth(), 0);
     }
 
     private int computeMaxScrollY() {
-        return Math.max(computeVerticalScrollRange() + getTitleHeight()
+        return Math.max(computeRealVerticalScrollRange() + getTitleHeight()
                 - getViewHeightWithTitle(), 0);
     }
 
diff --git a/core/java/android/widget/AbsListView.java b/core/java/android/widget/AbsListView.java
index a30059c..9150b5e 100644
--- a/core/java/android/widget/AbsListView.java
+++ b/core/java/android/widget/AbsListView.java
@@ -2601,8 +2601,9 @@
         }
 
         void startOverfling(int initialVelocity) {
-            mScroller.fling(0, mScrollY, 0, initialVelocity, 0, 0, 0, 0, 0, getHeight());
-            edgeReached();
+            final int min = mScrollY > 0 ? Integer.MIN_VALUE : 0;
+            final int max = mScrollY > 0 ? 0 : Integer.MAX_VALUE;
+            mScroller.fling(0, mScrollY, 0, initialVelocity, 0, 0, min, max, 0, getHeight());
             mTouchMode = TOUCH_MODE_OVERFLING;
             invalidate();
             post(this);
diff --git a/core/java/android/widget/AutoCompleteTextView.java b/core/java/android/widget/AutoCompleteTextView.java
index eb2da71..e15a520 100644
--- a/core/java/android/widget/AutoCompleteTextView.java
+++ b/core/java/android/widget/AutoCompleteTextView.java
@@ -92,6 +92,13 @@
 
     private static final int HINT_VIEW_ID = 0x17;
 
+    /**
+     * This value controls the length of time that the user
+     * must leave a pointer down without scrolling to expand
+     * the autocomplete dropdown list to cover the IME.
+     */
+    private static final int EXPAND_LIST_TIMEOUT = 250;
+
     private CharSequence mHintText;
     private int mHintResource;
 
@@ -132,6 +139,7 @@
 
     private ListSelectorHider mHideSelector;
     private Runnable mShowDropDownRunnable;
+    private Runnable mResizePopupRunnable = new ResizePopupRunnable();
 
     private PassThroughClickListener mPassThroughClickListener;
     private PopupDataSetObserver mObserver;
@@ -1297,6 +1305,7 @@
                 public void onNothingSelected(AdapterView<?> parent) {
                 }
             });
+            mDropDownList.setOnScrollListener(new PopupScrollListener());
 
             if (mItemSelectedListener != null) {
                 mDropDownList.setOnItemSelectedListener(mItemSelectedListener);
@@ -1437,17 +1446,41 @@
         }
     }
 
+    private class ResizePopupRunnable implements Runnable {
+        public void run() {
+            mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
+            showDropDown();
+        }
+    }
+
     private class PopupTouchInterceptor implements OnTouchListener {
         public boolean onTouch(View v, MotionEvent event) {
-            if (event.getAction() == MotionEvent.ACTION_DOWN &&
+            final int action = event.getAction();
+            if (action == MotionEvent.ACTION_DOWN &&
                     mPopup != null && mPopup.isShowing()) {
-                mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
-                showDropDown();
+                postDelayed(mResizePopupRunnable, EXPAND_LIST_TIMEOUT);
+            } else if (action == MotionEvent.ACTION_UP) {
+                removeCallbacks(mResizePopupRunnable);
             }
             return false;
         }
     }
     
+    private class PopupScrollListener implements ListView.OnScrollListener {
+        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
+                int totalItemCount) {
+
+        }
+
+        public void onScrollStateChanged(AbsListView view, int scrollState) {
+            if (scrollState == SCROLL_STATE_TOUCH_SCROLL &&
+                    !isInputMethodNotNeeded() && mPopup.getContentView() != null) {
+                removeCallbacks(mResizePopupRunnable);
+                mResizePopupRunnable.run();
+            }
+        }
+    }
+
     private class DropDownItemClickListener implements AdapterView.OnItemClickListener {
         public void onItemClick(AdapterView parent, View v, int position, long id) {
             performCompletion(v, position, id);
diff --git a/core/java/android/widget/DateTimeView.java b/core/java/android/widget/DateTimeView.java
index 3dab9f2..6b845b0 100644
--- a/core/java/android/widget/DateTimeView.java
+++ b/core/java/android/widget/DateTimeView.java
@@ -206,7 +206,12 @@
         if (format == null || "".equals(format)) {
             return DateFormat.getDateInstance(DateFormat.SHORT);
         } else {
-            return new SimpleDateFormat(format);
+            try {
+                return new SimpleDateFormat(format);
+            } catch (IllegalArgumentException e) {
+                // If we tried to use a bad format string, fall back to a default.
+                return DateFormat.getDateInstance(DateFormat.SHORT);
+            }
         }
     }
 
diff --git a/core/java/android/widget/OverScroller.java b/core/java/android/widget/OverScroller.java
index 6258024..48e7493 100644
--- a/core/java/android/widget/OverScroller.java
+++ b/core/java/android/widget/OverScroller.java
@@ -42,11 +42,31 @@
     }
 
     /**
-     * Creates a Scroller with the specified interpolator. If the interpolator is
-     * null, the default (viscous) interpolator will be used.
+     * Creates an OverScroller with default edge bounce coefficients.
+     * @param context The context of this application.
+     * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will
+     * be used.
      */
     public OverScroller(Context context, Interpolator interpolator) {
+        this(context, interpolator, MagneticOverScroller.DEFAULT_BOUNCE_COEFFICIENT,
+                MagneticOverScroller.DEFAULT_BOUNCE_COEFFICIENT);
+    }
+
+    /**
+     * Creates an OverScroller.
+     * @param context The context of this application.
+     * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will
+     * be used.
+     * @param bounceCoefficientX A value between 0 and 1 that will determine the proportion of the
+     * velocity which is preserved in the bounce when the horizontal edge is reached. A null value
+     * means no bounce.
+     * @param bounceCoefficientY Same as bounceCoefficientX but for the vertical direction.
+     */
+    public OverScroller(Context context, Interpolator interpolator,
+            float bounceCoefficientX, float bounceCoefficientY) {
         super(context, interpolator);
+        mOverScrollerX.setBounceCoefficient(bounceCoefficientX);
+        mOverScrollerY.setBounceCoefficient(bounceCoefficientY);
     }
 
     @Override
@@ -69,8 +89,11 @@
      */
     public boolean springback(int startX, int startY, int minX, int maxX, int minY, int maxY) {
         mMode = FLING_MODE;
-        return mOverScrollerX.springback(startX, minX, maxX)
-                || mOverScrollerY.springback(startY, minY, maxY);
+
+        // Make sure both methods are called.
+        final boolean spingbackX = mOverScrollerX.springback(startX, minX, maxX);
+        final boolean spingbackY = mOverScrollerY.springback(startY, minY, maxY);
+        return spingbackX || spingbackY;
     }
 
     @Override
@@ -130,8 +153,8 @@
     }
 
     /**
-     * Returns whether the current Scroller position is overscrolled or still within the minimum and
-     * maximum bounds provided in the
+     * Returns whether the current Scroller is currently returning to a valid position.
+     * Valid bounds were provided by the
      * {@link #fling(int, int, int, int, int, int, int, int, int, int)} method.
      * 
      * One should check this value before calling
@@ -139,11 +162,13 @@
      * a valid position will then be stopped. The caller has to take into account the fact that the
      * started scroll will start from an overscrolled position.
      * 
-     * @return true when the current position is overscrolled.
+     * @return true when the current position is overscrolled and interpolated back to a valid value.
      */
     public boolean isOverscrolled() {
-        return ((!mOverScrollerX.mFinished && mOverScrollerX.mState != MagneticOverScroller.TO_EDGE) ||
-                (!mOverScrollerY.mFinished && mOverScrollerY.mState != MagneticOverScroller.TO_EDGE));
+        return ((!mOverScrollerX.mFinished &&
+                mOverScrollerX.mState != MagneticOverScroller.TO_EDGE) ||
+                (!mOverScrollerY.mFinished &&
+                        mOverScrollerY.mState != MagneticOverScroller.TO_EDGE));
     }
 
     static class MagneticOverScroller extends Scroller.MagneticScroller {
@@ -156,30 +181,25 @@
         // The allowed overshot distance before boundary is reached.
         private int mOver;
 
-        // When the scroll goes beyond the edges limits, the deceleration is
-        // multiplied by this coefficient, so that the return to a valid
-        // position is faster.
-        private static final float OVERSCROLL_DECELERATION_COEF = 16.0f;
+        // Duration in milliseconds to go back from edge to edge. Springback is half of it.
+        private static final int OVERSCROLL_SPRINGBACK_DURATION = 200;
+
+        // Oscillation period
+        private static final float TIME_COEF =
+            1000.0f * (float) Math.PI / OVERSCROLL_SPRINGBACK_DURATION;
 
         // If the velocity is smaller than this value, no bounce is triggered
         // when the edge limits are reached (would result in a zero pixels
         // displacement anyway).
-        private static final float MINIMUM_VELOCITY_FOR_BOUNCE = 200.0f;
+        private static final float MINIMUM_VELOCITY_FOR_BOUNCE = 140.0f;
 
-        // Could be made public for tuning, but applications would no longer
-        // have the same look and feel.
-        private static final float BOUNCE_COEFFICIENT = 0.4f;
+        // Proportion of the velocity that is preserved when the edge is reached.
+        private static final float DEFAULT_BOUNCE_COEFFICIENT = 0.16f;
 
-        /*
-         * Get a signed deceleration that will reduce the velocity.
-         */
-        @Override
-        float getDeceleration(int velocity) {
-            float decelerationY = super.getDeceleration(velocity);
-            if (mState != TO_EDGE) {
-                decelerationY *= OVERSCROLL_DECELERATION_COEF;
-            }
-            return decelerationY;
+        private float mBounceCoefficient = DEFAULT_BOUNCE_COEFFICIENT;
+
+        void setBounceCoefficient(float coefficient) {
+            mBounceCoefficient = coefficient;
         }
 
         boolean springback(int start, int min, int max) {
@@ -192,20 +212,21 @@
             mDuration = 0;
 
             if (start < min) {
-                startSpringback(start, min, -1);
+                startSpringback(start, min, false);
             } else if (start > max) {
-                startSpringback(start, max, 1);
+                startSpringback(start, max, true);
             }
 
             return !mFinished;
         }
 
-        private void startSpringback(int start, int end, int sign) {
+        private void startSpringback(int start, int end, boolean positive) {
             mFinished = false;
             mState = TO_BOUNCE;
-            mDeceleration = getDeceleration(sign);
-            mFinal = end;
-            mDuration = (int) (1000.0f * Math.sqrt(2.0f * (end - start) / mDeceleration));
+            mStart = mFinal = end;
+            mDuration = OVERSCROLL_SPRINGBACK_DURATION;
+            mStartTime -= OVERSCROLL_SPRINGBACK_DURATION / 2;
+            mVelocity = (int) (Math.abs(end - start) * TIME_COEF * (positive ? 1.0 : -1.0f));
         }
 
         void fling(int start, int velocity, int min, int max, int over) {
@@ -214,39 +235,60 @@
 
             super.fling(start, velocity, min, max);
 
-            if (mStart > max) {
-                if (mStart >= max + over) {
+            if (start > max) {
+                if (start >= max + over) {
                     springback(max + over, min, max);
                 } else {
-                    // Make sure the deceleration brings us back to edge
-                    mVelocity = velocity > 0 ? velocity : -velocity;
-                    mCurrVelocity = velocity;
-                    notifyEdgeReached(start, max, over);
+                    if (velocity <= 0) {
+                        springback(start, min, max);
+                    } else {
+                        long time = AnimationUtils.currentAnimationTimeMillis();
+                        final double durationSinceEdge =
+                            Math.atan((start-max) * TIME_COEF / velocity) / TIME_COEF;
+                        mStartTime = (int) (time - 1000.0f * durationSinceEdge);
+
+                        // Simulate a bounce that started from edge
+                        mStart = max;
+
+                        mVelocity = (int) (velocity / Math.cos(durationSinceEdge * TIME_COEF));
+
+                        onEdgeReached();
+                    }
                 }
             } else {
-                if (mStart < min) {
-                    if (mStart <= min - over) {
+                if (start < min) {
+                    if (start <= min - over) {
                         springback(min - over, min, max);
                     } else {
-                        // Make sure the deceleration brings us back to edge
-                        mVelocity = velocity < 0 ? velocity : -velocity;
-                        mCurrVelocity = velocity;
-                        notifyEdgeReached(start, min, over);
+                        if (velocity >= 0) {
+                            springback(start, min, max);
+                        } else {
+                            long time = AnimationUtils.currentAnimationTimeMillis();
+                            final double durationSinceEdge =
+                                Math.atan((start-min) * TIME_COEF / velocity) / TIME_COEF;
+                            mStartTime = (int) (time - 1000.0f * durationSinceEdge);
+
+                            // Simulate a bounce that started from edge
+                            mStart = min;
+
+                            mVelocity = (int) (velocity / Math.cos(durationSinceEdge * TIME_COEF));
+
+                            onEdgeReached();
+                        }
+
                     }
                 }
             }
         }
 
         void notifyEdgeReached(int start, int end, int over) {
-            // Compute post-edge deceleration
-            mState = TO_BOUNDARY;
             mDeceleration = getDeceleration(mVelocity);
 
             // Local time, used to compute edge crossing time.
             float timeCurrent = mCurrVelocity / mDeceleration;
             final int distance = end - start;
             float timeEdge = -(float) Math.sqrt((2.0f * distance / mDeceleration)
-                             + (timeCurrent * timeCurrent));
+                    + (timeCurrent * timeCurrent));
 
             mVelocity = (int) (mDeceleration * timeEdge);
 
@@ -261,22 +303,21 @@
             onEdgeReached();
         }
 
-        void onEdgeReached() {
+        private void onEdgeReached() {
             // mStart, mVelocity and mStartTime were adjusted to their values when edge was reached.
-            mState = TO_BOUNDARY;
-            mDeceleration = getDeceleration(mVelocity);
-
-            int distance = Math.round((mVelocity * mVelocity) / (2.0f * mDeceleration));
+            final float distance = mVelocity / TIME_COEF;
 
             if (Math.abs(distance) < mOver) {
-                // Deceleration will bring us back to final position
+                // Spring force will bring us back to final position
                 mState = TO_BOUNCE;
                 mFinal = mStart;
-                mDuration = (int) (-2000.0f * mVelocity / mDeceleration);
+                mDuration = OVERSCROLL_SPRINGBACK_DURATION;
             } else {
                 // Velocity is too high, we will hit the boundary limit
-                mFinal = mStart + (mVelocity > 0 ? mOver : -mOver);
-                mDuration = computeDuration(mStart, mFinal, mVelocity, mDeceleration);
+                mState = TO_BOUNDARY;
+                int over = mVelocity > 0 ? mOver : -mOver;
+                mFinal = mStart + over;
+                mDuration = (int) (1000.0f * Math.asin(over / distance) / TIME_COEF);
             }
         }
 
@@ -300,26 +341,49 @@
                     break;
                 case TO_BOUNDARY:
                     mStartTime += mDuration;
-                    mStart = mFinal;
-                    mFinal = mStart - (mVelocity > 0 ? mOver : -mOver);
-                    mVelocity = 0;
-                    mDuration = (int) (1000.0f * Math.sqrt(Math.abs(2.0f * mOver / mDeceleration)));
-                    mState = TO_BOUNCE;
+                    startSpringback(mFinal, mFinal - (mVelocity > 0 ? mOver:-mOver), mVelocity > 0);
                     break;
                 case TO_BOUNCE:
-                    float edgeVelocity = mVelocity + mDeceleration * mDuration / 1000.0f;
-                    mVelocity = (int) (-edgeVelocity * BOUNCE_COEFFICIENT);
+                    //mVelocity = (int) (mVelocity * BOUNCE_COEFFICIENT);
+                    mVelocity = (int) (mVelocity * mBounceCoefficient);
                     if (Math.abs(mVelocity) < MINIMUM_VELOCITY_FOR_BOUNCE) {
                         return false;
                     }
-                    mStart = mFinal;
                     mStartTime += mDuration;
-                    mDuration = (int) (-2000.0f * mVelocity / mDeceleration);
                     break;
             }
 
             update();
             return true;
         }
+
+        /*
+         * Update the current position and velocity for current time. Returns
+         * true if update has been done and false if animation duration has been
+         * reached.
+         */
+        @Override
+        boolean update() {
+            final long time = AnimationUtils.currentAnimationTimeMillis();
+            final long duration = time - mStartTime;
+
+            if (duration > mDuration) {
+                return false;
+            }
+
+            double distance;
+            final float t = duration / 1000.0f;
+            if (mState == TO_EDGE) {
+                mCurrVelocity = mVelocity + mDeceleration * t;
+                distance = mVelocity * t + mDeceleration * t * t / 2.0f;
+            } else {
+                final float d = t * TIME_COEF;
+                mCurrVelocity = mVelocity * (float)Math.cos(d);
+                distance = mVelocity / TIME_COEF * Math.sin(d);
+            }
+
+            mCurrentPosition = mStart + (int) distance;
+            return true;
+        }
     }
 }
diff --git a/core/java/android/widget/ScrollView.java b/core/java/android/widget/ScrollView.java
index 239c5f4..f009432 100644
--- a/core/java/android/widget/ScrollView.java
+++ b/core/java/android/widget/ScrollView.java
@@ -573,6 +573,7 @@
         } else {
             super.scrollTo(scrollX, scrollY);
         }
+        awakenScrollBars();
     }
     
     private int getOverscrollMax() {
@@ -1297,7 +1298,6 @@
                 mScrollViewMovedFocus = false;
             }
     
-            awakenScrollBars(mScroller.getDuration());
             invalidate();
         }
     }
diff --git a/core/java/android/widget/Scroller.java b/core/java/android/widget/Scroller.java
index 542866a..6a2f000 100644
--- a/core/java/android/widget/Scroller.java
+++ b/core/java/android/widget/Scroller.java
@@ -431,7 +431,7 @@
         /*
          * Get a signed deceleration that will reduce the velocity.
          */
-        float getDeceleration(int velocity) {
+        static float getDeceleration(int velocity) {
             return velocity > 0 ? -GRAVITY : GRAVITY;
         }
 
diff --git a/core/java/android/widget/SimpleAdapter.java b/core/java/android/widget/SimpleAdapter.java
index 479965a..4b17a92 100644
--- a/core/java/android/widget/SimpleAdapter.java
+++ b/core/java/android/widget/SimpleAdapter.java
@@ -172,6 +172,10 @@
                     if (v instanceof Checkable) {
                         if (data instanceof Boolean) {
                             ((Checkable) v).setChecked((Boolean) data);
+                        } else if (v instanceof TextView) {
+                            // Note: keep the instanceof TextView check at the bottom of these
+                            // ifs since a lot of views are TextViews (e.g. CheckBoxes).
+                            setViewText((TextView) v, text);
                         } else {
                             throw new IllegalStateException(v.getClass().getName() +
                                     " should be bound to a Boolean, not a " +
diff --git a/core/jni/android_view_Surface.cpp b/core/jni/android_view_Surface.cpp
index 060ca50..ed26cbe 100644
--- a/core/jni/android_view_Surface.cpp
+++ b/core/jni/android_view_Surface.cpp
@@ -35,6 +35,11 @@
 
 namespace android {
 
+enum {
+    // should match Parcelable.java
+    PARCELABLE_WRITE_RETURN_VALUE = 0x0001
+};
+
 // ----------------------------------------------------------------------------
 
 static const char* const OutOfResourcesException =
@@ -612,6 +617,9 @@
 
     const sp<SurfaceControl>& control(getSurfaceControl(env, clazz));
     SurfaceControl::writeSurfaceToParcel(control, parcel);
+    if (flags & PARCELABLE_WRITE_RETURN_VALUE) {
+        setSurfaceControl(env, clazz, 0);
+    }
 }
 
 // ----------------------------------------------------------------------------
diff --git a/core/res/assets/webkit/togglePlugin.png b/core/res/assets/webkit/togglePlugin.png
new file mode 100644
index 0000000..008333c
--- /dev/null
+++ b/core/res/assets/webkit/togglePlugin.png
Binary files differ
diff --git a/core/res/assets/webkit/togglePluginBg.png b/core/res/assets/webkit/togglePluginBg.png
new file mode 100644
index 0000000..2c65acb
--- /dev/null
+++ b/core/res/assets/webkit/togglePluginBg.png
Binary files differ
diff --git a/core/res/res/values/themes.xml b/core/res/res/values/themes.xml
index 9e19c57..6dae8b8 100644
--- a/core/res/res/values/themes.xml
+++ b/core/res/res/values/themes.xml
@@ -471,7 +471,6 @@
 
     <!-- Theme for the search input bar. -->
     <style name="Theme.SearchBar" parent="Theme.Panel">
-        <item name="android:backgroundDimEnabled">true</item>
         <item name="windowContentOverlay">@null</item>        
     </style>
     
diff --git a/tests/ConnectivityManagerTest/Android.mk b/core/tests/ConnectivityManagerTest/Android.mk
similarity index 94%
rename from tests/ConnectivityManagerTest/Android.mk
rename to core/tests/ConnectivityManagerTest/Android.mk
index bd773d0..a1546fa 100644
--- a/tests/ConnectivityManagerTest/Android.mk
+++ b/core/tests/ConnectivityManagerTest/Android.mk
@@ -1,4 +1,4 @@
-# Copyright 2010, The Android Open Source Project
+# Copyright (C) 2010, The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
diff --git a/tests/ConnectivityManagerTest/AndroidManifest.xml b/core/tests/ConnectivityManagerTest/AndroidManifest.xml
similarity index 100%
rename from tests/ConnectivityManagerTest/AndroidManifest.xml
rename to core/tests/ConnectivityManagerTest/AndroidManifest.xml
diff --git a/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java
similarity index 94%
rename from tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java
rename to core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java
index 1dffa02..badcc1b 100644
--- a/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java
+++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2010, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package com.android.connectivitymanagertest;
 
 import android.app.Activity;
diff --git a/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestRunner.java b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestRunner.java
similarity index 62%
rename from tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestRunner.java
rename to core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestRunner.java
index 3affa65..592be92 100644
--- a/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestRunner.java
+++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestRunner.java
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2010, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package com.android.connectivitymanagertest;
 
 import android.os.Bundle;
@@ -13,7 +29,7 @@
  *
  * To run the connectivity manager tests:
  *
- * adb shell am instrument \
+ * adb shell am instrument -e ssid <ssid> \
  *     -w com.android.connectivitymanagertest/.ConnectivityManagerTestRunner
  */
 
@@ -39,5 +55,5 @@
         }
     }
 
-    public String TEST_SSID = "GoogleGuest";
+    public String TEST_SSID = null;
 }
diff --git a/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/NetworkState.java b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/NetworkState.java
similarity index 91%
rename from tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/NetworkState.java
rename to core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/NetworkState.java
index 925120e..2e5a0f8 100644
--- a/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/NetworkState.java
+++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/NetworkState.java
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package com.android.connectivitymanagertest;
 
 import android.net.NetworkInfo.State;
diff --git a/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/ConnectivityManagerMobileTest.java b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/ConnectivityManagerMobileTest.java
similarity index 88%
rename from tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/ConnectivityManagerMobileTest.java
rename to core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/ConnectivityManagerMobileTest.java
index ab81bb8..ad564ae 100644
--- a/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/ConnectivityManagerMobileTest.java
+++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/ConnectivityManagerMobileTest.java
@@ -1,3 +1,21 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+
 package com.android.connectivitymanagertest.functional;
 
 import com.android.connectivitymanagertest.ConnectivityManagerTestActivity;
@@ -86,7 +104,7 @@
             Log.v(LOG_TAG, "the state for WIFI is changed");
             Log.v(LOG_TAG, "reason: " +
                     cmActivity.getTransitionFailureReason(ConnectivityManager.TYPE_WIFI));
-            assertTrue(false);
+            assertTrue("state validation fail", false);
         }
         if (!cmActivity.validateNetworkStates(ConnectivityManager.TYPE_MOBILE)) {
             Log.v(LOG_TAG, "the state for MOBILE is changed");
@@ -101,6 +119,7 @@
     // Test case 2: test connection to a given AP
     @LargeTest
     public void testConnectToWifi() {
+        assertNotNull("SSID is null", TEST_ACCESS_POINT);
         //Prepare for connectivity verification
         NetworkInfo networkInfo = cmActivity.mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
         cmActivity.setStateTransitionCriteria(ConnectivityManager.TYPE_MOBILE, networkInfo.getState(),
diff --git a/core/tests/hosttests/src/android/content/pm/PackageManagerHostTests.java b/core/tests/hosttests/src/android/content/pm/PackageManagerHostTests.java
index 9c9d777..ca0094e 100644
--- a/core/tests/hosttests/src/android/content/pm/PackageManagerHostTests.java
+++ b/core/tests/hosttests/src/android/content/pm/PackageManagerHostTests.java
@@ -16,6 +16,7 @@
 
 package android.content.pm;
 
+import com.android.ddmlib.IDevice;
 import com.android.ddmlib.IShellOutputReceiver;
 import com.android.ddmlib.Log;
 import com.android.ddmlib.MultiLineReceiver;
@@ -35,17 +36,56 @@
  */
 public class PackageManagerHostTests extends DeviceTestCase {
 
+    private static final String LOG_TAG = "PackageManagerHostTests";
+
+    // TODO: get this value from Android Environment instead of hardcoding
+    private static final String APP_PRIVATE_PATH = "/data/app-private/";
+    private static final String DEVICE_APP_PATH = "/data/app/";
+    private static final String SDCARD_APP_PATH = "/mnt/secure/asec/";
+
+    private static final int MAX_WAIT_FOR_DEVICE_TIME = 120 * 1000;
+    private static final int WAIT_FOR_DEVICE_POLL_TIME = 10 * 1000;
+
+    // Various test files and their corresponding package names...
+
     // testPushAppPrivate constants
     // these constants must match values defined in test-apps/SimpleTestApp
     private static final String SIMPLE_APK = "SimpleTestApp.apk";
     private static final String SIMPLE_PKG = "com.android.framework.simpletestapp";
-    // TODO: get this value from Android Environment instead of hardcoding
-    private static final String APP_PRIVATE_PATH = "/data/app-private/";
 
-    private static final String LOG_TAG = "PackageManagerHostTests";
-
-    private static final int MAX_WAIT_FOR_DEVICE_TIME = 120 * 1000;
-    private static final int WAIT_FOR_DEVICE_POLL_TIME = 10 * 1000;
+    // Apk with install location set to auto
+    private static final String AUTO_LOC_APK = "AutoLocTestApp.apk";
+    private static final String AUTO_LOC_PKG = "com.android.framework.autoloctestapp";
+    // Apk with install location set to internalOnly
+    private static final String INTERNAL_LOC_APK = "InternalLocTestApp.apk";
+    private static final String INTERNAL_LOC_PKG = "com.android.framework.internalloctestapp";
+    // Apk with install location set to preferExternal
+    private static final String EXTERNAL_LOC_APK = "ExternalLocTestApp.apk";
+    private static final String EXTERNAL_LOC_PKG = "com.android.framework.externalloctestapp";
+    // Apk with no install location set
+    private static final String NO_LOC_APK = "NoLocTestApp.apk";
+    private static final String NO_LOC_PKG = "com.android.framework.noloctestapp";
+    // Apk with 2 different versions - v1 is set to external, v2 has no location setting
+    private static final String UPDATE_EXTERNAL_LOC_V1_EXT_APK
+            = "UpdateExternalLocTestApp_v1_ext.apk";
+    private static final String UPDATE_EXTERNAL_LOC_V2_NONE_APK
+            = "UpdateExternalLocTestApp_v2_none.apk";
+    private static final String UPDATE_EXTERNAL_LOC_PKG
+            = "com.android.framework.updateexternalloctestapp";
+    // Apk with 2 different versions - v1 is set to external, v2 is set to internalOnly
+    private static final String UPDATE_EXT_TO_INT_LOC_V1_EXT_APK
+            = "UpdateExtToIntLocTestApp_v1_ext.apk";
+    private static final String UPDATE_EXT_TO_INT_LOC_V2_INT_APK
+            = "UpdateExtToIntLocTestApp_v2_int.apk";
+    private static final String UPDATE_EXT_TO_INT_LOC_PKG
+            = "com.android.framework.updateexttointloctestapp";
+    // Apks with the same package name, but install location set to
+    // one of: Internal, External, Auto, or None
+    private static final String VERSATILE_LOC_PKG = "com.android.framework.versatiletestapp";
+    private static final String VERSATILE_LOC_INTERNAL_APK = "VersatileTestApp_Internal.apk";
+    private static final String VERSATILE_LOC_EXTERNAL_APK = "VersatileTestApp_External.apk";
+    private static final String VERSATILE_LOC_AUTO_APK = "VersatileTestApp_Auto.apk";
+    private static final String VERSATILE_LOC_NONE_APK = "VersatileTestApp_None.apk";
 
     @Override
     protected void setUp() throws Exception {
@@ -94,6 +134,18 @@
     }
 
     /**
+     * Helper method to install a file to device
+     * @param localFilePath the absolute file system path to file on local host to install
+     * @param reinstall set to <code>true</code> if re-install of app should be performed
+     * @throws IOException
+     */
+    private void installFile(final String localFilePath, final boolean replace)
+            throws IOException {
+        String result = getDevice().installPackage(localFilePath, replace);
+        assertEquals(null, result);
+    }
+
+    /**
      * Helper method to determine if file on device exists.
      *
      * @param destPath the absolute path of file on device to check
@@ -107,6 +159,21 @@
     }
 
     /**
+     * Helper method to determine if file exists on the device containing a given string.
+     *
+     * @param destPath the
+     * @return <code>true</code> if file exists containing given string,
+     *         <code>false</code> otherwise.
+     * @throws IOException if adb shell command failed
+     */
+    private boolean doesRemoteFileExistContainingString(String destPath, String searchString)
+            throws IOException {
+        String lsResult = executeShellCommand(String.format("ls %s",
+                destPath));
+        return lsResult.contains(searchString);
+    }
+
+    /**
      * Helper method to determine if package on device exists.
      *
      * @param packageName the Android manifest package to check.
@@ -120,6 +187,28 @@
     }
 
     /**
+     * Helper method to determine if app was installed on device.
+     *
+     * @param packageName package name to check for
+     * @return <code>true</code> if file exists, <code>false</code> otherwise.
+     * @throws IOException if adb shell command failed
+     */
+    private boolean doesAppExistOnDevice(String packageName) throws IOException {
+        return doesRemoteFileExistContainingString(DEVICE_APP_PATH, packageName);
+    }
+
+    /**
+     * Helper method to determine if app was installed on SD card.
+     *
+     * @param packageName package name to check for
+     * @return <code>true</code> if file exists, <code>false</code> otherwise.
+     * @throws IOException if adb shell command failed
+     */
+    private boolean doesAppExistOnSDCard(String packageName) throws IOException {
+        return doesRemoteFileExistContainingString(SDCARD_APP_PATH, packageName);
+    }
+
+    /**
      * Waits for device's package manager to respond.
      *
      * @throws InterruptedException
@@ -213,4 +302,218 @@
             // ignore
         }
     }
+
+    /**
+     * Helper method for installing an app to wherever is specified in its manifest, and
+     * then verifying the app was installed onto SD Card.
+     * <p/>
+     * Assumes adb is running as root in device under test.
+     */
+    void installAppAndVerifyExistsOnSDCard(String apkName, String pkgName, boolean overwrite)
+            throws IOException, InterruptedException {
+        // Start with a clean slate if we're not overwriting
+        if (!overwrite) {
+            // cleanup test app just in case it already exists
+            getDevice().uninstallPackage(pkgName);
+            // grep for package to make sure its not installed
+            assertFalse(doesPackageExist(pkgName));
+        }
+
+        installFile(getTestAppFilePath(apkName), overwrite);
+        assertTrue(doesAppExistOnSDCard(pkgName));
+        assertFalse(doesAppExistOnDevice(pkgName));
+        waitForDevice();
+
+        // grep for package to make sure it is installed
+        assertTrue(doesPackageExist(pkgName));
+    }
+
+    /**
+     * Helper method for installing an app to wherever is specified in its manifest, and
+     * then verifying the app was installed onto device.
+     * <p/>
+     * Assumes adb is running as root in device under test.
+     */
+    void installAppAndVerifyExistsOnDevice(String apkName, String pkgName, boolean overwrite)
+            throws IOException, InterruptedException {
+        // Start with a clean slate if we're not overwriting
+        if (!overwrite) {
+            // cleanup test app just in case it already exists
+            getDevice().uninstallPackage(pkgName);
+            // grep for package to make sure its not installed
+            assertFalse(doesPackageExist(pkgName));
+        }
+
+        installFile(getTestAppFilePath(apkName), overwrite);
+        assertFalse(doesAppExistOnSDCard(pkgName));
+        assertTrue(doesAppExistOnDevice(pkgName));
+        waitForDevice();
+
+        // grep for package to make sure it is installed
+        assertTrue(doesPackageExist(pkgName));
+    }
+
+    /**
+     * Helper method for uninstalling an app.
+     * <p/>
+     * Assumes adb is running as root in device under test.
+     */
+    void uninstallApp(String pkgName) throws IOException, InterruptedException {
+        getDevice().uninstallPackage(pkgName);
+        // make sure its not installed anymore
+        assertFalse(doesPackageExist(pkgName));
+    }
+
+    /**
+     * Regression test to verify that an app with its manifest set to installLocation=auto
+     * will install the app to the device.
+     * <p/>
+     * Assumes adb is running as root in device under test.
+     */
+    public void testInstallAppAutoLoc() throws IOException, InterruptedException {
+        Log.i(LOG_TAG, "Test an app with installLocation=auto gets installed on device");
+
+        try {
+            installAppAndVerifyExistsOnDevice(AUTO_LOC_APK, AUTO_LOC_PKG, false);
+        }
+        // cleanup test app
+        finally {
+            uninstallApp(AUTO_LOC_PKG);
+        }
+    }
+
+    /**
+     * Regression test to verify that an app with its manifest set to installLocation=internalOnly
+     * will install the app to the device.
+     * <p/>
+     * Assumes adb is running as root in device under test.
+     */
+    public void testInstallAppInternalLoc() throws IOException, InterruptedException {
+        Log.i(LOG_TAG, "Test an app with installLocation=internalOnly gets installed on device");
+
+        try {
+            installAppAndVerifyExistsOnDevice(INTERNAL_LOC_APK, INTERNAL_LOC_PKG, false);
+        }
+        // cleanup test app
+        finally {
+            uninstallApp(INTERNAL_LOC_PKG);
+        }
+    }
+
+    /**
+     * Regression test to verify that an app with its manifest set to installLocation=preferExternal
+     * will install the app to the SD card.
+     * <p/>
+     * Assumes adb is running as root in device under test.
+     */
+    public void testInstallAppExternalLoc() throws IOException, InterruptedException {
+        Log.i(LOG_TAG, "Test an app with installLocation=preferExternal gets installed on SD Card");
+
+        try {
+            installAppAndVerifyExistsOnSDCard(EXTERNAL_LOC_APK, EXTERNAL_LOC_PKG, false);
+        }
+        // cleanup test app
+        finally {
+            uninstallApp(EXTERNAL_LOC_PKG);
+        }
+    }
+
+    /**
+     * Regression test to verify that we can install an app onto the device,
+     * uninstall it, and reinstall it onto the SD card.
+     * <p/>
+     * Assumes adb is running as root in device under test.
+     */
+    // TODO: This currently relies on the app's manifest to switch from device to
+    // SD card install locations. We might want to make Device's installPackage()
+    // accept a installLocation flag so we can install a package to the
+    // destination of our choosing.
+    public void testReinstallInternalToExternal() throws IOException, InterruptedException {
+        Log.i(LOG_TAG, "Test installing an app first to the device, then to the SD Card");
+
+        try {
+            installAppAndVerifyExistsOnDevice(VERSATILE_LOC_INTERNAL_APK, VERSATILE_LOC_PKG, false);
+            uninstallApp(VERSATILE_LOC_PKG);
+            installAppAndVerifyExistsOnSDCard(VERSATILE_LOC_EXTERNAL_APK, VERSATILE_LOC_PKG, false);
+        }
+        // cleanup test app
+        finally {
+            uninstallApp(VERSATILE_LOC_PKG);
+        }
+    }
+
+    /**
+     * Regression test to verify that we can install an app onto the SD Card,
+     * uninstall it, and reinstall it onto the device.
+     * <p/>
+     * Assumes adb is running as root in device under test.
+     */
+    // TODO: This currently relies on the app's manifest to switch from device to
+    // SD card install locations. We might want to make Device's installPackage()
+    // accept a installLocation flag so we can install a package to the
+    // destination of our choosing.
+    public void testReinstallExternalToInternal() throws IOException, InterruptedException {
+        Log.i(LOG_TAG, "Test installing an app first to the SD Care, then to the device");
+
+        try {
+            // install the app externally
+            installAppAndVerifyExistsOnSDCard(VERSATILE_LOC_EXTERNAL_APK, VERSATILE_LOC_PKG, false);
+            uninstallApp(VERSATILE_LOC_PKG);
+            // then replace the app with one marked for internalOnly
+            installAppAndVerifyExistsOnDevice(VERSATILE_LOC_INTERNAL_APK, VERSATILE_LOC_PKG, true);
+        }
+        // cleanup test app
+        finally {
+            uninstallApp(VERSATILE_LOC_PKG);
+        }
+    }
+
+    /**
+     * Regression test to verify that updating an app on the SD card will install
+     * the update onto the SD card as well when location is not explicitly set in the
+     * updated apps' manifest file.
+     * <p/>
+     * Assumes adb is running as root in device under test.
+     */
+    public void testUpdateToSDCard() throws IOException, InterruptedException {
+        Log.i(LOG_TAG, "Test updating an app on the SD card stays on the SD card");
+
+        try {
+            // install the app externally
+            installAppAndVerifyExistsOnSDCard(UPDATE_EXTERNAL_LOC_V1_EXT_APK,
+                    UPDATE_EXTERNAL_LOC_PKG, false);
+            // now replace the app with one where the location is blank (app should stay external)
+            installAppAndVerifyExistsOnSDCard(UPDATE_EXTERNAL_LOC_V2_NONE_APK,
+                    UPDATE_EXTERNAL_LOC_PKG, true);
+        }
+        // cleanup test app
+        finally {
+            uninstallApp(UPDATE_EXTERNAL_LOC_PKG);
+        }
+    }
+
+    /**
+     * Regression test to verify that updating an app on the SD card will install
+     * the update onto the SD card as well when location is not explicitly set in the
+     * updated apps' manifest file.
+     * <p/>
+     * Assumes adb is running as root in device under test.
+     */
+    public void testUpdateSDCardToDevice() throws IOException, InterruptedException {
+        Log.i(LOG_TAG, "Test updating an app on the SD card to the Device through manifest change");
+
+        try {
+            // install the app externally
+            installAppAndVerifyExistsOnSDCard(UPDATE_EXT_TO_INT_LOC_V1_EXT_APK,
+                    UPDATE_EXT_TO_INT_LOC_PKG, false);
+            // now replace the app with an update marked for internalOnly...
+            installAppAndVerifyExistsOnDevice(UPDATE_EXT_TO_INT_LOC_V2_INT_APK,
+                    UPDATE_EXT_TO_INT_LOC_PKG, true);
+        }
+        // cleanup test app
+        finally {
+            uninstallApp(UPDATE_EXT_TO_INT_LOC_PKG);
+        }
+    }
+
 }
diff --git a/tests/ConnectivityManagerTest/Android.mk b/core/tests/hosttests/test-apps/AutoLocTestApp/Android.mk
similarity index 67%
copy from tests/ConnectivityManagerTest/Android.mk
copy to core/tests/hosttests/test-apps/AutoLocTestApp/Android.mk
index bd773d0..b3cab48 100644
--- a/tests/ConnectivityManagerTest/Android.mk
+++ b/core/tests/hosttests/test-apps/AutoLocTestApp/Android.mk
@@ -1,10 +1,10 @@
-# Copyright 2010, The Android Open Source Project
+# Copyright (C) 2010 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
-#     http://www.apache.org/licenses/LICENSE-2.0
+#      http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,18 +13,15 @@
 # limitations under the License.
 
 LOCAL_PATH:= $(call my-dir)
+
 include $(CLEAR_VARS)
 
-# We only want this apk build for tests.
 LOCAL_MODULE_TAGS := tests
 
-LOCAL_JAVA_LIBRARIES := android.test.runner
-
-# Include all test java files.
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-LOCAL_PACKAGE_NAME := ConnectivityManagerTest
+LOCAL_SDK_VERSION := current
 
-#LOCAL_INSTRUMENTATION_FOR := connectivitymanagertest
+LOCAL_PACKAGE_NAME := AutoLocTestApp
 
 include $(BUILD_PACKAGE)
diff --git a/core/tests/hosttests/test-apps/AutoLocTestApp/AndroidManifest.xml b/core/tests/hosttests/test-apps/AutoLocTestApp/AndroidManifest.xml
new file mode 100644
index 0000000..d8c806a
--- /dev/null
+++ b/core/tests/hosttests/test-apps/AutoLocTestApp/AndroidManifest.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+       package="com.android.framework.autoloctestapp"
+       android:installLocation="auto">
+
+    <application android:label="AutoLocTestApp"/>
+
+</manifest>
diff --git a/core/tests/hosttests/test-apps/AutoLocTestApp/src/com/android/framework/autoloctestapp/AutoLocTestAppActivity.java b/core/tests/hosttests/test-apps/AutoLocTestApp/src/com/android/framework/autoloctestapp/AutoLocTestAppActivity.java
new file mode 100644
index 0000000..fba5691
--- /dev/null
+++ b/core/tests/hosttests/test-apps/AutoLocTestApp/src/com/android/framework/autoloctestapp/AutoLocTestAppActivity.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.framework.autoloctestapp;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+/**
+ * Empty activity, not needed for this test
+ */
+public class AutoLocTestAppActivity extends Activity {
+
+}
diff --git a/tests/ConnectivityManagerTest/Android.mk b/core/tests/hosttests/test-apps/ExternalLocPermsFLTestApp/Android.mk
similarity index 67%
copy from tests/ConnectivityManagerTest/Android.mk
copy to core/tests/hosttests/test-apps/ExternalLocPermsFLTestApp/Android.mk
index bd773d0..9a05fa6 100644
--- a/tests/ConnectivityManagerTest/Android.mk
+++ b/core/tests/hosttests/test-apps/ExternalLocPermsFLTestApp/Android.mk
@@ -1,10 +1,10 @@
-# Copyright 2010, The Android Open Source Project
+# Copyright (C) 2010 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
-#     http://www.apache.org/licenses/LICENSE-2.0
+#      http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,18 +13,15 @@
 # limitations under the License.
 
 LOCAL_PATH:= $(call my-dir)
+
 include $(CLEAR_VARS)
 
-# We only want this apk build for tests.
 LOCAL_MODULE_TAGS := tests
 
-LOCAL_JAVA_LIBRARIES := android.test.runner
-
-# Include all test java files.
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-LOCAL_PACKAGE_NAME := ConnectivityManagerTest
+LOCAL_SDK_VERSION := current
 
-#LOCAL_INSTRUMENTATION_FOR := connectivitymanagertest
+LOCAL_PACKAGE_NAME := ExternalLocPermFLTestApp
 
 include $(BUILD_PACKAGE)
diff --git a/core/tests/hosttests/test-apps/ExternalLocPermsFLTestApp/AndroidManifest.xml b/core/tests/hosttests/test-apps/ExternalLocPermsFLTestApp/AndroidManifest.xml
new file mode 100644
index 0000000..f45c627
--- /dev/null
+++ b/core/tests/hosttests/test-apps/ExternalLocPermsFLTestApp/AndroidManifest.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+       package="com.android.framework.externallocpermsfltestapp"
+       android:installLocation="preferExternal">
+
+    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
+
+    <application android:label="ExternalLocPermsFLTestApp"/>
+
+</manifest>
diff --git a/core/tests/hosttests/test-apps/ExternalLocPermsFLTestApp/src/com/android/framework/externallocpermsfltestapp/ExternalLocPermsFLTestAppActivity.java b/core/tests/hosttests/test-apps/ExternalLocPermsFLTestApp/src/com/android/framework/externallocpermsfltestapp/ExternalLocPermsFLTestAppActivity.java
new file mode 100644
index 0000000..48fd744
--- /dev/null
+++ b/core/tests/hosttests/test-apps/ExternalLocPermsFLTestApp/src/com/android/framework/externallocpermsfltestapp/ExternalLocPermsFLTestAppActivity.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.framework.externallocpermsfltestapp;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+/**
+ * Empty activity, not needed for this test
+ */
+public class ExternalLocPermsFLTestAppActivity extends Activity {
+
+}
diff --git a/tests/ConnectivityManagerTest/Android.mk b/core/tests/hosttests/test-apps/ExternalLocTestApp/Android.mk
similarity index 67%
copy from tests/ConnectivityManagerTest/Android.mk
copy to core/tests/hosttests/test-apps/ExternalLocTestApp/Android.mk
index bd773d0..5aec78a 100644
--- a/tests/ConnectivityManagerTest/Android.mk
+++ b/core/tests/hosttests/test-apps/ExternalLocTestApp/Android.mk
@@ -1,10 +1,10 @@
-# Copyright 2010, The Android Open Source Project
+# Copyright (C) 2010 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
-#     http://www.apache.org/licenses/LICENSE-2.0
+#      http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,18 +13,15 @@
 # limitations under the License.
 
 LOCAL_PATH:= $(call my-dir)
+
 include $(CLEAR_VARS)
 
-# We only want this apk build for tests.
 LOCAL_MODULE_TAGS := tests
 
-LOCAL_JAVA_LIBRARIES := android.test.runner
-
-# Include all test java files.
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-LOCAL_PACKAGE_NAME := ConnectivityManagerTest
+LOCAL_SDK_VERSION := current
 
-#LOCAL_INSTRUMENTATION_FOR := connectivitymanagertest
+LOCAL_PACKAGE_NAME := ExternalLocTestApp
 
 include $(BUILD_PACKAGE)
diff --git a/core/tests/hosttests/test-apps/ExternalLocTestApp/AndroidManifest.xml b/core/tests/hosttests/test-apps/ExternalLocTestApp/AndroidManifest.xml
new file mode 100644
index 0000000..a0c7b02
--- /dev/null
+++ b/core/tests/hosttests/test-apps/ExternalLocTestApp/AndroidManifest.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+       package="com.android.framework.externalloctestapp"
+       android:installLocation="preferExternal">
+
+    <application android:label="ExternalLocTestApp"/>
+
+</manifest>
diff --git a/core/tests/hosttests/test-apps/ExternalLocTestApp/src/com/android/framework/externalloctestapp/ExternalLocTestAppActivity.java b/core/tests/hosttests/test-apps/ExternalLocTestApp/src/com/android/framework/externalloctestapp/ExternalLocTestAppActivity.java
new file mode 100644
index 0000000..0a61628
--- /dev/null
+++ b/core/tests/hosttests/test-apps/ExternalLocTestApp/src/com/android/framework/externalloctestapp/ExternalLocTestAppActivity.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.framework.externalloctestapp;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+/**
+ * Empty activity, not needed for this test
+ */
+public class ExternalLocTestAppActivity extends Activity {
+
+}
diff --git a/tests/ConnectivityManagerTest/Android.mk b/core/tests/hosttests/test-apps/InternalLocTestApp/Android.mk
similarity index 67%
copy from tests/ConnectivityManagerTest/Android.mk
copy to core/tests/hosttests/test-apps/InternalLocTestApp/Android.mk
index bd773d0..5b58e72 100644
--- a/tests/ConnectivityManagerTest/Android.mk
+++ b/core/tests/hosttests/test-apps/InternalLocTestApp/Android.mk
@@ -1,10 +1,10 @@
-# Copyright 2010, The Android Open Source Project
+# Copyright (C) 2010 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
-#     http://www.apache.org/licenses/LICENSE-2.0
+#      http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,18 +13,15 @@
 # limitations under the License.
 
 LOCAL_PATH:= $(call my-dir)
+
 include $(CLEAR_VARS)
 
-# We only want this apk build for tests.
 LOCAL_MODULE_TAGS := tests
 
-LOCAL_JAVA_LIBRARIES := android.test.runner
-
-# Include all test java files.
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-LOCAL_PACKAGE_NAME := ConnectivityManagerTest
+LOCAL_SDK_VERSION := current
 
-#LOCAL_INSTRUMENTATION_FOR := connectivitymanagertest
+LOCAL_PACKAGE_NAME := InternalLocTestApp
 
 include $(BUILD_PACKAGE)
diff --git a/core/tests/hosttests/test-apps/InternalLocTestApp/AndroidManifest.xml b/core/tests/hosttests/test-apps/InternalLocTestApp/AndroidManifest.xml
new file mode 100644
index 0000000..fae3ae0
--- /dev/null
+++ b/core/tests/hosttests/test-apps/InternalLocTestApp/AndroidManifest.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+       package="com.android.framework.internalloctestapp"
+       android:installLocation="internalOnly">
+
+    <application android:label="InternalLocTestApp"/>
+
+</manifest>
diff --git a/core/tests/hosttests/test-apps/InternalLocTestApp/src/com/android/framework/internalloctestapp/InternalLocTestAppActivity.java b/core/tests/hosttests/test-apps/InternalLocTestApp/src/com/android/framework/internalloctestapp/InternalLocTestAppActivity.java
new file mode 100644
index 0000000..6934641
--- /dev/null
+++ b/core/tests/hosttests/test-apps/InternalLocTestApp/src/com/android/framework/internalloctestapp/InternalLocTestAppActivity.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.framework.internalloctestapp;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+/**
+ * Empty activity, not needed for this test
+ */
+public class InternalLocTestAppActivity extends Activity {
+
+}
diff --git a/tests/ConnectivityManagerTest/Android.mk b/core/tests/hosttests/test-apps/NoLocTestApp/Android.mk
similarity index 67%
copy from tests/ConnectivityManagerTest/Android.mk
copy to core/tests/hosttests/test-apps/NoLocTestApp/Android.mk
index bd773d0..11916b0 100644
--- a/tests/ConnectivityManagerTest/Android.mk
+++ b/core/tests/hosttests/test-apps/NoLocTestApp/Android.mk
@@ -1,10 +1,10 @@
-# Copyright 2010, The Android Open Source Project
+# Copyright (C) 2010 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
-#     http://www.apache.org/licenses/LICENSE-2.0
+#      http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,18 +13,15 @@
 # limitations under the License.
 
 LOCAL_PATH:= $(call my-dir)
+
 include $(CLEAR_VARS)
 
-# We only want this apk build for tests.
 LOCAL_MODULE_TAGS := tests
 
-LOCAL_JAVA_LIBRARIES := android.test.runner
-
-# Include all test java files.
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-LOCAL_PACKAGE_NAME := ConnectivityManagerTest
+LOCAL_SDK_VERSION := current
 
-#LOCAL_INSTRUMENTATION_FOR := connectivitymanagertest
+LOCAL_PACKAGE_NAME := NoLocTestApp
 
 include $(BUILD_PACKAGE)
diff --git a/core/tests/hosttests/test-apps/NoLocTestApp/AndroidManifest.xml b/core/tests/hosttests/test-apps/NoLocTestApp/AndroidManifest.xml
new file mode 100644
index 0000000..bfdad37
--- /dev/null
+++ b/core/tests/hosttests/test-apps/NoLocTestApp/AndroidManifest.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+       package="com.android.framework.noloctestapp">
+
+    <application android:label="NoLocTestApp"/>
+
+</manifest>
diff --git a/core/tests/hosttests/test-apps/NoLocTestApp/src/com/android/framework/noloctestapp/NoLocTestAppActivity.java b/core/tests/hosttests/test-apps/NoLocTestApp/src/com/android/framework/noloctestapp/NoLocTestAppActivity.java
new file mode 100644
index 0000000..72c8d2b
--- /dev/null
+++ b/core/tests/hosttests/test-apps/NoLocTestApp/src/com/android/framework/noloctestapp/NoLocTestAppActivity.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.framework.noloctestapp;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+/**
+ * Empty activity, not needed for this test
+ */
+public class NoLocTestAppActivity extends Activity {
+
+}
diff --git a/tests/ConnectivityManagerTest/Android.mk b/core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v1_ext/Android.mk
similarity index 67%
copy from tests/ConnectivityManagerTest/Android.mk
copy to core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v1_ext/Android.mk
index bd773d0..f2baefe 100644
--- a/tests/ConnectivityManagerTest/Android.mk
+++ b/core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v1_ext/Android.mk
@@ -1,10 +1,10 @@
-# Copyright 2010, The Android Open Source Project
+# Copyright (C) 2010 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
-#     http://www.apache.org/licenses/LICENSE-2.0
+#      http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,18 +13,15 @@
 # limitations under the License.
 
 LOCAL_PATH:= $(call my-dir)
+
 include $(CLEAR_VARS)
 
-# We only want this apk build for tests.
 LOCAL_MODULE_TAGS := tests
 
-LOCAL_JAVA_LIBRARIES := android.test.runner
-
-# Include all test java files.
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-LOCAL_PACKAGE_NAME := ConnectivityManagerTest
+LOCAL_SDK_VERSION := current
 
-#LOCAL_INSTRUMENTATION_FOR := connectivitymanagertest
+LOCAL_PACKAGE_NAME := UpdateExtToIntLocTestApp_v1_ext
 
 include $(BUILD_PACKAGE)
diff --git a/core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v1_ext/AndroidManifest.xml b/core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v1_ext/AndroidManifest.xml
new file mode 100644
index 0000000..b572e86
--- /dev/null
+++ b/core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v1_ext/AndroidManifest.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+       package="com.android.framework.updateexttointloctestapp"
+       android:installLocation="preferExternal"
+       android:versionCode="1"
+       android:versionName="1.0">
+
+    <application android:label="UpdateExtToIntLocTestApp"/>
+
+</manifest>
diff --git a/core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v1_ext/src/com/android/framework/updateexttointloctestapp/UpdateExtToIntLocTestAppActivity.java b/core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v1_ext/src/com/android/framework/updateexttointloctestapp/UpdateExtToIntLocTestAppActivity.java
new file mode 100644
index 0000000..5c51fb7
--- /dev/null
+++ b/core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v1_ext/src/com/android/framework/updateexttointloctestapp/UpdateExtToIntLocTestAppActivity.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.framework.updateexttointloctestapp;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+/**
+ * Empty activity, not needed for this test
+ */
+public class UpdateExtToIntLocTestAppActivity extends Activity {
+
+}
diff --git a/tests/ConnectivityManagerTest/Android.mk b/core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v2_int/Android.mk
similarity index 67%
copy from tests/ConnectivityManagerTest/Android.mk
copy to core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v2_int/Android.mk
index bd773d0..492c326 100644
--- a/tests/ConnectivityManagerTest/Android.mk
+++ b/core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v2_int/Android.mk
@@ -1,10 +1,10 @@
-# Copyright 2010, The Android Open Source Project
+# Copyright (C) 2010 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
-#     http://www.apache.org/licenses/LICENSE-2.0
+#      http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,18 +13,15 @@
 # limitations under the License.
 
 LOCAL_PATH:= $(call my-dir)
+
 include $(CLEAR_VARS)
 
-# We only want this apk build for tests.
 LOCAL_MODULE_TAGS := tests
 
-LOCAL_JAVA_LIBRARIES := android.test.runner
-
-# Include all test java files.
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-LOCAL_PACKAGE_NAME := ConnectivityManagerTest
+LOCAL_SDK_VERSION := current
 
-#LOCAL_INSTRUMENTATION_FOR := connectivitymanagertest
+LOCAL_PACKAGE_NAME := UpdateExtToIntLocTestApp_v2_int
 
 include $(BUILD_PACKAGE)
diff --git a/core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v2_int/AndroidManifest.xml b/core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v2_int/AndroidManifest.xml
new file mode 100644
index 0000000..c9437ae
--- /dev/null
+++ b/core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v2_int/AndroidManifest.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+       package="com.android.framework.updateexttointloctestapp"
+       android:installLocation="internalOnly"
+       android:versionCode="2"
+       android:versionName="2.0">
+
+    <application android:label="UpdateExtToIntLocTestApp"/>
+
+</manifest>
diff --git a/core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v2_int/src/com/android/framework/updateexttointloctestapp/UpdateExtToIntLocTestAppActivity.java b/core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v2_int/src/com/android/framework/updateexttointloctestapp/UpdateExtToIntLocTestAppActivity.java
new file mode 100644
index 0000000..5c51fb7
--- /dev/null
+++ b/core/tests/hosttests/test-apps/UpdateExtToIntLocTestApp_v2_int/src/com/android/framework/updateexttointloctestapp/UpdateExtToIntLocTestAppActivity.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.framework.updateexttointloctestapp;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+/**
+ * Empty activity, not needed for this test
+ */
+public class UpdateExtToIntLocTestAppActivity extends Activity {
+
+}
diff --git a/tests/ConnectivityManagerTest/Android.mk b/core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v1_ext/Android.mk
similarity index 67%
copy from tests/ConnectivityManagerTest/Android.mk
copy to core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v1_ext/Android.mk
index bd773d0..45867f7 100644
--- a/tests/ConnectivityManagerTest/Android.mk
+++ b/core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v1_ext/Android.mk
@@ -1,10 +1,10 @@
-# Copyright 2010, The Android Open Source Project
+# Copyright (C) 2010 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
-#     http://www.apache.org/licenses/LICENSE-2.0
+#      http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,18 +13,15 @@
 # limitations under the License.
 
 LOCAL_PATH:= $(call my-dir)
+
 include $(CLEAR_VARS)
 
-# We only want this apk build for tests.
 LOCAL_MODULE_TAGS := tests
 
-LOCAL_JAVA_LIBRARIES := android.test.runner
-
-# Include all test java files.
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-LOCAL_PACKAGE_NAME := ConnectivityManagerTest
+LOCAL_SDK_VERSION := current
 
-#LOCAL_INSTRUMENTATION_FOR := connectivitymanagertest
+LOCAL_PACKAGE_NAME := UpdateExternalLocTestApp_v1_ext
 
 include $(BUILD_PACKAGE)
diff --git a/core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v1_ext/AndroidManifest.xml b/core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v1_ext/AndroidManifest.xml
new file mode 100644
index 0000000..ec622ce
--- /dev/null
+++ b/core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v1_ext/AndroidManifest.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+       package="com.android.framework.updateexternalloctestapp"
+       android:installLocation="preferExternal"
+       android:versionCode="1"
+       android:versionName="1.0">
+
+    <application android:label="UpdateExternalLocTestApp"/>
+
+</manifest>
diff --git a/core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v1_ext/src/com/android/framework/updateexternalloctestapp/UpdateExternalLocTestAppActivity.java b/core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v1_ext/src/com/android/framework/updateexternalloctestapp/UpdateExternalLocTestAppActivity.java
new file mode 100644
index 0000000..02ecb75
--- /dev/null
+++ b/core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v1_ext/src/com/android/framework/updateexternalloctestapp/UpdateExternalLocTestAppActivity.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.framework.updateexternalloctestapp;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+/**
+ * Empty activity, not needed for this test
+ */
+public class UpdateExternalLocTestAppActivity extends Activity {
+
+}
diff --git a/tests/ConnectivityManagerTest/Android.mk b/core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v2_none/Android.mk
similarity index 67%
copy from tests/ConnectivityManagerTest/Android.mk
copy to core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v2_none/Android.mk
index bd773d0..780a9d7 100644
--- a/tests/ConnectivityManagerTest/Android.mk
+++ b/core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v2_none/Android.mk
@@ -1,10 +1,10 @@
-# Copyright 2010, The Android Open Source Project
+# Copyright (C) 2010 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
-#     http://www.apache.org/licenses/LICENSE-2.0
+#      http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,18 +13,15 @@
 # limitations under the License.
 
 LOCAL_PATH:= $(call my-dir)
+
 include $(CLEAR_VARS)
 
-# We only want this apk build for tests.
 LOCAL_MODULE_TAGS := tests
 
-LOCAL_JAVA_LIBRARIES := android.test.runner
-
-# Include all test java files.
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-LOCAL_PACKAGE_NAME := ConnectivityManagerTest
+LOCAL_SDK_VERSION := current
 
-#LOCAL_INSTRUMENTATION_FOR := connectivitymanagertest
+LOCAL_PACKAGE_NAME := UpdateExternalLocTestApp_v2_none
 
 include $(BUILD_PACKAGE)
diff --git a/core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v2_none/AndroidManifest.xml b/core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v2_none/AndroidManifest.xml
new file mode 100644
index 0000000..50fb104
--- /dev/null
+++ b/core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v2_none/AndroidManifest.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+       package="com.android.framework.updateexternalloctestapp"
+       android:versionCode="2"
+       android:versionName="2.0">
+
+    <application android:label="UpdateExternalLocTestApp"/>
+</manifest>
diff --git a/core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v2_none/src/com/android/framework/updateexternalloctestapp/UpdateExternalLocTestAppActivity.java b/core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v2_none/src/com/android/framework/updateexternalloctestapp/UpdateExternalLocTestAppActivity.java
new file mode 100644
index 0000000..02ecb75
--- /dev/null
+++ b/core/tests/hosttests/test-apps/UpdateExternalLocTestApp_v2_none/src/com/android/framework/updateexternalloctestapp/UpdateExternalLocTestAppActivity.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.framework.updateexternalloctestapp;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+/**
+ * Empty activity, not needed for this test
+ */
+public class UpdateExternalLocTestAppActivity extends Activity {
+
+}
diff --git a/tests/ConnectivityManagerTest/Android.mk b/core/tests/hosttests/test-apps/VersatileTestApp_Auto/Android.mk
similarity index 67%
copy from tests/ConnectivityManagerTest/Android.mk
copy to core/tests/hosttests/test-apps/VersatileTestApp_Auto/Android.mk
index bd773d0..fc42bc4a 100644
--- a/tests/ConnectivityManagerTest/Android.mk
+++ b/core/tests/hosttests/test-apps/VersatileTestApp_Auto/Android.mk
@@ -1,10 +1,10 @@
-# Copyright 2010, The Android Open Source Project
+# Copyright (C) 2010 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
-#     http://www.apache.org/licenses/LICENSE-2.0
+#      http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,18 +13,15 @@
 # limitations under the License.
 
 LOCAL_PATH:= $(call my-dir)
+
 include $(CLEAR_VARS)
 
-# We only want this apk build for tests.
 LOCAL_MODULE_TAGS := tests
 
-LOCAL_JAVA_LIBRARIES := android.test.runner
-
-# Include all test java files.
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-LOCAL_PACKAGE_NAME := ConnectivityManagerTest
+LOCAL_SDK_VERSION := current
 
-#LOCAL_INSTRUMENTATION_FOR := connectivitymanagertest
+LOCAL_PACKAGE_NAME := VersatileTestApp_Auto
 
 include $(BUILD_PACKAGE)
diff --git a/core/tests/hosttests/test-apps/VersatileTestApp_Auto/AndroidManifest.xml b/core/tests/hosttests/test-apps/VersatileTestApp_Auto/AndroidManifest.xml
new file mode 100644
index 0000000..f249250
--- /dev/null
+++ b/core/tests/hosttests/test-apps/VersatileTestApp_Auto/AndroidManifest.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+       package="com.android.framework.versatiletestapp"
+       android:installLocation="auto">
+
+    <application android:label="VersatileTestApp"/>
+
+</manifest>
diff --git a/core/tests/hosttests/test-apps/VersatileTestApp_Auto/src/com/android/framework/versatiletestapp/VersatileTestAppActivity.java b/core/tests/hosttests/test-apps/VersatileTestApp_Auto/src/com/android/framework/versatiletestapp/VersatileTestAppActivity.java
new file mode 100644
index 0000000..c201c7a
--- /dev/null
+++ b/core/tests/hosttests/test-apps/VersatileTestApp_Auto/src/com/android/framework/versatiletestapp/VersatileTestAppActivity.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.framework.versatiletestapp;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+/**
+ * Empty activity, not needed for this test
+ */
+public class VersatileTestAppActivity extends Activity {
+
+}
diff --git a/tests/ConnectivityManagerTest/Android.mk b/core/tests/hosttests/test-apps/VersatileTestApp_External/Android.mk
similarity index 67%
copy from tests/ConnectivityManagerTest/Android.mk
copy to core/tests/hosttests/test-apps/VersatileTestApp_External/Android.mk
index bd773d0..c72a92c 100644
--- a/tests/ConnectivityManagerTest/Android.mk
+++ b/core/tests/hosttests/test-apps/VersatileTestApp_External/Android.mk
@@ -1,10 +1,10 @@
-# Copyright 2010, The Android Open Source Project
+# Copyright (C) 2010 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
-#     http://www.apache.org/licenses/LICENSE-2.0
+#      http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,18 +13,15 @@
 # limitations under the License.
 
 LOCAL_PATH:= $(call my-dir)
+
 include $(CLEAR_VARS)
 
-# We only want this apk build for tests.
 LOCAL_MODULE_TAGS := tests
 
-LOCAL_JAVA_LIBRARIES := android.test.runner
-
-# Include all test java files.
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-LOCAL_PACKAGE_NAME := ConnectivityManagerTest
+LOCAL_SDK_VERSION := current
 
-#LOCAL_INSTRUMENTATION_FOR := connectivitymanagertest
+LOCAL_PACKAGE_NAME := VersatileTestApp_External
 
 include $(BUILD_PACKAGE)
diff --git a/core/tests/hosttests/test-apps/VersatileTestApp_External/AndroidManifest.xml b/core/tests/hosttests/test-apps/VersatileTestApp_External/AndroidManifest.xml
new file mode 100644
index 0000000..0d17ac2
--- /dev/null
+++ b/core/tests/hosttests/test-apps/VersatileTestApp_External/AndroidManifest.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+       package="com.android.framework.versatiletestapp"
+       android:installLocation="preferExternal">
+
+    <application android:label="VersatileTestApp"/>
+
+</manifest>
diff --git a/core/tests/hosttests/test-apps/VersatileTestApp_External/src/com/android/framework/versatiletestapp/VersatileTestAppActivity.java b/core/tests/hosttests/test-apps/VersatileTestApp_External/src/com/android/framework/versatiletestapp/VersatileTestAppActivity.java
new file mode 100644
index 0000000..c201c7a
--- /dev/null
+++ b/core/tests/hosttests/test-apps/VersatileTestApp_External/src/com/android/framework/versatiletestapp/VersatileTestAppActivity.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.framework.versatiletestapp;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+/**
+ * Empty activity, not needed for this test
+ */
+public class VersatileTestAppActivity extends Activity {
+
+}
diff --git a/tests/ConnectivityManagerTest/Android.mk b/core/tests/hosttests/test-apps/VersatileTestApp_Internal/Android.mk
similarity index 67%
copy from tests/ConnectivityManagerTest/Android.mk
copy to core/tests/hosttests/test-apps/VersatileTestApp_Internal/Android.mk
index bd773d0..e477825 100644
--- a/tests/ConnectivityManagerTest/Android.mk
+++ b/core/tests/hosttests/test-apps/VersatileTestApp_Internal/Android.mk
@@ -1,10 +1,10 @@
-# Copyright 2010, The Android Open Source Project
+# Copyright (C) 2010 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
-#     http://www.apache.org/licenses/LICENSE-2.0
+#      http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,18 +13,15 @@
 # limitations under the License.
 
 LOCAL_PATH:= $(call my-dir)
+
 include $(CLEAR_VARS)
 
-# We only want this apk build for tests.
 LOCAL_MODULE_TAGS := tests
 
-LOCAL_JAVA_LIBRARIES := android.test.runner
-
-# Include all test java files.
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-LOCAL_PACKAGE_NAME := ConnectivityManagerTest
+LOCAL_SDK_VERSION := current
 
-#LOCAL_INSTRUMENTATION_FOR := connectivitymanagertest
+LOCAL_PACKAGE_NAME := VersatileTestApp_Internal
 
 include $(BUILD_PACKAGE)
diff --git a/core/tests/hosttests/test-apps/VersatileTestApp_Internal/AndroidManifest.xml b/core/tests/hosttests/test-apps/VersatileTestApp_Internal/AndroidManifest.xml
new file mode 100644
index 0000000..e8cae6e
--- /dev/null
+++ b/core/tests/hosttests/test-apps/VersatileTestApp_Internal/AndroidManifest.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+       package="com.android.framework.versatiletestapp"
+       android:installLocation="internalOnly">
+
+    <application android:label="VersatileTestApp"/>
+</manifest>
diff --git a/core/tests/hosttests/test-apps/VersatileTestApp_Internal/src/com/android/framework/versatiletestapp/VersatileTestAppActivity.java b/core/tests/hosttests/test-apps/VersatileTestApp_Internal/src/com/android/framework/versatiletestapp/VersatileTestAppActivity.java
new file mode 100644
index 0000000..c201c7a
--- /dev/null
+++ b/core/tests/hosttests/test-apps/VersatileTestApp_Internal/src/com/android/framework/versatiletestapp/VersatileTestAppActivity.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.framework.versatiletestapp;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+/**
+ * Empty activity, not needed for this test
+ */
+public class VersatileTestAppActivity extends Activity {
+
+}
diff --git a/tests/ConnectivityManagerTest/Android.mk b/core/tests/hosttests/test-apps/VersatileTestApp_None/Android.mk
similarity index 67%
copy from tests/ConnectivityManagerTest/Android.mk
copy to core/tests/hosttests/test-apps/VersatileTestApp_None/Android.mk
index bd773d0..1fc516c 100644
--- a/tests/ConnectivityManagerTest/Android.mk
+++ b/core/tests/hosttests/test-apps/VersatileTestApp_None/Android.mk
@@ -1,10 +1,10 @@
-# Copyright 2010, The Android Open Source Project
+# Copyright (C) 2010 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
-#     http://www.apache.org/licenses/LICENSE-2.0
+#      http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,18 +13,15 @@
 # limitations under the License.
 
 LOCAL_PATH:= $(call my-dir)
+
 include $(CLEAR_VARS)
 
-# We only want this apk build for tests.
 LOCAL_MODULE_TAGS := tests
 
-LOCAL_JAVA_LIBRARIES := android.test.runner
-
-# Include all test java files.
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-LOCAL_PACKAGE_NAME := ConnectivityManagerTest
+LOCAL_SDK_VERSION := current
 
-#LOCAL_INSTRUMENTATION_FOR := connectivitymanagertest
+LOCAL_PACKAGE_NAME := VersatileTestApp_None
 
 include $(BUILD_PACKAGE)
diff --git a/core/tests/hosttests/test-apps/VersatileTestApp_None/AndroidManifest.xml b/core/tests/hosttests/test-apps/VersatileTestApp_None/AndroidManifest.xml
new file mode 100644
index 0000000..1df40d4
--- /dev/null
+++ b/core/tests/hosttests/test-apps/VersatileTestApp_None/AndroidManifest.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+       package="com.android.framework.versatiletestapp">
+
+    <application android:label="VersatileTestApp"/>
+</manifest>
diff --git a/core/tests/hosttests/test-apps/VersatileTestApp_None/src/com/android/framework/versatiletestapp/VersatileTestAppActivity.java b/core/tests/hosttests/test-apps/VersatileTestApp_None/src/com/android/framework/versatiletestapp/VersatileTestAppActivity.java
new file mode 100644
index 0000000..c201c7a
--- /dev/null
+++ b/core/tests/hosttests/test-apps/VersatileTestApp_None/src/com/android/framework/versatiletestapp/VersatileTestAppActivity.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.framework.versatiletestapp;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+/**
+ * Empty activity, not needed for this test
+ */
+public class VersatileTestAppActivity extends Activity {
+
+}
diff --git a/data/fonts/DroidSansHebrew.ttf b/data/fonts/DroidSansHebrew.ttf
index 8cc670d..0b48628 100644
--- a/data/fonts/DroidSansHebrew.ttf
+++ b/data/fonts/DroidSansHebrew.ttf
Binary files differ
diff --git a/media/libstagefright/AwesomePlayer.cpp b/media/libstagefright/AwesomePlayer.cpp
index 164df72..407fd57 100644
--- a/media/libstagefright/AwesomePlayer.cpp
+++ b/media/libstagefright/AwesomePlayer.cpp
@@ -1153,6 +1153,8 @@
             Mutex::Autolock autoLock(mLock);
             if (mFlags & PREPARE_CANCELLED) {
                 LOGI("prepare was cancelled before preparing the prefetcher");
+
+                prefetcher.clear();
                 abortPrepare(UNKNOWN_ERROR);
                 return;
             }
diff --git a/opengl/java/android/opengl/GLSurfaceView.java b/opengl/java/android/opengl/GLSurfaceView.java
index 3f029a6..2c43e59 100644
--- a/opengl/java/android/opengl/GLSurfaceView.java
+++ b/opengl/java/android/opengl/GLSurfaceView.java
@@ -159,6 +159,7 @@
     private final static boolean LOG_SURFACE = false;
     private final static boolean LOG_RENDERER = false;
     private final static boolean LOG_RENDERER_DRAW_FRAME = false;
+    private final static boolean LOG_EGL = false;
     // Work-around for bug 2263168
     private final static boolean DRAW_TWICE_AFTER_SIZE_CHANGED = true;
     /**
@@ -683,7 +684,14 @@
 
         public void destroyContext(EGL10 egl, EGLDisplay display,
                 EGLContext context) {
-            egl.eglDestroyContext(display, context);
+            if (!egl.eglDestroyContext(display, context)) {
+                Log.e("DefaultContextFactory", "display:" + display + " context: " + context);
+                if (LOG_THREADS) {
+                    Log.i("DefaultContextFactory", "tid=" + Thread.currentThread().getId());
+                }
+                throw new RuntimeException("eglDestroyContext failed: "
+                        + EGLLogWrapper.getErrorString(egl.eglGetError()));
+            }
         }
     }
 
@@ -880,6 +888,9 @@
          * @param configSpec
          */
         public void start() {
+            if (LOG_EGL) {
+                Log.w("EglHelper", "start() tid=" + Thread.currentThread().getId());
+            }
             /*
              * Get an EGL instance
              */
@@ -909,8 +920,12 @@
             */
             mEglContext = mEGLContextFactory.createContext(mEgl, mEglDisplay, mEglConfig);
             if (mEglContext == null || mEglContext == EGL10.EGL_NO_CONTEXT) {
+                mEglContext = null;
                 throwEglException("createContext");
             }
+            if (LOG_EGL) {
+                Log.w("EglHelper", "createContext " + mEglContext + " tid=" + Thread.currentThread().getId());
+            }
 
             mEglSurface = null;
         }
@@ -920,6 +935,21 @@
          * OpenGL interface that renders to that surface.
          */
         public GL createSurface(SurfaceHolder holder) {
+            if (LOG_EGL) {
+                Log.w("EglHelper", "createSurface()  tid=" + Thread.currentThread().getId());
+            }
+            /*
+             * Check preconditions.
+             */
+            if (mEgl == null) {
+                throw new RuntimeException("egl not initialized");
+            }
+            if (mEglDisplay == null) {
+                throw new RuntimeException("eglDisplay not initialized");
+            }
+            if (mEglConfig == null) {
+                throw new RuntimeException("mEglConfig not initialized");
+            }
             /*
              *  The window size has changed, so we need to create a new
              *  surface.
@@ -942,6 +972,8 @@
                     mEglDisplay, mEglConfig, holder);
 
             if (mEglSurface == null || mEglSurface == EGL10.EGL_NO_SURFACE) {
+                Log.w("EglHelper", "createWindowSurface failed. mEglDisplay: " + mEglDisplay +
+                        " mEglConfig: " + mEglConfig + " holder: " + holder);
                 throwEglException("createWindowSurface");
             }
 
@@ -996,6 +1028,9 @@
         }
 
         public void destroySurface() {
+            if (LOG_EGL) {
+                Log.w("EglHelper", "destroySurface()  tid=" + Thread.currentThread().getId());
+            }
             if (mEglSurface != null && mEglSurface != EGL10.EGL_NO_SURFACE) {
                 mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
                         EGL10.EGL_NO_SURFACE,
@@ -1006,6 +1041,9 @@
         }
 
         public void finish() {
+            if (LOG_EGL) {
+                Log.w("EglHelper", "finish() tid=" + Thread.currentThread().getId());
+            }
             if (mEglContext != null) {
                 mEGLContextFactory.destroyContext(mEgl, mEglDisplay, mEglContext);
                 mEglContext = null;
@@ -1021,7 +1059,11 @@
         }
 
         private void throwEglException(String function, int error) {
-            throw new RuntimeException(function + " failed: " + error);
+            String message = function + " failed: " + EGLLogWrapper.getErrorString(error);
+            if (LOG_THREADS) {
+                Log.e("EglHelper", "throwEglException tid=" + Thread.currentThread().getId() + " " + message);
+            }
+            throw new RuntimeException(message);
         }
 
         EGL10 mEgl;
@@ -1174,9 +1216,14 @@
 
                                 // If we don't have an EGL context, try to acquire one.
                                 if ((! mHaveEglContext) && sGLThreadManager.tryAcquireEglContextLocked(this)) {
+                                    try {
+                                        mEglHelper.start();
+                                    } catch (RuntimeException t) {
+                                        sGLThreadManager.releaseEglContextLocked(this);
+                                        throw t;
+                                    }
                                     mHaveEglContext = true;
                                     createEglContext = true;
-                                    mEglHelper.start();
 
                                     sGLThreadManager.notifyAll();
                                 }
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java b/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
index fb39ac0..2ad663d 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
@@ -301,18 +301,19 @@
              * enabled or disabled based on product resources.
              */
             db.beginTransaction();
+            SQLiteStatement stmt = null;
             try {
                 db.execSQL("DELETE FROM system WHERE name='"
                         + Settings.System.WINDOW_ANIMATION_SCALE + "'");
                 db.execSQL("DELETE FROM system WHERE name='"
                         + Settings.System.TRANSITION_ANIMATION_SCALE + "'");
-                SQLiteStatement stmt = db.compileStatement("INSERT INTO system(name,value)"
+                stmt = db.compileStatement("INSERT INTO system(name,value)"
                         + " VALUES(?,?);");
                 loadDefaultAnimationSettings(stmt);
-                stmt.close();
                 db.setTransactionSuccessful();
             } finally {
                 db.endTransaction();
+                if (stmt != null) stmt.close();
             }
             upgradeVersion = 32;
         }
@@ -349,14 +350,15 @@
 
         if (upgradeVersion == 34) {
             db.beginTransaction();
+            SQLiteStatement stmt = null;
             try {
-                SQLiteStatement stmt = db.compileStatement("INSERT OR IGNORE INTO secure(name,value)"
+                stmt = db.compileStatement("INSERT OR IGNORE INTO secure(name,value)"
                         + " VALUES(?,?);");
                 loadSecure35Settings(stmt);
-                stmt.close();
                 db.setTransactionSuccessful();
             } finally {
                 db.endTransaction();
+                if (stmt != null) stmt.close();
             }
             upgradeVersion = 35;
         }
@@ -391,15 +393,16 @@
 
         if (upgradeVersion == 37) {
             db.beginTransaction();
+            SQLiteStatement stmt = null;
             try {
-                SQLiteStatement stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
+                stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
                         + " VALUES(?,?);");
                 loadStringSetting(stmt, Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
                         R.string.airplane_mode_toggleable_radios);
-                stmt.close();
                 db.setTransactionSuccessful();
             } finally {
                 db.endTransaction();
+                if (stmt != null) stmt.close();
             }
             upgradeVersion = 38;
         }
@@ -440,18 +443,19 @@
              * All animations are now turned on by default!
              */
             db.beginTransaction();
+            SQLiteStatement stmt = null;
             try {
                 db.execSQL("DELETE FROM system WHERE name='"
                         + Settings.System.WINDOW_ANIMATION_SCALE + "'");
                 db.execSQL("DELETE FROM system WHERE name='"
                         + Settings.System.TRANSITION_ANIMATION_SCALE + "'");
-                SQLiteStatement stmt = db.compileStatement("INSERT INTO system(name,value)"
+                stmt = db.compileStatement("INSERT INTO system(name,value)"
                         + " VALUES(?,?);");
                 loadDefaultAnimationSettings(stmt);
-                stmt.close();
                 db.setTransactionSuccessful();
             } finally {
                 db.endTransaction();
+                if (stmt != null) stmt.close();
             }
             upgradeVersion = 41;
         }
@@ -461,16 +465,17 @@
              * Initialize newly public haptic feedback setting
              */
             db.beginTransaction();
+            SQLiteStatement stmt = null;
             try {
                 db.execSQL("DELETE FROM system WHERE name='"
                         + Settings.System.HAPTIC_FEEDBACK_ENABLED + "'");
-                SQLiteStatement stmt = db.compileStatement("INSERT INTO system(name,value)"
+                stmt = db.compileStatement("INSERT INTO system(name,value)"
                         + " VALUES(?,?);");
                 loadDefaultHapticSettings(stmt);
-                stmt.close();
                 db.setTransactionSuccessful();
             } finally {
                 db.endTransaction();
+                if (stmt != null) stmt.close();
             }
             upgradeVersion = 42;
         }
@@ -480,15 +485,16 @@
              * Initialize new notification pulse setting
              */
             db.beginTransaction();
+            SQLiteStatement stmt = null;
             try {
-                SQLiteStatement stmt = db.compileStatement("INSERT INTO system(name,value)"
+                stmt = db.compileStatement("INSERT INTO system(name,value)"
                         + " VALUES(?,?);");
                 loadBooleanSetting(stmt, Settings.System.NOTIFICATION_LIGHT_PULSE,
                         R.bool.def_notification_pulse);
-                stmt.close();
                 db.setTransactionSuccessful();
             } finally {
                 db.endTransaction();
+                if (stmt != null) stmt.close();
             }
             upgradeVersion = 43;
         }
@@ -498,15 +504,16 @@
              * This upgrade stores bluetooth volume separately from voice volume
              */
             db.beginTransaction();
+            SQLiteStatement stmt = null;
             try {
-                SQLiteStatement stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
+                stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
                         + " VALUES(?,?);");
                 loadSetting(stmt, Settings.System.VOLUME_BLUETOOTH_SCO,
                         AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_BLUETOOTH_SCO]);
-                stmt.close();
                 db.setTransactionSuccessful();
             } finally {
                 db.endTransaction();
+                if (stmt != null) stmt.close();
             }
             upgradeVersion = 44;
         }
@@ -585,14 +592,15 @@
             * New settings for new user interface noises.
             */
            db.beginTransaction();
+           SQLiteStatement stmt = null;
            try {
-                SQLiteStatement stmt = db.compileStatement("INSERT INTO system(name,value)"
+                stmt = db.compileStatement("INSERT INTO system(name,value)"
                         + " VALUES(?,?);");
                 loadUISoundEffectsSettings(stmt);
-                stmt.close();
                 db.setTransactionSuccessful();
             } finally {
                 db.endTransaction();
+                if (stmt != null) stmt.close();
             }
 
            upgradeVersion = 50;
@@ -603,15 +611,16 @@
             * New settings for set install location UI.
             */
            db.beginTransaction();
+           SQLiteStatement stmt = null;
            try {
-                SQLiteStatement stmt = db.compileStatement("INSERT INTO system(name,value)"
+                stmt = db.compileStatement("INSERT INTO system(name,value)"
                         + " VALUES(?,?);");
                 loadBooleanSetting(stmt, Settings.System.SET_INSTALL_LOCATION,
                         R.bool.set_install_location);
-                stmt.close();
                 db.setTransactionSuccessful();
             } finally {
                 db.endTransaction();
+                if (stmt != null) stmt.close();
             }
 
            upgradeVersion = 51;
@@ -637,15 +646,16 @@
         if (upgradeVersion == 52) {
             // new vibration/silent mode settings
             db.beginTransaction();
+            SQLiteStatement stmt = null;
             try {
-                SQLiteStatement stmt = db.compileStatement("INSERT INTO system(name,value)"
+                stmt = db.compileStatement("INSERT INTO system(name,value)"
                         + " VALUES(?,?);");
                 loadBooleanSetting(stmt, Settings.System.VIBRATE_IN_SILENT,
                         R.bool.def_vibrate_in_silent);
-                stmt.close();
                 db.setTransactionSuccessful();
             } finally {
                 db.endTransaction();
+                if (stmt != null) stmt.close();
             }
 
             upgradeVersion = 53;
@@ -656,15 +666,16 @@
              * New settings for set install location UI.
              */
             db.beginTransaction();
+            SQLiteStatement stmt = null;
             try {
-                 SQLiteStatement stmt = db.compileStatement("INSERT INTO system(name,value)"
+                 stmt = db.compileStatement("INSERT INTO system(name,value)"
                          + " VALUES(?,?);");
                  loadIntegerSetting(stmt, Settings.System.DEFAULT_INSTALL_LOCATION,
                          R.integer.def_install_location);
-                 stmt.close();
                  db.setTransactionSuccessful();
              } finally {
                  db.endTransaction();
+                 if (stmt != null) stmt.close();
              }
 
             upgradeVersion = 54;
@@ -844,47 +855,50 @@
      * @param db the database to insert the volume levels into
      */
     private void loadVolumeLevels(SQLiteDatabase db) {
-        SQLiteStatement stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
-                + " VALUES(?,?);");
-
-        loadSetting(stmt, Settings.System.VOLUME_MUSIC,
-                AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_MUSIC]);
-        loadSetting(stmt, Settings.System.VOLUME_RING,
-                AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_RING]);
-        loadSetting(stmt, Settings.System.VOLUME_SYSTEM,
-                AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_SYSTEM]);
-        loadSetting(
-                stmt,
-                Settings.System.VOLUME_VOICE,
-                AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_VOICE_CALL]);
-        loadSetting(stmt, Settings.System.VOLUME_ALARM,
-                AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_ALARM]);
-        loadSetting(
-                stmt,
-                Settings.System.VOLUME_NOTIFICATION,
-                AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_NOTIFICATION]);
-        loadSetting(
-                stmt,
-                Settings.System.VOLUME_BLUETOOTH_SCO,
-                AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_BLUETOOTH_SCO]);
-
-        loadSetting(stmt, Settings.System.MODE_RINGER,
-                AudioManager.RINGER_MODE_NORMAL);
-
-        loadVibrateSetting(db, false);
-
-        // By default, only the ring/notification and system streams are affected
-        loadSetting(stmt, Settings.System.MODE_RINGER_STREAMS_AFFECTED,
-                (1 << AudioManager.STREAM_RING) | (1 << AudioManager.STREAM_NOTIFICATION) |
-                (1 << AudioManager.STREAM_SYSTEM) | (1 << AudioManager.STREAM_SYSTEM_ENFORCED));
-
-        loadSetting(stmt, Settings.System.MUTE_STREAMS_AFFECTED,
-                ((1 << AudioManager.STREAM_MUSIC) |
-                 (1 << AudioManager.STREAM_RING) |
-                 (1 << AudioManager.STREAM_NOTIFICATION) |
-                 (1 << AudioManager.STREAM_SYSTEM)));
-
-        stmt.close();
+        SQLiteStatement stmt = null;
+        try {
+            stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
+                    + " VALUES(?,?);");
+    
+            loadSetting(stmt, Settings.System.VOLUME_MUSIC,
+                    AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_MUSIC]);
+            loadSetting(stmt, Settings.System.VOLUME_RING,
+                    AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_RING]);
+            loadSetting(stmt, Settings.System.VOLUME_SYSTEM,
+                    AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_SYSTEM]);
+            loadSetting(
+                    stmt,
+                    Settings.System.VOLUME_VOICE,
+                    AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_VOICE_CALL]);
+            loadSetting(stmt, Settings.System.VOLUME_ALARM,
+                    AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_ALARM]);
+            loadSetting(
+                    stmt,
+                    Settings.System.VOLUME_NOTIFICATION,
+                    AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_NOTIFICATION]);
+            loadSetting(
+                    stmt,
+                    Settings.System.VOLUME_BLUETOOTH_SCO,
+                    AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_BLUETOOTH_SCO]);
+    
+            loadSetting(stmt, Settings.System.MODE_RINGER,
+                    AudioManager.RINGER_MODE_NORMAL);
+    
+            loadVibrateSetting(db, false);
+    
+            // By default, only the ring/notification and system streams are affected
+            loadSetting(stmt, Settings.System.MODE_RINGER_STREAMS_AFFECTED,
+                    (1 << AudioManager.STREAM_RING) | (1 << AudioManager.STREAM_NOTIFICATION) |
+                    (1 << AudioManager.STREAM_SYSTEM) | (1 << AudioManager.STREAM_SYSTEM_ENFORCED));
+    
+            loadSetting(stmt, Settings.System.MUTE_STREAMS_AFFECTED,
+                    ((1 << AudioManager.STREAM_MUSIC) |
+                     (1 << AudioManager.STREAM_RING) |
+                     (1 << AudioManager.STREAM_NOTIFICATION) |
+                     (1 << AudioManager.STREAM_SYSTEM)));
+        } finally {
+            if (stmt != null) stmt.close();
+        }
     }
 
     private void loadVibrateSetting(SQLiteDatabase db, boolean deleteOld) {
@@ -892,17 +906,21 @@
             db.execSQL("DELETE FROM system WHERE name='" + Settings.System.VIBRATE_ON + "'");
         }
 
-        SQLiteStatement stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
-                + " VALUES(?,?);");
-
-        // Vibrate off by default for ringer, on for notification
-        int vibrate = 0;
-        vibrate = AudioService.getValueForVibrateSetting(vibrate,
-                AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON);
-        vibrate = AudioService.getValueForVibrateSetting(vibrate,
-                AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
-        loadSetting(stmt, Settings.System.VIBRATE_ON, vibrate);
-        stmt.close();
+        SQLiteStatement stmt = null;
+        try {
+            stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
+                    + " VALUES(?,?);");
+    
+            // Vibrate off by default for ringer, on for notification
+            int vibrate = 0;
+            vibrate = AudioService.getValueForVibrateSetting(vibrate,
+                    AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON);
+            vibrate = AudioService.getValueForVibrateSetting(vibrate,
+                    AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
+            loadSetting(stmt, Settings.System.VIBRATE_ON, vibrate);
+        } finally {
+            if (stmt != null) stmt.close();
+        }
     }
 
     private void loadSettings(SQLiteDatabase db) {
@@ -911,69 +929,72 @@
     }
 
     private void loadSystemSettings(SQLiteDatabase db) {
-        SQLiteStatement stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
-                + " VALUES(?,?);");
-
-        loadBooleanSetting(stmt, Settings.System.DIM_SCREEN,
-                R.bool.def_dim_screen);
-        loadSetting(stmt, Settings.System.STAY_ON_WHILE_PLUGGED_IN,
-                "1".equals(SystemProperties.get("ro.kernel.qemu")) ? 1 : 0);
-        loadIntegerSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,
-                R.integer.def_screen_off_timeout);
-
-        // Set default cdma emergency tone
-        loadSetting(stmt, Settings.System.EMERGENCY_TONE, 0);
-
-        // Set default cdma call auto retry
-        loadSetting(stmt, Settings.System.CALL_AUTO_RETRY, 0);
-
-        // Set default cdma DTMF type
-        loadSetting(stmt, Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, 0);
-
-        // Set default hearing aid
-        loadSetting(stmt, Settings.System.HEARING_AID, 0);
-
-        // Set default tty mode
-        loadSetting(stmt, Settings.System.TTY_MODE, 0);
-
-        loadBooleanSetting(stmt, Settings.System.AIRPLANE_MODE_ON,
-                R.bool.def_airplane_mode_on);
-
-        loadStringSetting(stmt, Settings.System.AIRPLANE_MODE_RADIOS,
-                R.string.def_airplane_mode_radios);
-
-        loadStringSetting(stmt, Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
-                R.string.airplane_mode_toggleable_radios);
-
-        loadBooleanSetting(stmt, Settings.System.AUTO_TIME,
-                R.bool.def_auto_time); // Sync time to NITZ
-
-        loadIntegerSetting(stmt, Settings.System.SCREEN_BRIGHTNESS,
-                R.integer.def_screen_brightness);
-
-        loadBooleanSetting(stmt, Settings.System.SCREEN_BRIGHTNESS_MODE,
-                R.bool.def_screen_brightness_automatic_mode);
-
-        loadDefaultAnimationSettings(stmt);
-
-        loadBooleanSetting(stmt, Settings.System.ACCELEROMETER_ROTATION,
-                R.bool.def_accelerometer_rotation);
-
-        loadDefaultHapticSettings(stmt);
-
-        loadBooleanSetting(stmt, Settings.System.NOTIFICATION_LIGHT_PULSE,
-                R.bool.def_notification_pulse);
-        loadBooleanSetting(stmt, Settings.System.SET_INSTALL_LOCATION,
-                R.bool.set_install_location);
-        loadIntegerSetting(stmt, Settings.System.DEFAULT_INSTALL_LOCATION,
-                R.integer.def_install_location);
-
-        loadUISoundEffectsSettings(stmt);
-
-        loadBooleanSetting(stmt, Settings.System.VIBRATE_IN_SILENT,
-                R.bool.def_vibrate_in_silent);
-
-        stmt.close();
+        SQLiteStatement stmt = null;
+        try {
+            stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
+                    + " VALUES(?,?);");
+    
+            loadBooleanSetting(stmt, Settings.System.DIM_SCREEN,
+                    R.bool.def_dim_screen);
+            loadSetting(stmt, Settings.System.STAY_ON_WHILE_PLUGGED_IN,
+                    "1".equals(SystemProperties.get("ro.kernel.qemu")) ? 1 : 0);
+            loadIntegerSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,
+                    R.integer.def_screen_off_timeout);
+    
+            // Set default cdma emergency tone
+            loadSetting(stmt, Settings.System.EMERGENCY_TONE, 0);
+    
+            // Set default cdma call auto retry
+            loadSetting(stmt, Settings.System.CALL_AUTO_RETRY, 0);
+    
+            // Set default cdma DTMF type
+            loadSetting(stmt, Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, 0);
+    
+            // Set default hearing aid
+            loadSetting(stmt, Settings.System.HEARING_AID, 0);
+    
+            // Set default tty mode
+            loadSetting(stmt, Settings.System.TTY_MODE, 0);
+    
+            loadBooleanSetting(stmt, Settings.System.AIRPLANE_MODE_ON,
+                    R.bool.def_airplane_mode_on);
+    
+            loadStringSetting(stmt, Settings.System.AIRPLANE_MODE_RADIOS,
+                    R.string.def_airplane_mode_radios);
+    
+            loadStringSetting(stmt, Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
+                    R.string.airplane_mode_toggleable_radios);
+    
+            loadBooleanSetting(stmt, Settings.System.AUTO_TIME,
+                    R.bool.def_auto_time); // Sync time to NITZ
+    
+            loadIntegerSetting(stmt, Settings.System.SCREEN_BRIGHTNESS,
+                    R.integer.def_screen_brightness);
+    
+            loadBooleanSetting(stmt, Settings.System.SCREEN_BRIGHTNESS_MODE,
+                    R.bool.def_screen_brightness_automatic_mode);
+    
+            loadDefaultAnimationSettings(stmt);
+    
+            loadBooleanSetting(stmt, Settings.System.ACCELEROMETER_ROTATION,
+                    R.bool.def_accelerometer_rotation);
+    
+            loadDefaultHapticSettings(stmt);
+    
+            loadBooleanSetting(stmt, Settings.System.NOTIFICATION_LIGHT_PULSE,
+                    R.bool.def_notification_pulse);
+            loadBooleanSetting(stmt, Settings.System.SET_INSTALL_LOCATION,
+                    R.bool.set_install_location);
+            loadIntegerSetting(stmt, Settings.System.DEFAULT_INSTALL_LOCATION,
+                    R.integer.def_install_location);
+    
+            loadUISoundEffectsSettings(stmt);
+    
+            loadBooleanSetting(stmt, Settings.System.VIBRATE_IN_SILENT,
+                    R.bool.def_vibrate_in_silent);
+        } finally {
+            if (stmt != null) stmt.close();
+        }
     }
 
     private void loadUISoundEffectsSettings(SQLiteStatement stmt) {
@@ -1014,79 +1035,82 @@
     }
 
     private void loadSecureSettings(SQLiteDatabase db) {
-        SQLiteStatement stmt = db.compileStatement("INSERT OR IGNORE INTO secure(name,value)"
-                + " VALUES(?,?);");
-
-        loadBooleanSetting(stmt, Settings.Secure.BLUETOOTH_ON,
-                R.bool.def_bluetooth_on);
-
-        // Data roaming default, based on build
-        loadSetting(stmt, Settings.Secure.DATA_ROAMING,
-                "true".equalsIgnoreCase(
-                        SystemProperties.get("ro.com.android.dataroaming",
-                                "false")) ? 1 : 0);
-
-        loadBooleanSetting(stmt, Settings.Secure.INSTALL_NON_MARKET_APPS,
-                R.bool.def_install_non_market_apps);
-
-        loadStringSetting(stmt, Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
-                R.string.def_location_providers_allowed);
-
-        loadBooleanSetting(stmt, Settings.Secure.ASSISTED_GPS_ENABLED,
-                R.bool.assisted_gps_enabled);
-
-        loadIntegerSetting(stmt, Settings.Secure.NETWORK_PREFERENCE,
-                R.integer.def_network_preference);
-
-        loadBooleanSetting(stmt, Settings.Secure.USB_MASS_STORAGE_ENABLED,
-                R.bool.def_usb_mass_storage_enabled);
-
-        loadBooleanSetting(stmt, Settings.Secure.WIFI_ON,
-                R.bool.def_wifi_on);
-        loadBooleanSetting(stmt, Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
-                R.bool.def_networks_available_notification_on);
-
-        String wifiWatchList = SystemProperties.get("ro.com.android.wifi-watchlist");
-        if (!TextUtils.isEmpty(wifiWatchList)) {
-            loadSetting(stmt, Settings.Secure.WIFI_WATCHDOG_WATCH_LIST, wifiWatchList);
+        SQLiteStatement stmt = null;
+        try {
+            stmt = db.compileStatement("INSERT OR IGNORE INTO secure(name,value)"
+                    + " VALUES(?,?);");
+    
+            loadBooleanSetting(stmt, Settings.Secure.BLUETOOTH_ON,
+                    R.bool.def_bluetooth_on);
+    
+            // Data roaming default, based on build
+            loadSetting(stmt, Settings.Secure.DATA_ROAMING,
+                    "true".equalsIgnoreCase(
+                            SystemProperties.get("ro.com.android.dataroaming",
+                                    "false")) ? 1 : 0);
+    
+            loadBooleanSetting(stmt, Settings.Secure.INSTALL_NON_MARKET_APPS,
+                    R.bool.def_install_non_market_apps);
+    
+            loadStringSetting(stmt, Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
+                    R.string.def_location_providers_allowed);
+    
+            loadBooleanSetting(stmt, Settings.Secure.ASSISTED_GPS_ENABLED,
+                    R.bool.assisted_gps_enabled);
+    
+            loadIntegerSetting(stmt, Settings.Secure.NETWORK_PREFERENCE,
+                    R.integer.def_network_preference);
+    
+            loadBooleanSetting(stmt, Settings.Secure.USB_MASS_STORAGE_ENABLED,
+                    R.bool.def_usb_mass_storage_enabled);
+    
+            loadBooleanSetting(stmt, Settings.Secure.WIFI_ON,
+                    R.bool.def_wifi_on);
+            loadBooleanSetting(stmt, Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
+                    R.bool.def_networks_available_notification_on);
+    
+            String wifiWatchList = SystemProperties.get("ro.com.android.wifi-watchlist");
+            if (!TextUtils.isEmpty(wifiWatchList)) {
+                loadSetting(stmt, Settings.Secure.WIFI_WATCHDOG_WATCH_LIST, wifiWatchList);
+            }
+    
+            // Set the preferred network mode to 0 = Global, CDMA default
+            int type = SystemProperties.getInt("ro.telephony.default_network",
+                    RILConstants.PREFERRED_NETWORK_MODE);
+            loadSetting(stmt, Settings.Secure.PREFERRED_NETWORK_MODE, type);
+    
+            // Enable or disable Cell Broadcast SMS
+            loadSetting(stmt, Settings.Secure.CDMA_CELL_BROADCAST_SMS,
+                    RILConstants.CDMA_CELL_BROADCAST_SMS_DISABLED);
+    
+            // Set the preferred cdma subscription to 0 = Subscription from RUIM, when available
+            loadSetting(stmt, Settings.Secure.PREFERRED_CDMA_SUBSCRIPTION,
+                    RILConstants.PREFERRED_CDMA_SUBSCRIPTION);
+    
+            // Don't do this.  The SystemServer will initialize ADB_ENABLED from a
+            // persistent system property instead.
+            //loadSetting(stmt, Settings.Secure.ADB_ENABLED, 0);
+    
+            // Allow mock locations default, based on build
+            loadSetting(stmt, Settings.Secure.ALLOW_MOCK_LOCATION,
+                    "1".equals(SystemProperties.get("ro.allow.mock.location")) ? 1 : 0);
+    
+            loadSecure35Settings(stmt);
+    
+            loadBooleanSetting(stmt, Settings.Secure.MOUNT_PLAY_NOTIFICATION_SND,
+                    R.bool.def_mount_play_notification_snd);
+    
+            loadBooleanSetting(stmt, Settings.Secure.MOUNT_UMS_AUTOSTART,
+                    R.bool.def_mount_ums_autostart);
+    
+            loadBooleanSetting(stmt, Settings.Secure.MOUNT_UMS_PROMPT,
+                    R.bool.def_mount_ums_prompt);
+    
+            loadBooleanSetting(stmt, Settings.Secure.MOUNT_UMS_NOTIFY_ENABLED,
+                    R.bool.def_mount_ums_notify_enabled);
+        } finally {
+            if (stmt != null) stmt.close();
         }
-
-        // Set the preferred network mode to 0 = Global, CDMA default
-        int type = SystemProperties.getInt("ro.telephony.default_network",
-                RILConstants.PREFERRED_NETWORK_MODE);
-        loadSetting(stmt, Settings.Secure.PREFERRED_NETWORK_MODE, type);
-
-        // Enable or disable Cell Broadcast SMS
-        loadSetting(stmt, Settings.Secure.CDMA_CELL_BROADCAST_SMS,
-                RILConstants.CDMA_CELL_BROADCAST_SMS_DISABLED);
-
-        // Set the preferred cdma subscription to 0 = Subscription from RUIM, when available
-        loadSetting(stmt, Settings.Secure.PREFERRED_CDMA_SUBSCRIPTION,
-                RILConstants.PREFERRED_CDMA_SUBSCRIPTION);
-
-        // Don't do this.  The SystemServer will initialize ADB_ENABLED from a
-        // persistent system property instead.
-        //loadSetting(stmt, Settings.Secure.ADB_ENABLED, 0);
-
-        // Allow mock locations default, based on build
-        loadSetting(stmt, Settings.Secure.ALLOW_MOCK_LOCATION,
-                "1".equals(SystemProperties.get("ro.allow.mock.location")) ? 1 : 0);
-
-        loadSecure35Settings(stmt);
-
-        loadBooleanSetting(stmt, Settings.Secure.MOUNT_PLAY_NOTIFICATION_SND,
-                R.bool.def_mount_play_notification_snd);
-
-        loadBooleanSetting(stmt, Settings.Secure.MOUNT_UMS_AUTOSTART,
-                R.bool.def_mount_ums_autostart);
-
-        loadBooleanSetting(stmt, Settings.Secure.MOUNT_UMS_PROMPT,
-                R.bool.def_mount_ums_prompt);
-
-        loadBooleanSetting(stmt, Settings.Secure.MOUNT_UMS_NOTIFY_ENABLED,
-                R.bool.def_mount_ums_notify_enabled);
-
-        stmt.close();
     }
 
     private void loadSecure35Settings(SQLiteStatement stmt) {
diff --git a/services/java/com/android/server/MountService.java b/services/java/com/android/server/MountService.java
index a4703de..10920fa 100644
--- a/services/java/com/android/server/MountService.java
+++ b/services/java/com/android/server/MountService.java
@@ -100,6 +100,7 @@
         public static final int OpFailedMediaCorrupt           = 403;
         public static final int OpFailedVolNotMounted          = 404;
         public static final int OpFailedStorageBusy            = 405;
+        public static final int OpFailedStorageNotFound        = 406;
 
         /*
          * 600 series - Unsolicited broadcasts.
@@ -1290,21 +1291,22 @@
         waitForReady();
         warnOnNotMounted();
 
-        ArrayList<String> rsp = mConnector.doCommand("asec path " + id);
-
-        for (String line : rsp) {
-            String []tok = line.split(" ");
+        try {
+            ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
+            String []tok = rsp.get(0).split(" ");
             int code = Integer.parseInt(tok[0]);
-            if (code == VoldResponseCode.AsecPathResult) {
-                return tok[1];
+            if (code != VoldResponseCode.AsecPathResult) {
+                throw new IllegalStateException(String.format("Unexpected response code %d", code));
+            }
+            return tok[1];
+        } catch (NativeDaemonConnectorException e) {
+            int code = e.getCode();
+            if (code == VoldResponseCode.OpFailedStorageNotFound) {
+                throw new IllegalArgumentException(String.format("Container '%s' not found", id));
             } else {
-                Log.e(TAG, String.format("Unexpected response code %d", code));
-                return "";
+                throw new IllegalStateException(String.format("Unexpected response code %d", code));
             }
         }
-
-        Log.e(TAG, "Got an empty response");
-        return "";
     }
 
     public void finishMediaUpdate() {
diff --git a/services/java/com/android/server/NativeDaemonConnector.java b/services/java/com/android/server/NativeDaemonConnector.java
index 39c847a..08d7ce6 100644
--- a/services/java/com/android/server/NativeDaemonConnector.java
+++ b/services/java/com/android/server/NativeDaemonConnector.java
@@ -48,6 +48,8 @@
     private String                mSocket;
     private INativeDaemonConnectorCallbacks mCallbacks;
 
+    private final int BUFFER_SIZE = 4096;
+
     class ResponseCode {
         public static final int ActionInitiated                = 100;
 
@@ -87,7 +89,7 @@
     }
 
     private void listenToSocket() throws IOException {
-       LocalSocket socket = null;
+        LocalSocket socket = null;
 
         try {
             socket = new LocalSocket();
@@ -100,13 +102,13 @@
             InputStream inputStream = socket.getInputStream();
             mOutputStream = socket.getOutputStream();
 
-            byte[] buffer = new byte[4096];
+            byte[] buffer = new byte[BUFFER_SIZE];
+            int start = 0;
 
             while (true) {
-                int count = inputStream.read(buffer);
+                int count = inputStream.read(buffer, start, BUFFER_SIZE - start);
                 if (count < 0) break;
 
-                int start = 0;
                 for (int i = 0; i < count; i++) {
                     if (buffer[i] == 0) {
                         String event = new String(buffer, start, i - start);
@@ -139,6 +141,13 @@
                         start = i + 1;
                     }
                 }
+                if (start != count) {
+                    final int remaining = BUFFER_SIZE - start;
+                    System.arraycopy(buffer, start, buffer, 0, remaining);
+                    start = remaining;
+                } else {
+                    start = 0;
+                }
             }
         } catch (IOException ex) {
             Slog.e(TAG, "Communications error", ex);
diff --git a/services/java/com/android/server/PackageManagerService.java b/services/java/com/android/server/PackageManagerService.java
index f9e1963..53415c7 100644
--- a/services/java/com/android/server/PackageManagerService.java
+++ b/services/java/com/android/server/PackageManagerService.java
@@ -4001,6 +4001,8 @@
                             changedPermission = true;
                             gp.grantedPermissions.add(perm);
                             gp.gids = appendInts(gp.gids, bp.gids);
+                        } else if (!ps.haveGids) {
+                            gp.gids = appendInts(gp.gids, bp.gids);
                         }
                     } else {
                         Slog.w(TAG, "Not granting permission " + perm
@@ -4038,6 +4040,7 @@
             // changed.
             ps.permissionsFixed = true;
         }
+        ps.haveGids = true;
     }
     
     private final class ActivityIntentResolver
@@ -6926,7 +6929,8 @@
                     pw.print("    timeStamp="); pw.println(ps.getTimeStampStr());
                     pw.print("    signatures="); pw.println(ps.signatures);
                     pw.print("    permissionsFixed="); pw.print(ps.permissionsFixed);
-                            pw.print(" pkgFlags=0x"); pw.print(Integer.toHexString(ps.pkgFlags));
+                            pw.print(" haveGids="); pw.println(ps.haveGids);
+                    pw.print("    pkgFlags=0x"); pw.print(Integer.toHexString(ps.pkgFlags));
                             pw.print(" installStatus="); pw.print(ps.installStatus);
                             pw.print(" enabled="); pw.println(ps.enabled);
                     if (ps.disabledComponents.size() > 0) {
@@ -7548,6 +7552,7 @@
         PackageSignatures signatures = new PackageSignatures();
 
         boolean permissionsFixed;
+        boolean haveGids;
 
         /* Explicitly disabled components */
         HashSet<String> disabledComponents = new HashSet<String>(0);
@@ -7621,6 +7626,7 @@
             timeStampString = base.timeStampString;
             signatures = base.signatures;
             permissionsFixed = base.permissionsFixed;
+            haveGids = base.haveGids;
             disabledComponents = base.disabledComponents;
             enabledComponents = base.enabledComponents;
             enabled = base.enabled;
@@ -9572,11 +9578,10 @@
        if (doGc) {
            Runtime.getRuntime().gc();
        }
-       // Delete any stale containers if needed.
+       // List stale containers.
        if (removeCids != null) {
            for (String cid : removeCids) {
-               Log.i(TAG, "Destroying stale container : " + cid);
-               PackageHelper.destroySdDir(cid);
+               Log.w(TAG, "Container " + cid + " is stale");
            }
        }
    }
diff --git a/services/java/com/android/server/UiModeManagerService.java b/services/java/com/android/server/UiModeManagerService.java
index 5bd23f4..4e506e7 100644
--- a/services/java/com/android/server/UiModeManagerService.java
+++ b/services/java/com/android/server/UiModeManagerService.java
@@ -45,6 +45,7 @@
 import android.os.PowerManager;
 import android.os.RemoteException;
 import android.os.ServiceManager;
+import android.provider.Settings;
 import android.text.format.DateUtils;
 import android.text.format.Time;
 import android.util.Slog;
@@ -127,20 +128,39 @@
                     category = null;
                 }
                 if (category != null) {
+                    // This is the new activity that will serve as home while
+                    // we are in care mode.
                     intent = new Intent(Intent.ACTION_MAIN);
                     intent.addCategory(category);
                     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                             | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
+                    
+                    // Now we are going to be careful about switching the
+                    // configuration and starting the activity -- we need to
+                    // do this in a specific order under control of the
+                    // activity manager, to do it cleanly.  So compute the
+                    // new config, but don't set it yet, and let the
+                    // activity manager take care of both the start and config
+                    // change.
+                    Configuration newConfig = null;
+                    if (mHoldingConfiguration) {
+                        updateConfigurationLocked(false);
+                        newConfig = mConfiguration;
+                    }
                     try {
+                        ActivityManagerNative.getDefault().startActivityWithConfig(
+                                null, intent, null, null, 0, null, null, 0, false, false,
+                                newConfig);
                         mContext.startActivity(intent);
-                    } catch (ActivityNotFoundException e) {
+                        mHoldingConfiguration = false;
+                    } catch (RemoteException e) {
                         Slog.w(TAG, e.getCause());
                     }
                 }
 
                 if (mHoldingConfiguration) {
                     mHoldingConfiguration = false;
-                    updateConfigurationLocked();
+                    updateConfigurationLocked(true);
                 }
             }
         }
@@ -275,6 +295,9 @@
                 com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
         mDeskModeKeepsScreenOn = (context.getResources().getInteger(
                 com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
+        
+        mNightMode = Settings.Secure.getInt(mContext.getContentResolver(),
+                Settings.Secure.UI_NIGHT_MODE, UiModeManager.MODE_NIGHT_AUTO);
     }
 
     public void disableCarMode() {
@@ -319,6 +342,10 @@
             }
 
             if (mNightMode != mode) {
+                long ident = Binder.clearCallingIdentity();
+                Settings.Secure.putInt(mContext.getContentResolver(),
+                        Settings.Secure.UI_NIGHT_MODE, mode);
+                Binder.restoreCallingIdentity(ident);
                 mNightMode = mode;
                 updateLocked();
             }
@@ -360,7 +387,7 @@
         }
     }
 
-    final void updateConfigurationLocked() {
+    final void updateConfigurationLocked(boolean sendIt) {
         int uiMode = 0;
         if (mCarModeEnabled) {
             uiMode = Configuration.UI_MODE_TYPE_CAR;
@@ -385,13 +412,14 @@
 
         if (!mHoldingConfiguration && uiMode != mSetUiMode) {
             mSetUiMode = uiMode;
+            mConfiguration.uiMode = uiMode;
 
-            try {
-                final IActivityManager am = ActivityManagerNative.getDefault();
-                mConfiguration.uiMode = uiMode;
-                am.updateConfiguration(mConfiguration);
-            } catch (RemoteException e) {
-                Slog.w(TAG, "Failure communicating with activity manager", e);
+            if (sendIt) {
+                try {
+                    ActivityManagerNative.getDefault().updateConfiguration(mConfiguration);
+                } catch (RemoteException e) {
+                    Slog.w(TAG, "Failure communicating with activity manager", e);
+                }
             }
         }
     }
@@ -447,7 +475,7 @@
                 mHoldingConfiguration = true;
             }
 
-            updateConfigurationLocked();
+            updateConfigurationLocked(true);
 
             // keep screen on when charging and in car mode
             boolean keepScreenOn = mCharging &&
diff --git a/services/java/com/android/server/am/ActivityManagerService.java b/services/java/com/android/server/am/ActivityManagerService.java
index 7034c88..f734053 100644
--- a/services/java/com/android/server/am/ActivityManagerService.java
+++ b/services/java/com/android/server/am/ActivityManagerService.java
@@ -3612,7 +3612,7 @@
             Intent intent, String resolvedType, Uri[] grantedUriPermissions,
             int grantedMode, IBinder resultTo,
             String resultWho, int requestCode, boolean onlyIfNeeded,
-            boolean debug, WaitResult outResult) {
+            boolean debug, WaitResult outResult, Configuration config) {
         // Refuse possible leaked file descriptors
         if (intent != null && intent.hasFileDescriptors()) {
             throw new IllegalArgumentException("File descriptors passed in Intent");
@@ -3666,6 +3666,15 @@
                     grantedUriPermissions, grantedMode, aInfo,
                     resultTo, resultWho, requestCode, callingPid, callingUid,
                     onlyIfNeeded, componentSpecified);
+            if (config != null) {
+                // If the caller also wants to switch to a new configuration,
+                // do so now.  This allows a clean switch, as we are waiting
+                // for the current activity to pause (so we will not destroy
+                // it), and have not yet started the next activity.
+                enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
+                        "updateConfiguration()");
+                updateConfigurationLocked(config, null);
+            }
             Binder.restoreCallingIdentity(origId);
             
             if (outResult != null) {
@@ -3707,8 +3716,9 @@
             int grantedMode, IBinder resultTo,
             String resultWho, int requestCode, boolean onlyIfNeeded,
             boolean debug) {
-        return startActivityMayWait(caller, intent, resolvedType, grantedUriPermissions,
-                grantedMode, resultTo, resultWho, requestCode, onlyIfNeeded, debug, null);
+        return startActivityMayWait(caller, intent, resolvedType,
+                grantedUriPermissions, grantedMode, resultTo, resultWho,
+                requestCode, onlyIfNeeded, debug, null, null);
     }
 
     public final WaitResult startActivityAndWait(IApplicationThread caller,
@@ -3717,11 +3727,22 @@
             String resultWho, int requestCode, boolean onlyIfNeeded,
             boolean debug) {
         WaitResult res = new WaitResult();
-        startActivityMayWait(caller, intent, resolvedType, grantedUriPermissions,
-                grantedMode, resultTo, resultWho, requestCode, onlyIfNeeded, debug, res);
+        startActivityMayWait(caller, intent, resolvedType,
+                grantedUriPermissions, grantedMode, resultTo, resultWho,
+                requestCode, onlyIfNeeded, debug, res, null);
         return res;
     }
     
+    public final int startActivityWithConfig(IApplicationThread caller,
+            Intent intent, String resolvedType, Uri[] grantedUriPermissions,
+            int grantedMode, IBinder resultTo,
+            String resultWho, int requestCode, boolean onlyIfNeeded,
+            boolean debug, Configuration config) {
+        return startActivityMayWait(caller, intent, resolvedType,
+                grantedUriPermissions, grantedMode, resultTo, resultWho,
+                requestCode, onlyIfNeeded, debug, null, config);
+    }
+
      public int startActivityIntentSender(IApplicationThread caller,
             IntentSender intent, Intent fillInIntent, String resolvedType,
             IBinder resultTo, String resultWho, int requestCode,
diff --git a/services/java/com/android/server/connectivity/Tethering.java b/services/java/com/android/server/connectivity/Tethering.java
index 166b6b6..5e1b82f 100644
--- a/services/java/com/android/server/connectivity/Tethering.java
+++ b/services/java/com/android/server/connectivity/Tethering.java
@@ -91,7 +91,6 @@
     private static final String DNS_DEFAULT_SERVER2 = "4.2.2.2";
 
     private boolean mDunRequired;  // configuration info - must use DUN apn on 3g
-    private boolean mUseHiPri;
 
     private HierarchicalStateMachine mTetherMasterSM;
 
@@ -1052,13 +1051,12 @@
                 return false;
             }
             protected int turnOnMobileConnection() {
-                Log.d(TAG, "turnonMobileConnection with mUseHiPri="+mUseHiPri);
                 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
                 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
                 int retValue = Phone.APN_REQUEST_FAILED;
                 try {
                     retValue = service.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
-                            (mUseHiPri ? Phone.FEATURE_ENABLE_HIPRI : Phone.FEATURE_ENABLE_DUN),
+                            (mDunRequired ? Phone.FEATURE_ENABLE_DUN : Phone.FEATURE_ENABLE_HIPRI),
                             new Binder());
                 } catch (Exception e) {
                 }
@@ -1083,8 +1081,8 @@
                             IConnectivityManager.Stub.asInterface(b);
                     try {
                         service.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
-                                (mUseHiPri ? Phone.FEATURE_ENABLE_HIPRI :
-                                             Phone.FEATURE_ENABLE_DUN));
+                                (mDunRequired? Phone.FEATURE_ENABLE_DUN :
+                                             Phone.FEATURE_ENABLE_HIPRI));
                     } catch (Exception e) {
                         return false;
                     }
@@ -1175,57 +1173,51 @@
                 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
                 IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b);
                 mConnectionRequested = false;
-                mUseHiPri = false;
                 Log.d(TAG, "chooseUpstreamType(" + tryCell + "),  dunRequired ="
                         + mDunRequired + ", iface=" + iface);
-                if (mDunRequired) {
-                    // check if Dun is on
+                if (iface != null) {
                     try {
-                        NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_DUN);
-                        if (info.isConnected()) {
-                            Log.d(TAG, "setting dun ifacename =" + iface);
-                            notifyTetheredOfNewIface(iface);
-                            // even if we're already connected - it may be somebody else's
-                            // refcount, so add our own
-                            turnOnMobileConnection();
-                        }
-                    } catch (RemoteException e) {
-                        Log.e(TAG, "RemoteException calling ConnectivityManager");
-                        notifyTetheredOfNewIface(null);
-                    }
-                    if (tryCell == TRY_TO_SETUP_MOBILE_CONNECTION) {
-                        turnOnMobileConnection();
-                    }
-                } else {
-                    if (iface == null) {
-                        if (tryCell == TRY_TO_SETUP_MOBILE_CONNECTION) {
-                            Log.d(TAG, "turning on hipri");
-                            mUseHiPri = true;
-                            turnOnMobileConnection(); // try to turn on hipri
-                        }
-
-                    } else {
-                        try {
+                        if (mDunRequired) {
+                            // check if Dun is on - we can use that
+                            NetworkInfo info = cm.getNetworkInfo(
+                                    ConnectivityManager.TYPE_MOBILE_DUN);
+                            if (info.isConnected()) {
+                                Log.d(TAG, "setting dun ifacename =" + iface);
+                                // even if we're already connected - it may be somebody else's
+                                // refcount, so add our own
+                                turnOnMobileConnection();
+                            } else {
+                                // verify the iface is not the default mobile - can't use that!
+                                info = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
+                                if (info.isConnected()) {
+                                    iface = null; // can't accept this one
+                                }
+                            }
+                        } else {
                             Log.d(TAG, "checking if hipri brought us this connection");
                             NetworkInfo info = cm.getNetworkInfo(
                                     ConnectivityManager.TYPE_MOBILE_HIPRI);
                             if (info.isConnected()) {
                                 Log.d(TAG, "yes - hipri in use");
-                                mUseHiPri = true;
+                                // even if we're already connected - it may be sombody else's
+                                // refcount, so add our own
                                 turnOnMobileConnection();
                             }
-                        } catch (RemoteException e) {
-                            Log.e(TAG, "RemoteException calling ConnectivityManager");
                         }
-                        // we don't require Dun and have an iface that satisfies, so use it
-                        Log.d(TAG, "setting non-dun iface =" + iface);
-                        notifyTetheredOfNewIface(iface);
+                    } catch (RemoteException e) {
+                        Log.e(TAG, "RemoteException calling ConnectivityManager " + e);
+                        iface = null;
                     }
                 }
-                if (iface == null) {
+                // may have been set to null in the if above
+                if (iface == null ) {
+                    if (tryCell == TRY_TO_SETUP_MOBILE_CONNECTION) {
+                        turnOnMobileConnection();
+                    }
                     // wait for things to settle and retry
                     sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS);
                 }
+                notifyTetheredOfNewIface(iface);
             }
             protected void notifyTetheredOfNewIface(String ifaceName) {
                 Log.d(TAG, "notifying tethered with iface =" + ifaceName);
@@ -1240,7 +1232,6 @@
         class InitialState extends TetherMasterUtilState {
             @Override
             public void enter() {
-                mUseHiPri = false;
                 mConnectionRequested = false;
             }
             @Override
diff --git a/tests/AndroidTests/apks/install_decl_perm/res/values/strings.xml b/tests/AndroidTests/apks/install_decl_perm/res/values/strings.xml
index 5564300..3b8b3b1 100644
--- a/tests/AndroidTests/apks/install_decl_perm/res/values/strings.xml
+++ b/tests/AndroidTests/apks/install_decl_perm/res/values/strings.xml
@@ -2,4 +2,5 @@
 
 <!-- Just need this dummy file to have something to build. -->
 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+  <string name="dummy">dummy</string>
 </resources>
diff --git a/tests/AndroidTests/apks/install_use_perm_good/res/values/strings.xml b/tests/AndroidTests/apks/install_use_perm_good/res/values/strings.xml
index 5564300..3b8b3b1 100644
--- a/tests/AndroidTests/apks/install_use_perm_good/res/values/strings.xml
+++ b/tests/AndroidTests/apks/install_use_perm_good/res/values/strings.xml
@@ -2,4 +2,5 @@
 
 <!-- Just need this dummy file to have something to build. -->
 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+  <string name="dummy">dummy</string>
 </resources>
diff --git a/tests/AndroidTests/src/com/android/unit_tests/AppCacheTest.java b/tests/AndroidTests/src/com/android/unit_tests/AppCacheTest.java
index f2f7743..fa7b9ff 100755
--- a/tests/AndroidTests/src/com/android/unit_tests/AppCacheTest.java
+++ b/tests/AndroidTests/src/com/android/unit_tests/AppCacheTest.java
@@ -136,9 +136,7 @@
         if(localLOGV || TRACKING) Log.i(TAG, "blks3="+blks3);
         verifyTestFiles1(cacheDir, "testtmpdir", 5);
     }
-    
-    // TODO: flaky test
-    // @LargeTest
+
     public void testFreeApplicationCacheSomeFiles() throws Exception {
         StatFs st = new StatFs("/data");
         long blks1 = getFreeStorageBlks(st);
diff --git a/tests/AndroidTests/src/com/android/unit_tests/AsecTests.java b/tests/AndroidTests/src/com/android/unit_tests/AsecTests.java
index 9aed363..9a75047 100755
--- a/tests/AndroidTests/src/com/android/unit_tests/AsecTests.java
+++ b/tests/AndroidTests/src/com/android/unit_tests/AsecTests.java
@@ -259,6 +259,17 @@
         }
     }
 
+    public void testNonExistPath() {
+        IMountService ms = getMs();
+        try {
+            String path = ms.getSecureContainerPath("jparks.broke.it");
+            failStr(path);
+        } catch (IllegalArgumentException e) {
+        } catch (Exception e) {
+            failStr(e);
+        }
+    }
+
     public void testUnmountBusyContainer() {
         IMountService ms = getMs();
         try {
diff --git a/tests/LocationTracker/src/com/android/locationtracker/TrackerService.java b/tests/LocationTracker/src/com/android/locationtracker/TrackerService.java
index 5b75653..e2332bf 100644
--- a/tests/LocationTracker/src/com/android/locationtracker/TrackerService.java
+++ b/tests/LocationTracker/src/com/android/locationtracker/TrackerService.java
@@ -193,11 +193,11 @@
     }
 
     private boolean doDebugLogging() {
-        return getPreferences().getBoolean(DEBUG_PREF, true);
+        return getPreferences().getBoolean(DEBUG_PREF, false);
     }
 
     private boolean trackSignalStrength() {
-        return getPreferences().getBoolean(SIGNAL_PREF, true);
+        return getPreferences().getBoolean(SIGNAL_PREF, false);
     }
 
     private float getLocationMinDistance() {