Home | History | Annotate | Download | only in bluetooth
      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.settings.bluetooth;
     17 
     18 import android.bluetooth.BluetoothProfile;
     19 import android.content.Context;
     20 import android.media.AudioManager;
     21 import android.support.annotation.VisibleForTesting;
     22 import android.util.Log;
     23 
     24 import com.android.settings.connecteddevice.DevicePreferenceCallback;
     25 import com.android.settings.dashboard.DashboardFragment;
     26 import com.android.settingslib.bluetooth.LocalBluetoothManager;
     27 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
     28 import android.support.v7.preference.Preference;
     29 
     30 /**
     31  * Controller to maintain available media Bluetooth devices
     32  */
     33 public class AvailableMediaBluetoothDeviceUpdater extends BluetoothDeviceUpdater
     34         implements Preference.OnPreferenceClickListener {
     35 
     36     private static final String TAG = "AvailableMediaBluetoothDeviceUpdater";
     37     private static final boolean DBG = false;
     38 
     39     private final AudioManager mAudioManager;
     40 
     41     public AvailableMediaBluetoothDeviceUpdater(Context context, DashboardFragment fragment,
     42             DevicePreferenceCallback devicePreferenceCallback) {
     43         super(context, fragment, devicePreferenceCallback);
     44         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
     45     }
     46 
     47     @VisibleForTesting
     48     AvailableMediaBluetoothDeviceUpdater(DashboardFragment fragment,
     49             DevicePreferenceCallback devicePreferenceCallback,
     50             LocalBluetoothManager localBluetoothManager) {
     51         super(fragment, devicePreferenceCallback, localBluetoothManager);
     52         mAudioManager = (AudioManager) fragment.getContext().
     53                 getSystemService(Context.AUDIO_SERVICE);
     54     }
     55 
     56     @Override
     57     public void onAudioModeChanged() {
     58         forceUpdate();
     59     }
     60 
     61     @Override
     62     public void onProfileConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state,
     63             int bluetoothProfile) {
     64         if (DBG) {
     65             Log.d(TAG, "onProfileConnectionStateChanged() device: " +
     66                     cachedDevice.getName() + ", state: " + state + ", bluetoothProfile: "
     67                     + bluetoothProfile);
     68         }
     69         if (state == BluetoothProfile.STATE_CONNECTED) {
     70             if (isFilterMatched(cachedDevice)) {
     71                 addPreference(cachedDevice);
     72             } else {
     73                 removePreference(cachedDevice);
     74             }
     75         } else if (state == BluetoothProfile.STATE_DISCONNECTED) {
     76             removePreference(cachedDevice);
     77         }
     78     }
     79 
     80     @Override
     81     public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
     82         final int audioMode = mAudioManager.getMode();
     83         final int currentAudioProfile;
     84 
     85         if (audioMode == AudioManager.MODE_RINGTONE
     86                 || audioMode == AudioManager.MODE_IN_CALL
     87                 || audioMode == AudioManager.MODE_IN_COMMUNICATION) {
     88             // in phone call
     89             currentAudioProfile = BluetoothProfile.HEADSET;
     90         } else {
     91             // without phone call
     92             currentAudioProfile = BluetoothProfile.A2DP;
     93         }
     94 
     95         boolean isFilterMatched = false;
     96         if (isDeviceConnected(cachedDevice)) {
     97             if (DBG) {
     98                 Log.d(TAG, "isFilterMatched() current audio profile : " + currentAudioProfile);
     99             }
    100             // According to the current audio profile type,
    101             // this page will show the bluetooth device that have corresponding profile.
    102             // For example:
    103             // If current audio profile is a2dp, show the bluetooth device that have a2dp profile.
    104             // If current audio profile is headset,
    105             // show the bluetooth device that have headset profile.
    106             switch (currentAudioProfile) {
    107                 case BluetoothProfile.A2DP:
    108                     isFilterMatched = cachedDevice.isA2dpDevice();
    109                     break;
    110                 case BluetoothProfile.HEADSET:
    111                     isFilterMatched = cachedDevice.isHfpDevice();
    112                     break;
    113             }
    114             if (DBG) {
    115                 Log.d(TAG, "isFilterMatched() device : " +
    116                         cachedDevice.getName() + ", isFilterMatched : " + isFilterMatched);
    117             }
    118         }
    119         return isFilterMatched;
    120     }
    121 
    122     @Override
    123     public boolean onPreferenceClick(Preference preference) {
    124         final CachedBluetoothDevice device = ((BluetoothDevicePreference) preference)
    125                 .getBluetoothDevice();
    126         return device.setActive();
    127     }
    128 }
    129 
    130