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