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.annotation.NonNull; 20 import android.app.Activity; 21 import android.app.NotificationManager; 22 import android.content.Intent; 23 import android.graphics.drawable.ColorDrawable; 24 import android.os.Bundle; 25 import android.service.notification.StatusBarNotification; 26 import android.util.SparseArray; 27 import android.view.Gravity; 28 import android.view.View; 29 30 import androidx.leanback.widget.VerticalGridView; 31 32 import com.android.systemui.tv.res.R; 33 34 import java.util.function.Consumer; 35 36 import javax.inject.Inject; 37 38 /** 39 * This Activity shows a notification panel for tv. It is used if no other app (e.g. a launcher) can 40 * be found to show the notifications. 41 */ 42 public class TvNotificationPanelActivity extends Activity implements 43 TvNotificationHandler.Listener { 44 private final TvNotificationHandler mTvNotificationHandler; 45 private TvNotificationAdapter mTvNotificationAdapter; 46 private VerticalGridView mNotificationListView; 47 private View mNotificationPlaceholder; 48 private boolean mPanelAlreadyOpen = false; 49 private final Consumer<Boolean> mBlurConsumer = this::enableBlur; 50 51 @Inject TvNotificationPanelActivity(TvNotificationHandler tvNotificationHandler)52 public TvNotificationPanelActivity(TvNotificationHandler tvNotificationHandler) { 53 super(); 54 mTvNotificationHandler = tvNotificationHandler; 55 } 56 57 @Override onCreate(Bundle savedInstanceState)58 protected void onCreate(Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 61 if (maybeClosePanel(getIntent())) { 62 return; 63 } 64 mPanelAlreadyOpen = true; 65 66 setContentView(R.layout.notification_panel); 67 68 mNotificationPlaceholder = findViewById(R.id.no_notifications); 69 mTvNotificationAdapter = new TvNotificationAdapter(); 70 71 mNotificationListView = findViewById(R.id.notifications_list); 72 mNotificationListView.setAdapter(mTvNotificationAdapter); 73 mNotificationListView.setColumnWidth(R.dimen.notification_panel_width); 74 75 mTvNotificationHandler.setTvNotificationListener(this); 76 notificationsUpdated(mTvNotificationHandler.getCurrentNotifications()); 77 } 78 79 @Override notificationsUpdated(@onNull SparseArray<StatusBarNotification> notificationList)80 public void notificationsUpdated(@NonNull SparseArray<StatusBarNotification> notificationList) { 81 mTvNotificationAdapter.setNotifications(notificationList); 82 83 boolean noNotifications = notificationList.size() == 0; 84 mNotificationListView.setVisibility(noNotifications ? View.GONE : View.VISIBLE); 85 mNotificationPlaceholder.setVisibility(noNotifications ? View.VISIBLE : View.GONE); 86 } 87 88 @Override onNewIntent(Intent intent)89 public void onNewIntent(Intent intent) { 90 super.onNewIntent(intent); 91 maybeClosePanel(intent); 92 } 93 94 /** 95 * Handles intents from onCreate and onNewIntent. 96 * 97 * @return true if the panel is being closed, false if it is being opened 98 */ maybeClosePanel(Intent intent)99 private boolean maybeClosePanel(Intent intent) { 100 if (NotificationManager.ACTION_CLOSE_NOTIFICATION_HANDLER_PANEL.equals(intent.getAction()) 101 || (mPanelAlreadyOpen 102 && NotificationManager.ACTION_TOGGLE_NOTIFICATION_HANDLER_PANEL.equals( 103 intent.getAction()))) { 104 finish(); 105 return true; 106 } 107 return false; 108 } 109 110 @Override onAttachedToWindow()111 public void onAttachedToWindow() { 112 super.onAttachedToWindow(); 113 getWindow().setGravity(Gravity.END); 114 getWindowManager().addCrossWindowBlurEnabledListener(mBlurConsumer); 115 } 116 enableBlur(boolean enabled)117 private void enableBlur(boolean enabled) { 118 if (enabled) { 119 int blurRadius = getResources().getDimensionPixelSize( 120 R.dimen.notification_blur_radius); 121 getWindow().setBackgroundDrawable( 122 new ColorDrawable(getColor(R.color.notification_blur_background_color))); 123 getWindow().setBackgroundBlurRadius(blurRadius); 124 } else { 125 getWindow().setBackgroundDrawable( 126 new ColorDrawable(getColor(R.color.notification_default_background_color))); 127 getWindow().setBackgroundBlurRadius(0); 128 } 129 } 130 131 @Override onDetachedFromWindow()132 public void onDetachedFromWindow() { 133 super.onDetachedFromWindow(); 134 getWindowManager().removeCrossWindowBlurEnabledListener(mBlurConsumer); 135 } 136 137 @Override onDestroy()138 public void onDestroy() { 139 super.onDestroy(); 140 mTvNotificationHandler.setTvNotificationListener(null); 141 } 142 } 143