• 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 static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED;
20 
21 import android.app.ActivityOptions;
22 import android.app.PendingIntent;
23 import android.content.ActivityNotFoundException;
24 import android.content.Intent;
25 import android.graphics.drawable.Drawable;
26 import android.os.UserHandle;
27 import android.text.TextUtils;
28 import android.util.Slog;
29 import android.view.View;
30 import android.widget.DateTimeView;
31 import android.widget.ImageView;
32 import android.widget.TextView;
33 
34 import androidx.core.view.AccessibilityDelegateCompat;
35 import androidx.core.view.ViewCompat;
36 import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
37 import androidx.recyclerview.widget.RecyclerView;
38 
39 import com.android.internal.logging.InstanceId;
40 import com.android.internal.logging.UiEventLogger;
41 import com.android.settings.R;
42 
43 public class NotificationSbnViewHolder extends RecyclerView.ViewHolder {
44     private static final String TAG = "SbnViewHolder";
45 
46     private final TextView mPkgName;
47     private final ImageView mIcon;
48     private final DateTimeView mTime;
49     private final TextView mTitle;
50     private final TextView mSummary;
51     private final ImageView mProfileBadge;
52 
NotificationSbnViewHolder(View itemView)53     NotificationSbnViewHolder(View itemView) {
54         super(itemView);
55         mPkgName = itemView.findViewById(R.id.pkgname);
56         mIcon = itemView.findViewById(R.id.icon);
57         mTime = itemView.findViewById(R.id.timestamp);
58         mTitle = itemView.findViewById(R.id.title);
59         mSummary = itemView.findViewById(R.id.text);
60         mProfileBadge = itemView.findViewById(R.id.profile_badge);
61     }
62 
setSummary(CharSequence summary)63     void setSummary(CharSequence summary) {
64         mSummary.setVisibility(TextUtils.isEmpty(summary) ? View.GONE : View.VISIBLE);
65         mSummary.setText(summary);
66     }
67 
setTitle(CharSequence title)68     void setTitle(CharSequence title) {
69         mTitle.setText(title);
70     }
71 
setIcon(Drawable icon)72     void setIcon(Drawable icon) {
73         mIcon.setImageDrawable(icon);
74     }
75 
setIconBackground(Drawable background)76     void setIconBackground(Drawable background) {
77         mIcon.setBackground(background);
78     }
79 
setPackageLabel(String pkg)80     void setPackageLabel(String pkg) {
81         mPkgName.setText(pkg);
82     }
83 
setPostedTime(long postedTime)84     void setPostedTime(long postedTime) {
85         mTime.setTime(postedTime);
86     }
87 
setProfileBadge(Drawable badge)88     void setProfileBadge(Drawable badge) {
89         mProfileBadge.setImageDrawable(badge);
90         mProfileBadge.setVisibility(badge != null ? View.VISIBLE : View.GONE);
91     }
92 
addOnClick(int position, String pkg, int uid, int userId, PendingIntent pi, InstanceId instanceId, boolean isSnoozed, UiEventLogger uiEventLogger)93     void addOnClick(int position, String pkg, int uid, int userId, PendingIntent pi,
94             InstanceId instanceId,
95             boolean isSnoozed, UiEventLogger uiEventLogger) {
96         Intent appIntent = itemView.getContext().getPackageManager()
97                 .getLaunchIntentForPackage(pkg);
98         boolean isPendingIntentValid = pi != null && pi.isActivity();
99         if (isPendingIntentValid || appIntent != null) {
100             itemView.setOnClickListener(v -> {
101                 uiEventLogger.logWithInstanceIdAndPosition(
102                         isSnoozed
103                                 ? NotificationHistoryActivity.NotificationHistoryEvent
104                                 .NOTIFICATION_HISTORY_SNOOZED_ITEM_CLICK
105                                 : NotificationHistoryActivity.NotificationHistoryEvent
106                                 .NOTIFICATION_HISTORY_RECENT_ITEM_CLICK,
107                         uid, pkg, instanceId, position);
108                 if (pi != null && isPendingIntentValid) {
109                     try {
110                         ActivityOptions options = ActivityOptions.makeBasic();
111                         options.setPendingIntentBackgroundActivityStartMode(
112                                 MODE_BACKGROUND_ACTIVITY_START_ALLOWED);
113                         pi.send(options.toBundle());
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