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.graphics.Canvas;
20 import android.graphics.Paint;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.widget.Button;
24 import android.widget.LinearLayout;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 
29 import com.android.launcher3.R;
30 import com.android.launcher3.Utilities;
31 import com.android.launcher3.pageindicators.PageIndicator;
32 import com.android.launcher3.util.Themes;
33 
34 /**
35  * Supports two indicator colors, dedicated for personal and work tabs.
36  */
37 public class PersonalWorkSlidingTabStrip extends LinearLayout implements PageIndicator {
38     private static final int POSITION_PERSONAL = 0;
39     private static final int POSITION_WORK = 1;
40 
41     private final Paint mSelectedIndicatorPaint;
42     private final Paint mDividerPaint;
43 
44     private int mSelectedIndicatorHeight;
45     private int mIndicatorLeft = -1;
46     private int mIndicatorRight = -1;
47     private float mScrollOffset;
48     private int mSelectedPosition = 0;
49 
50     private AllAppsContainerView mContainerView;
51     private int mLastActivePage = 0;
52     private boolean mIsRtl;
53 
PersonalWorkSlidingTabStrip(@onNull Context context, @Nullable AttributeSet attrs)54     public PersonalWorkSlidingTabStrip(@NonNull Context context, @Nullable AttributeSet attrs) {
55         super(context, attrs);
56         setOrientation(HORIZONTAL);
57         setWillNotDraw(false);
58 
59         mSelectedIndicatorHeight =
60                 getResources().getDimensionPixelSize(R.dimen.all_apps_tabs_indicator_height);
61 
62         mSelectedIndicatorPaint = new Paint();
63         mSelectedIndicatorPaint.setColor(
64                 Themes.getAttrColor(context, android.R.attr.colorAccent));
65 
66         mDividerPaint = new Paint();
67         mDividerPaint.setColor(Themes.getAttrColor(context, android.R.attr.colorControlHighlight));
68         mDividerPaint.setStrokeWidth(
69                 getResources().getDimensionPixelSize(R.dimen.all_apps_divider_height));
70 
71         mIsRtl = Utilities.isRtl(getResources());
72     }
73 
74     /**
75      * Highlights tab with index pos
76      */
updateTabTextColor(int pos)77     public void updateTabTextColor(int pos) {
78         mSelectedPosition = pos;
79         for (int i = 0; i < getChildCount(); i++) {
80             Button tab = (Button) getChildAt(i);
81             tab.setSelected(i == pos);
82         }
83     }
84 
updateIndicatorPosition(float scrollOffset)85     private void updateIndicatorPosition(float scrollOffset) {
86         mScrollOffset = scrollOffset;
87         updateIndicatorPosition();
88     }
89 
90     @Override
onLayout(boolean changed, int l, int t, int r, int b)91     protected void onLayout(boolean changed, int l, int t, int r, int b) {
92         super.onLayout(changed, l, t, r, b);
93         updateTabTextColor(mSelectedPosition);
94         updateIndicatorPosition(mScrollOffset);
95     }
96 
updateIndicatorPosition()97     private void updateIndicatorPosition() {
98         int left = -1, right = -1;
99         final View leftTab = getLeftTab();
100         if (leftTab != null) {
101             left = (int) (leftTab.getLeft() + leftTab.getWidth() * mScrollOffset);
102             right = left + leftTab.getWidth();
103         }
104         setIndicatorPosition(left, right);
105     }
106 
getLeftTab()107     private View getLeftTab() {
108         return mIsRtl ? getChildAt(1) : getChildAt(0);
109     }
110 
setIndicatorPosition(int left, int right)111     private void setIndicatorPosition(int left, int right) {
112         if (left != mIndicatorLeft || right != mIndicatorRight) {
113             mIndicatorLeft = left;
114             mIndicatorRight = right;
115             invalidate();
116         }
117     }
118 
119     @Override
onDraw(Canvas canvas)120     protected void onDraw(Canvas canvas) {
121         super.onDraw(canvas);
122 
123         float y = getHeight() - mDividerPaint.getStrokeWidth();
124         canvas.drawLine(getPaddingLeft(), y, getWidth() - getPaddingRight(), y, mDividerPaint);
125         canvas.drawRect(mIndicatorLeft, getHeight() - mSelectedIndicatorHeight,
126             mIndicatorRight, getHeight(), mSelectedIndicatorPaint);
127     }
128 
129     @Override
setScroll(int currentScroll, int totalScroll)130     public void setScroll(int currentScroll, int totalScroll) {
131         float scrollOffset = ((float) currentScroll) / totalScroll;
132         updateIndicatorPosition(scrollOffset);
133     }
134 
135     @Override
setActiveMarker(int activePage)136     public void setActiveMarker(int activePage) {
137         updateTabTextColor(activePage);
138         if (mContainerView != null && mLastActivePage != activePage) {
139             updateIndicatorPosition(activePage);
140             mContainerView.onTabChanged(activePage);
141         }
142         mLastActivePage = activePage;
143     }
144 
setContainerView(AllAppsContainerView containerView)145     public void setContainerView(AllAppsContainerView containerView) {
146         mContainerView = containerView;
147     }
148 
149     @Override
setMarkersCount(int numMarkers)150     public void setMarkersCount(int numMarkers) { }
151 
152     @Override
hasOverlappingRendering()153     public boolean hasOverlappingRendering() {
154         return false;
155     }
156 }
157