OSDN Git Service

Add an empty status fragment for status and suggestions.
authorFan Zhang <zhfan@google.com>
Thu, 24 Mar 2016 20:01:05 +0000 (13:01 -0700)
committerFan Zhang <zhfan@google.com>
Fri, 1 Apr 2016 16:07:31 +0000 (09:07 -0700)
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 [new file with mode: 0644]
res/layout/dashboard_status.xml [new file with mode: 0644]
res/layout/sliding_tab_indicator_view.xml [new file with mode: 0644]
res/layout/sliding_tab_title_view.xml [new file with mode: 0644]
res/values/dimens.xml
res/values/strings.xml
src/com/android/settings/InstrumentedFragment.java
src/com/android/settings/SettingsActivity.java
src/com/android/settings/dashboard/DashboardContainerFragment.java
src/com/android/settings/dashboard/DashboardStatusFragment.java [new file with mode: 0644]
src/com/android/settings/widget/SlidingTabLayout.java [new file with mode: 0644]

diff --git a/res/layout/dashboard_container_header.xml b/res/layout/dashboard_container_header.xml
new file mode 100644 (file)
index 0000000..a12f249
--- /dev/null
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+
+<com.android.settings.widget.SlidingTabLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:id="@+id/sliding_tabs"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:background="?android:attr/colorPrimary"/>
diff --git a/res/layout/dashboard_status.xml b/res/layout/dashboard_status.xml
new file mode 100644 (file)
index 0000000..d9b2863
--- /dev/null
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+
+<FrameLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"/>
\ 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 (file)
index 0000000..9b2942f
--- /dev/null
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+
+<View
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/sliding_tab_selected_indicator"
+    android:layout_width="wrap_content"
+    android:layout_height="@dimen/pager_tabs_selected_indicator_height"
+    android:background="?android:attr/colorAccent"/>
diff --git a/res/layout/sliding_tab_title_view.xml b/res/layout/sliding_tab_title_view.xml
new file mode 100644 (file)
index 0000000..6b91ed7
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 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.
+-->
+
+<TextView
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="wrap_content"
+    android:layout_weight="1"
+    android:layout_height="wrap_content"
+    android:ellipsize="end"
+    android:maxLines="1"
+    android:textColor="@android:color/white"
+    android:gravity="center"
+    android:background="?android:attr/selectableItemBackground"
+    android:textAllCaps="true"
+    android:padding="@dimen/pager_tabs_title_padding"/>
\ No newline at end of file
index a4e3401..cfa1009 100755 (executable)
@@ -59,6 +59,8 @@
     <dimen name="appwidget_min_height">40dip</dimen>
 
     <dimen name="pager_tabs_padding">0dp</dimen>
+    <dimen name="pager_tabs_title_padding">16dp</dimen>
+    <dimen name="pager_tabs_selected_indicator_height">3dp</dimen>
 
     <!-- Minimum width for the popup for updating a user's photo. -->
     <dimen name="update_user_photo_popup_min_width">300dip</dimen>
index 2350e55..2f5cf3e 100644 (file)
     <string name="notification_suggestion_title">Control lock screen notifications</string>
     <!-- Summary of notification suggestion during optional steps of setup. [CHAR_LIMIT=80] -->
     <string name="notification_suggestion_summary">Show or hide notification content</string>
+    <!-- Setting tab title for general status. [CHAR LIMIT=20] -->
+    <string name="page_tab_title_status">Status</string>
+    <!-- Setting tab title for all setting options. [CHAR LIMIT=20] -->
+    <string name="page_tab_title_summary">All</string>
 
 </resources>
index bb6a1ba..ee06034 100644 (file)
@@ -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.
index 859782e..989f895 100644 (file)
@@ -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);
index d1745f8..ccc95fc 100644 (file)
 
 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 (file)
index 0000000..66106be
--- /dev/null
@@ -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 (file)
index 0000000..176bebc
--- /dev/null
@@ -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);
+            }
+        }
+    }
+}