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.view.View;
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 com.android.car.ui.CarUiText;
29 import com.android.car.ui.imewidescreen.CarUiImeSearchListItem;
30 import com.android.car.ui.recyclerview.CarUiListItem;
31 import com.android.car.ui.toolbar.SearchConfig.SearchConfigBuilder;
32 
33 import java.util.List;
34 import java.util.function.Consumer;
35 import java.util.function.Supplier;
36 
37 /**
38  * An interface for accessing a Chassis Toolbar, regardless of how the underlying views are
39  * represented.
40  * <p>
41  * Rendered views will comply with
42  * <a href="https://source.android.com/devices/automotive/hmi/car_ui/appendix_b">customization guardrails</a>
43  */
44 @SuppressWarnings("AndroidJdkLibsChecker")
45 public interface ToolbarController {
46 
47     /**
48      * Sets the title of the toolbar to a string resource.
49      *
50      * <p>The title may not always be shown, for example with one row layout with tabs.
51      */
setTitle(@tringRes int title)52     void setTitle(@StringRes int title);
53 
54     /**
55      * Sets the title of the toolbar to a CharSequence.
56      *
57      * <p>The title may not always be shown, for example with one row layout with tabs.
58      */
setTitle(@ullable CharSequence title)59     void setTitle(@Nullable CharSequence title);
60 
61     /**
62      * Sets the title of the toolbar to a CharSequence.
63      *
64      * <p>The title may not always be shown, for example with one row layout with tabs.
65      */
setTitle(@ullable CarUiText title)66     void setTitle(@Nullable CarUiText title);
67 
68     /**
69      * Gets the current toolbar title.
70      */
getTitle()71     CharSequence getTitle();
72 
73     /**
74      * Sets the subtitle of the toolbar to a string resource.
75      *
76      * <p>The title may not always be shown, for example with one row layout with tabs.
77      */
setSubtitle(@tringRes int title)78     void setSubtitle(@StringRes int title);
79 
80     /**
81      * Sets the subtitle of the toolbar to a CharSequence.
82      *
83      * <p>The title may not always be shown, for example with one row layout with tabs.
84      */
setSubtitle(@ullable CharSequence text)85     void setSubtitle(@Nullable CharSequence text);
86 
87     /**
88      * Sets the subtitle of the toolbar to a CharSequence.
89      *
90      * <p>The title may not always be shown, for example with one row layout with tabs.
91      */
setSubtitle(@ullable CarUiText text)92     void setSubtitle(@Nullable CarUiText text);
93 
94     /**
95      * Gets the current toolbar subtitle.
96      */
getSubtitle()97     CharSequence getSubtitle();
98 
99     /**
100      * Sets the tabs to display.
101      *
102      * @param tabs A list of {@link Tab}
103      * @see #setTabs(List, int)
104      */
setTabs(@ullable List<Tab> tabs)105     void setTabs(@Nullable List<Tab> tabs);
106 
107     /**
108      * Sets the tabs to display. This version will also take the index of a tab to start selected.
109      * This will not cause a callback to the tab's selected listener, unlike using
110      * {@link #setTabs(List)} followed by {@link #selectTab(int)}.
111      *
112      * @param tabs A list of {@link Tab}
113      * @param selectedTab The index of the tab to be initially selected
114      */
setTabs(@ullable List<Tab> tabs, int selectedTab)115     void setTabs(@Nullable List<Tab> tabs, int selectedTab);
116 
117     /**
118      * Gets the list of tabs being displayed. The returned list will be unmodifiable.
119      */
getTabs()120     List<Tab> getTabs();
121 
122     /**
123      * Gets the number of tabs in the toolbar. The tabs can be retrieved using
124      * {@link #getTab(int)}.
125      *
126      * @deprecated Use {@link #getTabs getTabs().size()} instead.
127      */
128     @Deprecated
getTabCount()129     int getTabCount();
130 
131     /**
132      * Gets the index of the tab.
133      *
134      * @deprecated Use {@link #getTabs getTabs().indexOf(tab)} instead.
135      */
136     @Deprecated
getTabPosition(TabLayout.Tab tab)137     int getTabPosition(TabLayout.Tab tab);
138 
139     /**
140      * Adds a tab to this toolbar. You can listen for when it is selected via
141      * {@link #registerOnTabSelectedListener(Toolbar.OnTabSelectedListener)}.
142      *
143      * @deprecated Use {@link #setTabs(List)} instead.
144      */
145     @Deprecated
addTab(TabLayout.Tab tab)146     void addTab(TabLayout.Tab tab);
147 
148     /**
149      * Removes all the tabs.
150      *
151      * @deprecated Use {{@link #setTabs(List)}} instead, passing it
152      * {@link java.util.Collections#emptyList()}
153      */
154     @Deprecated
clearAllTabs()155     void clearAllTabs();
156 
157     /**
158      * Gets a tab added to this toolbar. See
159      * {@link #addTab(TabLayout.Tab)}.
160      *
161      * @deprecated Use {@link #getTabs getTabs().get(position)} instead.
162      */
163     @Deprecated
getTab(int position)164     TabLayout.Tab getTab(int position);
165 
166     /**
167      * Selects a tab added to this toolbar.
168      *
169      * @see #setTabs(List)
170      */
selectTab(int position)171     void selectTab(int position);
172 
173     /**
174      * Returns the index of the currently selected tab. If there are no tabs, this method returns
175      * -1.
176      */
getSelectedTab()177     int getSelectedTab();
178 
179     /**
180      * Sets whether or not tabs should also be shown in the SUBPAGE {@link Toolbar.State}.
181      *
182      * @deprecated When not using {@link #setState(Toolbar.State)}, this method is not necessary.
183      * Simply add tabs to the toolbar when they should be shown.
184      */
185     @Deprecated
setShowTabsInSubpage(boolean showTabs)186     void setShowTabsInSubpage(boolean showTabs);
187 
188     /**
189      * Gets whether or not tabs should also be shown in the SUBPAGE {@link Toolbar.State}.
190      *
191      * @deprecated When not using {@link #setState(Toolbar.State)},
192      * {@link #setShowTabsInSubpage(boolean)} is not necessary.
193      * Simply add tabs to the toolbar when they should be shown.
194      */
195     @Deprecated
getShowTabsInSubpage()196     boolean getShowTabsInSubpage();
197 
198     /**
199      * Sets the logo to display in this toolbar. If navigation icon is being displayed, this logo
200      * will be displayed next to the title.
201      */
setLogo(@rawableRes int resId)202     void setLogo(@DrawableRes int resId);
203 
204     /**
205      * Sets the logo to display in this toolbar. If navigation icon is being displayed, this logo
206      * will be displayed next to the title.
207      */
setLogo(Drawable drawable)208     void setLogo(Drawable drawable);
209 
210     /** Sets the hint for the search bar. */
setSearchHint(@tringRes int resId)211     void setSearchHint(@StringRes int resId);
212 
213     /** Sets the hint for the search bar. */
setSearchHint(CharSequence hint)214     void setSearchHint(CharSequence hint);
215 
216     /** Gets the search hint */
getSearchHint()217     CharSequence getSearchHint();
218 
219     /**
220      * Sets the icon to display in the search box.
221      *
222      * <p>The icon will be lost on configuration change, make sure to set it in onCreate() or
223      * a similar place.
224      */
setSearchIcon(@rawableRes int resId)225     void setSearchIcon(@DrawableRes int resId);
226 
227     /**
228      * Sets the icon to display in the search box.
229      *
230      * <p>The icon will be lost on configuration change, make sure to set it in onCreate() or
231      * a similar place.
232      */
setSearchIcon(Drawable d)233     void setSearchIcon(Drawable d);
234 
235     /**
236      * Sets the search mode, which can enable/disable the search bar in the toolbar.
237      *
238      * See {@link SearchMode}.
239      */
setSearchMode(SearchMode mode)240     void setSearchMode(SearchMode mode);
241 
242     /**
243      * Returns the current search mode, set by {@link #setSearchMode}.
244      */
getSearchMode()245     SearchMode getSearchMode();
246 
247     /**
248      * Sets the {@link Toolbar.NavButtonMode}
249      *
250      * @deprecated Use {@link #setNavButtonMode(NavButtonMode)} instead.
251      */
252     @Deprecated
setNavButtonMode(Toolbar.NavButtonMode style)253     void setNavButtonMode(Toolbar.NavButtonMode style);
254 
255     /** Sets the {@link NavButtonMode} */
setNavButtonMode(NavButtonMode mode)256     void setNavButtonMode(NavButtonMode mode);
257 
258     /**
259      * Gets the {@link NavButtonMode}.
260      */
getNavButtonMode()261     NavButtonMode getNavButtonMode();
262 
263     /** Show/hide the background. When hidden, the toolbar is completely transparent. */
setBackgroundShown(boolean shown)264     void setBackgroundShown(boolean shown);
265 
266     /** Returns true is the toolbar background is shown */
getBackgroundShown()267     boolean getBackgroundShown();
268 
269     /**
270      * Sets the {@link MenuItem Menuitems} to display.
271      */
setMenuItems(@ullable List<MenuItem> items)272     void setMenuItems(@Nullable List<MenuItem> items);
273 
274     /**
275      * Sets the {@link MenuItem Menuitems} to display to a list defined in XML.
276      *
277      * <p>If this method is called twice with the same argument (and {@link #setMenuItems(List)}
278      * wasn't called), nothing will happen the second time, even if the MenuItems were changed.
279      *
280      * <p>The XML file must have one <MenuItems> tag, with a variable number of <MenuItem>
281      * child tags. See CarUiToolbarMenuItem in CarUi's attrs.xml for a list of available attributes.
282      *
283      * Example:
284      * <pre>{@code
285      * <MenuItems>
286      *     <MenuItem
287      *         app:title="Foo"/>
288      *     <MenuItem
289      *         app:title="Bar"
290      *         app:icon="@drawable/ic_tracklist"
291      *         app:onClick="xmlMenuItemClicked"/>
292      *     <MenuItem
293      *         app:title="Bar"
294      *         app:checkable="true"
295      *         app:uxRestrictions="FULLY_RESTRICTED"
296      *         app:onClick="xmlMenuItemClicked"/>
297      * </MenuItems>
298      * }</pre>
299      *
300      * @return The MenuItems that were loaded from XML.
301      * @see #setMenuItems(List)
302      */
setMenuItems(@mlRes int resId)303     List<MenuItem> setMenuItems(@XmlRes int resId);
304 
305     /** Gets the {@link MenuItem MenuItems} currently displayed */
306     @NonNull
getMenuItems()307     List<MenuItem> getMenuItems();
308 
309     /** Gets a {@link MenuItem} by id. */
310     @Nullable
findMenuItemById(int id)311     MenuItem findMenuItemById(int id);
312 
313     /** Gets a {@link MenuItem} by id. Will throw an IllegalArgumentException if not found. */
314     @NonNull
requireMenuItemById(int id)315     MenuItem requireMenuItemById(int id);
316 
317     /**
318      * Set whether or not to show the {@link MenuItem MenuItems} while searching. Default false.
319      * Even if this is set to true, the {@link MenuItem} created by
320      * {@link MenuItem.Builder#setToSearch()} will still be hidden.
321      */
setShowMenuItemsWhileSearching(boolean showMenuItems)322     void setShowMenuItemsWhileSearching(boolean showMenuItems);
323 
324     /** Returns if {@link MenuItem MenuItems} are shown while searching */
getShowMenuItemsWhileSearching()325     boolean getShowMenuItemsWhileSearching();
326 
327     /**
328      * Sets the search query.
329      */
setSearchQuery(String query)330     void setSearchQuery(String query);
331 
332     /**
333      * Sets the state of the toolbar. This will show/hide the appropriate elements of the toolbar
334      * for the desired state.
335      *
336      * @deprecated Instead of using setState(), simply add the elements you want to see when
337      * you want to see them. The back button visibility can be controlled with the
338      * {@link com.android.car.ui.toolbar.Toolbar.NavButtonMode#DISABLED} enum,
339      * and the search bar can also be controlled with the {@link SearchMode} enum.
340      */
341     @Deprecated
setState(Toolbar.State state)342     void setState(Toolbar.State state);
343 
344     /**
345      * Gets the current {@link Toolbar.State} of the toolbar.
346      *
347      * @deprecated See {@link #setState(Toolbar.State)} for details.
348      */
349     @Deprecated
getState()350     Toolbar.State getState();
351 
352     /**
353      * Returns whether or not the state of the toolbar was previously set.
354      *
355      * @deprecated See {@link #setState(Toolbar.State)} for details.
356      */
357     @Deprecated
isStateSet()358     boolean isStateSet();
359 
360     /**
361      * Registers a new {@link Toolbar.OnTabSelectedListener} to the list of listeners.
362      *
363      * @deprecated The tabs set via {@link #setTabs(List)} have their own selected callbacks.
364      */
365     @Deprecated
registerOnTabSelectedListener(Toolbar.OnTabSelectedListener listener)366     void registerOnTabSelectedListener(Toolbar.OnTabSelectedListener listener);
367 
368     /**
369      * Unregisters an existing {@link Toolbar.OnTabSelectedListener} from the list of listeners.
370      *
371      * @deprecated The tabs set via {@link #setTabs(List)} have their own selected callbacks.
372      */
373     @Deprecated
unregisterOnTabSelectedListener(Toolbar.OnTabSelectedListener listener)374     boolean unregisterOnTabSelectedListener(Toolbar.OnTabSelectedListener listener);
375 
376     /** Registers a new {@link Toolbar.OnSearchListener} to the list of listeners. */
registerOnSearchListener(Toolbar.OnSearchListener listener)377     void registerOnSearchListener(Toolbar.OnSearchListener listener);
378 
379     /** Unregisters an existing {@link Toolbar.OnSearchListener} from the list of listeners. */
unregisterOnSearchListener(Toolbar.OnSearchListener listener)380     boolean unregisterOnSearchListener(Toolbar.OnSearchListener listener);
381 
382     /** Registers a new {@link Consumer} to the list of listeners. */
registerSearchListener(Consumer<String> listener)383     void registerSearchListener(Consumer<String> listener);
384 
385     /** Unregisters an existing {@link Consumer} from the list of listeners. */
unregisterSearchListener(Consumer<String> listener)386     boolean unregisterSearchListener(Consumer<String> listener);
387 
388     /**
389      * Sets the search info to be displayed within the widescreen IME.
390      */
setSearchConfig(@ullable SearchConfig searchConfig)391     void setSearchConfig(@Nullable SearchConfig searchConfig);
392 
393     /**
394      * Returns the capabilities of what toolbar can do with Search
395      */
getSearchCapabilities()396     SearchCapabilities getSearchCapabilities();
397 
398     /**
399      * Returns true if the toolbar can display search result items. One example of this is when the
400      * system is configured to display search items in the IME instead of in the app.
401      *
402      * @deprecated See {@link SearchCapabilities#canShowSearchResultItems()} for details.
403      */
404     @Deprecated
canShowSearchResultItems()405     boolean canShowSearchResultItems();
406 
407     /**
408      * Returns true if the app is allowed to set search results view.
409      *
410      * @deprecated See {@link SearchCapabilities#canShowSearchResultsView()} for details.
411      */
412     @Deprecated
canShowSearchResultsView()413     boolean canShowSearchResultsView();
414 
415     /**
416      * Add a view within a container that will animate with the wide screen IME to display search
417      * results.
418      *
419      * <p>Note: Apps can only call this method if the package name is allowed via OEM to render
420      * their view.  To check if the application have the permission to do so or not first call
421      * {@link SearchCapabilities#canShowSearchResultsView()}. If the app is not allowed this
422      * method
423      * will throw an {@link IllegalStateException}
424      *
425      * @param view to be added in the container.
426      * @deprecated See {@link SearchConfigBuilder#setSearchResultsView(View)} for details.
427      */
428     @Deprecated
setSearchResultsView(@ullable View view)429     void setSearchResultsView(@Nullable View view);
430 
431     /**
432      * Set the icon to be displayed within the input field of IME window.
433      *
434      * @deprecated See {@link SearchConfigBuilder#setSearchResultsInputViewIcon(Drawable)} )} for
435      * details.
436      */
437     @Deprecated
setSearchResultsInputViewIcon(@onNull Drawable drawable)438     void setSearchResultsInputViewIcon(@NonNull Drawable drawable);
439 
440     /**
441      * Sets list of search item {@link CarUiListItem} to be displayed in the IMS
442      * template. This method should be called when system is running in a wide screen mode. Apps
443      * can check that by using {@link SearchCapabilities#canShowSearchResultItems()}
444      * Else, this method will throw an {@link IllegalStateException}
445      *
446      * @deprecated See {@link SearchConfigBuilder#setSearchResultItems(List)} )} for details.
447      */
448     @Deprecated
setSearchResultItems(List<? extends CarUiImeSearchListItem> searchItems)449     void setSearchResultItems(List<? extends CarUiImeSearchListItem> searchItems);
450 
451     /**
452      * Registers a new {@link Toolbar.OnSearchCompletedListener} to the list of listeners.
453      *
454      * @deprecated Use {@link #registerSearchCompletedListener(Runnable)} instead.
455      */
456     @Deprecated
registerOnSearchCompletedListener(Toolbar.OnSearchCompletedListener listener)457     void registerOnSearchCompletedListener(Toolbar.OnSearchCompletedListener listener);
458 
459     /**
460      * Unregisters an existing {@link Toolbar.OnSearchCompletedListener} from the list of
461      * listeners.
462      *
463      * @deprecated Use {@link #unregisterSearchCompletedListener(Runnable)} instead.
464      */
465     @Deprecated
unregisterOnSearchCompletedListener(Toolbar.OnSearchCompletedListener listener)466     boolean unregisterOnSearchCompletedListener(Toolbar.OnSearchCompletedListener listener);
467 
468     /** Registers a new {@link Runnable} to the list of listeners. */
registerSearchCompletedListener(Runnable listener)469     void registerSearchCompletedListener(Runnable listener);
470 
471     /**
472      * Unregisters an existing {@link Runnable} from the list of
473      * listeners.
474      */
unregisterSearchCompletedListener(Runnable listener)475     boolean unregisterSearchCompletedListener(Runnable listener);
476 
477     /**
478      * Registers a new {@link Toolbar.OnBackListener} to the list of listeners.
479      *
480      * @deprecated Use {@link #registerBackListener(Supplier)} instead.
481      */
482     @Deprecated
registerOnBackListener(Toolbar.OnBackListener listener)483     void registerOnBackListener(Toolbar.OnBackListener listener);
484 
485     /**
486      * Unregisters an existing {@link Toolbar.OnBackListener} from the list of listeners.
487      *
488      * @deprecated Use {@link #unregisterBackListener(Supplier)} instead.
489      */
490     @Deprecated
unregisterOnBackListener(Toolbar.OnBackListener listener)491     boolean unregisterOnBackListener(Toolbar.OnBackListener listener);
492 
493     /** Registers a new {@link Supplier} to the list of listeners. */
registerBackListener(Supplier<Boolean> listener)494     void registerBackListener(Supplier<Boolean> listener);
495 
496     /** Unregisters an existing {@link Runnable} from the list of listeners. */
unregisterBackListener(Supplier<Boolean> listener)497     boolean unregisterBackListener(Supplier<Boolean> listener);
498 
499     /** Gets a {@link ProgressBarController} */
getProgressBar()500     ProgressBarController getProgressBar();
501 }
502