Display setting and support in a tab layout - DO NOT MERGE

Bug: 28269035
Bug: 28139604
diff --git a/src/com/android/settings/InstrumentedFragment.java b/src/com/android/settings/InstrumentedFragment.java
index 0e3528a..3977426 100644
--- a/src/com/android/settings/InstrumentedFragment.java
+++ b/src/com/android/settings/InstrumentedFragment.java
@@ -16,11 +16,11 @@
 
 package com.android.settings;
 
-import com.android.internal.logging.MetricsLogger;
-
 import android.os.Bundle;
 import android.support.v14.preference.PreferenceFragment;
 
+import com.android.internal.logging.MetricsLogger;
+
 /**
  * Instrumented fragment that logs visibility state.
  */
@@ -30,6 +30,9 @@
 
     // Used by PreferenceActivity for the dummy fragment it adds, no useful data here.
     public static final int PREFERENCE_ACTIVITY_FRAGMENT = UNDECLARED + 1;
+    public static final int DASHBOARD_CONTAINER = UNDECLARED + 2;
+
+    public static final int SUPPORT_FRAGMENT = UNDECLARED + 3;
 
     /**
      * Declare the view of this category.
diff --git a/src/com/android/settings/SettingsActivity.java b/src/com/android/settings/SettingsActivity.java
index 176ab7e..cde3a2d 100644
--- a/src/com/android/settings/SettingsActivity.java
+++ b/src/com/android/settings/SettingsActivity.java
@@ -49,6 +49,7 @@
 import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.SearchView;
+
 import com.android.internal.util.ArrayUtils;
 import com.android.settings.Settings.WifiSettingsActivity;
 import com.android.settings.accessibility.AccessibilitySettings;
@@ -68,7 +69,7 @@
 import com.android.settings.applications.WriteSettingsDetails;
 import com.android.settings.applications.VrListenerSettings;
 import com.android.settings.bluetooth.BluetoothSettings;
-import com.android.settings.dashboard.DashboardSummary;
+import com.android.settings.dashboard.DashboardContainerFragment;
 import com.android.settings.dashboard.SearchResultsSummary;
 import com.android.settings.datausage.DataUsageSummary;
 import com.android.settings.deviceinfo.PrivateVolumeForget;
@@ -613,7 +614,7 @@
                 // Show Search affordance
                 mDisplaySearch = true;
                 mInitialTitleResId = R.string.dashboard_title;
-                switchToFragment(DashboardSummary.class.getName(), null, false, false,
+                switchToFragment(DashboardContainerFragment.class.getName(), null, false, false,
                         mInitialTitleResId, mInitialTitle, false);
             }
         }
@@ -687,7 +688,7 @@
     }
 
     /**
-     * Sets the id of the view continaing the main content. Should be called before calling super's
+     * Sets the id of the view containing the main content. Should be called before calling super's
      * onCreate.
      */
     protected void setMainContentId(int contentId) {
@@ -729,7 +730,7 @@
         setTitleFromBackStack();
     }
 
-    private int setTitleFromBackStack() {
+    private void setTitleFromBackStack() {
         final int count = getFragmentManager().getBackStackEntryCount();
 
         if (count == 0) {
@@ -738,13 +739,11 @@
             } else {
                 setTitle(mInitialTitle);
             }
-            return 0;
+            return;
         }
 
         FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
         setTitleFromBackStackEntry(bse);
-
-        return count;
     }
 
     private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
@@ -1196,6 +1195,7 @@
         if (current != null && current instanceof SearchResultsSummary) {
             mSearchResultsFragment = (SearchResultsSummary) current;
         } else {
+            setContentHeaderView(null);
             mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
                     SearchResultsSummary.class.getName(), null, false, true,
                     R.string.search_results_title, null, true);
diff --git a/src/com/android/settings/dashboard/DashboardContainerFragment.java b/src/com/android/settings/dashboard/DashboardContainerFragment.java
new file mode 100644
index 0000000..831ac50
--- /dev/null
+++ b/src/com/android/settings/dashboard/DashboardContainerFragment.java
@@ -0,0 +1,136 @@
+/*
+ * Copyright (C) 2016 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.settings.dashboard;
+
+import android.app.Activity;
+import android.app.Fragment;
+import android.app.FragmentManager;
+import android.content.Context;
+import android.os.Bundle;
+import android.support.v13.app.FragmentPagerAdapter;
+import android.support.v4.view.ViewPager;
+import android.view.LayoutInflater;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.View;
+import android.view.ViewGroup;
+
+import com.android.settings.InstrumentedFragment;
+import com.android.settings.R;
+import com.android.settings.overlay.FeatureFactory;
+import com.android.settings.overlay.SupportFeatureProvider;
+import com.android.settings.widget.SlidingTabLayout;
+import com.android.settingslib.drawer.SettingsDrawerActivity;
+import com.android.settingslib.HelpUtils;
+
+/**
+ * Container for Dashboard fragments.
+ */
+public final class DashboardContainerFragment extends InstrumentedFragment {
+
+    private static final int INDEX_SUMMARY_FRAGMENT = 0;
+    private static final int INDEX_SUPPORT_FRAGMENT = 1;
+
+    private ViewPager mViewPager;
+    private View mHeaderView;
+    private DashboardViewPagerAdapter mPagerAdapter;
+
+    @Override
+    protected int getMetricsCategory() {
+        return DASHBOARD_CONTAINER;
+    }
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setHasOptionsMenu(true);
+    }
+
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
+        final View content = inflater.inflate(R.layout.dashboard_container, parent, false);
+        mViewPager = (ViewPager) content.findViewById(R.id.pager);
+        mPagerAdapter = new DashboardViewPagerAdapter(getContext(), getChildFragmentManager());
+        mViewPager.setAdapter(mPagerAdapter);
+        mHeaderView = inflater.inflate(R.layout.dashboard_container_header, parent, false);
+        ((SlidingTabLayout) mHeaderView).setViewPager(mViewPager);
+        return content;
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+        if (mPagerAdapter.getCount() > 1) {
+            final Activity activity = getActivity();
+            if (activity instanceof SettingsDrawerActivity) {
+                ((SettingsDrawerActivity) getActivity()).setContentHeaderView(mHeaderView);
+            }
+        }
+    }
+
+    @Override
+    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+        super.onCreateOptionsMenu(menu, inflater);
+        if (getActivity() == null) return;
+        HelpUtils.prepareHelpMenuItem(getActivity(), menu, R.string.help_uri_dashboard,
+                getClass().getName());
+    }
+
+    private static final class DashboardViewPagerAdapter extends FragmentPagerAdapter {
+
+        private final Context mContext;
+        private final SupportFeatureProvider mSupportFeatureProvider;
+
+        public DashboardViewPagerAdapter(Context context, FragmentManager fragmentManager) {
+            super(fragmentManager);
+            mContext = context;
+            mSupportFeatureProvider =
+                    FeatureFactory.getFactory(context).getSupportFeatureProvider();
+        }
+
+        @Override
+        public CharSequence getPageTitle(int position) {
+            switch (position) {
+                case INDEX_SUMMARY_FRAGMENT:
+                    return mContext.getString(R.string.page_tab_title_summary);
+                case INDEX_SUPPORT_FRAGMENT:
+                    return mContext.getString(R.string.page_tab_title_support);
+            }
+            return super.getPageTitle(position);
+        }
+
+        @Override
+        public Fragment getItem(int position) {
+            switch (position) {
+                case INDEX_SUMMARY_FRAGMENT:
+                    return new DashboardSummary();
+                case INDEX_SUPPORT_FRAGMENT:
+                    return new SupportFragment();
+                default:
+                    throw new IllegalArgumentException(
+                            String.format(
+                                    "Position %d does not map to a valid dashboard fragment",
+                                    position));
+            }
+        }
+
+        @Override
+        public int getCount() {
+            return mSupportFeatureProvider == null ? 1 : 2;
+        }
+    }
+}
diff --git a/src/com/android/settings/dashboard/DashboardSummary.java b/src/com/android/settings/dashboard/DashboardSummary.java
index 66b3730..80054d0 100644
--- a/src/com/android/settings/dashboard/DashboardSummary.java
+++ b/src/com/android/settings/dashboard/DashboardSummary.java
@@ -21,13 +21,11 @@
 import android.support.v7.widget.LinearLayoutManager;
 import android.util.Log;
 import android.view.LayoutInflater;
-import android.view.Menu;
-import android.view.MenuInflater;
 import android.view.View;
 import android.view.ViewGroup;
+
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.logging.MetricsProto.MetricsEvent;
-import com.android.settingslib.HelpUtils;
 import com.android.settings.InstrumentedFragment;
 import com.android.settings.R;
 import com.android.settings.Settings;
@@ -83,7 +81,6 @@
         List<DashboardCategory> categories =
                 ((SettingsActivity) getActivity()).getDashboardCategories();
         mSummaryLoader = new SummaryLoader(getActivity(), categories);
-        setHasOptionsMenu(true);
         Context context = getContext();
         mConditionManager = ConditionManager.get(context);
         mSuggestionParser = new SuggestionParser(context,
@@ -99,14 +96,6 @@
     }
 
     @Override
-    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
-        super.onCreateOptionsMenu(menu, inflater);
-        if (getActivity() == null) return;
-        HelpUtils.prepareHelpMenuItem(getActivity(), menu, R.string.help_uri_dashboard,
-                getClass().getName());
-    }
-
-    @Override
     public void onResume() {
         long startTime = System.currentTimeMillis();
         super.onResume();
diff --git a/src/com/android/settings/dashboard/SupportFragment.java b/src/com/android/settings/dashboard/SupportFragment.java
new file mode 100644
index 0000000..2e4fedc
--- /dev/null
+++ b/src/com/android/settings/dashboard/SupportFragment.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2016 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.settings.dashboard;
+
+import android.annotation.DrawableRes;
+import android.annotation.IdRes;
+import android.annotation.StringRes;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ImageView;
+import android.widget.TextView;
+
+import com.android.settings.InstrumentedFragment;
+import com.android.settings.R;
+
+/**
+ * Fragment for support tab in SettingsGoogle.
+ */
+public final class SupportFragment extends InstrumentedFragment {
+
+    private View mContent;
+
+    @Override
+    protected int getMetricsCategory() {
+        return SUPPORT_FRAGMENT;
+    }
+
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+            Bundle savedInstanceState) {
+        mContent = inflater.inflate(R.layout.support_fragment, container, false);
+        // Update escalation items.
+        updateEscalationCard(R.id.escalation_by_phone, R.string.support_escalation_by_phone);
+        updateEscalationCard(R.id.escalation_by_email, R.string.support_escalation_by_email);
+        // Update other support items.
+        updateSupportTile(R.id.forum_tile, R.drawable.ic_forum_24dp, R.string.support_forum_title);
+        updateSupportTile(R.id.article_tile, R.drawable.ic_help_24dp,
+                R.string.support_articles_title);
+        // Update feedback item.
+        updateSupportTile(R.id.feedback_tile, R.drawable.ic_feedback_24dp,
+                R.string.support_feedback_title);
+        return mContent;
+    }
+
+    private void updateEscalationCard(@IdRes int cardId, @StringRes int title) {
+        final View card = mContent.findViewById(cardId);
+        ((TextView) card.findViewById(R.id.title)).setText(title);
+    }
+
+    private void updateSupportTile(@IdRes int tileId, @DrawableRes int icon, @StringRes int title) {
+        final View tile = mContent.findViewById(tileId);
+        ((ImageView) tile.findViewById(android.R.id.icon)).setImageResource(icon);
+        ((TextView) tile.findViewById(android.R.id.title)).setText(title);
+    }
+}
diff --git a/src/com/android/settings/overlay/FeatureFactory.java b/src/com/android/settings/overlay/FeatureFactory.java
index b0b2a3f..4dc9ba2 100644
--- a/src/com/android/settings/overlay/FeatureFactory.java
+++ b/src/com/android/settings/overlay/FeatureFactory.java
@@ -59,6 +59,8 @@
         return sFactory;
     }
 
+    public abstract SupportFeatureProvider getSupportFeatureProvider();
+
     public static final class FactoryNotFoundException extends RuntimeException {
         public FactoryNotFoundException(Throwable throwable) {
             super("Unable to create factory. Did you misconfigure Proguard?", throwable);
diff --git a/src/com/android/settings/overlay/FeatureFactoryImpl.java b/src/com/android/settings/overlay/FeatureFactoryImpl.java
index 036b4e7..d884080 100644
--- a/src/com/android/settings/overlay/FeatureFactoryImpl.java
+++ b/src/com/android/settings/overlay/FeatureFactoryImpl.java
@@ -19,5 +19,11 @@
 /**
  * {@link FeatureFactory} implementation for AOSP Settings.
  */
-public class FeatureFactoryImpl extends FeatureFactory {
+public final class FeatureFactoryImpl extends FeatureFactory {
+
+    @Override
+    public SupportFeatureProvider getSupportFeatureProvider() {
+        return null;
+    }
+
 }
diff --git a/src/com/android/settings/overlay/SupportFeatureProvider.java b/src/com/android/settings/overlay/SupportFeatureProvider.java
new file mode 100644
index 0000000..3b654f4
--- /dev/null
+++ b/src/com/android/settings/overlay/SupportFeatureProvider.java
@@ -0,0 +1,8 @@
+package com.android.settings.overlay;
+
+/**
+ * Feature provider for support tab.
+ */
+public interface SupportFeatureProvider {
+
+}
diff --git a/src/com/android/settings/widget/SlidingTabLayout.java b/src/com/android/settings/widget/SlidingTabLayout.java
new file mode 100644
index 0000000..176bebc
--- /dev/null
+++ b/src/com/android/settings/widget/SlidingTabLayout.java
@@ -0,0 +1,168 @@
+/*
+ * Copyright (C) 2016 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.settings.widget;
+
+import android.content.Context;
+import android.support.v4.view.PagerAdapter;
+import android.support.v4.view.ViewPager;
+import android.util.AttributeSet;
+import android.view.Gravity;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.widget.FrameLayout;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+import com.android.settings.R;
+
+/**
+ * To be used with ViewPager to provide a tab indicator component which give constant feedback as
+ * to the user's scroll progress.
+ */
+public final class SlidingTabLayout extends FrameLayout implements View.OnClickListener {
+
+    private final LinearLayout mTitleView;
+    private final View mIndicatorView;
+    private final LayoutInflater mLayoutInflater;
+
+    private ViewPager mViewPager;
+    private int mSelectedPosition;
+    private float mSelectionOffset;
+
+    public SlidingTabLayout(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        mLayoutInflater = LayoutInflater.from(context);
+        mTitleView = new LinearLayout(context);
+        mTitleView.setGravity(Gravity.CENTER_HORIZONTAL);
+        mIndicatorView = mLayoutInflater.inflate(R.layout.sliding_tab_indicator_view, this, false);
+
+        addView(mTitleView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
+        addView(mIndicatorView, mIndicatorView.getLayoutParams());
+    }
+
+    /**
+     * Sets the associated view pager. Note that the assumption here is that the pager content
+     * (number of tabs and tab titles) does not change after this call has been made.
+     */
+    public void setViewPager(ViewPager viewPager) {
+        mTitleView.removeAllViews();
+
+        mViewPager = viewPager;
+        if (viewPager != null) {
+            viewPager.addOnPageChangeListener(new InternalViewPagerListener());
+            populateTabStrip();
+        }
+    }
+
+    @Override
+    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+        final int titleCount = mTitleView.getChildCount();
+        if (titleCount > 0) {
+            final int width = MeasureSpec.makeMeasureSpec(
+                    mTitleView.getMeasuredWidth() / titleCount, MeasureSpec.EXACTLY);
+            final int height = MeasureSpec.makeMeasureSpec(
+                    mIndicatorView.getMeasuredHeight(), MeasureSpec.EXACTLY);
+            mIndicatorView.measure(width, height);
+        }
+    }
+
+    @Override
+    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
+        if (mTitleView.getChildCount() > 0) {
+            mTitleView.layout(0, 0, mTitleView.getMeasuredWidth(), mTitleView.getMeasuredHeight());
+            final int indicatorBottom = getMeasuredHeight();
+            final int indicatorHeight = mIndicatorView.getMeasuredHeight();
+            mIndicatorView.layout(0, indicatorBottom - indicatorHeight,
+                    mIndicatorView.getMeasuredWidth(), indicatorBottom);
+        }
+    }
+
+    @Override
+    public void onClick(View v) {
+        final int titleCount = mTitleView.getChildCount();
+        for (int i = 0; i < titleCount; i++) {
+            if (v == mTitleView.getChildAt(i)) {
+                mViewPager.setCurrentItem(i);
+                return;
+            }
+        }
+    }
+
+    private void onViewPagerPageChanged(int position, float positionOffset) {
+        mSelectedPosition = position;
+        mSelectionOffset = positionOffset;
+        mIndicatorView.setTranslationX(getIndicatorLeft());
+    }
+
+    private void populateTabStrip() {
+        final PagerAdapter adapter = mViewPager.getAdapter();
+
+        for (int i = 0; i < adapter.getCount(); i++) {
+            final TextView tabTitleView = (TextView) mLayoutInflater.inflate(
+                    R.layout.sliding_tab_title_view, mTitleView, false);
+
+            tabTitleView.setText(adapter.getPageTitle(i));
+            tabTitleView.setOnClickListener(this);
+
+            mTitleView.addView(tabTitleView);
+            if (i == mViewPager.getCurrentItem()) {
+                tabTitleView.setSelected(true);
+            }
+        }
+    }
+
+    private int getIndicatorLeft() {
+        View selectedTitle = mTitleView.getChildAt(mSelectedPosition);
+        int left = selectedTitle.getLeft();
+        if (mSelectionOffset > 0f && mSelectedPosition < (getChildCount() - 1)) {
+            View nextTitle = mTitleView.getChildAt(mSelectedPosition + 1);
+            left = (int) (mSelectionOffset * nextTitle.getLeft()
+                    + (1.0f - mSelectionOffset) * left);
+        }
+        return left;
+    }
+
+    private final class InternalViewPagerListener implements ViewPager.OnPageChangeListener {
+        private int mScrollState;
+
+        @Override
+        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
+            final int titleCount = mTitleView.getChildCount();
+            if ((titleCount == 0) || (position < 0) || (position >= titleCount)) {
+                return;
+            }
+            onViewPagerPageChanged(position, positionOffset);
+        }
+
+        @Override
+        public void onPageScrollStateChanged(int state) {
+            mScrollState = state;
+        }
+
+        @Override
+        public void onPageSelected(int position) {
+            if (mScrollState == ViewPager.SCROLL_STATE_IDLE) {
+                onViewPagerPageChanged(position, 0f);
+            }
+            final int titleCount = getChildCount();
+            for (int i = 0; i < titleCount; i++) {
+                getChildAt(i).setSelected(position == i);
+            }
+        }
+    }
+}