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