1 /*
2  * Copyright (C) 2023 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.carlauncher;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.util.AttributeSet;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.widget.TextView;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 import androidx.constraintlayout.widget.ConstraintLayout;
29 
30 /**
31  * Banner view to display text with multiple cta actions
32  */
33 public class Banner extends ConstraintLayout {
34 
35     private final TextView mFirstButton;
36     private final TextView mSecondButton;
37     private final TextView mTitleTextView;
38 
Banner(@onNull Context context)39     public Banner(@NonNull Context context) {
40         this(context, null);
41     }
42 
Banner(@onNull Context context, @Nullable AttributeSet attrs)43     public Banner(@NonNull Context context, @Nullable AttributeSet attrs) {
44         this(context, attrs, 0);
45     }
46 
Banner(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)47     public Banner(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
48         this(context, attrs, defStyleAttr, 0);
49     }
50 
Banner( @onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)51     public Banner(
52             @NonNull Context context,
53             @Nullable AttributeSet attrs,
54             int defStyleAttr,
55             int defStyleRes) {
56         super(context, attrs, defStyleAttr, defStyleRes);
57 
58         LayoutInflater inflater = LayoutInflater.from(getContext());
59         inflater.inflate(R.layout.banner, this);
60 
61         mFirstButton = requireViewById(R.id.banner_first_button);
62         mSecondButton = requireViewById(R.id.banner_second_button);
63         mTitleTextView = requireViewById(R.id.banner_title);
64 
65         TypedArray attrArray = context.getTheme().obtainStyledAttributes(
66                 attrs,
67                 R.styleable.Banner,
68                 defStyleAttr,
69                 defStyleRes);
70 
71         try {
72             setFirstButtonText(attrArray.getString(R.styleable.Banner_first_button_text));
73             setSecondButtonText(attrArray.getString(R.styleable.Banner_second_button_text));
74             setTitleText(attrArray.getString(R.styleable.Banner_title_text));
75         } finally {
76             attrArray.recycle();
77         }
78     }
79 
80     /**
81      * Sets the text to be displayed on the first button
82      *
83      * @param text text to be displayed
84      */
setFirstButtonText(String text)85     public void setFirstButtonText(String text) {
86         mFirstButton.setText(text);
87     }
88 
89     /**
90      * Register a callback to be invoked when the first button is clicked.
91      *
92      * @param listener The callback that will run on clicking the button
93      */
setFirstButtonOnClickListener(@ullable View.OnClickListener listener)94     public void setFirstButtonOnClickListener(@Nullable View.OnClickListener listener) {
95         mFirstButton.setOnClickListener(listener);
96     }
97 
98     /**
99      * Sets the text to be displayed on the second button
100      *
101      * @param text text to be displayed
102      */
setSecondButtonText(String text)103     public void setSecondButtonText(String text) {
104         mSecondButton.setText(text);
105     }
106 
107     /**
108      * Register a callback to be invoked when the first button is clicked.
109      *
110      * @param listener The callback that will run on clicking the button
111      */
setSecondButtonOnClickListener(@ullable View.OnClickListener listener)112     public void setSecondButtonOnClickListener(@Nullable View.OnClickListener listener) {
113         mSecondButton.setOnClickListener(listener);
114     }
115 
116     /**
117      * Sets the primary text to be displayed on banner
118      *
119      * @param text text to be displayed
120      */
setTitleText(String text)121     public void setTitleText(String text) {
122         mTitleTextView.setText(text);
123     }
124 }
125