Merge "Fix a key repeating bug."
diff --git a/api/current.xml b/api/current.xml
index 1e6fa92..7309429 100644
--- a/api/current.xml
+++ b/api/current.xml
@@ -19457,9 +19457,41 @@
 >
 <parameter name="tab" type="android.app.ActionBar.Tab">
 </parameter>
+<parameter name="setSelected" type="boolean">
+</parameter>
+</method>
+<method name="addTab"
+ return="void"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="tab" type="android.app.ActionBar.Tab">
+</parameter>
 <parameter name="position" type="int">
 </parameter>
 </method>
+<method name="addTab"
+ return="void"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="tab" type="android.app.ActionBar.Tab">
+</parameter>
+<parameter name="position" type="int">
+</parameter>
+<parameter name="setSelected" type="boolean">
+</parameter>
+</method>
 <method name="getCustomNavigationView"
  return="android.view.View"
  abstract="true"
diff --git a/core/java/android/app/ActionBar.java b/core/java/android/app/ActionBar.java
index b359ce6..e185624 100644
--- a/core/java/android/app/ActionBar.java
+++ b/core/java/android/app/ActionBar.java
@@ -397,14 +397,24 @@
 
     /**
      * Add a tab for use in tabbed navigation mode. The tab will be added at the end of the list.
+     * If this is the first tab to be added it will become the selected tab.
      *
      * @param tab Tab to add
      */
     public abstract void addTab(Tab tab);
 
     /**
+     * Add a tab for use in tabbed navigation mode. The tab will be added at the end of the list.
+     *
+     * @param tab Tab to add
+     * @param setSelected True if the added tab should become the selected tab.
+     */
+    public abstract void addTab(Tab tab, boolean setSelected);
+
+    /**
      * Add a tab for use in tabbed navigation mode. The tab will be inserted at
-     * <code>position</code>.
+     * <code>position</code>. If this is the first tab to be added it will become
+     * the selected tab.
      *
      * @param tab The tab to add
      * @param position The new position of the tab
@@ -412,6 +422,16 @@
     public abstract void addTab(Tab tab, int position);
 
     /**
+     * Add a tab for use in tabbed navigation mode. The tab will be insterted at
+     * <code>position</code>.
+     *
+     * @param tab The tab to add
+     * @param position The new position of the tab
+     * @param setSelected True if the added tab should become the selected tab.
+     */
+    public abstract void addTab(Tab tab, int position, boolean setSelected);
+
+    /**
      * Remove a tab from the action bar. If the removed tab was selected it will be deselected
      * and another tab will be selected if present.
      *
diff --git a/core/java/com/android/internal/app/ActionBarImpl.java b/core/java/com/android/internal/app/ActionBarImpl.java
index f4a041c..1d612e2 100644
--- a/core/java/com/android/internal/app/ActionBarImpl.java
+++ b/core/java/com/android/internal/app/ActionBarImpl.java
@@ -255,7 +255,6 @@
 
     private void configureTab(Tab tab, int position) {
         final TabImpl tabi = (TabImpl) tab;
-        final boolean isFirstTab = mTabs.isEmpty();
         final ActionBar.TabListener callback = tabi.getCallback();
 
         if (callback == null) {
@@ -265,26 +264,38 @@
         tabi.setPosition(position);
         mTabs.add(position, tabi);
 
-        if (isFirstTab) {
-            final FragmentTransaction trans = mActivity.getFragmentManager().openTransaction();
-            mSelectedTab = tabi;
-            callback.onTabSelected(tab, trans);
-            if (!trans.isEmpty()) {
-                trans.commit();
-            }
+        final int count = mTabs.size();
+        for (int i = position + 1; i < count; i++) {
+            mTabs.get(i).setPosition(i);
         }
     }
 
     @Override
     public void addTab(Tab tab) {
-        mActionView.addTab(tab);
-        configureTab(tab, mTabs.size());
+        addTab(tab, mTabs.isEmpty());
     }
 
     @Override
     public void addTab(Tab tab, int position) {
-        mActionView.addTab(tab, position);
+        addTab(tab, position, mTabs.isEmpty());
+    }
+
+    @Override
+    public void addTab(Tab tab, boolean setSelected) {
+        mActionView.addTab(tab, setSelected);
+        configureTab(tab, mTabs.size());
+        if (setSelected) {
+            selectTab(tab);
+        }
+    }
+
+    @Override
+    public void addTab(Tab tab, int position, boolean setSelected) {
+        mActionView.addTab(tab, position, setSelected);
         configureTab(tab, position);
+        if (setSelected) {
+            selectTab(tab);
+        }
     }
 
     @Override
diff --git a/core/java/com/android/internal/widget/ActionBarView.java b/core/java/com/android/internal/widget/ActionBarView.java
index be96e48..15d42cb 100644
--- a/core/java/com/android/internal/widget/ActionBarView.java
+++ b/core/java/com/android/internal/widget/ActionBarView.java
@@ -468,22 +468,20 @@
         return tabView;
     }
 
-    public void addTab(ActionBar.Tab tab) {
+    public void addTab(ActionBar.Tab tab, boolean setSelected) {
         ensureTabsExist();
-        final boolean isFirst = mTabLayout.getChildCount() == 0;
         View tabView = createTabView(tab);
         mTabLayout.addView(tabView);
-        if (isFirst) {
+        if (setSelected) {
             tabView.setSelected(true);
         }
     }
 
-    public void addTab(ActionBar.Tab tab, int position) {
+    public void addTab(ActionBar.Tab tab, int position, boolean setSelected) {
         ensureTabsExist();
-        final boolean isFirst = mTabLayout.getChildCount() == 0;
         final TabView tabView = createTabView(tab);
         mTabLayout.addView(tabView, position);
-        if (isFirst) {
+        if (setSelected) {
             tabView.setSelected(true);
         }
     }