1 /* 2 * Copyright (C) 2021 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.notification.template; 18 19 import android.annotation.ColorInt; 20 import android.annotation.Nullable; 21 import android.content.Context; 22 import android.graphics.drawable.Drawable; 23 import android.util.AttributeSet; 24 import android.view.View; 25 import android.widget.ImageView; 26 import android.widget.LinearLayout; 27 import android.widget.TextView; 28 29 import com.android.car.notification.R; 30 31 /** 32 * Represents a button that can have some text and a drawable. 33 */ 34 public class CarNotificationActionButton extends LinearLayout { 35 private final ImageView mImageView; 36 private final TextView mTextView; 37 private final boolean mUseIconsInActionButton; 38 CarNotificationActionButton(Context context)39 public CarNotificationActionButton(Context context) { 40 this(context, /* attrs= */ null); 41 } 42 CarNotificationActionButton(Context context, @Nullable AttributeSet attrs)43 public CarNotificationActionButton(Context context, @Nullable AttributeSet attrs) { 44 this(context, attrs, /* defStyleAttr= */ 0); 45 } 46 CarNotificationActionButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr)47 public CarNotificationActionButton(Context context, @Nullable AttributeSet attrs, 48 int defStyleAttr) { 49 this(context, attrs, defStyleAttr, /* defStyleRes= */ 0); 50 } 51 CarNotificationActionButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)52 public CarNotificationActionButton(Context context, AttributeSet attrs, int defStyleAttr, 53 int defStyleRes) { 54 super(context, attrs, defStyleAttr, defStyleRes); 55 56 inflate(context, R.layout.car_notification_action_button, /* root= */ this); 57 mTextView = findViewById(R.id.car_action_button_text); 58 mImageView = findViewById(R.id.car_action_button_icon); 59 mUseIconsInActionButton = 60 context.getResources().getBoolean(R.bool.config_showIconsInActionButtons); 61 } 62 63 /** 64 * Set text for button. 65 * 66 * If text is null, then {@link TextView} is hidden. 67 */ setText(String text)68 public void setText(String text) { 69 mTextView.setText(text); 70 if (text == null) { 71 mTextView.setVisibility(View.GONE); 72 } else { 73 mTextView.setVisibility(View.VISIBLE); 74 } 75 } 76 77 /** 78 * Set text color for button. 79 */ setTextColor(@olorInt int color)80 public void setTextColor(@ColorInt int color) { 81 mTextView.setTextColor(color); 82 } 83 84 /** 85 * @return text that has been set for button. 86 */ getText()87 public CharSequence getText() { 88 return mTextView.getText(); 89 } 90 91 /** 92 * Set drawable for button. 93 * 94 * If drawable is null, then {@link ImageView} is hidden. 95 */ setImageDrawable(Drawable drawable)96 public void setImageDrawable(Drawable drawable) { 97 if (!mUseIconsInActionButton) { 98 mImageView.setVisibility(View.GONE); 99 return; 100 } 101 102 mImageView.setImageDrawable(drawable); 103 if (drawable == null) { 104 mImageView.setVisibility(View.GONE); 105 } else { 106 mImageView.setVisibility(View.VISIBLE); 107 } 108 } 109 110 /** 111 * @return drawable that has been set for button. 112 */ getDrawable()113 public Drawable getDrawable() { 114 return mImageView.getDrawable(); 115 } 116 } 117