1 /*
2  * Copyright (C) 2020 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 
17 package com.android.car.ui.toolbar;
18 
19 import android.graphics.drawable.Drawable;
20 import android.widget.ProgressBar;
21 
22 import androidx.annotation.DrawableRes;
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 import androidx.annotation.StringRes;
26 import androidx.annotation.XmlRes;
27 
28 import java.util.List;
29 
30 /**
31  * An interface for accessing a Chassis Toolbar, regardless of how the underlying
32  * views are represented.
33  */
34 public interface ToolbarController {
35 
36     /**
37      * Returns {@code true} if a two row layout in enabled for the toolbar.
38      */
isTabsInSecondRow()39     boolean isTabsInSecondRow();
40 
41     /**
42      * Sets the title of the toolbar to a string resource.
43      *
44      * <p>The title may not always be shown, for example with one row layout with tabs.
45      */
setTitle(@tringRes int title)46     void setTitle(@StringRes int title);
47 
48     /**
49      * Sets the title of the toolbar to a CharSequence.
50      *
51      * <p>The title may not always be shown, for example with one row layout with tabs.
52      */
setTitle(CharSequence title)53     void setTitle(CharSequence title);
54 
55     /**
56      * Gets the current toolbar title.
57      */
getTitle()58     CharSequence getTitle();
59 
60     /**
61      * Gets the {@link TabLayout} for this toolbar.
62      */
getTabLayout()63     TabLayout getTabLayout();
64 
65     /**
66      * Adds a tab to this toolbar. You can listen for when it is selected via
67      * {@link #registerOnTabSelectedListener(Toolbar.OnTabSelectedListener)}.
68      */
addTab(TabLayout.Tab tab)69     void addTab(TabLayout.Tab tab);
70 
71     /** Removes all the tabs. */
clearAllTabs()72     void clearAllTabs();
73 
74     /**
75      * Gets a tab added to this toolbar. See
76      * {@link #addTab(TabLayout.Tab)}.
77      */
getTab(int position)78     TabLayout.Tab getTab(int position);
79 
80     /**
81      * Selects a tab added to this toolbar. See
82      * {@link #addTab(TabLayout.Tab)}.
83      */
selectTab(int position)84     void selectTab(int position);
85 
86     /**
87      * Sets whether or not tabs should also be shown in the SUBPAGE {@link Toolbar.State}.
88      */
setShowTabsInSubpage(boolean showTabs)89     void setShowTabsInSubpage(boolean showTabs);
90 
91     /**
92      * Gets whether or not tabs should also be shown in the SUBPAGE {@link Toolbar.State}.
93      */
getShowTabsInSubpage()94     boolean getShowTabsInSubpage();
95 
96     /**
97      * Sets the logo to display in this toolbar. If navigation icon is being displayed, this logo
98      * will be displayed next to the title.
99      */
setLogo(@rawableRes int resId)100     void setLogo(@DrawableRes int resId);
101 
102     /**
103      * Sets the logo to display in this toolbar. If navigation icon is being displayed, this logo
104      * will be displayed next to the title.
105      */
setLogo(Drawable drawable)106     void setLogo(Drawable drawable);
107 
108     /** Sets the hint for the search bar. */
setSearchHint(@tringRes int resId)109     void setSearchHint(@StringRes int resId);
110 
111     /** Sets the hint for the search bar. */
setSearchHint(CharSequence hint)112     void setSearchHint(CharSequence hint);
113 
114     /** Gets the search hint */
getSearchHint()115     CharSequence getSearchHint();
116 
117     /**
118      * Sets the icon to display in the search box.
119      *
120      * <p>The icon will be lost on configuration change, make sure to set it in onCreate() or
121      * a similar place.
122      */
setSearchIcon(@rawableRes int resId)123     void setSearchIcon(@DrawableRes int resId);
124 
125     /**
126      * Sets the icon to display in the search box.
127      *
128      * <p>The icon will be lost on configuration change, make sure to set it in onCreate() or
129      * a similar place.
130      */
setSearchIcon(Drawable d)131     void setSearchIcon(Drawable d);
132 
133 
134     /** Sets the {@link Toolbar.NavButtonMode} */
setNavButtonMode(Toolbar.NavButtonMode style)135     void setNavButtonMode(Toolbar.NavButtonMode style);
136 
137     /** Gets the {@link Toolbar.NavButtonMode} */
getNavButtonMode()138     Toolbar.NavButtonMode getNavButtonMode();
139 
140     /** Show/hide the background. When hidden, the toolbar is completely transparent. */
setBackgroundShown(boolean shown)141     void setBackgroundShown(boolean shown);
142 
143     /** Returns true is the toolbar background is shown */
getBackgroundShown()144     boolean getBackgroundShown();
145 
146     /**
147      * Sets the {@link MenuItem Menuitems} to display.
148      */
setMenuItems(@ullable List<MenuItem> items)149     void setMenuItems(@Nullable List<MenuItem> items);
150 
151     /**
152      * Sets the {@link MenuItem Menuitems} to display to a list defined in XML.
153      *
154      * <p>If this method is called twice with the same argument (and {@link #setMenuItems(List)}
155      * wasn't called), nothing will happen the second time, even if the MenuItems were changed.
156      *
157      * <p>The XML file must have one <MenuItems> tag, with a variable number of <MenuItem>
158      * child tags. See CarUiToolbarMenuItem in CarUi's attrs.xml for a list of available attributes.
159      *
160      * Example:
161      * <pre>
162      * <MenuItems>
163      *     <MenuItem
164      *         app:title="Foo"/>
165      *     <MenuItem
166      *         app:title="Bar"
167      *         app:icon="@drawable/ic_tracklist"
168      *         app:onClick="xmlMenuItemClicked"/>
169      *     <MenuItem
170      *         app:title="Bar"
171      *         app:checkable="true"
172      *         app:uxRestrictions="FULLY_RESTRICTED"
173      *         app:onClick="xmlMenuItemClicked"/>
174      * </MenuItems>
175      * </pre>
176      *
177      * @return The MenuItems that were loaded from XML.
178      * @see #setMenuItems(List)
179      */
setMenuItems(@mlRes int resId)180     List<MenuItem> setMenuItems(@XmlRes int resId);
181 
182     /** Gets the {@link MenuItem MenuItems} currently displayed */
183     @NonNull
getMenuItems()184     List<MenuItem> getMenuItems();
185 
186     /** Gets a {@link MenuItem} by id. */
187     @Nullable
findMenuItemById(int id)188     MenuItem findMenuItemById(int id);
189 
190     /** Gets a {@link MenuItem} by id. Will throw an IllegalArgumentException if not found. */
191     @NonNull
requireMenuItemById(int id)192     MenuItem requireMenuItemById(int id);
193 
194     /**
195      * Set whether or not to show the {@link MenuItem MenuItems} while searching. Default false.
196      * Even if this is set to true, the {@link MenuItem} created by
197      * {@link MenuItem.Builder#setToSearch()} will still be hidden.
198      */
setShowMenuItemsWhileSearching(boolean showMenuItems)199     void setShowMenuItemsWhileSearching(boolean showMenuItems);
200 
201     /** Returns if {@link MenuItem MenuItems} are shown while searching */
getShowMenuItemsWhileSearching()202     boolean getShowMenuItemsWhileSearching();
203 
204     /**
205      * Sets the search query.
206      */
setSearchQuery(String query)207     void setSearchQuery(String query);
208 
209     /**
210      * Sets the state of the toolbar. This will show/hide the appropriate elements of the toolbar
211      * for the desired state.
212      */
setState(Toolbar.State state)213     void setState(Toolbar.State state);
214 
215     /** Gets the current {@link Toolbar.State} of the toolbar. */
getState()216     Toolbar.State getState();
217 
218     /**
219      * Registers a new {@link Toolbar.OnHeightChangedListener} to the list of listeners. Register a
220      * {@link com.android.car.ui.recyclerview.CarUiRecyclerView} only if there is a toolbar at
221      * the top and a {@link com.android.car.ui.recyclerview.CarUiRecyclerView} in the view and
222      * nothing else. {@link com.android.car.ui.recyclerview.CarUiRecyclerView} will
223      * automatically adjust its height according to the height of the Toolbar.
224      */
registerToolbarHeightChangeListener(Toolbar.OnHeightChangedListener listener)225     void registerToolbarHeightChangeListener(Toolbar.OnHeightChangedListener listener);
226 
227     /** Unregisters an existing {@link Toolbar.OnHeightChangedListener} from the list of
228      * listeners. */
unregisterToolbarHeightChangeListener(Toolbar.OnHeightChangedListener listener)229     boolean unregisterToolbarHeightChangeListener(Toolbar.OnHeightChangedListener listener);
230 
231     /** Registers a new {@link Toolbar.OnTabSelectedListener} to the list of listeners. */
registerOnTabSelectedListener(Toolbar.OnTabSelectedListener listener)232     void registerOnTabSelectedListener(Toolbar.OnTabSelectedListener listener);
233 
234     /** Unregisters an existing {@link Toolbar.OnTabSelectedListener} from the list of listeners. */
unregisterOnTabSelectedListener(Toolbar.OnTabSelectedListener listener)235     boolean unregisterOnTabSelectedListener(Toolbar.OnTabSelectedListener listener);
236 
237     /** Registers a new {@link Toolbar.OnSearchListener} to the list of listeners. */
registerOnSearchListener(Toolbar.OnSearchListener listener)238     void registerOnSearchListener(Toolbar.OnSearchListener listener);
239 
240     /** Unregisters an existing {@link Toolbar.OnSearchListener} from the list of listeners. */
unregisterOnSearchListener(Toolbar.OnSearchListener listener)241     boolean unregisterOnSearchListener(Toolbar.OnSearchListener listener);
242 
243     /** Registers a new {@link Toolbar.OnSearchCompletedListener} to the list of listeners. */
registerOnSearchCompletedListener(Toolbar.OnSearchCompletedListener listener)244     void registerOnSearchCompletedListener(Toolbar.OnSearchCompletedListener listener);
245 
246     /** Unregisters an existing {@link Toolbar.OnSearchCompletedListener} from the list of
247      * listeners. */
unregisterOnSearchCompletedListener(Toolbar.OnSearchCompletedListener listener)248     boolean unregisterOnSearchCompletedListener(Toolbar.OnSearchCompletedListener listener);
249 
250     /** Registers a new {@link Toolbar.OnBackListener} to the list of listeners. */
registerOnBackListener(Toolbar.OnBackListener listener)251     void registerOnBackListener(Toolbar.OnBackListener listener);
252 
253     /** Unregisters an existing {@link Toolbar.OnBackListener} from the list of listeners. */
unregisterOnBackListener(Toolbar.OnBackListener listener)254     boolean unregisterOnBackListener(Toolbar.OnBackListener listener);
255 
256     /** Shows the progress bar */
showProgressBar()257     void showProgressBar();
258 
259     /** Hides the progress bar */
hideProgressBar()260     void hideProgressBar();
261 
262     /** Returns the progress bar */
getProgressBar()263     ProgressBar getProgressBar();
264 }
265