From 3feafd22ed998508d71ba466d88ce7d0541bd9b9 Mon Sep 17 00:00:00 2001 From: Fan Zhang Date: Thu, 24 Mar 2016 13:01:05 -0700 Subject: [PATCH] Add an empty status fragment for status and suggestions. Bug: 27927402 Also add a TabLayout to show title for these fragments, inspired by http://goo.gl/aOt51Q Change-Id: I40bab3075480670558ac9a896337109f10e8c90c --- res/layout/dashboard_container_header.xml | 23 +++ res/layout/dashboard_status.xml | 21 +++ res/layout/sliding_tab_indicator_view.xml | 22 +++ res/layout/sliding_tab_title_view.xml | 28 ++++ res/values/dimens.xml | 2 + res/values/strings.xml | 4 + src/com/android/settings/InstrumentedFragment.java | 1 + src/com/android/settings/SettingsActivity.java | 1 + .../dashboard/DashboardContainerFragment.java | 42 +++++- .../dashboard/DashboardStatusFragment.java | 41 +++++ .../android/settings/widget/SlidingTabLayout.java | 168 +++++++++++++++++++++ 11 files changed, 349 insertions(+), 4 deletions(-) create mode 100644 res/layout/dashboard_container_header.xml create mode 100644 res/layout/dashboard_status.xml create mode 100644 res/layout/sliding_tab_indicator_view.xml create mode 100644 res/layout/sliding_tab_title_view.xml create mode 100644 src/com/android/settings/dashboard/DashboardStatusFragment.java create mode 100644 src/com/android/settings/widget/SlidingTabLayout.java diff --git a/res/layout/dashboard_container_header.xml b/res/layout/dashboard_container_header.xml new file mode 100644 index 0000000000..a12f2498cb --- /dev/null +++ b/res/layout/dashboard_container_header.xml @@ -0,0 +1,23 @@ + + + + diff --git a/res/layout/dashboard_status.xml b/res/layout/dashboard_status.xml new file mode 100644 index 0000000000..d9b2863015 --- /dev/null +++ b/res/layout/dashboard_status.xml @@ -0,0 +1,21 @@ + + + + \ No newline at end of file diff --git a/res/layout/sliding_tab_indicator_view.xml b/res/layout/sliding_tab_indicator_view.xml new file mode 100644 index 0000000000..9b2942f06e --- /dev/null +++ b/res/layout/sliding_tab_indicator_view.xml @@ -0,0 +1,22 @@ + + + + diff --git a/res/layout/sliding_tab_title_view.xml b/res/layout/sliding_tab_title_view.xml new file mode 100644 index 0000000000..6b91ed7f19 --- /dev/null +++ b/res/layout/sliding_tab_title_view.xml @@ -0,0 +1,28 @@ + + + + \ No newline at end of file diff --git a/res/values/dimens.xml b/res/values/dimens.xml index a4e3401f02..cfa1009c33 100755 --- a/res/values/dimens.xml +++ b/res/values/dimens.xml @@ -59,6 +59,8 @@ 40dip 0dp + 16dp + 3dp 300dip diff --git a/res/values/strings.xml b/res/values/strings.xml index 2350e5573f..2f5cf3e09e 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -7353,5 +7353,9 @@ Control lock screen notifications Show or hide notification content + + Status + + All diff --git a/src/com/android/settings/InstrumentedFragment.java b/src/com/android/settings/InstrumentedFragment.java index bb6a1bad5b..ee060343bf 100644 --- a/src/com/android/settings/InstrumentedFragment.java +++ b/src/com/android/settings/InstrumentedFragment.java @@ -31,6 +31,7 @@ public abstract class InstrumentedFragment extends PreferenceFragment { public static final int DASHBOARD_CONTAINER = UNDECLARED + 1; // Used by PreferenceActivity for the dummy fragment it adds, no useful data here. public static final int PREFERENCE_ACTIVITY_FRAGMENT = UNDECLARED + 2; + public static final int DASHBOARD_STATUS = 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 859782ec04..989f895294 100644 --- a/src/com/android/settings/SettingsActivity.java +++ b/src/com/android/settings/SettingsActivity.java @@ -1198,6 +1198,7 @@ public class SettingsActivity extends SettingsDrawerActivity 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 index d1745f825d..ccc95fc0b9 100644 --- a/src/com/android/settings/dashboard/DashboardContainerFragment.java +++ b/src/com/android/settings/dashboard/DashboardContainerFragment.java @@ -16,8 +16,10 @@ 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; @@ -27,13 +29,19 @@ import android.view.ViewGroup; import com.android.settings.InstrumentedFragment; import com.android.settings.R; +import com.android.settings.widget.SlidingTabLayout; +import com.android.settingslib.drawer.SettingsDrawerActivity; /** * Container for Dashboard fragments. */ public final class DashboardContainerFragment extends InstrumentedFragment { + private static final int INDEX_BRIEF_FRAGMENT = 0; + private static final int INDEX_SUMMARY_FRAGMENT = 1; + private ViewPager mViewPager; + private View mHeaderView; private DashboardViewPagerAdapter mPagerAdapter; @Override @@ -45,22 +53,48 @@ public final class DashboardContainerFragment extends InstrumentedFragment { 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(getChildFragmentManager()); + 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(); + final Activity activity = getActivity(); + if (activity instanceof SettingsDrawerActivity) { + ((SettingsDrawerActivity) getActivity()).setContentHeaderView(mHeaderView); + } + } + private static final class DashboardViewPagerAdapter extends FragmentPagerAdapter { + private final Context mContext; - public DashboardViewPagerAdapter(FragmentManager fragmentManager) { + public DashboardViewPagerAdapter(Context context, FragmentManager fragmentManager) { super(fragmentManager); + mContext = context; + } + + @Override + public CharSequence getPageTitle(int position) { + switch (position) { + case INDEX_BRIEF_FRAGMENT: + return mContext.getString(R.string.page_tab_title_status); + case INDEX_SUMMARY_FRAGMENT: + return mContext.getString(R.string.page_tab_title_summary); + } + return super.getPageTitle(position); } @Override public Fragment getItem(int position) { switch (position) { - case 0: + case INDEX_BRIEF_FRAGMENT: + return new DashboardStatusFragment(); + case INDEX_SUMMARY_FRAGMENT: return new DashboardSummary(); default: throw new IllegalArgumentException( @@ -72,7 +106,7 @@ public final class DashboardContainerFragment extends InstrumentedFragment { @Override public int getCount() { - return 1; + return 2; } } } diff --git a/src/com/android/settings/dashboard/DashboardStatusFragment.java b/src/com/android/settings/dashboard/DashboardStatusFragment.java new file mode 100644 index 0000000000..66106be8f9 --- /dev/null +++ b/src/com/android/settings/dashboard/DashboardStatusFragment.java @@ -0,0 +1,41 @@ +/* + * 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.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import com.android.settings.InstrumentedFragment; +import com.android.settings.R; + +/** + * Dashboard fragment for showing status and suggestions. + */ +public final class DashboardStatusFragment extends InstrumentedFragment { + + @Override + protected int getMetricsCategory() { + return DASHBOARD_STATUS; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { + return inflater.inflate(R.layout.dashboard_status, parent, false); + } +} diff --git a/src/com/android/settings/widget/SlidingTabLayout.java b/src/com/android/settings/widget/SlidingTabLayout.java new file mode 100644 index 0000000000..176bebc845 --- /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); + } + } + } +} -- 2.11.0