Merge change 9693

* changes:
  add missing types
diff --git a/core/java/android/provider/Im.java b/core/java/android/provider/Im.java
index ef4ff51..a054132 100644
--- a/core/java/android/provider/Im.java
+++ b/core/java/android/provider/Im.java
@@ -1614,6 +1614,12 @@
         /** specifies whether or not to show as away when device is idle */
         public static final String SETTING_SHOW_AWAY_ON_IDLE = "show_away_on_idle";
 
+        /** specifies whether or not to upload heartbeat stat upon login */
+        public static final String SETTING_UPLOAD_HEARTBEAT_STAT = "upload_heartbeat_stat";
+
+        /** specifies the last heartbeat interval received from the server */
+        public static final String SETTING_HEARTBEAT_INTERVAL = "heartbeat_interval";
+
         /**
          * Used for reliable message queue (RMQ). This is for storing the last rmq id received
          * from the GTalk server
@@ -1833,6 +1839,28 @@
             putBooleanValue(contentResolver, providerId, SETTING_SHOW_AWAY_ON_IDLE, showAway);
         }
 
+        /**
+         * A convenience method to set whether or not to upload heartbeat stat.
+         *
+         * @param contentResolver The ContentResolver to use to access the setting table.
+         * @param uploadStat Whether or not to upload heartbeat stat.
+         */
+        public static void setUploadHeartbeatStat(ContentResolver contentResolver,
+                long providerId, boolean uploadStat) {
+            putBooleanValue(contentResolver, providerId, SETTING_UPLOAD_HEARTBEAT_STAT, uploadStat);
+        }
+
+        /**
+         * A convenience method to set the heartbeat interval last received from the server.
+         *
+         * @param contentResolver The ContentResolver to use to access the setting table.
+         * @param interval The heartbeat interval last received from the server.
+         */
+        public static void setHeartbeatInterval(ContentResolver contentResolver,
+                long providerId, long interval) {
+            putLongValue(contentResolver, providerId, SETTING_HEARTBEAT_INTERVAL, interval);
+        }
+
         public static class QueryMap extends ContentQueryMap {
             private ContentResolver mContentResolver;
             private long mProviderId;
@@ -1982,6 +2010,43 @@
             }
 
             /**
+             * Set whether or not to upload heartbeat stat.
+             *
+             * @param uploadStat whether or not to upload heartbeat stat.
+             */
+            public void setUploadHeartbeatStat(boolean uploadStat) {
+                ProviderSettings.setUploadHeartbeatStat(mContentResolver, mProviderId, uploadStat);
+            }
+
+            /**
+             * Get whether or not to upload heartbeat stat.
+             *
+             * @return Whether or not to upload heartbeat stat.
+             */
+            public boolean getUploadHeartbeatStat() {
+                return getBoolean(SETTING_UPLOAD_HEARTBEAT_STAT,
+                        false /* by default do not upload */);
+            }
+
+            /**
+             * Set the last received heartbeat interval from the server.
+             *
+             * @param interval the last received heartbeat interval from the server.
+             */
+            public void setHeartbeatInterval(long interval) {
+                ProviderSettings.setHeartbeatInterval(mContentResolver, mProviderId, interval);
+            }
+
+            /**
+             * Get the last received heartbeat interval from the server.
+             *
+             * @return the last received heartbeat interval from the server.
+             */
+            public long getHeartbeatInterval() {
+                return getLong(SETTING_HEARTBEAT_INTERVAL, 0L /* an invalid default interval */);
+            }
+
+            /**
              * Convenience function for retrieving a single settings value
              * as a boolean.
              *
@@ -2019,6 +2084,19 @@
                 ContentValues values = getValues(name);
                 return values != null ? values.getAsInteger(VALUE) : def;
             }
+
+            /**
+             * Convenience function for retrieving a single settings value
+             * as a Long.
+             *
+             * @param name The name of the setting to retrieve.
+             * @param def The value to return if the setting is not defined.
+             * @return The setting's current value or 'def' if it is not defined.
+             */
+            private long getLong(String name, long def) {
+                ContentValues values = getValues(name);
+                return values != null ? values.getAsLong(VALUE) : def;
+            }
         }
 
     }
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java
index dd7dd02..365d053 100644
--- a/graphics/java/android/renderscript/RenderScript.java
+++ b/graphics/java/android/renderscript/RenderScript.java
@@ -216,7 +216,6 @@
     }
 
 
-
     public enum DepthFunc {
         ALWAYS (0),
         LESS (1),
@@ -352,105 +351,6 @@
     }
 
     //////////////////////////////////////////////////////////////////////////////////
-    // Script
-
-    public class Script extends BaseObj {
-        Script(int id) {
-            super(RenderScript.this);
-            mID = id;
-        }
-
-        public void destroy() {
-            nScriptDestroy(mID);
-            mID = 0;
-        }
-
-        public void bindAllocation(Allocation va, int slot) {
-            nScriptBindAllocation(mID, va.mID, slot);
-        }
-    }
-
-    public void scriptCBegin() {
-        nScriptCBegin();
-    }
-
-    public void scriptCSetTimeZone(String timeZone) {
-        try {
-            byte[] bytes = timeZone.getBytes("UTF-8");
-            nScriptCSetTimeZone(bytes);
-        } catch (java.io.UnsupportedEncodingException e) {
-            throw new RuntimeException(e);
-        }
-    }
-
-    public void scriptCSetClearColor(float r, float g, float b, float a) {
-        nScriptCSetClearColor(r, g, b, a);
-    }
-
-    public void scriptCSetClearDepth(float d) {
-        nScriptCSetClearDepth(d);
-    }
-
-    public void scriptCSetClearStencil(int stencil) {
-        nScriptCSetClearStencil(stencil);
-    }
-
-    public void scriptCAddType(Type t) {
-        nScriptCAddType(t.mID);
-    }
-
-    public void scriptCSetRoot(boolean r) {
-        nScriptCSetRoot(r);
-    }
-
-    public void scriptCSetScript(String s) {
-        try {
-            byte[] bytes = s.getBytes("UTF-8");
-            nScriptCSetScript(bytes, 0, bytes.length);
-        } catch (java.io.UnsupportedEncodingException e) {
-            throw new RuntimeException(e);
-        }
-    }
-
-    public void scriptCSetScript(Resources resources, int id) {
-        InputStream is = resources.openRawResource(id);
-        try {
-            try {
-                scriptCSetScript(is);
-            } finally {
-                is.close();
-            }
-        } catch(IOException e) {
-            throw new Resources.NotFoundException();
-        }
-    }
-
-    public void  scriptCSetScript(InputStream is) throws IOException {
-        byte[] buf = new byte[1024];
-        int currentPos = 0;
-        while(true) {
-            int bytesLeft = buf.length - currentPos;
-            if (bytesLeft == 0) {
-                byte[] buf2 = new byte[buf.length * 2];
-                System.arraycopy(buf, 0, buf2, 0, buf.length);
-                buf = buf2;
-                bytesLeft = buf.length - currentPos;
-            }
-            int bytesRead = is.read(buf, currentPos, bytesLeft);
-            if (bytesRead <= 0) {
-                break;
-            }
-            currentPos += bytesRead;
-        }
-        nScriptCSetScript(buf, 0, currentPos);
-    }
-
-    public Script scriptCCreate() {
-        int id = nScriptCCreate();
-        return new Script(id);
-    }
-
-    //////////////////////////////////////////////////////////////////////////////////
     // ProgramVertex
 
     public class ProgramVertex extends BaseObj {
diff --git a/graphics/java/android/renderscript/Script.java b/graphics/java/android/renderscript/Script.java
new file mode 100644
index 0000000..e7bb7a5
--- /dev/null
+++ b/graphics/java/android/renderscript/Script.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2008 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 android.renderscript;
+
+/**
+ * @hide
+ **/
+public class Script extends BaseObj {
+    boolean mIsRoot;
+
+    Script(int id, RenderScript rs) {
+        super(rs);
+        mID = id;
+    }
+
+    public void destroy() {
+        mRS.nScriptDestroy(mID);
+        mID = 0;
+    }
+
+    public void bindAllocation(Allocation va, int slot) {
+        mRS.nScriptBindAllocation(mID, va.mID, slot);
+    }
+
+    public void setClearColor(float r, float g, float b, float a) {
+        //mRS.nScriptCSetClearColor(r, g, b, a);
+    }
+
+    public void setClearDepth(float d) {
+        //mRS.nScriptCSetClearDepth(d);
+    }
+
+    public void setClearStencil(int stencil) {
+        //mRS.nScriptCSetClearStencil(stencil);
+    }
+
+
+    public static class Builder {
+        RenderScript mRS;
+        boolean mIsRoot = false;
+        byte[] mTimeZone;
+
+        Builder(RenderScript rs) {
+            mRS = rs;
+        }
+
+        public void addType(Type t) {
+            mRS.nScriptCAddType(t.mID);
+        }
+
+        void transferCreate() {
+            if(mTimeZone != null) {
+                mRS.nScriptCSetTimeZone(mTimeZone);
+            }
+            mRS.nScriptCSetRoot(mIsRoot);
+        }
+
+        void transferObject(Script s) {
+            s.mIsRoot = mIsRoot;
+        }
+
+        public void setTimeZone(String timeZone) {
+            try {
+                mTimeZone = timeZone.getBytes("UTF-8");
+            } catch (java.io.UnsupportedEncodingException e) {
+                throw new RuntimeException(e);
+            }
+        }
+
+        public void setRoot(boolean r) {
+            mIsRoot = r;
+        }
+
+    }
+
+}
+
diff --git a/graphics/java/android/renderscript/ScriptC.java b/graphics/java/android/renderscript/ScriptC.java
new file mode 100644
index 0000000..0592f5d
--- /dev/null
+++ b/graphics/java/android/renderscript/ScriptC.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2008 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 android.renderscript;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import android.content.res.Resources;
+
+
+/**
+ * @hide
+ **/
+public class ScriptC extends Script {
+    ScriptC(int id, RenderScript rs) {
+        super(id, rs);
+    }
+
+
+
+
+    public static class Builder extends Script.Builder {
+        byte[] mProgram;
+        int mProgramLength;
+
+        public Builder(RenderScript rs) {
+            super(rs);
+        }
+
+        public void setScript(String s) {
+            try {
+                mProgram = s.getBytes("UTF-8");
+                mProgramLength = mProgram.length;
+            } catch (java.io.UnsupportedEncodingException e) {
+                throw new RuntimeException(e);
+            }
+        }
+
+        public void setScript(Resources resources, int id) {
+            InputStream is = resources.openRawResource(id);
+            try {
+                try {
+                    setScript(is);
+                } finally {
+                    is.close();
+                }
+            } catch(IOException e) {
+                throw new Resources.NotFoundException();
+            }
+        }
+
+        public void  setScript(InputStream is) throws IOException {
+            byte[] buf = new byte[1024];
+            int currentPos = 0;
+            while(true) {
+                int bytesLeft = buf.length - currentPos;
+                if (bytesLeft == 0) {
+                    byte[] buf2 = new byte[buf.length * 2];
+                    System.arraycopy(buf, 0, buf2, 0, buf.length);
+                    buf = buf2;
+                    bytesLeft = buf.length - currentPos;
+                }
+                int bytesRead = is.read(buf, currentPos, bytesLeft);
+                if (bytesRead <= 0) {
+                    break;
+                }
+                currentPos += bytesRead;
+            }
+            mProgram = buf;
+            mProgramLength = currentPos;
+        }
+
+        static synchronized ScriptC internalCreate(Builder b) {
+            b.mRS.nScriptCBegin();
+            b.transferCreate();
+
+            b.mRS.nScriptCSetScript(b.mProgram, 0, b.mProgramLength);
+
+
+            int id = b.mRS.nScriptCCreate();
+            ScriptC obj = new ScriptC(id, b.mRS);
+            b.transferObject(obj);
+            return obj;
+        }
+
+        public ScriptC create() {
+            return internalCreate(this);
+        }
+    }
+
+}
+
diff --git a/libs/rs/java/Film/src/com/android/film/FilmRS.java b/libs/rs/java/Film/src/com/android/film/FilmRS.java
index eda7624..a9eaead 100644
--- a/libs/rs/java/Film/src/com/android/film/FilmRS.java
+++ b/libs/rs/java/Film/src/com/android/film/FilmRS.java
@@ -29,6 +29,8 @@
 import android.renderscript.Element;
 import android.renderscript.Allocation;
 import android.renderscript.Dimension;
+import android.renderscript.ScriptC;
+import android.renderscript.Script;
 
 public class FilmRS {
     private final int POS_TRANSLATE = 0;
@@ -66,8 +68,8 @@
 
     private Resources mRes;
     private RenderScript mRS;
-    private RenderScript.Script mScriptStrip;
-    private RenderScript.Script mScriptImage;
+    private Script mScriptStrip;
+    private Script mScriptImage;
     private Element mElementVertex;
     private Element mElementIndex;
     private RenderScript.Sampler mSampler;
@@ -227,11 +229,11 @@
 
         Log.e("rs", "Done loading named");
 
-        mRS.scriptCBegin();
-        mRS.scriptCSetClearColor(0.0f, 0.0f, 0.0f, 1.0f);
-        mRS.scriptCSetScript(mRes, R.raw.filmstrip);
-        mRS.scriptCSetRoot(true);
-        mScriptStrip = mRS.scriptCCreate();
+        ScriptC.Builder sb = new ScriptC.Builder(mRS);
+        sb.setScript(mRes, R.raw.filmstrip);
+        sb.setRoot(true);
+        mScriptStrip = sb.create();
+        mScriptStrip.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
 
         mAllocPos = Allocation.createSized(mRS,
             Element.USER_FLOAT, mBufferPos.length);
diff --git a/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java b/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java
index 7123bf7..c8d02c1 100644
--- a/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java
+++ b/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java
@@ -29,6 +29,8 @@
 import android.renderscript.ProgramVertexAlloc;
 import android.renderscript.Element;
 import android.renderscript.Allocation;
+import android.renderscript.Script;
+import android.renderscript.ScriptC;
 
 public class FountainRS {
 
@@ -57,7 +59,7 @@
     private Allocation mIntAlloc;
     private Allocation mPartAlloc;
     private Allocation mVertAlloc;
-    private RenderScript.Script mScript;
+    private Script mScript;
     private RenderScript.ProgramFragmentStore mPFS;
     private RenderScript.ProgramFragment mPF;
 
@@ -98,11 +100,11 @@
         }
         mPartAlloc.data(t2);
 
-        mRS.scriptCBegin();
-        mRS.scriptCSetClearColor(0.0f, 0.0f, 0.0f, 1.0f);
-        mRS.scriptCSetScript(mRes, R.raw.fountain);
-        mRS.scriptCSetRoot(true);
-        mScript = mRS.scriptCCreate();
+        ScriptC.Builder sb = new ScriptC.Builder(mRS);
+        sb.setScript(mRes, R.raw.fountain);
+        sb.setRoot(true);
+        mScript = sb.create();
+        mScript.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
 
         mScript.bindAllocation(mIntAlloc, 0);
         mScript.bindAllocation(mPartAlloc, 1);
diff --git a/libs/rs/java/Grass/src/com/android/grass/rs/GrassRS.java b/libs/rs/java/Grass/src/com/android/grass/rs/GrassRS.java
index cf7affc..2511150 100644
--- a/libs/rs/java/Grass/src/com/android/grass/rs/GrassRS.java
+++ b/libs/rs/java/Grass/src/com/android/grass/rs/GrassRS.java
@@ -27,10 +27,11 @@
 import android.renderscript.Allocation;
 import android.renderscript.ProgramVertexAlloc;
 import static android.renderscript.Element.*;
-
 import static android.util.MathUtils.*;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
+import android.renderscript.Script;
+import android.renderscript.ScriptC;
 
 import java.util.TimeZone;
 
@@ -41,8 +42,8 @@
 
     private static final int RSID_SKY_TEXTURES = 1;
     private static final int SKY_TEXTURES_COUNT = 5;
-    
-    private static final int RSID_BLADES = 2;    
+
+    private static final int RSID_BLADES = 2;
     private static final int BLADES_COUNT = 100;
     private static final int BLADE_STRUCT_FIELDS_COUNT = 12;
     private static final int BLADE_STRUCT_DEGREE = 0;
@@ -57,16 +58,16 @@
     private static final int BLADE_STRUCT_H = 9;
     private static final int BLADE_STRUCT_S = 10;
     private static final int BLADE_STRUCT_B = 11;
-    
+
     private Resources mResources;
     private RenderScript mRS;
     private final BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
-    
+
     private final int mWidth;
     private final int mHeight;
 
     @SuppressWarnings({"FieldCanBeLocal"})
-    private RenderScript.Script mScript;
+    private Script mScript;
     @SuppressWarnings({"FieldCanBeLocal"})
     private RenderScript.Sampler mSampler;
     @SuppressWarnings({"FieldCanBeLocal"})
@@ -74,7 +75,7 @@
     @SuppressWarnings({"FieldCanBeLocal"})
     private RenderScript.ProgramFragmentStore mPfsBackground;
     @SuppressWarnings({"FieldCanBeLocal"})
-    private RenderScript.ProgramVertex mPvBackground;    
+    private RenderScript.ProgramVertex mPvBackground;
     @SuppressWarnings({"FieldCanBeLocal"})
     private ProgramVertexAlloc mPvOrthoAlloc;
 
@@ -112,13 +113,12 @@
         createProgramFragment();
         createScriptStructures();
 
-        mRS.scriptCBegin();
-        mRS.scriptCSetClearColor(0.0f, 0.0f, 0.0f, 1.0f);
-        mRS.scriptCSetScript(mResources, R.raw.grass);
-        mRS.scriptCSetTimeZone(TimeZone.getDefault().getID());
-        mRS.scriptCSetRoot(true);
-
-        mScript = mRS.scriptCCreate();
+        ScriptC.Builder sb = new ScriptC.Builder(mRS);
+        sb.setScript(mResources, R.raw.grass);
+        sb.setTimeZone(TimeZone.getDefault().getID());
+        sb.setRoot(true);
+        mScript = sb.create();
+        mScript.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
 
         loadSkyTextures();
         mScript.bindAllocation(mState, RSID_STATE);
@@ -189,7 +189,7 @@
         allocation.setName(name);
         return allocation;
     }
-    
+
     private Allocation loadTextureARGB(int id, String name) {
         // Forces ARGB 32 bits, because pngcrush sometimes optimize our PNGs to
         // indexed pictures, which are not well supported
@@ -219,7 +219,7 @@
         mRS.programFragmentSetTexEnvMode(0, MODULATE);
         mPfGrass = mRS.programFragmentCreate();
         mPfGrass.setName("PFGrass");
-        mPfGrass.bindSampler(mSampler, 0);        
+        mPfGrass.bindSampler(mSampler, 0);
     }
 
     private void createProgramFragmentStore() {
@@ -237,7 +237,7 @@
         mRS.programFragmentStoreDitherEnable(true);
         mRS.programFragmentStoreDepthMask(false);
         mPfsGrass = mRS.programFragmentStoreCreate();
-        mPfsGrass.setName("PFSGrass");        
+        mPfsGrass.setName("PFSGrass");
     }
 
     private void createProgramVertex() {
diff --git a/libs/rs/java/Rollo/src/com/android/rollo/RolloRS.java b/libs/rs/java/Rollo/src/com/android/rollo/RolloRS.java
index cb3dd51..578c225 100644
--- a/libs/rs/java/Rollo/src/com/android/rollo/RolloRS.java
+++ b/libs/rs/java/Rollo/src/com/android/rollo/RolloRS.java
@@ -22,6 +22,8 @@
 import android.renderscript.ProgramVertexAlloc;
 import android.renderscript.Element;
 import android.renderscript.Allocation;
+import android.renderscript.Script;
+import android.renderscript.ScriptC;
 
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
@@ -90,7 +92,7 @@
 
     private Resources mRes;
     private RenderScript mRS;
-    private RenderScript.Script mScript;
+    private Script mScript;
     private RenderScript.Sampler mSampler;
     private RenderScript.Sampler mSamplerText;
     private RenderScript.ProgramFragmentStore mPFSBackground;
@@ -313,12 +315,12 @@
 
 
     private void initRS() {
-        mRS.scriptCBegin();
-        mRS.scriptCSetClearColor(0.0f, 0.0f, 0.0f, 0.0f);
-        mRS.scriptCSetScript(mRes, R.raw.rollo);
-        //mRS.scriptCSetScript(mRes, R.raw.rollo2);
-        mRS.scriptCSetRoot(true);
-        mScript = mRS.scriptCCreate();
+        ScriptC.Builder sb = new ScriptC.Builder(mRS);
+        sb.setScript(mRes, R.raw.rollo);
+        //sb.setScript(mRes, R.raw.rollo2);
+        sb.setRoot(true);
+        mScript = sb.create();
+        mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
 
         mAllocStateBuf = new int[] {0, 0, 0, 8, 0, 0, -1, 0, mAllocIconIDBuf.length, 0, 0};
         mAllocState = Allocation.createSized(mRS,
diff --git a/services/java/com/android/server/BackupManagerService.java b/services/java/com/android/server/BackupManagerService.java
index 67f3816..77ba914 100644
--- a/services/java/com/android/server/BackupManagerService.java
+++ b/services/java/com/android/server/BackupManagerService.java
@@ -307,36 +307,42 @@
         mEverStored = new File(mBaseStateDir, "processed");
         File tempProcessedFile = new File(mBaseStateDir, "processed.new");
         try {
-            RandomAccessFile temp = new RandomAccessFile(tempProcessedFile, "rw");
-            mEverStoredStream = new RandomAccessFile(mEverStored, "r");
+            // If there are previous contents, parse them out then start a new
+            // file to continue the recordkeeping.
+            if (mEverStored.exists()) {
+                RandomAccessFile temp = new RandomAccessFile(tempProcessedFile, "rw");
+                mEverStoredStream = new RandomAccessFile(mEverStored, "r");
 
-            // parse its existing contents
-            mEverStoredStream.seek(0);
-            temp.seek(0);
-            try {
-                while (true) {
-                    PackageInfo info;
-                    String pkg = mEverStoredStream.readUTF();
-                    try {
-                        info = mPackageManager.getPackageInfo(pkg, 0);
-                        mEverStoredApps.add(pkg);
-                        temp.writeUTF(pkg);
-                        if (DEBUG) Log.v(TAG, "   + " + pkg);
-                    } catch (NameNotFoundException e) {
-                        // nope, this package was uninstalled; don't include it
-                        if (DEBUG) Log.v(TAG, "   - " + pkg);
+                // parse its existing contents
+                mEverStoredStream.seek(0);
+                temp.seek(0);
+                try {
+                    while (true) {
+                        PackageInfo info;
+                        String pkg = mEverStoredStream.readUTF();
+                        try {
+                            info = mPackageManager.getPackageInfo(pkg, 0);
+                            mEverStoredApps.add(pkg);
+                            temp.writeUTF(pkg);
+                            if (DEBUG) Log.v(TAG, "   + " + pkg);
+                        } catch (NameNotFoundException e) {
+                            // nope, this package was uninstalled; don't include it
+                            if (DEBUG) Log.v(TAG, "   - " + pkg);
+                        }
                     }
+                } catch (EOFException e) {
+                    // now we're at EOF
                 }
-            } catch (EOFException e) {
-                // now we're at EOF
-            }
 
-            // Once we've rewritten the backup history log, atomically replace the
-            // old one with the new one then reopen the file for continuing use.
-            temp.close();
-            mEverStoredStream.close();
-            tempProcessedFile.renameTo(mEverStored);
+                // Once we've rewritten the backup history log, atomically replace the
+                // old one with the new one then reopen the file for continuing use.
+                temp.close();
+                mEverStoredStream.close();
+                tempProcessedFile.renameTo(mEverStored);
+            }
+            // This will create the file if it doesn't exist
             mEverStoredStream = new RandomAccessFile(mEverStored, "rwd");
+            mEverStoredStream.seek(mEverStoredStream.length());
         } catch (IOException e) {
             Log.e(TAG, "Unable to open known-stored file!");
             mEverStoredStream = null;
diff --git a/services/java/com/android/server/NotificationManagerService.java b/services/java/com/android/server/NotificationManagerService.java
index aac7124..5dad8d0 100644
--- a/services/java/com/android/server/NotificationManagerService.java
+++ b/services/java/com/android/server/NotificationManagerService.java
@@ -96,7 +96,7 @@
     private Vibrator mVibrator = new Vibrator();
 
     // adb
-    private int mBatteryPlugged;
+    private boolean mUsbConnected;
     private boolean mAdbEnabled = false;
     private boolean mAdbNotificationShown = false;
     private Notification mAdbNotification;
@@ -310,8 +310,11 @@
                     mBatteryFull = batteryFull;
                     updateLights();
                 }
-                
-                mBatteryPlugged = intent.getIntExtra("plugged", 0);
+            } else if (action.equals(Intent.ACTION_UMS_CONNECTED)) {
+                mUsbConnected = true;
+                updateAdbNotification();
+            } else if (action.equals(Intent.ACTION_UMS_DISCONNECTED)) {
+                mUsbConnected = false;
                 updateAdbNotification();
             } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
                     || action.equals(Intent.ACTION_PACKAGE_RESTARTED)) {
@@ -380,6 +383,8 @@
         // register for battery changed notifications
         IntentFilter filter = new IntentFilter();
         filter.addAction(Intent.ACTION_BATTERY_CHANGED);
+        filter.addAction(Intent.ACTION_UMS_CONNECTED);
+        filter.addAction(Intent.ACTION_UMS_DISCONNECTED);
         filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
         filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
         mContext.registerReceiver(mIntentReceiver, filter);
@@ -954,7 +959,7 @@
     // security feature that we don't want people customizing the platform
     // to accidentally lose.
     private void updateAdbNotification() {
-        if (mAdbEnabled && mBatteryPlugged == BatteryManager.BATTERY_PLUGGED_USB) {
+        if (mAdbEnabled && mUsbConnected) {
             if ("0".equals(SystemProperties.get("persist.adb.notify"))) {
                 return;
             }
diff --git a/tests/backup/test_backup.sh b/tests/backup/test_backup.sh
index dbf9ed2..dd3907c 100755
--- a/tests/backup/test_backup.sh
+++ b/tests/backup/test_backup.sh
@@ -1,27 +1,54 @@
 #!/bin/bash
 
+# Copyright (C) 2009 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.
+
+
+ADB_OPTS="$@"
+
+#FIXME: what was this for?
 #adb kill-server
 
+# run adb as root so we can poke at com.android.backuptest's data
+root_status=$(adb $ADB_OPTS root)
+if [ "x$root_status" != "xadbd is already running as root" ]; then
+    sleep 2
+    adb $ADB_OPTS 'wait-for-device'
+fi
+
+# show commands as we go
+set -x
+
 # set the transport
-adb shell bmgr transport 1
+adb $ADB_OPTS shell bmgr transport com.google.android.backup/.BackupTransportService
 
 # load up the three files
-adb shell "rm /data/data/com.android.backuptest/files/* ; \
-           mkdir /data/data/com.android.backuptest ; \
-           mkdir /data/data/com.android.backuptest/files ; \
-           mkdir /data/data/com.android.backuptest/shared_prefs ; \
-           echo -n \"<map><int name=\\\"pref\\\" value=\\\"1\\\" /></map>\" > /data/data/com.android.backuptest/shared_prefs/raw.xml ; \
-           echo -n first file > /data/data/com.android.backuptest/files/file.txt ; \
-           echo -n asdf > /data/data/com.android.backuptest/files/another_file.txt ; \
-           echo -n 3 > /data/data/com.android.backuptest/files/3.txt ; \
-           echo -n "" > /data/data/com.android.backuptest/files/empty.txt ; \
+adb $ADB_OPTS shell \
+   "rm /data/data/com.android.backuptest/files/* ; \
+    mkdir /data/data/com.android.backuptest ; \
+    mkdir /data/data/com.android.backuptest/files ; \
+    mkdir /data/data/com.android.backuptest/shared_prefs ; \
+    echo -n \"<map><int name=\\\"pref\\\" value=\\\"1\\\" /></map>\" > /data/data com.android.backuptest/shared_prefs/raw.xml ; \
+    echo -n first file > /data/data/com.android.backuptest/files/file.txt ; \
+    echo -n asdf > /data/data/com.android.backuptest/files/another_file.txt ; \
+    echo -n 3 > /data/data/com.android.backuptest/files/3.txt ; \
+    echo -n "" > /data/data/com.android.backuptest/files/empty.txt ; \
 "
 
 # say that the data has changed
-adb shell bmgr backup com.android.backuptest
+adb $ADB_OPTS shell bmgr backup com.android.backuptest
 
 # run the backup
-adb shell bmgr run
-
-
+adb $ADB_OPTS shell bmgr run
 
diff --git a/tests/backup/test_restore.sh b/tests/backup/test_restore.sh
index ccf29cf..8de70cb 100755
--- a/tests/backup/test_restore.sh
+++ b/tests/backup/test_restore.sh
@@ -1,8 +1,25 @@
 #!/bin/bash
 
+# Copyright (C) 2009 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.
+
+
+ADB_OPTS="$@"
+
 function check_file
 {
-    data=$(adb shell cat /data/data/com.android.backuptest/$1)
+    data=$(adb $ADB_OPTS shell cat /data/data/com.android.backuptest/$1)
     if [ "$data" = "$2" ] ; then
         echo "$1 has correct value [$2]"
     else
@@ -12,24 +29,39 @@
     fi
 }
 
+# run adb as root so we can poke at com.android.backuptest's data
+root_status=$(adb $ADB_OPTS root)
+if [ "x$root_status" != "xadbd is already running as root" ]; then
+    echo -n "Restarting adb as root..."
+    sleep 2
+    adb $ADB_OPTS 'wait-for-device'
+    echo done.
+fi
+
 # delete the old data
 echo --- Previous files
-adb shell "ls -l /data/data/com.android.backuptest/files"
-adb shell "rm /data/data/com.android.backuptest/files/*"
+adb $ADB_OPTS shell "ls -l /data/data/com.android.backuptest/files"
+adb $ADB_OPTS shell "rm /data/data/com.android.backuptest/files/*"
 echo --- Previous shared_prefs
-adb shell "ls -l /data/data/com.android.backuptest/shared_prefs"
-adb shell "rm /data/data/com.android.backuptest/shared_prefs/*"
+adb $ADB_OPTS shell "ls -l /data/data/com.android.backuptest/shared_prefs"
+adb $ADB_OPTS shell "rm /data/data/com.android.backuptest/shared_prefs/*"
 echo --- Erased files and shared_prefs
-adb shell "ls -l /data/data/com.android.backuptest/files"
-adb shell "ls -l /data/data/com.android.backuptest/shared_prefs"
+adb $ADB_OPTS shell "ls -l /data/data/com.android.backuptest/files"
+adb $ADB_OPTS shell "ls -l /data/data/com.android.backuptest/shared_prefs"
 echo ---
 
 echo
 echo
 echo
 
+# FIXME: there's probably a smarter way to do this
+# FIXME: if we can get the android ID, that's probably the safest thing to do
+# pick the most recent set and restore from it
+restore_set=$(adb $ADB_OPTS shell bmgr list sets | head -n1 | awk '{print $1}')
+
 # run the restore
-adb shell bmgr restore 0
+printf "Restoring from set %d (hex: 0x%x)\n" $restore_set $restore_set
+adb $ADB_OPTS shell bmgr restore $restore_set
 
 echo
 echo
@@ -46,8 +78,9 @@
 echo
 echo
 echo --- Restored files
-adb shell "ls -l /data/data/com.android.backuptest/files"
+adb $ADB_OPTS shell "ls -l /data/data/com.android.backuptest/files"
 echo --- Restored shared_prefs
-adb shell "ls -l /data/data/com.android.backuptest/shared_prefs"
+adb $ADB_OPTS shell "ls -l /data/data/com.android.backuptest/shared_prefs"
 echo ---
 echo
+