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     @Retention(SOURCE)
60     @IntDef({SLOT_MAIN, SLOT_LEFT, SLOT_RIGHT, SLOT_EXPAND_COLLAPSE})
61     @interface SlotPosition {}
62 
63     /** Slot used for main actions {@link ControlBar}, usually at the bottom center */
64     int SLOT_MAIN = 0;
65     /** Slot used to host 'move left', 'rewind', 'previous' or similar secondary actions,
66      * usually at the left of the main action on the bottom row */
67     int SLOT_LEFT = 1;
68     /** Slot used to host 'move right', 'fast-forward', 'next' or similar secondary actions,
69      * usually at the right of the main action on the bottom row */
70     int SLOT_RIGHT = 2;
71     /** Slot reserved for the expand/collapse button */
72     int SLOT_EXPAND_COLLAPSE = 3;
73 
74 
75     /**
76      * Returns an index for a well-known slot position, adapted to the number of columns.
77      */
getSlotIndex(@lotPosition int slotPosition, int numColumns)78     static int getSlotIndex(@SlotPosition int slotPosition, int numColumns) {
79         switch (slotPosition) {
80             case SLOT_MAIN:
81                 return numColumns / 2;
82             case SLOT_LEFT:
83                 return numColumns < 3 ? -1 : (numColumns / 2) - 1;
84             case SLOT_RIGHT:
85                 return numColumns < 2 ? -1 : (numColumns / 2) + 1;
86             case SLOT_EXPAND_COLLAPSE:
87                 return numColumns - 1;
88             default:
89                 throw new IllegalArgumentException("Unknown position: " + slotPosition);
90         }
91     }
92 }
93