Home | History | Annotate | Download | only in bluetooth
      1 /*
      2  * Copyright (C) 2017 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.BluetoothDevice;
     19 import android.bluetooth.BluetoothProfile;
     20 import android.content.Context;
     21 import android.support.annotation.VisibleForTesting;
     22 
     23 import com.android.settings.connecteddevice.DevicePreferenceCallback;
     24 import com.android.settings.dashboard.DashboardFragment;
     25 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
     26 import com.android.settingslib.bluetooth.LocalBluetoothManager;
     27 import android.support.v7.preference.Preference;
     28 import android.util.Log;
     29 
     30 /**
     31  * Maintain and update saved bluetooth devices(bonded but not connected)
     32  */
     33 public class SavedBluetoothDeviceUpdater extends BluetoothDeviceUpdater
     34         implements Preference.OnPreferenceClickListener {
     35     private static final String TAG = "SavedBluetoothDeviceUpdater";
     36 
     37     public SavedBluetoothDeviceUpdater(Context context, DashboardFragment fragment,
     38             DevicePreferenceCallback devicePreferenceCallback) {
     39         super(context, fragment, devicePreferenceCallback);
     40     }
     41 
     42     @VisibleForTesting
     43     SavedBluetoothDeviceUpdater(DashboardFragment fragment,
     44             DevicePreferenceCallback devicePreferenceCallback,
     45             LocalBluetoothManager localBluetoothManager) {
     46         super(fragment, devicePreferenceCallback, localBluetoothManager);
     47     }
     48 
     49     @Override
     50     public void onProfileConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state,
     51             int bluetoothProfile) {
     52         if (state == BluetoothProfile.STATE_CONNECTED) {
     53             removePreference(cachedDevice);
     54         } else if (state == BluetoothProfile.STATE_DISCONNECTED) {
     55             addPreference(cachedDevice);
     56         }
     57     }
     58 
     59     @Override
     60     public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
     61         final BluetoothDevice device = cachedDevice.getDevice();
     62         return device.getBondState() == BluetoothDevice.BOND_BONDED && !device.isConnected();
     63     }
     64 
     65     @Override
     66     public boolean onPreferenceClick(Preference preference) {
     67         final CachedBluetoothDevice device = ((BluetoothDevicePreference) preference)
     68                 .getBluetoothDevice();
     69         device.connect(true);
     70         return true;
     71     }
     72 }
     73