1 /* 2 * Copyright (C) 2018 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.Icon; 24 import android.text.TextUtils; 25 import android.util.AttributeSet; 26 import android.view.View; 27 import android.widget.ImageView; 28 import android.widget.RelativeLayout; 29 import android.widget.TextView; 30 31 import androidx.annotation.VisibleForTesting; 32 33 import com.android.car.notification.NotificationUtils; 34 import com.android.car.notification.R; 35 36 /** 37 * Common notification body that consists of a title line, a content text line, and an image icon on 38 * the end. 39 * 40 * <p> For example, for a messaging notification, the title is the sender's name, 41 * the content is the message, and the image icon is the sender's avatar. 42 */ 43 public class CarNotificationBodyView extends RelativeLayout { 44 private static final int DEFAULT_MAX_LINES = 1; 45 @ColorInt 46 private final int mDefaultPrimaryTextColor; 47 @ColorInt 48 private final int mDefaultSecondaryTextColor; 49 private boolean mShowBigIcon; 50 private int mMaxLines; 51 private TextView mTitleView; 52 private TextView mContentView; 53 private ImageView mIconView; 54 CarNotificationBodyView(Context context)55 public CarNotificationBodyView(Context context) { 56 super(context); 57 } 58 CarNotificationBodyView(Context context, AttributeSet attrs)59 public CarNotificationBodyView(Context context, AttributeSet attrs) { 60 super(context, attrs); 61 init(attrs); 62 } 63 CarNotificationBodyView(Context context, AttributeSet attrs, int defStyleAttr)64 public CarNotificationBodyView(Context context, AttributeSet attrs, int defStyleAttr) { 65 super(context, attrs, defStyleAttr); 66 init(attrs); 67 } 68 CarNotificationBodyView( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)69 public CarNotificationBodyView( 70 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 71 super(context, attrs, defStyleAttr, defStyleRes); 72 init(attrs); 73 } 74 75 { 76 mDefaultPrimaryTextColor = 77 NotificationUtils.getAttrColor(getContext(), android.R.attr.textColorPrimary); 78 mDefaultSecondaryTextColor = 79 NotificationUtils.getAttrColor(getContext(), android.R.attr.textColorSecondary); getContext()80 inflate(getContext(), R.layout.car_notification_body_view, /* root= */ this); 81 } 82 init(AttributeSet attrs)83 private void init(AttributeSet attrs) { 84 TypedArray attributes = 85 getContext().obtainStyledAttributes(attrs, R.styleable.CarNotificationBodyView); 86 mShowBigIcon = 87 attributes.getBoolean(R.styleable.CarNotificationBodyView_showBigIcon, 88 /* defValue= */ false); 89 mMaxLines = attributes.getInteger(R.styleable.CarNotificationBodyView_maxLines, 90 /* defValue= */ DEFAULT_MAX_LINES); 91 attributes.recycle(); 92 } 93 94 @Override onFinishInflate()95 protected void onFinishInflate() { 96 super.onFinishInflate(); 97 mTitleView = findViewById(R.id.notification_body_title); 98 mContentView = findViewById(R.id.notification_body_content); 99 mIconView = findViewById(R.id.notification_body_icon); 100 } 101 102 /** 103 * Binds the notification body. 104 * 105 * @param title the primary text. 106 * @param content the secondary text. 107 * @param icon the large icon, usually used for avatars. 108 */ bind(CharSequence title, @Nullable CharSequence content, @Nullable Icon icon)109 public void bind(CharSequence title, @Nullable CharSequence content, @Nullable Icon icon) { 110 setVisibility(View.VISIBLE); 111 112 mTitleView.setVisibility(View.VISIBLE); 113 mTitleView.setText(title); 114 115 if (!TextUtils.isEmpty(content)) { 116 mContentView.setVisibility(View.VISIBLE); 117 mContentView.setMaxLines(mMaxLines); 118 mContentView.setText(content); 119 } 120 121 if (icon != null && mShowBigIcon) { 122 mIconView.setVisibility(View.VISIBLE); 123 mIconView.setImageIcon(icon); 124 } 125 } 126 bindTitleAndMessage(CharSequence title, CharSequence content)127 public void bindTitleAndMessage(CharSequence title, CharSequence content) { 128 setVisibility(View.VISIBLE); 129 130 mTitleView.setVisibility(View.VISIBLE); 131 mTitleView.setText(title); 132 if (!TextUtils.isEmpty(content)) { 133 mContentView.setVisibility(View.VISIBLE); 134 mContentView.setMaxLines(mMaxLines); 135 mContentView.setText(content); 136 mIconView.setVisibility(View.GONE); 137 } 138 } 139 140 /** 141 * Sets the primary text color. 142 */ setSecondaryTextColor(@olorInt int color)143 public void setSecondaryTextColor(@ColorInt int color) { 144 mContentView.setTextColor(color); 145 } 146 147 /** 148 * Sets the secondary text color. 149 */ setPrimaryTextColor(@olorInt int color)150 public void setPrimaryTextColor(@ColorInt int color) { 151 mTitleView.setTextColor(color); 152 } 153 154 /** 155 * Resets the notification actions empty for recycling. 156 */ reset()157 public void reset() { 158 setVisibility(View.GONE); 159 mTitleView.setVisibility(View.GONE); 160 mContentView.setVisibility(View.GONE); 161 mIconView.setVisibility(View.GONE); 162 setPrimaryTextColor(mDefaultPrimaryTextColor); 163 setSecondaryTextColor(mDefaultSecondaryTextColor); 164 } 165 166 @VisibleForTesting getTitleView()167 TextView getTitleView() { 168 return mTitleView; 169 } 170 171 @VisibleForTesting getContentView()172 TextView getContentView() { 173 return mContentView; 174 } 175 } 176