1 /* 2 * Copyright (C) 2023 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.systemui.tv.notifications; 18 19 import android.app.ActivityOptions; 20 import android.app.BroadcastOptions; 21 import android.app.Notification; 22 import android.app.PendingIntent; 23 import android.service.notification.StatusBarNotification; 24 import android.util.Log; 25 import android.util.SparseArray; 26 import android.view.LayoutInflater; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.widget.TextView; 30 31 import androidx.annotation.NonNull; 32 import androidx.recyclerview.widget.RecyclerView; 33 34 import com.android.systemui.tv.res.R; 35 36 /** 37 * Adapter for the VerticalGridView of the TvNotificationsPanelView. 38 */ 39 public class TvNotificationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 40 private static final String TAG = "TvNotificationAdapter"; 41 private SparseArray<StatusBarNotification> mNotifications; 42 TvNotificationAdapter()43 public TvNotificationAdapter() { 44 setHasStableIds(true); 45 } 46 47 @NonNull 48 @Override onCreateViewHolder(ViewGroup parent, int viewType)49 public TvNotificationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 50 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.notification_item, 51 parent, false); 52 return new TvNotificationViewHolder(view); 53 } 54 55 @Override onBindViewHolder(@onNull RecyclerView.ViewHolder viewHolder, int position)56 public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) { 57 if (mNotifications == null) { 58 Log.e(TAG, "Could not bind view holder because the notification is missing"); 59 return; 60 } 61 62 TvNotificationViewHolder holder = (TvNotificationViewHolder) viewHolder; 63 Notification notification = mNotifications.valueAt(position).getNotification(); 64 holder.mTitle.setText(notification.extras.getString(Notification.EXTRA_TITLE)); 65 holder.mDetails.setText(notification.extras.getString(Notification.EXTRA_TEXT)); 66 holder.mPendingIntent = notification.contentIntent; 67 } 68 69 @Override getItemCount()70 public int getItemCount() { 71 return mNotifications == null ? 0 : mNotifications.size(); 72 } 73 74 @Override getItemId(int position)75 public long getItemId(int position) { 76 // the item id is the notification id 77 return mNotifications.keyAt(position); 78 } 79 80 /** 81 * Updates the notifications and calls notifyDataSetChanged(). 82 */ setNotifications(SparseArray<StatusBarNotification> notifications)83 public void setNotifications(SparseArray<StatusBarNotification> notifications) { 84 this.mNotifications = notifications; 85 notifyDataSetChanged(); 86 } 87 88 private static class TvNotificationViewHolder extends RecyclerView.ViewHolder implements 89 View.OnClickListener { 90 final TextView mTitle; 91 final TextView mDetails; 92 PendingIntent mPendingIntent; 93 TvNotificationViewHolder(View itemView)94 protected TvNotificationViewHolder(View itemView) { 95 super(itemView); 96 mTitle = itemView.findViewById(R.id.notification_title); 97 mDetails = itemView.findViewById(R.id.notification_details); 98 itemView.setOnClickListener(this); 99 } 100 101 @Override onClick(View v)102 public void onClick(View v) { 103 try { 104 if (mPendingIntent != null) { 105 BroadcastOptions options = BroadcastOptions.makeBasic(); 106 options.setInteractive(true); 107 options.setPendingIntentBackgroundActivityStartMode( 108 ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED); 109 mPendingIntent.send(options.toBundle()); 110 } 111 } catch (PendingIntent.CanceledException e) { 112 Log.d(TAG, "Pending intent canceled for : " + mPendingIntent); 113 } 114 } 115 } 116 117 } 118