HidProfile.java revision 436b29e68e6608bed9e8e7d54385b8f62d89208e
1/* 2 * Copyright (C) 2011 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 17package com.android.settings.bluetooth; 18 19import android.bluetooth.BluetoothClass; 20import android.bluetooth.BluetoothDevice; 21import android.bluetooth.BluetoothInputDevice; 22import android.bluetooth.BluetoothProfile; 23import android.content.Context; 24 25import com.android.settings.R; 26 27import java.util.List; 28 29/** 30 * HidProfile handles Bluetooth HID profile. 31 */ 32final class HidProfile implements LocalBluetoothProfile { 33 private BluetoothInputDevice mService; 34 private boolean mProfileReady; 35 36 static final String NAME = "HID"; 37 38 // Order of this profile in device profiles list 39 private static final int ORDINAL = 3; 40 41 // These callbacks run on the main thread. 42 private final class InputDeviceServiceListener 43 implements BluetoothProfile.ServiceListener { 44 45 public void onServiceConnected(int profile, BluetoothProfile proxy) { 46 mService = (BluetoothInputDevice) proxy; 47 mProfileReady = true; 48 } 49 50 public void onServiceDisconnected(int profile) { 51 mProfileReady = false; 52 mService = null; 53 } 54 } 55 56 HidProfile(Context context, LocalBluetoothAdapter adapter) { 57 adapter.getProfileProxy(context, new InputDeviceServiceListener(), 58 BluetoothProfile.INPUT_DEVICE); 59 } 60 61 public boolean isConnectable() { 62 return true; 63 } 64 65 public boolean isAutoConnectable() { 66 return true; 67 } 68 69 public boolean connect(BluetoothDevice device) { 70 return mService.connect(device); 71 } 72 73 public boolean disconnect(BluetoothDevice device) { 74 return mService.disconnect(device); 75 } 76 77 public int getConnectionStatus(BluetoothDevice device) { 78 List<BluetoothDevice> deviceList = mService.getConnectedDevices(); 79 80 return !deviceList.isEmpty() && deviceList.get(0).equals(device) 81 ? mService.getConnectionState(device) 82 : BluetoothProfile.STATE_DISCONNECTED; 83 } 84 85 public boolean isPreferred(BluetoothDevice device) { 86 return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF; 87 } 88 89 public int getPreferred(BluetoothDevice device) { 90 return mService.getPriority(device); 91 } 92 93 public void setPreferred(BluetoothDevice device, boolean preferred) { 94 if (preferred) { 95 if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) { 96 mService.setPriority(device, BluetoothProfile.PRIORITY_ON); 97 } 98 } else { 99 mService.setPriority(device, BluetoothProfile.PRIORITY_OFF); 100 } 101 } 102 103 public boolean isProfileReady() { 104 return mProfileReady; 105 } 106 107 public String toString() { 108 return NAME; 109 } 110 111 public int getOrdinal() { 112 return ORDINAL; 113 } 114 115 public int getNameResource() { 116 return R.string.bluetooth_profile_hid; 117 } 118 119 public int getDisconnectResource() { 120 return R.string.bluetooth_disconnect_hid_profile; 121 } 122 123 public int getSummaryResourceForDevice(BluetoothDevice device) { 124 int state = mService.getConnectionState(device); 125 switch (state) { 126 case BluetoothProfile.STATE_DISCONNECTED: 127 return R.string.bluetooth_hid_profile_summary_use_for; 128 129 case BluetoothProfile.STATE_CONNECTED: 130 return R.string.bluetooth_hid_profile_summary_connected; 131 132 default: 133 return Utils.getConnectionStateSummary(state); 134 } 135 } 136 137 public int getDrawableResource(BluetoothClass btClass) { 138 if (btClass == null) { 139 return R.drawable.ic_bt_keyboard_hid; 140 } 141 return getHidClassDrawable(btClass); 142 } 143 144 static int getHidClassDrawable(BluetoothClass btClass) { 145 switch (btClass.getDeviceClass()) { 146 case BluetoothClass.Device.PERIPHERAL_KEYBOARD: 147 case BluetoothClass.Device.PERIPHERAL_KEYBOARD_POINTING: 148 return R.drawable.ic_bt_keyboard_hid; 149 case BluetoothClass.Device.PERIPHERAL_POINTING: 150 return R.drawable.ic_bt_pointing_hid; 151 default: 152 return R.drawable.ic_bt_misc_hid; 153 } 154 } 155} 156