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 private final View mDivider; 53 NotificationSbnViewHolder(View itemView)54 NotificationSbnViewHolder(View itemView) { 55 super(itemView); 56 mPkgName = itemView.findViewById(R.id.pkgname); 57 mIcon = itemView.findViewById(R.id.icon); 58 mTime = itemView.findViewById(R.id.timestamp); 59 mTitle = itemView.findViewById(R.id.title); 60 mSummary = itemView.findViewById(R.id.text); 61 mProfileBadge = itemView.findViewById(R.id.profile_badge); 62 mDivider = itemView.findViewById(R.id.divider); 63 } 64 setSummary(CharSequence summary)65 void setSummary(CharSequence summary) { 66 mSummary.setVisibility(TextUtils.isEmpty(summary) ? View.GONE : View.VISIBLE); 67 mSummary.setText(summary); 68 } 69 setTitle(CharSequence title)70 void setTitle(CharSequence title) { 71 mTitle.setText(title); 72 } 73 setIcon(Drawable icon)74 void setIcon(Drawable icon) { 75 mIcon.setImageDrawable(icon); 76 } 77 setIconBackground(Drawable background)78 void setIconBackground(Drawable background) { 79 mIcon.setBackground(background); 80 } 81 setPackageLabel(String pkg)82 void setPackageLabel(String pkg) { 83 mPkgName.setText(pkg); 84 } 85 setPostedTime(long postedTime)86 void setPostedTime(long postedTime) { 87 mTime.setTime(postedTime); 88 } 89 setProfileBadge(Drawable badge)90 void setProfileBadge(Drawable badge) { 91 mProfileBadge.setImageDrawable(badge); 92 mProfileBadge.setVisibility(badge != null ? View.VISIBLE : View.GONE); 93 } 94 setDividerVisible(boolean visible)95 void setDividerVisible(boolean visible) { 96 mDivider.setVisibility(visible ? View.VISIBLE : View.GONE); 97 } 98 addOnClick(int position, String pkg, int uid, int userId, PendingIntent pi, InstanceId instanceId, boolean isSnoozed, UiEventLogger uiEventLogger)99 void addOnClick(int position, String pkg, int uid, int userId, PendingIntent pi, 100 InstanceId instanceId, 101 boolean isSnoozed, UiEventLogger uiEventLogger) { 102 Intent appIntent = itemView.getContext().getPackageManager() 103 .getLaunchIntentForPackage(pkg); 104 boolean isPendingIntentValid = pi != null && pi.isActivity(); 105 if (isPendingIntentValid || appIntent != null) { 106 itemView.setOnClickListener(v -> { 107 uiEventLogger.logWithInstanceIdAndPosition( 108 isSnoozed 109 ? NotificationHistoryActivity.NotificationHistoryEvent 110 .NOTIFICATION_HISTORY_SNOOZED_ITEM_CLICK 111 : NotificationHistoryActivity.NotificationHistoryEvent 112 .NOTIFICATION_HISTORY_RECENT_ITEM_CLICK, 113 uid, pkg, instanceId, position); 114 if (pi != null && isPendingIntentValid) { 115 try { 116 ActivityOptions options = ActivityOptions.makeBasic(); 117 options.setPendingIntentBackgroundActivityStartMode( 118 MODE_BACKGROUND_ACTIVITY_START_ALLOWED); 119 pi.send(options.toBundle()); 120 } catch (PendingIntent.CanceledException e) { 121 Slog.e(TAG, "Could not launch", e); 122 } 123 } else if (appIntent != null) { 124 appIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 125 try { 126 itemView.getContext().startActivityAsUser(appIntent, UserHandle.of(userId)); 127 } catch (ActivityNotFoundException e) { 128 Slog.e(TAG, "no launch activity", e); 129 } 130 } 131 }); 132 ViewCompat.setAccessibilityDelegate(itemView, new AccessibilityDelegateCompat() { 133 @Override 134 public void onInitializeAccessibilityNodeInfo(View host, 135 AccessibilityNodeInfoCompat info) { 136 super.onInitializeAccessibilityNodeInfo(host, info); 137 CharSequence description = host.getResources().getText( 138 R.string.notification_history_open_notification); 139 AccessibilityNodeInfoCompat.AccessibilityActionCompat customClick = 140 new AccessibilityNodeInfoCompat.AccessibilityActionCompat( 141 AccessibilityNodeInfoCompat.ACTION_CLICK, description); 142 info.addAction(customClick); 143 } 144 }); 145 } 146 } 147 } 148