1 /*
2  * Copyright 2019 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 
18 package com.android.car.apps.common;
19 import static java.lang.annotation.RetentionPolicy.SOURCE;
20 
21 import android.graphics.drawable.Drawable;
22 import android.view.View;
23 import android.widget.ImageButton;
24 
25 import androidx.annotation.IntDef;
26 import androidx.annotation.Nullable;
27 
28 import java.lang.annotation.Retention;
29 
30 /**
31  * Interface for managing buttons to be shown in a control bar
32  */
33 public interface CarControlBar {
34 
35     /**
36      * Sets or clears the view to be shown at the given slot position. The view may not be shown if
37      * the provided slot position is hidden
38      */
setView(@ullable View view, @SlotPosition int slotPosition)39     void setView(@Nullable View view, @SlotPosition int slotPosition);
40 
41     /**
42      * Sets a list of views to be shown in the control bar. Not all the views are guaranteed to be
43      * shown, only as many as the control bar has space to show.
44      */
setViews(@ullable View[] views)45     void setViews(@Nullable View[] views);
46 
47     /**
48      * Create an ImageButton with the provided icon to be used in this control bar.
49      */
createIconButton(Drawable icon)50     ImageButton createIconButton(Drawable icon);
51 
52     /**
53      * Create an ImageButton with the provided icon to be used in this control bar, and the view
54      * id to inflate.
55      */
createIconButton(Drawable icon, int viewId)56     ImageButton createIconButton(Drawable icon, int viewId);
57 
58 
59     /** Constant for {@link #getFocusedViewIndex} and {@link #setFocusAtViewIndex}. */
60     int INVALID_VIEW_INDEX = -1;
61 
62     /**
63      * Returns the index of the focused view, or {@link #INVALID_VIEW_INDEX} if no view is focused.
64      */
getFocusedViewIndex()65     int getFocusedViewIndex();
66 
67     /**
68      *  Sets the focus onto the view designated by the given index (typically obtained by
69      *  {@link #getFocusedViewIndex}.
70      */
setFocusAtViewIndex(int viewIndex)71     void setFocusAtViewIndex(int viewIndex);
72 
73     @Retention(SOURCE)
74     @IntDef({SLOT_MAIN, SLOT_LEFT, SLOT_RIGHT, SLOT_EXPAND_COLLAPSE})
75     @interface SlotPosition {}
76 
77     /** Slot used for main actions {@link ControlBar}, usually at the bottom center */
78     int SLOT_MAIN = 0;
79     /** Slot used to host 'move left', 'rewind', 'previous' or similar secondary actions,
80      * usually at the left of the main action on the bottom row */
81     int SLOT_LEFT = 1;
82     /** Slot used to host 'move right', 'fast-forward', 'next' or similar secondary actions,
83      * usually at the right of the main action on the bottom row */
84     int SLOT_RIGHT = 2;
85     /** Slot reserved for the expand/collapse button */
86     int SLOT_EXPAND_COLLAPSE = 3;
87 
88 
89     /**
90      * Returns an index for a well-known slot position, adapted to the number of columns.
91      */
getSlotIndex(@lotPosition int slotPosition, int numColumns)92     static int getSlotIndex(@SlotPosition int slotPosition, int numColumns) {
93         switch (slotPosition) {
94             case SLOT_MAIN:
95                 return numColumns / 2;
96             case SLOT_LEFT:
97                 return numColumns < 3 ? -1 : (numColumns / 2) - 1;
98             case SLOT_RIGHT:
99                 return numColumns < 2 ? -1 : (numColumns / 2) + 1;
100             case SLOT_EXPAND_COLLAPSE:
101                 return numColumns - 1;
102             default:
103                 throw new IllegalArgumentException("Unknown position: " + slotPosition);
104         }
105     }
106 }
107