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 java.util.ArrayList; 23 import java.util.Collection; 24 import java.util.List; 25 26 /** 27 * MediaManager provide interface to get MediaDevice list. 28 */ 29 public abstract class MediaManager { 30 31 private static final String TAG = "MediaManager"; 32 33 protected final Collection<MediaDeviceCallback> mCallbacks = new ArrayList<>(); 34 protected final List<MediaDevice> mMediaDevices = new ArrayList<>(); 35 36 protected Context mContext; 37 protected Notification mNotification; 38 MediaManager(Context context, Notification notification)39 MediaManager(Context context, Notification notification) { 40 mContext = context; 41 mNotification = notification; 42 } 43 registerCallback(MediaDeviceCallback callback)44 protected void registerCallback(MediaDeviceCallback callback) { 45 synchronized (mCallbacks) { 46 if (!mCallbacks.contains(callback)) { 47 mCallbacks.add(callback); 48 } 49 } 50 } 51 unregisterCallback(MediaDeviceCallback callback)52 protected void unregisterCallback(MediaDeviceCallback callback) { 53 synchronized (mCallbacks) { 54 if (mCallbacks.contains(callback)) { 55 mCallbacks.remove(callback); 56 } 57 } 58 } 59 60 /** 61 * Start scan connected MediaDevice 62 */ startScan()63 public abstract void startScan(); 64 65 /** 66 * Stop scan MediaDevice 67 */ stopScan()68 public abstract void stopScan(); 69 findMediaDevice(String id)70 protected MediaDevice findMediaDevice(String id) { 71 for (MediaDevice mediaDevice : mMediaDevices) { 72 if (mediaDevice.getId().equals(id)) { 73 return mediaDevice; 74 } 75 } 76 Log.e(TAG, "findMediaDevice() can't found device"); 77 return null; 78 } 79 dispatchDeviceAdded(MediaDevice mediaDevice)80 protected void dispatchDeviceAdded(MediaDevice mediaDevice) { 81 synchronized (mCallbacks) { 82 for (MediaDeviceCallback callback : mCallbacks) { 83 callback.onDeviceAdded(mediaDevice); 84 } 85 } 86 } 87 dispatchDeviceRemoved(MediaDevice mediaDevice)88 protected void dispatchDeviceRemoved(MediaDevice mediaDevice) { 89 synchronized (mCallbacks) { 90 for (MediaDeviceCallback callback : mCallbacks) { 91 callback.onDeviceRemoved(mediaDevice); 92 } 93 } 94 } 95 dispatchDeviceListAdded()96 protected void dispatchDeviceListAdded() { 97 synchronized (mCallbacks) { 98 for (MediaDeviceCallback callback : mCallbacks) { 99 callback.onDeviceListAdded(new ArrayList<>(mMediaDevices)); 100 } 101 } 102 } 103 dispatchDeviceListRemoved(List<MediaDevice> devices)104 protected void dispatchDeviceListRemoved(List<MediaDevice> devices) { 105 synchronized (mCallbacks) { 106 for (MediaDeviceCallback callback : mCallbacks) { 107 callback.onDeviceListRemoved(devices); 108 } 109 } 110 } 111 dispatchConnectedDeviceChanged(String id)112 protected void dispatchConnectedDeviceChanged(String id) { 113 synchronized (mCallbacks) { 114 for (MediaDeviceCallback callback : mCallbacks) { 115 callback.onConnectedDeviceChanged(id); 116 } 117 } 118 } 119 dispatchDataChanged()120 protected void dispatchDataChanged() { 121 synchronized (mCallbacks) { 122 for (MediaDeviceCallback callback : mCallbacks) { 123 callback.onDeviceAttributesChanged(); 124 } 125 } 126 } 127 128 /** 129 * Callback for notifying device is added, removed and attributes changed. 130 */ 131 public interface MediaDeviceCallback { 132 /** 133 * Callback for notifying MediaDevice is added. 134 * 135 * @param device the MediaDevice 136 */ onDeviceAdded(MediaDevice device)137 void onDeviceAdded(MediaDevice device); 138 139 /** 140 * Callback for notifying MediaDevice list is added. 141 * 142 * @param devices the MediaDevice list 143 */ onDeviceListAdded(List<MediaDevice> devices)144 void onDeviceListAdded(List<MediaDevice> devices); 145 146 /** 147 * Callback for notifying MediaDevice is removed. 148 * 149 * @param device the MediaDevice 150 */ onDeviceRemoved(MediaDevice device)151 void onDeviceRemoved(MediaDevice device); 152 153 /** 154 * Callback for notifying MediaDevice list is removed. 155 * 156 * @param devices the MediaDevice list 157 */ onDeviceListRemoved(List<MediaDevice> devices)158 void onDeviceListRemoved(List<MediaDevice> devices); 159 160 /** 161 * Callback for notifying connected MediaDevice is changed. 162 * 163 * @param id the id of MediaDevice 164 */ onConnectedDeviceChanged(String id)165 void onConnectedDeviceChanged(String id); 166 167 /** 168 * Callback for notifying that MediaDevice attributes 169 * (e.g: device name, connection state, subtitle) is changed. 170 */ onDeviceAttributesChanged()171 void onDeviceAttributesChanged(); 172 } 173 } 174