Setting tab bar widths automatically

- will make launcher adapt better to different screen sizes
- also, moved customization tray tab setup code from Launcher to CustomizeTrayTabHost

Change-Id: I27a1acdacd231150bf191548d155c0e94d855796
diff --git a/src/com/android/launcher2/AllAppsPagedView.java b/src/com/android/launcher2/AllAppsPagedView.java
index b9b38c3..87d255e 100644
--- a/src/com/android/launcher2/AllAppsPagedView.java
+++ b/src/com/android/launcher2/AllAppsPagedView.java
@@ -65,6 +65,7 @@
     private final LayoutInflater mInflater;
     private boolean mAllowHardwareLayerCreation;
 
+    private int mPageContentWidth;
 
     public AllAppsPagedView(Context context) {
         this(context, null);
@@ -88,6 +89,11 @@
         Resources r = context.getResources();
         setDragSlopeThreshold(
                 r.getInteger(R.integer.config_allAppsDrawerDragSlopeThreshold) / 100.0f);
+
+        // Create a dummy page and set it up to find out the content width (used by our parent)
+        PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
+        setupPage(layout);
+        mPageContentWidth = layout.getContentWidth();
     }
 
     @Override
@@ -318,6 +324,10 @@
         mLauncher.unlockScreenOrientation();
     }
 
+    int getPageContentWidth() {
+        return mPageContentWidth;
+    }
+
     @Override
     public boolean isVisible() {
         return mZoom > 0.001f;
@@ -440,6 +450,13 @@
         // do nothing?
     }
 
+    private void setupPage(PagedViewCellLayout layout) {
+        layout.setCellCount(mCellCountX, mCellCountY);
+        layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
+                mPageLayoutPaddingBottom);
+        layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
+    }
+
     @Override
     public void syncPages() {
         // ensure that we have the right number of pages (min of 1, since we have placeholders)
@@ -449,7 +466,6 @@
         // remove any extra pages after the "last" page
         int extraPageDiff = curNumPages - numPages;
         for (int i = 0; i < extraPageDiff; ++i) {
-            PagedViewCellLayout page = (PagedViewCellLayout) getChildAt(numPages);
             removeViewAt(numPages);
         }
         // add any necessary pages
@@ -458,10 +474,7 @@
             if (mAllowHardwareLayerCreation) {
                 layout.allowHardwareLayerCreation();
             }
-            layout.setCellCount(mCellCountX, mCellCountY);
-            layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
-                    mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
-            layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
+            setupPage(layout);
             addView(layout);
         }
 
diff --git a/src/com/android/launcher2/AllAppsTabbed.java b/src/com/android/launcher2/AllAppsTabbed.java
index 0dd56ac..ee7bfc0 100644
--- a/src/com/android/launcher2/AllAppsTabbed.java
+++ b/src/com/android/launcher2/AllAppsTabbed.java
@@ -29,12 +29,9 @@
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
 import android.view.View;
-import android.view.ViewGroup;
 import android.widget.TabHost;
 import android.widget.TabWidget;
 import android.widget.TextView;
-import android.widget.TabHost.OnTabChangeListener;
-import android.widget.TabHost.TabContentFactory;
 
 import java.util.ArrayList;
 
@@ -82,6 +79,7 @@
             }
         };
 
+        // Create the tabs and wire them up properly
         TextView tabView;
         TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
         tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
@@ -119,6 +117,17 @@
             }
         });
 
+        // Set the width of the tab bar properly
+        int pageWidth = mAllApps.getPageContentWidth();
+        View allAppsTabBar = (View) findViewById(R.id.all_apps_tab_bar);
+        if (allAppsTabBar == null) throw new Resources.NotFoundException();
+        int tabWidgetPadding = 0;
+        final int childCount = tabWidget.getChildCount();
+        if (childCount > 0) {
+            tabWidgetPadding += tabWidget.getChildAt(0).getPaddingLeft() * 2;
+        }
+        allAppsTabBar.getLayoutParams().width = pageWidth + tabWidgetPadding;
+
         // It needs to be INVISIBLE so that it will be measured in the layout.
         // Otherwise the animations is messed up when we show it for the first time.
         setVisibility(INVISIBLE);
diff --git a/src/com/android/launcher2/CustomizePagedView.java b/src/com/android/launcher2/CustomizePagedView.java
index 332f6e4..3dfa7c2 100644
--- a/src/com/android/launcher2/CustomizePagedView.java
+++ b/src/com/android/launcher2/CustomizePagedView.java
@@ -140,6 +140,8 @@
 
     private int[] mDragViewOrigin = new int[2];
 
+    private int mPageContentWidth;
+
     public CustomizePagedView(Context context) {
         this(context, null, 0);
     }
@@ -172,6 +174,11 @@
                 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f);
         mMinPageWidth = r.getDimensionPixelSize(R.dimen.customization_drawer_content_min_width);
 
+        // Create a dummy page and set it up to find out the content width (used by our parent)
+        PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
+        setupPage(layout);
+        mPageContentWidth = layout.getContentWidth();
+
         setVisibility(View.GONE);
         setSoundEffectsEnabled(false);
         setupWorkspaceLayout();
@@ -1163,6 +1170,10 @@
         }
     }
 
+    int getPageContentWidth() {
+        return mPageContentWidth;
+    }
+
     @Override
     protected int getAssociatedLowerPageBound(int page) {
         return 0;
diff --git a/src/com/android/launcher2/CustomizeTrayTabHost.java b/src/com/android/launcher2/CustomizeTrayTabHost.java
index 76cfc84..3e04025 100644
--- a/src/com/android/launcher2/CustomizeTrayTabHost.java
+++ b/src/com/android/launcher2/CustomizeTrayTabHost.java
@@ -16,20 +16,111 @@
 
 package com.android.launcher2;
 
-import android.animation.Animator;
-import android.content.Context;
-import android.util.AttributeSet;
-import android.widget.TabHost;
+import com.android.launcher.R;
+import com.android.launcher2.CustomizePagedView.CustomizationType;
 
-public class CustomizeTrayTabHost extends TabHost implements LauncherTransitionable {
+import android.animation.Animator;
+import android.animation.AnimatorListenerAdapter;
+import android.animation.ObjectAnimator;
+import android.animation.ValueAnimator;
+import android.content.Context;
+import android.content.res.Resources;
+import android.util.AttributeSet;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.widget.TabHost;
+import android.widget.TabWidget;
+import android.widget.TextView;
+
+public class CustomizeTrayTabHost extends TabHost implements LauncherTransitionable  {
+    // tags for the customization tabs
+    private static final String WIDGETS_TAG = "widgets";
+    private static final String APPLICATIONS_TAG = "applications";
+    private static final String SHORTCUTS_TAG = "shortcuts";
+    private static final String WALLPAPERS_TAG = "wallpapers";
+
     private boolean mFirstLayout = true;
 
-    public CustomizeTrayTabHost(Context context) {
-        super(context);
-    }
+    private final LayoutInflater mInflater;
+    private Context mContext;
 
     public CustomizeTrayTabHost(Context context, AttributeSet attrs) {
         super(context, attrs);
+        mContext = context;
+        mInflater = LayoutInflater.from(context);
+    }
+
+    @Override
+    protected void onFinishInflate() {
+        setup();
+
+        final CustomizePagedView customizePagedView =
+            (CustomizePagedView) findViewById(R.id.customization_drawer_tab_contents);
+
+        // Configure tabs
+        TabContentFactory contentFactory = new TabContentFactory() {
+            public View createTabContent(String tag) {
+                return customizePagedView;
+            }
+        };
+
+        TextView tabView;
+        TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
+
+        tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
+        tabView.setText(mContext.getString(R.string.widgets_tab_label));
+        addTab(newTabSpec(WIDGETS_TAG).setIndicator(tabView).setContent(contentFactory));
+        tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
+        tabView.setText(mContext.getString(R.string.applications_tab_label));
+        addTab(newTabSpec(APPLICATIONS_TAG)
+                .setIndicator(tabView).setContent(contentFactory));
+        tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
+        tabView.setText(mContext.getString(R.string.wallpapers_tab_label));
+        addTab(newTabSpec(WALLPAPERS_TAG)
+                .setIndicator(tabView).setContent(contentFactory));
+        tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
+        tabView.setText(mContext.getString(R.string.shortcuts_tab_label));
+        addTab(newTabSpec(SHORTCUTS_TAG)
+                .setIndicator(tabView).setContent(contentFactory));
+        setOnTabChangedListener(new OnTabChangeListener() {
+            public void onTabChanged(String tabId) {
+                final CustomizePagedView.CustomizationType newType =
+                    getCustomizeFilterForTabTag(tabId);
+                if (newType != customizePagedView.getCustomizationFilter()) {
+                    // animate the changing of the tab content by fading pages in and out
+                    final Resources res = getResources();
+                    final int duration = res.getInteger(R.integer.config_tabTransitionTime);
+                    final float alpha = customizePagedView.getAlpha();
+                    ValueAnimator alphaAnim = ObjectAnimator.ofFloat(customizePagedView,
+                            "alpha", alpha, 0.0f);
+                    alphaAnim.setDuration(duration);
+                    alphaAnim.addListener(new AnimatorListenerAdapter() {
+                        @Override
+                        public void onAnimationEnd(Animator animation) {
+                            customizePagedView.setCustomizationFilter(newType);
+
+                            final float alpha = customizePagedView.getAlpha();
+                            ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
+                                    customizePagedView, "alpha", alpha, 1.0f);
+                            alphaAnim.setDuration(duration);
+                            alphaAnim.start();
+                        }
+                    });
+                    alphaAnim.start();
+                }
+            }
+        });
+
+        // Set the width of the tab bar properly
+        int pageWidth = customizePagedView.getPageContentWidth();
+        TabWidget customizeTabBar = (TabWidget) findViewById(com.android.internal.R.id.tabs);
+        if (customizeTabBar == null) throw new Resources.NotFoundException();
+        int tabWidgetPadding = 0;
+        final int childCount = tabWidget.getChildCount();
+        if (childCount > 0) {
+            tabWidgetPadding += tabWidget.getChildAt(0).getPaddingLeft() * 2;
+        }
+        customizeTabBar.getLayoutParams().width = pageWidth + tabWidgetPadding;
     }
 
     @Override
@@ -57,4 +148,17 @@
         mFirstLayout = false;
         super.onLayout(changed, l, t, r, b);
     }
+
+    CustomizationType getCustomizeFilterForTabTag(String tag) {
+        if (tag.equals(WIDGETS_TAG)) {
+            return CustomizationType.WidgetCustomization;
+        } else if (tag.equals(APPLICATIONS_TAG)) {
+            return CustomizationType.ApplicationCustomization;
+        } else if (tag.equals(WALLPAPERS_TAG)) {
+            return CustomizePagedView.CustomizationType.WallpaperCustomization;
+        } else if (tag.equals(SHORTCUTS_TAG)) {
+            return CustomizePagedView.CustomizationType.ShortcutCustomization;
+        }
+        return CustomizationType.WidgetCustomization;
+    }
 }
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index 64cbee4..f74710e 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -19,13 +19,11 @@
 
 import com.android.common.Search;
 import com.android.launcher.R;
-import com.android.launcher2.CustomizePagedView.CustomizationType;
 import com.android.launcher2.Workspace.ShrinkState;
 
 import android.animation.Animator;
 import android.animation.AnimatorListenerAdapter;
 import android.animation.AnimatorSet;
-import android.animation.ObjectAnimator;
 import android.animation.ValueAnimator;
 import android.animation.ValueAnimator.AnimatorUpdateListener;
 import android.app.Activity;
@@ -98,11 +96,8 @@
 import android.widget.LinearLayout;
 import android.widget.PopupWindow;
 import android.widget.TabHost;
-import android.widget.TabWidget;
 import android.widget.TextView;
 import android.widget.Toast;
-import android.widget.TabHost.OnTabChangeListener;
-import android.widget.TabHost.TabContentFactory;
 
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
@@ -172,12 +167,6 @@
     // Type: long
     private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
 
-    // tags for the customization tabs
-    private static final String WIDGETS_TAG = "widgets";
-    private static final String APPLICATIONS_TAG = "applications";
-    private static final String SHORTCUTS_TAG = "shortcuts";
-    private static final String WALLPAPERS_TAG = "wallpapers";
-
     private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
 
     /** The different states that Launcher can be in. */
@@ -214,7 +203,7 @@
     private DeleteZone mDeleteZone;
     private HandleView mHandleView;
     private AllAppsView mAllAppsGrid;
-    private TabHost mHomeCustomizationDrawer;
+    private CustomizeTrayTabHost mHomeCustomizationDrawer;
     private boolean mAutoAdvanceRunning = false;
 
     private View mButtonCluster;
@@ -293,19 +282,6 @@
         int cellY;
     }
 
-    private CustomizationType getCustomizeFilterForTabTag(String tag) {
-        if (tag.equals(WIDGETS_TAG)) {
-            return CustomizationType.WidgetCustomization;
-        } else if (tag.equals(APPLICATIONS_TAG)) {
-            return CustomizationType.ApplicationCustomization;
-        } else if (tag.equals(WALLPAPERS_TAG)) {
-            return CustomizePagedView.CustomizationType.WallpaperCustomization;
-        } else if (tag.equals(SHORTCUTS_TAG)) {
-            return CustomizePagedView.CustomizationType.ShortcutCustomization;
-        }
-        return CustomizationType.WidgetCustomization;
-    }
-
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -334,68 +310,12 @@
         loadHotseats();
         checkForLocaleChange();
         setContentView(R.layout.launcher);
-        mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
+        mHomeCustomizationDrawer = (CustomizeTrayTabHost) findViewById(R.id.customization_drawer);
         if (mHomeCustomizationDrawer != null) {
-            mHomeCustomizationDrawer.setup();
-
             // share the same customization workspace across all the tabs
-            mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
-                    R.layout.customization_drawer_tab_contents, mHomeCustomizationDrawer, false);
-            TabContentFactory contentFactory = new TabContentFactory() {
-                public View createTabContent(String tag) {
-                    return mCustomizePagedView;
-                }
-            };
+            mCustomizePagedView = (CustomizePagedView) findViewById(
+                    R.id.customization_drawer_tab_contents);
 
-
-            TextView tabView;
-            TabWidget tabWidget = (TabWidget)
-                    mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
-
-            tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
-            tabView.setText(getString(R.string.widgets_tab_label));
-            mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
-                    .setIndicator(tabView).setContent(contentFactory));
-            tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
-            tabView.setText(getString(R.string.applications_tab_label));
-            mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
-                    .setIndicator(tabView).setContent(contentFactory));
-            tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
-            tabView.setText(getString(R.string.wallpapers_tab_label));
-            mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
-                    .setIndicator(tabView).setContent(contentFactory));
-            tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
-            tabView.setText(getString(R.string.shortcuts_tab_label));
-            mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
-                    .setIndicator(tabView).setContent(contentFactory));
-            mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
-                public void onTabChanged(String tabId) {
-                    final CustomizePagedView.CustomizationType newType =
-                        getCustomizeFilterForTabTag(tabId);
-                    if (newType != mCustomizePagedView.getCustomizationFilter()) {
-                        // animate the changing of the tab content by fading pages in and out
-                        final Resources res = getResources();
-                        final int duration = res.getInteger(R.integer.config_tabTransitionTime);
-                        final float alpha = mCustomizePagedView.getAlpha();
-                        ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
-                                "alpha", alpha, 0.0f);
-                        alphaAnim.setDuration(duration);
-                        alphaAnim.addListener(new AnimatorListenerAdapter() {
-                            @Override
-                            public void onAnimationEnd(Animator animation) {
-                                mCustomizePagedView.setCustomizationFilter(newType);
-
-                                final float alpha = mCustomizePagedView.getAlpha();
-                                ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
-                                        mCustomizePagedView, "alpha", alpha, 1.0f);
-                                alphaAnim.setDuration(duration);
-                                alphaAnim.start();
-                            }
-                        });
-                        alphaAnim.start();
-                    }
-                }
-            });
         }
         setupViews();
 
@@ -957,7 +877,8 @@
             String curTab = savedState.getString("customize_currentTab");
             if (curTab != null) {
                 // We set this directly so that there is no delay before the tab is set
-                mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
+                mCustomizePagedView.setCustomizationFilter(
+                        mHomeCustomizationDrawer.getCustomizeFilterForTabTag(curTab));
                 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
             }
 
diff --git a/src/com/android/launcher2/PagedViewCellLayout.java b/src/com/android/launcher2/PagedViewCellLayout.java
index 28bb78b..53657e5 100644
--- a/src/com/android/launcher2/PagedViewCellLayout.java
+++ b/src/com/android/launcher2/PagedViewCellLayout.java
@@ -243,6 +243,15 @@
         setMeasuredDimension(newWidth, newHeight);
     }
 
+    int getContentWidth() {
+        // Return the distance from the left edge of the content of the leftmost icon to
+        // the right edge of the content of the rightmost icon
+
+        // icons are centered within cells, find out how much offset that accounts for
+        int iconHorizontalOffset = (mCellWidth - Utilities.getIconContentSize());
+        return mCellCountX * mCellWidth + (mCellCountX - 1) * mWidthGap - iconHorizontalOffset;
+    }
+
     @Override
     protected void onLayout(boolean changed, int l, int t, int r, int b) {
         int count = getChildCount();
@@ -281,17 +290,6 @@
         mHolographicChildren.setGap(widthGap, heightGap);
     }
 
-    public void setCellDimensions(int width, int height) {
-        mCellWidth = width;
-        mCellHeight = height;
-        mChildren.setCellDimensions(width, height);
-        mHolographicChildren.setCellDimensions(width, height);
-    }
-
-    public int getDefaultCellDimensions() {
-        return sDefaultCellDimensions;
-    }
-
     public int[] getCellCountForDimensions(int width, int height) {
         // Always assume we're working with the smallest span to make sure we
         // reserve enough space in both orientations
diff --git a/src/com/android/launcher2/Utilities.java b/src/com/android/launcher2/Utilities.java
index 60f71f5..8ab22eb 100644
--- a/src/com/android/launcher2/Utilities.java
+++ b/src/com/android/launcher2/Utilities.java
@@ -51,6 +51,7 @@
 
     private static int sIconWidth = -1;
     private static int sIconHeight = -1;
+    private static int sIconContentSize = -1;
     private static int sIconTextureWidth = -1;
     private static int sIconTextureHeight = -1;
 
@@ -90,6 +91,10 @@
     static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
     static int sColorIndex = 0;
 
+    static int getIconContentSize() {
+        return sIconContentSize;
+    }
+
     /**
      * Returns a bitmap suitable for the all apps view.  The bitmap will be a power
      * of two sized ARGB_8888 bitmap that can be used as a gl texture.
@@ -236,6 +241,9 @@
         final float density = metrics.density;
 
         sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
+        if (LauncherApplication.isScreenXLarge()) {
+            sIconContentSize = (int) resources.getDimension(R.dimen.app_icon_content_size);
+        }
         sIconTextureWidth = sIconTextureHeight = sIconWidth + 2;
 
         sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));