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     private NotificationClickHandlerFactory mClickHandlerFactory;
42 
ProgressNotificationViewHolder(View view, NotificationClickHandlerFactory clickHandlerFactory)43     public ProgressNotificationViewHolder(View view,
44             NotificationClickHandlerFactory clickHandlerFactory) {
45         super(view, clickHandlerFactory);
46         mHeaderView = view.findViewById(R.id.notification_header);
47         mBodyView = view.findViewById(R.id.notification_body);
48         mActionsView = view.findViewById(R.id.notification_actions);
49         mProgressBarView = view.findViewById(R.id.progress_bar);
50         mCardBackgroundColor = NotificationUtils.getAttrColor(view.getContext(),
51                 android.R.attr.colorPrimary);
52         mClickHandlerFactory = clickHandlerFactory;
53     }
54 
55     /**
56      * Binds a {@link AlertEntry} to a car progress notification template.
57      */
58     @Override
bind(AlertEntry alertEntry, boolean isInGroup, boolean isHeadsUp)59     public void bind(AlertEntry alertEntry, boolean isInGroup,
60             boolean isHeadsUp) {
61         super.bind(alertEntry, isInGroup, isHeadsUp);
62         bindBody(alertEntry);
63         mHeaderView.bind(alertEntry, isInGroup);
64         mActionsView.bind(mClickHandlerFactory, alertEntry);
65     }
66 
67     /**
68      * Private method that binds the data to the view.
69      */
bindBody(AlertEntry alertEntry)70     private void bindBody(AlertEntry alertEntry) {
71         Notification notification = alertEntry.getNotification();
72 
73         Bundle extraData = notification.extras;
74         CharSequence title = extraData.getCharSequence(Notification.EXTRA_TITLE);
75         CharSequence text = extraData.getCharSequence(Notification.EXTRA_TEXT);
76 
77         mBodyView.bind(title, text, loadAppLauncherIcon(alertEntry.getStatusBarNotification()),
78                 notification.getLargeIcon(), /* titleIcon= */ null, /* countText= */ null,
79                 notification.showsTime() ? notification.when : null);
80 
81         mProgressBarView.setVisibility(View.VISIBLE);
82         boolean isIndeterminate = extraData.getBoolean(Notification.EXTRA_PROGRESS_INDETERMINATE);
83         int progress = extraData.getInt(Notification.EXTRA_PROGRESS);
84         int progressMax = extraData.getInt(Notification.EXTRA_PROGRESS_MAX);
85         mProgressBarView.setIndeterminate(isIndeterminate);
86         mProgressBarView.setMax(progressMax);
87         mProgressBarView.setProgress(progress);
88 
89         // optional color
90         if (notification.color != Notification.COLOR_DEFAULT) {
91             int calculatedColor = NotificationUtils.resolveContrastColor(
92                     notification.color, mCardBackgroundColor);
93             ColorStateList colorStateList = ColorStateList.valueOf(calculatedColor);
94             mProgressBarView.setProgressTintList(colorStateList);
95         }
96     }
97 
98     /**
99      * Resets the basic notification view empty for recycling.
100      */
101     @Override
reset()102     void reset() {
103         super.reset();
104         mProgressBarView.setProgress(0);
105         mProgressBarView.setVisibility(View.GONE);
106         mProgressBarView.setProgressTintList(null);
107     }
108 }
109