1 /*
2  * Copyright (C) 2021 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.car.ui.toolbar;
17 
18 import android.graphics.drawable.Drawable;
19 
20 import androidx.annotation.NonNull;
21 import androidx.annotation.Nullable;
22 
23 import java.util.List;
24 import java.util.function.Consumer;
25 
26 /**
27  * This is a data class the represents a tab in the toolbar. They can be added to the
28  * toolbar via {@link ToolbarController#setTabs(List)}.
29  */
30 @SuppressWarnings("AndroidJdkLibsChecker")
31 public final class Tab {
32     @Nullable
33     private final String mText;
34     @Nullable
35     private final Drawable mIcon;
36     @Nullable
37     private final Consumer<Tab> mSelectedListener;
38     private final boolean mTinted;
39 
Tab(@onNull Builder builder)40     private Tab(@NonNull Builder builder) {
41         mText = builder.mText;
42         mIcon = builder.mIcon;
43         mSelectedListener = builder.mSelectedListener;
44         mTinted = builder.mTinted;
45     }
46 
47     /** Gets the tab's text */
48     @Nullable
getText()49     public String getText() {
50         return mText;
51     }
52 
53     /** Gets the tab's icon */
54     @Nullable
getIcon()55     public Drawable getIcon() {
56         return mIcon;
57     }
58 
59     /** Gets the listener to call when the tab is selected */
60     @Nullable
getSelectedListener()61     public Consumer<Tab> getSelectedListener() {
62         return mSelectedListener;
63     }
64 
65     /** Gets if the icon should be tinted to match the style of the toolbar. Default true. */
isTinted()66     public boolean isTinted() {
67         return mTinted;
68     }
69 
70     /** Creates a new {@link Builder} */
builder()71     public static Builder builder() {
72         return new Builder();
73     }
74 
75     /** Creates a new {@link Builder} that is initialized as a copy of this tab. */
copy()76     public Builder copy() {
77         return new Builder(this);
78     }
79 
80     /** Builder for {@link Tab} */
81     public static class Builder {
82         @Nullable
83         private String mText = null;
84         @Nullable
85         private Drawable mIcon = null;
86         @Nullable
87         private Consumer<Tab> mSelectedListener = null;
88         private boolean mTinted = true;
89 
Builder()90         private Builder() {
91         }
92 
Builder(Tab tab)93         private Builder(Tab tab) {
94             mText = tab.mText;
95             mIcon = tab.mIcon;
96             mSelectedListener = tab.mSelectedListener;
97             mTinted  = tab.mTinted;
98         }
99 
100         /** Sets the tab's text */
setText(String text)101         public Builder setText(String text) {
102             mText = text;
103             return this;
104         }
105 
106         /** Sets the tab's icon */
setIcon(Drawable icon)107         public Builder setIcon(Drawable icon) {
108             mIcon = icon;
109             return this;
110         }
111 
112         /** Sets a listener that is called when the tab is selected */
setSelectedListener(Consumer<Tab> callback)113         public Builder setSelectedListener(Consumer<Tab> callback) {
114             mSelectedListener = callback;
115             return this;
116         }
117 
118 
119         /** Sets if the icon should be tinted to match the style of the toolbar. Default true. */
setTinted(boolean tinted)120         public Builder setTinted(boolean tinted) {
121             mTinted = tinted;
122             return this;
123         }
124 
125         /** Builds the final {@link Tab} */
build()126         public Tab build() {
127             return new Tab(this);
128         }
129     }
130 }
131