Adding a sliding tab strip to the custom tab layout.

Bug: 68713881

Change-Id: Ib0873482f80903611f183ccf430185f40a292f8e
diff --git a/res/color/all_apps_tab_text.xml b/res/color/all_apps_tab_text.xml
new file mode 100644
index 0000000..f0c6310
--- /dev/null
+++ b/res/color/all_apps_tab_text.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 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.
+-->
+<selector xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:color="?android:attr/colorAccent" android:state_selected="true"/>
+    <item android:color="?android:attr/textColorTertiary"/>
+</selector>
\ No newline at end of file
diff --git a/res/layout/all_apps.xml b/res/layout/all_apps.xml
index 05f509f..832aaef 100644
--- a/res/layout/all_apps.xml
+++ b/res/layout/all_apps.xml
@@ -44,8 +44,8 @@
             android:layout_width="match_parent"
             android:layout_height="wrap_content"/>
 
-        <LinearLayout
-            android:id="@+id/tab_layout"
+        <com.android.launcher3.views.SlidingTabStrip
+            android:id="@+id/tabs"
             android:layout_width="match_parent"
             android:layout_height="@dimen/all_apps_header_tab_height"
             android:layout_below="@id/header_content"
@@ -56,6 +56,7 @@
                 android:layout_height="match_parent"
                 android:layout_weight="1"
                 android:text="@string/all_apps_personal_tab"
+                android:textColor="@color/all_apps_tab_text"
                 android:background="?android:attr/selectableItemBackground"/>
             <Button
                 android:id="@+id/tab_work"
@@ -63,8 +64,9 @@
                 android:layout_height="match_parent"
                 android:layout_weight="1"
                 android:text="@string/all_apps_work_tab"
+                android:textColor="@color/all_apps_tab_text"
                 android:background="?android:attr/selectableItemBackground"/>
-        </LinearLayout>
+        </com.android.launcher3.views.SlidingTabStrip>
 
     </RelativeLayout>
 
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 94db0cc..2176c8d 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -91,6 +91,7 @@
     <dimen name="all_apps_background_canvas_height">475dp</dimen>
     <dimen name="all_apps_caret_workspace_offset">18dp</dimen>
     <dimen name="all_apps_header_tab_height">50dp</dimen>
+    <dimen name="all_apps_tabs_indicator_height">2dp</dimen>
 
 <!-- Search bar in All Apps -->
     <dimen name="all_apps_header_max_elevation">3dp</dimen>
diff --git a/src/com/android/launcher3/allapps/AllAppsContainerView.java b/src/com/android/launcher3/allapps/AllAppsContainerView.java
index 271a133..efd7b97 100644
--- a/src/com/android/launcher3/allapps/AllAppsContainerView.java
+++ b/src/com/android/launcher3/allapps/AllAppsContainerView.java
@@ -58,8 +58,8 @@
 import com.android.launcher3.util.ComponentKeyMapper;
 import com.android.launcher3.util.ItemInfoMatcher;
 import com.android.launcher3.util.PackageUserKey;
-import com.android.launcher3.util.Themes;
 import com.android.launcher3.util.TransformingTouchDelegate;
+import com.android.launcher3.views.SlidingTabStrip;
 
 import java.util.HashMap;
 import java.util.List;
@@ -471,6 +471,7 @@
     }
 
     private void setupWorkProfileTabs() {
+        final SlidingTabStrip tabs = findViewById(R.id.tabs);
         mViewPager.setAdapter(new TabsPagerAdapter());
         mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
 
@@ -478,6 +479,7 @@
 
             @Override
             public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
+                tabs.updateIndicatorPosition(position, positionOffset);
                 if (positionOffset == 0 && !mVisible || positionOffset > 0 && mVisible) {
                     mVisible = positionOffset == 0;
                     for (int i = 0; i < mAH.length; i++) {
@@ -490,6 +492,7 @@
 
             @Override
             public void onPageSelected(int pos) {
+                tabs.updateTabTextColor(pos);
                 mFloatingHeaderHandler.setMainActive(pos == 0);
                 applyTouchDelegate();
                 if (mAH[pos].recyclerView != null) {
diff --git a/src/com/android/launcher3/views/SlidingTabStrip.java b/src/com/android/launcher3/views/SlidingTabStrip.java
new file mode 100644
index 0000000..45c6261
--- /dev/null
+++ b/src/com/android/launcher3/views/SlidingTabStrip.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.launcher3.views;
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+import android.util.AttributeSet;
+import android.view.View;
+import android.widget.Button;
+import android.widget.LinearLayout;
+
+import com.android.launcher3.R;
+import com.android.launcher3.util.Themes;
+
+public class SlidingTabStrip extends LinearLayout {
+
+    private final Paint mSelectedIndicatorPaint;
+    private int mSelectedIndicatorHeight;
+    private int mIndicatorLeft = -1;
+    private int mIndicatorRight = -1;
+    private int mSelectedPosition = -1;
+    private float mSelectionOffset;
+
+    public SlidingTabStrip(@NonNull Context context, @Nullable AttributeSet attrs) {
+        super(context, attrs);
+        setOrientation(HORIZONTAL);
+        setWillNotDraw(false);
+        mSelectedIndicatorPaint = new Paint();
+        mSelectedIndicatorPaint.setColor(Themes.getAttrColor(context, android.R.attr.colorAccent));
+        mSelectedIndicatorHeight = getResources()
+                .getDimensionPixelSize(R.dimen.all_apps_tabs_indicator_height);
+    }
+
+    public void updateIndicatorPosition(int position, float positionOffset) {
+        mSelectedPosition = position;
+        mSelectionOffset = positionOffset;
+        updateIndicatorPosition();
+    }
+
+    public void updateTabTextColor(int pos) {
+        for (int i=0; i < getChildCount(); i++) {
+            Button tab = (Button) getChildAt(i);
+            tab.setSelected(i == pos);
+        }
+    }
+
+    @Override
+    protected void onLayout(boolean changed, int l, int t, int r, int b) {
+        super.onLayout(changed, l, t, r, b);
+        updateTabTextColor(0);
+        updateIndicatorPosition(0, 0);
+    }
+
+    private void updateIndicatorPosition() {
+        final View tab = getChildAt(mSelectedPosition);
+        int left, right;
+
+        if (tab != null && tab.getWidth() > 0) {
+            left = tab.getLeft();
+            right = tab.getRight();
+
+            if (mSelectionOffset > 0f && mSelectedPosition < getChildCount() - 1) {
+                // Draw the selection partway between the tabs
+                View nextTitle = getChildAt(mSelectedPosition + 1);
+                left = (int) (mSelectionOffset * nextTitle.getLeft() +
+                        (1.0f - mSelectionOffset) * left);
+                right = (int) (mSelectionOffset * nextTitle.getRight() +
+                        (1.0f - mSelectionOffset) * right);
+            }
+        } else {
+            left = right = -1;
+        }
+
+        setIndicatorPosition(left, right);
+    }
+
+    private void setIndicatorPosition(int left, int right) {
+        if (left != mIndicatorLeft || right != mIndicatorRight) {
+            mIndicatorLeft = left;
+            mIndicatorRight = right;
+            invalidate();
+        }
+    }
+
+    @Override
+    protected void onDraw(Canvas canvas) {
+        super.onDraw(canvas);
+        canvas.drawRect(mIndicatorLeft, getHeight() - mSelectedIndicatorHeight,
+                mIndicatorRight, getHeight(), mSelectedIndicatorPaint);
+    }
+}
\ No newline at end of file