1 /*
2  * Copyright 2018 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.settingslib.media;
17 
18 import android.app.Notification;
19 import android.content.Context;
20 import android.util.Log;
21 
22 import androidx.mediarouter.media.MediaRouteSelector;
23 import androidx.mediarouter.media.MediaRouter;
24 
25 import com.android.internal.annotations.VisibleForTesting;
26 
27 /**
28  * InfoMediaManager provide interface to get InfoMediaDevice list.
29  */
30 public class InfoMediaManager extends MediaManager {
31 
32     private static final String TAG = "InfoMediaManager";
33 
34     @VisibleForTesting
35     final MediaRouterCallback mMediaRouterCallback = new MediaRouterCallback();
36     @VisibleForTesting
37     MediaRouteSelector mSelector;
38     @VisibleForTesting
39     MediaRouter mMediaRouter;
40 
41     private String mPackageName;
42 
InfoMediaManager(Context context, String packageName, Notification notification)43     InfoMediaManager(Context context, String packageName, Notification notification) {
44         super(context, notification);
45 
46         mMediaRouter = MediaRouter.getInstance(context);
47         mPackageName = packageName;
48         mSelector = new MediaRouteSelector.Builder()
49                 .addControlCategory(getControlCategoryByPackageName(mPackageName))
50                 .build();
51     }
52 
53     @Override
startScan()54     public void startScan() {
55         mMediaDevices.clear();
56         mMediaRouter.addCallback(mSelector, mMediaRouterCallback,
57                 MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
58     }
59 
60     @VisibleForTesting
getControlCategoryByPackageName(String packageName)61     String getControlCategoryByPackageName(String packageName) {
62         //TODO(b/117129183): Use package name to get ControlCategory.
63         //Since api not ready, return fixed ControlCategory for prototype.
64         return "com.google.android.gms.cast.CATEGORY_CAST/4F8B3483";
65     }
66 
67     @Override
stopScan()68     public void stopScan() {
69         mMediaRouter.removeCallback(mMediaRouterCallback);
70     }
71 
72     class MediaRouterCallback extends MediaRouter.Callback {
73         @Override
onRouteAdded(MediaRouter router, MediaRouter.RouteInfo route)74         public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo route) {
75             MediaDevice mediaDevice = findMediaDevice(MediaDeviceUtils.getId(route));
76             if (mediaDevice == null) {
77                 mediaDevice = new InfoMediaDevice(mContext, route);
78                 Log.d(TAG, "onRouteAdded() route : " + route.getName());
79                 mMediaDevices.add(mediaDevice);
80                 dispatchDeviceAdded(mediaDevice);
81             }
82         }
83 
84         @Override
onRouteRemoved(MediaRouter router, MediaRouter.RouteInfo route)85         public void onRouteRemoved(MediaRouter router, MediaRouter.RouteInfo route) {
86             final MediaDevice mediaDevice = findMediaDevice(MediaDeviceUtils.getId(route));
87             if (mediaDevice != null) {
88                 Log.d(TAG, "onRouteRemoved() route : " + route.getName());
89                 mMediaDevices.remove(mediaDevice);
90                 dispatchDeviceRemoved(mediaDevice);
91             }
92         }
93     }
94 }
95