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.content.Context;
21 
22 import com.android.settings.core.PreferenceControllerMixin;
23 import com.android.settings.development.BluetoothA2dpConfigStore;
24 import com.android.settings.development.BluetoothServiceConnectionListener;
25 import com.android.settingslib.core.lifecycle.Lifecycle;
26 import com.android.settingslib.core.lifecycle.LifecycleObserver;
27 import com.android.settingslib.core.lifecycle.events.OnDestroy;
28 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
29 
30 /**
31  * Abstract class for Bluetooth A2DP config controller in developer option.
32  */
33 public abstract class AbstractBluetoothPreferenceController extends
34         DeveloperOptionsPreferenceController implements BluetoothServiceConnectionListener,
35         LifecycleObserver, OnDestroy, PreferenceControllerMixin {
36 
37     protected volatile BluetoothA2dp mBluetoothA2dp;
38 
AbstractBluetoothPreferenceController(Context context, Lifecycle lifecycle, BluetoothA2dpConfigStore store)39     public AbstractBluetoothPreferenceController(Context context, Lifecycle lifecycle,
40                                                  BluetoothA2dpConfigStore store) {
41         super(context);
42         if (lifecycle != null) {
43             lifecycle.addObserver(this);
44         }
45     }
46 
47     @Override
onBluetoothServiceConnected(BluetoothA2dp bluetoothA2dp)48     public void onBluetoothServiceConnected(BluetoothA2dp bluetoothA2dp) {
49         mBluetoothA2dp = bluetoothA2dp;
50         updateState(mPreference);
51     }
52 
53     @Override
onBluetoothCodecUpdated()54     public void onBluetoothCodecUpdated() {
55         updateState(mPreference);
56     }
57 
58     @Override
onBluetoothServiceDisconnected()59     public void onBluetoothServiceDisconnected() {
60         mBluetoothA2dp = null;
61         updateState(mPreference);
62     }
63 
64     @Override
onDestroy()65     public void onDestroy() {
66         mBluetoothA2dp = null;
67     }
68 
69     /**
70      * Callback interface for this class to manipulate data from controller.
71      */
72     public interface Callback {
73         /**
74          * Callback method to notify preferences when the Bluetooth A2DP config is changed.
75          */
onBluetoothCodecChanged()76         void onBluetoothCodecChanged();
77 
78         /**
79          * Callback method to notify preferences when the HD audio(optional codec) state is changed.
80          *
81          * @param enabled Is {@code true} when the setting is enabled.
82          */
onBluetoothHDAudioEnabled(boolean enabled)83         void onBluetoothHDAudioEnabled(boolean enabled);
84     }
85 }
86