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.content.res.TypedArray; 23 import android.graphics.drawable.Drawable; 24 import android.util.AttributeSet; 25 import android.view.View; 26 import android.widget.ImageView; 27 import android.widget.LinearLayout; 28 import android.widget.TextView; 29 30 import com.android.car.notification.R; 31 32 /** 33 * Represents a button that can have some text and a drawable. 34 */ 35 public class CarNotificationActionButton extends LinearLayout { 36 private final ImageView mImageView; 37 private final TextView mTextView; 38 private final boolean mUseIconsInActionButton; 39 @ColorInt 40 private final int mDefaultTextColor; 41 CarNotificationActionButton(Context context)42 public CarNotificationActionButton(Context context) { 43 this(context, /* attrs= */ null); 44 } 45 CarNotificationActionButton(Context context, @Nullable AttributeSet attrs)46 public CarNotificationActionButton(Context context, @Nullable AttributeSet attrs) { 47 this(context, attrs, /* defStyleAttr= */ 0); 48 } 49 CarNotificationActionButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr)50 public CarNotificationActionButton(Context context, @Nullable AttributeSet attrs, 51 int defStyleAttr) { 52 this(context, attrs, defStyleAttr, /* defStyleRes= */ 0); 53 } 54 CarNotificationActionButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)55 public CarNotificationActionButton(Context context, AttributeSet attrs, int defStyleAttr, 56 int defStyleRes) { 57 super(context, attrs, defStyleAttr, defStyleRes); 58 59 inflate(context, R.layout.car_notification_action_button, /* root= */ this); 60 mTextView = findViewById(R.id.car_action_button_text); 61 mImageView = findViewById(R.id.car_action_button_icon); 62 mUseIconsInActionButton = 63 context.getResources().getBoolean(R.bool.config_showIconsInActionButtons); 64 65 if (attrs != null) { 66 TypedArray attributes = 67 context.obtainStyledAttributes(attrs, R.styleable.CarNotificationActionButton); 68 int color = attributes.getColor( 69 R.styleable.CarNotificationActionButton_textColor, /* defaultValue= */ 70 context.getResources().getColor(R.color.notification_accent_color)); 71 attributes.recycle(); 72 mDefaultTextColor = color; 73 } else { 74 mDefaultTextColor = context.getResources().getColor(R.color.notification_accent_color); 75 } 76 mTextView.setTextColor(mDefaultTextColor); 77 } 78 79 /** 80 * Set text for button. 81 * 82 * If text is null, then {@link TextView} is hidden. 83 */ setText(String text)84 public void setText(String text) { 85 mTextView.setText(text); 86 if (text == null) { 87 mTextView.setVisibility(View.GONE); 88 } else { 89 mTextView.setVisibility(View.VISIBLE); 90 } 91 } 92 93 /** 94 * Set text color for button. 95 */ setTextColor(@olorInt int color)96 public void setTextColor(@ColorInt int color) { 97 mTextView.setTextColor(color); 98 } 99 100 /** 101 * @return default text color defined by 102 * {@code R.styleable.CarNotificationActionButton_textColor} 103 */ 104 @ColorInt getDefaultTextColor()105 public int getDefaultTextColor() { 106 return mDefaultTextColor; 107 } 108 109 /** 110 * @return text that has been set for button. 111 */ getText()112 public CharSequence getText() { 113 return mTextView.getText(); 114 } 115 116 /** 117 * Set drawable for button. 118 * 119 * If drawable is null, then {@link ImageView} is hidden. 120 */ setImageDrawable(Drawable drawable)121 public void setImageDrawable(Drawable drawable) { 122 if (!mUseIconsInActionButton) { 123 mImageView.setVisibility(View.GONE); 124 return; 125 } 126 127 mImageView.setImageDrawable(drawable); 128 if (drawable == null) { 129 mImageView.setVisibility(View.GONE); 130 } else { 131 mImageView.setVisibility(View.VISIBLE); 132 } 133 } 134 135 /** 136 * @return drawable that has been set for button. 137 */ getDrawable()138 public Drawable getDrawable() { 139 return mImageView.getDrawable(); 140 } 141 } 142