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.development.bluetooth;
18 
19 import android.bluetooth.BluetoothA2dp;
20 import android.bluetooth.BluetoothAdapter;
21 import android.bluetooth.BluetoothDevice;
22 import android.bluetooth.BluetoothManager;
23 import android.bluetooth.BluetoothProfile;
24 import android.content.Context;
25 
26 import androidx.annotation.Nullable;
27 import androidx.annotation.VisibleForTesting;
28 
29 import com.android.settings.core.PreferenceControllerMixin;
30 import com.android.settings.development.BluetoothA2dpConfigStore;
31 import com.android.settings.development.BluetoothServiceConnectionListener;
32 import com.android.settingslib.core.lifecycle.Lifecycle;
33 import com.android.settingslib.core.lifecycle.LifecycleObserver;
34 import com.android.settingslib.core.lifecycle.events.OnDestroy;
35 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
36 
37 import java.util.List;
38 
39 /**
40  * Abstract class for Bluetooth A2DP config controller in developer option.
41  */
42 public abstract class AbstractBluetoothPreferenceController extends
43         DeveloperOptionsPreferenceController implements BluetoothServiceConnectionListener,
44         LifecycleObserver, OnDestroy, PreferenceControllerMixin {
45 
46     @Nullable protected volatile BluetoothA2dp mBluetoothA2dp;
47 
48     @VisibleForTesting
49     BluetoothAdapter mBluetoothAdapter;
50 
AbstractBluetoothPreferenceController( @ullable Context context, @Nullable Lifecycle lifecycle, @Nullable BluetoothA2dpConfigStore store)51     public AbstractBluetoothPreferenceController(
52             @Nullable Context context,
53             @Nullable Lifecycle lifecycle,
54             @Nullable BluetoothA2dpConfigStore store) {
55         super(context);
56         if (lifecycle != null) {
57             lifecycle.addObserver(this);
58         }
59         mBluetoothAdapter = context.getSystemService(BluetoothManager.class).getAdapter();
60     }
61 
62     @Override
onBluetoothServiceConnected(BluetoothA2dp bluetoothA2dp)63     public void onBluetoothServiceConnected(BluetoothA2dp bluetoothA2dp) {
64         mBluetoothA2dp = bluetoothA2dp;
65         updateState(mPreference);
66     }
67 
68     @Override
onBluetoothCodecUpdated()69     public void onBluetoothCodecUpdated() {
70         updateState(mPreference);
71     }
72 
73     @Override
onBluetoothServiceDisconnected()74     public void onBluetoothServiceDisconnected() {
75         mBluetoothA2dp = null;
76         updateState(mPreference);
77     }
78 
79     @Override
onDestroy()80     public void onDestroy() {
81         mBluetoothA2dp = null;
82     }
83 
84     /**
85      * Callback interface for this class to manipulate data from controller.
86      */
87     public interface Callback {
88         /**
89          * Callback method to notify preferences when the Bluetooth A2DP config is changed.
90          */
onBluetoothCodecChanged()91         void onBluetoothCodecChanged();
92 
93         /**
94          * Callback method to notify preferences when the HD audio(optional codec) state is changed.
95          *
96          * @param enabled Is {@code true} when the setting is enabled.
97          */
onBluetoothHDAudioEnabled(boolean enabled)98         void onBluetoothHDAudioEnabled(boolean enabled);
99     }
100 
getA2dpActiveDevice()101     protected BluetoothDevice getA2dpActiveDevice() {
102         if (mBluetoothAdapter == null) {
103             return null;
104         }
105         List<BluetoothDevice> activeDevices =
106                 mBluetoothAdapter.getActiveDevices(BluetoothProfile.A2DP);
107         return (activeDevices.size() > 0) ? activeDevices.get(0) : null;
108     }
109 }
110