Merge "Fix an artifact of going from off_t to off64_t"
diff --git a/core/java/android/content/res/StringBlock.java b/core/java/android/content/res/StringBlock.java
index 5e90b91..23a6f97 100644
--- a/core/java/android/content/res/StringBlock.java
+++ b/core/java/android/content/res/StringBlock.java
@@ -87,21 +87,48 @@
             if (style != null) {
                 if (mStyleIDs == null) {
                     mStyleIDs = new StyleIDs();
-                    mStyleIDs.boldId = nativeIndexOfString(mNative, "b");
-                    mStyleIDs.italicId = nativeIndexOfString(mNative, "i");
-                    mStyleIDs.underlineId = nativeIndexOfString(mNative, "u");
-                    mStyleIDs.ttId = nativeIndexOfString(mNative, "tt");
-                    mStyleIDs.bigId = nativeIndexOfString(mNative, "big");
-                    mStyleIDs.smallId = nativeIndexOfString(mNative, "small");
-                    mStyleIDs.supId = nativeIndexOfString(mNative, "sup");
-                    mStyleIDs.subId = nativeIndexOfString(mNative, "sub");
-                    mStyleIDs.strikeId = nativeIndexOfString(mNative, "strike");
-                    mStyleIDs.listItemId = nativeIndexOfString(mNative, "li");
-                    mStyleIDs.marqueeId = nativeIndexOfString(mNative, "marquee");
+                }
 
-                    if (localLOGV) Log.v(TAG, "BoldId=" + mStyleIDs.boldId
-                            + ", ItalicId=" + mStyleIDs.italicId
-                            + ", UnderlineId=" + mStyleIDs.underlineId);
+                // the style array is a flat array of <type, start, end> hence
+                // the magic constant 3.
+                for (int styleIndex = 0; styleIndex < style.length; styleIndex += 3) {
+                    int styleId = style[styleIndex];
+
+                    if (styleId == mStyleIDs.boldId || styleId == mStyleIDs.italicId
+                            || styleId == mStyleIDs.underlineId || styleId == mStyleIDs.ttId
+                            || styleId == mStyleIDs.bigId || styleId == mStyleIDs.smallId
+                            || styleId == mStyleIDs.subId || styleId == mStyleIDs.supId
+                            || styleId == mStyleIDs.strikeId || styleId == mStyleIDs.listItemId
+                            || styleId == mStyleIDs.marqueeId) {
+                        // id already found skip to next style
+                        continue;
+                    }
+
+                    String styleTag = nativeGetString(mNative, styleId);
+
+                    if (styleTag.equals("b")) {
+                        mStyleIDs.boldId = styleId;
+                    } else if (styleTag.equals("i")) {
+                        mStyleIDs.italicId = styleId;
+                    } else if (styleTag.equals("u")) {
+                        mStyleIDs.underlineId = styleId;
+                    } else if (styleTag.equals("tt")) {
+                        mStyleIDs.ttId = styleId;
+                    } else if (styleTag.equals("big")) {
+                        mStyleIDs.bigId = styleId;
+                    } else if (styleTag.equals("small")) {
+                        mStyleIDs.smallId = styleId;
+                    } else if (styleTag.equals("sup")) {
+                        mStyleIDs.supId = styleId;
+                    } else if (styleTag.equals("sub")) {
+                        mStyleIDs.subId = styleId;
+                    } else if (styleTag.equals("strike")) {
+                        mStyleIDs.strikeId = styleId;
+                    } else if (styleTag.equals("li")) {
+                        mStyleIDs.listItemId = styleId;
+                    } else if (styleTag.equals("marquee")) {
+                        mStyleIDs.marqueeId = styleId;
+                    }
                 }
 
                 res = applyStyles(str, style, mStyleIDs);
@@ -119,17 +146,17 @@
     }
 
     static final class StyleIDs {
-        private int boldId;
-        private int italicId;
-        private int underlineId;
-        private int ttId;
-        private int bigId;
-        private int smallId;
-        private int subId;
-        private int supId;
-        private int strikeId;
-        private int listItemId;
-        private int marqueeId;
+        private int boldId = -1;
+        private int italicId = -1;
+        private int underlineId = -1;
+        private int ttId = -1;
+        private int bigId = -1;
+        private int smallId = -1;
+        private int subId = -1;
+        private int supId = -1;
+        private int strikeId = -1;
+        private int listItemId = -1;
+        private int marqueeId = -1;
     }
 
     private CharSequence applyStyles(String str, int[] style, StyleIDs ids) {
@@ -403,6 +430,5 @@
     private static final native int nativeGetSize(int obj);
     private static final native String nativeGetString(int obj, int idx);
     private static final native int[] nativeGetStyle(int obj, int idx);
-    private static final native int nativeIndexOfString(int obj, String str);
     private static final native void nativeDestroy(int obj);
 }
diff --git a/core/java/android/widget/DatePicker.java b/core/java/android/widget/DatePicker.java
index a371290..595b487 100644
--- a/core/java/android/widget/DatePicker.java
+++ b/core/java/android/widget/DatePicker.java
@@ -22,6 +22,7 @@
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.text.format.DateFormat;
+import android.text.format.DateUtils;
 import android.util.AttributeSet;
 import android.util.SparseArray;
 import android.view.LayoutInflater;
@@ -33,6 +34,7 @@
 import java.text.DateFormatSymbols;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
+import java.util.Locale;
 
 /**
  * A view for selecting a month / year / day based on a calendar like layout.
@@ -47,7 +49,10 @@
 
     private static final int DEFAULT_START_YEAR = 1900;
     private static final int DEFAULT_END_YEAR = 2100;
-    
+
+    // This ignores Undecimber, but we only support real Gregorian calendars.
+    private static final int NUMBER_OF_MONTHS = 12;
+
     /* UI Components */
     private final NumberPicker mDayPicker;
     private final NumberPicker mMonthPicker;
@@ -62,6 +67,10 @@
     private int mMonth;
     private int mYear;
 
+    private Object mMonthUpdateLock = new Object();
+    private volatile Locale mMonthLocale;
+    private String[] mShortMonths;
+
     /**
      * The callback used to indicate the user changes the date.
      */
@@ -102,8 +111,7 @@
         });
         mMonthPicker = (NumberPicker) findViewById(R.id.month);
         mMonthPicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
-        DateFormatSymbols dfs = new DateFormatSymbols();
-        String[] months = dfs.getShortMonths();
+        final String[] months = getShortMonths();
 
         /*
          * If the user is in a locale where the month names are numeric,
@@ -114,9 +122,9 @@
             for (int i = 0; i < months.length; i++) {
                 months[i] = String.valueOf(i + 1);
             }
-            mMonthPicker.setRange(1, 12);
+            mMonthPicker.setRange(1, NUMBER_OF_MONTHS);
         } else {
-            mMonthPicker.setRange(1, 12, months);
+            mMonthPicker.setRange(1, NUMBER_OF_MONTHS, months);
         }
 
         mMonthPicker.setSpeed(200);
@@ -246,11 +254,30 @@
             mMonth = monthOfYear;
             mDay = dayOfMonth;
             updateSpinners();
-            reorderPickers(new DateFormatSymbols().getShortMonths());
+            reorderPickers(getShortMonths());
             notifyDateChanged();
         }
     }
 
+    private String[] getShortMonths() {
+        final Locale currentLocale = Locale.getDefault();
+        if (currentLocale.equals(mMonthLocale) && mShortMonths != null) {
+            return mShortMonths;
+        } else {
+            synchronized (mMonthUpdateLock) {
+                if (!currentLocale.equals(mMonthLocale)) {
+                    mShortMonths = new String[NUMBER_OF_MONTHS];
+                    for (int i = 0; i < NUMBER_OF_MONTHS; i++) {
+                        mShortMonths[i] = DateUtils.getMonthString(Calendar.JANUARY + i,
+                                DateUtils.LENGTH_MEDIUM);
+                    }
+                    mMonthLocale = currentLocale;
+                }
+            }
+            return mShortMonths;
+        }
+    }
+
     private static class SavedState extends BaseSavedState {
 
         private final int mYear;
diff --git a/core/jni/android_util_StringBlock.cpp b/core/jni/android_util_StringBlock.cpp
index 641fbce..a021efd 100644
--- a/core/jni/android_util_StringBlock.cpp
+++ b/core/jni/android_util_StringBlock.cpp
@@ -147,25 +147,6 @@
     return array;
 }
 
-static jint android_content_StringBlock_nativeIndexOfString(JNIEnv* env, jobject clazz,
-                                                         jint token, jstring str)
-{
-    ResStringPool* osb = (ResStringPool*)token;
-    if (osb == NULL || str == NULL) {
-        doThrow(env, "java/lang/NullPointerException");
-        return 0;
-    }
-
-    const char16_t* str16 = env->GetStringChars(str, NULL);
-    jsize strLen = env->GetStringLength(str);
-
-    ssize_t idx = osb->indexOfString(str16, strLen);
-
-    env->ReleaseStringChars(str, str16);
-
-    return idx;
-}
-
 static void android_content_StringBlock_nativeDestroy(JNIEnv* env, jobject clazz,
                                                    jint token)
 {
@@ -193,8 +174,6 @@
             (void*) android_content_StringBlock_nativeGetString },
     { "nativeGetStyle",    "(II)[I",
             (void*) android_content_StringBlock_nativeGetStyle },
-    { "nativeIndexOfString","(ILjava/lang/String;)I",
-            (void*) android_content_StringBlock_nativeIndexOfString },
     { "nativeDestroy",      "(I)V",
             (void*) android_content_StringBlock_nativeDestroy },
 };
diff --git a/docs/html/guide/guide_toc.cs b/docs/html/guide/guide_toc.cs
index 545807e..3cc1806 100644
--- a/docs/html/guide/guide_toc.cs
+++ b/docs/html/guide/guide_toc.cs
@@ -196,6 +196,9 @@
       </li>
     </ul>
     <ul>
+      <li><a href="<?cs var:toroot ?>guide/topics/fragments/index.html">
+            <span class="en">Fragments</span>
+          </a> <span class="new">new!</span></li>
       <li class="toggle-list">
         <div><a href="<?cs var:toroot ?>guide/topics/graphics/index.html">
                <span class="en">Graphics</span>
diff --git a/docs/html/guide/topics/fragments/index.jd b/docs/html/guide/topics/fragments/index.jd
new file mode 100644
index 0000000..ce10ef7
--- /dev/null
+++ b/docs/html/guide/topics/fragments/index.jd
@@ -0,0 +1,648 @@
+page.title=Fragments
+@jd:body
+
+<div id="qv-wrapper">
+<div id="qv">
+
+  <h2>Quickview</h2>
+  <ul>
+    <li>Decompose application functionality and UI into reusable modules</li>
+    <li>Add multiple fragments to a screen to avoid switching activities</li>
+    <li>Fragments have their own lifecycle, state, and back stack</li>
+    <li>Fragments require API Level HONEYCOMB or greater</li>
+  </ul>
+
+  <h2>In this document</h2>
+  <ol>
+    <li><a href="#Creating">Creating fragments</a></li>
+    <li><a href="#Adding">Adding a fragment to an activity</a></li>
+    <li><a href="#Managing">Managing fragments</a></li>
+    <li><a href="#Lifecycle">Handling the lifecycle</a></li>
+    <li><a href="#Integrating">Integrating with the activity</a></li>
+    <li><a href="#Menus">Adding menus</a></li>
+  </ol>
+
+  <h2>Key classes</h2>
+  <ol>
+    <li>{@link android.app.Fragment}</li>
+    <li>{@link android.app.FragmentManager}</li>
+    <li>{@link android.app.FragmentTransaction}</li>
+  </ol>
+
+  <!--
+  <h2>Related samples</h2>
+  <ol>
+    <li><a
+href="{@docRoot}resources/samples/NotePad/index.html">NotePad</a></li>
+  </ol>
+  -->
+</div>
+</div>
+
+
+<p>An {@link android.app.Activity} is always the window in which users interact with your
+application, but a {@link android.app.Fragment} can be responsible for distinct operations and UI
+that's embedded in an activity. So, when using fragments, your activity becomes more like a
+container for fragments that define the activity's behavior and UI.</p>
+
+<p>Fragments have their own
+set of lifecylce callback methods and recieve their own user input events. A fragment must always be
+embedded in an activity and the fragment's lifecycle is directly affected by the activity's
+lifecycle. For example, when the activity is stopped, so are all fragments in it, and when
+the activity is destroyed, so are all fragments. However, while an activity
+is active (in the "resumed" lifecycle stage), you can manipulate the lifecycle of each fragment
+independently. For example, you can add and remove fragments while the activity is active and you
+can add each fragment to a back stack within the activity&mdash;each back stack entry in the
+activity is actually a record of a "transaction" that occurred with the activity's fragments, so
+that the user can reverse the transaction with the BACK key (this is discussed more later).</p>
+
+<div class="figure" style="width:314px">
+<img src="{@docRoot}images/fragment_lifecycle.png" alt="" />
+<p class="img-caption"><strong>Figure 1.</strong> The lifecycle of a fragment (while its
+activity is running).</p>
+</div>
+
+<p>Android introduced fragments in Android X.X (API Level HONEYCOMB), with the primary intention to
+support more dynamic and flexible UI designs on large screen devices, such as tablets. Because a
+tablet has a much larger screen than a mobile phone, there's more room to interchange UI
+elements. Fragments allow that without the need for you to start a new activity or manage complex
+changes to the view hierarchy. By dividing the layout of an activity into fragments, the code
+that defines your activity becomes more modular and interchangable, allowing you to modify the
+activity's appearance at runtime and for different types of screens.</p>
+
+<p>For example, a news application can use one fragment to show a list of articles on the
+left and another fragment to display an article on the right&mdash;both fragments appear in one
+activity, side by side, and each fragment has its own set of lifecycle callback methods and handle
+their own user input events. Thus, instead using one activity to select an article and another
+activity to read the article, the user can select an article and read it all within the same
+activity.</p>
+
+<!-- ** TODO: Save this for later or move it down in the doc so the intro isn't overwhelming **
+     
+<p>A fragment can be a modular and reusable component in your application. That is, because
+the fragment defines its own behavior using its own set of lifecycle callbacks, you can
+include one fragment in multiple activities. This also enables you to create one version of your
+application for multiple screen sizes. For instance, on an extra large screen (<em>xlarge</em>
+screen configuration), you can embed two or more fragments in one activity, but on a normal-sized
+screen (<em>normal</em> screen configuration), you can embed just one fragment in an activity and
+then start other activities in order to display the other fragments.</p>
+-->
+
+<p>When you use a fragment as a part of your layout, it technically lives within a {@link
+android.view.View} of the activity's layout and defines its own layout of views. You can insert a
+fragment into your activity layout by declaring the fragment in the activity's XML layout file, as
+a {@code &lt;fragment&gt;} element, or from your application code by adding it to an existing {@link
+android.view.View}. However, a fragment is not required to be a part of the activity
+layout&mdash;you might use a fragment as an invisible worker for the activity (more about that
+later).</p>
+
+<p>The rest of this document describes how to build your application to use fragments, including
+how fragments can contribute to the activity options menu and action bar, create context menus,
+maintain their state when added to the activity's back stack, and more.</p>
+
+
+
+<h2 id="Creating">Creating a Fragment</h2>
+
+<p>An implementation of the {@link android.app.Fragment} class contains code that looks a lot like
+the code in an {@link android.app.Activity}. In fact, if you're
+converting an existing Android application to use fragments, you'll move code
+from your {@link android.app.Activity} implementation into your {@link android.app.Fragment} class
+implementation, and into some of the same callback methods. A fragment contains callback methods
+similar to an activity, such as {@link android.app.Fragment#onCreate onCreate()}, {@link
+android.app.Fragment#onStart onStart()}, {@link android.app.Fragment#onPause onPause()}, and {@link
+android.app.Fragment#onStop onStop()}.</p>
+
+<p>If you're creating a fragment to be a modular piece of an activity UI, then your
+implementation of {@link android.app.Fragment} should include most of the same lifecycle
+callback methods traditionally implemented by the activity to initialize elements of the UI and
+save and restore state information. Usually, you'll want to implement the following methods:</p>
+
+<dl>
+  <dt>{@link android.app.Fragment#onCreate onCreate()}</dt>
+  <dd>The system calls this when creating the fragment. Within your implementation, you should
+initialize the essential components of the fragment that should be retained when the fragment is
+paused or stopped.</dd>
+  <dt>{@link android.app.Fragment#onCreateView onCreateView()}</dt>
+  <dd>The system calls this when it's time for the fragment to draw its user interface for the
+first time. To draw a UI for your fragment, you must return a {@link android.view.View} from this
+method that is the root of your fragment's layout. You can return null if the fragment does not
+provide a UI.</dd>
+  <dt>{@link android.app.Activity#onPause onPause()}</dt>
+  <dd>The system calls this method as the first indication that the user is leaving the
+fragment (though it does not always mean the fragment is being destroyed). This is usually where you
+should commit any changes that should be persisted beyond the current user session (because
+the user might not come back).</dd>
+</dl>
+
+<p>Most applications should implement at least these three methods for each fragment, but there are
+several other lifecycle callback methods that you should also use in order to provide the best
+user experience when switching fragments and when the activity is paused or stopped. All of the
+lifecycle callback methods are discussed more later, in
+the section about <a href="#Lifecycle">Handling the Lifecycle</a>.</p>
+
+
+<p>There are also a few different subclasses of {@link android.app.Fragment} that you might want 
+to use:</p>
+
+<dl>
+  <dt>{@link android.app.DialogFragment}</dt>
+  <dd>Displays a floating dialog. Using this class to create a dialog is a good alternative to using
+the dialog helper methods in the {@link android.app.Activity} class, because the dialog can be
+incorporated into the fragment back stack managed by the activity.</dd>
+
+  <dt>{@link android.app.ListFragment}</dt>
+  <dd>Displays a list of items that are managed by an adapter (such as a {@link
+android.widget.SimpleCursorAdapter}), similar to {@link android.app.ListActivity}. Provides methods
+for managing a list, such as the {@link
+android.app.ListFragment#onListItemClick(ListView,View,int,long) onListItemClick()} callback to
+handle click events on list items.</dd>
+
+  <dt>{@link android.preference.PreferenceFragment}</dt>
+  <dd>Displays a hierarchy of {@link android.preference.Preference} objects as a list, similar to
+{@link android.preference.PreferenceActivity}. </dd>
+</dl>
+
+<p>However, subclassing the standard {@link android.app.Fragment} class is most common, if
+you're not creating a dialog, a list, or displaying preferences.</p>
+
+
+<h3 id="UI">Providing a user interface</h3>
+
+<p>To provide a UI layout for a fragment, you must implement
+the {@link android.app.Fragment#onCreateView onCreateView()}
+callback method in your {@link android.app.Fragment} (unless your fragment is a subclass of
+{@link android.app.ListFragment}, which returns a {@link android.widget.ListView} from this method
+by default). The Android system calls {@link android.app.Fragment#onCreateView onCreateView()} when
+it's time for the fragment to draw its layout. Your implementation of this method must return a
+{@link android.view.View} that is the root of your fragment's layout.</p>
+
+<p>The easiest way to provide your layout is to inflate it from a <a
+href="{@docRoot}guide/topics/resources/layout-resource.html">layout resource</a>. To help you
+inflate a layout, the {@link android.app.Fragment#onCreateView onCreateView()} method passes a
+{@link android.view.LayoutInflater} that you can use to get your layout. For example, here's a
+simple subclass of {@link android.app.Fragment} that contains an implementation of {@link
+android.app.Fragment#onCreateView onCreateView()} that loads the fragment's layout from a
+resource:</p>
+
+<pre>
+public static class SimpleFragment extends Fragment {
+    &#64;Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+                             Bundle savedInstanceState) {
+        // Inflate the layout for this fragment
+        return inflater.inflate(R.layout.simple_fragment, container, false);
+    }
+}
+</pre>
+
+<p>The {@link android.view.LayoutInflater#inflate(int,ViewGroup,boolean) inflate()} method takes
+three arguments:</p>
+<ul>
+  <li>The resource ID of the layout you want to inflate</li>
+  <li>The {@link android.view.ViewGroup} to be the parent of the
+inflated layout (supplying this is important in order to apply layout parameters from the parent
+view)</li>
+  <li>And a boolean indicating whether the inflated layout should be attached to the {@link
+android.view.ViewGroup} (from the second parameter) during inflation (in this case, this
+is false because the system is already going to insert the layout into the appropriate parent
+view&mdash;doing otherwise would create a redundant view group in the final layout)</li>
+</ul>
+
+<p>The {@code container} parameter passed to {@link android.app.Fragment#onCreateView
+onCreateView()} provides the parent {@link android.view.ViewGroup} in which your fragment layout
+will be inserted, which you can use to generate layout parameters for your
+fragment layout. The {@code savedInstanceState} parameter is a {@link android.os.Bundle} that
+provides data about the previous instance of the fragment, if the fragment is being resumed
+(restoring state is discussed more in the section about <a href="#Lifecycle">Handling the
+Lifecycle</a>.</p>
+
+
+<h3 id="Adding">Adding a Fragment to an Activity</h3>
+
+<p>Each fragment is embedded into the layout of its container activity as a part of the overall view
+hierarchy, whether or not it actually provides a UI. If a fragment is not embedded into the activity
+layout, then it is never created (it does not receive any lifecycle callbacks). There are two ways
+you can add a fragment to the activity layout:</p>
+
+<ul>
+  <li><b>Declare the fragment inside the activity's layout XML file.</b>
+<p>In this case, you can
+specify layout properties for the fragment as if it were a view itself and the fragment's layout
+fills that space. For example:</p>
+<pre>
+&lt;?xml version="1.0" encoding="utf-8"?&gt;
+&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="horizontal"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"&gt;
+    &lt;fragment android:name="com.example.news.ArticleListFragment"
+            android:id="@+id/list"
+            android:layout_weight="1"
+            android:layout_width="0dp"
+            android:layout_height="match_parent" /&gt;
+    &lt;fragment android:name="com.example.news.ArticleReaderFragment"
+            android:id="@+id/viewer"
+            android:layout_weight="2"
+            android:layout_width="0dp"
+            android:layout_height="match_parent" /&gt;
+&lt;/LinearLayout&gt;
+</pre>
+  <p>The {@code &lt;fragment&gt;} element uses the {@code android:name} attribute to specify the
+{@link android.app.Fragment} class to instantiate and insert into the layout. When the activity
+layout is created, the system instantiates each fragment in the layout and calls its {@link
+android.app.Fragment#onCreateView onCreateView()} method in order to retrieve the fragment's
+layout. The {@link android.view.View} object returned by {@link
+android.app.Fragment#onCreateView onCreateView()} is then
+placed directly in the activity layout in place of the {@code &lt;fragment&gt;} element.</p>
+
+<div class="note">
+  <p><strong>Note:</strong> Each fragment requires a unique identifier that
+the system can use to restore the fragment if the activity is restarted (and which you can use to
+perform fragment transactions). There are three ways to identify a fragment:</p>
+  <ul>
+    <li>Supply the {@code android:id} attribute with a unique ID, in the {@code
+&lt;fragment&gt;}</li>
+    <li>Supply the {@code android:tag} attribute with a unique string ID, in the {@code
+  &lt;fragment&gt;}</li>
+    <li>If neither of the previous two are provided, the system uses the ID of the container
+  view.</li>
+  </ul>
+</div>
+  </li>
+
+  <li><b>Or, programmatically add the fragment to an existing {@link android.view.ViewGroup}.</b>
+<p>At any time while your activity is running (in the "resumed" state), you can add (and remove)
+fragments to your activity layout. You simply need to specify a {@link android.view.ViewGroup} in
+which to place the fragment.</p>
+  <p>To make any fragment transactions in your activity (such as add, remove, or replace a
+fragment), you must use APIs from {@link android.app.FragmentTransaction}. You can get an instance
+of {@link android.app.FragmentTransaction} from your {@link android.app.Activity} using {@link
+android.app.Activity#openFragmentTransaction()}. You can then add a fragment using the {@link
+android.app.FragmentTransaction#add add()} method, specifying the fragment to add and the view in
+which to insert it. For example:</p>
+<pre>
+MyFragment fragment = new MyFragment();
+openFragmentTransaction().add(R.id.fragment_container, fragment).commit();
+</pre>
+  <p>The first argument passed to {@link android.app.FragmentTransaction#add add()}
+is the {@link android.view.ViewGroup} in which the fragment should be placed, specified by
+resource ID, and the second parameter is the fragment object.</p>
+  <p>Once you've made your changes using
+{@link android.app.FragmentTransaction}, you must
+call {@link android.app.FragmentTransaction#commit} in order for the changes to take effect.</p>
+  </li>
+</ul>
+
+
+<h3 id="Example1">Example: simple fragments</h3>
+
+<p>In the last couple sections, you saw how to declare layout for a fragment and add it to an
+activity. What follows is some code that brings it all together, like a "Hello World" for
+fragments.</p>
+
+<p>First, here's a layout file for a fragment:</p>
+<pre>
+&lt;?xml version="1.0" encoding="utf-8"?&gt;
+&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent" &gt;
+&lt;TextView  
+    android:layout_width="match_parent" 
+    android:layout_height="wrap_content"
+    android:text="@string/hello" /&gt;
+&lt;TextView  
+    android:layout_width="match_parent" 
+    android:layout_height="wrap_content"
+    android:text="@string/hello" /&gt;
+&lt;/LinearLayout&gt;
+</pre>
+
+<p>With that file saved at {@code res/layout/simple_fragment.xml}, the following {@link
+android.app.Fragment} uses it for its layout:</p>
+
+<pre>
+public static class SimpleFragment extends Fragment {
+    &#64;Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+                             Bundle savedInstanceState) {
+        // Inflate the layout for this fragment
+        return inflater.inflate(R.layout.simple_fragment, null);
+    }
+}
+</pre>
+
+<p>And the following layout for an activity applies the fragment twice, side by side:</p>
+
+<pre>
+&lt;?xml version="1.0" encoding="utf-8"?&gt;
+&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="horizontal"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"&gt;
+    &lt;fragment android:name="com.example.SimpleFragment"
+            android:id="@+id/list"
+            android:layout_weight="1"
+            android:layout_width="0dp"
+            android:layout_height="match_parent" /&gt;
+    &lt;fragment android:name="com.example.SimpleFragment"
+            android:id="@+id/viewer"
+            android:layout_weight="1"
+            android:layout_width="0dp"
+            android:layout_height="match_parent" /&gt;
+&lt;/LinearLayout&gt;
+</pre>
+
+<p>That's it. When an activity applies the previous layout as its content, the {@code
+SimpleFragment} class is instantiated for each occurence in the layout, applying the fragment
+layout when it receives the call to  {@link android.app.Fragment#onCreateView onCreateView()}.</p>
+
+<p>Although the fragment in this example implements only the {@link
+android.app.Fragment#onCreateView onCreateView()} callback, there are several other lifecycle
+callback methods that you should implement in your application. For example, {@link
+android.app.Fragment#onCreate onCreate()}, {@link android.app.Fragment#onPause onPause()}, {@link
+android.app.Fragment#onStop onStop()} and others that coincide with the fragment's lifecycle.</p>
+
+
+
+<h2 id="Managing">Managing Fragments</h2>
+
+<p>A useful feature of fragments is the ability to add, remove, replace, and perform other
+operations on a fragment as the user interacts with the activity, alowing for more rich user
+experiences without changing activities. In order to perform these operations, you must use {@link
+android.app.FragmentTransaction} to perform fragment "transactions." You can acquire {@link
+android.app.FragmentTransaction} from your activity with {@link
+android.app.Activity#openFragmentTransaction}.</p>
+
+<p>Common transactions you can perform with fragments include:</p>
+
+<dl>
+  <dt>{@link android.app.FragmentTransaction#add add()}</dt>
+    <dd>Add a {@link android.app.Fragment} to the {@link android.app.Activity} layout.</dd>
+  <dt>{@link android.app.FragmentTransaction#remove remove()}</dt>
+    <dd>Remove a {@link android.app.Fragment} from the {@link android.app.Activity} layout.</dd>
+  <dt>{@link android.app.FragmentTransaction#replace replace()}</dt>
+    <dd>Replace an existing {@link android.app.Fragment} with another one.</dd>
+</dl>
+
+<p>For every transaction (or set of transactions) you perform, you must call {@link
+android.app.FragmentTransaction#commit} in order for the transactions made with {@link
+android.app.FragmentTransaction} to be applied. Before you do, however, you can call {@link
+android.app.FragmentTransaction#addToBackStack} to add the current fragment state to the
+activity's back stack, so that the user can return to the previous fragment state with the BACK key.
+For example, here's how a new fragment can replace another one, but keep the previous fragment
+in the back stack:</p>
+
+<pre>
+// Create new fragment
+Fragment newFragment = new MyFragment();
+FragmentTransaction ft = openFragmentTransaction();
+// Replace and add to back stack
+ft.replace(newFragment, R.id.myfragment);
+ft.addToBackStack(null);
+// Apply changes
+ft.commit();
+</pre>
+
+<p>In this example, {@code newFragment} replaces whatever fragment is currently in the
+layout container identified by the {@code R.id.myfragment} ID. By calling {@link
+android.app.FragmentTransaction#addToBackStack addToBackStack()}, this transaction (the replace) is
+saved to the activity's back stack so that the user can reverse this change and bring back the
+previous fragment by pressing the BACK key.</p>
+
+<p>If you perform multiple transactions and call {@link
+android.app.FragmentTransaction#addToBackStack addToBackStack()}, then all transactions performed
+before {@link android.app.FragmentTransaction#commit} are added to the activity's back stack as a
+single event and the BACK key will reverse them all together.</p>
+
+
+
+<h2 id="Lifecycle">Handling the Lifecycle</h2>
+
+<p>A fragment has a lifecycle that corresponds to the lifecycle of the activity in which it
+resides. For example, a fragment has callback methods {@link
+android.app.Fragment#onCreate onCreate()}, {@link android.app.Fragment#onStart onStart()}, {@link
+android.app.Fragment#onPause onPause()}, {@link android.app.Fragment#onStop onStop()}, and more.</p>
+
+<p>The lifecycle of the activity directly affects the lifecycle of the fragment, such that each
+lifecycle callback for the activity results in a similar callback for each fragment (for
+example, when the activity receives {@link android.app.Activity#onPause}, each fragment receives
+{@link android.app.Fragment#onPause}). However, the
+fragment's lifecycle can also change independently&mdash;but only while the activity is
+resumed (while it is in the foreground)&mdash;because you can dynamically
+add, remove, and replace fragments without any change to the lifecycle of the activity.</p>
+
+<p>To accomodate backward navigation with the
+BACK key, you can optionally maintain a back stack of fragment transactions, as described in the
+previous section. So, if you
+replace one fragment with another, the user can press the BACK key and view the previous
+fragment. Additionally, each fragment can maintain its own state, such that
+when the user navigates back to a previous fragment, the state of that fragment can be restored in
+the same manner as the state of an activity is restored when it is stopped and restarted.</p>
+
+<p>Managing the lifecycle of a fragment is a lot like managing the lifecycle of an activity. A
+fragment and an activity both have an "resumed," "paused," and "stopped" state, and they can both
+retain their state using a {@link android.os.Bundle}. The only significant difference is that an
+activity is placed into a the task's back stack by default (so that the user can navigate to
+the previous activity with the BACK key), but a fragment is placed into the activity's back stack
+only when you explicitly call {@link android.app.FragmentTransaction#addToBackStack(String)
+addToBackStack()} before you {@link android.app.FragmentTransaction#commit()} a fragment
+transaction.</p>
+
+<p>The order in which you perform transactions with {@link android.app.FragmentTransaction} doesn't
+matter, except:</p>
+<ul>
+  <li>You must call {@link android.app.FragmentTransaction#commit()} last</li>
+  <li>If you're adding multiple fragments to the same container, then the order in which
+you add them determines the order they appear</li>
+</ul>
+<p>If you do not call {@link android.app.FragmentTransaction#addToBackStack(String)
+addToBackStack()} when you perform a transaction that removes a fragment, then that fragment is
+destroyed when the transaction is committed.</p>
+
+
+<h3 id="CoordinatingWithTheActivity">Coordinating with the activity lifecycle</h3>
+
+<p>The lifecycle of an activity directly affects the lifecycle of a {@link android.app.Fragment}
+embedded in that activity. The {@link android.app.Fragment} class has lifecycle callback
+methods that match those in the {@link android.app.Activity} class.</p>
+
+<p>Fragments have a few extra lifecycle callbacks, however, that handle unique interaction with the
+activity in order to perform actions such as build and destroy the fragment's UI. These additional
+callback methods are:</p>
+
+<dl>
+  <dt>{@link android.app.Fragment#onAttach onAttach()}</dt>
+    <dd>Called when the fragment has been associated with the activity (the {@link
+android.app.Activity} is passed in here).</dd>
+  <dt>{@link android.app.Fragment#onCreateView onCreateView()}</dt>
+    <dd>Called to create the view hierarchy associated with the fragment.</dd>
+  <dt>{@link android.app.Fragment#onActivityCreated onActivityCreated()}</dt>
+    <dd>Called when the activity's own {@link android.app.Activity#onCreate
+onCreate()} has finished.</dd>
+  <dt>{@link android.app.Fragment#onDestroyView onDestroyView()}</dt>
+    <dd>Called when the view hierarchy associated with the fragment is being removed.</dd>
+  <dt>{@link android.app.Fragment#onDetach onDetach()}</dt>
+    <dd>Called when the fragment is being disassociated from the activity.</dd>
+</dl>
+
+<p>The flow of a fragment's lifecycle, as it is affected by its container activity, is illustrated
+by figure 3. In this figure, you can see how each successive state of the activity determines which
+callback methods the fragment may receive. For example, when the activity has received
+its {@link android.app.Activity#onCreate onCreate()} callback, the fragment receives no more
+than the {@link android.app.Fragment#onActivityCreated onActivityCreated()} callback. However,
+once the activity reaches the resumed state, you can freely add and remove fragments to the
+activity, so the fragment lifecycle is no longer inhibitted by the state of the activity. Yet,
+when the activity leaves the resumed state, the fragment again is pushed through its lifecycle by
+the activity (unless you explicitly destroy the fragment sooner).</p>
+
+
+<img src="{@docRoot}images/activity_fragment_lifecycle.png" alt=""/>
+<p class="img-caption"><strong>Figure 3.</strong> The activity lifecycle's affect on the lifecycle
+of a fragment.</p>
+
+
+<h3 id="Integrating">Integrating with the Activity</h3>
+
+<p>Although a {@link android.app.Fragment} is implemented separate from an {@link
+android.app.Activity} and can be used inside multiple activities, a fragment is directly tied to its
+container activity and can access the Activity instance with {@link
+android.app.Fragment#getActivity()}. So, a fragment can
+easily perform tasks such as find a view in the activity:</p>
+
+<pre>
+View listView = {@link android.app.Fragment#getActivity()}.{@link android.app.Activity#findViewById findViewById}(R.id.list);
+</pre>
+
+<p>This makes it easy for your fragment to call public methods in the activity.</p>
+
+<p>Likewise, your activity can call public methods in the fragment when you have a reference to the
+{@link android.app.Fragment}. You can acquire a reference to the fragment with {@link
+android.app.Activity#findFragmentById findFragmentById()} and cast it to your implementation of
+{@link android.app.Fragment}. For example:</p>
+
+<pre>
+MyFragment fragment = (MyFragment) findFragmentById(R.id.myfragment);
+fragment.refreshList();
+</pre>
+
+
+<h4 id="Callbacks">Creating event callbacks to the activity</h4>
+
+<p>In some cases, you might need a fragment to share events with the activity. A good way to do that
+is to define a callback interface inside the fragment and require that the host activity implement
+it. When the activity receives a callback, it can share the information with other fragments in the layout as
+necessary.</p>
+
+<p>For example, if a news application has two fragments in an activity&mdash;one to show a list of
+articles (fragment A) and another to display an article (fragment B)&mdash;then fragment A must tell
+the activity when a list item is selected so that it can tell fragment B to display the article. In
+this case, the following interface is defined inside fragment A:</p>
+
+<pre>
+public static class FragmentA extends ListFragment {
+    ...
+    // Container Activity must implement this interface
+    public interface SelectedCallback {
+        public void onArticleSelected(Uri articleUri);
+    }
+    ...
+}
+</pre>
+
+<p>Then the activity that hosts the fragment implements the {@code SelectedCallback} interface and
+overrides {@code onArticleSelected()} to notify fragment B of the event from fragment A. To ensure
+that the host activity implements this interface, fragment A's {@link
+android.app.Fragment#onAttach onAttach()} callback method (called when
+the fragment is added to the activity) instantiates an instance of {@code SelectedCallback} by
+casting the {@link android.app.Activity} that is passed into {@link android.app.Fragment#onAttach
+onAttach()}:</p>
+
+<pre>
+public static class FragmentA extends ListFragment {
+    ...
+    &#64;Override
+    public void onAttach(Activity activity) {
+        super.onAttach(activity);
+        try {
+            mCallback = (SelectedCallback) activity;
+        } catch (ClassCastException e) {
+            activity.finish();
+            throw new ClassCastException(activity.toString() + " must implement SelectedCallback");
+        }
+    }
+    ...
+}
+</pre>
+
+<p>If the activity has not implemented the interface, then a {@link java.lang.ClassCastException} is
+thrown and the activity is shut down. On success, the {@code mCallback} member holds a reference to
+the {@link android.app.Activity}, so that fragment A can share events with the activity by calling
+methods defined by the {@code SelectedCallback} interface. For example, if fragment A is an
+extension of {@link android.app.ListFragment}, each time
+the user clicks a list item, the system calls {@link android.app.ListFragment#onListItemClick
+onListItemClick()} in the fragment, which then calls {@code onArticleSelected()} to share
+the event with the activity:</p>
+
+<pre>
+public static class FragmentA extends ListFragment {
+    ...
+    &#64;Override
+    public void onListItemClick(ListView l, View v, int position, long id) {
+        // Append the clicked item's row ID with the content provider Uri
+        Uri noteUri = ContentUris.{@link android.content.ContentUris#withAppendedId withAppendedId}(ArticleColumns.CONTENT_URI, id);
+        // Send the event and Uri to the host activity
+        mCallback.onArticleSelected(noteUri);
+    }
+    ...
+}
+</pre>
+
+<p>The {@code id} parameter passed to {@link
+android.app.ListFragment#onListItemClick onListItemClick()} is the row ID of the clicked item,
+which the activity (or other fragment) uses to fetch the article from the application's {@link
+android.content.ContentProvider}.</p>
+
+<p><!--To see a complete implementation of this kind of callback interface, see the <a
+href="{@docRoot}resources/samples/NotePad/index.html">NotePad sample</a>. -->More information about
+using a content provider is available in the <a
+href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a> document.</p>
+
+
+
+
+
+
+<h2 id="Menus">Adding Action Items to the Activity</h2>
+
+<p>Your fragments can contribute action items to the activity's <a
+href="{@docRoot}guide/topics/ui/actionbar.html">action bar</a> (and menu items to the options menu)
+using the callback methods
+{@link android.app.Fragment#onCreateOptionsMenu(Menu,MenuInflater) onCreateOptionsMenu()}. In order
+for this method to receive calls, however, you must call {@link
+android.app.Fragment#setHasOptionsMenu(boolean) setHasOptionsMenu()} during the {@link
+android.app.Fragment#onCreate(Bundle) onCreate()} callback in order to indicate that the fragment
+would like to receive a call to {@link android.app.Fragment#onCreateOptionsMenu(Menu,MenuInflater)
+onCreateOptionsMenu()}. Any action or menu items that you add from the fragment are appended to the
+existing
+items for the options menu (including those added by other fragments in the activity). The
+fragment also receives item-selected events with the {@link
+android.app.Fragment#onOptionsItemSelected(MenuItem) onOptionsItemSelected()} callback method.</p>
+
+<p>The {@link android.app.Fragment} class also contains methods to handle context menus. You can
+register a view to provide a context menu with {@link
+android.app.Fragment#registerForContextMenu(View) registerForContextMenu()}. When the user opens
+the context menu, the fragment receives a call to {@link
+android.app.Fragment#onCreateContextMenu(ContextMenu,View,ContextMenu.ContextMenuInfo)
+onCreateContextMenu()}. When the user selects an item, the fragment receives a call to {@link
+android.app.Fragment#onContextItemSelected(MenuItem) onContextItemSelected()}.</p>
+
+<p>For more information, see <a href="{@docRoot}guide/topics/ui/menus.html">Creating
+Menus</a> and <a href="{@docRoot}guide/topics/ui/actionbar.html">Using the Action Bar</a>.</p>
+
+
+
+
+
+
diff --git a/docs/html/guide/topics/manifest/activity-element.jd b/docs/html/guide/topics/manifest/activity-element.jd
index e030a4c..2648cb7 100644
--- a/docs/html/guide/topics/manifest/activity-element.jd
+++ b/docs/html/guide/topics/manifest/activity-element.jd
@@ -290,7 +290,8 @@
 <dd>An icon representing the activity. The icon is displayed to users when 
 a representation of the activity is required on-screen.  For example, icons 
 for activities that initiate tasks are displayed in the launcher window.  
-The icon is often accompanied by a label (see the {@code label} attribute).
+The icon is often accompanied by a label (see the <a href="#label">{@code
+android:label}</a> attribute).
 </p>
 
 <p>
diff --git a/docs/html/guide/topics/manifest/application-element.jd b/docs/html/guide/topics/manifest/application-element.jd
index 9ac07fd..1fadc6e 100644
--- a/docs/html/guide/topics/manifest/application-element.jd
+++ b/docs/html/guide/topics/manifest/application-element.jd
@@ -12,6 +12,7 @@
              android:<a href="#icon">icon</a>="<i>drawable resource</i>"
              android:<a href="#killrst">killAfterRestore</a>=["true" | "false"]
              android:<a href="#label">label</a>="<i>string resource</i>"
+             android:<a href="#logo">logo</a>="<i>drawable resource</i>"
              android:<a href="#space">manageSpaceActivity</a>="<i>string</i>"
              android:<a href="#nm">name</a>="<i>string</i>"
              android:<a href="#prmsn">permission</a>="<i>string</i>"
@@ -121,7 +122,7 @@
 
 <p>
 This attribute must be set as a reference to a drawable resource containing
-the image definition.   There is no default icon.
+the image (for example {@code "@drawable/icon"}).   There is no default icon.
 </p></dd>
 
 <dt><a name="killrst"></a>{@code android:killAfterRestore}</dt>
@@ -154,6 +155,11 @@
 it can also be set as a raw string.
 </p></dd>
 
+<dt><a name="logo"></a>{@code android:logo}</dt>
+<dd>A logo for the application as whole, and the default logo for activities.
+<p>This attribute must be set as a reference to a drawable resource containing
+the image (for example {@code "@drawable/logo"}).  There is no default logo.</p></dd>
+
 <dt><a name="space"></a>{@code android:manageSpaceActivity}</dt>
 <dd>The fully qualified name of an Activity subclass that the system 
 can launch to let users manage the memory occupied by the application 
diff --git a/docs/html/images/activity_fragment_lifecycle.png b/docs/html/images/activity_fragment_lifecycle.png
new file mode 100644
index 0000000..156aa40
--- /dev/null
+++ b/docs/html/images/activity_fragment_lifecycle.png
Binary files differ
diff --git a/docs/html/images/fragment_lifecycle.png b/docs/html/images/fragment_lifecycle.png
new file mode 100644
index 0000000..ce9d395
--- /dev/null
+++ b/docs/html/images/fragment_lifecycle.png
Binary files differ
diff --git a/docs/html/resources/resources-data.js b/docs/html/resources/resources-data.js
index d06b695..221406c 100644
--- a/docs/html/resources/resources-data.js
+++ b/docs/html/resources/resources-data.js
@@ -465,7 +465,7 @@
     }
   },
   {
-    tags: ['sample', 'ui', 'search', 'new'],
+    tags: ['sample', 'ui', 'search'],
     path: 'samples/SearchableDictionary/index.html',
     title: {
       en: 'Searchable Dictionary v2'
@@ -485,7 +485,7 @@
     }
   },
   {
-    tags: ['sample', 'testing', 'new'],
+    tags: ['sample', 'testing'],
     path: 'samples/Spinner/index.html',
     title: {
       en: 'Spinner'
@@ -495,7 +495,7 @@
     }
   },
   {
-    tags: ['sample', 'testing', 'new'],
+    tags: ['sample', 'testing'],
     path: 'samples/SpinnerTest/index.html',
     title: {
       en: 'SpinnerTest'
@@ -505,7 +505,7 @@
     }
   },
   {
-    tags: ['sample', 'newfeature', 'new'],
+    tags: ['sample', 'newfeature'],
     path: 'samples/TicTacToeLib/index.html',
     title: {
       en: 'TicTacToeLib'
@@ -515,7 +515,7 @@
     }
   },
   {
-    tags: ['sample', 'newfeature', 'new'],
+    tags: ['sample', 'newfeature',],
     path: 'samples/TicTacToeMain/index.html',
     title: {
       en: 'TicTacToeMain'
@@ -610,7 +610,7 @@
     }
   },
   {
-    tags: ['tutorial', 'testing', 'new'],
+    tags: ['tutorial', 'testing'],
     path: 'tutorials/testing/helloandroid_test.html',
     title: {
       en: 'Hello Testing'
@@ -620,7 +620,7 @@
     }
   },
   {
-    tags: ['tutorial', 'testing', 'new'],
+    tags: ['tutorial', 'testing'],
     path: 'tutorials/testing/activity_test.html',
     title: {
       en: 'Activity Testing'
diff --git a/media/libstagefright/Android.mk b/media/libstagefright/Android.mk
index 8fe1d4d..4ad1eb4 100644
--- a/media/libstagefright/Android.mk
+++ b/media/libstagefright/Android.mk
@@ -49,7 +49,6 @@
         WVMExtractor.cpp                  \
         XINGSeeker.cpp                    \
         avc_utils.cpp                     \
-        string.cpp
 
 LOCAL_C_INCLUDES:= \
 	$(JNI_H_INCLUDE) \
diff --git a/media/libstagefright/HTTPStream.cpp b/media/libstagefright/HTTPStream.cpp
index ccc6a34..e7f00aa 100644
--- a/media/libstagefright/HTTPStream.cpp
+++ b/media/libstagefright/HTTPStream.cpp
@@ -36,7 +36,7 @@
 namespace android {
 
 // static
-const char *HTTPStream::kStatusKey = ":status:";
+const char *HTTPStream::kStatusKey = ":status:";  // MUST be lowercase.
 
 HTTPStream::HTTPStream()
     : mState(READY),
@@ -220,7 +220,7 @@
         return err;
     }
 
-    mHeaders.add(string(kStatusKey), string(line));
+    mHeaders.add(AString(kStatusKey), AString(line));
 
     char *spacePos = strchr(line, ' ');
     if (spacePos == NULL) {
@@ -264,7 +264,10 @@
 
         char *colonPos = strchr(line, ':');
         if (colonPos == NULL) {
-            mHeaders.add(string(line), string());
+            AString key = line;
+            key.tolower();
+
+            mHeaders.add(key, AString());
         } else {
             char *end_of_key = colonPos;
             while (end_of_key > line && isspace(end_of_key[-1])) {
@@ -278,7 +281,10 @@
 
             *end_of_key = '\0';
 
-            mHeaders.add(string(line), string(start_of_value));
+            AString key = line;
+            key.tolower();
+
+            mHeaders.add(key, AString(start_of_value));
         }
     }
 
@@ -314,8 +320,11 @@
     return (ssize_t)total;
 }
 
-bool HTTPStream::find_header_value(const string &key, string *value) const {
-    ssize_t index = mHeaders.indexOfKey(key);
+bool HTTPStream::find_header_value(const AString &key, AString *value) const {
+    AString key_lower = key;
+    key_lower.tolower();
+
+    ssize_t index = mHeaders.indexOfKey(key_lower);
     if (index < 0) {
         value->clear();
         return false;
diff --git a/media/libstagefright/NuHTTPDataSource.cpp b/media/libstagefright/NuHTTPDataSource.cpp
index 15c9ac6..269b233 100644
--- a/media/libstagefright/NuHTTPDataSource.cpp
+++ b/media/libstagefright/NuHTTPDataSource.cpp
@@ -178,7 +178,7 @@
         }
 
         if (IsRedirectStatusCode(httpStatus)) {
-            string value;
+            AString value;
             CHECK(mHTTP.find_header_value("Location", &value));
 
             mState = DISCONNECTED;
@@ -198,9 +198,8 @@
         mHasChunkedTransferEncoding = false;
 
         {
-            string value;
-            if (mHTTP.find_header_value("Transfer-Encoding", &value)
-                    || mHTTP.find_header_value("Transfer-encoding", &value)) {
+            AString value;
+            if (mHTTP.find_header_value("Transfer-Encoding", &value)) {
                 // We don't currently support any transfer encodings but
                 // chunked.
 
@@ -222,9 +221,9 @@
         applyTimeoutResponse();
 
         if (offset == 0) {
-            string value;
+            AString value;
             unsigned long x;
-            if (mHTTP.find_header_value(string("Content-Length"), &value)
+            if (mHTTP.find_header_value(AString("Content-Length"), &value)
                     && ParseSingleUnsignedLong(value.c_str(), &x)) {
                 mContentLength = (off64_t)x;
                 mContentLengthValid = true;
@@ -239,9 +238,9 @@
                 return ERROR_UNSUPPORTED;
             }
 
-            string value;
+            AString value;
             unsigned long x;
-            if (mHTTP.find_header_value(string("Content-Range"), &value)) {
+            if (mHTTP.find_header_value(AString("Content-Range"), &value)) {
                 const char *slashPos = strchr(value.c_str(), '/');
                 if (slashPos != NULL
                         && ParseSingleUnsignedLong(slashPos + 1, &x)) {
@@ -439,7 +438,7 @@
 }
 
 void NuHTTPDataSource::applyTimeoutResponse() {
-    string timeout;
+    AString timeout;
     if (mHTTP.find_header_value("X-SocketTimeout", &timeout)) {
         const char *s = timeout.c_str();
         char *end;
diff --git a/media/libstagefright/ShoutcastSource.cpp b/media/libstagefright/ShoutcastSource.cpp
index 23b7681..783f2d0 100644
--- a/media/libstagefright/ShoutcastSource.cpp
+++ b/media/libstagefright/ShoutcastSource.cpp
@@ -14,7 +14,6 @@
  * limitations under the License.
  */
 
-#include "include/stagefright_string.h"
 #include "include/HTTPStream.h"
 
 #include <stdlib.h>
@@ -34,7 +33,7 @@
       mBytesUntilMetaData(0),
       mGroup(NULL),
       mStarted(false) {
-    string metaint;
+    AString metaint;
     if (mHttp->find_header_value("icy-metaint", &metaint)) {
         char *end;
         const char *start = metaint.c_str();
diff --git a/media/libstagefright/include/HTTPStream.h b/media/libstagefright/include/HTTPStream.h
index 793798f..545cd0c 100644
--- a/media/libstagefright/include/HTTPStream.h
+++ b/media/libstagefright/include/HTTPStream.h
@@ -18,10 +18,9 @@
 
 #define HTTP_STREAM_H_
 
-#include "stagefright_string.h"
-
 #include <sys/types.h>
 
+#include <media/stagefright/foundation/AString.h>
 #include <media/stagefright/MediaErrors.h>
 #include <utils/KeyedVector.h>
 #include <utils/threads.h>
@@ -50,7 +49,7 @@
     static const char *kStatusKey;
 
     bool find_header_value(
-            const string &key, string *value) const;
+            const AString &key, AString *value) const;
 
     // Pass a negative value to disable the timeout.
     void setReceiveTimeout(int seconds);
@@ -70,7 +69,7 @@
     Mutex mLock;
     int mSocket;
 
-    KeyedVector<string, string> mHeaders;
+    KeyedVector<AString, AString> mHeaders;
 
     HTTPStream(const HTTPStream &);
     HTTPStream &operator=(const HTTPStream &);
diff --git a/media/libstagefright/include/stagefright_string.h b/media/libstagefright/include/stagefright_string.h
deleted file mode 100644
index 5dc7116..0000000
--- a/media/libstagefright/include/stagefright_string.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Copyright (C) 2009 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef STRING_H_
-
-#define STRING_H_
-
-#include <utils/String8.h>
-
-namespace android {
-
-class string {
-public:
-    typedef size_t size_type;
-    static size_type npos;
-
-    string();
-    string(const char *s);
-    string(const char *s, size_t length);
-    string(const string &from, size_type start, size_type length = npos);
-
-    const char *c_str() const;
-    size_type size() const;
-
-    void clear();
-    void erase(size_type from, size_type length);
-
-    size_type find(char c) const;
-
-    bool operator<(const string &other) const;
-    bool operator==(const string &other) const;
-
-    string &operator+=(char c);
-
-private:
-    String8 mString;
-};
-
-}  // namespace android
-
-#endif  // STRING_H_
diff --git a/media/libstagefright/string.cpp b/media/libstagefright/string.cpp
deleted file mode 100644
index 8b2c36c..0000000
--- a/media/libstagefright/string.cpp
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * Copyright (C) 2009 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "include/stagefright_string.h"
-
-#include <media/stagefright/MediaDebug.h>
-
-namespace android {
-
-// static
-string::size_type string::npos = (string::size_type)-1;
-
-string::string() {
-}
-
-string::string(const char *s, size_t length)
-    : mString(s, length) {
-}
-
-string::string(const string &from, size_type start, size_type length) {
-    CHECK(start <= from.size());
-    if (length == npos) {
-        length = from.size() - start;
-    } else {
-        CHECK(start + length <= from.size());
-    }
-
-    mString.setTo(from.c_str() + start, length);
-}
-
-string::string(const char *s)
-    : mString(s) {
-}
-
-const char *string::c_str() const {
-    return mString.string();
-}
-
-string::size_type string::size() const {
-    return mString.length();
-}
-
-void string::clear() {
-    mString = String8();
-}
-
-string::size_type string::find(char c) const {
-    char s[2];
-    s[0] = c;
-    s[1] = '\0';
-
-    ssize_t index = mString.find(s);
-
-    return index < 0 ? npos : (size_type)index;
-}
-
-bool string::operator<(const string &other) const {
-    return mString < other.mString;
-}
-
-bool string::operator==(const string &other) const {
-    return mString == other.mString;
-}
-
-string &string::operator+=(char c) {
-    mString.append(&c, 1);
-
-    return *this;
-}
-
-void string::erase(size_t from, size_t length) {
-    String8 s(mString.string(), from);
-    s.append(mString.string() + from + length);
-    
-    mString = s;
-}
-
-}  // namespace android
-
diff --git a/media/tests/mtp/Android.mk b/media/tests/mtp/Android.mk
deleted file mode 100644
index a9074ed..0000000
--- a/media/tests/mtp/Android.mk
+++ /dev/null
@@ -1,61 +0,0 @@
-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
deleted file mode 100644
index 00d328e..0000000
--- a/media/tests/mtp/MtpFile.cpp
+++ /dev/null
@@ -1,187 +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.
- */
-
-#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
deleted file mode 100644
index ab8762b..0000000
--- a/media/tests/mtp/MtpFile.h
+++ /dev/null
@@ -1,57 +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.
- */
-
-#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
deleted file mode 100644
index 9732944..0000000
--- a/media/tests/mtp/mtp.cpp
+++ /dev/null
@@ -1,350 +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.
- */
-
-#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);
-    if (srcFile->getDevice()->readObject(info->mHandle, dest))
-        ret = 0;
-
-fail:
-    delete srcFile;
-    delete info;
-    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] == (char)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/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java b/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
index 6c84afd..d11a18e 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
@@ -61,7 +61,7 @@
     // database gets upgraded properly. At a minimum, please confirm that 'upgradeVersion'
     // is properly propagated through your change.  Not doing so will result in a loss of user
     // settings.
-    private static final int DATABASE_VERSION = 61;
+    private static final int DATABASE_VERSION = 62;
 
     private Context mContext;
 
@@ -793,22 +793,15 @@
         }
 
         if (upgradeVersion == 60) {
-            // Increase screen timeout for tablet
-            db.beginTransaction();
-            SQLiteStatement stmt = null;
-            try {
-                stmt = db.compileStatement("INSERT OR REPLACE INTO system(name,value)"
-                        + " VALUES(?,?);");
-                loadIntegerSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,
-                        R.integer.def_screen_off_timeout); 
-                db.setTransactionSuccessful();
-            } finally {
-                db.endTransaction();
-                if (stmt != null) stmt.close();
-            }
+            upgradeScreenTimeout(db);
             upgradeVersion = 61;
         }
 
+        if (upgradeVersion == 61) {
+            upgradeScreenTimeout(db);
+            upgradeVersion = 62;
+        }
+
         // *** Remember to update DATABASE_VERSION above!
 
         if (upgradeVersion != currentVersion) {
@@ -914,6 +907,23 @@
         }
     }
 
+    private void upgradeScreenTimeout(SQLiteDatabase db) {
+        // Change screen timeout to current default
+        db.beginTransaction();
+        SQLiteStatement stmt = null;
+        try {
+            stmt = db.compileStatement("INSERT OR REPLACE INTO system(name,value)"
+                    + " VALUES(?,?);");
+            loadIntegerSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,
+                    R.integer.def_screen_off_timeout);
+            db.setTransactionSuccessful();
+        } finally {
+            db.endTransaction();
+            if (stmt != null)
+                stmt.close();
+        }
+    }
+
     /**
      * Loads the default set of bookmarked shortcuts from an xml file.
      *
diff --git a/wifi/java/android/net/wifi/SupplicantStateTracker.java b/wifi/java/android/net/wifi/SupplicantStateTracker.java
new file mode 100644
index 0000000..a83a0ad
--- /dev/null
+++ b/wifi/java/android/net/wifi/SupplicantStateTracker.java
@@ -0,0 +1,333 @@
+/*
+ * 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.net.wifi;
+
+import com.android.internal.util.HierarchicalState;
+import com.android.internal.util.HierarchicalStateMachine;
+
+import android.net.wifi.WifiStateMachine.StateChangeResult;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Handler;
+import android.os.Message;
+import android.os.Parcelable;
+import android.util.Log;
+
+/**
+ * Tracks the state changes in supplicant and provides functionality
+ * that is based on these state changes:
+ * - detect a failed WPA handshake that loops indefinitely
+ * - password failure handling
+ * - Enable networks after a WPS success/failure
+ */
+class SupplicantStateTracker extends HierarchicalStateMachine {
+
+    private static final String TAG = "SupplicantStateTracker";
+    private static final boolean DBG = false;
+
+    private WifiStateMachine mWifiStateMachine;
+    private int mPasswordFailuresCount = 0;
+    /* Indicates authentication failure in supplicant broadcast.
+     * TODO: enhance auth failure reporting to include notification
+     * for all type of failures: EAP, WPS & WPA networks */
+    private boolean mAuthFailureInSupplicantBroadcast = false;
+
+    /* Maximum retries on a password failure notification */
+    private static final int MAX_RETRIES_ON_PASSWORD_FAILURE = 2;
+
+    /* Track if WPS was started since we need to re-enable networks
+     * and load configuration afterwards */
+    private boolean mWpsStarted = false;
+
+    private Context mContext;
+
+    private HierarchicalState mUninitializedState = new UninitializedState();
+    private HierarchicalState mDefaultState = new DefaultState();
+    private HierarchicalState mInactiveState = new InactiveState();
+    private HierarchicalState mDisconnectState = new DisconnectedState();
+    private HierarchicalState mScanState = new ScanState();
+    private HierarchicalState mHandshakeState = new HandshakeState();
+    private HierarchicalState mCompletedState = new CompletedState();
+    private HierarchicalState mDormantState = new DormantState();
+
+    public SupplicantStateTracker(Context context, WifiStateMachine wsm, Handler target) {
+        super(TAG, target.getLooper());
+
+        mContext = context;
+        mWifiStateMachine = wsm;
+        addState(mDefaultState);
+            addState(mUninitializedState, mDefaultState);
+            addState(mInactiveState, mDefaultState);
+            addState(mDisconnectState, mDefaultState);
+            addState(mScanState, mDefaultState);
+            addState(mHandshakeState, mDefaultState);
+            addState(mCompletedState, mDefaultState);
+            addState(mDormantState, mDefaultState);
+
+        setInitialState(mUninitializedState);
+
+        //start the state machine
+        start();
+    }
+
+    public void resetSupplicantState() {
+        transitionTo(mUninitializedState);
+    }
+
+
+    private void transitionOnSupplicantStateChange(StateChangeResult stateChangeResult) {
+        SupplicantState supState = (SupplicantState) stateChangeResult.state;
+
+        if (DBG) Log.d(TAG, "Supplicant state: " + supState.toString() + "\n");
+
+        switch (supState) {
+            case DISCONNECTED:
+                transitionTo(mDisconnectState);
+                break;
+            case SCANNING:
+                transitionTo(mScanState);
+                break;
+            case ASSOCIATING:
+            case ASSOCIATED:
+            case FOUR_WAY_HANDSHAKE:
+            case GROUP_HANDSHAKE:
+                transitionTo(mHandshakeState);
+                break;
+            case COMPLETED:
+                transitionTo(mCompletedState);
+                break;
+            case DORMANT:
+                transitionTo(mDormantState);
+                break;
+            case INACTIVE:
+                transitionTo(mInactiveState);
+                break;
+            case UNINITIALIZED:
+            case INVALID:
+                transitionTo(mUninitializedState);
+                break;
+            default:
+                Log.e(TAG, "Unknown supplicant state " + supState);
+                break;
+        }
+    }
+
+    private void sendSupplicantStateChangedBroadcast(StateChangeResult sc, boolean failedAuth) {
+        Intent intent = new Intent(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
+        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
+                | Intent.FLAG_RECEIVER_REPLACE_PENDING);
+        intent.putExtra(WifiManager.EXTRA_NEW_STATE, (Parcelable)sc.state);
+        if (failedAuth) {
+            intent.putExtra(
+                WifiManager.EXTRA_SUPPLICANT_ERROR,
+                WifiManager.ERROR_AUTHENTICATING);
+        }
+        mContext.sendStickyBroadcast(intent);
+    }
+
+    /********************************************************
+     * HSM states
+     *******************************************************/
+
+    class DefaultState extends HierarchicalState {
+        @Override
+         public void enter() {
+             if (DBG) Log.d(TAG, getName() + "\n");
+         }
+        @Override
+        public boolean processMessage(Message message) {
+            if (DBG) Log.d(TAG, getName() + message.toString() + "\n");
+            switch (message.what) {
+                case WifiStateMachine.PASSWORD_MAY_BE_INCORRECT_EVENT:
+                    mPasswordFailuresCount++;
+                    mAuthFailureInSupplicantBroadcast = true;
+                    break;
+                case WifiStateMachine.CMD_START_WPS_PBC:
+                case WifiStateMachine.CMD_START_WPS_PIN_FROM_AP:
+                case WifiStateMachine.CMD_START_WPS_PIN_FROM_DEVICE:
+                    mWpsStarted = true;
+                    break;
+                case WifiStateMachine.SUPPLICANT_STATE_CHANGE_EVENT:
+                    StateChangeResult stateChangeResult = (StateChangeResult) message.obj;
+                    sendSupplicantStateChangedBroadcast(stateChangeResult,
+                            mAuthFailureInSupplicantBroadcast);
+                    mAuthFailureInSupplicantBroadcast = false;
+                    transitionOnSupplicantStateChange(stateChangeResult);
+                    break;
+                default:
+                    Log.e(TAG, "Ignoring " + message);
+                    break;
+            }
+            return HANDLED;
+        }
+    }
+
+    class UninitializedState extends HierarchicalState {
+        @Override
+         public void enter() {
+             if (DBG) Log.d(TAG, getName() + "\n");
+             mWifiStateMachine.setNetworkAvailable(false);
+         }
+        @Override
+        public void exit() {
+            mWifiStateMachine.setNetworkAvailable(true);
+        }
+    }
+
+    class InactiveState extends HierarchicalState {
+        @Override
+         public void enter() {
+             if (DBG) Log.d(TAG, getName() + "\n");
+             /* A failed WPS connection */
+             if (mWpsStarted) {
+                 Log.e(TAG, "WPS set up failed, enabling other networks");
+                 WifiConfigStore.enableAllNetworks();
+                 mWpsStarted = false;
+             }
+             mWifiStateMachine.setNetworkAvailable(false);
+         }
+        @Override
+        public void exit() {
+            mWifiStateMachine.setNetworkAvailable(true);
+        }
+    }
+
+
+    class DisconnectedState extends HierarchicalState {
+        @Override
+         public void enter() {
+             if (DBG) Log.d(TAG, getName() + "\n");
+             /* If a disconnect event happens after password key failure
+              * exceeds maximum retries, disable the network
+              */
+
+             Message message = getCurrentMessage();
+             StateChangeResult stateChangeResult = (StateChangeResult) message.obj;
+
+             if (mPasswordFailuresCount >= MAX_RETRIES_ON_PASSWORD_FAILURE) {
+                 Log.d(TAG, "Failed to authenticate, disabling network " +
+                         stateChangeResult.networkId);
+                 WifiConfigStore.disableNetwork(stateChangeResult.networkId);
+                 mPasswordFailuresCount = 0;
+             }
+         }
+    }
+
+    class ScanState extends HierarchicalState {
+        @Override
+         public void enter() {
+             if (DBG) Log.d(TAG, getName() + "\n");
+         }
+    }
+
+    class HandshakeState extends HierarchicalState {
+        /**
+         * The max number of the WPA supplicant loop iterations before we
+         * decide that the loop should be terminated:
+         */
+        private static final int MAX_SUPPLICANT_LOOP_ITERATIONS = 4;
+        private int mLoopDetectIndex;
+        private int mLoopDetectCount;
+
+        @Override
+         public void enter() {
+             if (DBG) Log.d(TAG, getName() + "\n");
+             mLoopDetectIndex = 0;
+             mLoopDetectCount = 0;
+         }
+        @Override
+        public boolean processMessage(Message message) {
+            if (DBG) Log.d(TAG, getName() + message.toString() + "\n");
+            switch (message.what) {
+                case WifiStateMachine.SUPPLICANT_STATE_CHANGE_EVENT:
+                    StateChangeResult stateChangeResult = (StateChangeResult) message.obj;
+                    SupplicantState state = (SupplicantState) stateChangeResult.state;
+                    if (state == SupplicantState.ASSOCIATING ||
+                            state == SupplicantState.ASSOCIATED ||
+                            state == SupplicantState.FOUR_WAY_HANDSHAKE ||
+                            state == SupplicantState.GROUP_HANDSHAKE) {
+                        if (mLoopDetectIndex > state.ordinal()) {
+                            mLoopDetectCount++;
+                        }
+                        if (mLoopDetectCount > MAX_SUPPLICANT_LOOP_ITERATIONS) {
+                            Log.d(TAG, "Supplicant loop detected, disabling network " +
+                                    stateChangeResult.networkId);
+                            WifiConfigStore.disableNetwork(stateChangeResult.networkId);
+                        }
+                        mLoopDetectIndex = state.ordinal();
+                        sendSupplicantStateChangedBroadcast(stateChangeResult,
+                                mAuthFailureInSupplicantBroadcast);
+                    } else {
+                        //Have the DefaultState handle the transition
+                        return NOT_HANDLED;
+                    }
+                    break;
+                default:
+                    return NOT_HANDLED;
+            }
+            return HANDLED;
+        }
+    }
+
+    class CompletedState extends HierarchicalState {
+        @Override
+         public void enter() {
+             if (DBG) Log.d(TAG, getName() + "\n");
+             /* Reset password failure count */
+             mPasswordFailuresCount = 0;
+
+             /* A successful WPS connection */
+             if (mWpsStarted) {
+                 WifiConfigStore.enableAllNetworks();
+                 WifiConfigStore.loadConfiguredNetworks();
+                 mWpsStarted = false;
+             }
+         }
+        @Override
+        public boolean processMessage(Message message) {
+            if (DBG) Log.d(TAG, getName() + message.toString() + "\n");
+            switch(message.what) {
+                case WifiStateMachine.SUPPLICANT_STATE_CHANGE_EVENT:
+                    StateChangeResult stateChangeResult = (StateChangeResult) message.obj;
+                    SupplicantState state = (SupplicantState) stateChangeResult.state;
+                    sendSupplicantStateChangedBroadcast(stateChangeResult,
+                            mAuthFailureInSupplicantBroadcast);
+                    /* Ignore a re-auth in completed state */
+                    if (state == SupplicantState.ASSOCIATING ||
+                            state == SupplicantState.ASSOCIATED ||
+                            state == SupplicantState.FOUR_WAY_HANDSHAKE ||
+                            state == SupplicantState.GROUP_HANDSHAKE ||
+                            state == SupplicantState.COMPLETED) {
+                        break;
+                    }
+                    transitionOnSupplicantStateChange(stateChangeResult);
+                    break;
+                default:
+                    return NOT_HANDLED;
+            }
+            return HANDLED;
+        }
+    }
+
+    //TODO: remove after getting rid of the state in supplicant
+    class DormantState extends HierarchicalState {
+        @Override
+        public void enter() {
+            if (DBG) Log.d(TAG, getName() + "\n");
+        }
+    }
+}
\ No newline at end of file
diff --git a/wifi/java/android/net/wifi/WifiStateMachine.java b/wifi/java/android/net/wifi/WifiStateMachine.java
index 95e2df3..90abd02 100644
--- a/wifi/java/android/net/wifi/WifiStateMachine.java
+++ b/wifi/java/android/net/wifi/WifiStateMachine.java
@@ -49,8 +49,6 @@
 import android.net.LinkProperties;
 import android.os.Binder;
 import android.os.Message;
-import android.os.Parcelable;
-import android.os.Handler;
 import android.os.IBinder;
 import android.os.INetworkManagementService;
 import android.os.PowerManager;
@@ -93,8 +91,6 @@
  *
  * @hide
  */
-//TODO: we still need frequent scanning for the case when
-// we issue disconnect but need scan results for open network notification
 public class WifiStateMachine extends HierarchicalStateMachine {
 
     private static final String TAG = "WifiStateMachine";
@@ -120,11 +116,17 @@
     private String mLastBssid;
     private int mLastNetworkId;
     private boolean mEnableRssiPolling = false;
-    private boolean mPasswordKeyMayBeIncorrect = false;
+    private int mRssiPollToken = 0;
     private int mReconnectCount = 0;
     private boolean mIsScanMode = false;
 
     /**
+     * Interval in milliseconds between polling for RSSI
+     * and linkspeed information
+     */
+    private static final int POLL_RSSI_INTERVAL_MSECS = 3000;
+
+    /**
      * Instance of the bluetooth headset helper. This needs to be created
      * early because there is a delay before it actually 'connects', as
      * noted by its javadoc. If we check before it is connected, it will be
@@ -148,9 +150,6 @@
     /* Connection to a specific network involves disabling all networks,
      * this flag tracks if networks need to be re-enabled */
     private boolean mEnableAllNetworks = false;
-    /* Track if WPS was started since we need to re-enable networks
-     * and load configuration afterwards */
-    private boolean mWpsStarted = false;
 
     private AlarmManager mAlarmManager;
     private PendingIntent mScanIntent;
@@ -166,95 +165,95 @@
     private static final int EVENTLOG_SUPPLICANT_STATE_CHANGED  = 50023;
 
     /* Load the driver */
-    private static final int CMD_LOAD_DRIVER                      = 1;
+    static final int CMD_LOAD_DRIVER                      = 1;
     /* Unload the driver */
-    private static final int CMD_UNLOAD_DRIVER                    = 2;
+    static final int CMD_UNLOAD_DRIVER                    = 2;
     /* Indicates driver load succeeded */
-    private static final int CMD_LOAD_DRIVER_SUCCESS              = 3;
+    static final int CMD_LOAD_DRIVER_SUCCESS              = 3;
     /* Indicates driver load failed */
-    private static final int CMD_LOAD_DRIVER_FAILURE              = 4;
+    static final int CMD_LOAD_DRIVER_FAILURE              = 4;
     /* Indicates driver unload succeeded */
-    private static final int CMD_UNLOAD_DRIVER_SUCCESS            = 5;
+    static final int CMD_UNLOAD_DRIVER_SUCCESS            = 5;
     /* Indicates driver unload failed */
-    private static final int CMD_UNLOAD_DRIVER_FAILURE            = 6;
+    static final int CMD_UNLOAD_DRIVER_FAILURE            = 6;
     /* Set bluetooth headset proxy */
-    private static final int CMD_SET_BLUETOOTH_HEADSET_PROXY      = 7;
+    static final int CMD_SET_BLUETOOTH_HEADSET_PROXY      = 7;
     /* Set bluetooth A2dp proxy */
-    private static final int CMD_SET_BLUETOOTH_A2DP_PROXY         = 8;
+    static final int CMD_SET_BLUETOOTH_A2DP_PROXY         = 8;
 
     /* Start the supplicant */
-    private static final int CMD_START_SUPPLICANT                 = 11;
+    static final int CMD_START_SUPPLICANT                 = 11;
     /* Stop the supplicant */
-    private static final int CMD_STOP_SUPPLICANT                  = 12;
+    static final int CMD_STOP_SUPPLICANT                  = 12;
     /* Start the driver */
-    private static final int CMD_START_DRIVER                     = 13;
+    static final int CMD_START_DRIVER                     = 13;
     /* Start the driver */
-    private static final int CMD_STOP_DRIVER                      = 14;
+    static final int CMD_STOP_DRIVER                      = 14;
     /* Indicates DHCP succeded */
-    private static final int CMD_IP_CONFIG_SUCCESS                = 15;
+    static final int CMD_IP_CONFIG_SUCCESS                = 15;
     /* Indicates DHCP failed */
-    private static final int CMD_IP_CONFIG_FAILURE                = 16;
+    static final int CMD_IP_CONFIG_FAILURE                = 16;
     /* Re-configure interface */
-    private static final int CMD_RECONFIGURE_IP                   = 17;
+    static final int CMD_RECONFIGURE_IP                   = 17;
 
 
     /* Start the soft access point */
-    private static final int CMD_START_AP                         = 21;
+    static final int CMD_START_AP                         = 21;
     /* Stop the soft access point */
-    private static final int CMD_STOP_AP                          = 22;
+    static final int CMD_STOP_AP                          = 22;
 
 
     /* Supplicant events */
     /* Connection to supplicant established */
-    private static final int SUP_CONNECTION_EVENT                 = 31;
+    static final int SUP_CONNECTION_EVENT                 = 31;
     /* Connection to supplicant lost */
-    private static final int SUP_DISCONNECTION_EVENT              = 32;
+    static final int SUP_DISCONNECTION_EVENT              = 32;
     /* Driver start completed */
-    private static final int DRIVER_START_EVENT                   = 33;
+    static final int DRIVER_START_EVENT                   = 33;
     /* Driver stop completed */
-    private static final int DRIVER_STOP_EVENT                    = 34;
+    static final int DRIVER_STOP_EVENT                    = 34;
     /* Network connection completed */
-    private static final int NETWORK_CONNECTION_EVENT             = 36;
+    static final int NETWORK_CONNECTION_EVENT             = 36;
     /* Network disconnection completed */
-    private static final int NETWORK_DISCONNECTION_EVENT          = 37;
+    static final int NETWORK_DISCONNECTION_EVENT          = 37;
     /* Scan results are available */
-    private static final int SCAN_RESULTS_EVENT                   = 38;
+    static final int SCAN_RESULTS_EVENT                   = 38;
     /* Supplicate state changed */
-    private static final int SUPPLICANT_STATE_CHANGE_EVENT        = 39;
+    static final int SUPPLICANT_STATE_CHANGE_EVENT        = 39;
     /* Password may be incorrect */
-    private static final int PASSWORD_MAY_BE_INCORRECT_EVENT      = 40;
+    static final int PASSWORD_MAY_BE_INCORRECT_EVENT      = 40;
 
     /* Supplicant commands */
     /* Is supplicant alive ? */
-    private static final int CMD_PING_SUPPLICANT                  = 51;
+    static final int CMD_PING_SUPPLICANT                  = 51;
     /* Add/update a network configuration */
-    private static final int CMD_ADD_OR_UPDATE_NETWORK            = 52;
+    static final int CMD_ADD_OR_UPDATE_NETWORK            = 52;
     /* Delete a network */
-    private static final int CMD_REMOVE_NETWORK                   = 53;
+    static final int CMD_REMOVE_NETWORK                   = 53;
     /* Enable a network. The device will attempt a connection to the given network. */
-    private static final int CMD_ENABLE_NETWORK                   = 54;
+    static final int CMD_ENABLE_NETWORK                   = 54;
     /* Disable a network. The device does not attempt a connection to the given network. */
-    private static final int CMD_DISABLE_NETWORK                  = 55;
+    static final int CMD_DISABLE_NETWORK                  = 55;
     /* Blacklist network. De-prioritizes the given BSSID for connection. */
-    private static final int CMD_BLACKLIST_NETWORK                = 56;
+    static final int CMD_BLACKLIST_NETWORK                = 56;
     /* Clear the blacklist network list */
-    private static final int CMD_CLEAR_BLACKLIST                  = 57;
+    static final int CMD_CLEAR_BLACKLIST                  = 57;
     /* Save configuration */
-    private static final int CMD_SAVE_CONFIG                      = 58;
+    static final int CMD_SAVE_CONFIG                      = 58;
 
     /* Supplicant commands after driver start*/
     /* Initiate a scan */
-    private static final int CMD_START_SCAN                       = 71;
+    static final int CMD_START_SCAN                       = 71;
     /* Set scan mode. CONNECT_MODE or SCAN_ONLY_MODE */
-    private static final int CMD_SET_SCAN_MODE                    = 72;
+    static final int CMD_SET_SCAN_MODE                    = 72;
     /* Set scan type. SCAN_ACTIVE or SCAN_PASSIVE */
-    private static final int CMD_SET_SCAN_TYPE                    = 73;
+    static final int CMD_SET_SCAN_TYPE                    = 73;
     /* Disconnect from a network */
-    private static final int CMD_DISCONNECT                       = 74;
+    static final int CMD_DISCONNECT                       = 74;
     /* Reconnect to a network */
-    private static final int CMD_RECONNECT                        = 75;
+    static final int CMD_RECONNECT                        = 75;
     /* Reassociate to a network */
-    private static final int CMD_REASSOCIATE                      = 76;
+    static final int CMD_REASSOCIATE                      = 76;
     /* Controls power mode and suspend mode optimizations
      *
      * When high perf mode is enabled, power mode is set to
@@ -268,30 +267,30 @@
      * - turn off roaming
      * - DTIM wake up settings
      */
-    private static final int CMD_SET_HIGH_PERF_MODE               = 77;
+    static final int CMD_SET_HIGH_PERF_MODE               = 77;
     /* Set bluetooth co-existence
      * BLUETOOTH_COEXISTENCE_MODE_ENABLED
      * BLUETOOTH_COEXISTENCE_MODE_DISABLED
      * BLUETOOTH_COEXISTENCE_MODE_SENSE
      */
-    private static final int CMD_SET_BLUETOOTH_COEXISTENCE        = 78;
+    static final int CMD_SET_BLUETOOTH_COEXISTENCE        = 78;
     /* Enable/disable bluetooth scan mode
      * true(1)
      * false(0)
      */
-    private static final int CMD_SET_BLUETOOTH_SCAN_MODE          = 79;
+    static final int CMD_SET_BLUETOOTH_SCAN_MODE          = 79;
     /* Set the country code */
-    private static final int CMD_SET_COUNTRY_CODE                 = 80;
+    static final int CMD_SET_COUNTRY_CODE                 = 80;
     /* Request connectivity manager wake lock before driver stop */
-    private static final int CMD_REQUEST_CM_WAKELOCK              = 81;
+    static final int CMD_REQUEST_CM_WAKELOCK              = 81;
     /* Enables RSSI poll */
-    private static final int CMD_ENABLE_RSSI_POLL                 = 82;
+    static final int CMD_ENABLE_RSSI_POLL                 = 82;
     /* RSSI poll */
-    private static final int CMD_RSSI_POLL                        = 83;
+    static final int CMD_RSSI_POLL                        = 83;
     /* Set up packet filtering */
-    private static final int CMD_START_PACKET_FILTERING           = 84;
+    static final int CMD_START_PACKET_FILTERING           = 84;
     /* Clear packet filter */
-    private static final int CMD_STOP_PACKET_FILTERING            = 85;
+    static final int CMD_STOP_PACKET_FILTERING            = 85;
     /* Connect to a specified network (network id
      * or WifiConfiguration) This involves increasing
      * the priority of the network, enabling the network
@@ -300,32 +299,29 @@
      * an existing network. All the networks get enabled
      * upon a successful connection or a failure.
      */
-    private static final int CMD_CONNECT_NETWORK                  = 86;
+    static final int CMD_CONNECT_NETWORK                  = 86;
     /* Save the specified network. This involves adding
      * an enabled network (if new) and updating the
      * config and issuing a save on supplicant config.
      */
-    private static final int CMD_SAVE_NETWORK                     = 87;
+    static final int CMD_SAVE_NETWORK                     = 87;
     /* Delete the specified network. This involves
      * removing the network and issuing a save on
      * supplicant config.
      */
-    private static final int CMD_FORGET_NETWORK                   = 88;
+    static final int CMD_FORGET_NETWORK                   = 88;
     /* Start Wi-Fi protected setup push button configuration */
-    private static final int CMD_START_WPS_PBC                    = 89;
+    static final int CMD_START_WPS_PBC                    = 89;
     /* Start Wi-Fi protected setup pin method configuration with pin obtained from AP */
-    private static final int CMD_START_WPS_PIN_FROM_AP            = 90;
+    static final int CMD_START_WPS_PIN_FROM_AP            = 90;
     /* Start Wi-Fi protected setup pin method configuration with pin obtained from device */
-    private static final int CMD_START_WPS_PIN_FROM_DEVICE        = 91;
+    static final int CMD_START_WPS_PIN_FROM_DEVICE        = 91;
     /* Set the frequency band */
-    private static final int CMD_SET_FREQUENCY_BAND               = 92;
+    static final int CMD_SET_FREQUENCY_BAND               = 92;
 
-    /**
-     * Interval in milliseconds between polling for connection
-     * status items that are not sent via asynchronous events.
-     * An example is RSSI (signal strength).
-     */
-    private static final int POLL_RSSI_INTERVAL_MSECS = 3000;
+    /* Commands from the SupplicantStateTracker */
+    /* Indicates whether a wifi network is available for connection */
+    static final int CMD_SET_NETWORK_AVAILABLE             = 111;
 
     private static final int CONNECT_MODE   = 1;
     private static final int SCAN_ONLY_MODE = 2;
@@ -463,7 +459,7 @@
         mDhcpInfo = new DhcpInfo();
         mWifiInfo = new WifiInfo();
         mInterfaceName = SystemProperties.get("wifi.interface", "tiwlan0");
-        mSupplicantStateTracker = new SupplicantStateTracker(context, getHandler());
+        mSupplicantStateTracker = new SupplicantStateTracker(context, this, getHandler());
 
         mLinkProperties = new LinkProperties();
         BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
@@ -985,8 +981,6 @@
         sb.append("mLastBssid ").append(mLastBssid).append(LS);
         sb.append("mLastNetworkId ").append(mLastNetworkId).append(LS);
         sb.append("mEnableAllNetworks ").append(mEnableAllNetworks).append(LS);
-        sb.append("mEnableRssiPolling ").append(mEnableRssiPolling).append(LS);
-        sb.append("mPasswordKeyMayBeIncorrect ").append(mPasswordKeyMayBeIncorrect).append(LS);
         sb.append("mReconnectCount ").append(mReconnectCount).append(LS);
         sb.append("mIsScanMode ").append(mIsScanMode).append(LS);
         sb.append("Supplicant status").append(LS)
@@ -1215,6 +1209,44 @@
         return null;
     }
 
+    /*
+     * Fetch RSSI and linkspeed on current connection
+     */
+    private void fetchRssiAndLinkSpeedNative() {
+        int newRssi = WifiNative.getRssiCommand();
+        if (newRssi != -1 && -200 < newRssi && newRssi < 256) { // screen out invalid values
+            /* some implementations avoid negative values by adding 256
+             * so we need to adjust for that here.
+             */
+            if (newRssi > 0) newRssi -= 256;
+            mWifiInfo.setRssi(newRssi);
+            /*
+             * Rather then sending the raw RSSI out every time it
+             * changes, we precalculate the signal level that would
+             * be displayed in the status bar, and only send the
+             * broadcast if that much more coarse-grained number
+             * changes. This cuts down greatly on the number of
+             * broadcasts, at the cost of not mWifiInforming others
+             * interested in RSSI of all the changes in signal
+             * level.
+             */
+            // TODO: The second arg to the call below needs to be a symbol somewhere, but
+            // it's actually the size of an array of icons that's private
+            // to StatusBar Policy.
+            int newSignalLevel = WifiManager.calculateSignalLevel(newRssi, 4);
+            if (newSignalLevel != mLastSignalLevel) {
+                sendRssiChangeBroadcast(newRssi);
+            }
+            mLastSignalLevel = newSignalLevel;
+        } else {
+            mWifiInfo.setRssi(-200);
+        }
+        int newLinkSpeed = WifiNative.getLinkSpeedCommand();
+        if (newLinkSpeed != -1) {
+            mWifiInfo.setLinkSpeed(newLinkSpeed);
+        }
+    }
+
     private void setHighPerfModeEnabledNative(boolean enable) {
         if(!WifiNative.setSuspendOptimizationsCommand(!enable)) {
             Log.e(TAG, "set suspend optimizations failed!");
@@ -1336,19 +1368,6 @@
         mContext.sendBroadcast(intent);
     }
 
-    private void sendSupplicantStateChangedBroadcast(StateChangeResult sc, boolean failedAuth) {
-        Intent intent = new Intent(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
-        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
-                | Intent.FLAG_RECEIVER_REPLACE_PENDING);
-        intent.putExtra(WifiManager.EXTRA_NEW_STATE, (Parcelable)sc.state);
-        if (failedAuth) {
-            intent.putExtra(
-                WifiManager.EXTRA_SUPPLICANT_ERROR,
-                WifiManager.ERROR_AUTHENTICATING);
-        }
-        mContext.sendStickyBroadcast(intent);
-    }
-
     private void sendSupplicantConnectionChangedBroadcast(boolean connected) {
         if (!ActivityManagerNative.isSystemReady()) return;
 
@@ -1370,45 +1389,6 @@
     }
 
     /**
-     * Poll for info not reported via events
-     * RSSI & Linkspeed
-     */
-    private void requestPolledInfo() {
-        int newRssi = WifiNative.getRssiCommand();
-        if (newRssi != -1 && -200 < newRssi && newRssi < 256) { // screen out invalid values
-            /* some implementations avoid negative values by adding 256
-             * so we need to adjust for that here.
-             */
-            if (newRssi > 0) newRssi -= 256;
-            mWifiInfo.setRssi(newRssi);
-            /*
-             * Rather then sending the raw RSSI out every time it
-             * changes, we precalculate the signal level that would
-             * be displayed in the status bar, and only send the
-             * broadcast if that much more coarse-grained number
-             * changes. This cuts down greatly on the number of
-             * broadcasts, at the cost of not mWifiInforming others
-             * interested in RSSI of all the changes in signal
-             * level.
-             */
-            // TODO: The second arg to the call below needs to be a symbol somewhere, but
-            // it's actually the size of an array of icons that's private
-            // to StatusBar Policy.
-            int newSignalLevel = WifiManager.calculateSignalLevel(newRssi, 4);
-            if (newSignalLevel != mLastSignalLevel) {
-                sendRssiChangeBroadcast(newRssi);
-            }
-            mLastSignalLevel = newSignalLevel;
-        } else {
-            mWifiInfo.setRssi(-200);
-        }
-        int newLinkSpeed = WifiNative.getLinkSpeedCommand();
-        if (newLinkSpeed != -1) {
-            mWifiInfo.setLinkSpeed(newLinkSpeed);
-        }
-    }
-
-    /**
      * Resets the Wi-Fi Connections by clearing any state, resetting any sockets
      * using the interface, stopping DHCP & disabling interface
      */
@@ -1456,7 +1436,7 @@
      * WifiMonitor
      * thread.
      */
-    private static class StateChangeResult {
+    static class StateChangeResult {
         StateChangeResult(int networkId, String BSSID, Object state) {
             this.state = state;
             this.BSSID = BSSID;
@@ -1548,6 +1528,9 @@
         setWifiEnabled(true);
     }
 
+    void setNetworkAvailable(boolean available) {
+        sendMessage(CMD_SET_NETWORK_AVAILABLE, available ? 1 : 0);
+    }
 
     /********************************************************
      * HSM states
@@ -1576,7 +1559,9 @@
                     break;
                 case CMD_ENABLE_RSSI_POLL:
                     mEnableRssiPolling = (message.arg1 == 1);
-                    mSupplicantStateTracker.sendMessage(CMD_ENABLE_RSSI_POLL);
+                    break;
+                case CMD_SET_NETWORK_AVAILABLE:
+                    mNetworkInfo.setIsAvailable(message.arg1 == 1);
                     break;
                     /* Discard */
                 case CMD_LOAD_DRIVER:
@@ -1616,6 +1601,7 @@
                 case CMD_FORGET_NETWORK:
                 case CMD_START_WPS_PBC:
                 case CMD_START_WPS_PIN_FROM_AP:
+                case CMD_RSSI_POLL:
                     break;
                 default:
                     Log.e(TAG, "Error! unhandled message" + message);
@@ -2136,6 +2122,9 @@
                         WifiNative.setScanModeCommand(false);
                     }
                     break;
+                case CMD_START_SCAN:
+                    WifiNative.scanCommand(message.arg1 == SCAN_ACTIVE);
+                    break;
                 case CMD_SET_HIGH_PERF_MODE:
                     setHighPerfModeEnabledNative(message.arg1 == 1);
                     break;
@@ -2278,9 +2267,6 @@
                         transitionTo(mDisconnectedState);
                     }
                     break;
-                case CMD_START_SCAN:
-                    WifiNative.scanCommand(message.arg1 == SCAN_ACTIVE);
-                    break;
                     /* Ignore */
                 case CMD_DISCONNECT:
                 case CMD_RECONNECT:
@@ -2309,14 +2295,23 @@
             StateChangeResult stateChangeResult;
             switch(message.what) {
                 case PASSWORD_MAY_BE_INCORRECT_EVENT:
-                    mPasswordKeyMayBeIncorrect = true;
+                    mSupplicantStateTracker.sendMessage(PASSWORD_MAY_BE_INCORRECT_EVENT);
                     break;
                 case SUPPLICANT_STATE_CHANGE_EVENT:
                     stateChangeResult = (StateChangeResult) message.obj;
-                    mSupplicantStateTracker.handleEvent(stateChangeResult);
-                    break;
-                case CMD_START_SCAN:
-                    /* We need to set scan type in completed state */
+                    SupplicantState state = (SupplicantState) stateChangeResult.state;
+                    // Supplicant state change
+                    // [31-13] Reserved for future use
+                    // [8 - 0] Supplicant state (as defined in SupplicantState.java)
+                    // 50023 supplicant_state_changed (custom|1|5)
+                    EventLog.writeEvent(EVENTLOG_SUPPLICANT_STATE_CHANGED, state.ordinal());
+                    mWifiInfo.setSupplicantState(state);
+                    mWifiInfo.setNetworkId(stateChangeResult.networkId);
+                    if (state == SupplicantState.ASSOCIATING) {
+                        /* BSSID is valid only in ASSOCIATING state */
+                        mWifiInfo.setBSSID(stateChangeResult.BSSID);
+                    }
+
                     Message newMsg = obtainMessage();
                     newMsg.copyFrom(message);
                     mSupplicantStateTracker.sendMessage(newMsg);
@@ -2375,7 +2370,7 @@
                      * Upon success, the configuration list needs to be reloaded
                      */
                     if (success) {
-                        mWpsStarted = true;
+                        mSupplicantStateTracker.sendMessage(message.what);
                         /* Expect a disconnection from the old connection */
                         transitionTo(mDisconnectingState);
                     }
@@ -2388,7 +2383,7 @@
                     success = WifiConfigStore.startWpsWithPinFromAccessPoint(bssid, apPin);
 
                     if (success) {
-                        mWpsStarted = true;
+                        mSupplicantStateTracker.sendMessage(message.what);
                         /* Expect a disconnection from the old connection */
                         transitionTo(mDisconnectingState);
                     }
@@ -2400,7 +2395,7 @@
                     mReplyChannel.replyToMessage(message, CMD_START_WPS_PIN_FROM_DEVICE, pin);
 
                     if (success) {
-                        mWpsStarted = true;
+                        mSupplicantStateTracker.sendMessage(message.what);
                         /* Expect a disconnection from the old connection */
                         transitionTo(mDisconnectingState);
                     }
@@ -2583,6 +2578,10 @@
                       deferMessage(message);
                   }
                   break;
+                  /* Defer scan when IP is being fetched */
+              case CMD_START_SCAN:
+                  deferMessage(message);
+                  break;
               case CMD_RECONFIGURE_IP:
                   deferMessage(message);
                   break;
@@ -2619,13 +2618,11 @@
         @Override
         public void enter() {
             if (DBG) Log.d(TAG, getName() + "\n");
-            /* A successful WPS connection */
-            if (mWpsStarted) {
-                WifiConfigStore.enableAllNetworks();
-                WifiConfigStore.loadConfiguredNetworks();
-                mWpsStarted = false;
-            }
             EventLog.writeEvent(EVENTLOG_WIFI_STATE_CHANGED, getName());
+            mRssiPollToken++;
+            if (mEnableRssiPolling) {
+                sendMessage(obtainMessage(WifiStateMachine.CMD_RSSI_POLL, mRssiPollToken, 0));
+            }
         }
         @Override
         public boolean processMessage(Message message) {
@@ -2650,6 +2647,15 @@
                         deferMessage(message);
                     }
                     break;
+                case CMD_START_SCAN:
+                    /* When the network is connected, re-scanning can trigger
+                     * a reconnection. Put it in scan-only mode during scan.
+                     * When scan results are received, the mode is switched
+                     * back to CONNECT_MODE.
+                     */
+                    WifiNative.setScanResultHandlingCommand(SCAN_ONLY_MODE);
+                    WifiNative.scanCommand(message.arg1 == SCAN_ACTIVE);
+                    break;
                     /* Ignore connection to same network */
                 case CMD_CONNECT_NETWORK:
                     int netId = message.arg1;
@@ -2660,6 +2666,26 @@
                     /* Ignore */
                 case NETWORK_CONNECTION_EVENT:
                     break;
+                case CMD_RSSI_POLL:
+                    if (message.arg1 == mRssiPollToken) {
+                        // Get Info and continue polling
+                        fetchRssiAndLinkSpeedNative();
+                        sendMessageDelayed(obtainMessage(WifiStateMachine.CMD_RSSI_POLL,
+                                mRssiPollToken, 0), POLL_RSSI_INTERVAL_MSECS);
+                    } else {
+                        // Polling has completed
+                    }
+                    break;
+                case CMD_ENABLE_RSSI_POLL:
+                    mEnableRssiPolling = (message.arg1 == 1);
+                    mRssiPollToken++;
+                    if (mEnableRssiPolling) {
+                        // first poll
+                        fetchRssiAndLinkSpeedNative();
+                        sendMessageDelayed(obtainMessage(WifiStateMachine.CMD_RSSI_POLL,
+                                mRssiPollToken, 0), POLL_RSSI_INTERVAL_MSECS);
+                    }
+                    break;
                 default:
                     return NOT_HANDLED;
             }
@@ -2686,6 +2712,10 @@
                         deferMessage(message);
                     }
                     break;
+                    /* Handle in  DisconnectedState */
+                case SUPPLICANT_STATE_CHANGE_EVENT:
+                    deferMessage(message);
+                    break;
                 default:
                     return NOT_HANDLED;
             }
@@ -2734,6 +2764,12 @@
                     /* Ignore network disconnect */
                 case NETWORK_DISCONNECTION_EVENT:
                     break;
+                case SUPPLICANT_STATE_CHANGE_EVENT:
+                    StateChangeResult stateChangeResult = (StateChangeResult) message.obj;
+                    SupplicantState state = (SupplicantState) stateChangeResult.state;
+                    setDetailedState(WifiInfo.getDetailedStateOf(state));
+                    /* DriverStartedState does the rest of the handling */
+                    return NOT_HANDLED;
                 default:
                     return NOT_HANDLED;
             }
@@ -2795,375 +2831,4 @@
             return HANDLED;
         }
     }
-
-
-    class SupplicantStateTracker extends HierarchicalStateMachine {
-
-        private int mRssiPollToken = 0;
-
-        /**
-         * The max number of the WPA supplicant loop iterations before we
-         * decide that the loop should be terminated:
-         */
-        private static final int MAX_SUPPLICANT_LOOP_ITERATIONS = 4;
-        private int mLoopDetectIndex = 0;
-        private int mLoopDetectCount = 0;
-
-        /**
-         *  Supplicant state change commands follow
-         *  the ordinal values defined in SupplicantState.java
-         */
-        private static final int DISCONNECTED           = 0;
-        private static final int INACTIVE               = 1;
-        private static final int SCANNING               = 2;
-        private static final int ASSOCIATING            = 3;
-        private static final int ASSOCIATED             = 4;
-        private static final int FOUR_WAY_HANDSHAKE     = 5;
-        private static final int GROUP_HANDSHAKE        = 6;
-        private static final int COMPLETED              = 7;
-        private static final int DORMANT                = 8;
-        private static final int UNINITIALIZED          = 9;
-        private static final int INVALID                = 10;
-
-        private HierarchicalState mUninitializedState = new UninitializedState();
-        private HierarchicalState mInitializedState = new InitializedState();;
-        private HierarchicalState mInactiveState = new InactiveState();
-        private HierarchicalState mDisconnectState = new DisconnectedState();
-        private HierarchicalState mScanState = new ScanState();
-        private HierarchicalState mConnectState = new ConnectState();
-        private HierarchicalState mHandshakeState = new HandshakeState();
-        private HierarchicalState mCompletedState = new CompletedState();
-        private HierarchicalState mDormantState = new DormantState();
-
-        public SupplicantStateTracker(Context context, Handler target) {
-            super(TAG, target.getLooper());
-
-            addState(mUninitializedState);
-            addState(mInitializedState);
-                addState(mInactiveState, mInitializedState);
-                addState(mDisconnectState, mInitializedState);
-                addState(mScanState, mInitializedState);
-                addState(mConnectState, mInitializedState);
-                    addState(mHandshakeState, mConnectState);
-                    addState(mCompletedState, mConnectState);
-                addState(mDormantState, mInitializedState);
-
-            setInitialState(mUninitializedState);
-
-            //start the state machine
-            start();
-        }
-
-        public void handleEvent(StateChangeResult stateChangeResult) {
-            SupplicantState newState = (SupplicantState) stateChangeResult.state;
-
-            // Supplicant state change
-            // [31-13] Reserved for future use
-            // [8 - 0] Supplicant state (as defined in SupplicantState.java)
-            // 50023 supplicant_state_changed (custom|1|5)
-            EventLog.writeEvent(EVENTLOG_SUPPLICANT_STATE_CHANGED, newState.ordinal());
-
-            sendMessage(obtainMessage(newState.ordinal(), stateChangeResult));
-        }
-
-        public void resetSupplicantState() {
-            transitionTo(mUninitializedState);
-        }
-
-        private void resetLoopDetection() {
-            mLoopDetectCount = 0;
-            mLoopDetectIndex = 0;
-        }
-
-        private boolean handleTransition(Message msg) {
-            if (DBG) Log.d(TAG, getName() + msg.toString() + "\n");
-            switch (msg.what) {
-                case DISCONNECTED:
-                    transitionTo(mDisconnectState);
-                    break;
-                case SCANNING:
-                    transitionTo(mScanState);
-                    break;
-                case ASSOCIATING:
-                    StateChangeResult stateChangeResult = (StateChangeResult) msg.obj;
-                    /* BSSID is valid only in ASSOCIATING state */
-                    mWifiInfo.setBSSID(stateChangeResult.BSSID);
-                    //$FALL-THROUGH$
-                case ASSOCIATED:
-                case FOUR_WAY_HANDSHAKE:
-                case GROUP_HANDSHAKE:
-                    transitionTo(mHandshakeState);
-                    break;
-                case COMPLETED:
-                    transitionTo(mCompletedState);
-                    break;
-                case DORMANT:
-                    transitionTo(mDormantState);
-                    break;
-                case INACTIVE:
-                    transitionTo(mInactiveState);
-                    break;
-                case UNINITIALIZED:
-                case INVALID:
-                    transitionTo(mUninitializedState);
-                    break;
-                default:
-                    return NOT_HANDLED;
-            }
-            StateChangeResult stateChangeResult = (StateChangeResult) msg.obj;
-            SupplicantState supState = (SupplicantState) stateChangeResult.state;
-            setDetailedState(WifiInfo.getDetailedStateOf(supState));
-            mWifiInfo.setSupplicantState(supState);
-            mWifiInfo.setNetworkId(stateChangeResult.networkId);
-            return HANDLED;
-        }
-
-        /********************************************************
-         * HSM states
-         *******************************************************/
-
-        class InitializedState extends HierarchicalState {
-            @Override
-             public void enter() {
-                 if (DBG) Log.d(TAG, getName() + "\n");
-             }
-            @Override
-            public boolean processMessage(Message message) {
-                if (DBG) Log.d(TAG, getName() + message.toString() + "\n");
-                switch (message.what) {
-                    case CMD_START_SCAN:
-                        WifiNative.scanCommand(message.arg1 == SCAN_ACTIVE);
-                        break;
-                    default:
-                        if (DBG) Log.w(TAG, "Ignoring " + message);
-                        break;
-                }
-                return HANDLED;
-            }
-        }
-
-        class UninitializedState extends HierarchicalState {
-            @Override
-             public void enter() {
-                 if (DBG) Log.d(TAG, getName() + "\n");
-                 mNetworkInfo.setIsAvailable(false);
-                 resetLoopDetection();
-                 mPasswordKeyMayBeIncorrect = false;
-             }
-            @Override
-            public boolean processMessage(Message message) {
-                switch(message.what) {
-                    default:
-                        if (!handleTransition(message)) {
-                            if (DBG) Log.w(TAG, "Ignoring " + message);
-                        }
-                        break;
-                }
-                return HANDLED;
-            }
-            @Override
-            public void exit() {
-                mNetworkInfo.setIsAvailable(true);
-            }
-        }
-
-        class InactiveState extends HierarchicalState {
-            @Override
-             public void enter() {
-                 if (DBG) Log.d(TAG, getName() + "\n");
-
-                 /* A failed WPS connection */
-                 if (mWpsStarted) {
-                     Log.e(TAG, "WPS set up failed, enabling other networks");
-                     WifiConfigStore.enableAllNetworks();
-                     mWpsStarted = false;
-                 }
-
-                 Message message = getCurrentMessage();
-                 StateChangeResult stateChangeResult = (StateChangeResult) message.obj;
-
-                 mNetworkInfo.setIsAvailable(false);
-                 resetLoopDetection();
-                 mPasswordKeyMayBeIncorrect = false;
-
-                 sendSupplicantStateChangedBroadcast(stateChangeResult, false);
-             }
-            @Override
-            public boolean processMessage(Message message) {
-                return handleTransition(message);
-            }
-            @Override
-            public void exit() {
-                mNetworkInfo.setIsAvailable(true);
-            }
-        }
-
-
-        class DisconnectedState extends HierarchicalState {
-            @Override
-             public void enter() {
-                 if (DBG) Log.d(TAG, getName() + "\n");
-                 Message message = getCurrentMessage();
-                 StateChangeResult stateChangeResult = (StateChangeResult) message.obj;
-
-                 resetLoopDetection();
-
-                 /* If a disconnect event happens after a password key failure
-                  * event, disable the network
-                  */
-                 if (mPasswordKeyMayBeIncorrect) {
-                     Log.d(TAG, "Failed to authenticate, disabling network " +
-                             mWifiInfo.getNetworkId());
-                     WifiConfigStore.disableNetwork(mWifiInfo.getNetworkId());
-                     mPasswordKeyMayBeIncorrect = false;
-                     sendSupplicantStateChangedBroadcast(stateChangeResult, true);
-                 }
-                 else {
-                     sendSupplicantStateChangedBroadcast(stateChangeResult, false);
-                 }
-             }
-            @Override
-            public boolean processMessage(Message message) {
-                return handleTransition(message);
-            }
-        }
-
-        class ScanState extends HierarchicalState {
-            @Override
-             public void enter() {
-                 if (DBG) Log.d(TAG, getName() + "\n");
-                 Message message = getCurrentMessage();
-                 StateChangeResult stateChangeResult = (StateChangeResult) message.obj;
-
-                 mPasswordKeyMayBeIncorrect = false;
-                 resetLoopDetection();
-                 sendSupplicantStateChangedBroadcast(stateChangeResult, false);
-             }
-            @Override
-            public boolean processMessage(Message message) {
-                return handleTransition(message);
-            }
-        }
-
-        class ConnectState extends HierarchicalState {
-            @Override
-             public void enter() {
-                 if (DBG) Log.d(TAG, getName() + "\n");
-             }
-            @Override
-            public boolean processMessage(Message message) {
-                switch (message.what) {
-                    case CMD_START_SCAN:
-                        WifiNative.setScanResultHandlingCommand(SCAN_ONLY_MODE);
-                        WifiNative.scanCommand(message.arg1 == SCAN_ACTIVE);
-                        break;
-                    default:
-                        return NOT_HANDLED;
-                }
-                return HANDLED;
-            }
-        }
-
-        class HandshakeState extends HierarchicalState {
-            @Override
-             public void enter() {
-                 if (DBG) Log.d(TAG, getName() + "\n");
-                 final Message message = getCurrentMessage();
-                 StateChangeResult stateChangeResult = (StateChangeResult) message.obj;
-
-                 if (mLoopDetectIndex > message.what) {
-                     mLoopDetectCount++;
-                 }
-                 if (mLoopDetectCount > MAX_SUPPLICANT_LOOP_ITERATIONS) {
-                     WifiConfigStore.disableNetwork(stateChangeResult.networkId);
-                     mLoopDetectCount = 0;
-                 }
-
-                 mLoopDetectIndex = message.what;
-
-                 mPasswordKeyMayBeIncorrect = false;
-                 sendSupplicantStateChangedBroadcast(stateChangeResult, false);
-             }
-            @Override
-            public boolean processMessage(Message message) {
-                return handleTransition(message);
-            }
-        }
-
-        class CompletedState extends HierarchicalState {
-            @Override
-             public void enter() {
-                 if (DBG) Log.d(TAG, getName() + "\n");
-                 Message message = getCurrentMessage();
-                 StateChangeResult stateChangeResult = (StateChangeResult) message.obj;
-
-                 mRssiPollToken++;
-                 if (mEnableRssiPolling) {
-                     sendMessageDelayed(obtainMessage(CMD_RSSI_POLL, mRssiPollToken, 0),
-                             POLL_RSSI_INTERVAL_MSECS);
-                 }
-
-                 resetLoopDetection();
-
-                 mPasswordKeyMayBeIncorrect = false;
-                 sendSupplicantStateChangedBroadcast(stateChangeResult, false);
-             }
-            @Override
-            public boolean processMessage(Message message) {
-                switch(message.what) {
-                    case ASSOCIATING:
-                    case ASSOCIATED:
-                    case FOUR_WAY_HANDSHAKE:
-                    case GROUP_HANDSHAKE:
-                    case COMPLETED:
-                        break;
-                    case CMD_RSSI_POLL:
-                        if (message.arg1 == mRssiPollToken) {
-                            // Get Info and continue polling
-                            requestPolledInfo();
-                            sendMessageDelayed(obtainMessage(CMD_RSSI_POLL, mRssiPollToken, 0),
-                                    POLL_RSSI_INTERVAL_MSECS);
-                        } else {
-                            // Polling has completed
-                        }
-                        break;
-                    case CMD_ENABLE_RSSI_POLL:
-                        mRssiPollToken++;
-                        if (mEnableRssiPolling) {
-                            // first poll
-                            requestPolledInfo();
-                            sendMessageDelayed(obtainMessage(CMD_RSSI_POLL, mRssiPollToken, 0),
-                                    POLL_RSSI_INTERVAL_MSECS);
-                        }
-                        break;
-                    default:
-                        return handleTransition(message);
-                }
-                return HANDLED;
-            }
-        }
-
-        class DormantState extends HierarchicalState {
-            @Override
-            public void enter() {
-                if (DBG) Log.d(TAG, getName() + "\n");
-                Message message = getCurrentMessage();
-                StateChangeResult stateChangeResult = (StateChangeResult) message.obj;
-
-                resetLoopDetection();
-                mPasswordKeyMayBeIncorrect = false;
-
-                sendSupplicantStateChangedBroadcast(stateChangeResult, false);
-
-                /* TODO: reconnect is now being handled at DHCP failure handling
-                 * If we run into issues with staying in Dormant state, might
-                 * need a reconnect here
-                 */
-            }
-            @Override
-            public boolean processMessage(Message message) {
-                return handleTransition(message);
-            }
-        }
-    }
 }