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 
     17 package com.android.settingslib.bluetooth;
     18 
     19 import android.bluetooth.BluetoothAdapter;
     20 import android.bluetooth.BluetoothClass;
     21 import android.bluetooth.BluetoothDevice;
     22 import android.bluetooth.BluetoothHidDevice;
     23 import android.bluetooth.BluetoothProfile;
     24 import android.content.Context;
     25 import android.util.Log;
     26 
     27 import com.android.settingslib.R;
     28 
     29 import java.util.Collection;
     30 import java.util.List;
     31 
     32 /**
     33  * HidProfile handles Bluetooth HID profile.
     34  */
     35 public class HidDeviceProfile implements LocalBluetoothProfile {
     36     private static final String TAG = "HidDeviceProfile";
     37     // Order of this profile in device profiles list
     38     private static final int ORDINAL = 18;
     39     // HID Device Profile is always preferred.
     40     private static final int PREFERRED_VALUE = -1;
     41     private static final boolean DEBUG = true;
     42 
     43     private final LocalBluetoothAdapter mLocalAdapter;
     44     private final CachedBluetoothDeviceManager mDeviceManager;
     45     private final LocalBluetoothProfileManager mProfileManager;
     46     static final String NAME = "HID DEVICE";
     47 
     48     private BluetoothHidDevice mService;
     49     private boolean mIsProfileReady;
     50 
     51     HidDeviceProfile(Context context, LocalBluetoothAdapter adapter,
     52             CachedBluetoothDeviceManager deviceManager,
     53             LocalBluetoothProfileManager profileManager) {
     54         mLocalAdapter = adapter;
     55         mDeviceManager = deviceManager;
     56         mProfileManager = profileManager;
     57         adapter.getProfileProxy(context, new HidDeviceServiceListener(),
     58                 BluetoothProfile.HID_DEVICE);
     59     }
     60 
     61     // These callbacks run on the main thread.
     62     private final class HidDeviceServiceListener
     63             implements BluetoothProfile.ServiceListener {
     64 
     65         public void onServiceConnected(int profile, BluetoothProfile proxy) {
     66             if (DEBUG) {
     67                 Log.d(TAG,"Bluetooth service connected :-)");
     68             }
     69             mService = (BluetoothHidDevice) proxy;
     70             // We just bound to the service, so refresh the UI for any connected HID devices.
     71             List<BluetoothDevice> deviceList = mService.getConnectedDevices();
     72             for (BluetoothDevice nextDevice : deviceList) {
     73                 CachedBluetoothDevice device = mDeviceManager.findDevice(nextDevice);
     74                 // we may add a new device here, but generally this should not happen
     75                 if (device == null) {
     76                     Log.w(TAG, "HidProfile found new device: " + nextDevice);
     77                     device = mDeviceManager.addDevice(mLocalAdapter, mProfileManager, nextDevice);
     78                 }
     79                 Log.d(TAG, "Connection status changed: " + device);
     80                 device.onProfileStateChanged(HidDeviceProfile.this,
     81                         BluetoothProfile.STATE_CONNECTED);
     82                 device.refresh();
     83             }
     84             mIsProfileReady = true;
     85         }
     86 
     87         public void onServiceDisconnected(int profile) {
     88             if (DEBUG) {
     89                 Log.d(TAG, "Bluetooth service disconnected");
     90             }
     91             mIsProfileReady = false;
     92         }
     93     }
     94 
     95     @Override
     96     public boolean isProfileReady() {
     97         return mIsProfileReady;
     98     }
     99 
    100     @Override
    101     public int getProfileId() {
    102         return BluetoothProfile.HID_DEVICE;
    103     }
    104 
    105     @Override
    106     public boolean isConnectable() {
    107         return true;
    108     }
    109 
    110     @Override
    111     public boolean isAutoConnectable() {
    112         return false;
    113     }
    114 
    115     @Override
    116     public boolean connect(BluetoothDevice device) {
    117         return false;
    118     }
    119 
    120     @Override
    121     public boolean disconnect(BluetoothDevice device) {
    122         if (mService == null) {
    123             return false;
    124         }
    125         return mService.disconnect(device);
    126     }
    127 
    128     @Override
    129     public int getConnectionStatus(BluetoothDevice device) {
    130         if (mService == null) {
    131             return BluetoothProfile.STATE_DISCONNECTED;
    132         }
    133         List<BluetoothDevice> deviceList = mService.getConnectedDevices();
    134 
    135         return !deviceList.isEmpty() && deviceList.contains(device)
    136                 ? mService.getConnectionState(device)
    137                 : BluetoothProfile.STATE_DISCONNECTED;
    138     }
    139 
    140     @Override
    141     public boolean isPreferred(BluetoothDevice device) {
    142         return getConnectionStatus(device) != BluetoothProfile.STATE_DISCONNECTED;
    143     }
    144 
    145     @Override
    146     public int getPreferred(BluetoothDevice device) {
    147         return PREFERRED_VALUE;
    148     }
    149 
    150     @Override
    151     public void setPreferred(BluetoothDevice device, boolean preferred) {
    152         // if set preferred to false, then disconnect to the current device
    153         if (!preferred) {
    154             mService.disconnect(device);
    155         }
    156     }
    157 
    158     @Override
    159     public String toString() {
    160         return NAME;
    161     }
    162 
    163     @Override
    164     public int getOrdinal() {
    165         return ORDINAL;
    166     }
    167 
    168     @Override
    169     public int getNameResource(BluetoothDevice device) {
    170         return R.string.bluetooth_profile_hid;
    171     }
    172 
    173     @Override
    174     public int getSummaryResourceForDevice(BluetoothDevice device) {
    175         final int state = getConnectionStatus(device);
    176         switch (state) {
    177             case BluetoothProfile.STATE_DISCONNECTED:
    178                 return R.string.bluetooth_hid_profile_summary_use_for;
    179             case BluetoothProfile.STATE_CONNECTED:
    180                 return R.string.bluetooth_hid_profile_summary_connected;
    181             default:
    182                 return Utils.getConnectionStateSummary(state);
    183         }
    184     }
    185 
    186     @Override
    187     public int getDrawableResource(BluetoothClass btClass) {
    188         return R.drawable.ic_bt_misc_hid;
    189     }
    190 
    191     protected void finalize() {
    192         if (DEBUG) {
    193             Log.d(TAG, "finalize()");
    194         }
    195         if (mService != null) {
    196             try {
    197                 BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.HID_DEVICE,
    198                         mService);
    199                 mService = null;
    200             } catch (Throwable t) {
    201                 Log.w(TAG, "Error cleaning up HID proxy", t);
    202             }
    203         }
    204     }
    205 }
    206