Home | History | Annotate | Download | only in allapps
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.launcher3.allapps;
     17 
     18 import android.content.Context;
     19 import android.content.SharedPreferences;
     20 import android.graphics.Canvas;
     21 import android.graphics.Paint;
     22 import android.support.annotation.NonNull;
     23 import android.support.annotation.Nullable;
     24 import android.util.AttributeSet;
     25 import android.view.View;
     26 import android.widget.Button;
     27 import android.widget.LinearLayout;
     28 
     29 import com.android.launcher3.Launcher;
     30 import com.android.launcher3.R;
     31 import com.android.launcher3.Utilities;
     32 import com.android.launcher3.pageindicators.PageIndicator;
     33 import com.android.launcher3.util.Themes;
     34 
     35 /**
     36  * Supports two indicator colors, dedicated for personal and work tabs.
     37  */
     38 public class PersonalWorkSlidingTabStrip extends LinearLayout implements PageIndicator {
     39     private static final int POSITION_PERSONAL = 0;
     40     private static final int POSITION_WORK = 1;
     41 
     42     private static final String KEY_SHOWED_PEEK_WORK_TAB = "showed_peek_work_tab";
     43 
     44     private final Paint mSelectedIndicatorPaint;
     45     private final Paint mDividerPaint;
     46     private final SharedPreferences mSharedPreferences;
     47 
     48     private int mSelectedIndicatorHeight;
     49     private int mIndicatorLeft = -1;
     50     private int mIndicatorRight = -1;
     51     private float mScrollOffset;
     52     private int mSelectedPosition = 0;
     53 
     54     private AllAppsContainerView mContainerView;
     55     private int mLastActivePage = 0;
     56     private boolean mIsRtl;
     57 
     58     public PersonalWorkSlidingTabStrip(@NonNull Context context, @Nullable AttributeSet attrs) {
     59         super(context, attrs);
     60         setOrientation(HORIZONTAL);
     61         setWillNotDraw(false);
     62 
     63         mSelectedIndicatorHeight =
     64                 getResources().getDimensionPixelSize(R.dimen.all_apps_tabs_indicator_height);
     65 
     66         mSelectedIndicatorPaint = new Paint();
     67         mSelectedIndicatorPaint.setColor(
     68                 Themes.getAttrColor(context, android.R.attr.colorAccent));
     69 
     70         mDividerPaint = new Paint();
     71         mDividerPaint.setColor(Themes.getAttrColor(context, android.R.attr.colorControlHighlight));
     72         mDividerPaint.setStrokeWidth(
     73                 getResources().getDimensionPixelSize(R.dimen.all_apps_divider_height));
     74 
     75         mSharedPreferences = Launcher.getLauncher(getContext()).getSharedPrefs();
     76         mIsRtl = Utilities.isRtl(getResources());
     77     }
     78 
     79     private void updateIndicatorPosition(float scrollOffset) {
     80         mScrollOffset = scrollOffset;
     81         updateIndicatorPosition();
     82     }
     83 
     84     private void updateTabTextColor(int pos) {
     85         mSelectedPosition = pos;
     86         for (int i = 0; i < getChildCount(); i++) {
     87             Button tab = (Button) getChildAt(i);
     88             tab.setSelected(i == pos);
     89         }
     90     }
     91 
     92     @Override
     93     protected void onLayout(boolean changed, int l, int t, int r, int b) {
     94         super.onLayout(changed, l, t, r, b);
     95         updateTabTextColor(mSelectedPosition);
     96         updateIndicatorPosition(mScrollOffset);
     97     }
     98 
     99     private void updateIndicatorPosition() {
    100         int left = -1, right = -1;
    101         final View leftTab = getLeftTab();
    102         if (leftTab != null) {
    103             left = (int) (leftTab.getLeft() + leftTab.getWidth() * mScrollOffset);
    104             right = left + leftTab.getWidth();
    105         }
    106         setIndicatorPosition(left, right);
    107     }
    108 
    109     private View getLeftTab() {
    110         return mIsRtl ? getChildAt(1) : getChildAt(0);
    111     }
    112 
    113     private void setIndicatorPosition(int left, int right) {
    114         if (left != mIndicatorLeft || right != mIndicatorRight) {
    115             mIndicatorLeft = left;
    116             mIndicatorRight = right;
    117             invalidate();
    118         }
    119     }
    120 
    121     @Override
    122     protected void onDraw(Canvas canvas) {
    123         super.onDraw(canvas);
    124 
    125         float y = getHeight() - mDividerPaint.getStrokeWidth();
    126         canvas.drawLine(getPaddingLeft(), y, getWidth() - getPaddingRight(), y, mDividerPaint);
    127         canvas.drawRect(mIndicatorLeft, getHeight() - mSelectedIndicatorHeight,
    128             mIndicatorRight, getHeight(), mSelectedIndicatorPaint);
    129     }
    130 
    131     public void highlightWorkTabIfNecessary() {
    132         if (mSharedPreferences.getBoolean(KEY_SHOWED_PEEK_WORK_TAB, false)) {
    133             return;
    134         }
    135         if (mLastActivePage != POSITION_PERSONAL) {
    136             return;
    137         }
    138         highlightWorkTab();
    139         mSharedPreferences.edit().putBoolean(KEY_SHOWED_PEEK_WORK_TAB, true).apply();
    140     }
    141 
    142     private void highlightWorkTab() {
    143         View v = getChildAt(POSITION_WORK);
    144         v.post(() -> {
    145             v.setPressed(true);
    146             v.setPressed(false);
    147         });
    148     }
    149 
    150     @Override
    151     public void setScroll(int currentScroll, int totalScroll) {
    152         float scrollOffset = ((float) currentScroll) / totalScroll;
    153         updateIndicatorPosition(scrollOffset);
    154     }
    155 
    156     @Override
    157     public void setActiveMarker(int activePage) {
    158         updateTabTextColor(activePage);
    159         if (mContainerView != null && mLastActivePage != activePage) {
    160             mContainerView.onTabChanged(activePage);
    161         }
    162         mLastActivePage = activePage;
    163     }
    164 
    165     public void setContainerView(AllAppsContainerView containerView) {
    166         mContainerView = containerView;
    167     }
    168 
    169     @Override
    170     public void setMarkersCount(int numMarkers) { }
    171 
    172     @Override
    173     public boolean hasOverlappingRendering() {
    174         return false;
    175     }
    176 }
    177