• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.settings.notification.history;
18 
19 import android.app.PendingIntent;
20 import android.content.ActivityNotFoundException;
21 import android.content.Intent;
22 import android.graphics.drawable.Drawable;
23 import android.os.UserHandle;
24 import android.text.TextUtils;
25 import android.util.Slog;
26 import android.view.View;
27 import android.widget.DateTimeView;
28 import android.widget.ImageView;
29 import android.widget.TextView;
30 
31 import androidx.core.view.AccessibilityDelegateCompat;
32 import androidx.core.view.ViewCompat;
33 import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
34 import androidx.recyclerview.widget.RecyclerView;
35 
36 import com.android.internal.logging.InstanceId;
37 import com.android.internal.logging.UiEventLogger;
38 import com.android.settings.R;
39 
40 public class NotificationSbnViewHolder extends RecyclerView.ViewHolder {
41     private static final String TAG = "SbnViewHolder";
42 
43     private final TextView mPkgName;
44     private final ImageView mIcon;
45     private final DateTimeView mTime;
46     private final TextView mTitle;
47     private final TextView mSummary;
48     private final ImageView mProfileBadge;
49     private final View mDivider;
50 
NotificationSbnViewHolder(View itemView)51     NotificationSbnViewHolder(View itemView) {
52         super(itemView);
53         mPkgName = itemView.findViewById(R.id.pkgname);
54         mIcon = itemView.findViewById(R.id.icon);
55         mTime = itemView.findViewById(R.id.timestamp);
56         mTitle = itemView.findViewById(R.id.title);
57         mSummary = itemView.findViewById(R.id.text);
58         mProfileBadge = itemView.findViewById(R.id.profile_badge);
59         mDivider = itemView.findViewById(R.id.divider);
60     }
61 
setSummary(CharSequence summary)62     void setSummary(CharSequence summary) {
63         mSummary.setVisibility(TextUtils.isEmpty(summary) ? View.GONE : View.VISIBLE);
64         mSummary.setText(summary);
65     }
66 
setTitle(CharSequence title)67     void setTitle(CharSequence title) {
68         mTitle.setText(title);
69     }
70 
setIcon(Drawable icon)71     void setIcon(Drawable icon) {
72         mIcon.setImageDrawable(icon);
73     }
74 
setIconBackground(Drawable background)75     void setIconBackground(Drawable background) {
76         mIcon.setBackground(background);
77     }
78 
setPackageLabel(String pkg)79     void setPackageLabel(String pkg) {
80         mPkgName.setText(pkg);
81     }
82 
setPostedTime(long postedTime)83     void setPostedTime(long postedTime) {
84         mTime.setTime(postedTime);
85     }
86 
setProfileBadge(Drawable badge)87     void setProfileBadge(Drawable badge) {
88         mProfileBadge.setImageDrawable(badge);
89         mProfileBadge.setVisibility(badge != null ? View.VISIBLE : View.GONE);
90     }
91 
setDividerVisible(boolean visible)92     void setDividerVisible(boolean visible) {
93         mDivider.setVisibility(visible ? View.VISIBLE : View.GONE);
94     }
95 
addOnClick(int position, String pkg, int uid, int userId, PendingIntent pi, InstanceId instanceId, boolean isSnoozed, UiEventLogger uiEventLogger)96     void addOnClick(int position, String pkg, int uid, int userId, PendingIntent pi,
97             InstanceId instanceId,
98             boolean isSnoozed, UiEventLogger uiEventLogger) {
99         Intent appIntent = itemView.getContext().getPackageManager()
100                 .getLaunchIntentForPackage(pkg);
101         boolean isPendingIntentValid = pi != null && pi.isActivity();
102         if (isPendingIntentValid || appIntent != null) {
103             itemView.setOnClickListener(v -> {
104                 uiEventLogger.logWithInstanceIdAndPosition(
105                         isSnoozed
106                                 ? NotificationHistoryActivity.NotificationHistoryEvent
107                                 .NOTIFICATION_HISTORY_SNOOZED_ITEM_CLICK
108                                 : NotificationHistoryActivity.NotificationHistoryEvent
109                                 .NOTIFICATION_HISTORY_RECENT_ITEM_CLICK,
110                         uid, pkg, instanceId, position);
111                 if (pi != null && isPendingIntentValid) {
112                     try {
113                         pi.send();
114                     } catch (PendingIntent.CanceledException e) {
115                         Slog.e(TAG, "Could not launch", e);
116                     }
117                 } else if (appIntent != null) {
118                     appIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
119                     try {
120                         itemView.getContext().startActivityAsUser(appIntent, UserHandle.of(userId));
121                     } catch (ActivityNotFoundException e) {
122                         Slog.e(TAG, "no launch activity", e);
123                     }
124                 }
125             });
126             ViewCompat.setAccessibilityDelegate(itemView, new AccessibilityDelegateCompat() {
127                 @Override
128                 public void onInitializeAccessibilityNodeInfo(View host,
129                         AccessibilityNodeInfoCompat info) {
130                     super.onInitializeAccessibilityNodeInfo(host, info);
131                     CharSequence description = host.getResources().getText(
132                             R.string.notification_history_open_notification);
133                     AccessibilityNodeInfoCompat.AccessibilityActionCompat customClick =
134                             new AccessibilityNodeInfoCompat.AccessibilityActionCompat(
135                                     AccessibilityNodeInfoCompat.ACTION_CLICK, description);
136                     info.addAction(customClick);
137                 }
138             });
139         }
140     }
141 }
142