Merge "Moved practically all of the prerefactoring functionality to the new design."
diff --git a/CleanSpec.mk b/CleanSpec.mk
index 26d8a1b..1efe77c 100644
--- a/CleanSpec.mk
+++ b/CleanSpec.mk
@@ -54,7 +54,19 @@
 $(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/JAVA_LIBRARIES/android.policy*)
 $(call add-clean-step, rm -rf $(TARGET_OUT_JAVA_LIBRARIES)/android.policy.jar)
 $(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/JAVA_LIBRARIES/framework_intermediates)
-
+$(call add-clean-step, rm -f $(PRODUCT_OUT)/obj/lib/libequalizer.so)
+$(call add-clean-step, rm -f $(PRODUCT_OUT)/obj/lib/libequalizertest.so)
+$(call add-clean-step, rm -f $(PRODUCT_OUT)/obj/lib/libreverb.so)
+$(call add-clean-step, rm -f $(PRODUCT_OUT)/obj/lib/libreverbtest.so)
+$(call add-clean-step, rm -f $(PRODUCT_OUT)/symbols/system/lib/libequalizer.so)
+$(call add-clean-step, rm -f $(PRODUCT_OUT)/symbols/system/lib/libequalizertest.so)
+$(call add-clean-step, rm -f $(PRODUCT_OUT)/symbols/system/lib/libreverb.so)
+$(call add-clean-step, rm -f $(PRODUCT_OUT)/symbols/system/lib/libreverbtest.so)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/SHARED_LIBRARIES/libequalizer_intermediates)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/SHARED_LIBRARIES/libequalizertest_intermediates)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/SHARED_LIBRARIES/libreverb_intermediates)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/SHARED_LIBRARIES/libreverbtest_intermediates)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/lib/soundfx/)
 
 # ************************************************
 # NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
diff --git a/api/current.xml b/api/current.xml
index 96ca9e9..a828736 100644
--- a/api/current.xml
+++ b/api/current.xml
@@ -21241,6 +21241,19 @@
  visibility="protected"
 >
 </method>
+<method name="onStartActionMode"
+ return="android.view.ActionMode"
+ abstract="false"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="callback" type="android.view.ActionMode.Callback">
+</parameter>
+</method>
 <method name="onStop"
  return="void"
  abstract="false"
@@ -21900,6 +21913,19 @@
 <parameter name="requestCode" type="int">
 </parameter>
 </method>
+<method name="startContextMode"
+ return="android.view.ActionMode"
+ abstract="false"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="callback" type="android.view.ActionMode.Callback">
+</parameter>
+</method>
 <method name="startIntentSenderForResult"
  return="void"
  abstract="false"
@@ -24546,194 +24572,6 @@
 >
 </method>
 </class>
-<class name="ContextualMode"
- extends="java.lang.Object"
- abstract="true"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<constructor name="ContextualMode"
- type="android.app.ContextualMode"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</constructor>
-<method name="finish"
- return="void"
- abstract="true"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="getCustomView"
- return="android.view.View"
- abstract="true"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="getMenu"
- return="android.view.Menu"
- abstract="true"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="getSubtitle"
- return="java.lang.CharSequence"
- abstract="true"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="getTitle"
- return="java.lang.CharSequence"
- abstract="true"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="invalidate"
- return="void"
- abstract="true"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="setCustomView"
- return="void"
- abstract="true"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="view" type="android.view.View">
-</parameter>
-</method>
-<method name="setSubtitle"
- return="void"
- abstract="true"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="subtitle" type="java.lang.CharSequence">
-</parameter>
-</method>
-<method name="setTitle"
- return="void"
- abstract="true"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="title" type="java.lang.CharSequence">
-</parameter>
-</method>
-</class>
-<interface name="ContextualMode.Callback"
- abstract="true"
- static="true"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<method name="onContextualItemClicked"
- return="boolean"
- abstract="true"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="mode" type="android.app.ContextualMode">
-</parameter>
-<parameter name="item" type="android.view.MenuItem">
-</parameter>
-</method>
-<method name="onCreateContextualMode"
- return="boolean"
- abstract="true"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="mode" type="android.app.ContextualMode">
-</parameter>
-<parameter name="menu" type="android.view.Menu">
-</parameter>
-</method>
-<method name="onDestroyContextualMode"
- return="void"
- abstract="true"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="mode" type="android.app.ContextualMode">
-</parameter>
-</method>
-<method name="onPrepareContextualMode"
- return="boolean"
- abstract="true"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="mode" type="android.app.ContextualMode">
-</parameter>
-<parameter name="menu" type="android.view.Menu">
-</parameter>
-</method>
-</interface>
 <class name="DatePickerDialog"
  extends="android.app.AlertDialog"
  abstract="false"
@@ -25466,6 +25304,19 @@
  visibility="protected"
 >
 </method>
+<method name="onStartActionMode"
+ return="android.view.ActionMode"
+ abstract="false"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="callback" type="android.view.ActionMode.Callback">
+</parameter>
+</method>
 <method name="onStop"
  return="void"
  abstract="false"
@@ -40357,17 +40208,6 @@
  visibility="public"
 >
 </method>
-<method name="finishContextualMode"
- return="void"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
 <method name="getApplicationContext"
  return="android.content.Context"
  abstract="true"
@@ -41082,19 +40922,6 @@
 <parameter name="intent" type="android.content.Intent">
 </parameter>
 </method>
-<method name="startContextualMode"
- return="android.app.ContextualMode"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="callback" type="android.app.ContextualMode.Callback">
-</parameter>
-</method>
 <method name="startInstrumentation"
  return="boolean"
  abstract="true"
@@ -63391,6 +63218,17 @@
  visibility="public"
 >
 </method>
+<method name="executeUpdateDelete"
+ return="int"
+ abstract="false"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</method>
 <method name="simpleQueryForLong"
  return="long"
  abstract="false"
@@ -176373,6 +176211,194 @@
 >
 </field>
 </class>
+<class name="ActionMode"
+ extends="java.lang.Object"
+ abstract="true"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<constructor name="ActionMode"
+ type="android.view.ActionMode"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</constructor>
+<method name="finish"
+ return="void"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</method>
+<method name="getCustomView"
+ return="android.view.View"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</method>
+<method name="getMenu"
+ return="android.view.Menu"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</method>
+<method name="getSubtitle"
+ return="java.lang.CharSequence"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</method>
+<method name="getTitle"
+ return="java.lang.CharSequence"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</method>
+<method name="invalidate"
+ return="void"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</method>
+<method name="setCustomView"
+ return="void"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="view" type="android.view.View">
+</parameter>
+</method>
+<method name="setSubtitle"
+ return="void"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="subtitle" type="java.lang.CharSequence">
+</parameter>
+</method>
+<method name="setTitle"
+ return="void"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="title" type="java.lang.CharSequence">
+</parameter>
+</method>
+</class>
+<interface name="ActionMode.Callback"
+ abstract="true"
+ static="true"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<method name="onActionItemClicked"
+ return="boolean"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="mode" type="android.view.ActionMode">
+</parameter>
+<parameter name="item" type="android.view.MenuItem">
+</parameter>
+</method>
+<method name="onCreateActionMode"
+ return="boolean"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="mode" type="android.view.ActionMode">
+</parameter>
+<parameter name="menu" type="android.view.Menu">
+</parameter>
+</method>
+<method name="onDestroyActionMode"
+ return="void"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="mode" type="android.view.ActionMode">
+</parameter>
+</method>
+<method name="onPrepareActionMode"
+ return="boolean"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="mode" type="android.view.ActionMode">
+</parameter>
+<parameter name="menu" type="android.view.Menu">
+</parameter>
+</method>
+</interface>
 <interface name="ContextMenu"
  abstract="true"
  static="false"
@@ -188800,6 +188826,19 @@
  visibility="public"
 >
 </method>
+<method name="startActionMode"
+ return="android.view.ActionMode"
+ abstract="false"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="callback" type="android.view.ActionMode.Callback">
+</parameter>
+</method>
 <method name="startAnimation"
  return="void"
  abstract="false"
@@ -191577,6 +191616,21 @@
 <parameter name="originalView" type="android.view.View">
 </parameter>
 </method>
+<method name="startActionModeForChild"
+ return="android.view.ActionMode"
+ abstract="false"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="originalView" type="android.view.View">
+</parameter>
+<parameter name="callback" type="android.view.ActionMode.Callback">
+</parameter>
+</method>
 <method name="startLayoutAnimation"
  return="void"
  abstract="false"
@@ -192267,6 +192321,21 @@
 <parameter name="originalView" type="android.view.View">
 </parameter>
 </method>
+<method name="startActionModeForChild"
+ return="android.view.ActionMode"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="originalView" type="android.view.View">
+</parameter>
+<parameter name="callback" type="android.view.ActionMode.Callback">
+</parameter>
+</method>
 </interface>
 <class name="ViewStub"
  extends="android.view.View"
@@ -194037,6 +194106,19 @@
  visibility="public"
 >
 </method>
+<method name="onStartActionMode"
+ return="android.view.ActionMode"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="callback" type="android.view.ActionMode.Callback">
+</parameter>
+</method>
 <method name="onWindowAttributesChanged"
  return="void"
  abstract="true"
diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java
index 431f66d..61fd5f3 100644
--- a/core/java/android/app/Activity.java
+++ b/core/java/android/app/Activity.java
@@ -16,8 +16,8 @@
 
 package android.app;
 
-import java.util.ArrayList;
-import java.util.HashMap;
+import com.android.internal.app.ActionBarImpl;
+import com.android.internal.policy.PolicyManager;
 
 import android.content.ComponentCallbacks;
 import android.content.ComponentName;
@@ -52,7 +52,9 @@
 import android.util.EventLog;
 import android.util.Log;
 import android.util.SparseArray;
+import android.view.ActionMode;
 import android.view.ContextMenu;
+import android.view.ContextMenu.ContextMenuInfo;
 import android.view.ContextThemeWrapper;
 import android.view.InflateException;
 import android.view.KeyEvent;
@@ -62,21 +64,18 @@
 import android.view.MenuItem;
 import android.view.MotionEvent;
 import android.view.View;
+import android.view.View.OnCreateContextMenuListener;
 import android.view.ViewGroup;
+import android.view.ViewGroup.LayoutParams;
 import android.view.ViewManager;
 import android.view.Window;
 import android.view.WindowManager;
-import android.view.ContextMenu.ContextMenuInfo;
-import android.view.View.OnCreateContextMenuListener;
-import android.view.ViewGroup.LayoutParams;
 import android.view.accessibility.AccessibilityEvent;
 import android.widget.AdapterView;
 import android.widget.FrameLayout;
-import android.widget.LinearLayout;
 
-import com.android.internal.app.ActionBarImpl;
-import com.android.internal.policy.PolicyManager;
-import com.android.internal.widget.ActionBarView;
+import java.util.ArrayList;
+import java.util.HashMap;
 
 /**
  * An activity is a single, focused thing that the user can do.  Almost all
@@ -4073,6 +4072,25 @@
         }
     }
 
+    /**
+     * Start a context mode.
+     *
+     * @param callback Callback that will manage lifecycle events for this context mode
+     * @return The ContextMode that was started, or null if it was canceled
+     *
+     * @see ActionMode
+     */
+    public ActionMode startContextMode(ActionMode.Callback callback) {
+        return mWindow.getDecorView().startActionMode(callback);
+    }
+
+    public ActionMode onStartActionMode(ActionMode.Callback callback) {
+        if (mActionBar != null) {
+            return mActionBar.startContextMode(callback);
+        }
+        return null;
+    }
+
     // ------------------ Internal API ------------------
     
     final void setParent(Activity parent) {
@@ -4286,19 +4304,4 @@
             }
         }
     }
-
-    @Override
-    public ContextualMode startContextualMode(ContextualMode.Callback callback) {
-        if (mActionBar == null) {
-            return null;
-        }
-        return mActionBar.startContextualMode(callback);
-    }
-
-    @Override
-    public void finishContextualMode() {
-        if (mActionBar != null) {
-            mActionBar.finishContextualMode();
-        }
-    }
 }
diff --git a/core/java/android/app/ApplicationErrorReport.java b/core/java/android/app/ApplicationErrorReport.java
index 48cbd46..ddc2b1f 100644
--- a/core/java/android/app/ApplicationErrorReport.java
+++ b/core/java/android/app/ApplicationErrorReport.java
@@ -79,6 +79,11 @@
     public static final int TYPE_STRICT_MODE_VIOLATION = 4;
 
     /**
+     * An error report about a StrictMode violation.
+     */
+    public static final int TYPE_RUNNING_SERVICE = 5;
+
+    /**
      * Type of this report. Can be one of {@link #TYPE_NONE},
      * {@link #TYPE_CRASH}, {@link #TYPE_ANR}, or {@link #TYPE_BATTERY}.
      */
@@ -130,6 +135,12 @@
     public BatteryInfo batteryInfo;
     
     /**
+     * If this report is of type {@link #TYPE_RUNNING_SERVICE}, contains an instance
+     * of RunningServiceInfo; otherwise null.
+     */
+    public RunningServiceInfo runningServiceInfo;
+
+    /**
      * Create an uninitialized instance of {@link ApplicationErrorReport}.
      */
     public ApplicationErrorReport() {
@@ -223,6 +234,9 @@
             case TYPE_BATTERY:
                 batteryInfo.writeToParcel(dest, flags);
                 break;
+            case TYPE_RUNNING_SERVICE:
+                runningServiceInfo.writeToParcel(dest, flags);
+                break;
         }
     }
 
@@ -239,16 +253,25 @@
                 crashInfo = new CrashInfo(in);
                 anrInfo = null;
                 batteryInfo = null;
+                runningServiceInfo = null;
                 break;
             case TYPE_ANR:
                 anrInfo = new AnrInfo(in);
                 crashInfo = null;
                 batteryInfo = null;
+                runningServiceInfo = null;
                 break;
             case TYPE_BATTERY:
                 batteryInfo = new BatteryInfo(in);
                 anrInfo = null;
                 crashInfo = null;
+                runningServiceInfo = null;
+                break;
+            case TYPE_RUNNING_SERVICE:
+                batteryInfo = null;
+                anrInfo = null;
+                crashInfo = null;
+                runningServiceInfo = new RunningServiceInfo(in);
                 break;
         }
     }
@@ -494,6 +517,51 @@
         }
     }
 
+    /**
+     * Describes a running service report.
+     */
+    public static class RunningServiceInfo {
+        /**
+         * Duration in milliseconds that the service has been running.
+         */
+        public long durationMillis;
+
+        /**
+         * Dump of debug information about the service.
+         */
+        public String serviceDetails;
+
+        /**
+         * Create an uninitialized instance of RunningServiceInfo.
+         */
+        public RunningServiceInfo() {
+        }
+
+        /**
+         * Create an instance of RunningServiceInfo initialized from a Parcel.
+         */
+        public RunningServiceInfo(Parcel in) {
+            durationMillis = in.readLong();
+            serviceDetails = in.readString();
+        }
+
+        /**
+         * Save a RunningServiceInfo instance to a parcel.
+         */
+        public void writeToParcel(Parcel dest, int flags) {
+            dest.writeLong(durationMillis);
+            dest.writeString(serviceDetails);
+        }
+
+        /**
+         * Dump a BatteryInfo instance to a Printer.
+         */
+        public void dump(Printer pw, String prefix) {
+            pw.println(prefix + "durationMillis: " + durationMillis);
+            pw.println(prefix + "serviceDetails: " + serviceDetails);
+        }
+    }
+
     public static final Parcelable.Creator<ApplicationErrorReport> CREATOR
             = new Parcelable.Creator<ApplicationErrorReport>() {
         public ApplicationErrorReport createFromParcel(Parcel source) {
diff --git a/core/java/android/app/ContextualMode.java b/core/java/android/app/ContextualMode.java
deleted file mode 100644
index 5e4b5df..0000000
--- a/core/java/android/app/ContextualMode.java
+++ /dev/null
@@ -1,155 +0,0 @@
-/*
- * 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 android.app;
-
-import android.content.Context;
-import android.view.Menu;
-import android.view.MenuItem;
-import android.view.View;
-
-/**
- * Represents a contextual mode of the user interface. Contextual modes can be used for
- * modal interactions with content and replace parts of the normal UI until finished.
- * Examples of good contextual modes include selection modes, search, content editing, etc.
- */
-public abstract class ContextualMode {
-    /**
-     * Set the title of the contextual mode. This method will have no visible effect if
-     * a custom view has been set.
-     *
-     * @param title Title string to set
-     *
-     * @see #setCustomView(View)
-     */
-    public abstract void setTitle(CharSequence title);
-
-    /**
-     * Set the subtitle of the contextual mode. This method will have no visible effect if
-     * a custom view has been set.
-     *
-     * @param subtitle Subtitle string to set
-     *
-     * @see #setCustomView(View)
-     */
-    public abstract void setSubtitle(CharSequence subtitle);
-
-    /**
-     * Set a custom view for this contextual mode. The custom view will take the place of
-     * the title and subtitle. Useful for things like search boxes.
-     *
-     * @param view Custom view to use in place of the title/subtitle.
-     *
-     * @see #setTitle(CharSequence)
-     * @see #setSubtitle(CharSequence)
-     */
-    public abstract void setCustomView(View view);
-
-    /**
-     * Invalidate the contextual mode and refresh menu content. The contextual mode's
-     * {@link ContextualMode.Callback} will have its
-     * {@link Callback#onPrepareContextualMode(ContextualMode, Menu)} method called.
-     * If it returns true the menu will be scanned for updated content and any relevant changes
-     * will be reflected to the user.
-     */
-    public abstract void invalidate();
-
-    /**
-     * Finish and close this context mode. The context mode's {@link ContextualMode.Callback} will
-     * have its {@link Callback#onDestroyContextualMode(ContextualMode)} method called.
-     */
-    public abstract void finish();
-
-    /**
-     * Returns the menu of actions that this contextual mode presents.
-     * @return The contextual mode's menu.
-     */
-    public abstract Menu getMenu();
-
-    /**
-     * Returns the current title of this contextual mode.
-     * @return Title text
-     */
-    public abstract CharSequence getTitle();
-
-    /**
-     * Returns the current subtitle of this contextual mode.
-     * @return Subtitle text
-     */
-    public abstract CharSequence getSubtitle();
-
-    /**
-     * Returns the current custom view for this contextual mode.
-     * @return The current custom view
-     */
-    public abstract View getCustomView();
-
-    /**
-     * Callback interface for contextual modes. Supplied to
-     * {@link Context#startContextualMode(Callback)}, a Callback
-     * configures and handles events raised by a user's interaction with a context mode.
-     *
-     * <p>A context mode's lifecycle is as follows:
-     * <ul>
-     * <li>{@link Callback#onCreateContextualMode(ContextualMode, Menu)} once on initial
-     * creation</li>
-     * <li>{@link Callback#onPrepareContextualMode(ContextualMode, Menu)} after creation
-     * and any time the {@link ContextualMode} is invalidated</li>
-     * <li>{@link Callback#onContextualItemClicked(ContextualMode, MenuItem)} any time a
-     * contextual action button is clicked</li>
-     * <li>{@link Callback#onDestroyContextualMode(ContextualMode)} when the context mode
-     * is closed</li>
-     * </ul>
-     */
-    public interface Callback {
-        /**
-         * Called when a contextual mode is first created. The menu supplied will be used to
-         * generate action buttons for the contextual mode.
-         *
-         * @param mode ContextualMode being created
-         * @param menu Menu used to populate contextual action buttons
-         * @return true if the contextual mode should be created, false if entering this
-         *              mode should be aborted.
-         */
-        public boolean onCreateContextualMode(ContextualMode mode, Menu menu);
-
-        /**
-         * Called to refresh a contextual mode's action menu whenever it is invalidated.
-         *
-         * @param mode ContextualMode being prepared
-         * @param menu Menu used to populate contextual action buttons
-         * @return true if the menu or contextual mode was updated, false otherwise.
-         */
-        public boolean onPrepareContextualMode(ContextualMode mode, Menu menu);
-
-        /**
-         * Called to report a user click on a contextual action button.
-         *
-         * @param mode The current ContextualMode
-         * @param item The item that was clicked
-         * @return true if this callback handled the event, false if the standard MenuItem
-         *          invocation should continue.
-         */
-        public boolean onContextualItemClicked(ContextualMode mode, MenuItem item);
-
-        /**
-         * Called when a contextual mode is about to be exited and destroyed.
-         *
-         * @param mode The current ContextualMode being destroyed
-         */
-        public void onDestroyContextualMode(ContextualMode mode);
-    }
-}
\ No newline at end of file
diff --git a/core/java/android/app/Dialog.java b/core/java/android/app/Dialog.java
index da8c9e5..a9420b4e 100644
--- a/core/java/android/app/Dialog.java
+++ b/core/java/android/app/Dialog.java
@@ -18,16 +18,18 @@
 
 import com.android.internal.policy.PolicyManager;
 
-import android.content.Context;
-import android.content.DialogInterface;
 import android.content.ComponentName;
+import android.content.Context;
 import android.content.ContextWrapper;
+import android.content.DialogInterface;
 import android.graphics.drawable.Drawable;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
 import android.view.ContextMenu;
+import android.view.ContextMenu.ContextMenuInfo;
+import android.view.ActionMode;
 import android.view.ContextThemeWrapper;
 import android.view.Gravity;
 import android.view.KeyEvent;
@@ -36,13 +38,12 @@
 import android.view.MenuItem;
 import android.view.MotionEvent;
 import android.view.View;
+import android.view.View.OnCreateContextMenuListener;
 import android.view.ViewConfiguration;
 import android.view.ViewGroup;
+import android.view.ViewGroup.LayoutParams;
 import android.view.Window;
 import android.view.WindowManager;
-import android.view.ContextMenu.ContextMenuInfo;
-import android.view.View.OnCreateContextMenuListener;
-import android.view.ViewGroup.LayoutParams;
 import android.view.accessibility.AccessibilityEvent;
 
 import java.lang.ref.WeakReference;
@@ -832,6 +833,11 @@
         }
     }
 
+    public ActionMode onStartActionMode(ActionMode.Callback callback) {
+        // TODO Support context modes in dialogs
+        return null;
+    }
+
     /**
      * @return The activity associated with this dialog, or null if there is no assocaited activity.
      */
diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java
index aceeed6..3d64984 100644
--- a/core/java/android/content/Context.java
+++ b/core/java/android/content/Context.java
@@ -16,7 +16,6 @@
 
 package android.content;
 
-import android.app.ContextualMode;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.content.res.AssetManager;
@@ -1983,25 +1982,4 @@
     public boolean isRestricted() {
         return false;
     }
-
-    /**
-     * Start a contextual mode controlled by <code>callback</code>.
-     * The {@link ContextualMode.Callback} will receive lifecycle events for the duration
-     * of the contextual mode. There can only be one contextual mode active at a time.
-     * Starting a new contextual mode while one is already active will finish the old
-     * contextual mode.
-     *
-     * @param callback Callback handler that will manage this context mode.
-     * @return The new contextual mode started by this call, or <code>null</code>
-     *         if the mode was not started.
-     */
-    public ContextualMode startContextualMode(ContextualMode.Callback callback) {
-        return null;
-    }
-
-    /**
-     * Finish the current contextual mode if present.
-     */
-    public void finishContextualMode() {
-    }
 }
diff --git a/core/java/android/content/pm/PackageParser.java b/core/java/android/content/pm/PackageParser.java
index 6098617..8e93855 100644
--- a/core/java/android/content/pm/PackageParser.java
+++ b/core/java/android/content/pm/PackageParser.java
@@ -2792,6 +2792,9 @@
         // For use by package manager to keep track of where it has done dexopt.
         public boolean mDidDexOpt;
         
+        // User set enabled state.
+        public int mSetEnabled = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
+
         // Additional data supplied by callers.
         public Object mExtras;
         
@@ -3018,6 +3021,12 @@
     }
 
     private static boolean copyNeeded(int flags, Package p, Bundle metaData) {
+        if (p.mSetEnabled != PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
+            boolean enabled = p.mSetEnabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
+            if (p.applicationInfo.enabled != enabled) {
+                return true;
+            }
+        }
         if ((flags & PackageManager.GET_META_DATA) != 0
                 && (metaData != null || p.mAppMetaData != null)) {
             return true;
@@ -3051,6 +3060,7 @@
         if (!sCompatibilityModeEnabled) {
             ai.disableCompatibilityMode();
         }
+        ai.enabled = p.mSetEnabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
         return ai;
     }
 
diff --git a/core/java/android/database/sqlite/SQLiteDatabase.java b/core/java/android/database/sqlite/SQLiteDatabase.java
index cf4d07ff..623821b 100644
--- a/core/java/android/database/sqlite/SQLiteDatabase.java
+++ b/core/java/android/database/sqlite/SQLiteDatabase.java
@@ -67,7 +67,7 @@
  * is the Unicode Collation Algorithm and not tailored to the current locale.
  */
 public class SQLiteDatabase extends SQLiteClosable {
-    private static final String TAG = "Database";
+    private static final String TAG = "SQLiteDatabase";
     private static final int EVENT_DB_OPERATION = 52000;
     private static final int EVENT_DB_CORRUPT = 75004;
 
@@ -1641,18 +1641,7 @@
             }
 
             // Run the program and then cleanup
-            statement.execute();
-
-            long insertedRowId = lastInsertRow();
-            if (insertedRowId == -1) {
-                Log.e(TAG, "Error inserting " + initialValues + " using " + sql);
-            } else {
-                if (Config.LOGD && Log.isLoggable(TAG, Log.VERBOSE)) {
-                    Log.v(TAG, "Inserting row " + insertedRowId + " from "
-                            + initialValues + " using " + sql);
-                }
-            }
-            return insertedRowId;
+            return statement.executeInsert();
         } catch (SQLiteDatabaseCorruptException e) {
             onCorruption();
             throw e;
@@ -1689,8 +1678,7 @@
                     DatabaseUtils.bindObjectToProgram(statement, i + 1, whereArgs[i]);
                 }
             }
-            statement.execute();
-            return lastChangeCount();
+            return statement.executeUpdateDelete();
         } catch (SQLiteDatabaseCorruptException e) {
             onCorruption();
             throw e;
@@ -1782,12 +1770,7 @@
             }
 
             // Run the program and then cleanup
-            statement.execute();
-            int numChangedRows = lastChangeCount();
-            if (Config.LOGD && Log.isLoggable(TAG, Log.VERBOSE)) {
-                Log.v(TAG, "Updated " + numChangedRows + " using " + values + " and " + sql);
-            }
-            return numChangedRows;
+            return statement.executeUpdateDelete();
         } catch (SQLiteDatabaseCorruptException e) {
             onCorruption();
             throw e;
@@ -1834,13 +1817,18 @@
         long timeStart = SystemClock.uptimeMillis();
         lock();
         logTimeStat(mLastSqlStatement, timeStart, GET_LOCK_LOG_PREFIX);
+        SQLiteStatement stmt = null;
         try {
             closePendingStatements();
-            native_execSQL(sql);
+            stmt = compileStatement(sql);
+            stmt.execute();
         } catch (SQLiteDatabaseCorruptException e) {
             onCorruption();
             throw e;
         } finally {
+            if (stmt != null) {
+                stmt.close();
+            }
             unlock();
         }
 
@@ -1914,7 +1902,7 @@
                     DatabaseUtils.bindObjectToProgram(statement, i + 1, bindArgs[i]);
                 }
             }
-            statement.execute();
+            statement.executeUpdateDelete();
         } catch (SQLiteDatabaseCorruptException e) {
             onCorruption();
             throw e;
@@ -2025,7 +2013,7 @@
         }
         if (durationMillis >= sQueryLogTimeInMillis) {
             samplePercent = 100;
-        } else {;
+        } else {
             samplePercent = (int) (100 * durationMillis / sQueryLogTimeInMillis) + 1;
             if (mRandom.nextInt(100) >= samplePercent) return;
         }
@@ -2628,15 +2616,6 @@
     private native void enableSqlProfiling(String path, short connectionNum);
 
     /**
-     * Native call to execute a raw SQL statement. {@link #lock} must be held
-     * when calling this method.
-     *
-     * @param sql The raw SQL string
-     * @throws SQLException
-     */
-    /* package */ native void native_execSQL(String sql) throws SQLException;
-
-    /**
      * Native call to set the locale.  {@link #lock} must be held when calling
      * this method.
      * @throws SQLException
@@ -2644,20 +2623,6 @@
     /* package */ native void native_setLocale(String loc, int flags);
 
     /**
-     * Returns the row ID of the last row inserted into the database.
-     *
-     * @return the row ID of the last row inserted into the database.
-     */
-    /* package */ native long lastInsertRow();
-
-    /**
-     * Returns the number of changes made in the last statement executed.
-     *
-     * @return the number of changes made in the last statement executed.
-     */
-    /* package */ native int lastChangeCount();
-
-    /**
      * return the SQLITE_DBSTATUS_LOOKASIDE_USED documented here
      * http://www.sqlite.org/c3ref/c_dbstatus_lookaside_used.html
      * @return int value of SQLITE_DBSTATUS_LOOKASIDE_USED
diff --git a/core/java/android/database/sqlite/SQLiteStatement.java b/core/java/android/database/sqlite/SQLiteStatement.java
index b902803..7a683e4 100644
--- a/core/java/android/database/sqlite/SQLiteStatement.java
+++ b/core/java/android/database/sqlite/SQLiteStatement.java
@@ -48,18 +48,31 @@
     }
 
     /**
-     * Execute this SQL statement, if it is not a query. For example,
-     * CREATE TABLE, DELTE, INSERT, etc.
+     * Execute this SQL statement, if it is not a SELECT / INSERT / DELETE / UPDATE, for example
+     * CREATE / DROP table, view, trigger, index etc.
      *
      * @throws android.database.SQLException If the SQL string is invalid for
      *         some reason
      */
     public void execute() {
+        executeUpdateDelete();
+    }
+
+    /**
+     * Execute this SQL statement, if the the number of rows affected by exection of this SQL
+     * statement is of any importance to the caller - for example, UPDATE / DELETE SQL statements.
+     *
+     * @return the number of rows affected by this SQL statement execution.
+     * @throws android.database.SQLException If the SQL string is invalid for
+     *         some reason
+     */
+    public int executeUpdateDelete() {
         synchronized(this) {
             long timeStart = acquireAndLock(WRITE);
             try {
-                native_execute();
+                int numChanges = native_execute();
                 mDatabase.logTimeStat(mSql, timeStart);
+                return numChanges;
             } finally {
                 releaseAndUnlock();
             }
@@ -79,9 +92,9 @@
         synchronized(this) {
             long timeStart = acquireAndLock(WRITE);
             try {
-                native_execute();
+                long lastInsertedRowId = native_executeInsert();
                 mDatabase.logTimeStat(mSql, timeStart);
-                return (mDatabase.lastChangeCount() > 0) ? mDatabase.lastInsertRow() : -1;
+                return lastInsertedRowId;
             } finally {
                 releaseAndUnlock();
             }
@@ -182,7 +195,8 @@
         nHandle = mDatabase.mNativeHandle;
     }
 
-    private final native void native_execute();
+    private final native int native_execute();
+    private final native long native_executeInsert();
     private final native long native_1x1_long();
     private final native String native_1x1_string();
 }
diff --git a/core/java/android/os/BatteryStats.java b/core/java/android/os/BatteryStats.java
index 0afd6cd..a699388 100644
--- a/core/java/android/os/BatteryStats.java
+++ b/core/java/android/os/BatteryStats.java
@@ -106,7 +106,7 @@
 
     // NOTE: Update this list if you add/change any stats above.
     // These characters are supposed to represent "total", "last", "current", 
-    // and "unplugged". They were shortened for effeciency sake.
+    // and "unplugged". They were shortened for efficiency sake.
     private static final String[] STAT_NAMES = { "t", "l", "c", "u" };
     
     /**
@@ -395,19 +395,23 @@
         public char batteryTemperature;
         public char batteryVoltage;
         
+        // Constants from SCREEN_BRIGHTNESS_*
         public static final int STATE_BRIGHTNESS_MASK = 0x000000f;
         public static final int STATE_BRIGHTNESS_SHIFT = 0;
+        // Constants from SIGNAL_STRENGTH_*
         public static final int STATE_SIGNAL_STRENGTH_MASK = 0x00000f0;
         public static final int STATE_SIGNAL_STRENGTH_SHIFT = 4;
+        // Constants from ServiceState.STATE_*
         public static final int STATE_PHONE_STATE_MASK = 0x0000f00;
         public static final int STATE_PHONE_STATE_SHIFT = 8;
+        // Constants from DATA_CONNECTION_*
         public static final int STATE_DATA_CONNECTION_MASK = 0x000f000;
         public static final int STATE_DATA_CONNECTION_SHIFT = 12;
         
         public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<30;
         public static final int STATE_SCREEN_ON_FLAG = 1<<29;
         public static final int STATE_GPS_ON_FLAG = 1<<28;
-        public static final int STATE_PHONE_ON_FLAG = 1<<27;
+        public static final int STATE_PHONE_IN_CALL_FLAG = 1<<27;
         public static final int STATE_PHONE_SCANNING_FLAG = 1<<26;
         public static final int STATE_WIFI_ON_FLAG = 1<<25;
         public static final int STATE_WIFI_RUNNING_FLAG = 1<<24;
@@ -619,7 +623,7 @@
         new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged"),
         new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen"),
         new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps"),
-        new BitDescription(HistoryItem.STATE_PHONE_ON_FLAG, "phone"),
+        new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call"),
         new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning"),
         new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi"),
         new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running"),
@@ -717,6 +721,18 @@
     public abstract int getDischargeCurrentLevel();
 
     /**
+     * Get the amount the battery has discharged since the stats were
+     * last reset after charging, as a lower-end approximation.
+     */
+    public abstract int getLowDischargeAmountSinceCharge();
+
+    /**
+     * Get the amount the battery has discharged since the stats were
+     * last reset after charging, as an upper-end approximation.
+     */
+    public abstract int getHighDischargeAmountSinceCharge();
+
+    /**
      * Returns the total, last, or current battery uptime in microseconds.
      *
      * @param curTime the elapsed realtime in microseconds.
diff --git a/core/java/android/os/Binder.java b/core/java/android/os/Binder.java
index 59b8274..f8260ca 100644
--- a/core/java/android/os/Binder.java
+++ b/core/java/android/os/Binder.java
@@ -226,9 +226,16 @@
                     try {
                         fd.close();
                     } catch (IOException e) {
+                        // swallowed, not propagated back to the caller
                     }
                 }
             }
+            // Write the StrictMode header.
+            if (reply != null) {
+                reply.writeNoException();
+            } else {
+                StrictMode.clearGatheredViolations();
+            }
             return true;
         }
         return false;
@@ -341,12 +348,15 @@
 
     public void dump(FileDescriptor fd, String[] args) throws RemoteException {
         Parcel data = Parcel.obtain();
+        Parcel reply = Parcel.obtain();
         data.writeFileDescriptor(fd);
         data.writeStringArray(args);
         try {
-            transact(DUMP_TRANSACTION, data, null, 0);
+            transact(DUMP_TRANSACTION, data, reply, 0);
+            reply.readException();
         } finally {
             data.recycle();
+            reply.recycle();
         }
     }
     
diff --git a/core/java/android/provider/Downloads.java b/core/java/android/provider/Downloads.java
index 7e1d685..1b37107 100644
--- a/core/java/android/provider/Downloads.java
+++ b/core/java/android/provider/Downloads.java
@@ -624,6 +624,13 @@
                 "android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS";
 
         /**
+         * The permission to downloads files to the cache partition that won't be automatically
+         * purged when space is needed.
+         */
+        public static final String PERMISSION_CACHE_NON_PURGEABLE =
+                "android.permission.DOWNLOAD_CACHE_NON_PURGEABLE";
+
+        /**
          * The content:// URI for the data table in the provider
          */
         public static final Uri CONTENT_URI =
diff --git a/core/java/android/speech/tts/TextToSpeech.java b/core/java/android/speech/tts/TextToSpeech.java
index 26c167e..841257f 100755
--- a/core/java/android/speech/tts/TextToSpeech.java
+++ b/core/java/android/speech/tts/TextToSpeech.java
@@ -1065,6 +1065,9 @@
             if (!mStarted) {
                 return result;
             }
+            if (loc == null) {
+                return result;
+            }
             try {
                 String language = loc.getISO3Language();
                 String country = loc.getISO3Country();
diff --git a/core/java/android/view/ActionMode.java b/core/java/android/view/ActionMode.java
new file mode 100644
index 0000000..d4f4b93
--- /dev/null
+++ b/core/java/android/view/ActionMode.java
@@ -0,0 +1,151 @@
+/*
+ * 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 android.view;
+
+
+/**
+ * Represents a contextual mode of the user interface. Action modes can be used for
+ * modal interactions with content and replace parts of the normal UI until finished.
+ * Examples of good action modes include selection modes, search, content editing, etc.
+ */
+public abstract class ActionMode {
+    /**
+     * Set the title of the action mode. This method will have no visible effect if
+     * a custom view has been set.
+     *
+     * @param title Title string to set
+     *
+     * @see #setCustomView(View)
+     */
+    public abstract void setTitle(CharSequence title);
+
+    /**
+     * Set the subtitle of the action mode. This method will have no visible effect if
+     * a custom view has been set.
+     *
+     * @param subtitle Subtitle string to set
+     *
+     * @see #setCustomView(View)
+     */
+    public abstract void setSubtitle(CharSequence subtitle);
+
+    /**
+     * Set a custom view for this action mode. The custom view will take the place of
+     * the title and subtitle. Useful for things like search boxes.
+     *
+     * @param view Custom view to use in place of the title/subtitle.
+     *
+     * @see #setTitle(CharSequence)
+     * @see #setSubtitle(CharSequence)
+     */
+    public abstract void setCustomView(View view);
+
+    /**
+     * Invalidate the action mode and refresh menu content. The mode's
+     * {@link ActionMode.Callback} will have its
+     * {@link Callback#onPrepareActionMode(ActionMode, Menu)} method called.
+     * If it returns true the menu will be scanned for updated content and any relevant changes
+     * will be reflected to the user.
+     */
+    public abstract void invalidate();
+
+    /**
+     * Finish and close this action mode. The action mode's {@link ActionMode.Callback} will
+     * have its {@link Callback#onDestroyActionMode(ActionMode)} method called.
+     */
+    public abstract void finish();
+
+    /**
+     * Returns the menu of actions that this action mode presents.
+     * @return The action mode's menu.
+     */
+    public abstract Menu getMenu();
+
+    /**
+     * Returns the current title of this action mode.
+     * @return Title text
+     */
+    public abstract CharSequence getTitle();
+
+    /**
+     * Returns the current subtitle of this action mode.
+     * @return Subtitle text
+     */
+    public abstract CharSequence getSubtitle();
+
+    /**
+     * Returns the current custom view for this action mode.
+     * @return The current custom view
+     */
+    public abstract View getCustomView();
+
+    /**
+     * Callback interface for action modes. Supplied to
+     * {@link View#startActionMode(Callback)}, a Callback
+     * configures and handles events raised by a user's interaction with an action mode.
+     *
+     * <p>An action mode's lifecycle is as follows:
+     * <ul>
+     * <li>{@link Callback#onCreateActionMode(ActionMode, Menu)} once on initial
+     * creation</li>
+     * <li>{@link Callback#onPrepareActionMode(ActionMode, Menu)} after creation
+     * and any time the {@link ActionMode} is invalidated</li>
+     * <li>{@link Callback#onActionItemClicked(ActionMode, MenuItem)} any time a
+     * contextual action button is clicked</li>
+     * <li>{@link Callback#onDestroyActionMode(ActionMode)} when the action mode
+     * is closed</li>
+     * </ul>
+     */
+    public interface Callback {
+        /**
+         * Called when action mode is first created. The menu supplied will be used to
+         * generate action buttons for the action mode.
+         *
+         * @param mode ActionMode being created
+         * @param menu Menu used to populate action buttons
+         * @return true if the action mode should be created, false if entering this
+         *              mode should be aborted.
+         */
+        public boolean onCreateActionMode(ActionMode mode, Menu menu);
+
+        /**
+         * Called to refresh an action mode's action menu whenever it is invalidated.
+         *
+         * @param mode ActionMode being prepared
+         * @param menu Menu used to populate action buttons
+         * @return true if the menu or action mode was updated, false otherwise.
+         */
+        public boolean onPrepareActionMode(ActionMode mode, Menu menu);
+
+        /**
+         * Called to report a user click on an action button.
+         *
+         * @param mode The current ActionMode
+         * @param item The item that was clicked
+         * @return true if this callback handled the event, false if the standard MenuItem
+         *          invocation should continue.
+         */
+        public boolean onActionItemClicked(ActionMode mode, MenuItem item);
+
+        /**
+         * Called when an action mode is about to be exited and destroyed.
+         *
+         * @param mode The current ActionMode being destroyed
+         */
+        public void onDestroyActionMode(ActionMode mode);
+    }
+}
\ No newline at end of file
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index b1025c9..363aced 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -16,7 +16,6 @@
 
 package android.view;
 
-import android.graphics.RectF;
 import com.android.internal.R;
 import com.android.internal.view.menu.MenuBuilder;
 
@@ -35,6 +34,7 @@
 import android.graphics.PorterDuff;
 import android.graphics.PorterDuffXfermode;
 import android.graphics.Rect;
+import android.graphics.RectF;
 import android.graphics.Region;
 import android.graphics.Shader;
 import android.graphics.drawable.ColorDrawable;
@@ -57,6 +57,7 @@
 import android.util.Pools;
 import android.util.SparseArray;
 import android.view.ContextMenu.ContextMenuInfo;
+import android.view.View.MeasureSpec;
 import android.view.accessibility.AccessibilityEvent;
 import android.view.accessibility.AccessibilityEventSource;
 import android.view.accessibility.AccessibilityManager;
@@ -2541,6 +2542,18 @@
     }
 
     /**
+     * Start an action mode.
+     *
+     * @param callback Callback that will control the lifecycle of the action mode
+     * @return The new action mode if it is started, null otherwise
+     *
+     * @see ActionMode
+     */
+    public ActionMode startActionMode(ActionMode.Callback callback) {
+        return getParent().startActionModeForChild(this, callback);
+    }
+
+    /**
      * Register a callback to be invoked when a key is pressed in this view.
      * @param l the key listener to attach to this view
      */
diff --git a/core/java/android/view/ViewGroup.java b/core/java/android/view/ViewGroup.java
index 3c161ca..a713efc 100644
--- a/core/java/android/view/ViewGroup.java
+++ b/core/java/android/view/ViewGroup.java
@@ -16,7 +16,6 @@
 
 package android.view;
 
-import android.graphics.Matrix;
 import com.android.internal.R;
 
 import android.content.Context;
@@ -24,6 +23,7 @@
 import android.content.res.TypedArray;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
+import android.graphics.Matrix;
 import android.graphics.Paint;
 import android.graphics.Rect;
 import android.graphics.RectF;
@@ -35,6 +35,7 @@
 import android.util.EventLog;
 import android.util.Log;
 import android.util.SparseArray;
+import android.view.ViewGroup.LayoutParams;
 import android.view.accessibility.AccessibilityEvent;
 import android.view.animation.Animation;
 import android.view.animation.AnimationUtils;
@@ -474,6 +475,13 @@
     }
 
     /**
+     * {@inheritDoc}
+     */
+    public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
+        return mParent != null ? mParent.startActionModeForChild(originalView, callback) : null;
+    }
+
+    /**
      * Find the nearest view in the specified direction that wants to take
      * focus.
      *
diff --git a/core/java/android/view/ViewParent.java b/core/java/android/view/ViewParent.java
index b456c5d..d7d4c3f 100644
--- a/core/java/android/view/ViewParent.java
+++ b/core/java/android/view/ViewParent.java
@@ -162,6 +162,20 @@
     public void createContextMenu(ContextMenu menu);
 
     /**
+     * Start an action mode for the specified view.
+     * <p>
+     * In most cases, a subclass does not need to override this. However, if the
+     * subclass is added directly to the window manager (for example,
+     * {@link ViewManager#addView(View, android.view.ViewGroup.LayoutParams)})
+     * then it should override this and start the action mode.
+     *
+     * @param originalView The source view where the action mode was first invoked
+     * @param callback The callback that will handle lifecycle events for the action mode
+     * @return The new action mode if it was started, null otherwise
+     */
+    public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback);
+
+    /**
      * This method is called on the parent when a child's drawable state
      * has changed.
      *
diff --git a/core/java/android/view/ViewRoot.java b/core/java/android/view/ViewRoot.java
index 03cbb8a..877a302 100644
--- a/core/java/android/view/ViewRoot.java
+++ b/core/java/android/view/ViewRoot.java
@@ -2717,6 +2717,10 @@
         return false;
     }
 
+    public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
+        return null;
+    }
+
     public void createContextMenu(ContextMenu menu) {
     }
 
diff --git a/core/java/android/view/Window.java b/core/java/android/view/Window.java
index be681cc..ed02456 100644
--- a/core/java/android/view/Window.java
+++ b/core/java/android/view/Window.java
@@ -299,6 +299,14 @@
          * @see android.app.Activity#onSearchRequested() 
          */
         public boolean onSearchRequested();
+
+        /**
+         * Called when an action mode is being started.
+         *
+         * @param callback Callback to control the lifecycle of this action mode
+         * @return The ActionMode that was started, or null if it was canceled
+         */
+        public ActionMode onStartActionMode(ActionMode.Callback callback);
     }
 
     public Window(Context context) {
diff --git a/core/java/com/android/internal/app/ActionBarImpl.java b/core/java/com/android/internal/app/ActionBarImpl.java
index 050f630..6515f25 100644
--- a/core/java/com/android/internal/app/ActionBarImpl.java
+++ b/core/java/com/android/internal/app/ActionBarImpl.java
@@ -24,11 +24,11 @@
 
 import android.app.ActionBar;
 import android.app.Activity;
-import android.app.ContextualMode;
 import android.app.Fragment;
 import android.app.FragmentTransaction;
 import android.graphics.drawable.Drawable;
 import android.os.Handler;
+import android.view.ActionMode;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
@@ -66,7 +66,7 @@
     private TabImpl mSelectedTab;
     private int mTabSwitchMode = TAB_SWITCH_ADD_REMOVE;
     
-    private ContextMode mContextMode;
+    private ActionMode mContextMode;
     
     private static final int CONTEXT_DISPLAY_NORMAL = 0;
     private static final int CONTEXT_DISPLAY_SPLIT = 1;
@@ -229,8 +229,10 @@
         return mActionView.getDisplayOptions();
     }
 
-    public ContextualMode startContextualMode(ContextualMode.Callback callback) {
-        finishContextualMode();
+    public ActionMode startContextMode(ActionMode.Callback callback) {
+        if (mContextMode != null) {
+            mContextMode.finish();
+        }
 
         // Don't wait for the close context mode animation to finish.
         if (mClosingContext) {
@@ -239,8 +241,8 @@
             mCloseContext.run();
         }
 
-        ContextMode mode = new ContextMode(callback);
-        if (callback.onCreateContextualMode(mode, mode.getMenu())) {
+        ActionMode mode = new ContextModeImpl(callback);
+        if (callback.onCreateActionMode(mode, mode.getMenu())) {
             mode.invalidate();
             mUpperContextView.initForMode(mode);
             mAnimatorView.setDisplayedChild(CONTEXT_VIEW);
@@ -254,12 +256,6 @@
         return null;
     }
 
-    public void finishContextualMode() {
-        if (mContextMode != null) {
-            mContextMode.finish();
-        }
-    }
-
     private void configureTab(Tab tab, int position) {
         final TabImpl tabi = (TabImpl) tab;
         final boolean isFirstTab = mTabs.isEmpty();
@@ -365,12 +361,12 @@
     /**
      * @hide 
      */
-    public class ContextMode extends ContextualMode implements MenuBuilder.Callback {
-        private ContextualMode.Callback mCallback;
+    public class ContextModeImpl extends ActionMode implements MenuBuilder.Callback {
+        private ActionMode.Callback mCallback;
         private MenuBuilder mMenu;
         private WeakReference<View> mCustomView;
         
-        public ContextMode(ContextualMode.Callback callback) {
+        public ContextModeImpl(ActionMode.Callback callback) {
             mCallback = callback;
             mMenu = new MenuBuilder(mActionView.getContext());
             mMenu.setCallback(this);
@@ -388,7 +384,7 @@
                 return;
             }
 
-            mCallback.onDestroyContextualMode(this);
+            mCallback.onDestroyActionMode(this);
             mAnimatorView.setDisplayedChild(NORMAL_VIEW);
 
             // Clear out the context mode views after the animation finishes
@@ -404,7 +400,7 @@
 
         @Override
         public void invalidate() {
-            if (mCallback.onPrepareContextualMode(this, mMenu)) {
+            if (mCallback.onPrepareActionMode(this, mMenu)) {
                 // Refresh content in both context views
             }
         }
@@ -441,7 +437,7 @@
         }
 
         public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
-            return mCallback.onContextualItemClicked(this, item);
+            return mCallback.onActionItemClicked(this, item);
         }
 
         public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
diff --git a/core/java/com/android/internal/app/IBatteryStats.aidl b/core/java/com/android/internal/app/IBatteryStats.aidl
index 88ef395..d040d3f 100644
--- a/core/java/com/android/internal/app/IBatteryStats.aidl
+++ b/core/java/com/android/internal/app/IBatteryStats.aidl
@@ -24,8 +24,15 @@
     byte[] getStatistics();
     void noteStartWakelock(int uid, String name, int type);
     void noteStopWakelock(int uid, String name, int type);
+    
+    /* DO NOT CHANGE the position of noteStartSensor without updating
+       SensorService.cpp */
     void noteStartSensor(int uid, int sensor);
+
+    /* DO NOT CHANGE the position of noteStopSensor without updating
+       SensorService.cpp */
     void noteStopSensor(int uid, int sensor);
+
     void noteStartGps(int uid);
     void noteStopGps(int uid);
     void noteScreenOn();
diff --git a/core/java/com/android/internal/app/ShutdownThread.java b/core/java/com/android/internal/app/ShutdownThread.java
index a96253b..d1aff2a 100644
--- a/core/java/com/android/internal/app/ShutdownThread.java
+++ b/core/java/com/android/internal/app/ShutdownThread.java
@@ -84,7 +84,7 @@
     public static void shutdown(final Context context, boolean confirm) {
         // ensure that only one thread is trying to power down.
         // any additional calls are just returned
-        synchronized (sIsStartedGuard){
+        synchronized (sIsStartedGuard) {
             if (sIsStarted) {
                 Log.d(TAG, "Request to shutdown already running, returning.");
                 return;
@@ -133,6 +133,10 @@
 
     private static void beginShutdownSequence(Context context) {
         synchronized (sIsStartedGuard) {
+            if (sIsStarted) {
+                Log.d(TAG, "Request to shutdown already running, returning.");
+                return;
+            }
             sIsStarted = true;
         }
 
diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java
index 642c313..b3323e9 100644
--- a/core/java/com/android/internal/os/BatteryStatsImpl.java
+++ b/core/java/com/android/internal/os/BatteryStatsImpl.java
@@ -63,7 +63,7 @@
     private static final int MAGIC = 0xBA757475; // 'BATSTATS' 
 
     // Current on-disk Parcel version
-    private static final int VERSION = 48;
+    private static final int VERSION = 49;
 
     // Maximum number of items we will record in the history.
     private static final int MAX_HISTORY_ITEMS = 1000;
@@ -184,6 +184,8 @@
     int mDischargeStartLevel;
     int mDischargeUnplugLevel;
     int mDischargeCurrentLevel;
+    int mLowDischargeAmountSinceCharge;
+    int mHighDischargeAmountSinceCharge;
 
     long mLastWriteTime = 0; // Milliseconds
 
@@ -260,7 +262,7 @@
             mPluggedCount = in.readInt();
             mCount.set(mPluggedCount);
             mLoadedCount = in.readInt();
-            mLastCount = in.readInt();
+            mLastCount = 0;
             mUnpluggedCount = in.readInt();
             unpluggables.add(this);
         }
@@ -273,7 +275,6 @@
         public void writeToParcel(Parcel out) {
             out.writeInt(mCount.get());
             out.writeInt(mLoadedCount);
-            out.writeInt(mLastCount);
             out.writeInt(mUnpluggedCount);
         }
 
@@ -348,13 +349,12 @@
         void writeSummaryFromParcelLocked(Parcel out) {
             int count = mCount.get();
             out.writeInt(count);
-            out.writeInt(count - mLoadedCount);
         }
 
         void readSummaryFromParcelLocked(Parcel in) {
             mLoadedCount = in.readInt();
             mCount.set(mLoadedCount);
-            mLastCount = in.readInt();
+            mLastCount = 0;
             mUnpluggedCount = mPluggedCount = mLoadedCount;
         }
     }
@@ -428,11 +428,11 @@
             
             mCount = in.readInt();
             mLoadedCount = in.readInt();
-            mLastCount = in.readInt();
+            mLastCount = 0;
             mUnpluggedCount = in.readInt();
             mTotalTime = in.readLong();
             mLoadedTime = in.readLong();
-            mLastTime = in.readLong();
+            mLastTime = 0;
             mUnpluggedTime = in.readLong();
             unpluggables.add(this);
         }
@@ -467,11 +467,9 @@
         public void writeToParcel(Parcel out, long batteryRealtime) {
             out.writeInt(mCount);
             out.writeInt(mLoadedCount);
-            out.writeInt(mLastCount);
             out.writeInt(mUnpluggedCount);
             out.writeLong(computeRunTimeLocked(batteryRealtime));
             out.writeLong(mLoadedTime);
-            out.writeLong(mLastTime);
             out.writeLong(mUnpluggedTime);
         }
 
@@ -569,18 +567,16 @@
             long runTime = computeRunTimeLocked(batteryRealtime);
             // Divide by 1000 for backwards compatibility
             out.writeLong((runTime + 500) / 1000);
-            out.writeLong(((runTime - mLoadedTime) + 500) / 1000);
             out.writeInt(mCount);
-            out.writeInt(mCount - mLoadedCount);
         }
 
         void readSummaryFromParcelLocked(Parcel in) {
             // Multiply by 1000 for backwards compatibility
             mTotalTime = mLoadedTime = in.readLong() * 1000;
-            mLastTime = in.readLong() * 1000;
+            mLastTime = 0;
             mUnpluggedTime = mTotalTime;
             mCount = mLoadedCount = in.readInt();
-            mLastCount = in.readInt();
+            mLastCount = 0;
             mUnpluggedCount = mCount;
         }
     }
@@ -1243,7 +1239,6 @@
             if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
                     + Integer.toHexString(mHistoryCur.states));
             addHistoryRecordLocked(SystemClock.elapsedRealtime());
-            mGpsNesting++;
             mScreenOn = true;
             mScreenOnTimer.startRunningLocked(this);
             if (mScreenBrightnessBin >= 0) {
@@ -1297,7 +1292,7 @@
     
     public void notePhoneOnLocked() {
         if (!mPhoneOn) {
-            mHistoryCur.states |= HistoryItem.STATE_PHONE_ON_FLAG;
+            mHistoryCur.states |= HistoryItem.STATE_PHONE_IN_CALL_FLAG;
             if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
                     + Integer.toHexString(mHistoryCur.states));
             addHistoryRecordLocked(SystemClock.elapsedRealtime());
@@ -1308,7 +1303,7 @@
     
     public void notePhoneOffLocked() {
         if (mPhoneOn) {
-            mHistoryCur.states &= ~HistoryItem.STATE_PHONE_ON_FLAG;
+            mHistoryCur.states &= ~HistoryItem.STATE_PHONE_IN_CALL_FLAG;
             if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
                     + Integer.toHexString(mHistoryCur.states));
             addHistoryRecordLocked(SystemClock.elapsedRealtime());
@@ -1317,38 +1312,43 @@
         }
     }
 
+    void stopAllSignalStrengthTimersLocked(int except) {
+        for (int i = 0; i < NUM_SIGNAL_STRENGTH_BINS; i++) {
+            if (i == except) {
+                continue;
+            }
+            while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
+                mPhoneSignalStrengthsTimer[i].stopRunningLocked(this);
+            }
+        }
+    }
+
     /**
      * Telephony stack updates the phone state.
      * @param state phone state from ServiceState.getState()
      */
     public void notePhoneStateLocked(int state) {
+        boolean scanning = false;
+
         int bin = mPhoneSignalStrengthBin;
-        boolean isAirplaneMode = state == ServiceState.STATE_POWER_OFF;
-        // Stop all timers
-        if (isAirplaneMode || state == ServiceState.STATE_OUT_OF_SERVICE) {
-            for (int i = 0; i < NUM_SIGNAL_STRENGTH_BINS; i++) {
-                while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
-                    mPhoneSignalStrengthsTimer[i].stopRunningLocked(this);
-                }
-            }
-        }
-        // Stop Signal Scanning timer, in case we're going into service
-        if (mPhoneSignalScanningTimer.isRunningLocked()) {
-            mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
-            if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
-                    + Integer.toHexString(mHistoryCur.states));
-            addHistoryRecordLocked(SystemClock.elapsedRealtime());
-            mPhoneSignalScanningTimer.stopRunningLocked(this);
-        }
+
+        // If the phone is powered off, stop all timers.
+        if (state == ServiceState.STATE_POWER_OFF) {
+            stopAllSignalStrengthTimersLocked(-1);
 
         // If we're back in service or continuing in service, restart the old timer.
-        if (state == ServiceState.STATE_IN_SERVICE) {
+        } if (state == ServiceState.STATE_IN_SERVICE) {
             if (bin == -1) bin = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
             if (!mPhoneSignalStrengthsTimer[bin].isRunningLocked()) {
                 mPhoneSignalStrengthsTimer[bin].startRunningLocked(this);
             }
+
+        // If we're out of service, we are in the lowest signal strength
+        // bin and have the scanning bit set.
         } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
+            scanning = true;
             mPhoneSignalStrengthBin = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
+            stopAllSignalStrengthTimersLocked(mPhoneSignalStrengthBin);
             if (!mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].isRunningLocked()) {
                 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].startRunningLocked(this);
             }
@@ -1361,6 +1361,17 @@
             }
         }
         
+        if (!scanning) {
+            // If we are no longer scanning, then stop the scanning timer.
+            if (mPhoneSignalScanningTimer.isRunningLocked()) {
+                mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
+                if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
+                        + Integer.toHexString(mHistoryCur.states));
+                addHistoryRecordLocked(SystemClock.elapsedRealtime());
+                mPhoneSignalScanningTimer.stopRunningLocked(this);
+            }
+        }
+
         if (mPhoneServiceState != state) {
             mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
                     | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
@@ -2622,10 +2633,6 @@
                 out.writeLong(mLoadedSystemTime);
                 out.writeLong(mLoadedForegroundTime);
                 out.writeInt(mLoadedStarts);
-                out.writeLong(mLastUserTime);
-                out.writeLong(mLastSystemTime);
-                out.writeLong(mLastForegroundTime);
-                out.writeInt(mLastStarts);
                 out.writeLong(mUnpluggedUserTime);
                 out.writeLong(mUnpluggedSystemTime);
                 out.writeLong(mUnpluggedForegroundTime);
@@ -2652,10 +2659,10 @@
                 mLoadedSystemTime = in.readLong();
                 mLoadedForegroundTime = in.readLong();
                 mLoadedStarts = in.readInt();
-                mLastUserTime = in.readLong();
-                mLastSystemTime = in.readLong();
-                mLastForegroundTime = in.readLong();
-                mLastStarts = in.readInt();
+                mLastUserTime = 0;
+                mLastSystemTime = 0;
+                mLastForegroundTime = 0;
+                mLastStarts = 0;
                 mUnpluggedUserTime = in.readLong();
                 mUnpluggedSystemTime = in.readLong();
                 mUnpluggedForegroundTime = in.readLong();
@@ -2828,7 +2835,7 @@
             void readFromParcelLocked(Parcel in) {
                 mWakeups = in.readInt();
                 mLoadedWakeups = in.readInt();
-                mLastWakeups = in.readInt();
+                mLastWakeups = 0;
                 mUnpluggedWakeups = in.readInt();
 
                 int numServs = in.readInt();
@@ -2845,7 +2852,6 @@
             void writeToParcelLocked(Parcel out) {
                 out.writeInt(mWakeups);
                 out.writeInt(mLoadedWakeups);
-                out.writeInt(mLastWakeups);
                 out.writeInt(mUnpluggedWakeups);
 
                 out.writeInt(mServiceStats.size());
@@ -3002,9 +3008,9 @@
                     mLoadedStartTime = in.readLong();
                     mLoadedStarts = in.readInt();
                     mLoadedLaunches = in.readInt();
-                    mLastStartTime = in.readLong();
-                    mLastStarts = in.readInt();
-                    mLastLaunches = in.readInt();
+                    mLastStartTime = 0;
+                    mLastStarts = 0;
+                    mLastLaunches = 0;
                     mUnpluggedStartTime = in.readLong();
                     mUnpluggedStarts = in.readInt();
                     mUnpluggedLaunches = in.readInt();
@@ -3022,9 +3028,6 @@
                     out.writeLong(mLoadedStartTime);
                     out.writeInt(mLoadedStarts);
                     out.writeInt(mLoadedLaunches);
-                    out.writeLong(mLastStartTime);
-                    out.writeInt(mLastStarts);
-                    out.writeInt(mLastLaunches);
                     out.writeLong(mUnpluggedStartTime);
                     out.writeInt(mUnpluggedStarts);
                     out.writeInt(mUnpluggedLaunches);
@@ -3336,6 +3339,8 @@
         mDischargeStartLevel = 0;
         mDischargeUnplugLevel = 0;
         mDischargeCurrentLevel = 0;
+        mLowDischargeAmountSinceCharge = 0;
+        mHighDischargeAmountSinceCharge = 0;
     }
 
     public BatteryStatsImpl(Parcel p) {
@@ -3434,6 +3439,8 @@
                     doWrite = true;
                     resetAllStatsLocked();
                     mDischargeStartLevel = level;
+                    mLowDischargeAmountSinceCharge = 0;
+                    mHighDischargeAmountSinceCharge = 0;
                 }
                 updateKernelWakelocksLocked();
                 mHistoryCur.batteryLevel = (byte)level;
@@ -3457,6 +3464,10 @@
                 mTrackBatteryPastUptime += uptime - mTrackBatteryUptimeStart;
                 mTrackBatteryPastRealtime += realtime - mTrackBatteryRealtimeStart;
                 mDischargeCurrentLevel = level;
+                if (level < mDischargeUnplugLevel) {
+                    mLowDischargeAmountSinceCharge = mDischargeUnplugLevel-level-1;
+                    mHighDischargeAmountSinceCharge = mDischargeUnplugLevel-level;
+                }
                 doPlugLocked(getBatteryUptimeLocked(uptime), getBatteryRealtimeLocked(realtime));
             }
             if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
@@ -3723,6 +3734,20 @@
     }
 
     @Override
+    public int getLowDischargeAmountSinceCharge() {
+        synchronized(this) {
+            return mLowDischargeAmountSinceCharge;
+        }
+    }
+
+    @Override
+    public int getHighDischargeAmountSinceCharge() {
+        synchronized(this) {
+            return mHighDischargeAmountSinceCharge;
+        }
+    }
+
+    @Override
     public int getCpuSpeedSteps() {
         return sNumSpeedSteps;
     }
@@ -3926,15 +3951,13 @@
         
         mStartCount = in.readInt();
         mBatteryUptime = in.readLong();
-        mBatteryLastUptime = in.readLong();
         mBatteryRealtime = in.readLong();
-        mBatteryLastRealtime = in.readLong();
         mUptime = in.readLong();
-        mLastUptime = in.readLong();
         mRealtime = in.readLong();
-        mLastRealtime = in.readLong();
         mDischargeUnplugLevel = in.readInt();
         mDischargeCurrentLevel = in.readInt();
+        mLowDischargeAmountSinceCharge = in.readInt();
+        mHighDischargeAmountSinceCharge = in.readInt();
         
         mStartCount++;
         
@@ -4058,11 +4081,8 @@
                 String procName = in.readString();
                 Uid.Proc p = u.getProcessStatsLocked(procName);
                 p.mUserTime = p.mLoadedUserTime = in.readLong();
-                p.mLastUserTime = in.readLong();
                 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
-                p.mLastSystemTime = in.readLong();
                 p.mStarts = p.mLoadedStarts = in.readInt();
-                p.mLastStarts = in.readInt();
             }
 
             NP = in.readInt();
@@ -4074,17 +4094,13 @@
                 String pkgName = in.readString();
                 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
                 p.mWakeups = p.mLoadedWakeups = in.readInt();
-                p.mLastWakeups = in.readInt();
                 final int NS = in.readInt();
                 for (int is = 0; is < NS; is++) {
                     String servName = in.readString();
                     Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
                     s.mStartTime = s.mLoadedStartTime = in.readLong();
-                    s.mLastStartTime = in.readLong();
                     s.mStarts = s.mLoadedStarts = in.readInt();
-                    s.mLastStarts = in.readInt();
                     s.mLaunches = s.mLoadedLaunches = in.readInt();
-                    s.mLastLaunches = in.readInt();
                 }
             }
 
@@ -4111,16 +4127,13 @@
         
         out.writeInt(mStartCount);
         out.writeLong(computeBatteryUptime(NOW_SYS, STATS_SINCE_CHARGED));
-        out.writeLong(computeBatteryUptime(NOW_SYS, STATS_CURRENT));
         out.writeLong(computeBatteryRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
-        out.writeLong(computeBatteryRealtime(NOWREAL_SYS, STATS_CURRENT));
         out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
-        out.writeLong(computeUptime(NOW_SYS, STATS_CURRENT));
         out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
-        out.writeLong(computeRealtime(NOWREAL_SYS, STATS_CURRENT));
         out.writeInt(mDischargeUnplugLevel);
         out.writeInt(mDischargeCurrentLevel);
-        
+        out.writeInt(mLowDischargeAmountSinceCharge);
+        out.writeInt(mHighDischargeAmountSinceCharge);
         
         mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL);
         for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
@@ -4256,11 +4269,8 @@
                     out.writeString(ent.getKey());
                     Uid.Proc ps = ent.getValue();
                     out.writeLong(ps.mUserTime);
-                    out.writeLong(ps.mUserTime - ps.mLoadedUserTime);
                     out.writeLong(ps.mSystemTime);
-                    out.writeLong(ps.mSystemTime - ps.mLoadedSystemTime);
                     out.writeInt(ps.mStarts);
-                    out.writeInt(ps.mStarts - ps.mLoadedStarts);
                 }
             }
 
@@ -4272,7 +4282,6 @@
                     out.writeString(ent.getKey());
                     Uid.Pkg ps = ent.getValue();
                     out.writeInt(ps.mWakeups);
-                    out.writeInt(ps.mWakeups - ps.mLoadedWakeups);
                     final int NS = ps.mServiceStats.size();
                     out.writeInt(NS);
                     if (NS > 0) {
@@ -4282,11 +4291,8 @@
                             BatteryStatsImpl.Uid.Pkg.Serv ss = sent.getValue();
                             long time = ss.getStartTimeToNowLocked(NOW);
                             out.writeLong(time);
-                            out.writeLong(time - ss.mLoadedStartTime);
                             out.writeInt(ss.mStarts);
-                            out.writeInt(ss.mStarts - ss.mLoadedStarts);
                             out.writeInt(ss.mLaunches);
-                            out.writeInt(ss.mLaunches - ss.mLoadedLaunches);
                         }
                     }
                 }
@@ -4311,9 +4317,9 @@
         
         mStartCount = in.readInt();
         mBatteryUptime = in.readLong();
-        mBatteryLastUptime = in.readLong();
+        mBatteryLastUptime = 0;
         mBatteryRealtime = in.readLong();
-        mBatteryLastRealtime = in.readLong();
+        mBatteryLastRealtime = 0;
         mScreenOn = false;
         mScreenOnTimer = new StopwatchTimer(-1, null, mUnpluggables, in);
         for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
@@ -4337,10 +4343,10 @@
         mBluetoothOnTimer = new StopwatchTimer(-2, null, mUnpluggables, in);
         mUptime = in.readLong();
         mUptimeStart = in.readLong();
-        mLastUptime = in.readLong();
+        mLastUptime = 0;
         mRealtime = in.readLong();
         mRealtimeStart = in.readLong();
-        mLastRealtime = in.readLong();
+        mLastRealtime = 0;
         mOnBattery = in.readInt() != 0;
         mOnBatteryInternal = false; // we are no longer really running.
         mTrackBatteryPastUptime = in.readLong();
@@ -4351,6 +4357,8 @@
         mUnpluggedBatteryRealtime = in.readLong();
         mDischargeUnplugLevel = in.readInt();
         mDischargeCurrentLevel = in.readInt();
+        mLowDischargeAmountSinceCharge = in.readInt();
+        mHighDischargeAmountSinceCharge = in.readInt();
         mLastWriteTime = in.readLong();
 
         mMobileDataRx[STATS_LAST] = in.readLong();
@@ -4396,11 +4404,15 @@
     }
 
     public void writeToParcel(Parcel out, int flags) {
-        writeToParcelLocked(out, flags);
+        writeToParcelLocked(out, true, flags);
+    }
+
+    public void writeToParcelWithoutUids(Parcel out, int flags) {
+        writeToParcelLocked(out, false, flags);
     }
     
     @SuppressWarnings("unused") 
-    void writeToParcelLocked(Parcel out, int flags) {
+    void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
         final long uSecUptime = SystemClock.uptimeMillis() * 1000;
         final long uSecRealtime = SystemClock.elapsedRealtime() * 1000;
         final long batteryUptime = getBatteryUptimeLocked(uSecUptime);
@@ -4412,9 +4424,7 @@
         
         out.writeInt(mStartCount);
         out.writeLong(mBatteryUptime);
-        out.writeLong(mBatteryLastUptime);
         out.writeLong(mBatteryRealtime);
-        out.writeLong(mBatteryLastRealtime);
         mScreenOnTimer.writeToParcel(out, batteryRealtime);
         for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
             mScreenBrightnessTimer[i].writeToParcel(out, batteryRealtime);
@@ -4433,10 +4443,8 @@
         mBluetoothOnTimer.writeToParcel(out, batteryRealtime);
         out.writeLong(mUptime);
         out.writeLong(mUptimeStart);
-        out.writeLong(mLastUptime);
         out.writeLong(mRealtime);
         out.writeLong(mRealtimeStart);
-        out.writeLong(mLastRealtime);
         out.writeInt(mOnBattery ? 1 : 0);
         out.writeLong(batteryUptime);
         out.writeLong(mTrackBatteryUptimeStart);
@@ -4446,6 +4454,8 @@
         out.writeLong(mUnpluggedBatteryRealtime);
         out.writeInt(mDischargeUnplugLevel);
         out.writeInt(mDischargeCurrentLevel);
+        out.writeInt(mLowDischargeAmountSinceCharge);
+        out.writeInt(mHighDischargeAmountSinceCharge);
         out.writeLong(mLastWriteTime);
 
         out.writeLong(getMobileTcpBytesReceived(STATS_SINCE_UNPLUGGED));
@@ -4458,27 +4468,35 @@
 
         out.writeInt(getBluetoothPingCount());
 
-        out.writeInt(mKernelWakelockStats.size());
-        for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
-            SamplingTimer kwlt = ent.getValue();
-            if (kwlt != null) {
-                out.writeInt(1);
-                out.writeString(ent.getKey());
-                Timer.writeTimerToParcel(out, kwlt, batteryRealtime);
-            } else {
-                out.writeInt(0);
+        if (inclUids) {
+            out.writeInt(mKernelWakelockStats.size());
+            for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
+                SamplingTimer kwlt = ent.getValue();
+                if (kwlt != null) {
+                    out.writeInt(1);
+                    out.writeString(ent.getKey());
+                    Timer.writeTimerToParcel(out, kwlt, batteryRealtime);
+                } else {
+                    out.writeInt(0);
+                }
             }
+        } else {
+            out.writeInt(0);
         }
 
         out.writeInt(sNumSpeedSteps);
 
-        int size = mUidStats.size();
-        out.writeInt(size);
-        for (int i = 0; i < size; i++) {
-            out.writeInt(mUidStats.keyAt(i));
-            Uid uid = mUidStats.valueAt(i);
+        if (inclUids) {
+            int size = mUidStats.size();
+            out.writeInt(size);
+            for (int i = 0; i < size; i++) {
+                out.writeInt(mUidStats.keyAt(i));
+                Uid uid = mUidStats.valueAt(i);
 
-            uid.writeToParcelLocked(out, batteryRealtime);
+                uid.writeToParcelLocked(out, batteryRealtime);
+            }
+        } else {
+            out.writeInt(0);
         }
     }
 
diff --git a/core/java/com/android/internal/widget/ActionBarContextView.java b/core/java/com/android/internal/widget/ActionBarContextView.java
index 08405a3..a185e21 100644
--- a/core/java/com/android/internal/widget/ActionBarContextView.java
+++ b/core/java/com/android/internal/widget/ActionBarContextView.java
@@ -19,11 +19,11 @@
 import com.android.internal.view.menu.ActionMenuView;
 import com.android.internal.view.menu.MenuBuilder;
 
-import android.app.ContextualMode;
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.graphics.drawable.Drawable;
 import android.util.AttributeSet;
+import android.view.ActionMode;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -135,7 +135,7 @@
         }
     }
 
-    public void initForMode(final ContextualMode mode) {
+    public void initForMode(final ActionMode mode) {
         if (mCloseButton == null) {
             mCloseButton = new ImageButton(getContext());
             mCloseButton.setImageDrawable(mCloseDrawable);
diff --git a/core/jni/android/graphics/TextLayout.cpp b/core/jni/android/graphics/TextLayout.cpp
index 3de2c95..716d960 100644
--- a/core/jni/android/graphics/TextLayout.cpp
+++ b/core/jni/android/graphics/TextLayout.cpp
@@ -151,7 +151,7 @@
 }
 
 bool TextLayout::prepareText(SkPaint *paint, const jchar* text, jsize len, jint bidiFlags,
-        const jchar** outText, int32_t* outBytes) {
+        const jchar** outText, int32_t* outBytes, jchar** outBuffer) {
     const jchar *workText = text;
     jchar *buffer = NULL;
     int dir = kDirection_LTR;
@@ -196,8 +196,8 @@
 
     *outBytes = (workLimit - workText) << 1;
     *outText = workText;
-    
-    free(buffer);
+    *outBuffer = buffer;
+
     return true;
 }
 
@@ -207,8 +207,9 @@
 void TextLayout::handleText(SkPaint *paint, const jchar* text, jsize len,
                             jint bidiFlags, jfloat x, jfloat y,SkCanvas *canvas, SkPath *path) {
     const jchar *workText;
+    jchar *buffer = NULL;
     int32_t workBytes;
-    if (prepareText(paint, text, len, bidiFlags, &workText, &workBytes)) {
+    if (prepareText(paint, text, len, bidiFlags, &workText, &workBytes, &buffer)) {
         SkScalar x_ = SkFloatToScalar(x);
         SkScalar y_ = SkFloatToScalar(y);
         if (canvas) {
@@ -216,6 +217,7 @@
         } else {
             paint->getTextPath(workText, workBytes, x_, y_, path);
         }
+        free(buffer);
     }
 }
 
diff --git a/core/jni/android/graphics/TextLayout.h b/core/jni/android/graphics/TextLayout.h
index dc7208a..3d05e18 100644
--- a/core/jni/android/graphics/TextLayout.h
+++ b/core/jni/android/graphics/TextLayout.h
@@ -65,7 +65,7 @@
                                SkPath* path, SkCanvas* canvas);
                                
    static bool prepareText(SkPaint *paint, const jchar* text, jsize len, jint bidiFlags,
-        const jchar** outText, int32_t* outBytes);
+        const jchar** outText, int32_t* outBytes, jchar** outBuffer);
 
 private:
     static bool needsLayout(const jchar* text, jint len, jint bidiFlags);
diff --git a/core/jni/android_database_SQLiteDatabase.cpp b/core/jni/android_database_SQLiteDatabase.cpp
index 290b532..003bbc1 100644
--- a/core/jni/android_database_SQLiteDatabase.cpp
+++ b/core/jni/android_database_SQLiteDatabase.cpp
@@ -15,7 +15,7 @@
  */
 
 #undef LOG_TAG
-#define LOG_TAG "Database"
+#define LOG_TAG "SqliteDatabaseCpp"
 
 #include <utils/Log.h>
 #include <utils/String8.h>
@@ -245,77 +245,6 @@
     }
 }
 
-/* public native void native_execSQL(String sql); */
-static void native_execSQL(JNIEnv* env, jobject object, jstring sqlString)
-{
-    int err;
-    int stepErr;
-    sqlite3_stmt * statement = NULL;
-    sqlite3 * handle = (sqlite3 *)env->GetIntField(object, offset_db_handle);
-    jchar const * sql = env->GetStringChars(sqlString, NULL);
-    jsize sqlLen = env->GetStringLength(sqlString);
-
-    if (sql == NULL || sqlLen == 0) {
-        jniThrowException(env, "java/lang/IllegalArgumentException",
-                "You must supply an SQL string");
-        return;
-    }
-
-    err = sqlite3_prepare16_v2(handle, sql, sqlLen * 2, &statement, NULL);
-
-    env->ReleaseStringChars(sqlString, sql);
-
-    if (err != SQLITE_OK) {
-        char const * sql8 = env->GetStringUTFChars(sqlString, NULL);
-        LOGE("Failure %d (%s) on %p when preparing '%s'.\n", err, sqlite3_errmsg(handle),
-                handle, sql8);
-        throw_sqlite3_exception(env, handle, sql8);
-        env->ReleaseStringUTFChars(sqlString, sql8);
-        return;
-    }
-
-    stepErr = sqlite3_step(statement);
-    err = sqlite3_finalize(statement);
-
-    if (stepErr != SQLITE_DONE) {
-        if (stepErr == SQLITE_ROW) {
-            throw_sqlite3_exception(env,
-                    "Queries cannot be performed using execSQL(), use query() instead.");
-        } else {
-            char const * sql8 = env->GetStringUTFChars(sqlString, NULL);
-            LOGE("Failure %d (%s) on %p when executing '%s'\n", err, sqlite3_errmsg(handle),
-                    handle, sql8);
-            throw_sqlite3_exception(env, handle, sql8);
-            env->ReleaseStringUTFChars(sqlString, sql8);
-
-        }
-    } else
-#ifndef DB_LOG_STATEMENTS
-    IF_LOGV()
-#endif
-    {
-        char const * sql8 = env->GetStringUTFChars(sqlString, NULL);
-        LOGV("Success on %p when executing '%s'\n", handle, sql8);
-        env->ReleaseStringUTFChars(sqlString, sql8);
-    }
-}
-
-/* native long lastInsertRow(); */
-static jlong lastInsertRow(JNIEnv* env, jobject object)
-{
-    sqlite3 * handle = (sqlite3 *)env->GetIntField(object, offset_db_handle);
-
-    return sqlite3_last_insert_rowid(handle);
-}
-
-/* native int lastChangeCount(); */
-static jint lastChangeCount(JNIEnv* env, jobject object)
-{
-    sqlite3 * handle = (sqlite3 *)env->GetIntField(object, offset_db_handle);
-
-    return sqlite3_changes(handle);
-}
-
 /* native int native_getDbLookaside(); */
 static jint native_getDbLookaside(JNIEnv* env, jobject object)
 {
@@ -522,9 +451,6 @@
     {"dbclose", "()V", (void *)dbclose},
     {"enableSqlTracing", "(Ljava/lang/String;S)V", (void *)enableSqlTracing},
     {"enableSqlProfiling", "(Ljava/lang/String;S)V", (void *)enableSqlProfiling},
-    {"native_execSQL", "(Ljava/lang/String;)V", (void *)native_execSQL},
-    {"lastInsertRow", "()J", (void *)lastInsertRow},
-    {"lastChangeCount", "()I", (void *)lastChangeCount},
     {"native_setLocale", "(Ljava/lang/String;I)V", (void *)native_setLocale},
     {"native_getDbLookaside", "()I", (void *)native_getDbLookaside},
     {"releaseMemory", "()I", (void *)native_releaseMemory},
diff --git a/core/jni/android_database_SQLiteStatement.cpp b/core/jni/android_database_SQLiteStatement.cpp
index 0341e0b..2212d9a 100644
--- a/core/jni/android_database_SQLiteStatement.cpp
+++ b/core/jni/android_database_SQLiteStatement.cpp
@@ -16,7 +16,7 @@
 */
 
 #undef LOG_TAG
-#define LOG_TAG "Cursor"
+#define LOG_TAG "SQLiteStatementCpp"
 
 #include <jni.h>
 #include <JNIHelp.h>
@@ -48,24 +48,40 @@
         (sqlite3 *)env->GetIntField(object, gHandleField)
 
 
-static void native_execute(JNIEnv* env, jobject object)
+static jint native_execute(JNIEnv* env, jobject object)
 {
     int err;
     sqlite3 * handle = GET_HANDLE(env, object);
     sqlite3_stmt * statement = GET_STATEMENT(env, object);
+    int numChanges = -1;
 
     // Execute the statement
     err = sqlite3_step(statement);
 
-    // Throw an exception if an error occured
+    // Throw an exception if an error occurred
     if (err == SQLITE_ROW) {
-        LOGV("Queries cannot be performed using execute(). use SQLiteDatabase.query() instead.");
+        throw_sqlite3_exception(env,
+                "Queries can be performed using SQLiteDatabase query or rawQuery methods only.");
     } else if (err != SQLITE_DONE) {
         throw_sqlite3_exception_errcode(env, err, sqlite3_errmsg(handle));
+    } else {
+        numChanges = sqlite3_changes(handle);
     }
 
-    // Reset the statment so it's ready to use again
+    // Reset the statement so it's ready to use again
     sqlite3_reset(statement);
+    return numChanges;
+}
+
+static jlong native_executeInsert(JNIEnv* env, jobject object)
+{
+    sqlite3 * handle = GET_HANDLE(env, object);
+    jint numChanges = native_execute(env, object);
+    if (numChanges > 0) {
+        return sqlite3_last_insert_rowid(handle);
+    } else {
+        return -1;
+    }
 }
 
 static jlong native_1x1_long(JNIEnv* env, jobject object)
@@ -117,11 +133,11 @@
     return value;
 }
 
-
 static JNINativeMethod sMethods[] =
 {
      /* name, signature, funcPtr */
-    {"native_execute", "()V", (void *)native_execute},
+    {"native_execute", "()I", (void *)native_execute},
+    {"native_executeInsert", "()J", (void *)native_executeInsert},
     {"native_1x1_long", "()J", (void *)native_1x1_long},
     {"native_1x1_string", "()Ljava/lang/String;", (void *)native_1x1_string},
 };
diff --git a/core/jni/android_view_GLES20Canvas.cpp b/core/jni/android_view_GLES20Canvas.cpp
index d2e7faf..1e4a66d 100644
--- a/core/jni/android_view_GLES20Canvas.cpp
+++ b/core/jni/android_view_GLES20Canvas.cpp
@@ -256,9 +256,11 @@
 static void renderText(OpenGLRenderer* renderer, const jchar* text, int count,
         jfloat x, jfloat y, int flags, SkPaint* paint) {
     const jchar *workText;
+    jchar* buffer = NULL;
     int32_t workBytes;
-    if (TextLayout::prepareText(paint, text, count, flags, &workText, &workBytes)) {
+    if (TextLayout::prepareText(paint, text, count, flags, &workText, &workBytes, &buffer)) {
         renderer->drawText((const char*) workText, workBytes, count, x, y, paint);
+        free(buffer);
     }
 }
 
diff --git a/core/tests/coretests/src/android/database/DatabaseGeneralTest.java b/core/tests/coretests/src/android/database/DatabaseGeneralTest.java
index e33421a..cd38bf07 100644
--- a/core/tests/coretests/src/android/database/DatabaseGeneralTest.java
+++ b/core/tests/coretests/src/android/database/DatabaseGeneralTest.java
@@ -21,6 +21,7 @@
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteException;
 import android.os.Handler;
 import android.os.Parcel;
 import android.test.AndroidTestCase;
@@ -384,54 +385,28 @@
 
     @MediumTest
     public void testSchemaChange2() throws Exception {
-        SQLiteDatabase db1 = mDatabase;
-        SQLiteDatabase db2 = SQLiteDatabase.openOrCreateDatabase(mDatabaseFile, null);
-        Cursor cursor;
-
-        db1.execSQL("CREATE TABLE db1 (_id INTEGER PRIMARY KEY, data TEXT);");
-
-        cursor = db1.query("db1", null, null, null, null, null, null);
-        assertNotNull("Cursor is null", cursor);
+        mDatabase.execSQL("CREATE TABLE db1 (_id INTEGER PRIMARY KEY, data TEXT);");
+        Cursor cursor = mDatabase.query("db1", null, null, null, null, null, null);
+        assertNotNull(cursor);
         assertEquals(0, cursor.getCount());
-        cursor.deactivate();
-        // this cause exception because we're still using sqlite_prepate16 and not
-        // sqlite_prepare16_v2. The v2 variant added the ability to check the
-        // schema version and handle the case when the schema has changed
-        // Marco Nelissen claim it was 2x slower to compile SQL statements so
-        // I reverted back to the v1 variant.
-        /* db2.execSQL("CREATE TABLE db2 (_id INTEGER PRIMARY KEY, data TEXT);");
-
-        cursor = db1.query("db1", null, null, null, null, null, null);
-        assertNotNull("Cursor is null", cursor);
-        assertEquals(0, cursor.count());
-        cursor.deactivate();
-        */
+        cursor.close();
     }
 
     @MediumTest
     public void testSchemaChange3() throws Exception {
-        SQLiteDatabase db1 = mDatabase;
-        SQLiteDatabase db2 = SQLiteDatabase.openOrCreateDatabase(mDatabaseFile, null);
-        Cursor cursor;
-
-
-        db1.execSQL("CREATE TABLE db1 (_id INTEGER PRIMARY KEY, data TEXT);");
-        db1.execSQL("INSERT INTO db1 (data) VALUES ('test');");
-
-        cursor = db1.query("db1", null, null, null, null, null, null);
-        // this cause exception because we're still using sqlite_prepate16 and not
-        // sqlite_prepare16_v2. The v2 variant added the ability to check the
-        // schema version and handle the case when the schema has changed
-        // Marco Nelissen claim it was 2x slower to compile SQL statements so
-        // I reverted back to the v1 variant.
-        /* db2.execSQL("CREATE TABLE db2 (_id INTEGER PRIMARY KEY, data TEXT);");
-
-        assertNotNull("Cursor is null", cursor);
-        assertEquals(1, cursor.count());
-        assertTrue(cursor.first());
-        assertEquals("test", cursor.getString(cursor.getColumnIndexOrThrow("data")));
-        cursor.deactivate();
-        */
+        mDatabase.execSQL("CREATE TABLE db1 (_id INTEGER PRIMARY KEY, data TEXT);");
+        mDatabase.execSQL("INSERT INTO db1 (data) VALUES ('test');");
+        mDatabase.execSQL("ALTER TABLE db1 ADD COLUMN blah int;");
+        Cursor c = null;
+        try {
+            c = mDatabase.rawQuery("select blah from db1", null);
+        } catch (SQLiteException e) {
+            fail("unexpected exception: " + e.getMessage());
+        } finally {
+            if (c != null) {
+                c.close();
+            }
+        }
     }
 
     private class ChangeObserver extends ContentObserver {
diff --git a/core/tests/hosttests/src/android/content/pm/PackageManagerHostTestUtils.java b/core/tests/hosttests/src/android/content/pm/PackageManagerHostTestUtils.java
index fc71b77..38191b0 100644
--- a/core/tests/hosttests/src/android/content/pm/PackageManagerHostTestUtils.java
+++ b/core/tests/hosttests/src/android/content/pm/PackageManagerHostTestUtils.java
@@ -16,24 +16,24 @@
 
 package android.content.pm;
 
+import com.android.ddmlib.AdbCommandRejectedException;
 import com.android.ddmlib.AndroidDebugBridge;
 import com.android.ddmlib.IDevice;
 import com.android.ddmlib.IShellOutputReceiver;
+import com.android.ddmlib.InstallException;
 import com.android.ddmlib.Log;
 import com.android.ddmlib.MultiLineReceiver;
-import com.android.ddmlib.SyncService;
+import com.android.ddmlib.ShellCommandUnresponsiveException;
+import com.android.ddmlib.SyncException;
+import com.android.ddmlib.TimeoutException;
 import com.android.ddmlib.SyncService.ISyncProgressMonitor;
-import com.android.ddmlib.SyncService.SyncResult;
 import com.android.ddmlib.testrunner.ITestRunListener;
 import com.android.ddmlib.testrunner.RemoteAndroidTestRunner;
 import com.android.ddmlib.testrunner.TestIdentifier;
-import com.android.hosttest.DeviceTestCase;
-import com.android.hosttest.DeviceTestSuite;
 
 import java.io.BufferedReader;
-import java.io.File;
-import java.io.InputStreamReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.io.StringReader;
 import java.lang.Runtime;
 import java.lang.Process;
@@ -42,7 +42,6 @@
 import java.util.regex.Pattern;
 
 import junit.framework.Assert;
-import com.android.hosttest.DeviceTestCase;
 
 /**
  * Set of tests that verify host side install cases
@@ -120,8 +119,14 @@
      * Helper method to run tests and return the listener that collected the results.
      * @param pkgName Android application package for tests
      * @return the {@link CollectingTestRunListener}
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
      */
-    private CollectingTestRunListener doRunTests(String pkgName) throws IOException {
+    private CollectingTestRunListener doRunTests(String pkgName) throws IOException,
+    TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException {
         RemoteAndroidTestRunner testRunner = new RemoteAndroidTestRunner(
                 pkgName, mDevice);
         CollectingTestRunListener listener = new CollectingTestRunListener();
@@ -138,8 +143,14 @@
      *
      * @param pkgName Android application package for tests
      * @return true if every test passed, false otherwise.
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
      */
-    public boolean runDeviceTestsDidAllTestsPass(String pkgName) throws IOException {
+    public boolean runDeviceTestsDidAllTestsPass(String pkgName) throws IOException,
+            TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException {
         CollectingTestRunListener listener = doRunTests(pkgName);
         return listener.didAllTestsPass();
     }
@@ -147,22 +158,26 @@
     /**
      * Helper method to push a file to device
      * @param apkAppPrivatePath
-     * @throws IOException
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws IOException if connection to device was lost.
+     * @throws SyncException if the sync failed for another reason.
      */
     public void pushFile(final String localFilePath, final String destFilePath)
-            throws IOException {
-        SyncResult result = mDevice.getSyncService().pushFile(
-                localFilePath, destFilePath, new NullSyncProgressMonitor());
-        assertEquals(SyncService.RESULT_OK, result.getCode());
+            throws IOException, SyncException, TimeoutException, AdbCommandRejectedException {
+        mDevice.getSyncService().pushFile(localFilePath,
+                destFilePath, new NullSyncProgressMonitor());
     }
 
     /**
      * Helper method to install a file
      * @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
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed
      */
-    public void installFile(final String localFilePath, final boolean replace) throws IOException {
+    public void installFile(final String localFilePath, final boolean replace) throws IOException,
+            InstallException {
         String result = mDevice.installPackage(localFilePath, replace);
         assertEquals(null, result);
     }
@@ -172,10 +187,11 @@
      * @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
      * @return the string output of the failed install attempt
-     * @throws IOException
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed
      */
     public String installFileFail(final String localFilePath, final boolean replace)
-            throws IOException {
+            throws IOException, InstallException {
         String result = mDevice.installPackage(localFilePath, replace);
         assertNotNull(result);
         return result;
@@ -185,10 +201,17 @@
      * Helper method to install a file to device as forward locked
      * @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
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws SyncException if the sync failed for another reason.
+     * @throws InstallException if the install failed.
      */
     public String installFileForwardLocked(final String localFilePath, final boolean replace)
-            throws IOException {
+            throws IOException, SyncException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException, InstallException {
         String remoteFilePath = mDevice.syncPackageToDevice(localFilePath);
         InstallReceiver receiver = new InstallReceiver();
         String cmd = String.format(replace ? "pm install -r -l \"%1$s\"" :
@@ -203,9 +226,14 @@
      *
      * @param destPath the absolute path of file on device to check
      * @return <code>true</code> if file exists, <code>false</code> otherwise.
-     * @throws IOException if adb shell command failed
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
      */
-    public boolean doesRemoteFileExist(String destPath) throws IOException {
+    public boolean doesRemoteFileExist(String destPath) throws IOException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
         String lsGrep = executeShellCommand(String.format("ls %s", destPath));
         return !lsGrep.contains("No such file or directory");
     }
@@ -216,10 +244,15 @@
      * @param destPath the absolute path of the file
      * @return <code>true</code> if file exists containing given string,
      *         <code>false</code> otherwise.
-     * @throws IOException if adb shell command failed
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
      */
     public boolean doesRemoteFileExistContainingString(String destPath, String searchString)
-            throws IOException {
+            throws IOException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         String lsResult = executeShellCommand(String.format("ls %s", destPath));
         return lsResult.contains(searchString);
     }
@@ -229,9 +262,14 @@
      *
      * @param packageName the Android manifest package to check.
      * @return <code>true</code> if package exists, <code>false</code> otherwise
-     * @throws IOException if adb shell command failed
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
      */
-    public boolean doesPackageExist(String packageName) throws IOException {
+    public boolean doesPackageExist(String packageName) throws IOException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
         String pkgGrep = executeShellCommand(String.format("pm path %s", packageName));
         return pkgGrep.contains("package:");
     }
@@ -241,9 +279,14 @@
      *
      * @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
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
      */
-    public boolean doesAppExistOnDevice(String packageName) throws IOException {
+    public boolean doesAppExistOnDevice(String packageName) throws IOException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
         return doesRemoteFileExistContainingString(DEVICE_APP_PATH, packageName);
     }
 
@@ -252,9 +295,14 @@
      *
      * @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
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
      */
-    public boolean doesAppExistOnSDCard(String packageName) throws IOException {
+    public boolean doesAppExistOnSDCard(String packageName) throws IOException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
         return doesRemoteFileExistContainingString(SDCARD_APP_PATH, packageName);
     }
 
@@ -263,9 +311,14 @@
      *
      * @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
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
      */
-    public boolean doesAppExistAsForwardLocked(String packageName) throws IOException {
+    public boolean doesAppExistAsForwardLocked(String packageName) throws IOException,
+            TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException {
         return doesRemoteFileExistContainingString(APP_PRIVATE_PATH, packageName);
     }
 
@@ -273,9 +326,14 @@
      * Waits for device's package manager to respond.
      *
      * @throws InterruptedException
-     * @throws IOException
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
      */
-    public void waitForPackageManager() throws InterruptedException, IOException {
+    public void waitForPackageManager() throws InterruptedException, IOException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "waiting for device");
         int currentWaitTime = 0;
         // poll the package manager until it returns something for android
@@ -341,9 +399,14 @@
      *
      * @param packageName The name of the package to wait to load
      * @throws InterruptedException
-     * @throws IOException
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
      */
-    public void waitForApp(String packageName) throws InterruptedException, IOException {
+    public void waitForApp(String packageName) throws InterruptedException, IOException,
+            TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "waiting for app to launch");
         int currentWaitTime = 0;
         // poll the package manager until it returns something for the package we're looking for
@@ -360,9 +423,14 @@
     /**
      * Helper method which executes a adb shell command and returns output as a {@link String}
      * @return the output of the command
-     * @throws IOException
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
      */
-    public String executeShellCommand(String command) throws IOException {
+    public String executeShellCommand(String command) throws IOException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, String.format("adb shell %s", command));
         CollectingOutputReceiver receiver = new CollectingOutputReceiver();
         mDevice.executeShellCommand(command, receiver);
@@ -374,9 +442,14 @@
     /**
      * Helper method ensures we are in root mode on the host side. It returns only after
      * PackageManager is actually up and running.
-     * @throws IOException
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
      */
-    public void runAdbRoot() throws IOException, InterruptedException {
+    public void runAdbRoot() throws IOException, InterruptedException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "adb root");
         Runtime runtime = Runtime.getRuntime();
         Process process = runtime.exec("adb root"); // adb should be in the path
@@ -394,10 +467,15 @@
     /**
      * Helper method which reboots the device and returns once the device is online again
      * and package manager is up and running (note this function is synchronous to callers).
-     * @throws IOException
      * @throws InterruptedException
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
      */
-    public void rebootDevice() throws IOException, InterruptedException {
+    public void rebootDevice() throws IOException, InterruptedException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
         String command = "reboot"; // no need for -s since mDevice is already tied to a device
         Log.i(LOG_TAG, command);
         CollectingOutputReceiver receiver = new CollectingOutputReceiver();
@@ -550,17 +628,23 @@
     /**
      * 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.
      *
      * @param the path of the apk to install
      * @param the name of the package
      * @param <code>true</code> if the app should be overwritten, <code>false</code> otherwise
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
-     * <p/>
-     * Assumes adb is running as root in device under test.
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
     public void installAppAndVerifyExistsOnSDCard(String apkPath, String pkgName, boolean overwrite)
-            throws IOException, InterruptedException {
+            throws IOException, InterruptedException, InstallException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
         // Start with a clean slate if we're not overwriting
         if (!overwrite) {
             // cleanup test app just in case it already exists
@@ -581,17 +665,23 @@
     /**
      * 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.
      *
      * @param the path of the apk to install
      * @param the name of the package
      * @param <code>true</code> if the app should be overwritten, <code>false</code> otherwise
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
-     * <p/>
-     * Assumes adb is running as root in device under test.
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
     public void installAppAndVerifyExistsOnDevice(String apkPath, String pkgName, boolean overwrite)
-            throws IOException, InterruptedException {
+            throws IOException, InterruptedException, InstallException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
         // Start with a clean slate if we're not overwriting
         if (!overwrite) {
             // cleanup test app just in case it already exists
@@ -612,17 +702,24 @@
     /**
      * Helper method for installing an app as forward-locked, and
      * then verifying the app was installed in the proper forward-locked location.
+     * <p/>
+     * Assumes adb is running as root in device under test.
      *
      * @param the path of the apk to install
      * @param the name of the package
      * @param <code>true</code> if the app should be overwritten, <code>false</code> otherwise
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
-     * <p/>
-     * Assumes adb is running as root in device under test.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
      */
     public void installFwdLockedAppAndVerifyExists(String apkPath,
-            String pkgName, boolean overwrite) throws IOException, InterruptedException {
+            String pkgName, boolean overwrite) throws IOException, InterruptedException,
+            InstallException, SyncException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         // Start with a clean slate if we're not overwriting
         if (!overwrite) {
             // cleanup test app just in case it already exists
@@ -643,14 +740,21 @@
 
     /**
      * Helper method for uninstalling an app.
-     *
-     * @param pkgName package name to uninstall
-     * @throws IOException if adb shell command failed
-     * @throws InterruptedException if the thread was interrupted
      * <p/>
      * Assumes adb is running as root in device under test.
+     *
+     * @param pkgName package name to uninstall
+     * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the uninstall failed.
      */
-    public void uninstallApp(String pkgName) throws IOException, InterruptedException {
+    public void uninstallApp(String pkgName) throws IOException, InterruptedException,
+            InstallException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         mDevice.uninstallPackage(pkgName);
         // make sure its not installed anymore
         assertFalse(doesPackageExist(pkgName));
@@ -660,12 +764,18 @@
      * Helper method for clearing any installed non-system apps.
      * Useful ensuring no non-system apps are installed, and for cleaning up stale files that
      * may be lingering on the system for whatever reason.
-     *
-     * @throws IOException if adb shell command failed
      * <p/>
      * Assumes adb is running as root in device under test.
+     *
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the uninstall failed.
      */
-    public void wipeNonSystemApps() throws IOException {
+    public void wipeNonSystemApps() throws IOException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException, InstallException {
       String allInstalledPackages = executeShellCommand("pm list packages -f");
       BufferedReader outputReader = new BufferedReader(new StringReader(allInstalledPackages));
 
@@ -690,8 +800,14 @@
      *
      * <p/>
      * Assumes adb is running as root in device under test.
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
      */
-    public void setDevicePreferredInstallLocation(InstallLocPreference pref) throws IOException {
+    public void setDevicePreferredInstallLocation(InstallLocPreference pref) throws IOException,
+            TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException {
         String command = "pm setInstallLocation %d";
         int locValue = 0;
         switch (pref) {
@@ -713,8 +829,14 @@
      *
      * <p/>
      * Assumes adb is running as root in device under test.
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
      */
-    public InstallLocPreference getDevicePreferredInstallLocation() throws IOException {
+    public InstallLocPreference getDevicePreferredInstallLocation() throws IOException,
+            TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException {
         String result = executeShellCommand("pm getInstallLocation");
         if (result.indexOf('0') != -1) {
             return InstallLocPreference.AUTO;
diff --git a/core/tests/hosttests/src/android/content/pm/PackageManagerHostTests.java b/core/tests/hosttests/src/android/content/pm/PackageManagerHostTests.java
index 1b797d5..22a2be6 100644
--- a/core/tests/hosttests/src/android/content/pm/PackageManagerHostTests.java
+++ b/core/tests/hosttests/src/android/content/pm/PackageManagerHostTests.java
@@ -16,13 +16,12 @@
 
 package android.content.pm;
 
-import com.android.ddmlib.IDevice;
-import com.android.ddmlib.IShellOutputReceiver;
+import com.android.ddmlib.AdbCommandRejectedException;
+import com.android.ddmlib.InstallException;
 import com.android.ddmlib.Log;
-import com.android.ddmlib.MultiLineReceiver;
-import com.android.ddmlib.SyncService;
-import com.android.ddmlib.SyncService.ISyncProgressMonitor;
-import com.android.ddmlib.SyncService.SyncResult;
+import com.android.ddmlib.ShellCommandUnresponsiveException;
+import com.android.ddmlib.SyncException;
+import com.android.ddmlib.TimeoutException;
 import com.android.hosttest.DeviceTestCase;
 import com.android.hosttest.DeviceTestSuite;
 
@@ -156,10 +155,18 @@
      * the app, and otherwise cause the system to blow up.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws SyncException if the sync failed for another reason.
+     * @throws InstallException if the install failed.
      */
-    public void testPushAppPrivate() throws IOException, InterruptedException {
+    public void testPushAppPrivate() throws IOException, InterruptedException, InstallException,
+            TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException,
+            SyncException {
         Log.i(LOG_TAG, "testing pushing an apk to /data/app-private");
         final String apkAppPrivatePath =  appPrivatePath + SIMPLE_APK;
 
@@ -187,12 +194,18 @@
      * @param apkName the file name of the test app apk
      * @param pkgName the package name of the test app apk
      * @param expectedLocation the file name of the test app apk
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
     private void doStandardInstall(String apkName, String pkgName,
             PackageManagerHostTestUtils.InstallLocation expectedLocation)
-            throws IOException, InterruptedException {
+            throws IOException, InterruptedException, InstallException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
 
         if (expectedLocation == PackageManagerHostTestUtils.InstallLocation.DEVICE) {
             mPMHostUtils.installAppAndVerifyExistsOnDevice(
@@ -211,12 +224,18 @@
      * Assumes adb is running as root in device under test.
      * @param preference the device's preferred location of where to install apps
      * @param expectedLocation the expected location of where the apk was installed
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
     public void installAppAutoLoc(PackageManagerHostTestUtils.InstallLocPreference preference,
             PackageManagerHostTestUtils.InstallLocation expectedLocation)
-            throws IOException, InterruptedException {
+            throws IOException, InterruptedException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException, InstallException {
 
         PackageManagerHostTestUtils.InstallLocPreference savedPref =
                 PackageManagerHostTestUtils.InstallLocPreference.AUTO;
@@ -239,10 +258,16 @@
      * will install the app to the device when device's preference is auto.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
-    public void testInstallAppAutoLocPrefIsAuto() throws IOException, InterruptedException {
+    public void testInstallAppAutoLocPrefIsAuto() throws IOException, InterruptedException,
+            InstallException, TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test installLocation=auto, prefer=auto gets installed on device");
         installAppAutoLoc(PackageManagerHostTestUtils.InstallLocPreference.AUTO,
                 PackageManagerHostTestUtils.InstallLocation.DEVICE);
@@ -253,10 +278,17 @@
      * will install the app to the device when device's preference is internal.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
-    public void testInstallAppAutoLocPrefIsInternal() throws IOException, InterruptedException {
+    public void testInstallAppAutoLocPrefIsInternal() throws IOException, InterruptedException,
+            InstallException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test installLocation=auto, prefer=internal gets installed on device");
         installAppAutoLoc(PackageManagerHostTestUtils.InstallLocPreference.INTERNAL,
                 PackageManagerHostTestUtils.InstallLocation.DEVICE);
@@ -267,10 +299,17 @@
      * will install the app to the SD card when device's preference is external.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
-    public void testInstallAppAutoLocPrefIsExternal() throws IOException, InterruptedException {
+    public void testInstallAppAutoLocPrefIsExternal() throws IOException, InterruptedException,
+            InstallException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test installLocation=auto, prefer=external gets installed on device");
         installAppAutoLoc(PackageManagerHostTestUtils.InstallLocPreference.EXTERNAL,
                 PackageManagerHostTestUtils.InstallLocation.DEVICE);
@@ -283,12 +322,18 @@
      * Assumes adb is running as root in device under test.
      * @param preference the device's preferred location of where to install apps
      * @param expectedLocation the expected location of where the apk was installed
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the (un)install failed.
      */
     public void installAppInternalLoc(PackageManagerHostTestUtils.InstallLocPreference preference,
             PackageManagerHostTestUtils.InstallLocation expectedLocation)
-            throws IOException, InterruptedException {
+            throws IOException, InterruptedException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException, InstallException {
 
         PackageManagerHostTestUtils.InstallLocPreference savedPref =
             PackageManagerHostTestUtils.InstallLocPreference.AUTO;
@@ -311,10 +356,17 @@
      * will install the app to the device when device's preference is auto.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
-    public void testInstallAppInternalLocPrefIsAuto() throws IOException, InterruptedException {
+    public void testInstallAppInternalLocPrefIsAuto() throws IOException, InterruptedException,
+            InstallException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test installLocation=internal, prefer=auto gets installed on device");
         installAppInternalLoc(PackageManagerHostTestUtils.InstallLocPreference.AUTO,
                 PackageManagerHostTestUtils.InstallLocation.DEVICE);
@@ -325,10 +377,17 @@
      * will install the app to the device when device's preference is internal.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
-    public void testInstallAppInternalLocPrefIsInternal() throws IOException, InterruptedException {
+    public void testInstallAppInternalLocPrefIsInternal() throws IOException, InterruptedException,
+            InstallException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test installLocation=internal, prefer=internal is installed on device");
         installAppInternalLoc(PackageManagerHostTestUtils.InstallLocPreference.INTERNAL,
                 PackageManagerHostTestUtils.InstallLocation.DEVICE);
@@ -339,10 +398,17 @@
      * will install the app to the device when device's preference is external.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
-    public void testInstallAppInternalLocPrefIsExternal() throws IOException, InterruptedException {
+    public void testInstallAppInternalLocPrefIsExternal() throws IOException, InterruptedException,
+            InstallException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test installLocation=internal, prefer=external is installed on device");
         installAppInternalLoc(PackageManagerHostTestUtils.InstallLocPreference.EXTERNAL,
                 PackageManagerHostTestUtils.InstallLocation.DEVICE);
@@ -355,12 +421,18 @@
      * Assumes adb is running as root in device under test.
      * @param preference the device's preferred location of where to install apps
      * @param expectedLocation the expected location of where the apk was installed
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
     public void installAppExternalLoc(PackageManagerHostTestUtils.InstallLocPreference preference,
             PackageManagerHostTestUtils.InstallLocation expectedLocation)
-            throws IOException, InterruptedException {
+            throws IOException, InterruptedException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException, InstallException {
 
         PackageManagerHostTestUtils.InstallLocPreference savedPref =
             PackageManagerHostTestUtils.InstallLocPreference.AUTO;
@@ -384,10 +456,17 @@
      * will install the app to the device when device's preference is auto.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
-    public void testInstallAppExternalLocPrefIsAuto() throws IOException, InterruptedException {
+    public void testInstallAppExternalLocPrefIsAuto() throws IOException, InterruptedException,
+            InstallException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test installLocation=external, pref=auto gets installed on SD Card");
         installAppExternalLoc(PackageManagerHostTestUtils.InstallLocPreference.AUTO,
                 PackageManagerHostTestUtils.InstallLocation.SDCARD);
@@ -398,10 +477,17 @@
      * will install the app to the device when device's preference is internal.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
-    public void testInstallAppExternalLocPrefIsInternal() throws IOException, InterruptedException {
+    public void testInstallAppExternalLocPrefIsInternal() throws IOException, InterruptedException,
+            InstallException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test installLocation=external, pref=internal gets installed on SD Card");
         installAppExternalLoc(PackageManagerHostTestUtils.InstallLocPreference.INTERNAL,
                 PackageManagerHostTestUtils.InstallLocation.SDCARD);
@@ -412,10 +498,17 @@
      * will install the app to the device when device's preference is external.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
-    public void testInstallAppExternalLocPrefIsExternal() throws IOException, InterruptedException {
+    public void testInstallAppExternalLocPrefIsExternal() throws IOException, InterruptedException,
+            InstallException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test installLocation=external, pref=external gets installed on SD Card");
         installAppExternalLoc(PackageManagerHostTestUtils.InstallLocPreference.EXTERNAL,
                 PackageManagerHostTestUtils.InstallLocation.SDCARD);
@@ -427,10 +520,17 @@
      * system decide.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
-    public void testInstallAppNoLocPrefIsAuto() throws IOException, InterruptedException {
+    public void testInstallAppNoLocPrefIsAuto() throws IOException, InterruptedException,
+            TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException,
+            InstallException {
         Log.i(LOG_TAG, "Test an app with no installLocation gets installed on device");
 
         PackageManagerHostTestUtils.InstallLocPreference savedPref =
@@ -456,10 +556,17 @@
      * external.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
-    public void testInstallAppNoLocPrefIsExternal() throws IOException, InterruptedException {
+    public void testInstallAppNoLocPrefIsExternal() throws IOException, InterruptedException,
+            TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException,
+            InstallException {
         Log.i(LOG_TAG, "Test an app with no installLocation gets installed on SD card");
 
         PackageManagerHostTestUtils.InstallLocPreference savedPref =
@@ -485,10 +592,17 @@
      * internal.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
-    public void testInstallAppNoLocPrefIsInternal() throws IOException, InterruptedException {
+    public void testInstallAppNoLocPrefIsInternal() throws IOException, InterruptedException,
+            TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException,
+            InstallException {
         Log.i(LOG_TAG, "Test an app with no installLocation gets installed on device");
 
         PackageManagerHostTestUtils.InstallLocPreference savedPref =
@@ -513,10 +627,18 @@
      * forward-locked will get installed to the correct location.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws SyncException if the sync failed for another reason.
+     * @throws InstallException if the install failed.
      */
-    public void testInstallFwdLockedAppInternal() throws IOException, InterruptedException {
+    public void testInstallFwdLockedAppInternal() throws IOException, InterruptedException,
+            InstallException, SyncException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test an app with installLoc set to Internal gets installed to app-private");
 
         try {
@@ -534,10 +656,18 @@
      * forward-locked will get installed to the correct location.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws SyncException if the sync failed for another reason.
+     * @throws InstallException if the install failed.
      */
-    public void testInstallFwdLockedAppExternal() throws IOException, InterruptedException {
+    public void testInstallFwdLockedAppExternal() throws IOException, InterruptedException,
+            InstallException, SyncException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test an app with installLoc set to Internal gets installed to app-private");
 
         try {
@@ -555,10 +685,18 @@
      * forward-locked will get installed to the correct location.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws SyncException if the sync failed for another reason.
+     * @throws InstallException if the install failed.
      */
-    public void testInstallFwdLockedAppAuto() throws IOException, InterruptedException {
+    public void testInstallFwdLockedAppAuto() throws IOException, InterruptedException,
+            InstallException, SyncException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test an app with installLoc set to Auto gets installed to app-private");
 
         try {
@@ -576,10 +714,18 @@
      * forward-locked installed will get installed to the correct location.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws SyncException if the sync failed for another reason.
+     * @throws InstallException if the install failed.
      */
-    public void testInstallFwdLockedAppNone() throws IOException, InterruptedException {
+    public void testInstallFwdLockedAppNone() throws IOException, InterruptedException,
+            InstallException, SyncException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test an app with no installLoc set gets installed to app-private");
 
         try {
@@ -597,14 +743,21 @@
      * uninstall it, and reinstall it onto the SD card.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
     // 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 {
+    public void testReinstallInternalToExternal() throws IOException, InterruptedException,
+            InstallException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test installing an app first to the device, then to the SD Card");
 
         try {
@@ -625,14 +778,21 @@
      * uninstall it, and reinstall it onto the device.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
     // 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 {
+    public void testReinstallExternalToInternal() throws IOException, InterruptedException,
+            InstallException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test installing an app first to the SD Care, then to the device");
 
         try {
@@ -655,10 +815,16 @@
      * the update onto the SD card as well when location is set to external for both versions
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
-    public void testUpdateBothExternal() throws IOException, InterruptedException {
+    public void testUpdateBothExternal() throws IOException, InterruptedException, InstallException,
+            TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test updating an app on the SD card stays on the SD card");
 
         try {
@@ -681,10 +847,16 @@
      * updated apps' manifest file.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
-    public void testUpdateToSDCard() throws IOException, InterruptedException {
+    public void testUpdateToSDCard() throws IOException, InterruptedException, InstallException,
+            TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test updating an app on the SD card stays on the SD card");
 
         try {
@@ -706,10 +878,17 @@
      * the update onto the device if the manifest has changed to installLocation=internalOnly
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
-    public void testUpdateSDCardToDevice() throws IOException, InterruptedException {
+    public void testUpdateSDCardToDevice() throws IOException, InterruptedException,
+            InstallException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test updating an app on the SD card to the Device through manifest change");
 
         try {
@@ -731,11 +910,18 @@
      * the update onto the device's forward-locked location
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws SyncException if the sync failed for another reason.
+     * @throws InstallException if the install failed.
      */
     public void testInstallAndUpdateExternalLocForwardLockedApp()
-            throws IOException, InterruptedException {
+            throws IOException, InterruptedException, InstallException, SyncException,
+            TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test updating a forward-locked app marked preferExternal");
 
         try {
@@ -757,11 +943,18 @@
      * the update onto the device's forward-locked location
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws SyncException if the sync failed for another reason.
+     * @throws InstallException if the install failed.
      */
     public void testInstallAndUpdateNoLocForwardLockedApp()
-            throws IOException, InterruptedException {
+            throws IOException, InterruptedException, InstallException, SyncException,
+            TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test updating a forward-locked app with no installLocation pref set");
 
         try {
@@ -783,11 +976,18 @@
      * and then launched without crashing.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws SyncException if the sync failed for another reason.
+     * @throws InstallException if the install failed.
      */
     public void testInstallAndLaunchAllPermsAppOnSD()
-            throws IOException, InterruptedException {
+            throws IOException, InterruptedException, InstallException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test launching an app with all perms set, installed on SD card");
 
         try {
@@ -808,11 +1008,17 @@
      * run without permissions errors.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
     public void testInstallAndLaunchFLPermsAppOnSD()
-            throws IOException, InterruptedException {
+            throws IOException, InterruptedException, InstallException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test launching an app with location perms set, installed on SD card");
 
         try {
@@ -833,11 +1039,17 @@
      * run without permissions errors.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
     public void testInstallAndLaunchBTPermsAppOnSD()
-            throws IOException, InterruptedException {
+            throws IOException, InterruptedException, InstallException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test launching an app with bluetooth perms set, installed on SD card");
 
         try {
@@ -858,11 +1070,17 @@
      * SecurityException when launched if its other shared apps are not installed.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
     public void testInstallAndLaunchSharedPermsAppOnSD_NoPerms()
-            throws IOException, InterruptedException {
+            throws IOException, InterruptedException, InstallException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test launching an app with no explicit perms set, installed on SD card");
 
         try {
@@ -888,11 +1106,17 @@
      * shared apps are installed.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
     public void testInstallAndLaunchSharedPermsAppOnSD_GrantedPerms()
-            throws IOException, InterruptedException {
+            throws IOException, InterruptedException, InstallException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test launching an app with no explicit perms set, installed on SD card");
 
         try {
@@ -921,11 +1145,17 @@
      * run without permissions errors even after a reboot
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
     public void testInstallAndLaunchFLPermsAppOnSD_Reboot()
-            throws IOException, InterruptedException {
+            throws IOException, InterruptedException, InstallException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test launching an app with location perms set, installed on SD card");
 
         try {
@@ -951,11 +1181,17 @@
      * shared apps are installed, even after a reboot.
      * <p/>
      * Assumes adb is running as root in device under test.
-     * @throws IOException if adb shell command failed
      * @throws InterruptedException if the thread was interrupted
+     * @throws TimeoutException in case of a timeout on the connection.
+     * @throws AdbCommandRejectedException if adb rejects the command
+     * @throws ShellCommandUnresponsiveException if the device did not output anything for
+     * a period longer than the max time to output.
+     * @throws IOException if connection to device was lost.
+     * @throws InstallException if the install failed.
      */
     public void testInstallAndLaunchSharedPermsAppOnSD_Reboot()
-            throws IOException, InterruptedException {
+            throws IOException, InterruptedException, InstallException, TimeoutException,
+            AdbCommandRejectedException, ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test launching an app on SD, with no explicit perms set after reboot");
 
         try {
diff --git a/core/tests/hosttests/src/android/content/pm/PackageManagerStressHostTests.java b/core/tests/hosttests/src/android/content/pm/PackageManagerStressHostTests.java
index 715c55b..a2a5dd3 100644
--- a/core/tests/hosttests/src/android/content/pm/PackageManagerStressHostTests.java
+++ b/core/tests/hosttests/src/android/content/pm/PackageManagerStressHostTests.java
@@ -16,8 +16,11 @@
 
 package android.content.pm;
 
-import com.android.ddmlib.IDevice;
+import com.android.ddmlib.AdbCommandRejectedException;
+import com.android.ddmlib.InstallException;
 import com.android.ddmlib.Log;
+import com.android.ddmlib.ShellCommandUnresponsiveException;
+import com.android.ddmlib.TimeoutException;
 import com.android.hosttest.DeviceTestCase;
 import com.android.hosttest.DeviceTestSuite;
 
@@ -138,7 +141,9 @@
      * <p/>
      * Assumes adb is running as root in device under test.
      */
-    public void testUpdateAppManyTimesOnSD() throws IOException, InterruptedException {
+    public void testUpdateAppManyTimesOnSD() throws IOException, InterruptedException,
+            InstallException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test updating an app on SD numerous times");
 
         // cleanup test app just in case it already exists
@@ -173,7 +178,9 @@
      * <p/>
      * Assumes adb is running as root in device under test.
      */
-    public void testUninstallReinstallAppOnSDManyTimes() throws IOException, InterruptedException {
+    public void testUninstallReinstallAppOnSDManyTimes() throws IOException, InterruptedException,
+            InstallException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test updating an app on the SD card stays on the SD card");
 
         // cleanup test app just in case it was already exists
@@ -207,7 +214,9 @@
      * <p/>
      * Assumes adb is running as root in device under test.
      */
-    public void testInstallManyLargeAppsOnSD() throws IOException, InterruptedException {
+    public void testInstallManyLargeAppsOnSD() throws IOException, InterruptedException,
+            InstallException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test installing 20 large apps onto the sd card");
 
         try {
@@ -251,7 +260,9 @@
      * <p/>
      * Assumes adb is running as root in device under test.
      */
-    public void testInstallManyAppsOnSD() throws IOException, InterruptedException {
+    public void testInstallManyAppsOnSD() throws IOException, InterruptedException,
+            InstallException, TimeoutException, AdbCommandRejectedException,
+            ShellCommandUnresponsiveException {
         Log.i(LOG_TAG, "Test installing 500 small apps onto SD");
 
         try {
diff --git a/docs/html/guide/guide_toc.cs b/docs/html/guide/guide_toc.cs
index 19f0f1d..d0318cf 100644
--- a/docs/html/guide/guide_toc.cs
+++ b/docs/html/guide/guide_toc.cs
@@ -371,6 +371,9 @@
             <span class="zh-CN" style="display:none">应用程序版本控制</span>
             <span class="zh-TW" style="display:none">應用程式版本設定</span>
           </a></li>
+      <li><a href="<?cs var:toroot ?>guide/publishing/licensing.html">
+            <span class="en">Licensing Your Applications</span>
+          </a> <span class="new">new!</span></li>
       <li><a href="<?cs var:toroot ?>guide/publishing/preparing.html">
             <span class="en">Preparing to Publish</span>
             <span class="de" style="display:none">Vorbereitung auf die Veröffentlichung</span>
diff --git a/docs/html/guide/publishing/licensing.jd b/docs/html/guide/publishing/licensing.jd
new file mode 100644
index 0000000..e3b9135
--- /dev/null
+++ b/docs/html/guide/publishing/licensing.jd
@@ -0,0 +1,2373 @@
+page.title=Licensing Your Applications
+@jd:body
+
+<div id="qv-wrapper">
+<div id="qv">
+
+  <h2>Market Licensing quickview: </h2>
+  <ul>
+    <li>Licensing lets you protect your application on any device that includes Android Market.</li>
+    <li>Your app maintains control of how it enforces its licensing status. </li>
+    <li>Adding licensing to an app is straightforward, using the library available through the SDK.</li>
+    <li>The service is free and is available to all developers who publish on Android Market. </li>
+  </ul>
+
+  <h2>In this document</h2>
+  <ol>
+    <li><a href="#account">Setting Up A Publisher Account</a></li>
+    <li><a href="#dev-setup">Setting Up the Development Environment</a></li>
+    <li><a href="#app-integration">Integrating the LVL with Your Application</a>
+    <ol>
+       <li><a href="#add-library">Including the LVL</a></li>
+       <li><a href="#manifest-permission">Adding the licensing permission</a></li>
+       <li><a href="#impl-Policy">Implementing a Policy</a></li>
+       <li><a href="#impl-Obfuscator">Implementing an Obfuscator</a></li>
+       <li><a href="#impl-lc">Checking the license</a></li>
+       <li><a href="#impl-DeviceLimiter">Implementing a DeviceLimiter</a></li>
+    </ol></li>
+    <li><a href="#test-env">Setting Up the Test Environment</a>
+    <ol>
+       <li><a href="#test-response">Test responses</a></li>
+       <li><a href="#test-acct-setup">Test accounts</a></li>
+       <li><a href="#acct-signin">Signing in on a device or emulator</a></li>
+    </ol></li>
+    <li><a href="#app-obfuscation">Obfuscating Your Application</a></li>
+    <li><a href="#app-publishing">Publishing a Licensed Application</a></li>
+    <li><a href="#support">Where to Get Support</a></li>
+  </ol>
+
+  <h2>Appendix</h2>
+  <ol>
+    <li><a href="#lvl-summary">Summary of LVL Classes and Interfaces</a></li>
+    <li><a href="#server-response-codes">Server Response Codes</a></li>
+    <li><a href="#extras">Server Response Extras</a></li>
+  </ol>
+
+</div>
+</div>
+
+<p>Android Market offers a licensing service that lets you enforce licensing
+policies for paid applications that you publish through Android Market. With
+Android Market Licensing, your applications can query Android Market at run time to
+obtain their licensing status for the current user, then allow or disallow
+further use as appropriate. </p>
+
+<p>Using the service, you can apply a flexible licensing policy on an
+application-by-application basis &mdash; each application can enforce licensing
+in the way most appropriate for it. If necessary, an application can apply custom
+constraints based on the licensing status obtained from Android Market.
+For example, an application can check the licensing status and then apply custom
+constraints that allow the user to run it unlicensed for a specific number
+of times, or for a specific validity period. An application can also restrict use of the
+application to a specific device, in addition to any other constraints. </p>
+
+<p>The licensing service is a secure means of controlling access to your
+applications. When an application checks the licensing status, the Market server
+signs the licensing status response using a key pair that is uniquely associated
+with the publisher account. Your application stores the public key in its
+compiled <code>.apk</code> file and uses it to verify the licensing status
+response.</p>
+
+<p>Any application that you publish through Android Market can use the Android
+Market Licensing service. No special account or registration is needed.
+Additionally, because the service uses no dedicated framework APIs, you can add
+licensing to any legacy application that uses a minimum API level of 3 or
+higher.</p>
+
+<p>To help you add licensing to your application, the Android SDK provides
+library sources that you can include in your application project. The
+License Verification Library (LVL) handles all of
+the licensing-related communication with the Android Market client and the
+licensing service. With the LVL integrated, your application can determine its
+licensing status for the current user by simply calling a library checker method
+and implementing a callback that receives the status.</p>
+
+<p>This document explains how the licensing service works and how to add it to
+your application. </p>
+
+
+<h2 id="overview">Overview</h2>
+
+<p>Android Market Licensing is a network-based service that lets an application
+on an Android-powered device query a trusted licensing server, to determine
+whether the application is licensed to the current device user. After receiving
+the server response, the application can then allow or disallow further use of
+the application as needed. In the service, the role of the licensing server is
+to provide the license status for the current user; the application itself is
+responsible for querying the server and conditionally granting access to the
+application. </p>
+
+<h4>Application, Android Market client, and server</h4>
+
+<p>The licensing service is based on the capability of the Android Market server
+to determine whether a given user is licensed to use a given application. The
+server considers a user licensed if the user is recorded to have purchased the
+application, or if the application is available for free. To properly identify
+the user and determine the license status, the server requires information about
+the application and user &mdash; the application and the Android Market client
+work together to assemble the information and pass it to the server. </p>
+
+<p>In the licensing service, an application does not query the licensing server
+directly, but instead calls the Android Market client over remote IPC to
+initiate a license request. In the license request:</p>
+
+<ul>
+<li>The application provides its package name and a nonce that is later used to
+validate any response from the server, as well as a callback over which the
+response can be returned asynchronously.</li>
+<li>The Android Market client, which has greater permissions than the
+application, collects the necessary information about the user and the device,
+such as the device's primary Google account username, IMSI, and other
+information. It then sends the license check request to the server on behalf of
+the application.</li>
+<li>The server evaluates the request using all available information, attempting
+to establish the user's identity to a sufficient level of confidence. The server
+then checks the user identity against purchase records for the application and
+returns a license response, which the Android Market client returns to the
+application over the IPC callback.</li>
+</ul>
+
+<p>Notice that during a license check, the application does not manage any
+network connections or use any licensing related APIs in the Android platform.
+</p>
+
+<div class="figure" style="width:469px">
+<img src="{@docRoot}images/licensing_arch.png" alt=""/>
+<p class="img-caption"><strong>Figure 1.</strong> Your application initiates a
+license check through the LVL and the Android Market
+client, which handles communication with the Market server.</p>
+</div>
+
+<h4>License responses secured through public key cryptography</h4>
+
+<p>To ensure the integrity of each license query, the server signs the license
+response data using an RSA key pair that is shared exclusively between the
+server and the application publisher.</p>
+
+<p>The licensing service generates a single licensing key pair for each
+publisher account and exposes the public key in the account's profile page. The
+publisher copies the public key and embeds it in the application source code,
+then compiles and publishes the <code>.apk.</code> The server retains the
+private key internally and uses it to sign license responses for applications
+published on that account. </p>
+
+<p>When the application receives a signed response, it uses the embedded public
+key to verify the data. The use of public key cryptography in the licensing
+service makes it possible for the application to detect responses that have been
+tampered with or are spoofed.</p>
+
+<h4>Use of licensing in your application</h4>
+
+<p>To use licensing in your application, add code to the application to
+initiate a license check request and handle the response when it is received.
+You can choose when, and how often, you want your application to check its
+license and you have full control over how it handles the response, verifies the
+signed response data, and enforces access controls. </p>
+
+<p>To simplify the process of adding support for licensing, download and
+integrate the Licensing Verification Library, described below. Integration is
+straightforward.</p>
+
+<p>When you are finished integrating the LVL, use a test environment
+provided by the publisher site to test your application's handling of server
+responses. </p>
+
+<p>Finally, publish the application <code>.apk</code> on Market using the
+normal process. If you previously used the copy-protection provided by Android
+Market, you can remove it from applications that use licensing. </p>
+
+<h4>Licensing Verification Library simplifies implementation</h4>
+
+<p>The Android SDK includes a License Verification Library (LVL) that you can
+download and use as the basis for your application's licensing implementation.
+The LVL greatly simplifies the process of adding licensing to your application
+and helps ensure a more secure, robust implementation for your application. The
+LVL provides internal classes that handle most of the standard operations of a
+license query, such as contacting Android Market to initiate a license request
+and decrypting and validating the responses. It also exposes key interfaces that
+let you easily plug in your custom code for defining licensing policy and
+managing access as needed by your application. The key LVL interfaces are: </p>
+
+<ul>
+<li>Policy &mdash; your implementation determines whether to allow access to the
+application, based on the license response received from the server and any
+other data available (such as from a backend server associated with your
+application). The implementation can evaluate the various fields of the license
+response and apply other constraints, if needed. The implementation also lets
+you manage the handling of license checks that result in errors, such as network
+errors.</li>
+<li>LicenseCheckerCallback &mdash; your implementation manages access to the
+application, based on the result of the Policy's handling of the license
+response. Your implementation can manage access in any way needed, including
+displaying the license result in the UI or directing the user to purchase the
+application (if not currently licensed). </li>
+</ul>
+
+<p>To help you get started with a Policy, the LVL provides two fully complete
+Policy implementations that you can use without modification or adapt to your
+needs:</p>
+
+<ul>
+<li><a href="#ServerManagedPolicy">ServerManagedPolicy</a> is a flexible Policy
+that uses settings provided by the licensing server to manage response caching
+and access to the application while the device is offline (such as when the
+user is on on an airplane). For most applications, the use of
+ServerManagedPolicy is highly recommended. </li>
+<li><a href="#StrictPolicy">StrictPolicy</a> is a restrictive Policy that
+does not cache any response data and allows the application access <em>only</em>
+when the server returns a licensed response.</li>
+</ul>
+
+<p>The LVL is available as a downloadable component of the Android SDK. The
+component includes both the LVL itself and an example application that shows how
+the library should be integrated with your application and how your application
+should manage response data, UI interaction, and error conditions. </p>
+
+<p>The LVL sources are provided as an Android <em>library project</em>, which
+means that you can maintain a single set of library sources and share them
+across multiple applications. A full test environment is also available through
+the SDK, so you can develop and test the licensing implementation in your
+applications before publishing them, even if you don't have access to a
+physical device.</p>
+
+<h4>Requirements and limitations</h4>
+
+<p>Android Market Licensing is designed to let you apply license controls to
+applications that you publish through Android Market and that users download
+from Market. The service is not designed to let you control access to
+applications that are not published through Android Market or that are run
+on devices that do not offer the Android Market client. </p>
+
+<p>Here are some points to keep in mind as you implement licensing in your
+application: </p>
+
+<ul>
+<li>Only paid applications published through Market can use the
+service. </li>
+<li>An application can use the service only if the Android Market client is
+installed on its host device and the device is running Android 1.5 (API level 3)
+or higher.</li>
+<li>To complete a license check, the licensing server must be accessible over
+the network. You can implement license caching behaviors to manage access when
+there is no network connectivity. </li>
+<li>The security of your application's licensing controls ultimately relies on
+the design of your implementation itself. The service provides the building
+blocks that let you securely check licensing, but the actual enforcement and
+handling of the license are factors in your control. By following the best
+practices in this document, you can help ensure that your implementation will be
+secure.</li>
+<li>Adding licensing to an application does not affect the way the application
+functions when run on a device that does not offer Android Market.</li>
+<li>Licensing is currently for paid apps only, since free apps are considered
+licensed for all users. If your application is already published as free, 
+you won't be able to upload a new version that uses licensing.</li>
+</ul>
+
+<h4>Replacement for copy protection</h4>
+
+<p>Android Market Licensing is a flexible, secure mechanism for controlling
+access to your applications. It effectively replaces the copy-protection
+mechanism and gives you wider distribution potential for your applications. </p>
+
+<ul>
+<li>A limitation of copy protection is that applications using it can be
+installed only on compatible devices that provide a secure internal storage
+environment. For example, a copy-protected application cannot be downloaded
+from Market to a device that provides root access, and the application
+cannot be installed to a device's SD card. </li>
+<li>With Android Market licensing, you can move to a license-based model in
+which access is not bound to the characteristics of the host device, but to your
+publisher account on Android Market and the licensing policy that you define.
+Your application can be installed and controlled on any compatible device on
+any storage, including SD card.</li>
+</ul>
+
+<p>Although no license mechanism can completely prevent all unauthorized use,
+the licensing service lets you control access for most types of normal usage,
+across all compatible devices, locked or unlocked, that run Android 1.5 or
+higher version of the platform.</p>
+
+<p>The sections below describe how to add Android Market licensing to your
+applications. </p>
+
+<h2 id="account">Setting Up a Publisher Account</h2>
+
+<p>Android Market licensing lets you manage access to applications that
+users have downloaded from Android Market. To use licensing in an application,
+you need to have a publisher account on Android Market so that you can
+publish the application to users. </p>
+
+<p>If you don't already have a publisher account, you need to register for one
+using your Google account and agree to the terms of service. Once you are
+registered, you can upload applications at your convenience and begin debugging
+and testing your licensing implementation. For more information about publishing
+on Android Market, see <a
+href="{@docRoot}guide/publishing/publishing.html">Publishing Your
+Applications</a></p>
+
+<p>To register as an Android Market developer and set up your publisher account,
+visit the Android Market publisher site:</p>
+
+<p style="margin-left:2em;"><a
+href="http://market.android.com/publish">http://market.android.com/publish</a>
+</p>
+
+<p>If you already have a publisher account on Android Market, use your existing
+account to set up licensing. You <em>do not</em> need to register for a new
+account to support licensing (and doing so is not recommended, especially if you
+are adding licensing support to applications that you have already published).
+In all cases, if you have published applications, you manage licensing for those
+applications through the account on which the applications are published. </p>
+
+<p>Once your publisher account is set up, use the account to:</p>
+
+<ul>
+<li>Obtain a public key for licensing</li>
+<li>Debug and test an application's licensing implementation, prior to
+publishing the application</li>
+<li>Publish the applications to which you have added licensing support</li>
+</ul>
+
+<h4>Administrative settings for licensing</h4>
+
+<p>Once you are signed into your publisher account, you can manage several
+administrative controls for Android Market licensing. The controls are available
+in the Edit Profile page, in the "Licensing" panel, shown below. The controls
+let you: </p>
+
+<ul>
+<li>Set up multiple "test accounts", identified by email address. The licensing
+server allows users signed into test accounts on a device or emulator to send
+license checks and receive static test reponses.</li>
+<li>Obtain the account's public key for licensing. When you are implementing
+licensing in an application, you must copy the public key string into the
+application.</li>
+<li>Configure static test responses that the server sends, when it receives a
+license check for an application uploaded to the publisher account, from a user
+signed in to the publisher account or a test account.</li>
+</ul>
+
+<div style="margin-bottom:2em;">
+
+<img src="{@docRoot}images/licensing_public_key.png" style="text-align:left;margin-bottom:0;" />
+<div style="margin:0 2em;padding:0"><strong>Figure 2.</strong> The Licensing
+panel of your account's Edit Profile page lets you manage administrative
+settings for licensing.</div>
+</div>
+
+<p>For more information about how to work with test accounts and static test
+responses, see <a href="#test-env">Setting Up a Testing Environment</a>, below.
+
+<h2 id="dev-setup">Setting Up the Development Environment</h2>
+
+<p>Once you've set up your publisher account on Android Market, the next step is
+to set up your development environment for licensing. </p>
+
+<p>Setting up your environment for licensing involves these tasks:</p>
+
+<ol>
+<li><a href="#download-sdk">Downloading the latest SDK</a>, if you haven't already done so </li>
+<li><a href="#runtime-setup">Setting up the runtime environment</a> for development</a></li>
+<li><a href="#download-lvl">Downloading the Market Licensing component</a> into your SDK </li>
+<li><a href="#lvl-setup">Setting up the Licensing Verification Library</a></li>
+<li><a href="#add-library">Including the LVL library project in your application</a></li>
+</ol>
+
+<p>The sections below describe these tasks. When you are done with setup,
+you can begin <a href="#app-integration">integrating the LVL into your applications</a>.</p>
+
+<p>To get started, you need to set up a proper runtime environment on which
+you can run, debug and test your application's implementation of license
+checking and enforcement. </p>
+
+
+<h3 id="download-sdk">Downloading the latest SDK</h3>
+
+<div class="sidebox-wrapper">
+<div class="sidebox">
+<h2>Licensing sample application</h2>
+
+<p>To work with Android Market licensing, you need a functioning Android
+application to which you can add licensing support. </p>
+
+<p style="margin-top:.5em;">If you are new to Android
+and don't yet have a functioning application, the LVL component includes a sample
+application that you can set up as a new application project. The sample provides
+a complete, working example of how licensing works. For more information, see <a
+href="#download-lvl">Downloading the LVL</a>.</p>
+
+</div>
+</div>
+
+<p>If you haven't done so, you need to download the Android SDK before you can
+develop Android applications. The SDK provides the tools that you need to build
+and debug Android applications, including applications that use Android Market
+licensing. For complete information, including installation intructions, see
+the <a href="{@docRoot}sdk/index.html">Android SDK</a>. </p>
+
+<p>If you have already installed the SDK, make sure to update the
+SDK tools and ADT Plugin to the latest versions. You can update the SDK tools
+using the Android SDK and AVD Manager and ADT through <strong>Help</strong> &gt;
+<strong>Software Updates...</strong> in Eclipse. </p>
+
+<p>After you've installed the latest SDK and tools, set up your development
+environment as described below. </p>
+
+
+<h3 id="runtime-setup">Setting up the runtime environment</h3>
+
+<p>As described earlier, applications check licensing status not by contacting
+the licensing server directly, but by binding to a service provided by the
+Android Market application and initiating a license check request. The Android
+Market service then handles the direct communication with the licensing server
+and finally routes the response back to your application. To debug and test
+licensing in your application, you need to set up a runtime environment that
+includes the necessary Android Market service, so that your application is able
+to send license check requests to the licensing server. </p>
+
+<p>There are two types of runtime environment that you can use: </p>
+
+<ul>
+<li>An Android-powered device that includes the Android Market application, or</li>
+<li>An Android emulator running the Google APIs Add-on, API level 8 (release 2)
+or higher</li>
+</ul>
+
+<p>The sections below provide more information. </p>
+
+<h4 id="runtime-device">Running on a device</h4>
+
+<p>You can use an Android-powered device as the runtime environment for
+debugging and testing licensing on your application.</p>
+
+<p>The device you use must:</p>
+
+<ul>
+<li>Run a standard version of the Android 1.5 or later (API level
+3 or higher) platform, <em>and</em> </li>
+<li>Run a system image on which the Android Market client application
+is preinstalled. </li>
+</ul>
+
+<p>If Android Market is not preinstalled in the system image, your application won't
+be able to communicate with the Android Market licensing server. </p>
+
+<p>For general information about how to set up a device for use in developing
+Android applications, see <a
+href="{@docRoot}guide/developing/device.html">Developing on a Device</a>.</p>
+
+<h4 id="runtime-emulator">Running on an Android emulator</h4>
+
+<p>You can also use an Android emulator as your runtime
+environment for debugging and testing licensing.</p>
+
+<p>Because the standard Android platforms provided in the Android SDK <em>do
+not</em> include Android Market, you need to download the Google APIs Add-On
+platform, API Level 8 (or higher), from the SDK repository. After downloading
+the add-on, you need to create an AVD configuration that uses that system image.
+</p>
+
+<p>The Google APIs Add-On does not include the full Android Market client.
+However, it does provide: </p>
+
+<ul>
+<li>An Android Market background service that implements the
+<code>ILicensingService</code> remote interface, so that your application can
+send license checks over the network to the licensing server. </li>
+<li>A set of underlying account services that let you add an a Google account on
+the AVD and sign in using your publisher account or test account credentials.
+Signing in using your publisher or test account enables you to debug and test
+your application without having publish it. For more information see <a
+href="#acct-signin">Signing in to an authorized account</a>, below.</li>
+</ul>
+
+<p>Several versions of the add-on are available in the SDK repository, but only
+<strong>Google APIs Add-On, API 8 (release 2) or higher</strong> version of the
+add-on includes the necessary Android Market services. This means that you
+cannot use Google APIs Add-On API 7 or lower as a runtime environment for
+developing licensing on an emulator.</p>
+
+<div style="margin-bottom:2em;">
+
+<img src="{@docRoot}images/licensing_gapis_8.png" style="text-align:left;margin-bottom:0;" />
+<div style="margin:0 2em;padding:0"><strong>Figure 3.</strong> Google APIs
+Add-On, API 8 (release 2) or higher lets you debug and test your licensing
+implemention in an emulator.</div>
+</div>
+
+<p>To set up an emulator for adding licensing to an application, follow
+these steps: </p>
+
+<ol>
+  <li>Launch the Android SDK and AVD Manager. </li>
+  <li>In the <strong>Available Packages</strong> panel, select and download the
+SDK component "Google APIs (Google Inc.) - API Level 8" (or higher) from the SDK
+repository, as shown in the figure above.
+  <p>When the download is complete, use the Android SDK and AVD Manager to
+create a new AVD based on that component, described next.</p></li>
+  <li>In the <strong>Virtual
+Devices</strong> panel of the Android SDK and AVD Manager, click
+<strong>New</strong> and set the configuration details for the new AVD. </li>
+  <li>In the dialog that appears, assign a descriptive name to the AVD and then
+use the "Target" menu to choose the "Google APIs (Google Inc.) - API Level 8" as
+the system image to run on the new AVD. Set the other configuration details as
+needed and then click <strong>Create AVD</strong> to finish. The SDK tools
+create the new AVD configuration, which then appears in the list of available
+Android Virtual Devices.</li>
+</ol>
+
+<p>If you are not familiar with AVDs or how to use them, see <a
+href="{@docRoot}guide/developing/tools/avd.html">Android Virtual Devices</a>.</p>
+
+<h4 id="project-update">Updating your project configuration</h4>
+
+<p>After you set up a runtime environment that meets the requirements described
+above &mdash; either on an actual device or on an emulator &mdash; make sure to
+update your application project or build scripts as needed, so that your compiled
+<code>.apk</code> files that use licensing are deployed into that environment.
+In particular, if you are developing in Eclipse, make sure that you set up a
+Run/Debug Configuration that targets the appropriate device or AVD. </p>
+
+<p>You do not need to make any changes to your application's
+build configuration, provided that the project is already configured to compile
+against a standard Android 1.5 (API level 3) or higher library. For example:
+
+<ul>
+<li>If you have an existing application that is compiled against
+the Android 1.5 library, you do not need to make any changes to your
+build configuration to support licensing. The build target meets the minimum
+requirements for licensing, so you would continue building
+against the same version of the Android platform.</li>
+
+<li>Similarly, if you are building against Android 1.5 (API level 3) but
+are using an emulator running the Google APIs Add-On API 8 as the application's
+runtime environment, there is no need to change your application's build
+configuration. </li>
+</ul>
+
+<p>In general, adding licensing to an application should have no impact
+whatsoever on the application's build configuration.</p>
+
+
+<h3 id="download-lvl">Downloading the LVL</h3>
+
+<p>The License Verification Library (LVL) is a collection of helper classes that
+greatly simplify the work that you need to do to add licensing to your
+application. In all cases, we recommend that you download the LVL and use it as
+the basis for the licensing implementation in your application.</p>
+
+<p>The LVL is available as a downloadable component of the Android SDK. The
+component includes: </p>
+
+<ul>
+<li>The LVL sources, stored inside an Android library project. </li>
+<li>An example application called "sample" that depends on the LVL library
+project. The example illustrates how an application uses the library helper
+classes to check and enforce licensing.</li>
+</ul>
+
+<p>To download the LVL component into your development environment, use the
+Android SDK and AVD Manager. Launch the Android SDK and AVD Manager and then
+select the "Market Licensing" component, as shown in the figure below.
+Accept the terms and click <strong>Install Selected</strong> to begin the download. </p>
+
+<div style="margin-bottom:2em;">
+
+<img src="{@docRoot}images/licensing_package.png" style="text-align:left;margin-bottom:0;" />
+<div style="margin:0 2em;padding:0"><strong>Figure 4.</strong> The Market
+Licensing package contains the LVL and the LVL sample application. </div>
+</div>
+
+<p>When the download is complete, the Android SDK and AVD Manager installs both
+the LVL library project and the example application into these directories: </p>
+
+<p style="margin-left:2em"><code>&lt;<em>sdk</em>&gt;/marketlicensing/library/</code>
+&nbsp;&nbsp;(the LVL library project)<br />
+<code>&lt;<em>sdk</em>&gt;/marketlicensing/sample/</code>&nbsp;&nbsp;(the example
+application)</p>
+
+<p>If you aren't familiar with how to download components into your SDK, see the
+<a href="{@docRoot}sdk/adding-components.html">Adding SDK Components</a>
+document. </p>
+
+
+<h3 id="lvl-setup">Setting Up the Licensing Verification Library</h3>
+
+<p>After downloading the LVL to your computer, you need to set it up in your
+development environment, either as an Android library project or by
+copying (or importing) the library sources directly into your existing
+application package. In general, using the LVL as a library project is recommended,
+since it lets you reuse your licensing code across multiple applications and
+maintain it more easily over time. Note that the LVL is not designed to be
+compiled separately and added to an application as a static .jar file. </p>
+
+<h4>Moving the library sources out  location</h4>
+
+<p>Because you will be customizing the LVL sources to some extent, you should
+make sure to <em>move or copy</em> the library sources (the entire
+directory at <code>&lt;<em>sdk</em>&gt;/marketlicensing/library/</code>)
+to a working directory outside of the SDK. You can then add the sources
+in the working location to your source-code management system, rather
+than those in the SDK.</p>
+
+<p>Moving the library sources is important is because, when you later update the
+Market licensing package, the SDK installs the new files to the same location as
+the older files. Moving your working library files to a safe location ensures
+that they won't be inadvertently overwritten when you download a new version of
+the LVL.</p>
+
+<h4>Creating the LVL as a library project</h4>
+
+<div class="sidebox-wrapper">
+<div class="sidebox">
+<h2>Working with library projects</h2>
+
+<p>The LVL is provided as an Android library project, which means that you can
+share its code and resources across multiple applications. </p>
+
+<p style="margin-top:.5em;">If you aren't familiar with library projects or how
+to use them, read more in <a
+href="{@docRoot}guide/developing/eclipse-adt.html#libraryProject">Developing in
+Eclipse with ADT</a> or <a
+href="{@docRoot}guide/developing/other-ide.html#libraryProject">Developing in
+Other IDEs</a>, as appropriate for your environment.</p>
+
+</div>
+</div>
+
+<p>The recommended way of using the LVL is setting it up as a new Android
+<em>library project</em>. A library project is a type of development project
+that holds shared Android source code and resources. Other Android application
+projects can reference the library project and, at build time, include its
+compiled sources in their <code>.apk</code> files. In the context of licensing, this means
+that you can do most of your licensing development once, in a library project,
+then include the library sources in your various application projects. In this
+way, you can easily maintain a uniform implementation of licensing across all of
+your projects and maintain it centrally. </p>
+
+<p>The LVL is provided as a configured library project &mdash; once you have
+downloaded it, you can start using it right away. </p>
+
+<p>If you are working in Eclipse with ADT, you need to add the LVL to your
+workspace as a new development project, in the same way as you would a new
+application project. </p>
+
+<ol>
+<li>Use the New Project Wizard to create a new
+project from existing sources. Select the LVL's <code>library</code> directory
+(the directory containing the library's AndroidManifest.xml file) as the project
+root.</li>
+<li>When you are creating the library project, you can select any application
+name, package, and set other fields as needed. </li>
+<li>For the library's build target, select Android 1.5 (API level 3) or higher.</li>
+</ol>
+
+<p> When created, the project is
+predefined as a library project in its <code>default.properties</code> file, so
+no further configuration is needed. </p>
+
+<p>For more information about how to create an application project or work with
+library projects in Eclipse, see <a
+href="{@docRoot}guide/developing/eclipse-adt.html#CreatingAProject">Developing
+in Eclipse with ADT</a>.</p>
+
+<h4>Copying the LVL sources to your application</h4>
+
+<p>As an alternative to adding the LVL as a library project, you can copy the
+library sources directly into your application. To do so, copy (or import) the
+directory
+<code>&lt;<em>sdk</em>&gt;/extras/marketlicensing/library/src/com</code> into
+your application's <code>src/</code> directory.</p>
+
+<p>If you add the LVL sources directly to your application, you can skip the
+next section and start working with the library, as described in <a
+href="#app-integration"></a>.</p>
+
+
+<h3 id="add-library">Including the LVL library project sources in your
+application</h3>
+
+<p>If you want to use the LVL sources as a library project, you need to add a
+reference to the LVL library project in your project properties. This tells
+build tools to include the LVL library project sources in your application at
+compile time. The process for adding a reference to a library project varies,
+based on your development environment, as described below.</p>
+
+<p> If you are developing in Eclipse with ADT, you should already have added the
+library project to your workspace, as described in the previous section. If you
+haven't done that already, do it now before continuing. </p>
+
+<p>Next, open the application's project properties window, as shown below.
+Select the "Android" properties group and click <strong>Add</strong>, then
+choose the LVL library project (com_android_vending_licensing) and click
+<strong>OK</strong>. For more information, see
+<a href="{@docRoot}guide/developing/eclipse-adt.html#libraryProject">Developing
+in Eclipse with ADT</a></p>
+
+<div style="margin-bottom:2em;">
+
+<img src="{@docRoot}images/licensing_add_library.png" style="text-align:left;margin-bottom:0;" />
+<div style="margin:0 2em;padding:0"><strong>Figure 5.</strong> If you are
+working in Eclipse with ADT, you can add the LVL library project to your
+application from the application's project properties.</div>
+</div>
+
+<p>If you are developing using the SDK command-line tools, navigate to the
+directory containing your application project and open the
+<code>default.properties</code> file. Add a line to the file that specifies the
+<code>android.library.reference.&lt;n&gt;</code> key and the path to the
+library. For example: </p>
+
+<pre>android.library.reference.1=path/to/library_project</pre>
+
+<p>Alternatively, you can use this command to update the project
+properties, including the reference to the library project:</p>
+
+<pre class="no-pretty-print" style="color:black">android update lib-project
+--target <em>&lt;target_ID&gt;</em> \
+--path <em>path/to/my/app_project</em> \
+--library <em>path/to/my/library_project</em>
+</pre>
+
+<p>For more information about working with library projects, see <a
+href="{@docRoot}guide/developing/eclipse-adt.html#libraryProject">Developing
+in Eclipse with ADT</a> or <a
+href="{@docRoot}guide/developing/other-ide.html#libraryProject">Developing in
+Other IDEs</a>, as appropriate for your environment.</p>
+
+
+<h2 id="app-integration">Integrating the LVL with Your Application</h2>
+
+<p>Once you've followed the steps above to set up a publisher account and
+development environment, you are ready to begin integrating the LVL with your
+application. </p>
+
+<p>Integrating the LVL with your application code involves these tasks:</p>
+
+<ol>
+<li><a href="#manifest-permission">Adding the licensing permission</a> your application's manifest.</li>
+<li><a href="#impl-Policy">Implementing a Policy</a> &mdash; you can choose one of the full implementations provided in the LVL or create your own.</li>
+<li><a href="#impl-Obfuscator">Implementing an Obfuscator</a>, if your Policy will cache any license response data. </li>
+<li><a href="#impl-lc">Adding code to check the license</a> in your application's main Activity</li>
+<li><a href="#impl-DeviceLimiter">Implementing a DeviceLimiter</a> (optional and not recommended for most applications)</li>
+</ol>
+
+<p>The sections below describe these tasks. When you are done with the
+integration, you should be able to compile your application successfully and you
+can begin testing, as described in <a href="#test-env">Setting Up the Test
+Environment</a>.</p>
+
+<p>For an overview of the full set of source files included in the LVL, see <a
+href="#lvl-summary">Summary of LVL Classes and Interfaces</a>.</p>
+
+
+<h3 id="manifest-permission">Adding the licensing permission to your
+AndroidManifest.xml</h3>
+
+<p>To use the Android Market application for sending a license check to the
+server, your application must request the proper permission,
+<code>com.android.vending.CHECK_LICENSE</code>. If your application does
+not declare the licensing permission but attempts to initiate a license check,
+the LVL throws a security exception.</p>
+
+<p>To request the licensing permission in your application, declare a <a
+href="{@docRoot}guide/topics/manifest/uses-permission-element.html"><code>&lt;uses-permission&gt;</code></a>
+element as a child of <code>&lt;manifest&gt;</code>, as follows: </p>
+
+<p style="margin-left:2em;"><code>&lt;uses-permission
+android:name="com.android.vending.CHECK_LICENSE"&gt;</code></p>
+
+<p>For example, here's how the LVL sample application declares the permission:
+</p>
+
+<pre>&lt;?xml version="1.0" encoding="utf-8"?&gt;
+
+&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" ..."&gt;
+    ...
+    &lt;!-- Devices &gt;= 3 have version of Android Market that supports licensing. --&gt;
+    &lt;uses-sdk android:minSdkVersion="3" /&gt;
+    &lt;!-- Required permission to check licensing. --&gt;
+    &lt;uses-permission android:name="com.android.vending.CHECK_LICENSE" /&gt;
+    ...
+&lt;/manifest&gt;
+</pre>
+
+<p class="note"><strong>Note:</strong> Currently, you cannot declare the
+<code>CHECK_LICENSE</code> permission in the LVL's manifest, because the SDK
+Tools will not merge it into the manifests of dependent applications. Instead,
+you must declare the permission in the manifest of each dependent application.
+</p>
+
+
+<h3 id="impl-Policy">Implementing a Policy</h3>
+
+<div class="sidebox-wrapper">
+<div class="sidebox">
+<h2>ServerManagedPolicy</h2>
+
+<p>The LVL includes a complete Policy implementation called ServerManagedPolicy
+that makes use of license-management settings provided by the Android Market
+server. </p>
+
+<p style="margin-top:.5em;">Use of ServerManagedPolicy as the basis for your
+Policy is strongly recommended. For more information, see <a
+href="#ServerManagedPolicy">ServerManagedPolicy</a> section, below.</p>
+
+</div>
+</div>
+
+<p>Android Market licensing service does not itself determine whether a
+given user with a given license should be granted access to your application.
+Rather, that responsibility is left to a Policy implementation that you provide
+in your application.</p>
+
+<p>Policy is an interface declared by the LVL that is designed to hold your
+application's logic for allowing or disallowing user access, based on the result
+of a license check. To use the LVL, your application <em>must</em> provide an
+implementation of Policy. </p>
+
+<p>The Policy interface declares two methods, <code>allowAccess()</code> and
+<code>processServerResponse()</code>, which are called by a LicenseChecker
+instance when processing a response from the license server. It also declares an
+enum called <code>LicenseResponse</code>, which specifies the license response
+value passed in calls to <code>processServerResponse()</code>. </p>
+
+<ul>
+<li><code>processServerResponse()</code> lets you preprocess the raw response
+data received from the licensing server, prior to determining whether to grant
+access.
+
+<p>A typical implementation would extract some or all fields from the license
+response and store the data locally to a persistent store, such as through
+{@link android.content.SharedPreferences} storage, to ensure that the data is
+accessible across application invocations and device power cycles. For example,
+a Policy would maintain the timestamp of last successful license check, the
+retry count, the license validity period, and similar information in a
+persistent store, rather than resetting the values each time the application is
+launched.</p>
+
+<p>When storing response data locally, the Policy must ensure that the data is
+obfuscated (see <a href="#impl-Obfuscator">Implementing an Obfuscator</a>,
+below).</p></li>
+
+<li><code>allowAccess()</code> determines whether to grant the user access to
+your application, based on any available license response data (from the
+licensing server or from cache) or other application-specific information.  For
+example, your implementation of <code>allowAccess()</code> could take into
+account additional criteria, such as usage or other data retrieved from a
+backend server. In all cases, an implementation of <code>allowAccess()</code>
+should only return <code>true</code> if there user is licensed to use the
+application, as determined by the licensing server, or if there is a transient
+network or system problem that prevents the license check from completing. In
+such cases, your implementation can maintain a count of retry responses and
+provisionally allow access until the next license check is complete.</li>
+
+</ul>
+
+<p>To simplify the process of adding licensing to your application and to
+provide an illustration of how a Policy should be designed, the LVL includes
+two full Policy implementations that you can use without modification:</p>
+
+<ul>
+<li><a href="#ServerManagedPolicy">ServerManagedPolicy</a>, a flexible Policy
+that uses server-provided settings and cached responses to manage access across
+varied network conditions, and</li>
+<li><a href="#StrictPolicy">StrictPolicy</a>, which does not cache any response
+data and allows access <em>only</em> if the server returns a licensed
+response.</li>
+</ul>
+
+<p>For most applications, the use of ServerManagedPolicy is highly
+recommended. ServerManagedPolicy is the LVL default and is integrated with
+the LVL sample application.</p>
+
+
+<h4 id="custom-policies">Guidelines for custom policies</h4>
+
+<p>In your licensing implementation, you can use one of the complete policies
+provided in the LVL (ServerManagedPolicy or StrictPolicy) or you can create a
+custom Policy. For any type of custom policy, there are several important design
+points to understand and account for in your implementation.</p>
+
+<p>The licensing server applies general request limits to guard against overuse
+of resources that could result in denial of service. When an application exceeds
+the request limit, the licensing server returns a 503 response, which gets
+passed through to your application as a general server error. This means that no
+license response will be available to the user until the limit is reset, which
+can affect the user for an indefinite period.</p>
+
+<p>If you are designing a custom Policy, we recommend that the Policy:
+<ol>
+<!-- <li>Limits the number of points at which your app calls for a license check
+to the minimum. </li> -->
+<li>Caches (and properly obfuscates) the most recent successful license response
+in local persistent storage.</li>
+<li>Returns the cached response for all license checks, for as long as the
+cached response is valid, rather than making a request to the licensing server.
+Setting the response validity accrording to the server-provided <code>VT</code>
+extra is highly recommended. See <a href="#extras">Server Response Extras</a>
+for more information.</li>
+<li>Uses an exponential backoff period if retrying any requests the result in
+errors. However, because the Android Market client automatically retries failed
+requests, the Policy does not need to do so, in most cases.</li>
+<li>Provides for a "grace period" that allows the user to access your
+application for a limited time or number of uses, while a license check is being
+retried. The grace period benefits the user by allowing access until the next
+license check can be completed successfully and it benefits you by placing a
+hard limit on access to your application when there is no valid license response
+available.</li>
+</ol>
+
+<p>Designing your Policy according to the guidelines listed above is critical,
+because it ensures the best possible experience for users while giving you
+effective control over your application even in error conditions. </p>
+
+<p>Note that any Policy can use settings provided by the licensing server to
+help manage validity and caching, retry grace period, and more. Extracting the
+server-provided settings is straightforward and making use of them is highly
+recommended. See the ServerManagedPolicy implementation for an example of how to
+extract and use the extras. For a list of server settings and information about
+how to use them, see  <a href="#extras">Server Response Extras</a> in the
+Appendix of this document.</p>
+
+<h4 id="ServerManagedPolicy">ServerManagedPolicy</h4>
+
+<div class="sidebox-wrapper">
+<div class="sidebox">
+<h2>Server Response Extras</h2>
+
+<p>For certain types of licensing responses, the licensing server appends extra
+settings to the responses, to help the application manage licensing effectively.
+</p>
+
+<p style="margin-top:.5em;">See <a href="#extras">Server Response Extras</a> for
+a list of settings and <code>ServerManagedPolicy.java</code> for information
+about how a Policy can usethe extras.</p>
+
+</div>
+</div>
+
+<p>The LVL includes a full and recommended implementation of the Policy
+interface called ServerManagedPolicy. The implementation is integrated with the
+LVL classes and serves as the default Policy in the library. </p>
+
+<p>ServerManagedPolicy provides all of the handling for license and retry
+responses. It caches all of the response data locally in a
+{@link android.content.SharedPreferences} file, obfuscating it with the
+application's Obfuscator implementation. This ensures that the license response
+data is secure and persists across device power cycles. ServerManagedPolicy
+provides concrete implementations of the interface methods
+<code>processServerResponse()</code> and <code>allowAccess()</code> and also
+includes a set of supporting methods and types for managing license
+responses.</p>
+
+<p>Importantly, a key feature of ServerMangedPolicy is its use of
+server-provided settings as the basis for managing licensing across an
+application's refund period and through varying network and error conditions.
+When an application contacts the Android Market server for a license check, the
+server appends several settings as key-value pairs in the license response
+extras field. For example, the server provides recommended values for the
+application's license validity period, retry grace period, and maximum allowable
+retry count, among others. ServerManagedPolicy extracts the values from the
+license response in its <code>processServerResponse()</code> method and checks
+them in its <code>allowAccess()</code> method. For a list of the server-provided
+settings used by ServerManagedPolicy, see <a href="#extras">Server Response
+Extras</a> in the Appendix of this document.</p>
+
+<p>For convenience, best performance, and the benefit of using license settings
+from the Android Market server, <strong>using ServerManagedPolicy as your
+licensing Policy is strongly recommended</strong>. </p>
+
+<p>If you are concerned about the security of license response data that is
+stored locally in SharedPreferences, you can use a stronger obfuscation
+algorithm or design a stricter Policy that does not store license data. The LVL
+includes an example of such a Policy &mdash; see <a
+href="#StrictPolicy">StrictPolicy</a> for more information.</p>
+
+<p>To use ServerManagedPolicy, simply import it to your Activity, create an
+instance, and pass a reference to the instance when constructing your
+LicenseChecker. See <a href="#lc-lcc">Instantiate LicenseChecker and
+LicenseCheckerCallback</a> for more information. </p>
+
+<h4 id="StrictPolicy">StrictPolicy</h4>
+
+<p>The LVL includes an alternative full implementation of the Policy interface
+called StrictPolicy. The StrictPolicy implementation provides a more restrictive
+Policy than ServerManagedPolicy, in that it does not allow the user to access
+the application unless a license response is received from the server at the
+time of access that indicates that the user is licensed.</p>
+
+<p>The principal feature of StrictPolicy is that it does not store <em>any</em>
+license response data locally, in a persistent store. Because no data is stored,
+retry requests are not tracked and cached responses can not be used to fulfill
+license checks. The Policy allows access only if:</p>
+
+<ul>
+<li>The license response is received from the licensing server, and </li>
+<li>The license response indicates that the user is licensed to access the
+application. </li>
+</ul>
+
+<p>Using StrictPolicy is appropriate if your primary concern is to ensure that,
+in all possible cases, no user will be allowed to access the application unless
+the user is confirmed to be licensed at the time of use. Additionally, the
+Policy offers slightly more security than ServerManagedPolicy &mdash; since
+there is no data cached locally, there is no way a malicious user could tamper
+with the cached data and obtain access to the application.</p>
+
+<p>At the same time, this Policy presents a challenge for normal users, since it
+means that they won't be able to access the application when there is no network
+(cell or wi-fi) connection available. Another side-effect is that your
+application will send more license check requests to the server, since using a
+cached response is not possible. Depending on network conditions, this might
+prove challenging for users also.</p>
+
+<p>Overall, this policy represents a tradeoff of some degree of user convenience
+for absolute security and control over access. Consider the tradeoff carefully
+before using this Policy.</p>
+
+<p>To use StrictPolicy, simply import it to your Activity, create an instance,
+and pass a reference to it when constructing your LicenseChecker. See
+<a href="#lc-lcc">Instantiate LicenseChecker and LicenseCheckerCallback</a>
+for more information. </p>
+
+<h3 id="impl-Obfuscator">Implementing an Obfuscator</h3>
+
+<div class="sidebox-wrapper">
+<div class="sidebox">
+<h2>AESObfuscator</h2>
+
+<p>The LVL includes a full Obfuscator implementation in the
+<code>AESObfuscator.java</code> file. The Obfuscator uses AES encryption to
+obfuscate/unobfuscate data. If you are using a Policy (such as
+ServerManagedPolicy) that caches license response data, using AESObfuscator as
+basis for your Obfuscator implementation is highly recommended. </p>
+
+</div>
+</div>
+
+<p>A typical Policy implementation needs to save the license response data for
+an application to a persistent store, so that it is accessible across
+application invocations and device power cycles.  For example, a Policy would
+maintain the timestamp of the last successful license check, the retry count,
+the license validity period, and similar information in a persistent store,
+rather than resetting the values each time the application is launched. The
+default Policy included in the LVL, ServerManagedPolicy, stores license response
+data in a {@link android.content.SharedPreferences} instance, to ensure that the
+data is persistent. </p>
+
+<p>Because the Policy will use stored license response data to determine whether
+to allow or disallow access to the application, it <em>must</em> ensure that any
+stored data is secure and can not be reused or manipulated by a root user on a
+device. Specifically, the Policy must always obfuscate the data before storing
+it, using a key that is unique for the application and device. Obfuscating using
+a key that is both application-specific and device-specific is critical, because
+it prevents the obfuscated data from being shared among applications and
+devices.</p>
+
+<p>The LVL assists the application with storing its license response data in a
+secure, persistent manner. First, it provides an <code>Obfuscator</code>
+interface that lets your application supply the obfuscation algorithm of its
+choice for stored data. Building on that, the LVL provides the helper class
+<code>PreferenceObfuscator</code>, which handles most of the work of calling the
+application's Obfuscator class and reading and writing the obfuscated data in a
+SharedPreferences instance. </p>
+
+<p>The LVL provides a full Obfuscator implementation called
+<code>AESObfuscator</code> that uses AES encryption to obfuscate data. You can
+use <code>AESObfuscator</code> in your application without modification or you
+can adapt it to your needs. For more information, see the next section.</p>
+
+<p>Alternatively, you can write a custom Obfuscator based on your own code
+or use an obfuscator program such as ProGuard for additional security.</p>
+
+<h4 id="AESObfuscator">AESObfuscator</h4>
+
+<p>The LVL includes a full and recommended implementation of the Obfuscator
+interface called AESObfuscator. The implementation is integrated with the
+LVL sample application and serves as the default Obfuscator in the library. </p>
+
+<p>AESObfuscator provides secure obfuscation of data by using AES to
+encrypt and decrypt the data as it is written to or read from storage.
+The Obfuscator seeds the encryption using three data fields provided
+by the application: </p>
+
+<ol>
+<li>A salt &mdash; an array of random bytes to use for each (un)obfuscation. </li>
+<li>An application identifier string, typically the package name of the application.</li>
+<li>A device identifier string, derived from as many device-specific sources
+as possible, so as to make it as unique.</li>
+</ol>
+
+<p>To use AESObfuscator, first import it to your Activity. Declare a private
+static final array to hold the salt bytes and initialize it to 20 randomly
+generated bytes.</p>
+
+<pre>    ...
+    // Generate 20 random bytes, and put them here.
+    private static final byte[] SALT = new byte[] {
+     -46, 65, 30, -128, -103, -57, 74, -64, 51, 88, -95,
+     -45, 77, -117, -36, -113, -11, 32, -64, 89
+     };
+    ...
+</pre>
+
+<p>Next, declare a variable to hold a device identifier and generate a value for
+it in any way needed. For example, the sample application included in the LVL
+queries the system settings for the
+<code>android.Settings.Secure.ANDROID_ID</code>, which is unique to each device.
+</p>
+
+<p>Note that, depending on the APIs you use to derive device-specific
+information, your application might need to request additional permissions in
+order to secure device-specific information. For example, if you query the
+TelephonyManager to obtain the device IMEI or related data, your application
+will also need to request the <code>android.permission.READ_PHONE_STATE</code>
+permission in its manifest. Before requesting permissions in this way, consider
+how doing so might affect your application or its filtering of your application
+on Android Market (since some permissions can cause the SDK build tools to add
+the associated <code>&lt;uses-feature&gt;</code>).</p>
+
+<p>Finally, construct an instance of AESObfuscator, passing the salt,
+application identifier, and device identifier. You can construct the instance
+directly, while constructing your Policy and LicenseChecker. For example:</p>
+
+<pre>    ...
+    // Construct the LicenseChecker with a Policy.
+    mChecker = new LicenseChecker(
+        this, new ServerManagedPolicy(this,
+            new AESObfuscator(SALT, getPackageName(), deviceId)),
+        BASE64_PUBLIC_KEY  // Your public licensing key.
+        );
+    ...
+</pre>
+
+<p>For a complete example, see MainActivity in the LVL sample application.</p>
+
+
+<h3 id="impl-lc">Checking the license from your application's main Activity</h3>
+
+<p>Once you've implemented a Policy for managing access to your application, the
+next step is to add a license check to your application, which initiates a query
+to the licensing server if needed and manages access to the application based on
+the license response. All of the work of adding the license check and handling
+the response takes place in your main {@link android.app.Activity} source file.
+</p>
+
+<p>To add the license check and handle the response, you must:</p>
+
+<ol>
+    <li><a href="#imports">Add imports</a></li>
+    <li><a href="#lc-impl">Implement LicenseCheckerCallback</a> as a private inner class</li>
+    <li><a href="#thread-handler">Create a Handler</a> for posting from LicenseCheckerCallback to the UI thread</li>
+    <li><a href="#lc-lcc">Instantiate LicenseChecker</a> and LicenseCheckerCallback</li>
+    <li><a href="#check-access">Call checkAccess()</a> to initiate the license check</li>
+    <li><a href="#account-key">Embed your public key</a> for licensing</li>
+    <li><a href="#handler-cleanup">Call your LicenseChecker's onDestroy() method</a> to close IPC connections.</li>
+</ol>
+
+<p>The sections below describe these tasks. </p>
+
+<h4 id="lc-overview">Overview of license check and response</h4>
+
+<div class="sidebox-wrapper">
+<div class="sidebox">
+<h2>Example: MainActivity</h2>
+
+<p>The sample application included with the LVL provides a full example of how
+to initiate a license check and handle the result, in the
+<code>MainActivity.java</code> file.</p>
+
+</div>
+</div>
+
+<p>In most cases, you should add the license check to your application's main
+{@link android.app.Activity}, in the <code>onCreate()</code> method. This
+ensures that when the user launches your application directly, the license check
+will be invoked immediately. In some cases, you can add license checks in other
+locations as well. For example, if your application includes multiple Activity
+components that other applications can start by {@link android.content.Intent},
+you could add license checks in those Activities.</p>
+
+<p>A license check consists of two main actions: </p>
+
+<ul>
+<li>A call to a method to initiate the license check &mdash; in the LVL, this is
+a call to the <code>checkAccess()</code> method of a LicenseChecker object that
+you construct.</li>
+<li>A callback that returns the result of the license check. In the LVL, this is
+a <code>LicenseCheckerCallback</code> interface that you implement. The
+interface declares two methods, <code>allow()</code> and
+<code>dontAllow()</code>, which are invoked by the library based on to the
+result of the license check. You implement those two methods with whatever logic
+you need, to allow or disallow the user access to your application. Note that
+these methods do not determine <em>whether</em> to allow access &mdash; that
+determination is the responsibility of your Policy implementation. Rather, these
+methods simply provide the application behaviors for <em>how</em> to allow and
+disallow access (and handle application errors).</li>
+</ul>
+
+<div style="margin-bottom:2em;">
+
+<img src="{@docRoot}images/licensing_flow.png" style="text-align:left;margin-bottom:0;margin-left:3em;" />
+<div style="margin:.5em 0 1.5em 2em;padding:0"><strong>Figure 6.</strong> Overview of a
+typical license check interaction.</div>
+</div>
+
+<p>The diagram above illustrates how a typical license check takes place: </p>
+
+<ol>
+<li>Code in the application's main Activity instantiates LicenseCheckerCallback
+and LicenseChecker objects. When constructing LicenseChecker, the code passes in
+{@link android.content.Context}, a Policy implementation to use, and the
+publisher account's public key for licensing as parameters. </li>
+<li>The code then calls the <code>checkAccess()</code> method on the
+LicenseChecker object. The method implementation calls the Policy to determine
+whether there is a valid license response cached locally, in
+{@link android.content.SharedPreferences}.
+<ul>
+<li>If so, the <code>checkAccess()</code> implementation calls
+<code>allow()</code>.</li>
+<li>Otherwise, the LicenseChecker initiates a license check request that is sent
+to the licensing server.</li>
+</ul>
+</li>
+<li>When a response is received, LicenseChecker creates a LicenseValidator that
+verifies the signed license data and extracts the fields of the response, then
+passes them to your Policy for further evaluation.
+  <ul>
+    <li>If the license is valid, the Policy caches the response in
+SharedPreferences and notifies the validator, which then calls the
+<code>allow()</code> method on the LicenseCheckerCallback object. </li>
+    <li>If the license not valid, the Policy notifies the validator, which calls
+the <code>dontAllow()</code> method on LicenseCheckerCallback. </li>
+  </ul>
+</li>
+<li>In case of a recoverable local or server error, such as when the network is
+not available to send the request, LicenseChecker passes a RETRY response to
+your Policy's <code>processServerResponse()</code> method. </li>
+<li>In case of a application error, such as when the application attempts to
+check the license of an invalid package name, LicenseChecker passes an error
+response to the LicenseCheckerCallback's  <code>applicationError()</code>
+method. </li>
+</ol>
+
+<p>Note that, in addition to initiating the license check and handling the
+result, which are described in the sections below, your application also needs
+to provide a <a href="#impl-Policy">Policy implementation</a> and, if the Policy
+stores response data (such as ServerManagedPolicy), an <a
+href="#impl-Obfuscator">Obfuscator</a> implementation. </p>
+
+
+<h4 id="imports">Add imports</h4>
+
+<p>First, open the class file of the application's main Activity and import
+LicenseChecker and LicenseCheckerCallback from the LVL package.</p>
+
+<pre>    import com.android.vending.licensing.LicenseChecker;
+    import com.android.vending.licensing.LicenseCheckerCallback;</pre>
+
+<p>If you are using the default Policy implementation provided with the LVL,
+ServerManagedPolicy, import it also, together with the AESObfuscator. If you are
+using a custom Policy or Obfuscator, import those instead. </p>
+
+<pre>    import com.android.vending.licensing.ServerManagedPolicy;
+    import com.android.vending.licensing.AESObfuscator;</pre>
+
+<h4 id="lc-impl">Implement LicenseCheckerCallback as a private inner class</h4>
+
+<p>LicenseCheckerCallback is an interface provided by the LVL for handling
+result of a license check. To support licensing using the LVL, you must
+implement LicenseCheckerCallback and
+its methods to allow or disallow access to the application.</p>
+
+<p>The result of a license check is always a call to one of the
+LicenseCheckerCallback methods, made based on the validation of the response
+payload, the server response code itself, and any additional processing provided
+by your Policy. Your application can implement the methods in any way needed. In
+general, it's best to keep the methods simple, limiting them to managing UI
+state and application access. If you want to add further processing of license
+responses, such as by contacting a backend server or applying custom constraints,
+you should consider incorporating that code into your Policy, rather than
+putting it in the LicenseCheckerCallback methods. </p>
+
+<p>In most cases, you should declare your implementation of
+LicenseCheckerCallback as a private class inside your application's main
+Activity class. </p>
+
+<p>Implement the <code>allow()</code> and <code>dontAllow()</code> methods as
+needed. To start with, you can use simple result-handling behaviors in the
+methods, such as displaying the license result in a dialog. This helps you get
+your application running sooner and can assist with debugging. Later, after you
+have determined the exact behaviors you want, you can add more complex handling.
+</p>
+
+<p>Some suggestions for handling unlicensed responses in
+<code>dontAllow()</code> include: </p>
+
+<ul>
+<li>Display a "Try again" dialog to the user, including a button to initiate a
+new license check. </li>
+<li>Display a "Purchase this application" dialog, including a button that
+deep-links the user to the application's details page on Market, from which the
+use can purchase the application. For more information on how to set up such
+links, see <a
+href="{@docRoot}guide/publishing/publishing.html#marketintent">Using Intents to
+Launch the Market Application on a Device</a>. </li>
+<li>Display a Toast notification that indicates that the features of the
+application are limited because it is not licensed. </li>
+</ul>
+
+<p>The example below shows how the LVL sample application implements
+LicenseCheckerCallback, with methods that display the license check result in a
+dialog. </p>
+
+<pre>    private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
+        public void allow() {
+            if (isFinishing()) {
+                // Don't update UI if Activity is finishing.
+                return;
+            }
+            // Should allow user access.
+            displayResult(getString(R.string.allow));
+        }
+
+        public void dontAllow() {
+            if (isFinishing()) {
+                // Don't update UI if Activity is finishing.
+                return;
+            }
+            displayResult(getString(R.string.dont_allow));
+            // Should not allow access. An app can handle as needed,
+            // typically by informing the user that the app is not licensed
+            // and then shutting down the app or limiting the user to a
+            // restricted set of features.
+            // In this example, we show a dialog that takes the user to Market.
+            showDialog(0);
+        }
+    }
+</pre>
+
+<p>Additionally, you should implement the <code>applicationError()</code>
+method, which the LVL calls to let your application handle errors that are not
+retryable. For a list of such errors, see <a
+href="#server-response-codes">Server Response Codes</a> in the Appendix of this
+document. You can implement the method in any way needed. In most cases, the
+method should log the error code and call <code>dontAllow()</code>.</p>
+
+<h4 id="thread-handler">Create a Handler for posting from LicenseCheckerCallback
+to the UI thread</h4>
+
+<p>During a license check, the LVL passes the request to the Android Market
+application, which handles communication with the licensing server. The LVL
+passes the request over asynchronous IPC (using {@link android.os.Binder}) so
+the actual processing and network communication do not take place on a thread
+managed by your application. Similarly, when the Android Market application
+receives the result, it invokes a  callback method over IPC, which in turn
+executes in an IPC thread pool in your application's process.</p>
+
+<p>The LicenseChecker class manages your application's IPC communication with
+the Android Market application, including the call that sends the request and
+the callback that receives the response. LicenseChecker also tracks open license
+requests and manages their timeouts. </p>
+
+<p>So that it can handle timeouts properly and also process incoming responses
+without affecting your application's UI thread, LicenseChecker spawns a
+background thread at instantiation. In the thread it does all processing of
+license check results, whether the result is a response received from the server
+or a timeout error. At the conclusion of processing, the LVL calls your
+LicenseCheckerCallback methods from the background thread. </p>
+
+<p>To your application, this means that:</p>
+
+<ol>
+<li>Your LicenseCheckerCallback methods will be invoked, in many cases, from a
+background thread.</li>
+<li>Those methods won't be able to update state or invoke any processing in the
+UI thread, unless you create a Handler in the UI thread and have your callback
+methods post to the Handler.</li>
+</ol>
+
+<p>If you want your LicenseCheckerCallback methods to update the UI thread,
+instantiate a {@link android.os.Handler} in the main Activity's 
+{@link android.app.Activity#onCreate(android.os.Bundle) onCreate()} method,
+as shown below. In this example, the LVL sample application's
+LicenseCheckerCallback methods (see above) call <code>displayResult()</code> to
+update the UI thread through the Handler's
+{@link android.os.Handler#post(java.lang.Runnable) post()} method.</p>
+
+<pre>private Handler mHandler;
+
+    &#64;Override
+    public void onCreate(Bundle savedInstanceState) {
+        ...
+        mHandler = new Handler();
+    }
+</pre>
+
+<p>Then, in your LicenseCheckerCallback methods, you can use Handler methods to
+post Runnable or Message objects to the Handler. Here's how the sample
+application included in the LVL posts a Runnable to a Handler in the UI thread
+to display the license status.</p>
+
+<pre>    private void displayResult(final String result) {
+        mHandler.post(new Runnable() {
+            public void run() {
+                mStatusText.setText(result);
+                setProgressBarIndeterminateVisibility(false);
+                mCheckLicenseButton.setEnabled(true);
+            }
+        });
+    }
+</pre>
+
+<h4 id="lc-lcc">Instantiate LicenseChecker and LicenseCheckerCallback</h4>
+
+<p>In the main Activity's
+{@link android.app.Activity#onCreate(android.os.Bundle) onCreate()} method,
+create private instances of LicenseCheckerCallback and LicenseChecker. You must
+instantiate LicenseCheckerCallback first, because you need to pass a reference
+to that instance when you call the contructor for LicenseChecker. </p>
+
+<p>When you instantiate LicenseChecker, you need to pass in these parameters:</p>
+
+<ul>
+<li>The application {@link android.content.Context}</li>
+<li>A reference to the Policy implementation to use for the license check. In
+most cases, you would use the default Policy implementation provided by the LVL,
+ServerManagedPolicy. </li>
+<li>The String variable holding your publisher account's public key for
+licensing. </li>
+</ul>
+
+<p>If you are using ServerManagedPolicy, you won't need to access the class
+directly, so you can instantiate it directly in the LicenseChecker constructor,
+as shown in the example below. Note that you need to pass a reference to a new
+Obfuscator instance when you construct ServerManagedPolicy.</p>
+
+<p>The example below shows the instantiation of LicenseChecker and
+LicenseCheckerCallback from the <code>onCreate()</code> method of an Activity
+class. </p>
+
+<pre>public class MainActivity extends Activity {
+    ...
+    private LicenseCheckerCallback mLicenseCheckerCallback;
+    private LicenseChecker mChecker;
+
+    &#64;Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        ...
+        // Construct the LicenseCheckerCallback. The library calls this when done.
+        mLicenseCheckerCallback = new MyLicenseCheckerCallback();
+
+        // Construct the LicenseChecker with a Policy.
+        mChecker = new LicenseChecker(
+            this, new ServerManagedPolicy(this,
+                new AESObfuscator(SALT, getPackageName(), deviceId)),
+            BASE64_PUBLIC_KEY  // Your public licensing key.
+            );
+        ...
+    }
+}
+</pre>
+
+
+<p>Note that LicenseChecker calls the LicenseCheckerCallback methods from the UI
+thread <em>only</em> if there is valid license response cached locally. If the
+license check is sent to the server, the callbacks always originate from the
+background thread, even for network errors. </p>
+
+
+<h4 id="check-access">Call checkAccess() to initiate the license check</h4>
+
+<p>In your main Activity, add a call to the <code>checkAccess()</code> method of the
+LicenseChecker instance. In the call, pass a reference to your
+LicenseCheckerCallback instance as a parameter. If you need to handle any
+special UI effects or state management before the call, you might find it useful
+to call <code>checkAccess()</code> from a wrapper method. For example, the LVL
+sample application calls <code>checkAccess()</code> from a
+<code>doCheck()</code> wrapper method:</p>
+
+<pre>    &#64;Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        ...
+        // Call a wrapper method that initiates the license check
+        doCheck();
+        ...
+    }
+    ...
+    private void doCheck() {
+        mCheckLicenseButton.setEnabled(false);
+        setProgressBarIndeterminateVisibility(true);
+        mStatusText.setText(R.string.checking_license);
+        mChecker.checkAccess(mLicenseCheckerCallback);
+    }
+</pre>
+
+
+<h4 id="account-key">Embed your public key for licensing</h4>
+
+<p>For each publisher account, the Android Market service automatically
+generates a  2048-bit RSA public/private key pair that is used exlusively for
+licensing. The key pair is uniquely associated with the publisher account and is
+shared across all applications that are published through the account. Although
+associated with a publisher account, the key pair is <em>not</em> the same as
+the key that you use to sign your applications (or derived from it).</p>
+
+<p>The Android Market publisher site exposes the public key for licensing to any
+developer signed in to the publisher account, but it keeps the private key
+hidden from all users in a secure location. When an application requests a
+license check for an application published in your account, the licensing server
+signs the license response using the private key of your account's key pair.
+When the LVL receives the response, it uses the public key provided by the
+application to verify the signature of the license response. </p>
+
+<p>To add licensing to an application, you must obtain your publisher account's
+public key for licensing and copy it into your application. Here's how to find
+your account's public key for licensing:</p>
+
+<ol>
+<li>Go to the Android Market <a
+href="http://market.android.com/publish">publisher site</a> and sign in.
+Make sure that you sign in to the account from which the application you are
+licensing is published (or will be published). </li>
+<li>In the account home page, locate the "Edit profile" link and click it. </li>
+<li>In the Edit Profile page, locate the "Licensing" pane, shown below. Your
+public key for licensing is given in the "Public key" text box. </p>
+</ol>
+
+<p>To add the public key to your application, simply copy/paste the key string
+from the text box into your application as the value of the String variable
+<code>BASE64_PUBLIC_KEY</code>. When you are copying, make sure that you have
+selected the entire key string, without omitting any characters. </p>
+
+<p>Here's an example from the LVL sample application:</p>
+
+<pre>    public class MainActivity extends Activity {
+        private static final String BASE64_PUBLIC_KEY = "MIIBIjANBgkqhkiG ... "; //truncated for this example
+    ...
+    }
+</pre>
+
+<h4 id="handler-cleanup">Call your LicenseChecker's onDestroy() method
+to close IPC connections</h4>
+
+<p>Finally, to let the LVL clean up before your application
+{@link android.content.Context} changes, add a call to the LicenseChecker's
+<code>onDestroy()</code> method from your Activity's
+{@link android.app.Activity#onDestroy()} implementation. The call causes the
+LicenseChecker to properly close any open IPC connection to the Android Market
+application's ILicensingService and removes any local references to the service
+and handler.</p>
+
+<p>Failing to add the call the LicenseChecker's <code>onDestroy()</code> method
+can lead to problems over the lifecycle of your application. For example, if the
+user changes screen orientation while a license check is active, the application
+{@link android.content.Context} is destroyed. If your application does not
+properly close the LicenseChecker's IPC connection, your application will crash
+when the response is received. Similarly, if the user exits your application
+while a license check is in progress,  your application will crash when the
+response is received, unless your application has properly called the
+LicenseChecker's <code>onDestroy()</code> method to disconnect from the service.
+</p>
+
+<p>Here's an example from the sample application included in the LVL, where
+<code>mChecker</code> is the LicenseChecker instance:</p>
+
+<pre>    &#64;Override
+    protected void onDestroy() {
+        super.onDestroy();
+        mChecker.onDestroy();
+        ...
+    }
+</pre>
+
+<p>If you are extending or modifying LicenseChecker, you might also need to call
+the LicenseChecker's <code>finishCheck()</code> method, to clean up any open IPC
+connections.</p>
+
+<h3 id="impl-DeviceLimiter">Implementing a DeviceLimiter</h3>
+
+<p>In some cases, you might want your Policy to limit the number of actual
+devices that are permitted to use a single license. This would prevent a user
+from moving a licensed application onto a number of devices and using the
+application on those devices under the same account ID. It would also prevent a
+user from "sharing" the application by providing the account information
+associated with the license to other individuals, who could then sign in to that
+account on their devices and access the license to the application. </p>
+
+<p>The LVL supports per-device licensing by providing a
+<code>DeviceLimiter</code> interface, which declares a single method,
+<code>allowDeviceAccess()</code>. When a LicenseValidator is handling a response
+from the licensing server, it calls <code>allowDeviceAccess()</code>, passing a
+user ID string extracted from the response.</p>
+
+<p>If you do not want to support device limitation, <strong>no work is
+required</strong> &mdash; the LicenseChecker class automatically uses a default
+implementation called NullDeviceLimiter. As the name suggests, NullDeviceLimiter
+is a "no-op" class whose <code>allowDeviceAccess()</code> method simply returns
+a LICENSED response for all users and devices. </p>
+
+<div style="border-left:4px solid #FFCF00;margin:1em;padding: 0 0 0 .5em">
+<p><strong>Caution:</strong> Per-device licensing is <em>not recommended for
+most applications</em> because:</p>
+<ul>
+<li>It requires that you provide a backend server to manage a users and devices
+mapping, and </li>
+<li>It could inadvertently result in a user being denied access to an
+application that they have legitimately purchased on another device.</li>
+</ul>
+</div>
+
+
+<h2 id="test-env">Setting Up the Testing Environment</h2>
+
+<p>The Android Market publisher site provides configuration tools that let you
+and others test licensing on your application before it is published. As you are
+implementing licensing, you can make use of the publisher site tools to test
+your application's Policy and handling of different licensing responses and
+error conditions.</p>
+
+<p>The main components of the test environment for licensing include: </p>
+
+<ul>
+<li>A "Test response" configuration in your publisher account that lets you
+control the licensing response returned, when the server processes a license
+check for an uploaded application from the publisher account or a test
+account.</li>
+<li>An optional set of test accounts that will receive the configured test
+response when checking the license of an application that you have uploaded
+(regardless whether the application is published or not).</li>
+<li>A runtime environment for the application that includes the Android Market
+application or Google APIs Add-On, on which the user is signed in to the
+publisher account or one of the test accounts.
+</ul>
+
+<p>Setting up the test environment properly involves:</p>
+
+<ol>
+<li><a href="#test-response">Setting static test responses</a> that are returned by the licensing server.</li>
+<li><a href="#test-acct-setup">Setting up test accounts</a> as needed.</li>
+<li><a href="#acct-signin">Signing in</a> properly to an emulator or device, before initiating a license check test.</li>
+</ol>
+
+<p>The sections below provide more information.</p>
+
+
+<h3 id="test-response">Setting test responses for license checks</h3>
+
+<p>Android Market provides a configuration setting in your publisher account
+that lets you override the normal processing of a license check for an
+application you have uploaded and return a specified response code. The setting
+is for testing only and applies <em>only</em> to license checks for applications
+that you have uploaded. For other users (users not signed in to test accounts),
+the server always processes license checks according to normal rules.  </p>
+
+<p>To set a test response for your account, sign in to your publisher account
+and click "Edit Profile". In the Edit Profile page, locate the Test Response
+menu in the Licensing panel, shown below. You can select from the full set of
+valid server response codes to control the response or condition you want to
+test in your application.</p>
+
+<p>In general, you should make sure to test your application's licensing
+implementation with every response code available in the Test Response" menu.
+For a description of the codes, see <a href="#server-response-codes">Server
+Response Codes</a>, below.</p>
+
+<div style="margin-bottom:2em;" id="licensing_test_response">
+
+<img src="{@docRoot}images/licensing_test_response.png" style="text-align:left;margin-bottom:0;" />
+<div style="margin:0 2em;padding:0"><strong>Figure 7.</strong> The Licensing
+panel of your account's Edit Profile page, showing the Test Accounts field and the
+Test Response menu.</div>
+</div>
+
+<p>Note that the test response that you configure applies account-wide &mdash;
+that is, it applies not to a single application, but to <em>all</em>
+applications associated with the publisher account. If you are testing multiple
+applications at once, changing the test response will affect all of those
+applications on their next license check.</p>
+
+<p>Finally, before you can successfully send these test responses to your
+application, you must sign in to the device or emulator on which the application
+is installed, and from which it is querying the server. Specifically, you must
+sign using either your publisher account or one of the test accounts that you
+have set up. For more information about test accounts, see the next section.</p>
+
+<p>See <a href="#server-response-codes">Server Response Codes</a> for a list of
+test responses available and their meanings. </p>
+
+
+<h3 id="test-acct-setup">Setting up test accounts</h3>
+
+<p>In some cases, you might want to let multiple teams of developers test
+licensing on applications that will ultimately be published through your
+publisher account, but without giving them access to your publisher account's
+sign-in credentials. To meet that need, the Android Market publisher site lets
+you set up one or more optional <em>test accounts</em> &mdash; accounts that are
+authorized to query the licensing server and receive static test responses from
+your publisher account.</p>
+
+<p>Test accounts are standard Google accounts that you register on your
+publisher account, such that they will receive the test response for
+applications that you have uploaded. Developers can then sign in to their
+devices or emulators using the test account credentials and initiate license
+checks from installed applications. When the licensing server receives a license
+check from a user of a test account, it returns the static test response
+configured for the publisher account.  </p>
+
+<p>Necessarily, there are limitations on the access and permissions given to
+users signed in through test accounts, including:</p>
+
+<ul>
+<li>Test account users can query the licensing server only for applications that
+are already uploaded to the publisher account. </li>
+<li>Test account users do not have permission to upload applications to your
+publisher account.</li>
+<li>Test account users do not have permission to set the publisher account's
+static test response.</li>
+</ul>
+
+<p>The table below summarizes the differences in capabilities, between the
+publisher account, a test account, and any other account.</p>
+
+<p class="table-caption" id="acct-types-table"><strong>Table 1.</strong>
+Differences in account types for testing licensing.</p>
+
+<table>
+<tr>
+<th>Account Type</th>
+<th>Can check license before upload?</th>
+<th>Can receive test response?</th>
+<th>Can set test response?</th>
+</tr>
+
+<tr>
+<td>Publisher account</td>
+<td>Yes</td>
+<td>Yes</td>
+<td>Yes</td>
+</tr>
+
+<tr>
+<td>Test account</td>
+<td>No</td>
+<td>Yes</td>
+<td>No</td>
+</tr>
+
+<tr>
+<td>Other</td>
+<td>No</td>
+<td>No</td>
+<td>No</td>
+</tr>
+</table>
+
+<h4 id="reg-test-acct">Registering test accounts on the publisher account</h4>
+
+<p>To get started, you need to register each test account in your publisher
+account. As shown in <a href="#licensing_test_response">Figure 7</a>, above, you
+register test accounts in the Licensing panel of your publisher account's Edit
+Profile page. Simply enter the accounts as a comma-delimited list and click
+<strong>Save</strong> to save your profile changes.</p>
+
+<p>You can use any Google account as a test account. If you want to own and
+control the test accounts, you can create the accounts yourself and distribute
+the credentials to your developers or testers.</p>
+
+<h4 id="test-app-upload">Handling application upload and distribution for test
+account users</h4>
+
+<p>As mentioned above, users of test accounts can only receive static test
+responses for applications that are uploaded to the publisher account. Since
+those users do not have permission to upload applications, as the publisher you
+will need to work with those users to collect apps for upload and distribute
+uploaded apps for testing. You can handle collection and distribution in any way
+that is convenient. </p>
+
+<p>Once an application is uploaded and becomes known to the licensing server,
+developers and testers can continue modify the application in their local
+development environment, without having to upload new versions. You only need to
+upload a new version if the local application increments the
+<code>versionCode</code> attribute in the manifest file. </p>
+
+<h4 id="test-key">Distributing your public key to test account users</h4>
+
+<p>The licensing server handles static test responses in the normal way,
+including signing the license response data, adding extras parameters, and so
+on. To support developers who are implementing licensing using test accounts,
+rather than having access to the publisher account, you will need to distribute
+your public key to them. Developers without access to the publisher site do not
+have access to your public key, and without the key they won't be able to
+verify license responses. </p>
+
+<p>Note that if you decide to generate a new licensing key pair for your account
+for some reason, you need to notify all users of test accounts. For
+testers, you can embed the new key in the application package and distribute it
+to users. For developers, you will need to distribute the new key to them
+directly. </p>
+
+
+<h3 id="acct-signin">Signing in to an authorized account in the runtime
+environment</h3>
+
+<p>The licensing service is designed to determine whether a given user is
+licensed to use a given application &mdash; during a license check, the Android
+Market application gathers the user ID from the primary account on the system
+and sends it to the server, together with the package name of the application
+and other information. However, if there is no user information available, the
+license check cannot succeed, so the Android Market application terminates the
+request and returns an error to the application. </p>
+
+<p>During testing, to ensure that your application can successfully query the
+licensing server, you must make sure that you sign in to an account <em>on the
+device or emulator</em> using:</p>
+
+<ul>
+<li>The credentials of a publisher account, or</li>
+<li>The credentials of a test account that is registered with a publisher
+account</li>
+</ul>
+
+
+<div class="sidebox-wrapper">
+<div class="sidebox">
+<h2>Signing in to a Google account on an emulator</h2>
+
+<p>If you are testing licensing on an emulator, you need to sign in to a Google
+account on the emulator. If you do not see an option to create a new Google
+account, the problem might be that your AVD is running a standard Android system
+image, rather than the Google APIs Add-On, API 8 (release 2) or higher. </p>
+
+<p style="margin-top:.5em;">For more information, see <a
+href="#runtime-setup">Setting up the runtime environment</a>, above.</p>
+
+</div>
+</div>
+
+<p>Signing in using a publisher account offers the advantage of letting your
+applications receive static test responses even before the applications are
+uploaded to the publisher site.</p>
+
+<p>If you are part of a larger organization or are working with external groups
+on applications that will be published through your site, you will more likely
+want to distribute test accounts instead, then use those to sign in during
+testing. </p>
+
+<p>To sign in on a device or emulator, follow the steps below. The preferred
+approach is to sign in as the primary account &mdash; however, if there are
+other accounts already in use on the device or emulator, you can create an
+additional account and sign in to it using the publisher or test account
+credentials.  </p>
+
+<ol>
+<li>Open Settings &gt; Accounts &amp; sync</li>
+<li>Select <strong>Add Account</strong> and choose to add a "Google" account.
+</li>
+<li>Select <strong>Next</strong> and then <strong>Sign in</strong>.</li>
+<li>Enter the username and password of either the publisher account or a test
+account that is registered in the publisher account.</li>
+<li>Select <strong>Sign in</strong>. The system signs you in to the new
+account.</li>
+</ol>
+
+<p>Once you are signed in, you can begin testing licensing in your application
+(if you have completed the LVL integration steps above). When your application
+initiates a license check, it will receive a response containing the static test
+response configured on the publisher account. </p>
+
+<p>Note that, if you are using an emulator, you will need to sign in to the
+publisher account or test account each time you wipe data when restarting the
+emulator.</p>
+
+<div style="margin:2em 1em 1em 1em;">
+
+<img src="{@docRoot}images/licensing_device_signin.png" style="text-align:left;" />
+<div style="margin:.25em 1.25em;padding:0"><strong>Figure 8.</strong> Example of
+setting up a Google account on a device or emulator.</div>
+</div>
+
+<h2 id="app-obfuscation">Obfuscating Your Application</h2>
+
+<p>To ensure the security of your application, particularly for a paid
+application that uses licensing and/or custom constraints and protections, it's
+very important to obfuscate your application code. Properly obfuscating your
+code makes it more difficult for a malicious user to decompile the application's
+bytecode, modify it &mdash; such as by removing the license check &mdash;
+and then recompile it.</p>
+
+<p>Several obfuscator programs are available for Android applications, including
+<a href="http://proguard.sourceforge.net/">ProGuard</a>, which also offers
+code-optimization features. The use of ProGuard or a similar program to obfuscate
+your code is <em>strongly recommended</em> for all applications that use Android
+Market Licensing. </p>
+
+<h2 id="app-publishing">Publishing a Licensed Application</h2>
+
+<p>When you are finished testing your license implementation, you are ready to
+publish the application on Android Market. Follow the normal steps to <a
+href="{@docRoot}guide/publishing/preparing.html">prepare</a>, <a
+href="{@docRoot}guide/publishing/app-signing.html">sign</a>, and then <a
+href="{@docRoot}guide/publishing/publishing.html">publish the application</a>.
+</p>
+
+<h4>Removing Copy Protection</h4>
+
+<p>After uploading your licensed application, remember to remove copy protection
+from the application, if it is currently used. To check and remove copy
+protection, sign in to the publisher site and go the application's upload
+details page. In the Publishing options section, make sure that the Copy
+Protection radio button selection is "Off".</p>
+
+<h4>Considerations for Free Apps</h4>
+
+<p>Licensing is currently supported only for paid applications. If you already
+published your application as free, you won't be able to upload an updated
+version that includes licensing (that is, an application that uses the same
+package name and that includes the <a href="#manifest-permission">licensing
+permission</a>). Here are some points to keep in mind:</p>
+
+<ul>
+<li>If you want to offer a free version of your application that provides a
+reduced feature set (or that offers the full feature set for trial period), the
+free version of your application must not include the licensing permission and
+must use a different package name than the paid version of the app.</li>
+<li>If you want to offer a paid version of your free application that uses
+licensing, you can do so under a new package name.</li>
+</ul>
+
+<h2 id="support">Where to Get Support</h2>
+
+<p>If you have questions or encounter problems while implementing or deploying
+publishing in your applications, please use the support resources listed in the
+table below. By directing your queries to the correct forum, you can get the
+support you need more quickly. </p>
+
+<p class="table-caption"><strong>Table 2.</strong> Developer support resources
+for Android Market Licensing Service.</p>
+
+<table>
+
+<tr>
+<th>Support Type</th>
+<th>Resource</th>
+<th>Range of Topics</th>
+</tr>
+<tr>
+<td rowspan="2">Development and testing issues</td>
+<td>Google Groups: <a
+href="http://groups.google.com/group/android-developers">android-developers</a>
+</td>
+<td rowspan="2">LVL download and integration, library projects, Policy
+questions, user experience ideas, handling of responses, Obfuscator, IPC, test
+environment setup</td>
+</tr>
+<tr>
+<td>Stack Overflow: <a
+href="http://stackoverflow.com/questions/tagged/android">http://stackoverflow.com/questions/tagged/android</a></td>
+</tr>
+<tr>
+<td rowspan="2">Accounts, publishing, and deployment issues</td>
+<td><a href="http://www.google.com/support/forum/p/Android+Market">Android
+Market Help Forum</a></td>
+<td rowspan="2">Publisher accounts, licensing key pair, test accounts, server
+responses, test responses, application deployment and results</td>
+</tr>
+<tr>
+<td><a
+href="http://market.android.com/support/bin/answer.py?answer=186113">Market
+Licensing Support FAQ</a></td>
+</tr>
+<tr>
+<td>LVL issue tracker</td>
+<td><a href="http://code.google.com/p/marketlicensing/issues/">Marketlicensing
+project issue tracker</a></td>
+<td>Bug and issue reports related specifically to the LVL source code classes
+and interface implementations</td>
+</tr>
+
+</table>
+
+<p>For general information about how to post to the groups listed above, see <a
+href="{@docRoot}resources/community-groups.html">Developer Forums</a> document
+in the Resources tab.</p>
+
+<h2 id="lvl-summary">Summary of LVL Classes and Interfaces</h2>
+
+<p>The table below lists all of the source files in the License Verification
+Library (LVL) available through the Android SDK. All of the files are part of
+the <code>com.android.vending.licensing</code> package.</p>
+
+<p class="table-caption"><strong>Table A-1.</strong> Summary of LVL library
+classes and interfaces.</p>
+
+<div style="width:99%">
+<table width="100%">
+
+<tr>
+<th width="15%">Category</th>
+<th width="20%">Name</th>
+<th width="100%">Description</th>
+</tr>
+
+<tr>
+<td rowspan="2">License check and result</td>
+<td>LicenseChecker</td>
+<td>Class that you instantiate (or subclass) to initiate a license check.</td>
+</tr>
+<tr>
+<td><em>LicenseCheckerCallback</em></td>
+<td>Interface that you implement to handle result of the license check.</td>
+</tr>
+
+<tr>
+<td rowspan="3" width="15%">Policy</td>
+<td width="20%"><em>Policy</em></td>
+<td width="100%">Interface that you implement to determine whether to allow
+access to the application, based on the license response. </td>
+</tr>
+<tr>
+<td>ServerManagedPolicy</td>
+<td width="100%">Default Policy implementation. Uses settings provided by the
+licensing server to manage local storage of license data, license validity,
+retry.</td>
+</tr>
+<tr>
+<td>StrictPolicy</td>
+<td>Alternative Policy implementation. Enforces licensing based on a direct
+license response from the server only. No caching or request retry.</td>
+</tr>
+
+<tr>
+<td rowspan="2" width="15%">Data obfuscation <br><em>(optional)</em></td>
+<td width="20%"><em>Obfuscator</em></td>
+<td width="100%">Interface that you implement if you are using a Policy (such as
+ServerManagedPolicy) that caches license response data in a persistent store.
+Applies an obfuscation algorithm to encode and decode data being written or
+read.</td>
+</tr>
+<tr>
+<td>AESObfuscator</td>
+<td>Default Obfuscator implementation that uses AES encryption/decryption
+algorithm to obfuscate/unobfuscate data.</td>
+</tr>
+
+<tr>
+<td rowspan="2" width="15%">Device limitation<br><em>(optional)</em></td>
+<td width="20%"><em>DeviceLimiter</em></td>
+<td width="100%">Interface that you implement if you want to restrict use of an
+application to a specific device. Called from LicenseValidator. Implementing
+DeviceLimiter is not recommended for most applications because it requires a
+backend server and may cause the user to lose access to licensed applications,
+unless designed with care.</td>
+</tr>
+<tr>
+<td>NullDeviceLimiter</td>
+<td>Default DeviceLimiter implementation that is a no-op (allows access to all
+devices).</td>
+</tr>
+
+<tr>
+<td rowspan="6" width="15%">Library core, no integration needed</td>
+<td width="20%">ResponseData</td>
+<td width="100%">Class that holds the fields of a license response.</td>
+</tr>
+<tr>
+<td>LicenseValidator</td>
+<td>Class that decrypts and verifies a response received from the licensing
+server.</td>
+</tr>
+<tr>
+<td>ValidationException</td>
+<td>Class that indicates errors that occur when validating the integrity of data
+managed by an Obfuscator.</td>
+</tr>
+<tr>
+<td>PreferenceObfuscator</td>
+<td>Utility class that writes/reads obfuscated data to the system's
+{@link android.content.SharedPreferences} store.</td>
+</tr>
+<tr>
+<td><em>ILicensingService</em></td>
+<td>One-way IPC interface over which a license check request is passed to the
+Android Market client.</td>
+</tr>
+<tr>
+<td><em>ILicenseResultListener</em></td>
+<td>One-way IPC callback implementation over which the application receives an
+asynchronous response from the licensing server.</td>
+</tr>
+
+</table>
+</div>
+
+
+<h2 id="server-response-codes">Server Response Codes</h2>
+
+<p>The table below lists all of the license response codes supported by the
+licensing server. In general, an application should handle all of these response
+codes. By default, the LicenseValidator class in the LVL provides all of the
+necessary handling of these response codes for you. </p>
+
+<p class="table-caption"><strong>Table A-2.</strong> Summary of response codes
+returned by the Android Market server in a license response.</p>
+
+<table>
+
+<tr>
+<th>Response Code</th>
+<th>Description</th>
+<th>Signed?</th>
+<th>Extras</th>
+<th>Comments</th>
+</tr>
+<tr>
+<td>LICENSED</td>
+<td>The application is licensed to the user. The user has purchased the
+application or the application is free.</td>
+<td>Yes</td>
+<td><code>VT</code>,&nbsp;<code>GT</code>, <code>GR</code></td>
+<td><em>Allow access according to Policy constraints.</em></td>
+</tr>
+<tr>
+<td>LICENSED_OLD_KEY</td>
+<td>The application is licensed to the user, but there is an updated application
+version available that is signed with a different key. </td>
+<td>Yes </td>
+<td><code>VT</code>, <code>GT</code>, <code>GR</code>, <code>UT</code></td>
+<td><em>Optionally allow access according to Policy constraints.</em>
+<p style="margin-top:.5em;">Can indicate that the key pair used by the installed
+application version is invalid or compromised. The application can allow access
+if needed or inform the user that an upgrade is available and limit further use
+until upgrade.</p>
+</td>
+</tr>
+<tr>
+<td>NOT_LICENSED</td>
+<td>The application is not licensed to the user.</td>
+<td>No</td>
+<td></td>
+<td><em>Do not allow access.</em></td>
+</tr>
+<tr>
+<td>ERROR_CONTACTING_SERVER</td>
+<td>Local error &mdash; the Android Market application was not able to reach the
+licensing server, possibly because of network availability problems. </td>
+<td>No</td>
+<td></td>
+<td><em>Retry the license check according to Policy retry limits.</em></td>
+</tr>
+<tr>
+<td>ERROR_SERVER_FAILURE</td>
+<td>Server error &mdash; the server could not load the publisher account's key
+pair for licensing.</td>
+<td>No</td>
+<td></td>
+<td><em>Retry the license check according to Policy retry limits.</em>
+</td>
+</tr>
+<tr>
+<td>ERROR_INVALID_PACKAGE_NAME</td>
+<td>Local error &mdash; the application requested a license check for a package
+that is not installed on the device. </td>
+<td>No </td>
+<td></td>
+<td><em>Do not retry the license check.</em>
+<p style="margin-top:.5em;">Typically caused by a developement error.</p>
+</td>
+</tr>
+<tr>
+<td>ERROR_NON_MATCHING_UID</td>
+<td>Local error &mdash; the application requested a license check for a package
+whose UID (package, user ID pair) does not match that of the requesting
+application. </td>
+<td>No </td>
+<td></td>
+<td><em>Do not retry the license check.</em>
+<p style="margin-top:.5em;">Typically caused by a developement error.</p>
+</td>
+</tr>
+<tr>
+<td>ERROR_NOT_MARKET_MANAGED</td>
+<td>Server error &mdash; the application (package name) was not recognized by
+Android Market. </td>
+<td>No</td>
+<td></td>
+<td><em>Do not retry the license check.</em>
+<p style="margin-top:.5em;">Can indicate that the application was not published
+through Android Market or that there is an development error in the licensing
+implementation.</p>
+</td>
+</tr>
+
+</table>
+
+
+<h2 id="extras">Server Response Extras</h2>
+
+<p>The licensing server includes several settings in certain types of license
+responses, to assist the application and its Policy in managing access to the
+application across the 24-hour refund period and other conditions. Specifically,
+the server provides recommended values for the application's license validity
+period, retry grace period, maximum allowable retry count, and other settings.
+The server appends the settings as key-value pairs in the license response
+"extras" field. </p>
+
+<p>Any Policy implementation can extract the extras settings from the license
+response and use them as needed. The LVL default Policy implementation, <a
+href="#ServerManagedPolicy">ServerManagedPolicy</a>, serves as a working
+implementation and an illustration of how to obtain, store, and use the
+settings. </p>
+
+<p class="table-caption"><strong>Table A-3.</strong> Summary of
+license-management settings supplied by the Android Market server in a license
+response.</p>
+
+<table>
+<tr>
+<th>Extra</th><th>Description</th>
+</tr>
+
+<tr>
+  <td>VT</td>
+  <td>License validity timestamp. Specifies the date/time at which the current
+(cached) license response expires and must be rechecked on the licensing server.
+ </td>
+</tr>
+<tr>
+  <td>GT</td>
+  <td>Grace period timestamp. Specifies the end of the period during which a
+Policy may allow access to the application, even though the response status is
+RETRY. <p>The value is managed by the server, however a typical value would be 5
+or more days.</p></td>
+</tr>
+<tr>
+  <td>GR</td>
+  <td>Maximum retries count. Specifies how many consecutive RETRY license checks
+the Policy should allow, before denying the user access to the application.
+<p>The value is managed by the server, however a typical value would be "10" or
+higher.</p></td>
+</tr>
+<tr>
+  <td>UT</td>
+  <td>Update timestamp. Specifies the day/time when the most recent update to
+this application was uploaded and published. <p>The server returns this extra
+only for LICENSED_OLD_KEYS responses, to allow the Policy to determine how much
+time has elapsed since an update was published with new licensing keys before
+denying the user access to the application. </p></td>
+</tr>
+
+</table>
+
+<p>The sections below provide more information about the server-provided
+settings and how to use them. </p>
+
+<h4>License validity period</h4>
+
+<p>The Android Market licensing server sets a license validity period for all
+downloaded applications. The period expresses the interval of time over which an
+application's license status should be considered as unchanging and cacheable by
+a licensing Policy in the application. The licensing server includes the
+validity period in its response to all license checks, appending an
+end-of-validity timestamp to the response as an extra under the key "VT". A
+Policy can extract the VT key value and use it to conditionally allow access to
+the application without rechecking the license, until the validity period
+expires. </p>
+
+<p>The license validity signals to a licensing Policy when it must recheck the
+licensing status with the licensing server. It is <em>not</em> intended to imply
+whether an application is actually licensed for use. That is, when an
+application's license validity period expires, this does not mean that the
+application is no longer licensed for use &mdash; rather, it indicates only that
+the Policy must recheck the licensing status with the server. It follows that,
+as long as the license validity period is not expired, it is acceptable for the
+Policy to cache the initial license status locally and return the cached license
+status instead of sending a new license check to the server.</p>
+
+<p>The licensing server manages the validity period as a means of helping the
+application properly enforce licensing across the refund period offered by
+Android Market for paid applications. It sets the validity period based on
+whether the application was purchased and, if so, how long ago. Specifically,
+the server sets a validity period as follows:</p>
+
+<ul>
+<li>For a paid application, the server sets the initial license validity period
+so that the license reponse remains valid for as long as the application is
+refundable. A licensing Policy in the application may cache the
+result of the initial license check and does not need to recheck the license
+until the validity period has expired.</li>
+<li>When an application is no longer refundable, the server
+sets a longer validity period &mdash; typically a number of days. </li>
+<li>For a free application, the server sets the validity period to a very high
+value (<code>long.MAX_VALUE</code>). This ensures that, provided the Policy has
+cached the validity timestamp locally, it will not need to recheck the
+license status of the application in the future.</li>
+</ul>
+
+<p>The ServerManagedPolicy implementation uses the extracted timestamp
+(<code>mValidityTimestamp</code>) as a primary condition for determining whether
+to recheck the license status with the server before allowing the user access to
+the application. </p>
+
+<h4>Retry period and maximum retry count</h4>
+
+<p>In some cases, system or network conditions can prevent an application's
+license check from reaching the licensing server, or prevent the server's
+response from reaching the Android Market client application. For example, the
+user might launch an application when there is no cell network or data
+connection available &mdash; such as when on an airplane &mdash; or when the
+network connection is unstable or the cell signal is weak. </p>
+
+<p>When network problems prevent or interrupt a license check, the Android
+Market client notifies the application by returning a "RETRY" response code to
+the Policy's <code>processServerResponse()</code> method. In the case of system
+problems, such as when the application is unable to bind with Android Market's
+ILicensingService implementation, the LicenseChecker library itself calls the
+Policy <code>processServerResonse()</code> method with a "RETRY" response code.
+</p>
+
+<p>In general, the RETRY response code is a signal to the application that an
+error has occurred that has prevented a license check from completing. 
+
+<p>The Android Market server helps an application to manage licensing under
+error conditions by setting a retry "grace period" and a recommended maximum
+retries count. The server includes these values in all license check responses,
+appending them as extras under the keys "GT" and "GR". </p>
+
+<p>The application Policy can extract the GT and GR extras and use them to
+conditionally allow access to the application, as follows:</p>
+
+<ul>
+<li>For a license check that results in a RETRY response, the Policy should
+cache the RETRY response code and increment a count of RETRY responses.</li>
+<li>The Policy should allow the user to access the application, provided that
+either the retry grace period is still active or the maximum retries count has
+not been reached.</li>
+</ul>
+
+<p>The ServerManagedPolicy uses the server-supplied GT and GR values as
+described above. The example below shows the conditional handling of the retry
+responses in the <code>allow()</code> method. The count of RETRY responses is
+maintained in the <code>processServerResponse()</code> method, not shown. </p>
+
+
+<pre>    public boolean allowAccess() {
+        long ts = System.currentTimeMillis();
+        if (mLastResponse == LicenseResponse.LICENSED) {
+            // Check if the LICENSED response occurred within the validity timeout.
+            if (ts &lt;= mValidityTimestamp) {
+                // Cached LICENSED response is still valid.
+                return true;
+            }
+        } else if (mLastResponse == LicenseResponse.RETRY &amp;&amp;
+                   ts &lt; mLastResponseTime + MILLIS_PER_MINUTE) {
+            // Only allow access if we are within the retry period or we haven't used up our
+            // max retries.
+            return (ts &lt;= mRetryUntil || mRetryCount &lt;= mMaxRetries);
+        }
+        return false;
+    }</pre>
+
diff --git a/docs/html/guide/publishing/preparing.jd b/docs/html/guide/publishing/preparing.jd
index c1c6351..442c12a 100644
--- a/docs/html/guide/publishing/preparing.jd
+++ b/docs/html/guide/publishing/preparing.jd
@@ -39,13 +39,14 @@
 <ol>
 <li>Test your application extensively on an actual device </li>
 <li>Consider adding an End User License Agreement in your application</li>
+<li>Consider adding licensing support</li>
 <li>Specify an icon and label in the application's manifest</li>
 <li>Turn off logging and debugging and clean up data/files</li>
 </ol>
 
 <p>Before you do the final compile of your application:</p>
 
-<ol start="5">
+<ol start="6">
 <li>Version your application</li>
 <li>Obtain a suitable cryptographic key</li>
 <li>Register for a Maps API Key, if your application is using MapView elements</li>
@@ -53,7 +54,7 @@
 
 <p><em>Compile your application...</em></p>
 <p>After compiling your application:</p>
-<ol start="8">
+<ol start="9">
 <li>Sign your application</li>
 <li>Test your compiled application</li>
 </ol>
@@ -101,7 +102,19 @@
 <p>To protect your person, organization, and intellectual property, you may want
 to provide an End User License Agreement (EULA) with your application. 
 
-<h3 id="iconlabel">3. Specify an icon and label in the application's manifest</h3>
+<h3 id="eula">3. Consider adding support for Android Market Licensing</h3>
+
+<p>If you are publishing a paid application through Android Market, consider
+adding support for Android Market Licensing. Licensing lets you control access
+to your application based on whether the current user has purchased it.
+Using Android Market Licensing is optional. 
+
+<p>For complete information about Android Market Licensing Service and how to
+use it in your application, see <a
+href="{@docRoot}guide/publishing/licensing.html">Licensing Your
+Applications</a>.</p>
+
+<h3 id="iconlabel">4. Specify an icon and label in the application's manifest</h3>
 
 <p>The icon and label that you specify in an application's manifest are
 important because they are displayed to users as your application's icon and
@@ -116,7 +129,7 @@
 <p>As regards the design of your icon, you should try to make it match as much
 as possible the style used by the built-in Android applications.</p>
 
-<h3 id="logging">4. Turn off logging and debugging and clean up data/files</h3>
+<h3 id="logging">5. Turn off logging and debugging and clean up data/files</h3>
 
 <p>For release, you should make sure that debug facilities are turned off and
 that debug and other unnecessary data/files are removed from your application
@@ -133,7 +146,7 @@
 
 <h2 id="finalcompile">Before you do the final compile of your application</h2>
 
-<h3 id="versionapp">5. Version your application</h3>
+<h3 id="versionapp">6. Version your application</h3>
 
 <p>Before you compile your application, you must make sure that you have defined
 a version number for your application, specifying an appropriate value for both
@@ -152,7 +165,7 @@
 application, see <a href="{@docRoot}guide/publishing/versioning.html">Versioning 
 Your Applications</a>.</p>
 
-<h3 id="cryptokey">6. Obtain a suitable cryptographic key</h3>
+<h3 id="cryptokey">7. Obtain a suitable cryptographic key</h3>
 
 <p>If you have read and followed all of the preparation steps up to this point,
 your application is compiled and ready for signing. Inside the .apk, the
@@ -173,7 +186,7 @@
 <li>Sign your application for release, later in the preparation process</li>
 </ul>
 
-<h3 id="mapsApiKey">7. Register for a Maps API Key, if your application is using
+<h3 id="mapsApiKey">8. Register for a Maps API Key, if your application is using
 MapView elements</h3>
 
 <div class="sidebox-wrapper">
@@ -231,7 +244,7 @@
 
 <h2 id="post-compile">After compiling your application</h2>
 
-<h3 id="signapp">8. Sign your application</h3>
+<h3 id="signapp">9. Sign your application</h3>
 
 <p>Sign your application using your private key and then
 align it with the {@code zipalign} tool. Signing your application
@@ -239,7 +252,7 @@
 <a href="{@docRoot}guide/publishing/app-signing.html">Signing Your 
 Applications</a> for complete information. </p>
 
-<h3 id="testapp">9. Test your compiled and signed application</h3>
+<h3 id="testapp">10. Test your compiled and signed application</h3>
 
 <p>Before you release your compiled application, you should thoroughly test it
 on the target mobile device (and target network, if possible). In particular,
diff --git a/docs/html/guide/publishing/publishing.jd b/docs/html/guide/publishing/publishing.jd
index 0c087ef..9b470c8 100644
--- a/docs/html/guide/publishing/publishing.jd
+++ b/docs/html/guide/publishing/publishing.jd
@@ -18,7 +18,8 @@
 <ol>
 <li><a href="#overview">Publishing on Android Market</a>
     <ol>
-    <li><a href="#marketupgrade">Publishing Updates on Android Market</a>
+    <li><a href="#marketupgrade">Publishing Updates on Android Market</a></li>
+    <li><a href="#marketLicensing">Using Android Market Licensing Service</a></li>
     <li><a href="#marketintent">Using Intents to Launch the Market Application</a></li>
     </ol></li>
 <!--
@@ -30,6 +31,7 @@
 <h2>See also</h2>
 
 <ol>
+<li><a href="{@docRoot}guide/publishing/licensing.html">Licensing Your Applications</a></li>
 <li><a href="{@docRoot}guide/publishing/preparing.html">Preparing to Publish</a></li>
 </ol>
 
@@ -59,7 +61,7 @@
 <p>The sections below provide information about publishing your Android
 application to mobile device users.</p>
 
-<h2 id="market">Publishing on Android Market</h2>
+<h2 id="overview">Publishing on Android Market</h2>
 
 <p>Android Market is a hosted service that makes it easy for users to find and
 download Android applications to their Android-powered devices, and makes it 
@@ -121,7 +123,26 @@
 consider it a new application and will not offer it to users as an update.</p>
 
 
+<h3 id="marketLicensing">Using Android Market Licensing Service</h3>
 
+<p>Android Market offers a licensing service that lets you enforce licensing
+policies for paid applications that you publish through Android Market. With
+Android Market Licensing, your applications can query Android Market at run time
+to obtain their licensing status for the current user, then allow or disallow
+further use as appropriate. Using the service, you can apply a flexible
+licensing policy on an application-by-application basis &mdash; each 
+application can enforce its licensing status in the way most appropriate
+for it. </p>
+
+<p>Any application that you publish through Android Market can use the Android
+Market Licensing Service. The service uses no dedicated framework APIs, you can
+add licensing to any legacy application that uses a minimum API level of 3 or
+higher.</p>
+
+<p>For complete information about Android Market Licensing Service and how to
+use it in your application, see <a
+href="{@docRoot}guide/publishing/licensing.html">Licensing Your
+Applications</a>.</p>
 
 
 <h3 id="marketintent">Using Intents to Launch the Market Application on 
diff --git a/docs/html/images/licensing_add_library.png b/docs/html/images/licensing_add_library.png
new file mode 100644
index 0000000..90b4435
--- /dev/null
+++ b/docs/html/images/licensing_add_library.png
Binary files differ
diff --git a/docs/html/images/licensing_arch.png b/docs/html/images/licensing_arch.png
new file mode 100644
index 0000000..ba7484a
--- /dev/null
+++ b/docs/html/images/licensing_arch.png
Binary files differ
diff --git a/docs/html/images/licensing_device_signin.png b/docs/html/images/licensing_device_signin.png
new file mode 100644
index 0000000..a4f5f88
--- /dev/null
+++ b/docs/html/images/licensing_device_signin.png
Binary files differ
diff --git a/docs/html/images/licensing_flow.png b/docs/html/images/licensing_flow.png
new file mode 100644
index 0000000..b33119e
--- /dev/null
+++ b/docs/html/images/licensing_flow.png
Binary files differ
diff --git a/docs/html/images/licensing_gapis_8.png b/docs/html/images/licensing_gapis_8.png
new file mode 100644
index 0000000..43ad262
--- /dev/null
+++ b/docs/html/images/licensing_gapis_8.png
Binary files differ
diff --git a/docs/html/images/licensing_package.png b/docs/html/images/licensing_package.png
new file mode 100644
index 0000000..5da5632
--- /dev/null
+++ b/docs/html/images/licensing_package.png
Binary files differ
diff --git a/docs/html/images/licensing_public_key.png b/docs/html/images/licensing_public_key.png
new file mode 100644
index 0000000..1630209
--- /dev/null
+++ b/docs/html/images/licensing_public_key.png
Binary files differ
diff --git a/docs/html/images/licensing_test_response.png b/docs/html/images/licensing_test_response.png
new file mode 100644
index 0000000..ead2152
--- /dev/null
+++ b/docs/html/images/licensing_test_response.png
Binary files differ
diff --git a/docs/html/resources/faq/commontasks.jd b/docs/html/resources/faq/commontasks.jd
index 2f09b00..9c32e9c 100644
--- a/docs/html/resources/faq/commontasks.jd
+++ b/docs/html/resources/faq/commontasks.jd
@@ -158,7 +158,7 @@
 <ul>
     <li>Create an {@link android.app.Dialog app.Dialog} class </li>
     <li>Create an {@link android.app.AlertDialog app.AlertDialog} class </li>
-    <li>Set the {@link android.R.style#Theme_Dialog} <em>theme</em> attribute to <code>&#064;android:style/Theme.Dialog</code>
+    <li>Set the {@link android.R.style#Theme_Dialog} <em>theme</em> attribute to <code>&#64;android:style/Theme.Dialog</code>
         in your AndroidManifest.xml file. For example:    
     <pre>&lt;activity class=&quot;AddRssItem&quot; android:label=&quot;Add an item&quot; android:theme=&quot;&#064;android:style/Theme.Dialog&quot;/&gt;</pre></li>
 </ul>
diff --git a/include/media/EffectBassBoostApi.h b/include/media/EffectBassBoostApi.h
index b24a5f4..75f8d78 100644
--- a/include/media/EffectBassBoostApi.h
+++ b/include/media/EffectBassBoostApi.h
@@ -23,9 +23,10 @@
 extern "C" {
 #endif
 
-// TODO: include OpenSLES_IID.h instead
+#ifndef OPENSL_ES_H_
 static const effect_uuid_t SL_IID_BASSBOOST_ = { 0x0634f220, 0xddd4, 0x11db, 0xa0fc, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
 const effect_uuid_t * const SL_IID_BASSBOOST = &SL_IID_BASSBOOST_;
+#endif //OPENSL_ES_H_
 
 /* enumerated parameter settings for BassBoost effect */
 typedef enum
diff --git a/include/media/EffectEnvironmentalReverbApi.h b/include/media/EffectEnvironmentalReverbApi.h
index d490f71..2233e3f 100644
--- a/include/media/EffectEnvironmentalReverbApi.h
+++ b/include/media/EffectEnvironmentalReverbApi.h
@@ -23,9 +23,10 @@
 extern "C" {
 #endif
 
-// TODO: include OpenSLES_IID.h instead
+#ifndef OPENSL_ES_H_
 static const effect_uuid_t SL_IID_ENVIRONMENTALREVERB_ = { 0xc2e5d5f0, 0x94bd, 0x4763, 0x9cac, { 0x4e, 0x23, 0x4d, 0x6, 0x83, 0x9e } };
 const effect_uuid_t * const SL_IID_ENVIRONMENTALREVERB = &SL_IID_ENVIRONMENTALREVERB_;
+#endif //OPENSL_ES_H_
 
 /* enumerated parameter settings for environmental reverb effect */
 typedef enum
@@ -45,20 +46,19 @@
     REVERB_PARAM_BYPASS
 } t_env_reverb_params;
 
-//t_reverb_properties is equal to SLEnvironmentalReverbSettings defined in OpenSL ES specification.
-typedef struct s_reverb_properties {
+//t_reverb_settings is equal to SLEnvironmentalReverbSettings defined in OpenSL ES specification.
+typedef struct s_reverb_settings {
     int16_t roomLevel;
     int16_t roomHFLevel;
     int32_t decayTime;
     int16_t decayHFRatio;
     int16_t reflectionsLevel;
     int32_t reflectionsDelay;
-    int32_t reverbDelay;
     int16_t reverbLevel;
+    int32_t reverbDelay;
     int16_t diffusion;
     int16_t density;
-    int16_t padding;
-} t_reverb_properties;
+} __attribute__((packed)) t_reverb_settings;
 
 
 #if __cplusplus
diff --git a/include/media/EffectEqualizerApi.h b/include/media/EffectEqualizerApi.h
index cb05b32..0492ea0 100644
--- a/include/media/EffectEqualizerApi.h
+++ b/include/media/EffectEqualizerApi.h
@@ -19,8 +19,10 @@
 
 #include <media/EffectApi.h>
 
-// for the definition of SL_IID_EQUALIZER
-#include "OpenSLES.h"
+#ifndef OPENSL_ES_H_
+static const effect_uuid_t SL_IID_EQUALIZER_ = { 0x0bed4300, 0xddd6, 0x11db, 0x8f34, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
+const effect_uuid_t * const SL_IID_EQUALIZER = &SL_IID_EQUALIZER_;
+#endif //OPENSL_ES_H_
 
 #if __cplusplus
 extern "C" {
@@ -37,9 +39,16 @@
     EQ_PARAM_GET_BAND,              // Gets the band that has the most effect on the given frequency.
     EQ_PARAM_CUR_PRESET,            // Gets/Sets the current preset.
     EQ_PARAM_GET_NUM_OF_PRESETS,    // Gets the total number of presets the equalizer supports.
-    EQ_PARAM_GET_PRESET_NAME        // Gets the preset name based on the index.
+    EQ_PARAM_GET_PRESET_NAME,       // Gets the preset name based on the index.
+    EQ_PARAM_PROPERTIES             // Gets/Sets all parameters at a time.
 } t_equalizer_params;
 
+//t_equalizer_settings groups all current equalizer setting for backup and restore.
+typedef struct s_equalizer_settings {
+    uint16_t curPreset;
+    uint16_t numBands;
+    uint16_t bandLevels[];
+} t_equalizer_settings;
 
 #if __cplusplus
 }  // extern "C"
diff --git a/include/media/EffectPresetReverbApi.h b/include/media/EffectPresetReverbApi.h
index 34ffffe..53205bb 100644
--- a/include/media/EffectPresetReverbApi.h
+++ b/include/media/EffectPresetReverbApi.h
@@ -23,10 +23,10 @@
 extern "C" {
 #endif
 
-// TODO: include OpenSLES_IID.h instead
-
+#ifndef OPENSL_ES_H_
 static const effect_uuid_t SL_IID_PRESETREVERB_ = { 0x47382d60, 0xddd8, 0x11db, 0xbf3a, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
 const effect_uuid_t * const SL_IID_PRESETREVERB = &SL_IID_PRESETREVERB_;
+#endif //OPENSL_ES_H_
 
 /* enumerated parameter settings for preset reverb effect */
 typedef enum
diff --git a/include/media/EffectVirtualizerApi.h b/include/media/EffectVirtualizerApi.h
index 601c384..c3d5131 100644
--- a/include/media/EffectVirtualizerApi.h
+++ b/include/media/EffectVirtualizerApi.h
@@ -23,9 +23,10 @@
 extern "C" {
 #endif
 
-// TODO: include OpenSLES_IID.h instead
+#ifndef OPENSL_ES_H_
 static const effect_uuid_t SL_IID_VIRTUALIZER_ = { 0x37cc2c00, 0xdddd, 0x11db, 0x8577, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
 const effect_uuid_t * const SL_IID_VIRTUALIZER = &SL_IID_VIRTUALIZER_;
+#endif //OPENSL_ES_H_
 
 /* enumerated parameter settings for virtualizer effect */
 typedef enum
diff --git a/include/media/EffectVisualizerApi.h b/include/media/EffectVisualizerApi.h
index 1155db8..bef1a4f 100644
--- a/include/media/EffectVisualizerApi.h
+++ b/include/media/EffectVisualizerApi.h
@@ -23,10 +23,11 @@
 extern "C" {
 #endif
 
-//TODO replace by openSL ES include when available
+#ifndef OPENSL_ES_H_
 static const effect_uuid_t SL_IID_VISUALIZATION_ =
     { 0xe46b26a0, 0xdddd, 0x11db, 0x8afd, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
 const effect_uuid_t * const SL_IID_VISUALIZATION = &SL_IID_VISUALIZATION_;
+#endif //OPENSL_ES_H_
 
 #define VISUALIZER_CAPTURE_SIZE_MAX 1024  // maximum capture size in samples
 #define VISUALIZER_CAPTURE_SIZE_MIN 128   // minimum capture size in samples
diff --git a/libs/hwui/FontRenderer.cpp b/libs/hwui/FontRenderer.cpp
index 14ad7d7..5595ab0 100644
--- a/libs/hwui/FontRenderer.cpp
+++ b/libs/hwui/FontRenderer.cpp
@@ -80,6 +80,21 @@
             nPenX, nPenY - height, 0, u1, v1);
 }
 
+Font::CachedGlyphInfo* Font::getCachedUTFChar(SkPaint* paint, int32_t utfChar) {
+    CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueFor(utfChar);
+    if (cachedGlyph == NULL) {
+        cachedGlyph = cacheGlyph(paint, utfChar);
+    }
+
+    // Is the glyph still in texture cache?
+    if (!cachedGlyph->mIsValid) {
+        const SkGlyph& skiaGlyph = paint->getUnicharMetrics(utfChar);
+        updateGlyphCache(paint, skiaGlyph, cachedGlyph);
+    }
+
+    return cachedGlyph;
+}
+
 void Font::renderUTF(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
         int numGlyphs, int x, int y) {
     if (numGlyphs == 0 || text == NULL || len == 0) {
@@ -102,16 +117,7 @@
             break;
         }
 
-        CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueFor(utfChar);
-        if (cachedGlyph == NULL) {
-            cachedGlyph = cacheGlyph(paint, utfChar);
-        }
-
-        // Is the glyph still in texture cache?
-        if (!cachedGlyph->mIsValid) {
-            const SkGlyph& skiaGlyph = paint->getUnicharMetrics(utfChar);
-            updateGlyphCache(paint, skiaGlyph, cachedGlyph);
-        }
+        CachedGlyphInfo* cachedGlyph = getCachedUTFChar(paint, utfChar);
 
         // If it's still not valid, we couldn't cache it, so we shouldn't draw garbage
         if (cachedGlyph->mIsValid) {
@@ -340,6 +346,8 @@
     nextLine += mCacheLines.top()->mMaxHeight;
     mCacheLines.push(new CacheTextureLine(mCacheWidth, 24, nextLine, 0));
     nextLine += mCacheLines.top()->mMaxHeight;
+    mCacheLines.push(new CacheTextureLine(mCacheWidth, 24, nextLine, 0));
+    nextLine += mCacheLines.top()->mMaxHeight;
     mCacheLines.push(new CacheTextureLine(mCacheWidth, 32, nextLine, 0));
     nextLine += mCacheLines.top()->mMaxHeight;
     mCacheLines.push(new CacheTextureLine(mCacheWidth, 32, nextLine, 0));
@@ -392,6 +400,12 @@
     initTextTexture();
     initVertexArrayBuffers();
 
+    // We store a string with letters in a rough frequency of occurrence
+    mLatinPrecache = String16("eisarntolcdugpmhbyfvkwzxjq ");
+    mLatinPrecache += String16("EISARNTOLCDUGPMHBYFVKWZXJQ");
+    mLatinPrecache += String16(",.?!()-+@;:`'");
+    mLatinPrecache += String16("0123456789");
+
     mInitialized = true;
 }
 
@@ -484,8 +498,38 @@
     }
 }
 
-void FontRenderer::setFont(uint32_t fontId, float fontSize) {
+uint32_t FontRenderer::getRemainingCacheCapacity() {
+    uint32_t remainingCapacity = 0;
+    float totalPixels = 0;
+    for(uint32_t i = 0; i < mCacheLines.size(); i ++) {
+         remainingCapacity += (mCacheLines[i]->mMaxWidth - mCacheLines[i]->mCurrentCol);
+         totalPixels += mCacheLines[i]->mMaxWidth;
+    }
+    remainingCapacity = (remainingCapacity * 100) / totalPixels;
+    return remainingCapacity;
+}
+
+void FontRenderer::precacheLatin(SkPaint* paint) {
+    // Remaining capacity is measured in %
+    uint32_t remainingCapacity = getRemainingCacheCapacity();
+    uint32_t precacheIdx = 0;
+    while(remainingCapacity > 25 && precacheIdx < mLatinPrecache.size()) {
+        mCurrentFont->getCachedUTFChar(paint, (int32_t)mLatinPrecache[precacheIdx]);
+        remainingCapacity = getRemainingCacheCapacity();
+        precacheIdx ++;
+    }
+}
+
+void FontRenderer::setFont(SkPaint* paint, uint32_t fontId, float fontSize) {
+    uint32_t currentNumFonts = mActiveFonts.size();
     mCurrentFont = Font::create(this, fontId, fontSize);
+
+    const float maxPrecacheFontSize = 40.0f;
+    bool isNewFont = currentNumFonts != mActiveFonts.size();
+
+    if(isNewFont && fontSize <= maxPrecacheFontSize ){
+        precacheLatin(paint);
+    }
 }
 
 void FontRenderer::renderText(SkPaint* paint, const Rect* clip, const char *text,
diff --git a/libs/hwui/FontRenderer.h b/libs/hwui/FontRenderer.h
index bacd3aa..2f7a8ba 100644
--- a/libs/hwui/FontRenderer.h
+++ b/libs/hwui/FontRenderer.h
@@ -18,6 +18,7 @@
 #define ANDROID_UI_FONT_RENDERER_H
 
 #include <utils/String8.h>
+#include <utils/String16.h>
 #include <utils/Vector.h>
 #include <utils/KeyedVector.h>
 
@@ -82,10 +83,12 @@
 
     void invalidateTextureCache();
 
-    CachedGlyphInfo *cacheGlyph(SkPaint* paint, int32_t glyph);
+    CachedGlyphInfo* cacheGlyph(SkPaint* paint, int32_t glyph);
     void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo *glyph);
     void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y);
 
+    CachedGlyphInfo* getCachedUTFChar(SkPaint* paint, int32_t utfChar);
+
     FontRenderer* mState;
     uint32_t mFontId;
     float mFontSize;
@@ -99,7 +102,7 @@
     void init();
     void deinit();
 
-    void setFont(uint32_t fontId, float fontSize);
+    void setFont(SkPaint* paint, uint32_t fontId, float fontSize);
     void renderText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
             uint32_t len, int numGlyphs, int x, int y);
 
@@ -160,6 +163,9 @@
 
     void checkInit();
 
+    String16 mLatinPrecache;
+    void precacheLatin(SkPaint* paint);
+
     void issueDrawCommand();
     void appendMeshQuad(float x1, float y1, float z1, float u1, float v1, float x2, float y2,
             float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3,
@@ -169,6 +175,7 @@
     uint32_t mCacheHeight;
 
     Vector<CacheTextureLine*> mCacheLines;
+    uint32_t getRemainingCacheCapacity();
 
     Font* mCurrentFont;
     Vector<Font*> mActiveFonts;
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index d30d718..2e44e122 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -569,7 +569,7 @@
     // TODO: Implement scale properly
     const Rect& clip = mSnapshot->getLocalClip();
 
-    mFontRenderer.setFont(SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize());
+    mFontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize());
     mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
 
     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
diff --git a/libs/rs/rsContext.cpp b/libs/rs/rsContext.cpp
index e897d00..d6df581 100644
--- a/libs/rs/rsContext.cpp
+++ b/libs/rs/rsContext.cpp
@@ -468,8 +468,11 @@
     timerInit();
     timerSet(RS_TIMER_INTERNAL);
 
-    LOGV("RS Launching thread(s)");
-    mWorkers.mCount = 2;
+    int cpu = sysconf(_SC_NPROCESSORS_ONLN);
+    LOGV("RS Launching thread(s), reported CPU count %i", cpu);
+    if (cpu < 2) cpu = 0;
+
+    mWorkers.mCount = (uint32_t)cpu;
     mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
     mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
     mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
diff --git a/media/java/android/media/BassBoost.java b/media/java/android/media/BassBoost.java
index ef4ce05..75c2c88 100644
--- a/media/java/android/media/BassBoost.java
+++ b/media/java/android/media/BassBoost.java
@@ -19,17 +19,19 @@
 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
+import android.media.AudioEffect;
 import android.os.Bundle;
 import android.util.Log;
+
 import java.nio.ByteOrder;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
+import java.util.StringTokenizer;
 
-import android.media.AudioEffect;
 
 /**
  * Bass boost is an audio effect to boost or amplify low frequencies of the sound. It is comparable
- * to an simple equalizer but limited to one band amplification in the low frequency range.
+ * to a simple equalizer but limited to one band amplification in the low frequency range.
  * <p>An application creates a BassBoost object to instantiate and control a bass boost engine
  * in the audio framework.
  * <p>The methods, parameter types and units exposed by the BassBoost implementation are directly
@@ -210,4 +212,82 @@
             }
         }
     }
+
+    /**
+     * The Settings class regroups all bass boost parameters. It is used in
+     * conjuntion with getProperties() and setProperties() methods to backup and restore
+     * all parameters in a single call.
+     */
+    public static class Settings {
+        public short strength;
+
+        public Settings() {
+        }
+
+        /**
+         * Settings class constructor from a key=value; pairs formatted string. The string is
+         * typically returned by Settings.toString() method.
+         * @throws IllegalArgumentException if the string is not correctly formatted.
+         */
+        public Settings(String settings) {
+            StringTokenizer st = new StringTokenizer(settings, "=;");
+            int tokens = st.countTokens();
+            if (st.countTokens() != 3) {
+                throw new IllegalArgumentException("settings: " + settings);
+            }
+            String key = st.nextToken();
+            if (!key.equals("BassBoost")) {
+                throw new IllegalArgumentException(
+                        "invalid settings for BassBoost: " + key);
+            }
+            try {
+                key = st.nextToken();
+                if (!key.equals("strength")) {
+                    throw new IllegalArgumentException("invalid key name: " + key);
+                }
+                strength = Short.parseShort(st.nextToken());
+             } catch (NumberFormatException nfe) {
+                throw new IllegalArgumentException("invalid value for key: " + key);
+            }
+        }
+
+        @Override
+        public String toString() {
+            String str = new String (
+                    "BassBoost"+
+                    ";strength="+Short.toString(strength)
+                    );
+            return str;
+        }
+    };
+
+
+    /**
+     * Gets the bass boost properties. This method is useful when a snapshot of current
+     * bass boost settings must be saved by the application.
+     * @return a BassBoost.Settings object containing all current parameters values
+     * @throws IllegalStateException
+     * @throws IllegalArgumentException
+     * @throws UnsupportedOperationException
+     */
+    public BassBoost.Settings getProperties()
+    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
+        Settings settings = new Settings();
+        short[] value = new short[1];
+        checkStatus(getParameter(PARAM_STRENGTH, value));
+        settings.strength = value[0];
+        return settings;
+    }
+
+    /**
+     * Sets the bass boost properties. This method is useful when bass boost settings have to
+     * be applied from a previous backup.
+     * @throws IllegalStateException
+     * @throws IllegalArgumentException
+     * @throws UnsupportedOperationException
+     */
+    public void setProperties(BassBoost.Settings settings)
+    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
+        checkStatus(setParameter(PARAM_STRENGTH, settings.strength));
+    }
 }
diff --git a/media/java/android/media/EnvironmentalReverb.java b/media/java/android/media/EnvironmentalReverb.java
index 88230fc..3cc8452 100644
--- a/media/java/android/media/EnvironmentalReverb.java
+++ b/media/java/android/media/EnvironmentalReverb.java
@@ -19,12 +19,13 @@
 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
+import android.media.AudioEffect;
 import android.os.Bundle;
 import android.util.Log;
+
 import java.nio.ByteOrder;
 import java.nio.ByteBuffer;
-
-import android.media.AudioEffect;
+import java.util.StringTokenizer;
 
 /**
  * A sound generated within a room travels in many directions. The listener first hears the
@@ -107,6 +108,9 @@
      */
     public static final int PARAM_DENSITY = 9;
 
+    // used by setProperties()/getProperties
+    private static final int PARAM_PROPERTIES = 10;
+
     /**
      * Registered listener for parameter changes
      */
@@ -142,7 +146,6 @@
     public EnvironmentalReverb(int priority, int audioSession)
     throws IllegalArgumentException, UnsupportedOperationException, RuntimeException {
         super(EFFECT_TYPE_ENV_REVERB, EFFECT_TYPE_NULL, priority, audioSession);
-        Log.e(TAG, "contructor");
     }
 
     /**
@@ -501,4 +504,169 @@
             }
         }
     }
+
+    /**
+     * The Settings class regroups all environmental reverb parameters. It is used in
+     * conjuntion with getProperties() and setProperties() methods to backup and restore
+     * all parameters in a single call.
+     */
+    public static class Settings {
+        public short roomLevel;
+        public short roomHFLevel;
+        public int decayTime;
+        public short decayHFRatio;
+        public short reflectionsLevel;
+        public int reflectionsDelay;
+        public short reverbLevel;
+        public int reverbDelay;
+        public short diffusion;
+        public short density;
+
+        public Settings() {
+        }
+
+        /**
+         * Settings class constructor from a key=value; pairs formatted string. The string is
+         * typically returned by Settings.toString() method.
+         * @throws IllegalArgumentException if the string is not correctly formatted.
+         */
+        public Settings(String settings) {
+            StringTokenizer st = new StringTokenizer(settings, "=;");
+            int tokens = st.countTokens();
+            if (st.countTokens() != 21) {
+                throw new IllegalArgumentException("settings: " + settings);
+            }
+            String key = st.nextToken();
+            if (!key.equals("EnvironmentalReverb")) {
+                throw new IllegalArgumentException(
+                        "invalid settings for EnvironmentalReverb: " + key);
+            }
+
+            try {
+                key = st.nextToken();
+                if (!key.equals("roomLevel")) {
+                    throw new IllegalArgumentException("invalid key name: " + key);
+                }
+                roomLevel = Short.parseShort(st.nextToken());
+                key = st.nextToken();
+                if (!key.equals("roomHFLevel")) {
+                    throw new IllegalArgumentException("invalid key name: " + key);
+                }
+                roomHFLevel = Short.parseShort(st.nextToken());
+                key = st.nextToken();
+                if (!key.equals("decayTime")) {
+                    throw new IllegalArgumentException("invalid key name: " + key);
+                }
+                decayTime = Integer.parseInt(st.nextToken());
+                key = st.nextToken();
+                if (!key.equals("decayHFRatio")) {
+                    throw new IllegalArgumentException("invalid key name: " + key);
+                }
+                decayHFRatio = Short.parseShort(st.nextToken());
+                key = st.nextToken();
+                if (!key.equals("reflectionsLevel")) {
+                    throw new IllegalArgumentException("invalid key name: " + key);
+                }
+                reflectionsLevel = Short.parseShort(st.nextToken());
+                key = st.nextToken();
+                if (!key.equals("reflectionsDelay")) {
+                    throw new IllegalArgumentException("invalid key name: " + key);
+                }
+                reflectionsDelay = Integer.parseInt(st.nextToken());
+                key = st.nextToken();
+                if (!key.equals("reverbLevel")) {
+                    throw new IllegalArgumentException("invalid key name: " + key);
+                }
+                reverbLevel = Short.parseShort(st.nextToken());
+                key = st.nextToken();
+                if (!key.equals("reverbDelay")) {
+                    throw new IllegalArgumentException("invalid key name: " + key);
+                }
+                reverbDelay = Integer.parseInt(st.nextToken());
+                key = st.nextToken();
+                if (!key.equals("diffusion")) {
+                    throw new IllegalArgumentException("invalid key name: " + key);
+                }
+                diffusion = Short.parseShort(st.nextToken());
+                key = st.nextToken();
+                if (!key.equals("density")) {
+                    throw new IllegalArgumentException("invalid key name: " + key);
+                }
+                density = Short.parseShort(st.nextToken());
+             } catch (NumberFormatException nfe) {
+                throw new IllegalArgumentException("invalid value for key: " + key);
+            }
+        }
+
+        @Override
+        public String toString() {
+            return new String (
+                    "EnvironmentalReverb"+
+                    ";roomLevel="+Short.toString(roomLevel)+
+                    ";roomHFLevel="+Short.toString(roomHFLevel)+
+                    ";decayTime="+Integer.toString(decayTime)+
+                    ";decayHFRatio="+Short.toString(decayHFRatio)+
+                    ";reflectionsLevel="+Short.toString(reflectionsLevel)+
+                    ";reflectionsDelay="+Integer.toString(reflectionsDelay)+
+                    ";reverbLevel="+Short.toString(reverbLevel)+
+                    ";reverbDelay="+Integer.toString(reverbDelay)+
+                    ";diffusion="+Short.toString(diffusion)+
+                    ";density="+Short.toString(density)
+                    );
+        }
+    };
+
+    // Keep this in sync with sizeof(s_reverb_settings) defined in
+    // frameworks/base/include/media/EffectEnvironmentalReverbApi.h
+    static private int PROPERTY_SIZE = 26;
+
+    /**
+     * Gets the environmental reverb properties. This method is useful when a snapshot of current
+     * reverb settings must be saved by the application.
+     * @return an EnvironmentalReverb.Settings object containing all current parameters values
+     * @throws IllegalStateException
+     * @throws IllegalArgumentException
+     * @throws UnsupportedOperationException
+     */
+    public EnvironmentalReverb.Settings getProperties()
+    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
+        byte[] param = new byte[PROPERTY_SIZE];
+        checkStatus(getParameter(PARAM_PROPERTIES, param));
+        Settings settings = new Settings();
+        settings.roomLevel = byteArrayToShort(param, 0);
+        settings.roomHFLevel = byteArrayToShort(param, 2);
+        settings.decayTime = byteArrayToInt(param, 4);
+        settings.decayHFRatio = byteArrayToShort(param, 8);
+        settings.reflectionsLevel = byteArrayToShort(param, 10);
+        settings.reflectionsDelay = byteArrayToInt(param, 12);
+        settings.reverbLevel = byteArrayToShort(param, 16);
+        settings.reverbDelay = byteArrayToInt(param, 18);
+        settings.diffusion = byteArrayToShort(param, 22);
+        settings.density = byteArrayToShort(param, 24);
+        return settings;
+    }
+
+    /**
+     * Sets the environmental reverb properties. This method is useful when reverb settings have to
+     * be applied from a previous backup.
+     * @throws IllegalStateException
+     * @throws IllegalArgumentException
+     * @throws UnsupportedOperationException
+     */
+    public void setProperties(EnvironmentalReverb.Settings settings)
+    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
+
+        byte[] param = concatArrays(shortToByteArray(settings.roomLevel),
+                                    shortToByteArray(settings.roomHFLevel),
+                                    intToByteArray(settings.decayTime),
+                                    shortToByteArray(settings.decayHFRatio),
+                                    shortToByteArray(settings.reflectionsLevel),
+                                    intToByteArray(settings.reflectionsDelay),
+                                    shortToByteArray(settings.reverbLevel),
+                                    intToByteArray(settings.reverbDelay),
+                                    shortToByteArray(settings.diffusion),
+                                    shortToByteArray(settings.density));
+
+        checkStatus(setParameter(PARAM_PROPERTIES, param));
+    }
 }
diff --git a/media/java/android/media/Equalizer.java b/media/java/android/media/Equalizer.java
index 082f694..21c37bb 100644
--- a/media/java/android/media/Equalizer.java
+++ b/media/java/android/media/Equalizer.java
@@ -19,13 +19,15 @@
 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
+import android.media.AudioEffect;
 import android.os.Bundle;
 import android.util.Log;
+
 import java.nio.ByteOrder;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
+import java.util.StringTokenizer;
 
-import android.media.AudioEffect;
 
 /**
  * An Equalizer is used to alter the frequency response of a particular music source or of the main
@@ -87,12 +89,19 @@
      * Request preset name. Parameter ID for OnParameterChangeListener
      */
     public static final int PARAM_GET_PRESET_NAME = 8;
+    // used by setProperties()/getProperties
+    private static final int PARAM_PROPERTIES = 9;
     /**
      * maximum size for perset name
      */
     public static final int PARAM_STRING_SIZE_MAX = 32;
 
     /**
+     * Number of bands implemented by Equalizer engine
+     */
+    private short mNumBands = 0;
+
+    /**
      * Number of presets implemented by Equalizer engine
      */
     private int mNumPresets;
@@ -136,6 +145,8 @@
            UnsupportedOperationException, RuntimeException {
         super(EFFECT_TYPE_EQUALIZER, EFFECT_TYPE_NULL, priority, audioSession);
 
+        getNumberOfBands();
+
         mNumPresets = (int)getNumberOfPresets();
 
         if (mNumPresets != 0) {
@@ -150,7 +161,6 @@
                 while (value[length] != 0) length++;
                 try {
                     mPresetNames[i] = new String(value, 0, length, "ISO-8859-1");
-                    Log.e(TAG, "preset #: "+i+" name: "+mPresetNames[i]+" length: "+length);
                 } catch (java.io.UnsupportedEncodingException e) {
                     Log.e(TAG, "preset name decode error");
                 }
@@ -167,11 +177,15 @@
      */
     public short getNumberOfBands()
     throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
+        if (mNumBands != 0) {
+            return mNumBands;
+        }
         int[] param = new int[1];
         param[0] = PARAM_NUM_BANDS;
         short[] value = new short[1];
         checkStatus(getParameter(param, value));
-        return value[0];
+        mNumBands = value[0];
+        return mNumBands;
     }
 
     /**
@@ -440,4 +454,120 @@
         }
     }
 
+    /**
+     * The Settings class regroups all equalizer parameters. It is used in
+     * conjuntion with getProperties() and setProperties() methods to backup and restore
+     * all parameters in a single call.
+     */
+    public static class Settings {
+        public short curPreset;
+        public short numBands = 0;
+        public short[] bandLevels = null;
+
+        public Settings() {
+        }
+
+        /**
+         * Settings class constructor from a key=value; pairs formatted string. The string is
+         * typically returned by Settings.toString() method.
+         * @throws IllegalArgumentException if the string is not correctly formatted.
+         */
+        public Settings(String settings) {
+            StringTokenizer st = new StringTokenizer(settings, "=;");
+            int tokens = st.countTokens();
+            if (st.countTokens() < 5) {
+                throw new IllegalArgumentException("settings: " + settings);
+            }
+            String key = st.nextToken();
+            if (!key.equals("Equalizer")) {
+                throw new IllegalArgumentException(
+                        "invalid settings for Equalizer: " + key);
+            }
+            try {
+                key = st.nextToken();
+                if (!key.equals("curPreset")) {
+                    throw new IllegalArgumentException("invalid key name: " + key);
+                }
+                curPreset = Short.parseShort(st.nextToken());
+                key = st.nextToken();
+                if (!key.equals("numBands")) {
+                    throw new IllegalArgumentException("invalid key name: " + key);
+                }
+                numBands = Short.parseShort(st.nextToken());
+                if (st.countTokens() != numBands*2) {
+                    throw new IllegalArgumentException("settings: " + settings);
+                }
+                bandLevels = new short[numBands];
+                for (int i = 0; i < numBands; i++) {
+                    key = st.nextToken();
+                    if (!key.equals("band"+(i+1)+"Level")) {
+                        throw new IllegalArgumentException("invalid key name: " + key);
+                    }
+                    bandLevels[i] = Short.parseShort(st.nextToken());
+                }
+             } catch (NumberFormatException nfe) {
+                throw new IllegalArgumentException("invalid value for key: " + key);
+            }
+        }
+
+        @Override
+        public String toString() {
+
+            String str = new String (
+                    "Equalizer"+
+                    ";curPreset="+Short.toString(curPreset)+
+                    ";numBands="+Short.toString(numBands)
+                    );
+            for (int i = 0; i < numBands; i++) {
+                str = str.concat(";band"+(i+1)+"Level="+Short.toString(bandLevels[i]));
+            }
+            return str;
+        }
+    };
+
+
+    /**
+     * Gets the equalizer properties. This method is useful when a snapshot of current
+     * equalizer settings must be saved by the application.
+     * @return an Equalizer.Settings object containing all current parameters values
+     * @throws IllegalStateException
+     * @throws IllegalArgumentException
+     * @throws UnsupportedOperationException
+     */
+    public Equalizer.Settings getProperties()
+    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
+        byte[] param = new byte[4 + mNumBands * 2];
+        checkStatus(getParameter(PARAM_PROPERTIES, param));
+        Settings settings = new Settings();
+        settings.curPreset = byteArrayToShort(param, 0);
+        settings.numBands = byteArrayToShort(param, 2);
+        settings.bandLevels = new short[mNumBands];
+        for (int i = 0; i < mNumBands; i++) {
+            settings.bandLevels[i] = byteArrayToShort(param, 4 + 2*i);
+        }
+        return settings;
+    }
+
+    /**
+     * Sets the equalizer properties. This method is useful when equalizer settings have to
+     * be applied from a previous backup.
+     * @throws IllegalStateException
+     * @throws IllegalArgumentException
+     * @throws UnsupportedOperationException
+     */
+    public void setProperties(Equalizer.Settings settings)
+    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
+        if (settings.numBands != settings.bandLevels.length ||
+            settings.numBands != mNumBands) {
+            throw new IllegalArgumentException("settings invalid band count: " +settings.numBands);
+        }
+
+        byte[] param = concatArrays(shortToByteArray(settings.curPreset),
+                                    shortToByteArray(mNumBands));
+        for (int i = 0; i < mNumBands; i++) {
+            param = concatArrays(param,
+                                 shortToByteArray(settings.bandLevels[i]));
+        }
+        checkStatus(setParameter(PARAM_PROPERTIES, param));
+    }
 }
diff --git a/media/java/android/media/MtpClient.java b/media/java/android/media/MtpClient.java
index 0fe9bb4..1aebcb8 100644
--- a/media/java/android/media/MtpClient.java
+++ b/media/java/android/media/MtpClient.java
@@ -16,6 +16,7 @@
 
 package android.media;
 
+import android.os.ParcelFileDescriptor;
 import android.util.Log;
 
 /**
@@ -64,6 +65,11 @@
         return native_get_storage_id(deviceID, objectID);
     }
 
+    // create a file descriptor for reading the contents of an object over MTP
+    public ParcelFileDescriptor openFile(int deviceID, int objectID) {
+        return native_open_file(deviceID, objectID);
+    }
+
     public interface Listener {
         // called when a new MTP device has been discovered
         void deviceAdded(int id);
@@ -94,4 +100,5 @@
     private native boolean native_delete_object(int deviceID, int objectID);
     private native int native_get_parent(int deviceID, int objectID);
     private native int native_get_storage_id(int deviceID, int objectID);
+    private native ParcelFileDescriptor native_open_file(int deviceID, int objectID);
 }
diff --git a/media/java/android/media/PresetReverb.java b/media/java/android/media/PresetReverb.java
index 83a01a4..c7d7037 100644
--- a/media/java/android/media/PresetReverb.java
+++ b/media/java/android/media/PresetReverb.java
@@ -19,12 +19,14 @@
 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
+import android.media.AudioEffect;
 import android.os.Bundle;
 import android.util.Log;
+
 import java.nio.ByteOrder;
 import java.nio.ByteBuffer;
+import java.util.StringTokenizer;
 
-import android.media.AudioEffect;
 
 /**
  * A sound generated within a room travels in many directions. The listener first hears the
@@ -116,7 +118,6 @@
     public PresetReverb(int priority, int audioSession)
     throws IllegalArgumentException, UnsupportedOperationException, RuntimeException {
         super(EFFECT_TYPE_PRESET_REVERB, EFFECT_TYPE_NULL, priority, audioSession);
-        Log.e(TAG, "contructor");
     }
 
     /**
@@ -144,10 +145,8 @@
      */
     public short getPreset()
     throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
-        int[] param = new int[1];
-        param[0] = PARAM_PRESET;
         short[] value = new short[1];
-        checkStatus(getParameter(param, value));
+        checkStatus(getParameter(PARAM_PRESET, value));
         return value[0];
     }
 
@@ -216,4 +215,82 @@
             }
         }
     }
+
+    /**
+     * The Settings class regroups all preset reverb parameters. It is used in
+     * conjuntion with getProperties() and setProperties() methods to backup and restore
+     * all parameters in a single call.
+     */
+    public static class Settings {
+        public short preset;
+
+        public Settings() {
+        }
+
+        /**
+         * Settings class constructor from a key=value; pairs formatted string. The string is
+         * typically returned by Settings.toString() method.
+         * @throws IllegalArgumentException if the string is not correctly formatted.
+         */
+        public Settings(String settings) {
+            StringTokenizer st = new StringTokenizer(settings, "=;");
+            int tokens = st.countTokens();
+            if (st.countTokens() != 3) {
+                throw new IllegalArgumentException("settings: " + settings);
+            }
+            String key = st.nextToken();
+            if (!key.equals("PresetReverb")) {
+                throw new IllegalArgumentException(
+                        "invalid settings for PresetReverb: " + key);
+            }
+            try {
+                key = st.nextToken();
+                if (!key.equals("preset")) {
+                    throw new IllegalArgumentException("invalid key name: " + key);
+                }
+                preset = Short.parseShort(st.nextToken());
+             } catch (NumberFormatException nfe) {
+                throw new IllegalArgumentException("invalid value for key: " + key);
+            }
+        }
+
+        @Override
+        public String toString() {
+            String str = new String (
+                    "PresetReverb"+
+                    ";preset="+Short.toString(preset)
+                    );
+            return str;
+        }
+    };
+
+
+    /**
+     * Gets the preset reverb properties. This method is useful when a snapshot of current
+     * preset reverb settings must be saved by the application.
+     * @return a PresetReverb.Settings object containing all current parameters values
+     * @throws IllegalStateException
+     * @throws IllegalArgumentException
+     * @throws UnsupportedOperationException
+     */
+    public PresetReverb.Settings getProperties()
+    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
+        Settings settings = new Settings();
+        short[] value = new short[1];
+        checkStatus(getParameter(PARAM_PRESET, value));
+        settings.preset = value[0];
+        return settings;
+    }
+
+    /**
+     * Sets the preset reverb properties. This method is useful when preset reverb settings have to
+     * be applied from a previous backup.
+     * @throws IllegalStateException
+     * @throws IllegalArgumentException
+     * @throws UnsupportedOperationException
+     */
+    public void setProperties(PresetReverb.Settings settings)
+    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
+        checkStatus(setParameter(PARAM_PRESET, settings.preset));
+    }
 }
diff --git a/media/java/android/media/Virtualizer.java b/media/java/android/media/Virtualizer.java
index 9f71297..2c8909e 100644
--- a/media/java/android/media/Virtualizer.java
+++ b/media/java/android/media/Virtualizer.java
@@ -19,13 +19,15 @@
 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
+import android.media.AudioEffect;
 import android.os.Bundle;
 import android.util.Log;
+
 import java.nio.ByteOrder;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
+import java.util.StringTokenizer;
 
-import android.media.AudioEffect;
 
 /**
  * An audio virtualizer is a general name for an effect to spatialize audio channels. The exact
@@ -211,4 +213,82 @@
             }
         }
     }
+
+    /**
+     * The Settings class regroups all virtualizer parameters. It is used in
+     * conjuntion with getProperties() and setProperties() methods to backup and restore
+     * all parameters in a single call.
+     */
+    public static class Settings {
+        public short strength;
+
+        public Settings() {
+        }
+
+        /**
+         * Settings class constructor from a key=value; pairs formatted string. The string is
+         * typically returned by Settings.toString() method.
+         * @throws IllegalArgumentException if the string is not correctly formatted.
+         */
+        public Settings(String settings) {
+            StringTokenizer st = new StringTokenizer(settings, "=;");
+            int tokens = st.countTokens();
+            if (st.countTokens() != 3) {
+                throw new IllegalArgumentException("settings: " + settings);
+            }
+            String key = st.nextToken();
+            if (!key.equals("Virtualizer")) {
+                throw new IllegalArgumentException(
+                        "invalid settings for Virtualizer: " + key);
+            }
+            try {
+                key = st.nextToken();
+                if (!key.equals("strength")) {
+                    throw new IllegalArgumentException("invalid key name: " + key);
+                }
+                strength = Short.parseShort(st.nextToken());
+             } catch (NumberFormatException nfe) {
+                throw new IllegalArgumentException("invalid value for key: " + key);
+            }
+        }
+
+        @Override
+        public String toString() {
+            String str = new String (
+                    "Virtualizer"+
+                    ";strength="+Short.toString(strength)
+                    );
+            return str;
+        }
+    };
+
+
+    /**
+     * Gets the virtualizer properties. This method is useful when a snapshot of current
+     * virtualizer settings must be saved by the application.
+     * @return a Virtualizer.Settings object containing all current parameters values
+     * @throws IllegalStateException
+     * @throws IllegalArgumentException
+     * @throws UnsupportedOperationException
+     */
+    public Virtualizer.Settings getProperties()
+    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
+        Settings settings = new Settings();
+        short[] value = new short[1];
+        checkStatus(getParameter(PARAM_STRENGTH, value));
+        settings.strength = value[0];
+        return settings;
+    }
+
+    /**
+     * Sets the virtualizer properties. This method is useful when virtualizer settings have to
+     * be applied from a previous backup.
+     * @throws IllegalStateException
+     * @throws IllegalArgumentException
+     * @throws UnsupportedOperationException
+     */
+    public void setProperties(Virtualizer.Settings settings)
+    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
+        checkStatus(setParameter(PARAM_STRENGTH, settings.strength));
+    }
 }
diff --git a/media/jni/android_media_MtpClient.cpp b/media/jni/android_media_MtpClient.cpp
index f69053c..67740bc 100644
--- a/media/jni/android_media_MtpClient.cpp
+++ b/media/jni/android_media_MtpClient.cpp
@@ -29,6 +29,7 @@
 
 #include "MtpClient.h"
 #include "MtpDevice.h"
+#include "MtpObjectInfo.h"
 
 using namespace android;
 
@@ -38,6 +39,19 @@
 static jmethodID method_deviceRemoved;
 static jfieldID field_context;
 
+static struct file_descriptor_offsets_t
+{
+    jclass mClass;
+    jmethodID mConstructor;
+    jfieldID mDescriptor;
+} gFileDescriptorOffsets;
+
+static struct parcel_file_descriptor_offsets_t
+{
+    jclass mClass;
+    jmethodID mConstructor;
+} gParcelFileDescriptorOffsets;
+
 #ifdef HAVE_ANDROID_OS
 
 static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
@@ -187,6 +201,38 @@
         return -1;
 }
 
+static jobject
+android_media_MtpClient_open_file(JNIEnv *env, jobject thiz,
+        jint device_id, jint object_id)
+{
+#ifdef HAVE_ANDROID_OS
+    MyClient *client = (MyClient *)env->GetIntField(thiz, field_context);
+    MtpDevice* device = client->getDevice(device_id);
+    if (!device)
+        return NULL;
+
+    MtpObjectInfo* info = device->getObjectInfo(object_id);
+    if (!info)
+        return NULL;
+    int object_size = info->mCompressedSize;
+    delete info;
+    int fd = device->readObject(object_id, object_size);
+    if (fd < 0)
+        return NULL;
+
+    jobject fileDescriptor = env->NewObject(gFileDescriptorOffsets.mClass,
+        gFileDescriptorOffsets.mConstructor);
+    if (fileDescriptor != NULL) {
+        env->SetIntField(fileDescriptor, gFileDescriptorOffsets.mDescriptor, fd);
+    } else {
+        return NULL;
+    }
+    return env->NewObject(gParcelFileDescriptorOffsets.mClass,
+        gParcelFileDescriptorOffsets.mConstructor, fileDescriptor);
+#endif
+    return NULL;
+}
+
 // ----------------------------------------------------------------------------
 
 static JNINativeMethod gMethods[] = {
@@ -197,6 +243,8 @@
     {"native_delete_object",   "(II)Z", (void *)android_media_MtpClient_delete_object},
     {"native_get_parent",      "(II)I", (void *)android_media_MtpClient_get_parent},
     {"native_get_storage_id",  "(II)I", (void *)android_media_MtpClient_get_storage_id},
+    {"native_open_file",       "(II)Landroid/os/ParcelFileDescriptor;",
+                                        (void *)android_media_MtpClient_open_file},
 };
 
 static const char* const kClassPathName = "android/media/MtpClient";
@@ -228,6 +276,21 @@
         return -1;
     }
 
+   clazz = env->FindClass("java/io/FileDescriptor");
+    LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor");
+    gFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
+    gFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "()V");
+    gFileDescriptorOffsets.mDescriptor = env->GetFieldID(clazz, "descriptor", "I");
+    LOG_FATAL_IF(gFileDescriptorOffsets.mDescriptor == NULL,
+                 "Unable to find descriptor field in java.io.FileDescriptor");
+
+   clazz = env->FindClass("android/os/ParcelFileDescriptor");
+    LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor");
+    gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
+    gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "(Ljava/io/FileDescriptor;)V");
+    LOG_FATAL_IF(gParcelFileDescriptorOffsets.mConstructor == NULL,
+                 "Unable to find constructor for android.os.ParcelFileDescriptor");
+
     return AndroidRuntime::registerNativeMethods(env,
                 "android/media/MtpClient", gMethods, NELEM(gMethods));
 }
diff --git a/media/jni/audioeffect/android_media_AudioEffect.cpp b/media/jni/audioeffect/android_media_AudioEffect.cpp
index beb3dfc..e3b9e36 100644
--- a/media/jni/audioeffect/android_media_AudioEffect.cpp
+++ b/media/jni/audioeffect/android_media_AudioEffect.cpp
@@ -549,7 +549,7 @@
     p = (effect_param_t *) malloc(sizeof(effect_param_t) + voffset + vsize);
     memcpy(p->data, lpParam, psize);
     p->psize = psize;
-    memcpy(p->data + voffset, lpValue, psize);
+    memcpy(p->data + voffset, lpValue, vsize);
     p->vsize = vsize;
 
     lStatus = lpAudioEffect->setParameter(p);
diff --git a/media/libeffects/lvm/lib/Bass/src/LVDBE_Control.c b/media/libeffects/lvm/lib/Bass/src/LVDBE_Control.c
index e66513f..8cf84b7 100755
--- a/media/libeffects/lvm/lib/Bass/src/LVDBE_Control.c
+++ b/media/libeffects/lvm/lib/Bass/src/LVDBE_Control.c
@@ -18,8 +18,8 @@
 /****************************************************************************************
 
      $Author: nxp007753 $
-     $Revision: 1223 $
-     $Date: 2010-07-15 14:27:01 +0200 (Thu, 15 Jul 2010) $
+     $Revision: 1315 $
+     $Date: 2010-07-23 11:52:08 +0200 (Fri, 23 Jul 2010) $
 
 *****************************************************************************************/
 
@@ -124,10 +124,11 @@
     /*
      * Setup the high pass filter
      */
-    LoadConst_16(0,                                                         /* Clear the history, value 0 */
-                 (LVM_INT16 *)&pInstance->pData->HPFTaps,                   /* Destination */
-                 sizeof(pInstance->pData->HPFTaps)/sizeof(LVM_INT16));      /* Number of words */
-    BQ_2I_D32F32Cll_TRC_WRA_01_Init(&pInstance->pCoef->HPFInstance,         /* Initialise the filter */
+    LoadConst_16(0,                                                 /* Clear the history, value 0 */
+                 (void *)&pInstance->pData->HPFTaps,                /* Destination Cast to void: \
+                                                                     no dereferencing in function*/
+                 sizeof(pInstance->pData->HPFTaps)/sizeof(LVM_INT16));   /* Number of words */
+    BQ_2I_D32F32Cll_TRC_WRA_01_Init(&pInstance->pCoef->HPFInstance,      /* Initialise the filter */
                                     &pInstance->pData->HPFTaps,
                                     (BQ_C32_Coefs_t *)&LVDBE_HPF_Table[Offset]);
 
@@ -135,10 +136,11 @@
     /*
      * Setup the band pass filter
      */
-    LoadConst_16(0,                                                         /* Clear the history, value 0 */
-                 (LVM_INT16 *)&pInstance->pData->BPFTaps,                   /* Destination */
-                 sizeof(pInstance->pData->BPFTaps)/sizeof(LVM_INT16));      /* Number of words */
-    BP_1I_D32F32Cll_TRC_WRA_02_Init(&pInstance->pCoef->BPFInstance,         /* Initialise the filter */
+    LoadConst_16(0,                                                 /* Clear the history, value 0 */
+                 (void *)&pInstance->pData->BPFTaps,                /* Destination Cast to void:\
+                                                                     no dereferencing in function*/
+                 sizeof(pInstance->pData->BPFTaps)/sizeof(LVM_INT16));   /* Number of words */
+    BP_1I_D32F32Cll_TRC_WRA_02_Init(&pInstance->pCoef->BPFInstance,      /* Initialise the filter */
                                     &pInstance->pData->BPFTaps,
                                     (BP_C32_Coefs_t *)&LVDBE_BPF_Table[Offset]);
 
diff --git a/media/libeffects/lvm/lib/Bundle/src/LVM_Buffers.c b/media/libeffects/lvm/lib/Bundle/src/LVM_Buffers.c
index 41785a3..7273400 100755
--- a/media/libeffects/lvm/lib/Bundle/src/LVM_Buffers.c
+++ b/media/libeffects/lvm/lib/Bundle/src/LVM_Buffers.c
@@ -19,8 +19,8 @@
 /****************************************************************************************
 
      $Author: nxp007753 $
-     $Revision: 1082 $
-     $Date: 2010-07-05 12:44:39 +0200 (Mon, 05 Jul 2010) $
+     $Revision: 1316 $
+     $Date: 2010-07-23 11:53:24 +0200 (Fri, 23 Jul 2010) $
 
 *****************************************************************************************/
 
diff --git a/media/libeffects/lvm/lib/Bundle/src/LVM_Control.c b/media/libeffects/lvm/lib/Bundle/src/LVM_Control.c
index 4667feb..922f77d 100755
--- a/media/libeffects/lvm/lib/Bundle/src/LVM_Control.c
+++ b/media/libeffects/lvm/lib/Bundle/src/LVM_Control.c
@@ -18,8 +18,8 @@
 /****************************************************************************************
 
      $Author: nxp007753 $
-     $Revision: 1255 $
-     $Date: 2010-07-16 17:07:29 +0200 (Fri, 16 Jul 2010) $
+     $Revision: 1316 $
+     $Date: 2010-07-23 11:53:24 +0200 (Fri, 23 Jul 2010) $
 
 *****************************************************************************************/
 
@@ -314,8 +314,9 @@
             /*
              * Clear the taps
              */
-            LoadConst_16((LVM_INT16)0,                                                                   /* Value */
-                         (LVM_INT16 *)&pInstance->pTE_Taps->TrebleBoost_Taps,                            /* Destination */
+            LoadConst_16((LVM_INT16)0,                                     /* Value */
+                         (void *)&pInstance->pTE_Taps->TrebleBoost_Taps,  /* Destination.\
+                                                     Cast to void: no dereferencing in function */
                          (LVM_UINT16)(sizeof(pInstance->pTE_Taps->TrebleBoost_Taps)/sizeof(LVM_INT16))); /* Number of words */
         }
     }
diff --git a/media/libeffects/lvm/lib/Bundle/src/LVM_Init.c b/media/libeffects/lvm/lib/Bundle/src/LVM_Init.c
index 7ac5685..323a2a3 100755
--- a/media/libeffects/lvm/lib/Bundle/src/LVM_Init.c
+++ b/media/libeffects/lvm/lib/Bundle/src/LVM_Init.c
@@ -18,8 +18,8 @@
 /************************************************************************************
 
      $Author: nxp007753 $
-     $Revision: 1255 $
-     $Date: 2010-07-16 17:07:29 +0200 (Fri, 16 Jul 2010) $
+     $Revision: 1316 $
+     $Date: 2010-07-23 11:53:24 +0200 (Fri, 23 Jul 2010) $
 
 *************************************************************************************/
 
diff --git a/media/libeffects/lvm/lib/Common/src/MixSoft_2St_D32C31_SAT.c b/media/libeffects/lvm/lib/Common/src/MixSoft_2St_D32C31_SAT.c
index b03f0ba..83f95ac 100755
--- a/media/libeffects/lvm/lib/Common/src/MixSoft_2St_D32C31_SAT.c
+++ b/media/libeffects/lvm/lib/Common/src/MixSoft_2St_D32C31_SAT.c
@@ -18,9 +18,9 @@
 /************************************************************************/
 /*                                                                      */
 /*     Project::                                                        */
-/*     $Author: beq07716 $*/
-/*     $Revision: 1000 $*/
-/*     $Date: 2010-06-28 13:08:20 +0200 (Mon, 28 Jun 2010) $*/
+/*     $Author: nxp007753 $*/
+/*     $Revision: 1316 $*/
+/*     $Date: 2010-07-23 11:53:24 +0200 (Fri, 23 Jul 2010) $*/
 /*                                                                      */
 /************************************************************************/
 
@@ -51,7 +51,8 @@
     if ((pInstance->Current1 != pInstance->Target1) || (pInstance->Current2 != pInstance->Target2))
     {
         MixSoft_1St_D32C31_WRA( (Mix_1St_Cll_t*) pInstance, src1, dst, n);
-        MixInSoft_D32C31_SAT( (Mix_1St_Cll_t*) &pInstance->Alpha2, src2, dst, n);
+        MixInSoft_D32C31_SAT( (void *) &pInstance->Alpha2,     /* Cast to void: no dereferencing in function*/
+            src2, dst, n);
     }
 
     /******************************************************************************
@@ -61,7 +62,8 @@
     else
     {
         if (pInstance->Current1 == 0)
-            MixSoft_1St_D32C31_WRA( (Mix_1St_Cll_t*) &pInstance->Alpha2, src2, dst, n);
+            MixSoft_1St_D32C31_WRA( (void *) &pInstance->Alpha2, /* Cast to void: no dereferencing in function*/
+            src2, dst, n);
         else if (pInstance->Current2 == 0)
             MixSoft_1St_D32C31_WRA( (Mix_1St_Cll_t*) pInstance, src1, dst, n);
         else
diff --git a/media/libeffects/lvm/lib/Eq/src/LVEQNB_Control.c b/media/libeffects/lvm/lib/Eq/src/LVEQNB_Control.c
index 88f6fb0..dac2449 100755
--- a/media/libeffects/lvm/lib/Eq/src/LVEQNB_Control.c
+++ b/media/libeffects/lvm/lib/Eq/src/LVEQNB_Control.c
@@ -18,8 +18,8 @@
 /**********************************************************************************
 
      $Author: nxp007753 $
-     $Revision: 1223 $
-     $Date: 2010-07-15 14:27:01 +0200 (Thu, 15 Jul 2010) $
+     $Revision: 1316 $
+     $Date: 2010-07-23 11:53:24 +0200 (Fri, 23 Jul 2010) $
 
 ***********************************************************************************/
 
diff --git a/media/libeffects/lvm/lib/StereoWidening/src/LVCS_BypassMix.c b/media/libeffects/lvm/lib/StereoWidening/src/LVCS_BypassMix.c
index 2a83e89..b1d9408 100755
--- a/media/libeffects/lvm/lib/StereoWidening/src/LVCS_BypassMix.c
+++ b/media/libeffects/lvm/lib/StereoWidening/src/LVCS_BypassMix.c
@@ -17,9 +17,9 @@
 
 /************************************************************************************
 
-     $Author: nxp007753 $
-     $Revision: 1246 $
-     $Date: 2010-07-16 11:07:10 +0200 (Fri, 16 Jul 2010) $
+     $Author: beq06068 $
+     $Revision: 1307 $
+     $Date: 2010-07-22 17:41:25 +0200 (Thu, 22 Jul 2010) $
 
 *************************************************************************************/
 
@@ -90,7 +90,7 @@
      */
     if ((pParams->OperatingMode == LVCS_ON) &&
         (pInstance->bTimerDone == LVM_TRUE)
-        && (LVC_Mixer_GetTarget(&pInstance->MSBypassMixer.MixerStream[1]) != 0x7FFF) /* this indicates an off->on transtion */
+        && (pInstance->MSTarget1 != 0x7FFF) /* this indicates an off->on transtion */
         )
     {
         pInstance->TransitionGain = pParams->EffectLevel;
@@ -260,17 +260,15 @@
                             LVM_INT16           CallbackParam)
 {
     LVCS_Instance_t     *pInstance = (LVCS_Instance_t  *)hInstance;
-    LVM_INT32           Target1;
 
-    Target1 = LVC_Mixer_GetTarget(&pInstance->MSBypassMixer.MixerStream[0]);
-    (void)pGeneralPurpose;
+   (void)pGeneralPurpose;
 
     /*
      * Off transition has completed in Headphone mode
      */
     if ((pInstance->OutputDevice == LVCS_HEADPHONE) &&
         (pInstance->bInOperatingModeTransition)     &&
-        (Target1 == 0x0000)&&  /* this indicates an on->off transition */
+        (pInstance->MSTarget0 == 0x0000)&&  /* this indicates an on->off transition */
         (CallbackParam == 0))
     {
         /* Set operating mode to OFF */
@@ -289,7 +287,7 @@
 
 
     if ((pInstance->OutputDevice == LVCS_HEADPHONE)  &&
-        (Target1 == 1) &&
+        (pInstance->MSTarget0 == 1) &&
         (pInstance->bTimerDone == LVM_TRUE)){
 
         /* Exit transition state */
diff --git a/media/libeffects/lvm/lib/StereoWidening/src/LVCS_Control.c b/media/libeffects/lvm/lib/StereoWidening/src/LVCS_Control.c
index 5dfca256..fea44bf 100755
--- a/media/libeffects/lvm/lib/StereoWidening/src/LVCS_Control.c
+++ b/media/libeffects/lvm/lib/StereoWidening/src/LVCS_Control.c
@@ -17,9 +17,9 @@
 
 /************************************************************************************
 
-     $Author: nxp007753 $
-     $Revision: 1246 $
-     $Date: 2010-07-16 11:07:10 +0200 (Fri, 16 Jul 2010) $
+     $Author: beq06068 $
+     $Revision: 1307 $
+     $Date: 2010-07-22 17:41:25 +0200 (Thu, 22 Jul 2010) $
 
 *************************************************************************************/
 
@@ -94,9 +94,6 @@
 
     if (pParams->SampleRate != pInstance->Params.SampleRate)
     {
-        LVC_Mixer_VarSlope_SetTimeConstant(&pInstance->MSBypassMixer.MixerStream[0],LVCS_BYPASS_MIXER_TC,pParams->SampleRate,2);
-
-        LVC_Mixer_VarSlope_SetTimeConstant(&pInstance->MSBypassMixer.MixerStream[1],LVCS_BYPASS_MIXER_TC,pParams->SampleRate,2);
         pInstance->TimerParams.SamplingRate = LVCS_SampleRateTable[pParams->SampleRate];
     }
 
@@ -130,6 +127,29 @@
 
         pInstance->VolCorrect = pLVCS_VolCorrectTable[Offset];
 
+        LVC_Mixer_Init(&pInstance->BypassMix.Mixer_Instance.MixerStream[0],0,0);
+
+
+        {
+            LVM_UINT32          Gain;
+            const Gain_t        *pOutputGainTable = (Gain_t*)&LVCS_OutputGainTable[0];
+            Gain = (LVM_UINT32)(pOutputGainTable[Offset].Loss * LVM_MAXINT_16);
+            Gain = (LVM_UINT32)pOutputGainTable[Offset].UnprocLoss * (Gain >> 15);
+            Gain=Gain>>15;
+            /*
+             * Apply the gain correction and shift, note the result is in Q3.13 format
+             */
+            Gain = (Gain * pInstance->VolCorrect.GainMin) >>12;
+
+            LVC_Mixer_Init(&pInstance->BypassMix.Mixer_Instance.MixerStream[1],0,Gain);
+            LVC_Mixer_VarSlope_SetTimeConstant(&pInstance->BypassMix.Mixer_Instance.MixerStream[0],
+                    LVCS_BYPASS_MIXER_TC,pParams->SampleRate,2);
+            LVC_Mixer_VarSlope_SetTimeConstant(&pInstance->BypassMix.Mixer_Instance.MixerStream[1],
+                    LVCS_BYPASS_MIXER_TC,pParams->SampleRate,2);
+
+        }
+
+
         err=LVCS_SEnhancerInit(hInstance,
                            pParams);
 
@@ -199,44 +219,15 @@
 
         /* Change transition bypass mixer settings if needed depending on transition type */
         if(pParams->OperatingMode != LVCS_OFF){
-            LVM_INT32 Current1;
-            LVM_INT32 Current2;
-
-            Current1 = LVC_Mixer_GetCurrent(&pInstance->MSBypassMixer.MixerStream[0]);
-            Current2 = LVC_Mixer_GetCurrent(&pInstance->MSBypassMixer.MixerStream[1]);
-
-            if(pInstance->bInOperatingModeTransition != LVM_TRUE)
-            {
-                Current1 = 0x00000000;
-                Current2 = LVM_MAXINT_16;
-            }
-            pInstance->MSBypassMixer.MixerStream[0].CallbackSet = 1;
-            pInstance->MSBypassMixer.MixerStream[1].CallbackSet = 1;
-
-            LVC_Mixer_Init(&pInstance->MSBypassMixer.MixerStream[0],LVM_MAXINT_16,Current1);
-            LVC_Mixer_Init(&pInstance->MSBypassMixer.MixerStream[1],0,Current2);
+            pInstance->MSTarget0=LVM_MAXINT_16;
+            pInstance->MSTarget1=0;
         }
         else
         {
-            LVM_INT32 Current1;
-            LVM_INT32 Current2;
-
-            Current1 = LVC_Mixer_GetCurrent(&pInstance->MSBypassMixer.MixerStream[0]);
-            Current2 = LVC_Mixer_GetCurrent(&pInstance->MSBypassMixer.MixerStream[1]);
-
-            if(pInstance->bInOperatingModeTransition != LVM_TRUE)
-            {
-                Current1 = LVM_MAXINT_16;
-                Current2 = 0x00000000;
-            }
-            pInstance->MSBypassMixer.MixerStream[0].CallbackSet = 1;
-            pInstance->MSBypassMixer.MixerStream[1].CallbackSet = 1;
             pInstance->Params.OperatingMode = OperatingModeSave;
-            LVC_Mixer_Init(&pInstance->MSBypassMixer.MixerStream[0],0x00000000,Current1);
-            LVC_Mixer_Init(&pInstance->MSBypassMixer.MixerStream[1],LVM_MAXINT_16,Current2);
+            pInstance->MSTarget1=LVM_MAXINT_16;
+            pInstance->MSTarget0=0;
         }
-        LVC_Mixer_SetTimeConstant(&pInstance->MSBypassMixer.MixerStream[0],LVCS_BYPASS_MIXER_TC,pParams->SampleRate,2);
-        LVC_Mixer_SetTimeConstant(&pInstance->MSBypassMixer.MixerStream[1],LVCS_BYPASS_MIXER_TC,pParams->SampleRate,2);
 
 
         /* Set transition flag */
diff --git a/media/libeffects/lvm/lib/StereoWidening/src/LVCS_Equaliser.c b/media/libeffects/lvm/lib/StereoWidening/src/LVCS_Equaliser.c
index ca615b0..7ab6571 100755
--- a/media/libeffects/lvm/lib/StereoWidening/src/LVCS_Equaliser.c
+++ b/media/libeffects/lvm/lib/StereoWidening/src/LVCS_Equaliser.c
@@ -17,9 +17,9 @@
 
 /************************************************************************************
 
-     $Author: beq07716 $
-     $Revision: 1001 $
-     $Date: 2010-06-28 13:23:02 +0200 (Mon, 28 Jun 2010) $
+     $Author: nxp007753 $
+     $Revision: 1315 $
+     $Date: 2010-07-23 11:52:08 +0200 (Fri, 23 Jul 2010) $
 
 *************************************************************************************/
 
@@ -94,8 +94,9 @@
         Coeffs.B1 = (LVM_INT16)-pEqualiserCoefTable[Offset].B1;
         Coeffs.B2 = (LVM_INT16)-pEqualiserCoefTable[Offset].B2;
 
-        LoadConst_16((LVM_INT16)0,                                                           /* Value */
-                     (LVM_INT16 *)&pData->EqualiserBiquadTaps,                               /* Destination */
+        LoadConst_16((LVM_INT16)0,                                                       /* Value */
+                     (void *)&pData->EqualiserBiquadTaps,   /* Destination Cast to void:\
+                                                               no dereferencing in function*/
                      (LVM_UINT16)(sizeof(pData->EqualiserBiquadTaps)/sizeof(LVM_INT16)));    /* Number of words */
 
         BQ_2I_D16F32Css_TRC_WRA_01_Init(&pCoefficients->EqualiserBiquadInstance,
diff --git a/media/libeffects/lvm/lib/StereoWidening/src/LVCS_Init.c b/media/libeffects/lvm/lib/StereoWidening/src/LVCS_Init.c
index 4aa95d3..f5f7cd0 100755
--- a/media/libeffects/lvm/lib/StereoWidening/src/LVCS_Init.c
+++ b/media/libeffects/lvm/lib/StereoWidening/src/LVCS_Init.c
@@ -17,9 +17,9 @@
 
 /************************************************************************************
 
-     $Author: beq07716 $
-     $Revision: 1001 $
-     $Date: 2010-06-28 13:23:02 +0200 (Mon, 28 Jun 2010) $
+     $Author: beq06068 $
+     $Revision: 1307 $
+     $Date: 2010-07-22 17:41:25 +0200 (Thu, 22 Jul 2010) $
 
 *************************************************************************************/
 
@@ -159,7 +159,6 @@
                                LVCS_Capabilities_t   *pCapabilities)
 {
 
-    LVM_INT16                       Offset;
     LVCS_Instance_t                 *pInstance;
     LVCS_VolCorrect_t               *pLVCS_VolCorrectTable;
 
@@ -197,30 +196,18 @@
     pInstance->Params.EffectLevel    = 0;
     pInstance->Params.ReverbLevel    = (LVM_UINT16)0x8000;
     pLVCS_VolCorrectTable            = (LVCS_VolCorrect_t*)&LVCS_VolCorrectTable[0];
-    Offset                           = (LVM_INT16)(pInstance->Params.SpeakerType + (pInstance->Params.SourceFormat*(1+LVCS_EX_HEADPHONES)));
-    pInstance->VolCorrect            = pLVCS_VolCorrectTable[Offset];
+    pInstance->VolCorrect            = pLVCS_VolCorrectTable[0];
     pInstance->TransitionGain        = 0;
+    /* These current and target values are intialized again in LVCS_Control.c */
     LVC_Mixer_Init(&pInstance->BypassMix.Mixer_Instance.MixerStream[0],0,0);
+    /* These current and target values are intialized again in LVCS_Control.c */
     LVC_Mixer_Init(&pInstance->BypassMix.Mixer_Instance.MixerStream[1],0,0);
 
     /*
      * Initialise the bypass variables
      */
-    pInstance->MSBypassMixer.MixerStream[0].CallbackParam      = 0;
-    pInstance->MSBypassMixer.MixerStream[0].pCallbackHandle    = LVM_NULL;
-    pInstance->MSBypassMixer.MixerStream[0].pCallBack          = LVM_NULL;
-    pInstance->MSBypassMixer.MixerStream[0].CallbackSet        = 0;
-    LVC_Mixer_Init(&pInstance->MSBypassMixer.MixerStream[0],0,0);
-    LVC_Mixer_SetTimeConstant(&pInstance->MSBypassMixer.MixerStream[0],0,LVM_FS_44100,2);
-
-
-    pInstance->MSBypassMixer.MixerStream[1].CallbackParam      = 0;
-    pInstance->MSBypassMixer.MixerStream[1].pCallbackHandle    = LVM_NULL;
-    pInstance->MSBypassMixer.MixerStream[1].pCallBack          = LVM_NULL;
-    pInstance->MSBypassMixer.MixerStream[1].CallbackSet        = 0;
-    LVC_Mixer_Init(&pInstance->MSBypassMixer.MixerStream[1],0,0);
-    LVC_Mixer_SetTimeConstant(&pInstance->MSBypassMixer.MixerStream[1],0,LVM_FS_44100,2);
-
+    pInstance->MSTarget0=0;
+    pInstance->MSTarget1=0;
     pInstance->bInOperatingModeTransition          = LVM_FALSE;
     pInstance->bTimerDone                        = LVM_FALSE;
     pInstance->TimerParams.CallBackParam         = 0;
diff --git a/media/libeffects/lvm/lib/StereoWidening/src/LVCS_Private.h b/media/libeffects/lvm/lib/StereoWidening/src/LVCS_Private.h
index be30829..a977690 100755
--- a/media/libeffects/lvm/lib/StereoWidening/src/LVCS_Private.h
+++ b/media/libeffects/lvm/lib/StereoWidening/src/LVCS_Private.h
@@ -17,9 +17,9 @@
 
 /************************************************************************************
 
-     $Author: beq07716 $
-     $Revision: 1001 $
-     $Date: 2010-06-28 13:23:02 +0200 (Mon, 28 Jun 2010) $
+     $Author: beq06068 $
+     $Revision: 1307 $
+     $Date: 2010-07-22 17:41:25 +0200 (Thu, 22 Jul 2010) $
 
 *************************************************************************************/
 
@@ -128,7 +128,8 @@
     LVCS_BypassMix_t        BypassMix;          /* Bypass mixer configuration */
 
     /* Bypass variable */
-    LVMixer3_2St_st         MSBypassMixer;                      /* Bypass mixer used in transitions in MS mode */
+    LVM_INT16               MSTarget0;                          /* Mixer state control variable for smooth transtion */
+    LVM_INT16               MSTarget1;                          /* Mixer state control variable for smooth transtion */
     LVM_INT16               bInOperatingModeTransition;         /* Operating mode transition flag */
     LVM_INT16               bTimerDone;                         /* Timer completion flag */
     LVM_Timer_Params_t      TimerParams;                        /* Timer parameters */
diff --git a/media/libeffects/lvm/lib/StereoWidening/src/LVCS_ReverbGenerator.c b/media/libeffects/lvm/lib/StereoWidening/src/LVCS_ReverbGenerator.c
index 2efef84..861bde6 100755
--- a/media/libeffects/lvm/lib/StereoWidening/src/LVCS_ReverbGenerator.c
+++ b/media/libeffects/lvm/lib/StereoWidening/src/LVCS_ReverbGenerator.c
@@ -17,9 +17,9 @@
 
 /************************************************************************************
 
-     $Author: beq07716 $
-     $Revision: 1001 $
-     $Date: 2010-06-28 13:23:02 +0200 (Mon, 28 Jun 2010) $
+     $Author: nxp007753 $
+     $Revision: 1315 $
+     $Date: 2010-07-23 11:52:08 +0200 (Fri, 23 Jul 2010) $
 
 *************************************************************************************/
 
@@ -113,7 +113,7 @@
         Coeffs.B2 = (LVM_INT16)-pReverbCoefTable[Offset].B2;
 
         LoadConst_16(0,                                                                 /* Value */
-                     (LVM_INT16 *)&pData->ReverbBiquadTaps,                             /* Destination */
+                     (void *)&pData->ReverbBiquadTaps,                             /* Destination Cast to void: no dereferencing in function*/
                      (LVM_UINT16)(sizeof(pData->ReverbBiquadTaps)/sizeof(LVM_INT16)));  /* Number of words */
 
         BQ_2I_D16F16Css_TRC_WRA_01_Init(&pCoefficients->ReverbBiquadInstance,
diff --git a/media/libeffects/lvm/lib/StereoWidening/src/LVCS_StereoEnhancer.c b/media/libeffects/lvm/lib/StereoWidening/src/LVCS_StereoEnhancer.c
index 4ff4716..b67d824 100755
--- a/media/libeffects/lvm/lib/StereoWidening/src/LVCS_StereoEnhancer.c
+++ b/media/libeffects/lvm/lib/StereoWidening/src/LVCS_StereoEnhancer.c
@@ -17,9 +17,9 @@
 
 /************************************************************************************
 
-     $Author: beq07716 $
-     $Revision: 1001 $
-     $Date: 2010-06-28 13:23:02 +0200 (Mon, 28 Jun 2010) $
+     $Author: nxp007753 $
+     $Revision: 1315 $
+     $Date: 2010-07-23 11:52:08 +0200 (Fri, 23 Jul 2010) $
 
 *************************************************************************************/
 
@@ -90,7 +90,8 @@
 
         /* Clear the taps */
         LoadConst_16(0,                                                                 /* Value */
-                     (LVM_INT16 *)&pData->SEBiquadTapsMid,                              /* Destination */
+                     (void *)&pData->SEBiquadTapsMid,              /* Destination Cast to void:\
+                                                                      no dereferencing in function*/
                      (LVM_UINT16)(sizeof(pData->SEBiquadTapsMid)/sizeof(LVM_UINT16)));  /* Number of words */
 
         FO_1I_D16F16Css_TRC_WRA_01_Init(&pCoefficient->SEBiquadInstanceMid,
@@ -116,7 +117,8 @@
 
         /* Clear the taps */
         LoadConst_16(0,                                                                 /* Value */
-                     (LVM_INT16 *)&pData->SEBiquadTapsSide,                             /* Destination */
+                     (void *)&pData->SEBiquadTapsSide,             /* Destination Cast to void:\
+                                                                      no dereferencing in function*/
                      (LVM_UINT16)(sizeof(pData->SEBiquadTapsSide)/sizeof(LVM_UINT16))); /* Number of words */
 
 
diff --git a/media/libeffects/lvm/wrapper/Android.mk b/media/libeffects/lvm/wrapper/Android.mk
index aed594d..7855dcd 100644
--- a/media/libeffects/lvm/wrapper/Android.mk
+++ b/media/libeffects/lvm/wrapper/Android.mk
@@ -1,35 +1,36 @@
-LOCAL_PATH:= $(call my-dir)

-

-# music bundle wrapper

-LOCAL_PATH:= $(call my-dir)

-include $(CLEAR_VARS)

-

-LOCAL_ARM_MODE := arm

-

-LOCAL_SRC_FILES:= \

-	Bundle/EffectBundle.cpp

-

-LOCAL_MODULE:= libbundlewrapper

-

-LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/soundfx

-

-LOCAL_PRELINK_MODULE := false

-

-LOCAL_STATIC_LIBRARIES += libmusicbundle

-

-LOCAL_SHARED_LIBRARIES := \

-     libcutils

-

-ifeq ($(TARGET_SIMULATOR),true)

-LOCAL_LDLIBS += -ldl

-else

-LOCAL_SHARED_LIBRARIES += libdl

-endif

-

-LOCAL_C_INCLUDES += \

-	$(LOCAL_PATH)/Bundle \

-	$(LOCAL_PATH)/../lib/Common/lib/ \

-	$(LOCAL_PATH)/../lib/Bundle/lib/

-

-

-include $(BUILD_SHARED_LIBRARY)

+LOCAL_PATH:= $(call my-dir)
+
+# music bundle wrapper
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_ARM_MODE := arm
+
+LOCAL_SRC_FILES:= \
+	Bundle/EffectBundle.cpp
+
+LOCAL_MODULE:= libbundlewrapper
+
+LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/soundfx
+
+LOCAL_PRELINK_MODULE := false
+
+LOCAL_STATIC_LIBRARIES += libmusicbundle
+
+LOCAL_SHARED_LIBRARIES := \
+     libcutils \
+
+ifeq ($(TARGET_SIMULATOR),true)
+LOCAL_LDLIBS += -ldl
+else
+LOCAL_SHARED_LIBRARIES += libdl
+endif
+
+
+LOCAL_C_INCLUDES += \
+	$(LOCAL_PATH)/Bundle \
+	$(LOCAL_PATH)/../lib/Common/lib/ \
+	$(LOCAL_PATH)/../lib/Bundle/lib/
+
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/media/libeffects/lvm/wrapper/Bundle/EffectBundle.cpp b/media/libeffects/lvm/wrapper/Bundle/EffectBundle.cpp
index 6043dd5..4440447 100644
--- a/media/libeffects/lvm/wrapper/Bundle/EffectBundle.cpp
+++ b/media/libeffects/lvm/wrapper/Bundle/EffectBundle.cpp
@@ -1625,7 +1625,7 @@
     //LOGV("\tBassBoost_getParameter start");
 
     switch (param){
-        case BASSBOOST_PARAM_STRENGTH_SUP:
+        case BASSBOOST_PARAM_STRENGTH_SUPPORTED:
         case BASSBOOST_PARAM_STRENGTH:
             if (*pValueSize != sizeof(int16_t)){
                 LOGV("\tLVM_ERROR : BassBoost_getParameter() invalid pValueSize2 %d", *pValueSize);
@@ -1640,10 +1640,10 @@
     }
 
     switch (param){
-        case BASSBOOST_PARAM_STRENGTH_SUP:
+        case BASSBOOST_PARAM_STRENGTH_SUPPORTED:
             *(uint32_t *)pValue = 1;
 
-            //LOGV("\tBassBoost_getParameter() BASSBOOST_PARAM_STRENGTH_SUP Value is %d",
+            //LOGV("\tBassBoost_getParameter() BASSBOOST_PARAM_STRENGTH_SUPPORTED Value is %d",
             //        *(uint32_t *)pValue);
             break;
 
@@ -1735,7 +1735,7 @@
     //LOGV("\tVirtualizer_getParameter start");
 
     switch (param){
-        case VIRTUALIZER_PARAM_STRENGTH_SUP:
+        case VIRTUALIZER_PARAM_STRENGTH_SUPPORTED:
         case VIRTUALIZER_PARAM_STRENGTH:
             if (*pValueSize != sizeof(int16_t)){
                 LOGV("\tLVM_ERROR : Virtualizer_getParameter() invalid pValueSize2 %d",*pValueSize);
@@ -1750,10 +1750,10 @@
     }
 
     switch (param){
-        case VIRTUALIZER_PARAM_STRENGTH_SUP:
+        case VIRTUALIZER_PARAM_STRENGTH_SUPPORTED:
             *(uint32_t *)pValue = 1;
 
-            //LOGV("\tVirtualizer_getParameter() VIRTUALIZER_PARAM_STRENGTH_SUP Value is %d",
+            //LOGV("\tVirtualizer_getParameter() VIRTUALIZER_PARAM_STRENGTH_SUPPORTED Value is %d",
             //        *(uint32_t *)pValue);
             break;
 
@@ -1876,6 +1876,14 @@
     case EQ_PARAM_GET_PRESET_NAME:
         break;
 
+    case EQ_PARAM_PROPERTIES:
+        if (*pValueSize < (2 + FIVEBAND_NUMBANDS) * sizeof(uint16_t)) {
+            LOGV("\tLVM_ERROR : Equalizer_getParameter() invalid pValueSize 1  %d", *pValueSize);
+            return -EINVAL;
+        }
+        *pValueSize = (2 + FIVEBAND_NUMBANDS) * sizeof(uint16_t);
+        break;
+
     default:
         LOGV("\tLVM_ERROR : Equalizer_getParameter unknown param %d", param);
         return -EINVAL;
@@ -1959,6 +1967,16 @@
         //      param2, gEqualizerPresets[param2].name, *pValueSize);
         break;
 
+    case EQ_PARAM_PROPERTIES: {
+        uint16_t *p = (uint16_t *)pValue;
+        LOGV("\tEqualizer_getParameter() EQ_PARAM_PROPERTIES");
+        p[0] = EqualizerGetPreset(pContext);
+        p[1] = FIVEBAND_NUMBANDS;
+        for (int i = 0; i < FIVEBAND_NUMBANDS; i++) {
+            p[2 + i] = EqualizerGetBandLevel(pContext, i);
+        }
+    } break;
+
     default:
         LOGV("\tLVM_ERROR : Equalizer_getParameter() invalid param %d", param);
         status = -EINVAL;
@@ -2337,7 +2355,6 @@
                               void                *pReplyData){
     EffectContext * pContext = (EffectContext *) self;
     int retsize;
-    int status = 0;
 
     //LOGV("\t\nEffect_command start");
 
@@ -2371,54 +2388,29 @@
 
     switch (cmdCode){
         case EFFECT_CMD_INIT:
+            if (pReplyData == NULL || *replySize != sizeof(int)){
+                LOGV("\tLVM_ERROR, EFFECT_CMD_INIT: ERROR for effect type %d",
+                        pContext->EffectType);
+                return -EINVAL;
+            }
+            *(int *) pReplyData = 0;
             //LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_INIT start");
             if(pContext->EffectType == LVM_BASS_BOOST){
                 //LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_INIT for LVM_BASS_BOOST");
-
-                if (pReplyData == NULL || *replySize != sizeof(int)){
-                    LOGV("\tLVM_ERROR : BassBoost_command cmdCode Case: "
-                            "EFFECT_CMD_INIT: ERROR");
-                    return -EINVAL;
-                }
-
                 android::BassSetStrength(pContext, 0);
             }
             if(pContext->EffectType == LVM_VIRTUALIZER){
                 //LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_INIT for LVM_VIRTUALIZER");
-
-                if (pReplyData == NULL || *replySize != sizeof(int)){
-                    LOGV("\tLVM_ERROR : Virtualizer_command cmdCode Case: "
-                            "EFFECT_CMD_INIT: ERROR");
-                    return -EINVAL;
-                }
-
                 android::VirtualizerSetStrength(pContext, 0);
             }
             if(pContext->EffectType == LVM_EQUALIZER){
                 //LOGV("\tEffect_command cmdCode Case: EFFECT_CMD_INIT for LVM_EQUALIZER");
-
-                if (pReplyData == NULL || *replySize != sizeof(int)){
-                    LOGV("\tLVM_ERROR : Equalizer_command cmdCode Case: "
-                            "EFFECT_CMD_INIT: ERROR");
-                    return -EINVAL;
-                }
-
                 android::EqualizerSetPreset(pContext, 0);
             }
             if(pContext->EffectType == LVM_VOLUME){
                 //LOGV("\tEffect_command cmdCode Case: "
                 //        "EFFECT_CMD_INIT start");
-
-                if (pReplyData == NULL || *replySize != sizeof(int)){
-                    LOGV("\tLVM_ERROR : Volume_command cmdCode Case: "
-                            "EFFECT_CMD_INIT: ERROR");
-                    return -EINVAL;
-                }
-
-                status = android::VolumeSetVolumeLevel(pContext, 0);
-                if(status == -EINVAL){
-                    return -EINVAL;
-                }
+                *(int *) pReplyData = android::VolumeSetVolumeLevel(pContext, 0);
             }
             break;
 
diff --git a/media/libeffects/lvm/wrapper/Bundle/EffectBundle.h b/media/libeffects/lvm/wrapper/Bundle/EffectBundle.h
index 029f843..d009bf9 100644
--- a/media/libeffects/lvm/wrapper/Bundle/EffectBundle.h
+++ b/media/libeffects/lvm/wrapper/Bundle/EffectBundle.h
@@ -17,7 +17,9 @@
 #ifndef ANDROID_EFFECTBUNDLE_H_
 #define ANDROID_EFFECTBUNDLE_H_
 
-#include <media/EffectApi.h>
+#include <media/EffectEqualizerApi.h>
+#include <media/EffectBassBoostApi.h>
+#include <media/EffectVirtualizerApi.h>
 #include <LVM.h>
 
 #if __cplusplus
@@ -29,22 +31,11 @@
 #define MAX_CALL_SIZE           256
 //#define LVM_PCM
 
-//TODO: this should be included from each effect API include
-static const effect_uuid_t SL_IID_BASSBOOST_ = { 0x0634f220, 0xddd4, 0x11db, 0xa0fc,
-                                               { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
-const effect_uuid_t * const SL_IID_BASSBOOST = &SL_IID_BASSBOOST_;
-
-static const effect_uuid_t SL_IID_EQUALIZER_ = { 0x0bed4300, 0xddd6, 0x11db, 0x8f34,
-                                               { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
-const effect_uuid_t * const SL_IID_EQUALIZER = &SL_IID_EQUALIZER_;
-
-static const effect_uuid_t SL_IID_VIRTUALIZER_ = { 0x37cc2c00, 0xdddd, 0x11db, 0x8577,
-                                                 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
-const effect_uuid_t * const SL_IID_VIRTUALIZER = &SL_IID_VIRTUALIZER_;
-
+#ifndef OPENSL_ES_H_
 static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
                                             { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
 const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
+#endif //OPENSL_ES_H_
 
 typedef enum
 {
@@ -112,34 +103,6 @@
     BundledEffectContext            *pBundledContext;
 };
 
-//TODO: this should be included from each effect API include
-/* enumerated parameter settings for BassBoost effect */
-typedef enum
-{
-    BASSBOOST_PARAM_STRENGTH_SUP,        // type SLboolean  = typedef SLuint32
-    BASSBOOST_PARAM_STRENGTH             // type SLpermille = typedef SLuint16
-} t_bassboost_params;
-
-/* enumerated parameter settings for Virtualizer effect */
-typedef enum
-{
-    VIRTUALIZER_PARAM_STRENGTH_SUP,        // type SLboolean  = typedef SLuint32
-    VIRTUALIZER_PARAM_STRENGTH             // type SLpermille = typedef SLuint16
-} t_virtualizer_params;
-
-/* enumerated parameter settings for Equalizer effect */
-typedef enum
-{
-    EQ_PARAM_NUM_BANDS,           // Gets the number of frequency bands that the equalizer supports.
-    EQ_PARAM_LEVEL_RANGE,         // Returns the minimum and maximum band levels supported.
-    EQ_PARAM_BAND_LEVEL,          // Gets/Sets the gain set for the given equalizer band.
-    EQ_PARAM_CENTER_FREQ,         // Gets the center frequency of the given band.
-    EQ_PARAM_BAND_FREQ_RANGE,     // Gets the frequency range of the given frequency band.
-    EQ_PARAM_GET_BAND,            // Gets the band that has the most effect on the given frequency.
-    EQ_PARAM_CUR_PRESET,          // Gets/Sets the current preset.
-    EQ_PARAM_GET_NUM_OF_PRESETS,  // Gets the total number of presets the equalizer supports.
-    EQ_PARAM_GET_PRESET_NAME      // Gets the preset name based on the index.
-} t_equalizer_params;
 
 /* enumerated parameter settings for Volume effect */
 typedef enum
diff --git a/media/libeffects/testlibs/EffectReverb.c b/media/libeffects/testlibs/EffectReverb.c
index 2ce7558..3f9069f 100644
--- a/media/libeffects/testlibs/EffectReverb.c
+++ b/media/libeffects/testlibs/EffectReverb.c
@@ -688,7 +688,7 @@
         void *pValue) {
     int32_t *pValue32;
     int16_t *pValue16;
-    t_reverb_properties *pProperties;
+    t_reverb_settings *pProperties;
     int32_t i;
     int32_t temp;
     int32_t temp2;
@@ -727,7 +727,7 @@
             break;
 
         case REVERB_PARAM_PROPERTIES:
-            size = sizeof(t_reverb_properties);
+            size = sizeof(t_reverb_settings);
             break;
 
         default:
@@ -740,7 +740,7 @@
 
         pValue32 = (int32_t *) pValue;
         pValue16 = (int16_t *) pValue;
-        pProperties = (t_reverb_properties *) pValue;
+        pProperties = (t_reverb_settings *) pValue;
 
         switch (param) {
         case REVERB_PARAM_BYPASS:
@@ -971,7 +971,7 @@
         void *pValue) {
     int32_t value32;
     int16_t value16;
-    t_reverb_properties *pProperties;
+    t_reverb_settings *pProperties;
     int32_t i;
     int32_t temp;
     int32_t temp2;
@@ -1019,7 +1019,7 @@
             break;
 
         case REVERB_PARAM_PROPERTIES:
-            paramSize = sizeof(t_reverb_properties);
+            paramSize = sizeof(t_reverb_settings);
             break;
 
         default:
@@ -1035,7 +1035,7 @@
         } else if (paramSize == sizeof(int32_t)) {
             value32 = *(int32_t *) pValue;
         } else {
-            pProperties = (t_reverb_properties *) pValue;
+            pProperties = (t_reverb_settings *) pValue;
         }
 
         pPreset = &pReverb->m_sPreset.m_sPreset[pReverb->m_nNextRoom];
diff --git a/media/mtp/MtpClient.cpp b/media/mtp/MtpClient.cpp
index b53fe54..ceb6a43 100644
--- a/media/mtp/MtpClient.cpp
+++ b/media/mtp/MtpClient.cpp
@@ -38,6 +38,16 @@
 
 namespace android {
 
+static bool isMtpDevice(uint16_t vendor, uint16_t product) {
+    // Sandisk Sansa Fuze
+    if (vendor == 0x0781 && product == 0x74c2)
+        return true;
+    // Samsung YP-Z5
+    if (vendor == 0x04e8 && product == 0x503c)
+        return true;
+    return false;
+}
+
 class MtpClientThread : public Thread {
 private:
     MtpClient*   mClient;
@@ -128,8 +138,32 @@
                 LOGD("Found MTP device: \"%s\" \"%s\"\n", usb_device_get_manufacturer_name(device),
                         usb_device_get_product_name(device));
             } else {
-                // not an MTP or PTP device
-                continue;
+                // look for special cased devices based on vendor/product ID
+                // we are doing this mainly for testing purposes
+                uint16_t vendor = usb_device_get_vendor_id(device);
+                uint16_t product = usb_device_get_product_id(device);
+                if (!isMtpDevice(vendor, product)) {
+                    // not an MTP or PTP device
+                    continue;
+                }
+                // request MTP OS string and descriptor
+                // some music players need to see this before entering MTP mode.
+                char buffer[256];
+                memset(buffer, 0, sizeof(buffer));
+                int ret = usb_device_send_control(device,
+                        USB_DIR_IN|USB_RECIP_DEVICE|USB_TYPE_STANDARD,
+                        USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) | 0xEE,
+                        0, sizeof(buffer), buffer);
+                printf("usb_device_send_control returned %d errno: %d\n", ret, errno);
+                if (ret > 0) {
+                    printf("got MTP string %s\n", buffer);
+                    ret = usb_device_send_control(device,
+                            USB_DIR_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR, 1,
+                            0, 4, sizeof(buffer), buffer);
+                    printf("OS descriptor got %d\n", ret);
+                } else {
+                    printf("no MTP string\n");
+                }
             }
 
             // if we got here, then we have a likely MTP or PTP device
@@ -165,7 +199,7 @@
             struct usb_endpoint *ep_intr = usb_endpoint_open(device, ep_intr_desc);
 
             if (usb_device_claim_interface(device, interface->bInterfaceNumber)) {
-                LOGE("usb_device_claim_interface failed\n");
+                LOGE("usb_device_claim_interface failed errno: %d\n", errno);
                 usb_endpoint_close(ep_in);
                 usb_endpoint_close(ep_out);
                 usb_endpoint_close(ep_intr);
diff --git a/media/mtp/MtpCursor.cpp b/media/mtp/MtpCursor.cpp
index 5b6672a..91201c3 100644
--- a/media/mtp/MtpCursor.cpp
+++ b/media/mtp/MtpCursor.cpp
@@ -367,7 +367,7 @@
                     goto fail;
                  break;
             case OBJECT_THUMB:
-                if (!putThumbnail(window, objectID, row, i))
+                if (!putThumbnail(window, objectID, objectInfo->mFormat, row, i))
                     goto fail;
                 break;
             default:
@@ -427,19 +427,25 @@
     return true;
 }
 
-bool MtpCursor::putThumbnail(CursorWindow* window, int objectID, int row, int column) {
+bool MtpCursor::putThumbnail(CursorWindow* window, int objectID, int format, int row, int column) {
     MtpDevice* device = mClient->getDevice(mDeviceID);
-    int size;
-    void* thumbnail = device->getThumbnail(objectID, size);
+    void* thumbnail;
+    int size, offset;
+    if (format == MTP_FORMAT_ASSOCIATION) {
+        thumbnail = NULL;
+        size = offset = 0;
+    } else {
+        thumbnail = device->getThumbnail(objectID, size);
 
-    LOGD("putThumbnail: %p, size: %d\n", thumbnail, size);
-    int offset = window->alloc(size);
-    if (!offset) {
-        window->freeLastRow();
-        LOGE("Failed allocating %u bytes for thumbnail", size);
-        return false;
+        LOGD("putThumbnail: %p, size: %d\n", thumbnail, size);
+        offset = window->alloc(size);
+        if (!offset) {
+            window->freeLastRow();
+            LOGE("Failed allocating %u bytes for thumbnail", size);
+            return false;
+        }
     }
-    if (size > 0)
+    if (thumbnail)
         window->copyIn(offset, (const uint8_t*)thumbnail, size);
 
     // This must be updated after the call to alloc(), since that
diff --git a/media/mtp/MtpCursor.h b/media/mtp/MtpCursor.h
index d51c052..3f84753 100644
--- a/media/mtp/MtpCursor.h
+++ b/media/mtp/MtpCursor.h
@@ -68,7 +68,7 @@
     bool        prepareRow(CursorWindow* window);
     bool        putLong(CursorWindow* window, int value, int row, int column);
     bool        putString(CursorWindow* window, const char* text, int row, int column);
-    bool        putThumbnail(CursorWindow* window, int objectID, int row, int column);
+    bool        putThumbnail(CursorWindow* window, int objectID, int format, int row, int column);
 };
 
 }; // namespace android
diff --git a/media/mtp/MtpDataPacket.cpp b/media/mtp/MtpDataPacket.cpp
index ebe764a..c159e20 100644
--- a/media/mtp/MtpDataPacket.cpp
+++ b/media/mtp/MtpDataPacket.cpp
@@ -20,6 +20,8 @@
 #include <sys/types.h>
 #include <fcntl.h>
 
+#include <usbhost/usbhost.h>
+
 #include "MtpDataPacket.h"
 #include "MtpStringBuffer.h"
 
@@ -391,6 +393,35 @@
     return length;
 }
 
+int MtpDataPacket::readData(struct usb_endpoint *ep, void* buffer, int length) {
+    int packetSize = usb_endpoint_max_packet(ep);
+    int read = 0;
+    while (read < length) {
+        int ret = transfer(ep, (char *)buffer + read, packetSize);
+        if (ret < 0) {
+printf("MtpDataPacket::readData returning %d\n", ret);
+            return ret;
+        }
+        read += ret;
+    }
+printf("MtpDataPacket::readData returning %d\n", read);
+    return read;
+}
+
+int MtpDataPacket::readDataHeader(struct usb_endpoint *ep) {
+    int length = transfer(ep, mBuffer, usb_endpoint_max_packet(ep));
+    if (length >= 0)
+        mPacketSize = length;
+    return length;
+}
+
+int MtpDataPacket::writeDataHeader(struct usb_endpoint *ep, uint32_t length) {
+    MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, length);
+    MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
+    int ret = transfer(ep, mBuffer, MTP_CONTAINER_HEADER_SIZE);
+    return (ret < 0 ? ret : 0);
+}
+
 int MtpDataPacket::write(struct usb_endpoint *ep) {
     MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
     MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
@@ -403,6 +434,19 @@
     return (ret < 0 ? ret : 0);
 }
 
+int MtpDataPacket::write(struct usb_endpoint *ep, void* buffer, uint32_t length) {
+    int ret = 0;
+    int packetSize = usb_endpoint_max_packet(ep);
+    while (length > 0) {
+        int write = (length > packetSize ? packetSize : length);
+        int ret = transfer(ep, buffer, write);
+        if (ret < 0)
+            break;
+        length -= ret;
+    }
+    return (ret < 0 ? ret : 0);
+}
+
 #endif // MTP_HOST
 
 void* MtpDataPacket::getData(int& outLength) const {
diff --git a/media/mtp/MtpDataPacket.h b/media/mtp/MtpDataPacket.h
index 9a24d61..e8314d7 100644
--- a/media/mtp/MtpDataPacket.h
+++ b/media/mtp/MtpDataPacket.h
@@ -98,7 +98,12 @@
 
 #ifdef MTP_HOST
     int                 read(struct usb_endpoint *ep);
+    int                 readData(struct usb_endpoint *ep, void* buffer, int length);
+    int                 readDataHeader(struct usb_endpoint *ep);
+
+    int                 writeDataHeader(struct usb_endpoint *ep, uint32_t length);
     int                 write(struct usb_endpoint *ep);
+    int                 write(struct usb_endpoint *ep, void* buffer, uint32_t length);
 #endif
 
     inline bool         hasData() const { return mPacketSize > MTP_CONTAINER_HEADER_SIZE; }
diff --git a/media/mtp/MtpDevice.cpp b/media/mtp/MtpDevice.cpp
index 3ceb9b4..367694b 100644
--- a/media/mtp/MtpDevice.cpp
+++ b/media/mtp/MtpDevice.cpp
@@ -23,6 +23,7 @@
 #include "MtpProperty.h"
 #include "MtpStorageInfo.h"
 #include "MtpStringBuffer.h"
+#include "MtpUtils.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -31,6 +32,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <endian.h>
 
 #include <usbhost/usbhost.h>
 
@@ -93,6 +95,8 @@
 }
 
 bool MtpDevice::openSession() {
+    Mutex::Autolock autoLock(mMutex);
+
     mSessionID = 0;
     mTransactionID = 0;
     MtpSessionID newSession = 1;
@@ -117,6 +121,8 @@
 }
 
 MtpDeviceInfo* MtpDevice::getDeviceInfo() {
+    Mutex::Autolock autoLock(mMutex);
+
     mRequest.reset();
     if (!sendRequest(MTP_OPERATION_GET_DEVICE_INFO))
         return NULL;
@@ -132,6 +138,8 @@
 }
 
 MtpStorageIDList* MtpDevice::getStorageIDs() {
+    Mutex::Autolock autoLock(mMutex);
+
     mRequest.reset();
     if (!sendRequest(MTP_OPERATION_GET_STORAGE_IDS))
         return NULL;
@@ -145,6 +153,8 @@
 }
 
 MtpStorageInfo* MtpDevice::getStorageInfo(MtpStorageID storageID) {
+    Mutex::Autolock autoLock(mMutex);
+
     mRequest.reset();
     mRequest.setParameter(1, storageID);
     if (!sendRequest(MTP_OPERATION_GET_STORAGE_INFO))
@@ -162,6 +172,8 @@
 
 MtpObjectHandleList* MtpDevice::getObjectHandles(MtpStorageID storageID,
             MtpObjectFormat format, MtpObjectHandle parent) {
+    Mutex::Autolock autoLock(mMutex);
+
     mRequest.reset();
     mRequest.setParameter(1, storageID);
     mRequest.setParameter(2, format);
@@ -178,6 +190,8 @@
 }
 
 MtpObjectInfo* MtpDevice::getObjectInfo(MtpObjectHandle handle) {
+    Mutex::Autolock autoLock(mMutex);
+
     // FIXME - we might want to add some caching here
 
     mRequest.reset();
@@ -196,6 +210,8 @@
 }
 
 void* MtpDevice::getThumbnail(MtpObjectHandle handle, int& outLength) {
+    Mutex::Autolock autoLock(mMutex);
+
     mRequest.reset();
     mRequest.setParameter(1, handle);
     if (sendRequest(MTP_OPERATION_GET_THUMB) && readData()) {
@@ -208,7 +224,90 @@
     return NULL;
 }
 
+MtpObjectHandle MtpDevice::sendObjectInfo(MtpObjectInfo* info) {
+    Mutex::Autolock autoLock(mMutex);
+
+    mRequest.reset();
+    MtpObjectHandle parent = info->mParent;
+    if (parent == 0)
+        parent = MTP_PARENT_ROOT;
+
+    mRequest.setParameter(1, info->mStorageID);
+    mRequest.setParameter(2, info->mParent);
+
+    mData.putUInt32(info->mStorageID);
+    mData.putUInt16(info->mFormat);
+    mData.putUInt16(info->mProtectionStatus);
+    mData.putUInt32(info->mCompressedSize);
+    mData.putUInt16(info->mThumbFormat);
+    mData.putUInt32(info->mThumbCompressedSize);
+    mData.putUInt32(info->mThumbPixWidth);
+    mData.putUInt32(info->mThumbPixHeight);
+    mData.putUInt32(info->mImagePixWidth);
+    mData.putUInt32(info->mImagePixHeight);
+    mData.putUInt32(info->mImagePixDepth);
+    mData.putUInt32(info->mParent);
+    mData.putUInt16(info->mAssociationType);
+    mData.putUInt32(info->mAssociationDesc);
+    mData.putUInt32(info->mSequenceNumber);
+    mData.putString(info->mName);
+
+    char created[100], modified[100];
+    formatDateTime(info->mDateCreated, created, sizeof(created));
+    formatDateTime(info->mDateModified, modified, sizeof(modified));
+
+    mData.putString(created);
+    mData.putString(modified);
+    if (info->mKeywords)
+        mData.putString(info->mKeywords);
+    else
+        mData.putEmptyString();
+
+   if (sendRequest(MTP_OPERATION_SEND_OBJECT_INFO) && sendData()) {
+        printf("MTP_OPERATION_SEND_OBJECT_INFO sent\n");
+        MtpResponseCode ret = readResponse();
+        printf("sendObjectInfo response: %04X\n", ret);
+        if (ret == MTP_RESPONSE_OK) {
+            info->mStorageID = mResponse.getParameter(1);
+            info->mParent = mResponse.getParameter(2);
+            info->mHandle = mResponse.getParameter(3);
+            return info->mHandle;
+        }
+    }
+    return (MtpObjectHandle)-1;
+}
+
+bool MtpDevice::sendObject(MtpObjectInfo* info, int srcFD) {
+    Mutex::Autolock autoLock(mMutex);
+
+    int remaining = info->mCompressedSize;
+    mRequest.reset();
+    mRequest.setParameter(1, info->mHandle);
+    if (sendRequest(MTP_OPERATION_SEND_OBJECT)) {
+        printf("MTP_OPERATION_SEND_OBJECT sent\n");
+        // send data header
+        writeDataHeader(MTP_OPERATION_SEND_OBJECT, remaining);
+
+        char buffer[65536];
+        while (remaining > 0) {
+            int count = read(srcFD, buffer, sizeof(buffer));
+            if (count > 0) {
+                int written = mData.write(mEndpointOut, buffer, count);
+                printf("wrote %d\n", written);
+                // FIXME check error
+                remaining -= count;
+            } else {
+                break;
+            }
+        }
+    }
+    MtpResponseCode ret = readResponse();
+    return (remaining == 0 && ret == MTP_RESPONSE_OK);
+}
+
 bool MtpDevice::deleteObject(MtpObjectHandle handle) {
+    Mutex::Autolock autoLock(mMutex);
+
     mRequest.reset();
     mRequest.setParameter(1, handle);
     if (sendRequest(MTP_OPERATION_DELETE_OBJECT)) {
@@ -236,6 +335,8 @@
 }
 
 MtpProperty* MtpDevice::getDevicePropDesc(MtpDeviceProperty code) {
+    Mutex::Autolock autoLock(mMutex);
+
     mRequest.reset();
     mRequest.setParameter(1, code);
     if (!sendRequest(MTP_OPERATION_GET_DEVICE_PROP_DESC))
@@ -251,6 +352,98 @@
     return NULL;
 }
 
+class ReadObjectThread : public Thread {
+private:
+    MtpDevice*          mDevice;
+    MtpObjectHandle     mHandle;
+    int                 mObjectSize;
+    void*               mInitialData;
+    int                 mInitialDataLength;
+    int                 mFD;
+
+public:
+    ReadObjectThread(MtpDevice* device, MtpObjectHandle handle, int objectSize)
+        : mDevice(device),
+          mHandle(handle),
+          mObjectSize(objectSize),
+          mInitialData(NULL),
+          mInitialDataLength(0)
+    {
+    }
+
+    virtual ~ReadObjectThread() {
+        if (mFD >= 0)
+            close(mFD);
+        free(mInitialData);
+    }
+
+    // returns file descriptor
+    int init() {
+        mDevice->mRequest.reset();
+        mDevice->mRequest.setParameter(1, mHandle);
+        if (mDevice->sendRequest(MTP_OPERATION_GET_OBJECT)
+                && mDevice->mData.readDataHeader(mDevice->mEndpointIn)) {
+
+            // mData will contain header and possibly the beginning of the object data
+            mInitialData = mDevice->mData.getData(mInitialDataLength);
+
+            // create a pipe for the client to read from
+            int pipefd[2];
+            if (pipe(pipefd) < 0) {
+                LOGE("pipe failed (%s)", strerror(errno));
+                return -1;
+            }
+
+            mFD = pipefd[1];
+            return pipefd[0];
+        } else {
+           return -1;
+        }
+    }
+
+    virtual bool threadLoop() {
+        int remaining = mObjectSize;
+        if (mInitialData) {
+            write(mFD, mInitialData, mInitialDataLength);
+            remaining -= mInitialDataLength;
+            free(mInitialData);
+            mInitialData = NULL;
+        }
+
+        char buffer[65536];
+        while (remaining > 0) {
+            int readSize = (remaining > sizeof(buffer) ? sizeof(buffer) : remaining);
+            int count = mDevice->mData.readData(mDevice->mEndpointIn, buffer, readSize);
+            int written;
+            if (count >= 0) {
+                int written = write(mFD, buffer, count);
+                // FIXME check error
+                remaining -= count;
+            } else {
+                break;
+            }
+        }
+
+        MtpResponseCode ret = mDevice->readResponse();
+        mDevice->mMutex.unlock();
+        return false;
+    }
+};
+
+    // returns the file descriptor for a pipe to read the object's data
+int MtpDevice::readObject(MtpObjectHandle handle, int objectSize) {
+    mMutex.lock();
+
+    ReadObjectThread* thread = new ReadObjectThread(this, handle, objectSize);
+    int fd = thread->init();
+    if (fd < 0) {
+        delete thread;
+        mMutex.unlock();
+    } else {
+        thread->run("ReadObjectThread");
+    }
+    return fd;
+}
 
 bool MtpDevice::sendRequest(MtpOperationCode operation) {
     LOGD("sendRequest: %s\n", MtpDebug::getOperationCodeName(operation));
@@ -262,7 +455,7 @@
     return (ret > 0);
 }
 
-bool MtpDevice::sendData(MtpOperationCode operation) {
+bool MtpDevice::sendData() {
     LOGD("sendData\n");
     mData.setOperationCode(mRequest.getOperationCode());
     mData.setTransactionID(mRequest.getTransactionID());
@@ -285,6 +478,12 @@
     }
 }
 
+bool MtpDevice::writeDataHeader(MtpOperationCode operation, int dataLength) {
+    mData.setOperationCode(operation);
+    mData.setTransactionID(mRequest.getTransactionID());
+    return (!mData.writeDataHeader(mEndpointOut, dataLength));
+}
+
 MtpResponseCode MtpDevice::readResponse() {
     LOGD("readResponse\n");
     int ret = mResponse.read(mEndpointIn);
diff --git a/media/mtp/MtpDevice.h b/media/mtp/MtpDevice.h
index e41a872..57f492f 100644
--- a/media/mtp/MtpDevice.h
+++ b/media/mtp/MtpDevice.h
@@ -22,6 +22,8 @@
 #include "MtpResponsePacket.h"
 #include "MtpTypes.h"
 
+#include <utils/threads.h>
+
 struct usb_device;
 
 namespace android {
@@ -52,6 +54,9 @@
     MtpDataPacket           mData;
     MtpResponsePacket       mResponse;
 
+    // to ensure only one MTP transaction at a time
+    Mutex                   mMutex;
+
 public:
                             MtpDevice(struct usb_device* device, int interface,
                                     struct usb_endpoint *ep_in, struct usb_endpoint *ep_out,
@@ -73,16 +78,24 @@
     MtpObjectHandleList*    getObjectHandles(MtpStorageID storageID, MtpObjectFormat format, MtpObjectHandle parent);
     MtpObjectInfo*          getObjectInfo(MtpObjectHandle handle);
     void*                   getThumbnail(MtpObjectHandle handle, int& outLength);
+    MtpObjectHandle         sendObjectInfo(MtpObjectInfo* info);
+    bool                    sendObject(MtpObjectInfo* info, int srcFD);
     bool                    deleteObject(MtpObjectHandle handle);
     MtpObjectHandle         getParent(MtpObjectHandle handle);
     MtpObjectHandle         getStorageID(MtpObjectHandle handle);
 
     MtpProperty*            getDevicePropDesc(MtpDeviceProperty code);
 
+    // returns the file descriptor for a pipe to read the object's data
+    int                     readObject(MtpObjectHandle handle, int objectSize);
+
 private:
+    friend class ReadObjectThread;
+
     bool                    sendRequest(MtpOperationCode operation);
-    bool                    sendData(MtpOperationCode operation);
+    bool                    sendData();
     bool                    readData();
+    bool                    writeDataHeader(MtpOperationCode operation, int dataLength);
     MtpResponseCode         readResponse();
 
 };
diff --git a/media/tests/CameraBrowser/AndroidManifest.xml b/media/tests/CameraBrowser/AndroidManifest.xml
index dbbd62a..5a71179 100644
--- a/media/tests/CameraBrowser/AndroidManifest.xml
+++ b/media/tests/CameraBrowser/AndroidManifest.xml
@@ -1,6 +1,8 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.android.camerabrowser">
 
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+
     <application android:label="@string/app_label">
         <activity android:name="CameraBrowser" android:label="Camera Browser">
             <intent-filter>
diff --git a/media/tests/CameraBrowser/res/menu/object_menu.xml b/media/tests/CameraBrowser/res/menu/object_menu.xml
index caea4ab..a0865f0 100644
--- a/media/tests/CameraBrowser/res/menu/object_menu.xml
+++ b/media/tests/CameraBrowser/res/menu/object_menu.xml
@@ -16,6 +16,8 @@
 
 <menu xmlns:android="http://schemas.android.com/apk/res/android">
 
+    <item android:id="@+id/save"
+        android:title="@string/save_item" />
     <item android:id="@+id/delete"
         android:title="@string/delete_item" />
 </menu>
diff --git a/media/tests/CameraBrowser/res/values/strings.xml b/media/tests/CameraBrowser/res/values/strings.xml
index 8f94e6c..56c5111 100644
--- a/media/tests/CameraBrowser/res/values/strings.xml
+++ b/media/tests/CameraBrowser/res/values/strings.xml
@@ -32,9 +32,12 @@
     <string name="keywords_label">Keywords: </string>
 
     <!-- menu items -->
+    <string name="save_item">Save</string>
     <string name="delete_item">Delete</string>
 
     <!-- toasts -->
+    <string name="object_saved_message">Object saved</string>
+    <string name="save_failed_message">Could not save object</string>
     <string name="object_deleted_message">Object deleted</string>
     <string name="delete_failed_message">Could not delete object</string>
 
diff --git a/media/tests/CameraBrowser/src/com/android/camerabrowser/ObjectBrowser.java b/media/tests/CameraBrowser/src/com/android/camerabrowser/ObjectBrowser.java
index 34f8fff..a002028 100644
--- a/media/tests/CameraBrowser/src/com/android/camerabrowser/ObjectBrowser.java
+++ b/media/tests/CameraBrowser/src/com/android/camerabrowser/ObjectBrowser.java
@@ -26,16 +26,12 @@
 import android.os.Bundle;
 import android.provider.Mtp;
 import android.util.Log;
-import android.view.Menu;
-import android.view.MenuInflater;
-import android.view.MenuItem;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ImageView;
 import android.widget.ListView;
 import android.widget.ResourceCursorAdapter;
 import android.widget.TextView;
-import android.widget.Toast;
 
  /**
  * A list view displaying all objects within a container (folder or storage unit).
@@ -119,47 +115,6 @@
         }
     }
 
-    @Override
-    public boolean onCreateOptionsMenu(Menu menu) {
-        MenuInflater inflater = getMenuInflater();
-        inflater.inflate(R.menu.object_menu, menu);
-        return true;
-    }
-
-    @Override
-    public boolean onPrepareOptionsMenu(Menu menu) {
-        int position = mList.getSelectedItemPosition();
-        MenuItem item = menu.findItem(R.id.delete);
-        item.setEnabled(position != AdapterView.INVALID_POSITION);
-        return true;
-    }
-
-    @Override
-    public boolean onOptionsItemSelected(MenuItem item) {
-        switch (item.getItemId()) {
-            case R.id.delete:
-                deleteSelected();
-                return true;
-        }
-        return false;
-    }
-
-    private void deleteSelected() {
-        int position = mList.getSelectedItemPosition();
-        int rowID = (int)mAdapter.getItemId(position);
-        Uri uri = Mtp.Object.getContentUri(mDeviceID, rowID);
-
-        Log.d(TAG, "deleting " + uri);
-
-        int result = getContentResolver().delete(uri, null, null);
-        if (result > 0) {
-            Toast.makeText(this, R.string.object_deleted_message, Toast.LENGTH_SHORT).show();
-            mCursor.requery();
-        } else {
-            Toast.makeText(this, R.string.delete_failed_message, Toast.LENGTH_SHORT).show();
-        }
-    }
-
     private class ObjectCursorAdapter extends ResourceCursorAdapter {
 
         public ObjectCursorAdapter(Context context, Cursor c) {
diff --git a/media/tests/CameraBrowser/src/com/android/camerabrowser/ObjectViewer.java b/media/tests/CameraBrowser/src/com/android/camerabrowser/ObjectViewer.java
index 3033c54..af18196 100644
--- a/media/tests/CameraBrowser/src/com/android/camerabrowser/ObjectViewer.java
+++ b/media/tests/CameraBrowser/src/com/android/camerabrowser/ObjectViewer.java
@@ -22,12 +22,25 @@
 import android.graphics.BitmapFactory;
 import android.net.Uri;
 import android.os.Bundle;
+import android.os.Environment;
+import android.os.FileUtils;
+import android.os.ParcelFileDescriptor;
+import android.os.Process;
 import android.provider.Mtp;
 import android.util.Log;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
 import android.view.View;
 import android.widget.ImageView;
 import android.widget.TextView;
+import android.widget.Toast;
 
+import java.io.File;
+import java.io.FileDescriptor;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.util.Calendar;
 import java.util.Date;
 
 /**
@@ -106,18 +119,135 @@
             view.setText(c.getString(12));
             byte[] thumbnail = c.getBlob(13);
             if (thumbnail != null) {
-                Log.d(TAG, "got thumbnail, length: " + thumbnail.length);
-                for (int i = 0; i < 50; i++) {
-                    Log.d(TAG, "    " + Integer.toHexString(thumbnail[i]));
-                }
-
                 ImageView thumbView = (ImageView)findViewById(R.id.thumbnail);
                 Bitmap bitmap = BitmapFactory.decodeByteArray(thumbnail, 0, thumbnail.length);
-                Log.d(TAG, "bitmap: " + bitmap);
                 if (bitmap != null) {
                     thumbView.setImageBitmap(bitmap);
                 }
             }
         }
     }
+
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu) {
+        MenuInflater inflater = getMenuInflater();
+        inflater.inflate(R.menu.object_menu, menu);
+        return true;
+    }
+
+    @Override
+    public boolean onPrepareOptionsMenu(Menu menu) {
+        MenuItem item = menu.findItem(R.id.save);
+        item.setEnabled(true);
+        item = menu.findItem(R.id.delete);
+        item.setEnabled(true);
+        return true;
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        switch (item.getItemId()) {
+            case R.id.save:
+                save();
+                return true;
+            case R.id.delete:
+                delete();
+                return true;
+        }
+        return false;
+    }
+
+    private static String getTimestamp() {
+        Calendar c = Calendar.getInstance();
+        c.setTimeInMillis(System.currentTimeMillis());
+        return String.format("%tY-%tm-%td-%tH-%tM-%tS", c, c, c, c, c, c);
+    }
+
+    private void save() {
+        boolean success = false;
+        Uri uri = Mtp.Object.getContentUri(mDeviceID, mObjectID);
+        File destFile = null;
+        ParcelFileDescriptor pfd = null;
+        FileInputStream fis = null;
+        FileOutputStream fos = null;
+
+        try {
+            pfd = getContentResolver().openFileDescriptor(uri, "r");
+            Log.d(TAG, "save got pfd " + pfd);
+            if (pfd != null) {
+                fis = new FileInputStream(pfd.getFileDescriptor());
+                Log.d(TAG, "save got fis " + fis);
+                File destDir = Environment.getExternalStoragePublicDirectory(
+                        Environment.DIRECTORY_DCIM);
+                destDir.mkdirs();
+                destFile = new File(destDir, "CameraBrowser-" + getTimestamp() + ".jpeg");
+
+
+                Log.d(TAG, "save got destFile " + destFile);
+
+                if (destFile.exists()) {
+                    destFile.delete();
+                }
+                fos = new FileOutputStream(destFile);
+
+                byte[] buffer = new byte[65536];
+                int bytesRead;
+                while ((bytesRead = fis.read(buffer)) >= 0) {
+                    Log.d(TAG, "copying the bytes numbering " + bytesRead);
+                    fos.write(buffer, 0, bytesRead);
+                }
+
+                // temporary workaround until we straighten out permissions in /data/media
+                // 1015 is AID_SDCARD_RW
+                FileUtils.setPermissions(destDir.getPath(), 0775, Process.myUid(), 1015);
+                FileUtils.setPermissions(destFile.getPath(), 0664, Process.myUid(), 1015);
+
+                success = true;
+            }
+        } catch (Exception e) {
+            Log.e(TAG, "Exception in ObjectView.save", e);
+        } finally {
+            if (fis != null) {
+                try {
+                    fis.close();
+                } catch (Exception e) {
+                }
+            }
+            if (fos != null) {
+                try {
+                    fos.close();
+                } catch (Exception e) {
+                }
+            }
+            if (pfd != null) {
+                try {
+                    pfd.close();
+                } catch (Exception e) {
+                }
+            }
+        }
+
+        if (success) {
+            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
+            intent.setData(Uri.fromFile(destFile));
+            sendBroadcast(intent);
+            Toast.makeText(this, R.string.object_saved_message, Toast.LENGTH_SHORT).show();
+        } else {
+            Toast.makeText(this, R.string.save_failed_message, Toast.LENGTH_SHORT).show();
+        }
+    }
+
+    private void delete() {
+        Uri uri = Mtp.Object.getContentUri(mDeviceID, mObjectID);
+
+        Log.d(TAG, "deleting " + uri);
+
+        int result = getContentResolver().delete(uri, null, null);
+        if (result > 0) {
+            Toast.makeText(this, R.string.object_deleted_message, Toast.LENGTH_SHORT).show();
+            finish();
+        } else {
+            Toast.makeText(this, R.string.delete_failed_message, Toast.LENGTH_SHORT).show();
+        }
+    }
 }
diff --git a/media/tests/mtp/Android.mk b/media/tests/mtp/Android.mk
new file mode 100644
index 0000000..a9074ed
--- /dev/null
+++ b/media/tests/mtp/Android.mk
@@ -0,0 +1,61 @@
+LOCAL_PATH:= $(call my-dir)
+
+ifneq ($(TARGET_SIMULATOR),true)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := \
+	mtp.cpp \
+	MtpFile.cpp \
+
+LOCAL_C_INCLUDES += \
+    frameworks/base/media/mtp \
+
+LOCAL_CFLAGS := -DMTP_HOST
+
+LOCAL_MODULE := mtp
+
+LOCAL_STATIC_LIBRARIES := libmtp libusbhost libutils libcutils
+
+include $(BUILD_EXECUTABLE)
+
+endif
+
+ifeq ($(HOST_OS),linux)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := \
+	mtp.cpp \
+	MtpFile.cpp \
+	../../../libs/utils/RefBase.cpp \
+	../../../libs/utils/SharedBuffer.cpp \
+	../../../libs/utils/Threads.cpp \
+	../../../libs/utils/VectorImpl.cpp \
+
+LOCAL_C_INCLUDES += \
+    frameworks/base/media/mtp \
+
+LOCAL_CFLAGS := -DMTP_HOST -g -O0
+
+have_readline := $(wildcard /usr/include/readline/readline.h)
+have_history := $(wildcard /usr/lib/libhistory*)
+ifneq ($(strip $(have_readline)),)
+LOCAL_CFLAGS += -DHAVE_READLINE=1
+endif
+
+LOCAL_LDLIBS += -lpthread
+ifneq ($(strip $(have_readline)),)
+LOCAL_LDLIBS += -lreadline -lncurses
+endif
+ifneq ($(strip $(have_history)),)
+LOCAL_LDLIBS += -lhistory
+endif
+
+LOCAL_MODULE := mtp
+
+LOCAL_STATIC_LIBRARIES := libmtp libusbhost libcutils
+
+include $(BUILD_HOST_EXECUTABLE)
+
+endif
diff --git a/media/tests/mtp/MtpFile.cpp b/media/tests/mtp/MtpFile.cpp
new file mode 100644
index 0000000..00d328e
--- /dev/null
+++ b/media/tests/mtp/MtpFile.cpp
@@ -0,0 +1,187 @@
+/*
+ * 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.
+ */
+
+#include "MtpClient.h"
+#include "MtpDevice.h"
+#include "MtpDeviceInfo.h"
+#include "MtpObjectInfo.h"
+#include "MtpStorage.h"
+#include "MtpUtils.h"
+
+#include "MtpFile.h"
+
+namespace android {
+
+MtpClient* MtpFile::sClient = NULL;
+
+MtpFile::MtpFile(MtpDevice* device)
+    :   mDevice(device),
+        mStorage(0),
+        mHandle(0)
+{
+}
+
+MtpFile::MtpFile(MtpDevice* device, MtpStorageID storage)
+    :   mDevice(device),
+        mStorage(storage),
+        mHandle(0)
+{
+}
+
+MtpFile::MtpFile(MtpDevice* device, MtpStorageID storage, MtpObjectHandle handle)
+    :   mDevice(device),
+        mStorage(storage),
+        mHandle(handle)
+{
+}
+
+MtpFile::MtpFile(MtpFile* file)
+    :   mDevice(file->mDevice),
+        mStorage(file->mStorage),
+        mHandle(file->mHandle)
+{
+}
+
+MtpFile::~MtpFile() {
+}
+
+void MtpFile::print() {
+    if (mHandle) {
+
+    } else if (mStorage) {
+        printf("%x\n", mStorage);
+    } else {
+        int id = mDevice->getID();
+        MtpDeviceInfo* info = mDevice->getDeviceInfo();
+        if (info)
+            printf("%d\t%s %s %s\n", id, info->mManufacturer, info->mModel, info->mSerial);
+        else
+            printf("%d\t(no device info available)\n", id);
+        delete info;
+    }
+}
+
+MtpObjectInfo* MtpFile::getObjectInfo() {
+    return mDevice->getObjectInfo(mHandle);
+}
+
+void MtpFile::list() {
+    if (mStorage) {
+        MtpObjectHandleList* handles = mDevice->getObjectHandles(mStorage, 0,
+                                                (mHandle ? mHandle : -1));
+        if (handles) {
+            for (int i = 0; i < handles->size(); i++) {
+                MtpObjectHandle handle = (*handles)[i];
+                MtpObjectInfo* info = mDevice->getObjectInfo(handle);
+                if (info) {
+                    char modified[100];
+                    struct tm tm;
+
+                    gmtime_r(&info->mDateModified, &tm);
+                    strftime(modified, sizeof(modified), "%a %b %e %H:%M:%S GMT %Y", &tm);
+                    printf("%s Handle: %d Format: %04X Size: %d Modified: %s\n",
+                            info->mName, handle, info->mFormat, info->mCompressedSize, modified);
+                    delete info;
+                }
+            }
+            delete handles;
+        }
+    } else {
+        // list storage units for device
+        MtpStorageIDList* storageList = mDevice->getStorageIDs();
+        for (int i = 0; i < storageList->size(); i++) {
+            MtpStorageID storageID = (*storageList)[i];
+            printf("%x\n", storageID);
+        }
+    }
+}
+
+void MtpFile::init(MtpClient* client) {
+    sClient = client;
+}
+
+MtpFile* MtpFile::parsePath(MtpFile* base, char* path) {
+    MtpDevice* device = NULL;
+    MtpStorageID storage = 0;
+    MtpObjectHandle handle = 0;
+
+    if (path[0] != '/' && base) {
+        device = base->mDevice;
+        storage = base->mStorage;
+        handle = base->mHandle;
+    }
+
+    // parse an absolute path
+    if (path[0] == '/')
+        path++;
+    char* tok = strtok(path, "/");
+    while (tok) {
+        if (storage) {
+            // find child of current handle
+            MtpObjectHandleList* handles = device->getObjectHandles(storage, 0,
+                                                    (handle ? handle : -1));
+            MtpObjectHandle childHandle = 0;
+
+            if (handles) {
+                for (int i = 0; i < handles->size() && !childHandle; i++) {
+                    MtpObjectHandle handle = (*handles)[i];
+                    MtpObjectInfo* info = device->getObjectInfo(handle);
+                    if (info && !strcmp(tok, info->mName))
+                        childHandle = handle;
+                    delete info;
+                }
+                delete handles;
+            }
+            if (childHandle)
+                handle = childHandle;
+            else
+                return NULL;
+        } else if (device) {
+            unsigned int id;
+            // find storage for the device
+            if (sscanf(tok, "%x", &id) == 1) {
+                MtpStorageIDList* storageList = device->getStorageIDs();
+                bool found = false;
+                for (int i = 0; i < storageList->size(); i++) {
+                    if ((*storageList)[i] == id) {
+                        found = true;
+                        break;
+                    }
+                }
+                if (found)
+                    storage = id;
+                else
+                    return NULL;
+            }
+        } else {
+            // find device
+            unsigned int id;
+            if (sscanf(tok, "%d", &id) == 1)
+                device = sClient->getDevice(id);
+            if (!device)
+                return NULL;
+        }
+
+        tok = strtok(NULL, "/");
+    }
+
+    if (device)
+        return new MtpFile(device, storage, handle);
+    else
+        return NULL;
+}
+
+}
diff --git a/media/tests/mtp/MtpFile.h b/media/tests/mtp/MtpFile.h
new file mode 100644
index 0000000..ab8762b
--- /dev/null
+++ b/media/tests/mtp/MtpFile.h
@@ -0,0 +1,57 @@
+/*
+ * 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.
+ */
+
+#ifndef _MTP_FILE_H
+#define _MTP_FILE_H
+
+#include "MtpTypes.h"
+
+namespace android {
+
+class MtpClient;
+class MtpDevice;
+class MtpObjectInfo;
+
+// File-like abstraction for the interactive shell.
+// This can be used to represent an MTP device, storage unit or object
+// (either file or association).
+class MtpFile {
+private:
+    MtpDevice*          mDevice;
+    MtpStorageID        mStorage;
+    MtpObjectHandle     mHandle;
+    static MtpClient*   sClient;
+
+public:
+    MtpFile(MtpDevice* device);
+    MtpFile(MtpDevice* device, MtpStorageID storage);
+    MtpFile(MtpDevice* device, MtpStorageID storage, MtpObjectHandle handle);
+    MtpFile(MtpFile* file);
+    virtual ~MtpFile();
+
+    MtpObjectInfo* getObjectInfo();
+    void print();
+    void list();
+
+    inline MtpDevice* getDevice() const { return mDevice; }
+
+    static void init(MtpClient* client);
+    static MtpFile* parsePath(MtpFile* base, char* path);
+};
+
+}
+
+#endif // _MTP_DIRECTORY_H
diff --git a/media/tests/mtp/mtp.cpp b/media/tests/mtp/mtp.cpp
new file mode 100644
index 0000000..2c5e23b
--- /dev/null
+++ b/media/tests/mtp/mtp.cpp
@@ -0,0 +1,370 @@
+/*
+ * 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.
+ */
+
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#if HAVE_READLINE
+#include <readline/readline.h>
+#include <readline/history.h>
+#endif
+
+#include "MtpClient.h"
+#include "MtpDevice.h"
+#include "MtpObjectInfo.h"
+
+#include "MtpFile.h"
+
+#define PROMPT  "mtp> "
+
+using namespace android;
+
+static MtpClient* sClient = NULL;
+
+// current working directory information for interactive shell
+static MtpFile* sCurrentDirectory = NULL;
+
+static MtpFile* parse_path(char* path) {
+    return MtpFile::parsePath(sCurrentDirectory, path);
+}
+
+class MyClient : public MtpClient {
+private:
+    virtual void deviceAdded(MtpDevice *device) {
+    }
+
+    virtual void deviceRemoved(MtpDevice *device) {
+    }
+
+public:
+};
+
+static void init() {
+    sClient = new MyClient;
+    sClient->start();
+    MtpFile::init(sClient);
+}
+
+static int set_cwd(int argc, char* argv[]) {
+    if (argc != 1) {
+        fprintf(stderr, "cd should have one argument\n");
+        return -1;
+    }
+    if (!strcmp(argv[0], "/")) {
+        delete sCurrentDirectory;
+        sCurrentDirectory = NULL;
+    }
+    else {
+        MtpFile* file = parse_path(argv[0]);
+        if (file) {
+            delete sCurrentDirectory;
+            sCurrentDirectory = file;
+        } else {
+            fprintf(stderr, "could not find %s\n", argv[0]);
+            return -1;
+        }
+    }
+    return 0;
+}
+
+static void list_devices() {
+    // TODO - need to make sure the list will not change while iterating
+    MtpDeviceList& devices = sClient->getDeviceList();
+    for (int i = 0; i < devices.size(); i++) {
+        MtpDevice* device = devices[i];
+        MtpFile* file = new MtpFile(device);
+        file->print();
+        delete file;
+    }
+}
+
+static int list(int argc, char* argv[]) {
+    if (argc == 0) {
+        // list cwd
+        if (sCurrentDirectory) {
+            sCurrentDirectory->list();
+        } else {
+            list_devices();
+        }
+    }
+
+    for (int i = 0; i < argc; i++) {
+        char* path = argv[i];
+        if (!strcmp(path, "/")) {
+            list_devices();
+        } else {
+            MtpFile* file = parse_path(path);
+            if (!file) {
+                fprintf(stderr, "could not find %s\n", path);
+                return -1;
+            }
+            file->list();
+        }
+    }
+
+    return 0;
+}
+
+static int get_file(int argc, char* argv[]) {
+    int ret = -1;
+    int srcFD = -1;
+    int destFD = -1;
+    MtpFile* srcFile = NULL;
+    MtpObjectInfo* info = NULL;
+    char* dest;
+
+    if (argc < 1) {
+        fprintf(stderr, "not enough arguments\n");
+        return -1;
+    } else if (argc > 2) {
+        fprintf(stderr, "too many arguments\n");
+        return -1;
+    }
+
+    // find source object
+    char* src = argv[0];
+    srcFile = parse_path(src);
+    if (!srcFile) {
+        fprintf(stderr, "could not find %s\n", src);
+        return -1;
+    }
+    info = srcFile->getObjectInfo();
+    if (!info) {
+        fprintf(stderr, "could not find object info for %s\n", src);
+        goto fail;
+    }
+    if (info->mFormat == MTP_FORMAT_ASSOCIATION) {
+        fprintf(stderr, "copying directories not implemented yet\n");
+        goto fail;
+    }
+
+    dest = (argc > 1 ? argv[1] : info->mName);
+    destFD = open(dest, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+    if (destFD < 0) {
+        fprintf(stderr, "could not create %s\n", dest);
+        goto fail;
+    }
+    srcFD = srcFile->getDevice()->readObject(info->mHandle, info->mCompressedSize);
+    if (srcFD < 0)
+        goto fail;
+
+    char buffer[65536];
+    while (1) {
+        int count = read(srcFD, buffer, sizeof(buffer));
+        if (count <= 0)
+            break;
+        write(destFD, buffer, count);
+    }
+    // FIXME - error checking and reporting
+    ret = 0;
+
+fail:
+    delete srcFile;
+    delete info;
+    if (srcFD >= 0)
+        close(srcFD);
+    if (destFD >= 0)
+        close(destFD);
+    return ret;
+}
+
+static int put_file(int argc, char* argv[]) {
+    int ret = -1;
+    int srcFD = -1;
+    MtpFile* destFile = NULL;
+    MtpObjectInfo* srcInfo = NULL;
+    MtpObjectInfo* destInfo = NULL;
+    MtpObjectHandle handle;
+    struct stat statbuf;
+    const char* lastSlash;
+
+    if (argc < 1) {
+        fprintf(stderr, "not enough arguments\n");
+        return -1;
+    } else if (argc > 2) {
+        fprintf(stderr, "too many arguments\n");
+        return -1;
+    }
+    const char* src = argv[0];
+    srcFD = open(src, O_RDONLY);
+    if (srcFD < 0) {
+        fprintf(stderr, "could not open %s\n", src);
+        goto fail;
+    }
+    if (argc == 2) {
+         char* dest = argv[1];
+        destFile = parse_path(dest);
+        if (!destFile) {
+            fprintf(stderr, "could not find %s\n", dest);
+            goto fail;
+        }
+    } else {
+        if (!sCurrentDirectory) {
+            fprintf(stderr, "current working directory not set\n");
+            goto fail;
+        }
+        destFile = new MtpFile(sCurrentDirectory);
+    }
+
+    destInfo = destFile->getObjectInfo();
+    if (!destInfo) {
+        fprintf(stderr, "could not find object info destination directory\n");
+        goto fail;
+    }
+    if (destInfo->mFormat != MTP_FORMAT_ASSOCIATION) {
+        fprintf(stderr, "destination not a directory\n");
+        goto fail;
+    }
+
+    if (fstat(srcFD, &statbuf))
+        goto fail;
+
+    srcInfo = new MtpObjectInfo(0);
+    srcInfo->mStorageID = destInfo->mStorageID;
+    srcInfo->mFormat = MTP_FORMAT_EXIF_JPEG;  // FIXME
+    srcInfo->mCompressedSize = statbuf.st_size;
+    srcInfo->mParent = destInfo->mHandle;
+    lastSlash = strrchr(src, '/');
+    srcInfo->mName = strdup(lastSlash ? lastSlash + 1 : src);
+    srcInfo->mDateModified = statbuf.st_mtime;
+    handle = destFile->getDevice()->sendObjectInfo(srcInfo);
+    if (handle <= 0) {
+        printf("sendObjectInfo returned %04X\n", handle);
+        goto fail;
+    }
+    if (destFile->getDevice()->sendObject(srcInfo, srcFD))
+        ret = 0;
+
+fail:
+    delete destFile;
+    delete srcInfo;
+    delete destInfo;
+    if (srcFD >= 0)
+        close(srcFD);
+    printf("returning %d\n", ret);
+    return ret;
+}
+
+typedef int (* command_func)(int argc, char* argv[]);
+
+struct command_table_entry {
+    const char* name;
+    command_func func;
+};
+
+const command_table_entry command_list[] = {
+    {   "cd",       set_cwd         },
+    {   "ls",       list            },
+    {   "get",      get_file        },
+    {   "put",      put_file        },
+    {   NULL,       NULL            },
+};
+
+
+static int do_command(int argc, char* argv[]) {
+    const command_table_entry* command = command_list;
+    const char* name = *argv++;
+    argc--;
+
+    while (command->name) {
+        if (!strcmp(command->name, name))
+            return command->func(argc, argv);
+        else
+            command++;
+    }
+    fprintf(stderr, "unknown command %s\n", name);
+    return -1;
+}
+
+static int shell() {
+    int argc;
+    int result = 0;
+#define MAX_ARGS    100
+    char* argv[MAX_ARGS];
+
+#if HAVE_READLINE
+    using_history();
+#endif
+
+    while (1) {
+#if HAVE_READLINE
+        char* line = readline(PROMPT);
+        if (!line) {
+            printf("\n");
+            exit(0);
+        }
+#else
+        char    buffer[1000];
+        printf("%s", PROMPT);
+        char* line = NULL;
+        size_t length = 0;
+
+        buffer[0] = 0;
+        fgets(buffer, sizeof(buffer), stdin);
+        int count = strlen(buffer);
+        if (count > 0 && buffer[0] == EOF) {
+            printf("\n");
+            exit(0);
+        }
+        if (count > 0 && line[count - 1] == '\n')
+            line[count - 1] == 0;
+#endif
+        char* tok = strtok(line, " \t\n\r");
+        if (!tok)
+            continue;
+        if (!strcmp(tok, "quit") || !strcmp(tok, "exit")) {
+            exit(0);
+        }
+#if HAVE_READLINE
+        add_history(line);
+#endif
+        argc = 0;
+        while (tok) {
+            if (argc + 1 == MAX_ARGS) {
+                fprintf(stderr, "too many arguments\n");
+                result = -1;
+                goto bottom_of_loop;
+            }
+
+            argv[argc++] = strdup(tok);
+            tok = strtok(NULL, " \t\n\r");
+        }
+
+        result = do_command(argc, argv);
+
+bottom_of_loop:
+        for (int i = 0; i < argc; i++)
+            free(argv[i]);
+        free(line);
+    }
+
+    return result;
+}
+
+int main(int argc, char* argv[]) {
+    init();
+
+    if (argc == 1)
+        return shell();
+    else
+        return do_command(argc - 1, argv + 1);
+}
diff --git a/packages/DefaultContainerService/res/values/strings.xml b/packages/DefaultContainerService/res/values/strings.xml
index 2897f34..37f5b61 100644
--- a/packages/DefaultContainerService/res/values/strings.xml
+++ b/packages/DefaultContainerService/res/values/strings.xml
@@ -19,5 +19,5 @@
 -->
 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
     <!-- service name  -->
-    <string name="service_name">Media Container Service</string>
+    <string name="service_name">Package Access Helper</string>
 </resources>
diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindow.java b/policy/src/com/android/internal/policy/impl/PhoneWindow.java
index cc91f31..5771e85 100644
--- a/policy/src/com/android/internal/policy/impl/PhoneWindow.java
+++ b/policy/src/com/android/internal/policy/impl/PhoneWindow.java
@@ -55,6 +55,7 @@
 import android.util.EventLog;
 import android.util.Log;
 import android.util.SparseArray;
+import android.view.ActionMode;
 import android.view.Gravity;
 import android.view.HapticFeedbackConstants;
 import android.view.InputQueue;
@@ -78,12 +79,9 @@
 import android.view.inputmethod.InputMethodManager;
 import android.widget.FrameLayout;
 import android.widget.ImageView;
-import android.widget.ListPopupWindow;
 import android.widget.ProgressBar;
 import android.widget.TextView;
 
-import java.lang.ref.WeakReference;
-
 /**
  * Android-specific Window.
  * <p>
@@ -1930,6 +1928,19 @@
             return mContextMenuHelper != null;
         }
 
+        @Override
+        public ActionMode startActionModeForChild(View originalView,
+                ActionMode.Callback callback) {
+            // originalView can be used here to be sure that we don't obscure
+            // relevant content with the context mode UI.
+            return startActionMode(callback);
+        }
+
+        @Override
+        public ActionMode startActionMode(ActionMode.Callback callback) {
+            return getCallback().onStartActionMode(callback);
+        }
+
         public void startChanging() {
             mChanging = true;
         }
diff --git a/services/java/com/android/server/PackageManagerService.java b/services/java/com/android/server/PackageManagerService.java
index af2145e..01376eb 100644
--- a/services/java/com/android/server/PackageManagerService.java
+++ b/services/java/com/android/server/PackageManagerService.java
@@ -1495,6 +1495,7 @@
                 ps.pkg.applicationInfo.publicSourceDir = ps.resourcePathString;
                 ps.pkg.applicationInfo.sourceDir = ps.codePathString;
                 ps.pkg.applicationInfo.dataDir = getDataPathForPackage(ps.pkg).getPath();
+                ps.pkg.mSetEnabled = ps.enabled;
             }
             return generatePackageInfo(ps.pkg, flags);
         }
@@ -6875,6 +6876,7 @@
                     return;
                 }
                 pkgSetting.enabled = newState;
+                pkgSetting.pkg.mSetEnabled = newState;
             } else {
                 // We're dealing with a component level state change
                 switch (newState) {
@@ -8261,6 +8263,7 @@
 
         private void insertPackageSettingLP(PackageSetting p, PackageParser.Package pkg) {
             p.pkg = pkg;
+            pkg.mSetEnabled = p.enabled;
             String codePath = pkg.applicationInfo.sourceDir;
             String resourcePath = pkg.applicationInfo.publicSourceDir;
             // Update code path if needed
@@ -9486,6 +9489,9 @@
         }
 
         boolean isEnabledLP(ComponentInfo componentInfo, int flags) {
+            if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
+                return true;
+            }
             final PackageSetting packageSettings = mPackages.get(componentInfo.packageName);
             if (Config.LOGV) {
                 Log.v(TAG, "isEnabledLock - packageName = " + componentInfo.packageName
@@ -9501,14 +9507,20 @@
                     Debug.waitForDebugger();
                     Log.i(TAG, "We will crash!");
                 }
+                return false;
             }
-            return ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0)
-                   || ((componentInfo.enabled
-                        && ((packageSettings.enabled == COMPONENT_ENABLED_STATE_ENABLED)
-                            || (componentInfo.applicationInfo.enabled
-                                && packageSettings.enabled != COMPONENT_ENABLED_STATE_DISABLED))
-                        && !packageSettings.disabledComponents.contains(componentInfo.name))
-                       || packageSettings.enabledComponents.contains(componentInfo.name));
+            if (packageSettings.enabled == COMPONENT_ENABLED_STATE_DISABLED
+                    || (packageSettings.pkg != null && !packageSettings.pkg.applicationInfo.enabled
+                            && packageSettings.enabled == COMPONENT_ENABLED_STATE_DEFAULT)) {
+                return false;
+            }
+            if (packageSettings.enabledComponents.contains(componentInfo.name)) {
+                return true;
+            }
+            if (packageSettings.disabledComponents.contains(componentInfo.name)) {
+                return false;
+            }
+            return componentInfo.enabled;
         }
     }
 
diff --git a/services/java/com/android/server/am/ActivityManagerService.java b/services/java/com/android/server/am/ActivityManagerService.java
index 0c8769e..fa2ec1f 100644
--- a/services/java/com/android/server/am/ActivityManagerService.java
+++ b/services/java/com/android/server/am/ActivityManagerService.java
@@ -6755,7 +6755,7 @@
                 }
                 return;
             } else if ("service".equals(cmd)) {
-                dumpService(fd, pw, args, opti, true);
+                dumpService(fd, pw, args, opti, dumpAll);
                 return;
             } else if ("services".equals(cmd) || "s".equals(cmd)) {
                 synchronized (this) {
@@ -7061,20 +7061,28 @@
             componentNameString = args[opti];
             opti++;
             ComponentName componentName = ComponentName.unflattenFromString(componentNameString);
-            r = componentName != null ? mServices.get(componentName) : null;
+            synchronized (this) {
+                r = componentName != null ? mServices.get(componentName) : null;
+            }
             newArgs = new String[args.length - opti];
             if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti);
         }
 
         if (r != null) {
-            dumpService(fd, pw, r, newArgs);
+            dumpService(fd, pw, r, newArgs, dumpAll);
         } else {
-            for (ServiceRecord r1 : mServices.values()) {
-                if (componentNameString == null
-                        || r1.name.flattenToString().contains(componentNameString)) {
-                    dumpService(fd, pw, r1, newArgs);
+            ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>();
+            synchronized (this) {
+                for (ServiceRecord r1 : mServices.values()) {
+                    if (componentNameString == null
+                            || r1.name.flattenToString().contains(componentNameString)) {
+                        services.add(r1);
+                    }
                 }
             }
+            for (int i=0; i<services.size(); i++) {
+                dumpService(fd, pw, services.get(i), newArgs, dumpAll);
+            }
         }
     }
 
@@ -7082,8 +7090,16 @@
      * Invokes IApplicationThread.dumpService() on the thread of the specified service if
      * there is a thread associated with the service.
      */
-    private void dumpService(FileDescriptor fd, PrintWriter pw, ServiceRecord r, String[] args) {
+    private void dumpService(FileDescriptor fd, PrintWriter pw, ServiceRecord r, String[] args,
+            boolean dumpAll) {
         pw.println("  Service " + r.name.flattenToString());
+        if (dumpAll) {
+            synchronized (this) {
+                pw.print("  * "); pw.println(r);
+                r.dump(pw, "    ");
+            }
+            pw.println("");
+        }
         if (r.app != null && r.app.thread != null) {
             try {
                 // flush anything that is already in the PrintWriter since the thread is going
@@ -7091,6 +7107,7 @@
                 pw.flush();
                 r.app.thread.dumpService(fd, r, args);
                 pw.print("\n");
+                pw.flush();
             } catch (RemoteException e) {
                 pw.println("got a RemoteException while dumping the service");
             }
diff --git a/services/sensorservice/SensorService.cpp b/services/sensorservice/SensorService.cpp
index b79373d..3025f77 100644
--- a/services/sensorservice/SensorService.cpp
+++ b/services/sensorservice/SensorService.cpp
@@ -24,6 +24,7 @@
 #include <utils/Errors.h>
 #include <utils/RefBase.h>
 #include <utils/Singleton.h>
+#include <utils/String16.h>
 
 #include <binder/BinderService.h>
 #include <binder/IServiceManager.h>
@@ -38,39 +39,65 @@
 namespace android {
 // ---------------------------------------------------------------------------
 
-/*
- * TODO:
- * - make sure to keep the last value of each event type so we can quickly
- *   send something to application when they enable a sensor that is already
- *   active (the issue here is that it can take time before a value is
- *   produced by the h/w if the rate is low or if it's a one-shot sensor).
- * - send sensor info to battery service
- */
-
-// ---------------------------------------------------------------------------
-
 class BatteryService : public Singleton<BatteryService> {
+    static const int TRANSACTION_noteStartSensor = IBinder::FIRST_CALL_TRANSACTION + 3;
+    static const int TRANSACTION_noteStopSensor = IBinder::FIRST_CALL_TRANSACTION + 4;
+    static const String16 DESCRIPTOR;
+
     friend class Singleton<BatteryService>;
     sp<IBinder> mBatteryStatService;
+
     BatteryService() {
-        const String16 name("batteryinfo");
-        //getService(name, &mBatteryStatService);
+        const sp<IServiceManager> sm(defaultServiceManager());
+        if (sm != NULL) {
+            const String16 name("batteryinfo");
+            mBatteryStatService = sm->getService(name);
+        }
     }
+
+    status_t noteStartSensor(int uid, int handle) {
+        Parcel data, reply;
+        data.writeInterfaceToken(DESCRIPTOR);
+        data.writeInt32(uid);
+        data.writeInt32(handle);
+        status_t err = mBatteryStatService->transact(
+                TRANSACTION_noteStartSensor, data, &reply, 0);
+        err = reply.readExceptionCode();
+        return err;
+    }
+
+    status_t noteStopSensor(int uid, int handle) {
+        Parcel data, reply;
+        data.writeInterfaceToken(DESCRIPTOR);
+        data.writeInt32(uid);
+        data.writeInt32(handle);
+        status_t err = mBatteryStatService->transact(
+                TRANSACTION_noteStopSensor, data, &reply, 0);
+        err = reply.readExceptionCode();
+        return err;
+    }
+
 public:
     void enableSensor(int handle) {
         if (mBatteryStatService != 0) {
             int uid = IPCThreadState::self()->getCallingUid();
-            //mBatteryStatService->noteStartSensor(uid, handle);
+            int64_t identity = IPCThreadState::self()->clearCallingIdentity();
+            noteStartSensor(uid, handle);
+            IPCThreadState::self()->restoreCallingIdentity(identity);
         }
     }
     void disableSensor(int handle) {
         if (mBatteryStatService != 0) {
             int uid = IPCThreadState::self()->getCallingUid();
-            //mBatteryStatService->noteStopSensor(uid, handle);
+            int64_t identity = IPCThreadState::self()->clearCallingIdentity();
+            noteStopSensor(uid, handle);
+            IPCThreadState::self()->restoreCallingIdentity(identity);
         }
     }
 };
 
+const String16 BatteryService::DESCRIPTOR("com.android.internal.app.IBatteryStats");
+
 ANDROID_SINGLETON_STATIC_INSTANCE(BatteryService)
 
 // ---------------------------------------------------------------------------
@@ -103,8 +130,12 @@
         LOGE_IF(err, "couldn't open device for module %s (%s)",
                 SENSORS_HARDWARE_MODULE_ID, strerror(-err));
 
+        sensors_event_t event;
+        memset(&event, 0, sizeof(event));
+
         struct sensor_t const* list;
         int count = mSensorModule->get_sensors_list(mSensorModule, &list);
+        mLastEventSeen.setCapacity(count);
         for (int i=0 ; i<count ; i++) {
             Sensor sensor(list + i);
             LOGI("%s", sensor.getName().string());
@@ -112,6 +143,7 @@
             if (mSensorDevice) {
                 mSensorDevice->activate(mSensorDevice, sensor.getHandle(), 0);
             }
+            mLastEventSeen.add(sensor.getHandle(), event);
         }
 
         if (mSensorDevice) {
@@ -138,6 +170,19 @@
         result.append(buffer);
     } else {
         Mutex::Autolock _l(mLock);
+        snprintf(buffer, SIZE, "Sensor List:\n");
+        result.append(buffer);
+        for (size_t i=0 ; i<mSensorList.size() ; i++) {
+            const Sensor& s(mSensorList[i]);
+            const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
+            snprintf(buffer, SIZE, "%s (vendor=%s, handle=%d, last=<%5.1f,%5.1f,%5.1f>)\n",
+                    s.getName().string(),
+                    s.getVendor().string(),
+                    s.getHandle(),
+                    e.data[0], e.data[1], e.data[2]);
+            result.append(buffer);
+        }
+
         snprintf(buffer, SIZE, "%d active connections\n",
                 mActiveConnections.size());
         result.append(buffer);
@@ -178,6 +223,19 @@
         size_t numConnections = activeConnections.size();
         if (numConnections) {
             Mutex::Autolock _l(mLock);
+
+            // record the last event for each sensor
+            int32_t prev = buffer[0].sensor;
+            for (ssize_t i=1 ; i<count ; i++) {
+                // record the last event of each sensor type in this buffer
+                int32_t curr = buffer[i].sensor;
+                if (curr != prev) {
+                    mLastEventSeen.editValueFor(prev) = buffer[i-1];
+                    prev = curr;
+                }
+            }
+            mLastEventSeen.editValueFor(prev) = buffer[count-1];
+
             for (size_t i=0 ; i<numConnections ; i++) {
                 sp<SensorEventConnection> connection(activeConnections[i].promote());
                 if (connection != 0) {
@@ -258,7 +316,16 @@
             BatteryService::getInstance().enableSensor(handle);
         }
     } else {
-        rec->addConnection(connection);
+        if (rec->addConnection(connection)) {
+            // this sensor is already activated, but we are adding a
+            // connection that uses it. Immediately send down the last
+            // known value of the requested sensor.
+            sensors_event_t scratch;
+            sensors_event_t& event(mLastEventSeen.editValueFor(handle));
+            if (event.version == sizeof(sensors_event_t)) {
+                connection->sendEvents(&event, 1);
+            }
+        }
     }
     if (err == NO_ERROR) {
         // connection now active
@@ -430,18 +497,27 @@
         sensors_event_t* scratch)
 {
     // filter out events not for this connection
-    size_t count=0, i=0;
-    while (i<numEvents) {
-        const int32_t curr = buffer[i].sensor;
-        if (mSensorInfo.indexOfKey(curr) >= 0) {
-            do {
-                scratch[count++] = buffer[i++];
-            } while ((i<numEvents) && (buffer[i].sensor == curr));
-        } else {
-            i++;
+    size_t count = 0;
+    if (scratch) {
+        size_t i=0;
+        while (i<numEvents) {
+            const int32_t curr = buffer[i].sensor;
+            if (mSensorInfo.indexOfKey(curr) >= 0) {
+                do {
+                    scratch[count++] = buffer[i++];
+                } while ((i<numEvents) && (buffer[i].sensor == curr));
+            } else {
+                i++;
+            }
         }
+    } else {
+        scratch = const_cast<sensors_event_t *>(buffer);
+        count = numEvents;
     }
 
+    if (count == 0)
+        return 0;
+
     ssize_t size = mChannel->write(scratch, count*sizeof(sensors_event_t));
     if (size == -EAGAIN) {
         // the destination doesn't accept events anymore, it's probably
diff --git a/services/sensorservice/SensorService.h b/services/sensorservice/SensorService.h
index f77652d..9f37799 100644
--- a/services/sensorservice/SensorService.h
+++ b/services/sensorservice/SensorService.h
@@ -87,7 +87,7 @@
         SensorEventConnection(const sp<SensorService>& service);
 
         status_t sendEvents(sensors_event_t const* buffer, size_t count,
-                sensors_event_t* scratch);
+                sensors_event_t* scratch = NULL);
         bool hasSensor(int32_t handle) const;
         bool hasAnySensor() const;
         bool addSensor(int32_t handle);
@@ -123,6 +123,9 @@
     DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
     SortedVector< wp<SensorEventConnection> > mActiveConnections;
 
+    // The size of this vector is constant, only the items are mutable
+    KeyedVector<int32_t, sensors_event_t> mLastEventSeen;
+
 public:
     static char const* getServiceName() { return "sensorservice"; }
 
diff --git a/telephony/java/com/android/internal/telephony/CallManager.java b/telephony/java/com/android/internal/telephony/CallManager.java
index 9a13a5f..1381a2d 100644
--- a/telephony/java/com/android/internal/telephony/CallManager.java
+++ b/telephony/java/com/android/internal/telephony/CallManager.java
@@ -16,8 +16,6 @@
 
 package com.android.internal.telephony;
 
-
-
 import android.content.Context;
 import android.os.AsyncResult;
 import android.os.Handler;
@@ -25,7 +23,6 @@
 import android.os.RegistrantList;
 import android.telephony.PhoneStateListener;
 
-
 import java.util.ArrayList;
 import java.util.List;
 
@@ -49,8 +46,23 @@
 public final class CallManager {
 
     private static final int EVENT_DISCONNECT = 100;
-    private static final int EVENT_CALL_STATE_CHANGED = 101;
-
+    private static final int EVENT_PRECISE_CALL_STATE_CHANGED = 101;
+    private static final int EVENT_NEW_RINGING_CONNECTION = 102;
+    private static final int EVENT_UNKNOWN_CONNECTION = 103;
+    private static final int EVENT_INCOMING_RING = 104;
+    private static final int EVENT_RINGBACK_TONE = 105;
+    private static final int EVENT_IN_CALL_VOICE_PRIVACY_ON = 106;
+    private static final int EVENT_IN_CALL_VOICE_PRIVACY_OFF = 107;
+    private static final int EVENT_CALL_WAITING = 108;
+    private static final int EVENT_DISPLAY_INFO = 109;
+    private static final int EVENT_SIGNAL_INFO = 110;
+    private static final int EVENT_CDMA_OTA_STATUS_CHANGE = 111;
+    private static final int EVENT_RESEND_INCALL_MUTE = 112;
+    private static final int EVENT_MMI_INITIATE = 113;
+    private static final int EVENT_MMI_COMPLETE = 114;
+    private static final int EVENT_ECM_TIMER_RESET = 115;
+    private static final int EVENT_SUBSCRIPTION_INFO_READY = 116;
+    private static final int EVENT_SUPP_SERVICE_FAILED = 117;
 
     // Singleton instance
     private static final CallManager INSTANCE = new CallManager();
@@ -89,15 +101,48 @@
     protected final RegistrantList mServiceStateRegistrants
     = new RegistrantList();
 
-    protected final RegistrantList mMmiCompleteRegistrants
-    = new RegistrantList();
-
     protected final RegistrantList mMmiRegistrants
     = new RegistrantList();
 
     protected final RegistrantList mUnknownConnectionRegistrants
     = new RegistrantList();
 
+    protected final RegistrantList mRingbackToneRegistrants
+    = new RegistrantList();
+
+    protected final RegistrantList mInCallVoicePrivacyOnRegistrants
+    = new RegistrantList();
+
+    protected final RegistrantList mInCallVoicePrivacyOffRegistrants
+    = new RegistrantList();
+
+    protected final RegistrantList mCallWaitingRegistrants
+    = new RegistrantList();
+
+    protected final RegistrantList mDisplayInfoRegistrants
+    = new RegistrantList();
+
+    protected final RegistrantList mSignalInfoRegistrants
+    = new RegistrantList();
+
+    protected final RegistrantList mCdmaOtaStatusChangeRegistrants
+    = new RegistrantList();
+
+    protected final RegistrantList mResendIncallMuteRegistrants
+    = new RegistrantList();
+
+    protected final RegistrantList mMmiInitiateRegistrants
+    = new RegistrantList();
+
+    protected final RegistrantList mMmiCompleteRegistrants
+    = new RegistrantList();
+
+    protected final RegistrantList mEcmTimerResetRegistrants
+    = new RegistrantList();
+
+    protected final RegistrantList mSubscriptionInfoReadyRegistrants
+    = new RegistrantList();
+
     protected final RegistrantList mSuppServiceFailedRegistrants
     = new RegistrantList();
 
@@ -118,9 +163,28 @@
     }
 
     /**
+     * Get current coarse-grained voice call state.
+     * If the Call Manager has an active call and call waiting occurs,
+     * then the phone state is RINGING not OFFHOOK
+     *
+     */
+    public Phone.State getState() {
+        Phone.State s = Phone.State.IDLE;
+
+        for(Phone phone : mPhones) {
+            if (phone.getState() == Phone.State.RINGING) {
+                return Phone.State.RINGING;
+            } else if (phone.getState() == Phone.State.OFFHOOK) {
+                s = Phone.State.OFFHOOK;
+            }
+        }
+        return s;
+    }
+
+    /**
      * Register phone to CallManager
      * @param phone
-     * @return
+     * @return true if register successfully
      */
     public boolean registerPhone(Phone phone) {
         if (phone != null && !mPhones.contains(phone)) {
@@ -159,13 +223,45 @@
     }
 
     private void registerForPhoneStates(Phone phone) {
-        phone.registerForPreciseCallStateChanged(mHandler, EVENT_CALL_STATE_CHANGED, null);
+        phone.registerForPreciseCallStateChanged(mHandler, EVENT_PRECISE_CALL_STATE_CHANGED, null);
         phone.registerForDisconnect(mHandler, EVENT_DISCONNECT, null);
+        phone.registerForNewRingingConnection(mHandler, EVENT_NEW_RINGING_CONNECTION, null);
+        phone.registerForUnknownConnection(mHandler, EVENT_UNKNOWN_CONNECTION, null);
+        phone.registerForIncomingRing(mHandler, EVENT_INCOMING_RING, null);
+        phone.registerForRingbackTone(mHandler, EVENT_RINGBACK_TONE, null);
+        phone.registerForInCallVoicePrivacyOn(mHandler, EVENT_IN_CALL_VOICE_PRIVACY_ON, null);
+        phone.registerForInCallVoicePrivacyOff(mHandler, EVENT_IN_CALL_VOICE_PRIVACY_OFF, null);
+        phone.registerForCallWaiting(mHandler, EVENT_CALL_WAITING, null);
+        phone.registerForDisplayInfo(mHandler, EVENT_DISPLAY_INFO, null);
+        phone.registerForSignalInfo(mHandler, EVENT_SIGNAL_INFO, null);
+        phone.registerForCdmaOtaStatusChange(mHandler, EVENT_CDMA_OTA_STATUS_CHANGE, null);
+        phone.registerForResendIncallMute(mHandler, EVENT_RESEND_INCALL_MUTE, null);
+        phone.registerForMmiInitiate(mHandler, EVENT_MMI_INITIATE, null);
+        phone.registerForMmiComplete(mHandler, EVENT_MMI_COMPLETE, null);
+        phone.registerForEcmTimerReset(mHandler, EVENT_ECM_TIMER_RESET, null);
+        phone.registerForSubscriptionInfoReady(mHandler, EVENT_SUBSCRIPTION_INFO_READY, null);
+        phone.registerForSuppServiceFailed(mHandler, EVENT_SUPP_SERVICE_FAILED, null);
     }
 
     private void unregisterForPhoneStates(Phone phone) {
         phone.unregisterForPreciseCallStateChanged(mHandler);
         phone.unregisterForDisconnect(mHandler);
+        phone.unregisterForNewRingingConnection(mHandler);
+        phone.unregisterForUnknownConnection(mHandler);
+        phone.unregisterForIncomingRing(mHandler);
+        phone.unregisterForRingbackTone(mHandler);
+        phone.unregisterForInCallVoicePrivacyOn(mHandler);
+        phone.unregisterForInCallVoicePrivacyOff(mHandler);
+        phone.unregisterForCallWaiting(mHandler);
+        phone.unregisterForDisplayInfo(mHandler);
+        phone.unregisterForSignalInfo(mHandler);
+        phone.unregisterForCdmaOtaStatusChange(mHandler);
+        phone.unregisterForResendIncallMute(mHandler);
+        phone.unregisterForMmiInitiate(mHandler);
+        phone.unregisterForMmiComplete(mHandler);
+        phone.unregisterForEcmTimerReset(mHandler);
+        phone.unregisterForSubscriptionInfoReady(mHandler);
+        phone.unregisterForSuppServiceFailed(mHandler);
     }
 
     /**
@@ -523,12 +619,16 @@
      * Notifies when a previously untracked non-ringing/waiting connection has appeared.
      * This is likely due to some other entity (eg, SIM card application) initiating a call.
      */
-    public void registerForUnknownConnection(Handler h, int what, Object obj){}
+    public void registerForUnknownConnection(Handler h, int what, Object obj){
+        mUnknownConnectionRegistrants.addUnique(h, what, obj);
+    }
 
     /**
      * Unregisters for unknown connection notifications.
      */
-    public void unregisterForUnknownConnection(Handler h){}
+    public void unregisterForUnknownConnection(Handler h){
+        mUnknownConnectionRegistrants.remove(h);
+    }
 
 
     /**
@@ -543,14 +643,18 @@
      *  If Connection.isRinging() is true, then
      *   Connection.getCall() == Phone.getRingingCall()
      */
-    public void registerForNewRingingConnection(Handler h, int what, Object obj){}
+    public void registerForNewRingingConnection(Handler h, int what, Object obj){
+        mNewRingingConnectionRegistrants.addUnique(h, what, obj);
+    }
 
     /**
      * Unregisters for new ringing connection notification.
      * Extraneous calls are tolerated silently
      */
 
-    public void unregisterForNewRingingConnection(Handler h){}
+    public void unregisterForNewRingingConnection(Handler h){
+        mNewRingingConnectionRegistrants.remove(h);
+    }
 
     /**
      * Notifies when an incoming call rings.<p>
@@ -560,14 +664,18 @@
      *  AsyncResult.userObj = obj
      *  AsyncResult.result = a Connection. <p>
      */
-    public void registerForIncomingRing(Handler h, int what, Object obj){}
+    public void registerForIncomingRing(Handler h, int what, Object obj){
+        mIncomingRingRegistrants.addUnique(h, what, obj);
+    }
 
     /**
      * Unregisters for ring notification.
      * Extraneous calls are tolerated silently
      */
 
-    public void unregisterForIncomingRing(Handler h){}
+    public void unregisterForIncomingRing(Handler h){
+        mIncomingRingRegistrants.remove(h);
+    }
 
     /**
      * Notifies when out-band ringback tone is needed.<p>
@@ -578,26 +686,32 @@
      *  AsyncResult.result = boolean, true to start play ringback tone
      *                       and false to stop. <p>
      */
-    public void registerForRingbackTone(Handler h, int what, Object obj){}
+    public void registerForRingbackTone(Handler h, int what, Object obj){
+        mRingbackToneRegistrants.addUnique(h, what, obj);
+    }
 
     /**
      * Unregisters for ringback tone notification.
      */
 
-    public void unregisterForRingbackTone(Handler h){}
+    public void unregisterForRingbackTone(Handler h){
+        mRingbackToneRegistrants.remove(h);
+    }
 
     /**
      * Registers the handler to reset the uplink mute state to get
      * uplink audio.
      */
-    public void registerForResendIncallMute(Handler h, int what, Object obj){}
+    public void registerForResendIncallMute(Handler h, int what, Object obj){
+        mResendIncallMuteRegistrants.addUnique(h, what, obj);
+    }
 
     /**
      * Unregisters for resend incall mute notifications.
      */
-    public void unregisterForResendIncallMute(Handler h){}
-
-
+    public void unregisterForResendIncallMute(Handler h){
+        mResendIncallMuteRegistrants.remove(h);
+    }
 
     /**
      * Register for notifications of initiation of a new MMI code request.
@@ -610,13 +724,17 @@
      *
      * <code>obj.result</code> will be an "MmiCode" object.
      */
-    public void registerForMmiInitiate(Handler h, int what, Object obj){}
+    public void registerForMmiInitiate(Handler h, int what, Object obj){
+        mMmiInitiateRegistrants.addUnique(h, what, obj);
+    }
 
     /**
      * Unregisters for new MMI initiate notification.
      * Extraneous calls are tolerated silently
      */
-    public void unregisterForMmiInitiate(Handler h){}
+    public void unregisterForMmiInitiate(Handler h){
+        mMmiInitiateRegistrants.remove(h);
+    }
 
     /**
      * Register for notifications that an MMI request has completed
@@ -626,13 +744,17 @@
      * <code>Message.obj</code> will contain an AsyncResult.
      * <code>obj.result</code> will be an "MmiCode" object
      */
-    public void registerForMmiComplete(Handler h, int what, Object obj){}
+    public void registerForMmiComplete(Handler h, int what, Object obj){
+        mMmiCompleteRegistrants.addUnique(h, what, obj);
+    }
 
     /**
      * Unregisters for MMI complete notification.
      * Extraneous calls are tolerated silently
      */
-    public void unregisterForMmiComplete(Handler h){}
+    public void unregisterForMmiComplete(Handler h){
+        mMmiCompleteRegistrants.remove(h);
+    }
 
     /**
      * Registration point for Ecm timer reset
@@ -640,15 +762,17 @@
      * @param what user-defined message code
      * @param obj placed in Message.obj
      */
-    public void registerForEcmTimerReset(Handler h, int what, Object obj){}
+    public void registerForEcmTimerReset(Handler h, int what, Object obj){
+        mEcmTimerResetRegistrants.addUnique(h, what, obj);
+    }
 
     /**
      * Unregister for notification for Ecm timer reset
      * @param h Handler to be removed from the registrant list.
      */
-    public void unregisterForEcmTimerReset(Handler h){}
-
-
+    public void unregisterForEcmTimerReset(Handler h){
+        mEcmTimerResetRegistrants.remove(h);
+    }
 
     /**
      * Register for ServiceState changed.
@@ -664,25 +788,6 @@
     public void unregisterForServiceStateChanged(Handler h){}
 
     /**
-     * Register for Supplementary Service notifications from the network.
-     * Message.obj will contain an AsyncResult.
-     * AsyncResult.result will be a SuppServiceNotification instance.
-     *
-     * @param h Handler that receives the notification message.
-     * @param what User-defined message code.
-     * @param obj User object.
-     */
-    public void registerForSuppServiceNotification(Handler h, int what, Object obj){}
-
-    /**
-     * Unregisters for Supplementary Service notifications.
-     * Extraneous calls are tolerated silently
-     *
-     * @param h Handler to be removed from the registrant list.
-     */
-    public void unregisterForSuppServiceNotification(Handler h){}
-
-    /**
      * Register for notifications when a supplementary service attempt fails.
      * Message.obj will contain an AsyncResult.
      *
@@ -690,7 +795,9 @@
      * @param what User-defined message code.
      * @param obj User object.
      */
-    public void registerForSuppServiceFailed(Handler h, int what, Object obj){}
+    public void registerForSuppServiceFailed(Handler h, int what, Object obj){
+        mSuppServiceFailedRegistrants.addUnique(h, what, obj);
+    }
 
     /**
      * Unregister for notifications when a supplementary service attempt fails.
@@ -698,7 +805,9 @@
      *
      * @param h Handler to be removed from the registrant list.
      */
-    public void unregisterForSuppServiceFailed(Handler h){}
+    public void unregisterForSuppServiceFailed(Handler h){
+        mSuppServiceFailedRegistrants.remove(h);
+    }
 
     /**
      * Register for notifications when a sInCall VoicePrivacy is enabled
@@ -707,14 +816,18 @@
      * @param what User-defined message code.
      * @param obj User object.
      */
-    public void registerForInCallVoicePrivacyOn(Handler h, int what, Object obj){}
+    public void registerForInCallVoicePrivacyOn(Handler h, int what, Object obj){
+        mInCallVoicePrivacyOnRegistrants.addUnique(h, what, obj);
+    }
 
     /**
-     * Unegister for notifications when a sInCall VoicePrivacy is enabled
+     * Unregister for notifications when a sInCall VoicePrivacy is enabled
      *
      * @param h Handler to be removed from the registrant list.
      */
-    public void unregisterForInCallVoicePrivacyOn(Handler h){}
+    public void unregisterForInCallVoicePrivacyOn(Handler h){
+        mInCallVoicePrivacyOnRegistrants.remove(h);
+    }
 
     /**
      * Register for notifications when a sInCall VoicePrivacy is disabled
@@ -723,14 +836,85 @@
      * @param what User-defined message code.
      * @param obj User object.
      */
-    public void registerForInCallVoicePrivacyOff(Handler h, int what, Object obj){}
+    public void registerForInCallVoicePrivacyOff(Handler h, int what, Object obj){
+        mInCallVoicePrivacyOffRegistrants.addUnique(h, what, obj);
+    }
 
     /**
-     * Unegister for notifications when a sInCall VoicePrivacy is disabled
+     * Unregister for notifications when a sInCall VoicePrivacy is disabled
      *
      * @param h Handler to be removed from the registrant list.
      */
-    public void unregisterForInCallVoicePrivacyOff(Handler h){}
+    public void unregisterForInCallVoicePrivacyOff(Handler h){
+        mInCallVoicePrivacyOffRegistrants.remove(h);
+    }
+
+    /**
+     * Register for notifications when CDMA call waiting comes
+     *
+     * @param h Handler that receives the notification message.
+     * @param what User-defined message code.
+     * @param obj User object.
+     */
+    public void registerForCallWaiting(Handler h, int what, Object obj){
+        mCallWaitingRegistrants.addUnique(h, what, obj);
+    }
+
+    /**
+     * Unregister for notifications when CDMA Call waiting comes
+     * @param h Handler to be removed from the registrant list.
+     */
+    public void unregisterForCallWaiting(Handler h){
+        mCallWaitingRegistrants.remove(h);
+    }
+
+
+    /**
+     * Register for signal information notifications from the network.
+     * Message.obj will contain an AsyncResult.
+     * AsyncResult.result will be a SuppServiceNotification instance.
+     *
+     * @param h Handler that receives the notification message.
+     * @param what User-defined message code.
+     * @param obj User object.
+     */
+
+    public void registerForSignalInfo(Handler h, int what, Object obj){
+        mSignalInfoRegistrants.addUnique(h, what, obj);
+    }
+
+    /**
+     * Unregisters for signal information notifications.
+     * Extraneous calls are tolerated silently
+     *
+     * @param h Handler to be removed from the registrant list.
+     */
+    public void unregisterForSignalInfo(Handler h){
+        mSignalInfoRegistrants.remove(h);
+    }
+
+    /**
+     * Register for display information notifications from the network.
+     * Message.obj will contain an AsyncResult.
+     * AsyncResult.result will be a SuppServiceNotification instance.
+     *
+     * @param h Handler that receives the notification message.
+     * @param what User-defined message code.
+     * @param obj User object.
+     */
+    public void registerForDisplayInfo(Handler h, int what, Object obj){
+        mDisplayInfoRegistrants.addUnique(h, what, obj);
+    }
+
+    /**
+     * Unregisters for display information notifications.
+     * Extraneous calls are tolerated silently
+     *
+     * @param h Handler to be removed from the registrant list.
+     */
+    public void unregisterForDisplayInfo(Handler h) {
+        mDisplayInfoRegistrants.remove(h);
+    }
 
     /**
      * Register for notifications when CDMA OTA Provision status change
@@ -739,13 +923,17 @@
      * @param what User-defined message code.
      * @param obj User object.
      */
-    public void registerForCdmaOtaStatusChange(Handler h, int what, Object obj){}
+    public void registerForCdmaOtaStatusChange(Handler h, int what, Object obj){
+        mCdmaOtaStatusChangeRegistrants.addUnique(h, what, obj);
+    }
 
     /**
-     * Unegister for notifications when CDMA OTA Provision status change
+     * Unregister for notifications when CDMA OTA Provision status change
      * @param h Handler to be removed from the registrant list.
      */
-    public void unregisterForCdmaOtaStatusChange(Handler h){}
+    public void unregisterForCdmaOtaStatusChange(Handler h){
+        mCdmaOtaStatusChangeRegistrants.remove(h);
+    }
 
     /**
      * Registration point for subscription info ready
@@ -753,13 +941,17 @@
      * @param what what code of message when delivered
      * @param obj placed in Message.obj
      */
-    public void registerForSubscriptionInfoReady(Handler h, int what, Object obj){}
+    public void registerForSubscriptionInfoReady(Handler h, int what, Object obj){
+        mSubscriptionInfoReadyRegistrants.addUnique(h, what, obj);
+    }
 
     /**
      * Unregister for notifications for subscription info
      * @param h Handler to be removed from the registrant list.
      */
-    public void unregisterForSubscriptionInfoReady(Handler h){}
+    public void unregisterForSubscriptionInfoReady(Handler h){
+        mSubscriptionInfoReadyRegistrants.remove(h);
+    }
 
     /* APIs to access foregroudCalls, backgroudCalls, and ringingCalls
      * 1. APIs to access list of calls
@@ -974,9 +1166,57 @@
                 case EVENT_DISCONNECT:
                     mDisconnectRegistrants.notifyRegistrants((AsyncResult) msg.obj);
                     break;
-                case EVENT_CALL_STATE_CHANGED:
+                case EVENT_PRECISE_CALL_STATE_CHANGED:
                     mPreciseCallStateRegistrants.notifyRegistrants((AsyncResult) msg.obj);
                     break;
+                case EVENT_NEW_RINGING_CONNECTION:
+                    mNewRingingConnectionRegistrants.notifyRegistrants((AsyncResult) msg.obj);
+                    break;
+                case EVENT_UNKNOWN_CONNECTION:
+                    mUnknownConnectionRegistrants.notifyRegistrants((AsyncResult) msg.obj);
+                    break;
+                case EVENT_INCOMING_RING:
+                    mIncomingRingRegistrants.notifyRegistrants((AsyncResult) msg.obj);
+                    break;
+                case EVENT_RINGBACK_TONE:
+                    mRingbackToneRegistrants.notifyRegistrants((AsyncResult) msg.obj);
+                    break;
+                case EVENT_IN_CALL_VOICE_PRIVACY_ON:
+                    mInCallVoicePrivacyOnRegistrants.notifyRegistrants((AsyncResult) msg.obj);
+                    break;
+                case EVENT_IN_CALL_VOICE_PRIVACY_OFF:
+                    mInCallVoicePrivacyOffRegistrants.notifyRegistrants((AsyncResult) msg.obj);
+                    break;
+                case EVENT_CALL_WAITING:
+                    mCallWaitingRegistrants.notifyRegistrants((AsyncResult) msg.obj);
+                    break;
+                case EVENT_DISPLAY_INFO:
+                    mDisplayInfoRegistrants.notifyRegistrants((AsyncResult) msg.obj);
+                    break;
+                case EVENT_SIGNAL_INFO:
+                    mSignalInfoRegistrants.notifyRegistrants((AsyncResult) msg.obj);
+                    break;
+                case EVENT_CDMA_OTA_STATUS_CHANGE:
+                    mCdmaOtaStatusChangeRegistrants.notifyRegistrants((AsyncResult) msg.obj);
+                    break;
+                case EVENT_RESEND_INCALL_MUTE:
+                    mResendIncallMuteRegistrants.notifyRegistrants((AsyncResult) msg.obj);
+                    break;
+                case EVENT_MMI_INITIATE:
+                    mMmiInitiateRegistrants.notifyRegistrants((AsyncResult) msg.obj);
+                    break;
+                case EVENT_MMI_COMPLETE:
+                    mMmiCompleteRegistrants.notifyRegistrants((AsyncResult) msg.obj);
+                    break;
+                case EVENT_ECM_TIMER_RESET:
+                    mEcmTimerResetRegistrants.notifyRegistrants((AsyncResult) msg.obj);
+                    break;
+                case EVENT_SUBSCRIPTION_INFO_READY:
+                    mSubscriptionInfoReadyRegistrants.notifyRegistrants((AsyncResult) msg.obj);
+                    break;
+                case EVENT_SUPP_SERVICE_FAILED:
+                    mSuppServiceFailedRegistrants.notifyRegistrants((AsyncResult) msg.obj);
+                    break;
             }
         }
     };
diff --git a/telephony/java/com/android/internal/telephony/cdma/CdmaCall.java b/telephony/java/com/android/internal/telephony/cdma/CdmaCall.java
index c3bb01f..4ad61bb 100644
--- a/telephony/java/com/android/internal/telephony/cdma/CdmaCall.java
+++ b/telephony/java/com/android/internal/telephony/cdma/CdmaCall.java
@@ -75,8 +75,7 @@
 
     public Phone
     getPhone() {
-        //TODO, see GsmCall
-        return null;
+        return owner.phone;
     }
 
     public boolean isMultiparty() {
diff --git a/telephony/java/com/android/internal/telephony/gsm/GsmCall.java b/telephony/java/com/android/internal/telephony/gsm/GsmCall.java
index 9542d20..58124a2 100644
--- a/telephony/java/com/android/internal/telephony/gsm/GsmCall.java
+++ b/telephony/java/com/android/internal/telephony/gsm/GsmCall.java
@@ -70,8 +70,7 @@
 
     public Phone
     getPhone() {
-        //TODO
-        return null;
+        return owner.phone;
     }
 
     public boolean
diff --git a/telephony/tests/telephonytests/Android.mk b/telephony/tests/telephonytests/Android.mk
index 45e265a..98e4403 100644
--- a/telephony/tests/telephonytests/Android.mk
+++ b/telephony/tests/telephonytests/Android.mk
@@ -5,6 +5,8 @@
 
 LOCAL_SRC_FILES := $(call all-subdir-java-files)
 
+LOCAL_STATIC_JAVA_LIBRARIES := librilproto-java
+
 LOCAL_JAVA_LIBRARIES := android.test.runner
 
 LOCAL_PACKAGE_NAME := FrameworksTelephonyTests
diff --git a/telephony/tests/telephonytests/AndroidManifest.xml b/telephony/tests/telephonytests/AndroidManifest.xml
index 6a97423..ba1d957 100644
--- a/telephony/tests/telephonytests/AndroidManifest.xml
+++ b/telephony/tests/telephonytests/AndroidManifest.xml
@@ -32,6 +32,13 @@
         android:targetPackage="com.android.frameworks.telephonytests"
         android:label="Frameworks Telephony Tests">
     </instrumentation>
+
+    <instrumentation android:name=".TelephonyMockRilTestRunner"
+        android:targetPackage="com.android.frameworks.telephonytests"
+        android:label="Test Runner for Mock Ril Tests"
+    />
+
     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
+    <uses-permission android:name="android.permission.INTERNET" />
 
 </manifest>
diff --git a/telephony/tests/telephonytests/src/com/android/frameworks/telephonytests/TelephonyMockRilTestRunner.java b/telephony/tests/telephonytests/src/com/android/frameworks/telephonytests/TelephonyMockRilTestRunner.java
new file mode 100644
index 0000000..9192f57
--- /dev/null
+++ b/telephony/tests/telephonytests/src/com/android/frameworks/telephonytests/TelephonyMockRilTestRunner.java
@@ -0,0 +1,93 @@
+/*
+ * 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.frameworks.telephonytests;
+
+import android.os.Bundle;
+
+import android.test.InstrumentationTestRunner;
+import android.test.InstrumentationTestSuite;
+import android.util.Log;
+
+import java.io.IOException;
+
+import com.android.internal.telephony.RilChannel;
+import com.android.internal.telephony.mockril.MockRilTest;
+
+import junit.framework.TestSuite;
+
+public class TelephonyMockRilTestRunner extends InstrumentationTestRunner {
+
+    public RilChannel mMockRilChannel;
+
+    @Override
+    public TestSuite getAllTests() {
+        log("getAllTests E");
+        TestSuite suite = new InstrumentationTestSuite(this);
+        suite.addTestSuite(MockRilTest.class);
+        log("getAllTests X");
+        return suite;
+    }
+
+    @Override
+    public ClassLoader getLoader() {
+        log("getLoader EX");
+        return TelephonyMockRilTestRunner.class.getClassLoader();
+    }
+
+    @Override
+    public void onCreate(Bundle icicle) {
+        log("onCreate E");
+        try {
+            mMockRilChannel = RilChannel.makeRilChannel();
+        } catch (IOException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        log("onCreate X");
+
+        super.onCreate(icicle);
+    }
+
+    @Override
+    public void onDestroy() {
+        // I've not seen this called
+        log("onDestroy EX");
+        super.onDestroy();
+    }
+
+    @Override
+    public void onStart() {
+        // Called when the instrumentation thread is started.
+        // At the moment we don't need the thread so return
+        // which will shut down this unused thread.
+        log("onStart EX");
+        super.onStart();
+    }
+
+    @Override
+    public void finish(int resultCode, Bundle results) {
+        // Called when complete so I ask the mMockRilChannel to quit.
+        log("finish E");
+        mMockRilChannel.close();
+        log("finish X");
+        super.finish(resultCode, results);
+    }
+
+    private void log(String s) {
+        Log.e("TelephonyMockRilTestRunner", s);
+    }
+}
diff --git a/telephony/tests/telephonytests/src/com/android/internal/telephony/mockril/MockRilTest.java b/telephony/tests/telephonytests/src/com/android/internal/telephony/mockril/MockRilTest.java
new file mode 100644
index 0000000..ee03b7c
--- /dev/null
+++ b/telephony/tests/telephonytests/src/com/android/internal/telephony/mockril/MockRilTest.java
@@ -0,0 +1,176 @@
+/*
+ * 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.internal.telephony.mockril;
+
+import android.util.Log;
+import android.test.InstrumentationTestCase;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import com.android.internal.communication.MsgHeader;
+import com.android.internal.communication.Msg;
+import com.android.internal.telephony.RilChannel;
+import com.android.internal.telephony.ril_proto.RilCtrlCmds;
+
+import com.android.frameworks.telephonytests.TelephonyMockRilTestRunner;
+import com.google.protobuf.micro.InvalidProtocolBufferMicroException;
+
+// Test suite for test ril
+public class MockRilTest extends InstrumentationTestCase {
+    private static final String TAG = "MockRilTest";
+
+    RilChannel mMockRilChannel;
+    TelephonyMockRilTestRunner mRunner;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mRunner = (TelephonyMockRilTestRunner)getInstrumentation();
+        mMockRilChannel = mRunner.mMockRilChannel;
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    static void log(String s) {
+        Log.v(TAG, s);
+    }
+
+    /**
+     * Test protobuf serialization and deserialization
+     * @throws InvalidProtocolBufferMicroException
+     */
+    public void testProtobufSerDes() throws InvalidProtocolBufferMicroException {
+        log("testProtobufSerdes E");
+
+        RilCtrlCmds.CtrlRspRadioState rs = new RilCtrlCmds.CtrlRspRadioState();
+        assertTrue(String.format("expected rs.state == 0 was %d", rs.getState()),
+                rs.getState() == 0);
+        rs.setState(1);
+        assertTrue(String.format("expected rs.state == 1 was %d", rs.getState()),
+                rs.getState() == 1);
+
+        byte[] rs_ser = rs.toByteArray();
+        RilCtrlCmds.CtrlRspRadioState rsNew = RilCtrlCmds.CtrlRspRadioState.parseFrom(rs_ser);
+        assertTrue(String.format("expected rsNew.state == 1 was %d", rs.getState()),
+                rs.getState() == 1);
+
+        log("testProtobufSerdes X");
+    }
+
+    /**
+     * Test echo command works using writeMsg & readMsg
+     */
+    public void testEchoMsg() throws IOException {
+        log("testEchoMsg E");
+
+        MsgHeader mh = new MsgHeader();
+        mh.setCmd(0);
+        mh.setToken(1);
+        mh.setStatus(2);
+        ByteBuffer data = ByteBuffer.allocate(3);
+        data.put((byte)3);
+        data.put((byte)4);
+        data.put((byte)5);
+        Msg.send(mMockRilChannel, mh, data);
+
+        Msg respMsg = Msg.recv(mMockRilChannel);
+        assertTrue(String.format("expected mhd.header.cmd == 0 was %d",respMsg.getCmd()),
+                respMsg.getCmd() == 0);
+        assertTrue(String.format("expected mhd.header.token == 1 was %d",respMsg.getToken()),
+                respMsg.getToken() == 1);
+        assertTrue(String.format("expected mhd.header.status == 2 was %d", respMsg.getStatus()),
+                respMsg.getStatus() == 2);
+        assertTrue(String.format("expected mhd.data[0] == 3 was %d", respMsg.getData(0)),
+                respMsg.getData(0) == 3);
+        assertTrue(String.format("expected mhd.data[1] == 4 was %d", respMsg.getData(1)),
+                respMsg.getData(1) == 4);
+        assertTrue(String.format("expected mhd.data[2] == 5 was %d", respMsg.getData(2)),
+                respMsg.getData(2) == 5);
+
+        log("testEchoMsg X");
+    }
+
+    /**
+     * Test get as
+     */
+    public void testGetAs() {
+        log("testGetAs E");
+
+        // Use a message header as the protobuf data content
+        MsgHeader mh = new MsgHeader();
+        mh.setCmd(12345);
+        mh.setToken(9876);
+        mh.setStatus(7654);
+        mh.setLengthData(4321);
+        byte[] data = mh.toByteArray();
+        MsgHeader mhResult = Msg.getAs(MsgHeader.class, data);
+
+        assertTrue(String.format("expected cmd == 12345 was %d", mhResult.getCmd()),
+                mhResult.getCmd() == 12345);
+        assertTrue(String.format("expected token == 9876 was %d", mhResult.getToken()),
+                mhResult.getToken() == 9876);
+        assertTrue(String.format("expected status == 7654 was %d", mhResult.getStatus()),
+                mhResult.getStatus() == 7654);
+        assertTrue(String.format("expected lengthData == 4321 was %d", mhResult.getLengthData()),
+                mhResult.getLengthData() == 4321);
+
+        Msg msg = Msg.obtain();
+        msg.setData(ByteBuffer.wrap(data));
+
+        mhResult = msg.getDataAs(MsgHeader.class);
+
+        assertTrue(String.format("expected cmd == 12345 was %d", mhResult.getCmd()),
+                mhResult.getCmd() == 12345);
+        assertTrue(String.format("expected token == 9876 was %d", mhResult.getToken()),
+                mhResult.getToken() == 9876);
+        assertTrue(String.format("expected status == 7654 was %d", mhResult.getStatus()),
+                mhResult.getStatus() == 7654);
+        assertTrue(String.format("expected lengthData == 4321 was %d", mhResult.getLengthData()),
+                mhResult.getLengthData() == 4321);
+
+        log("testGetAs X");
+    }
+
+    public void testGetRadioState() throws IOException {
+        log("testGetRadioState E");
+
+        Msg.send(mMockRilChannel, 1, 9876, 0, null);
+
+        Msg resp = Msg.recv(mMockRilChannel);
+        //resp.printHeader("testGetRadioState");
+
+        assertTrue(String.format("expected cmd == 1 was %d", resp.getCmd()),
+                resp.getCmd() == 1);
+        assertTrue(String.format("expected token == 9876 was %d", resp.getToken()),
+                resp.getToken() == 9876);
+        assertTrue(String.format("expected status == 0 was %d", resp.getStatus()),
+                resp.getStatus() == 0);
+
+        RilCtrlCmds.CtrlRspRadioState rsp = resp.getDataAs(RilCtrlCmds.CtrlRspRadioState.class);
+
+        int state = rsp.getState();
+        log("testGetRadioState state=" + state);
+        assertTrue(String.format("expected RadioState >= 0 && RadioState <= 9 was %d", state),
+                ((state >= 0) && (state <= 9)));
+
+        log("testGetRadioState X");
+    }
+}
diff --git a/tests/DumpRenderTree/src/com/android/dumprendertree/CallbackProxy.java b/tests/DumpRenderTree/src/com/android/dumprendertree/CallbackProxy.java
index a5870f8..b62db51 100644
--- a/tests/DumpRenderTree/src/com/android/dumprendertree/CallbackProxy.java
+++ b/tests/DumpRenderTree/src/com/android/dumprendertree/CallbackProxy.java
@@ -177,7 +177,7 @@
             break;
 
         case LAYOUT_DUMP_TEXT:
-            mLayoutTestController.dumpAsText();
+            mLayoutTestController.dumpAsText(msg.arg1 == 1);
             break;
 
         case LAYOUT_DUMP_CHILD_FRAMES_TEXT:
@@ -387,7 +387,11 @@
     }
 
     public void dumpAsText() {
-        obtainMessage(LAYOUT_DUMP_TEXT).sendToTarget();
+        obtainMessage(LAYOUT_DUMP_TEXT, 0).sendToTarget();
+    }
+
+    public void dumpAsText(boolean enablePixelTests) {
+        obtainMessage(LAYOUT_DUMP_TEXT, enablePixelTests ? 1 : 0).sendToTarget();
     }
 
     public void dumpChildFramesAsText() {
diff --git a/tests/DumpRenderTree/src/com/android/dumprendertree/FileFilter.java b/tests/DumpRenderTree/src/com/android/dumprendertree/FileFilter.java
index 8bdf77c..939c623 100644
--- a/tests/DumpRenderTree/src/com/android/dumprendertree/FileFilter.java
+++ b/tests/DumpRenderTree/src/com/android/dumprendertree/FileFilter.java
@@ -110,6 +110,23 @@
         ignoreResultList.add("storage/private-browsing-readonly.html"); // private browsing not supported
         ignoreResultList.add("websocket/tests/workers"); // workers not supported
 
+        // Expected failures due to missing expected results
+        ignoreResultList.add("dom/xhtml/level3/core/canonicalform08.xhtml");
+        ignoreResultList.add("dom/xhtml/level3/core/canonicalform09.xhtml");
+        ignoreResultList.add("dom/xhtml/level3/core/documentgetinputencoding03.xhtml");
+        ignoreResultList.add("dom/xhtml/level3/core/entitygetinputencoding02.xhtml");
+        ignoreResultList.add("dom/xhtml/level3/core/entitygetxmlversion02.xhtml");
+        ignoreResultList.add("dom/xhtml/level3/core/nodegetbaseuri05.xhtml");
+        ignoreResultList.add("dom/xhtml/level3/core/nodegetbaseuri07.xhtml");
+        ignoreResultList.add("dom/xhtml/level3/core/nodegetbaseuri09.xhtml");
+        ignoreResultList.add("dom/xhtml/level3/core/nodegetbaseuri10.xhtml");
+        ignoreResultList.add("dom/xhtml/level3/core/nodegetbaseuri11.xhtml");
+        ignoreResultList.add("dom/xhtml/level3/core/nodegetbaseuri15.xhtml");
+        ignoreResultList.add("dom/xhtml/level3/core/nodegetbaseuri17.xhtml");
+        ignoreResultList.add("dom/xhtml/level3/core/nodegetbaseuri18.xhtml");
+        ignoreResultList.add("dom/xhtml/level3/core/nodelookupnamespaceuri01.xhtml");
+        ignoreResultList.add("dom/xhtml/level3/core/nodelookupprefix19.xhtml");
+
         // TODO: These need to be triaged
         ignoreResultList.add("fast/css/case-transform.html"); // will not fix #619707
         ignoreResultList.add("fast/dom/Element/offsetLeft-offsetTop-body-quirk.html"); // different screen size result in extra spaces in Apple compared to us
diff --git a/tests/DumpRenderTree/src/com/android/dumprendertree/LayoutTestController.java b/tests/DumpRenderTree/src/com/android/dumprendertree/LayoutTestController.java
index 83460bd..15288b5 100644
--- a/tests/DumpRenderTree/src/com/android/dumprendertree/LayoutTestController.java
+++ b/tests/DumpRenderTree/src/com/android/dumprendertree/LayoutTestController.java
@@ -18,30 +18,30 @@
 
 public interface LayoutTestController {
 
-	public void dumpAsText();
-	public void dumpChildFramesAsText();
-	public void waitUntilDone();
-	public void notifyDone();
-	
-	// Force a redraw of the page
-	public void display();
-	// Used with pixel dumps of content
-	public void testRepaint();
-	
-	// If the page title changes, add the information to the output.
-	public void dumpTitleChanges();
-	public void dumpBackForwardList();
-	public void dumpChildFrameScrollPositions();
-	public void dumpEditingCallbacks();
-	
-	// Show/Hide window for window.onBlur() testing
-	public void setWindowIsKey(boolean b);
-	// Mac function, used to disable events going to the window
-	public void setMainFrameIsFirstResponder(boolean b);
-	
-	public void dumpSelectionRect();
-	
-	// invalidate and draw one line at a time of the web view.
+    public void dumpAsText(boolean enablePixelTests);
+    public void dumpChildFramesAsText();
+    public void waitUntilDone();
+    public void notifyDone();
+
+    // Force a redraw of the page
+    public void display();
+    // Used with pixel dumps of content
+    public void testRepaint();
+
+    // If the page title changes, add the information to the output.
+    public void dumpTitleChanges();
+    public void dumpBackForwardList();
+    public void dumpChildFrameScrollPositions();
+    public void dumpEditingCallbacks();
+
+    // Show/Hide window for window.onBlur() testing
+    public void setWindowIsKey(boolean b);
+    // Mac function, used to disable events going to the window
+    public void setMainFrameIsFirstResponder(boolean b);
+
+    public void dumpSelectionRect();
+
+    // invalidate and draw one line at a time of the web view.
     public void repaintSweepHorizontally();
     
     // History testing functions
diff --git a/tests/DumpRenderTree/src/com/android/dumprendertree/TestShellActivity.java b/tests/DumpRenderTree/src/com/android/dumprendertree/TestShellActivity.java
index bf66fae..ef96813 100644
--- a/tests/DumpRenderTree/src/com/android/dumprendertree/TestShellActivity.java
+++ b/tests/DumpRenderTree/src/com/android/dumprendertree/TestShellActivity.java
@@ -342,7 +342,12 @@
 
     // .......................................
     // LayoutTestController Functions
-    public void dumpAsText() {
+    public void dumpAsText(boolean enablePixelTests) {
+        // Added after webkit update to r63859. See trac.webkit.org/changeset/63730.
+        if (enablePixelTests) {
+            Log.v(LOGTAG, "dumpAsText(enablePixelTests == true) not implemented on Android!");
+        }
+
         mDumpDataType = DumpDataType.DUMP_AS_TEXT;
         mDumpTopFrameAsText = true;
         if (mWebView != null) {