1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.components.browser_ui.media;
6 
7 import android.util.SparseArray;
8 
9 import androidx.annotation.VisibleForTesting;
10 
11 import org.chromium.base.supplier.Supplier;
12 
13 /**
14  * A class that manages the services/notifications for various media types.
15  * Each notification is associated with a different {@link MediaNotificationController}.
16  */
17 public class MediaNotificationManager {
18     private static final String TAG = "MediaNotification";
19 
20     // Maps the notification ids to their corresponding notification managers.
21     private static SparseArray<MediaNotificationController> sControllers;
22 
23     static {
24         sControllers = new SparseArray<MediaNotificationController>();
25     }
26 
MediaNotificationManager()27     private MediaNotificationManager() {}
28 
29     /**
30      * Shows the notification with media controls with the specified media info. Replaces/updates
31      * the current notification if already showing. Does nothing if |mediaNotificationInfo| hasn't
32      * changed from the last one. If |mediaNotificationInfo.isPaused| is true and the tabId
33      * mismatches |mMediaNotificationInfo.isPaused|, it is also no-op.
34      *
35      * @param notificationInfo information to show in the notification
36      * @param delegate a factory function for the delegate passed to new {@link
37      *         MediaNotificatonController} instances.
38      */
show(MediaNotificationInfo notificationInfo, Supplier<MediaNotificationController.Delegate> delegateFactory)39     public static void show(MediaNotificationInfo notificationInfo,
40             Supplier<MediaNotificationController.Delegate> delegateFactory) {
41         MediaNotificationController controller = sControllers.get(notificationInfo.id);
42         if (controller == null) {
43             controller = new MediaNotificationController(delegateFactory.get());
44             sControllers.put(notificationInfo.id, controller);
45         }
46 
47         controller.queueNotification(notificationInfo);
48     }
49 
50     /**
51      * Hides the notification for the specified tabId and notificationId
52      *
53      * @param tabId the id of the tab that showed the notification or invalid tab id.
54      * @param notificationId the id of the notification to hide for this tab.
55      */
hide(int tabId, int notificationId)56     public static void hide(int tabId, int notificationId) {
57         MediaNotificationController controller = getController(notificationId);
58         if (controller == null) return;
59 
60         controller.hideNotification(tabId);
61     }
62 
63     /**
64      * Hides notifications with the specified id for all tabs if shown.
65      *
66      * @param notificationId the id of the notification to hide for all tabs.
67      */
clear(int notificationId)68     public static void clear(int notificationId) {
69         MediaNotificationController controller = getController(notificationId);
70         if (controller == null) return;
71 
72         controller.clearNotification();
73         sControllers.remove(notificationId);
74     }
75 
76     /**
77      * Activates the Android MediaSession. This method is used to activate Android MediaSession more
78      * often because some old version of Android might send events to the latest active session
79      * based on when setActive(true) was called and regardless of the current playback state.
80      * @param tabId the id of the tab requesting to reactivate the Android MediaSession.
81      * @param notificationId the id of the notification to reactivate Android MediaSession for.
82      */
activateAndroidMediaSession(int tabId, int notificationId)83     public static void activateAndroidMediaSession(int tabId, int notificationId) {
84         MediaNotificationController controller = getController(notificationId);
85         if (controller == null) return;
86         controller.activateAndroidMediaSession(tabId);
87     }
88 
getController(int notificationId)89     public static MediaNotificationController getController(int notificationId) {
90         return sControllers.get(notificationId);
91     }
92 
93     @VisibleForTesting
setControllerForTesting( int notificationId, MediaNotificationController controller)94     public static void setControllerForTesting(
95             int notificationId, MediaNotificationController controller) {
96         sControllers.put(notificationId, controller);
97     }
98 }
99