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