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 package com.android.car.notification.template; 17 18 import android.annotation.ColorInt; 19 import android.app.Notification; 20 import android.content.res.ColorStateList; 21 import android.os.Bundle; 22 import android.view.View; 23 import android.widget.ProgressBar; 24 25 import com.android.car.notification.AlertEntry; 26 import com.android.car.notification.NotificationClickHandlerFactory; 27 import com.android.car.notification.NotificationUtils; 28 import com.android.car.notification.R; 29 30 /** 31 * Basic notification view template that displays a progress bar notification. 32 * This template is only used in notification center and never as a heads-up notification. 33 */ 34 public class ProgressNotificationViewHolder extends CarNotificationBaseViewHolder { 35 private final CarNotificationHeaderView mHeaderView; 36 private final CarNotificationBodyView mBodyView; 37 private final CarNotificationActionsView mActionsView; 38 private final ProgressBar mProgressBarView; 39 @ColorInt 40 private final int mCardBackgroundColor; 41 @ColorInt 42 private final int mProgressBarColor; 43 @ColorInt 44 private final int mProgressBarBackgroundColor; 45 private NotificationClickHandlerFactory mClickHandlerFactory; 46 ProgressNotificationViewHolder(View view, NotificationClickHandlerFactory clickHandlerFactory)47 public ProgressNotificationViewHolder(View view, 48 NotificationClickHandlerFactory clickHandlerFactory) { 49 super(view, clickHandlerFactory); 50 mHeaderView = view.findViewById(R.id.notification_header); 51 mBodyView = view.findViewById(R.id.notification_body); 52 mActionsView = view.findViewById(R.id.notification_actions); 53 mProgressBarView = view.findViewById(R.id.progress_bar); 54 mCardBackgroundColor = getContext().getColor(R.color.notification_background_color); 55 mProgressBarColor = getContext().getColor(R.color.progress_bar_color); 56 mProgressBarBackgroundColor = getContext().getColor(R.color.progress_bar_bg_color); 57 mClickHandlerFactory = clickHandlerFactory; 58 } 59 60 /** 61 * Binds a {@link AlertEntry} to a car progress notification template. 62 */ 63 @Override bind(AlertEntry alertEntry, boolean isInGroup, boolean isHeadsUp, boolean isSeen)64 public void bind(AlertEntry alertEntry, boolean isInGroup, 65 boolean isHeadsUp, boolean isSeen) { 66 super.bind(alertEntry, isInGroup, isHeadsUp, isSeen); 67 bindBody(alertEntry); 68 mHeaderView.bind(alertEntry, isInGroup); 69 mActionsView.bind(mClickHandlerFactory, alertEntry); 70 } 71 72 /** 73 * Private method that binds the data to the view. 74 */ bindBody(AlertEntry alertEntry)75 private void bindBody(AlertEntry alertEntry) { 76 Notification notification = alertEntry.getNotification(); 77 78 Bundle extraData = notification.extras; 79 CharSequence title = extraData.getCharSequence(Notification.EXTRA_TITLE); 80 CharSequence text = extraData.getCharSequence(Notification.EXTRA_TEXT); 81 82 mBodyView.bind(title, text, 83 alertEntry.getStatusBarNotification(), 84 notification.getLargeIcon(), /* titleIcon= */ null, /* countText= */ null, 85 notification.showsTime() ? notification.when : null); 86 87 mProgressBarView.setVisibility(View.VISIBLE); 88 boolean isIndeterminate = extraData.getBoolean(Notification.EXTRA_PROGRESS_INDETERMINATE); 89 int progress = extraData.getInt(Notification.EXTRA_PROGRESS); 90 int progressMax = extraData.getInt(Notification.EXTRA_PROGRESS_MAX); 91 mProgressBarView.setIndeterminate(isIndeterminate); 92 mProgressBarView.setMax(progressMax); 93 mProgressBarView.setProgress(progress); 94 mProgressBarView.setProgressTintList(ColorStateList.valueOf(mProgressBarColor)); 95 mProgressBarView.setProgressBackgroundTintList( 96 ColorStateList.valueOf(mProgressBarBackgroundColor)); 97 98 // optional color 99 if (notification.color != Notification.COLOR_DEFAULT) { 100 int calculatedColor = NotificationUtils.resolveContrastColor( 101 notification.color, mCardBackgroundColor); 102 ColorStateList progressBarColorStateList = ColorStateList.valueOf(calculatedColor); 103 mProgressBarView.setProgressTintList(progressBarColorStateList); 104 } 105 } 106 107 /** 108 * Resets the basic notification view empty for recycling. 109 */ 110 @Override reset()111 void reset() { 112 super.reset(); 113 mProgressBarView.setProgress(0); 114 mProgressBarView.setVisibility(View.GONE); 115 mProgressBarView.setProgressTintList(null); 116 } 117 } 118