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.workprofile;
17 
18 import android.content.Context;
19 import android.content.res.TypedArray;
20 import android.util.AttributeSet;
21 import android.widget.Button;
22 import android.widget.LinearLayout;
23 
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 
27 import com.android.launcher3.DeviceProfile;
28 import com.android.launcher3.R;
29 import com.android.launcher3.pageindicators.PageIndicator;
30 import com.android.launcher3.views.ActivityContext;
31 
32 /**
33  * Supports two indicator colors, dedicated for personal and work tabs.
34  */
35 public class PersonalWorkSlidingTabStrip extends LinearLayout implements PageIndicator {
36     private final boolean mIsAlignOnIcon;
37     private OnActivePageChangedListener mOnActivePageChangedListener;
38     private int mLastActivePage = 0;
39 
PersonalWorkSlidingTabStrip(@onNull Context context, @Nullable AttributeSet attrs)40     public PersonalWorkSlidingTabStrip(@NonNull Context context, @Nullable AttributeSet attrs) {
41         super(context, attrs);
42         TypedArray typedArray = context.obtainStyledAttributes(attrs,
43                 R.styleable.PersonalWorkSlidingTabStrip);
44         mIsAlignOnIcon = typedArray.getBoolean(
45                 R.styleable.PersonalWorkSlidingTabStrip_alignOnIcon, false);
46         typedArray.recycle();
47     }
48 
49     /**
50      * Highlights tab with index pos
51      */
updateTabTextColor(int pos)52     public void updateTabTextColor(int pos) {
53         for (int i = 0; i < getChildCount(); i++) {
54             Button tab = (Button) getChildAt(i);
55             tab.setSelected(i == pos);
56         }
57     }
58 
59     @Override
setScroll(int currentScroll, int totalScroll)60     public void setScroll(int currentScroll, int totalScroll) {
61     }
62 
63     @Override
setActiveMarker(int activePage)64     public void setActiveMarker(int activePage) {
65         updateTabTextColor(activePage);
66         if (mOnActivePageChangedListener != null && mLastActivePage != activePage) {
67             mOnActivePageChangedListener.onActivePageChanged(activePage);
68         }
69         mLastActivePage = activePage;
70     }
71 
setOnActivePageChangedListener(OnActivePageChangedListener listener)72     public void setOnActivePageChangedListener(OnActivePageChangedListener listener) {
73         mOnActivePageChangedListener = listener;
74     }
75 
76     @Override
setMarkersCount(int numMarkers)77     public void setMarkersCount(int numMarkers) {
78     }
79 
80     @Override
hasOverlappingRendering()81     public boolean hasOverlappingRendering() {
82         return false;
83     }
84 
85     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)86     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
87         if (mIsAlignOnIcon) {
88             // If any padding is not specified, restrict the width to emulate padding
89             int size = MeasureSpec.getSize(widthMeasureSpec);
90             size = getTabWidth(getContext(), size);
91             widthMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
92         }
93         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
94     }
95 
96     /**
97      * Returns distance between left and right app icons
98      */
getTabWidth(Context context, int totalWidth)99     public static int getTabWidth(Context context, int totalWidth) {
100         DeviceProfile grid = ActivityContext.lookupContext(context).getDeviceProfile();
101         int iconPadding = totalWidth / grid.numShownAllAppsColumns - grid.allAppsIconSizePx;
102         return totalWidth - iconPadding;
103     }
104 
105     /**
106      * Interface definition for a callback to be invoked when an active page has been changed.
107      */
108     public interface OnActivePageChangedListener {
109         /** Called when the active page has been changed. */
onActivePageChanged(int currentActivePage)110         void onActivePageChanged(int currentActivePage);
111     }
112 }
113