1 /* 2 * Copyright (C) 2022 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.wallpaper.widget; 17 18 import android.content.Context; 19 import android.util.AttributeSet; 20 import android.view.LayoutInflater; 21 import android.widget.FrameLayout; 22 import android.widget.TextView; 23 24 import androidx.annotation.IntDef; 25 import androidx.annotation.NonNull; 26 import androidx.annotation.Nullable; 27 28 import com.android.wallpaper.R; 29 import com.android.wallpaper.util.SystemColors; 30 31 /** 32 * Custom layout for duo tabs. 33 */ 34 public final class DuoTabs extends FrameLayout { 35 36 public static final int TAB_PRIMARY = 0; 37 public static final int TAB_SECONDARY = 1; 38 39 /** 40 * Overlay tab 41 */ 42 @IntDef({TAB_PRIMARY, TAB_SECONDARY}) 43 public @interface Tab { 44 } 45 46 /** 47 * Overlay tab selected listener 48 */ 49 public interface OnTabSelectedListener { 50 51 /** 52 * On tab selected 53 */ onTabSelected(@ab int tab)54 void onTabSelected(@Tab int tab); 55 } 56 57 private OnTabSelectedListener mOnTabSelectedListener; 58 private final TextView mPrimaryTab; 59 private final TextView mSecondaryTab; 60 @Tab private int mCurrentOverlayTab; 61 62 /** 63 * Constructor 64 */ DuoTabs(@onNull Context context, @Nullable AttributeSet attrs)65 public DuoTabs(@NonNull Context context, @Nullable AttributeSet attrs) { 66 super(context, attrs); 67 LayoutInflater.from(context).inflate(R.layout.duo_tabs, this, true); 68 mPrimaryTab = findViewById(R.id.tab_primary); 69 mSecondaryTab = findViewById(R.id.tab_secondary); 70 mPrimaryTab.setOnClickListener(v -> selectTab(TAB_PRIMARY)); 71 mSecondaryTab.setOnClickListener(v -> selectTab(TAB_SECONDARY)); 72 } 73 74 /** 75 * Set tab text 76 */ setTabText(String primaryTabText, String secondaryTabText)77 public void setTabText(String primaryTabText, String secondaryTabText) { 78 mPrimaryTab.setText(primaryTabText); 79 mSecondaryTab.setText(secondaryTabText); 80 } 81 82 /** 83 * Select a tab 84 */ selectTab(@ab int tab)85 public void selectTab(@Tab int tab) { 86 updateTabIndicator(tab); 87 if (mOnTabSelectedListener != null) { 88 mOnTabSelectedListener.onTabSelected(tab); 89 } 90 mCurrentOverlayTab = tab; 91 } 92 93 /** 94 * Set listener 95 */ setOnTabSelectedListener( OnTabSelectedListener onTabSelectedListener)96 public void setOnTabSelectedListener( 97 OnTabSelectedListener onTabSelectedListener) { 98 mOnTabSelectedListener = onTabSelectedListener; 99 } 100 101 /** 102 * Update the background color in case the context theme has changed. 103 */ updateBackgroundColor()104 public void updateBackgroundColor() { 105 mPrimaryTab.setBackground(null); 106 mSecondaryTab.setBackground(null); 107 updateTabIndicator(mCurrentOverlayTab); 108 } 109 updateTabIndicator(@ab int tab)110 private void updateTabIndicator(@Tab int tab) { 111 Context c = getContext(); 112 mPrimaryTab.setBackgroundResource( 113 tab == TAB_PRIMARY 114 ? R.drawable.duo_tabs_button_indicator_background 115 : R.drawable.duo_tabs_button_background); 116 mPrimaryTab.setTextColor( 117 tab == TAB_PRIMARY 118 ? getResources().getColor(R.color.text_color_on_accent) 119 : SystemColors.getColor(c, android.R.attr.textColorPrimary)); 120 mSecondaryTab.setBackgroundResource( 121 tab == TAB_SECONDARY 122 ? R.drawable.duo_tabs_button_indicator_background 123 : R.drawable.duo_tabs_button_background); 124 mSecondaryTab.setTextColor( 125 tab == TAB_SECONDARY 126 ? getResources().getColor(R.color.text_color_on_accent) 127 : SystemColors.getColor(c, android.R.attr.textColorPrimary)); 128 } 129 getSelectedTab()130 public @Tab int getSelectedTab() { 131 return mCurrentOverlayTab; 132 } 133 } 134