1 /* 2 * Copyright (C) 2019 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.settings.media; 18 19 import android.bluetooth.BluetoothProfile; 20 import android.content.Context; 21 import android.net.Uri; 22 import android.util.Log; 23 24 import com.android.settings.bluetooth.Utils; 25 import com.android.settings.slices.SliceBackgroundWorker; 26 import com.android.settingslib.bluetooth.BluetoothCallback; 27 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 28 import com.android.settingslib.bluetooth.LocalBluetoothManager; 29 30 import java.io.IOException; 31 32 /** 33 * Listener for background change from {@code BluetoothCallback} to update media output indicator. 34 */ 35 public class MediaOutputIndicatorWorker extends SliceBackgroundWorker implements BluetoothCallback { 36 37 private static final String TAG = "MediaOutputIndicatorWorker"; 38 39 private LocalBluetoothManager mLocalBluetoothManager; 40 MediaOutputIndicatorWorker(Context context, Uri uri)41 public MediaOutputIndicatorWorker(Context context, Uri uri) { 42 super(context, uri); 43 } 44 45 @Override onSlicePinned()46 protected void onSlicePinned() { 47 mLocalBluetoothManager = Utils.getLocalBtManager(getContext()); 48 if (mLocalBluetoothManager == null) { 49 Log.e(TAG, "Bluetooth is not supported on this device"); 50 return; 51 } 52 mLocalBluetoothManager.getEventManager().registerCallback(this); 53 } 54 55 @Override onSliceUnpinned()56 protected void onSliceUnpinned() { 57 if (mLocalBluetoothManager == null) { 58 Log.e(TAG, "Bluetooth is not supported on this device"); 59 return; 60 } 61 mLocalBluetoothManager.getEventManager().unregisterCallback(this); 62 } 63 64 @Override close()65 public void close() throws IOException { 66 mLocalBluetoothManager = null; 67 } 68 69 @Override onBluetoothStateChanged(int bluetoothState)70 public void onBluetoothStateChanged(int bluetoothState) { 71 // To handle the case that Bluetooth on and no connected devices 72 notifySliceChange(); 73 } 74 75 @Override onActiveDeviceChanged(CachedBluetoothDevice activeDevice, int bluetoothProfile)76 public void onActiveDeviceChanged(CachedBluetoothDevice activeDevice, int bluetoothProfile) { 77 if (bluetoothProfile == BluetoothProfile.A2DP) { 78 notifySliceChange(); 79 } 80 } 81 82 @Override onAudioModeChanged()83 public void onAudioModeChanged() { 84 notifySliceChange(); 85 } 86 } 87