• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 package com.android.car.carlauncher;
17 
18 import android.app.Notification;
19 import android.app.NotificationManager;
20 import android.content.Context;
21 import android.os.RemoteException;
22 import android.service.notification.StatusBarNotification;
23 import android.util.Log;
24 
25 import com.android.car.media.common.source.MediaModels;
26 import com.android.car.media.common.source.MediaSessionHelper;
27 
28 /** Utility class that handles common MediaSession related logic*/
29 public class MediaSessionUtils {
30     private static final String TAG = "MediaSessionUtils";
31 
MediaSessionUtils()32     private MediaSessionUtils() {}
33 
34     /** Create a MediaModels object */
getMediaModels(Context context)35     public static MediaModels getMediaModels(Context context) {
36         return new MediaModels(context.getApplicationContext(),
37                 createNotificationProvider(context));
38     }
39 
40     /** Create a MediaSessionHelper object */
getMediaSessionHelper(Context context)41     public static MediaSessionHelper getMediaSessionHelper(Context context) {
42         return new MediaSessionHelper(context.getApplicationContext(),
43                 createNotificationProvider(context));
44     }
45 
createNotificationProvider( Context context)46     private static MediaSessionHelper.NotificationProvider createNotificationProvider(
47             Context context) {
48         return new MediaSessionHelper.NotificationProvider() {
49             @Override
50             public StatusBarNotification[] getActiveNotifications() {
51                 try {
52                     return NotificationManager.getService()
53                             .getActiveNotificationsWithAttribution(
54                                     context.getPackageName(), null);
55                 } catch (RemoteException e) {
56                     Log.e(TAG, "Exception trying to get active notifications " + e);
57                     return new StatusBarNotification[0];
58                 }
59             }
60 
61             @Override
62             public boolean isMediaNotification(Notification notification) {
63                 return notification.isMediaNotification();
64             }
65         };
66     }
67 }
68