1 package com.android.settingslib.bluetooth; 2 3 import android.bluetooth.BluetoothClass; 4 import android.bluetooth.BluetoothDevice; 5 import android.bluetooth.BluetoothProfile; 6 import android.content.Context; 7 import android.graphics.drawable.Drawable; 8 import android.support.annotation.DrawableRes; 9 import android.util.Pair; 10 11 import com.android.settingslib.R; 12 import com.android.settingslib.graph.BluetoothDeviceLayerDrawable; 13 14 import java.util.List; 15 16 public class Utils { 17 public static final boolean V = false; // verbose logging 18 public static final boolean D = true; // regular logging 19 20 private static ErrorListener sErrorListener; 21 getConnectionStateSummary(int connectionState)22 public static int getConnectionStateSummary(int connectionState) { 23 switch (connectionState) { 24 case BluetoothProfile.STATE_CONNECTED: 25 return R.string.bluetooth_connected; 26 case BluetoothProfile.STATE_CONNECTING: 27 return R.string.bluetooth_connecting; 28 case BluetoothProfile.STATE_DISCONNECTED: 29 return R.string.bluetooth_disconnected; 30 case BluetoothProfile.STATE_DISCONNECTING: 31 return R.string.bluetooth_disconnecting; 32 default: 33 return 0; 34 } 35 } 36 showError(Context context, String name, int messageResId)37 static void showError(Context context, String name, int messageResId) { 38 if (sErrorListener != null) { 39 sErrorListener.onShowError(context, name, messageResId); 40 } 41 } 42 setErrorListener(ErrorListener listener)43 public static void setErrorListener(ErrorListener listener) { 44 sErrorListener = listener; 45 } 46 47 public interface ErrorListener { onShowError(Context context, String name, int messageResId)48 void onShowError(Context context, String name, int messageResId); 49 } 50 getBtClassDrawableWithDescription(Context context, CachedBluetoothDevice cachedDevice)51 public static Pair<Drawable, String> getBtClassDrawableWithDescription(Context context, 52 CachedBluetoothDevice cachedDevice) { 53 return getBtClassDrawableWithDescription(context, cachedDevice, 1 /* iconScale */); 54 } 55 getBtClassDrawableWithDescription(Context context, CachedBluetoothDevice cachedDevice, float iconScale)56 public static Pair<Drawable, String> getBtClassDrawableWithDescription(Context context, 57 CachedBluetoothDevice cachedDevice, float iconScale) { 58 BluetoothClass btClass = cachedDevice.getBtClass(); 59 final int level = cachedDevice.getBatteryLevel(); 60 if (btClass != null) { 61 switch (btClass.getMajorDeviceClass()) { 62 case BluetoothClass.Device.Major.COMPUTER: 63 return new Pair<>(getBluetoothDrawable(context, R.drawable.ic_bt_laptop, level, 64 iconScale), 65 context.getString(R.string.bluetooth_talkback_computer)); 66 67 case BluetoothClass.Device.Major.PHONE: 68 return new Pair<>( 69 getBluetoothDrawable(context, R.drawable.ic_bt_cellphone, level, 70 iconScale), 71 context.getString(R.string.bluetooth_talkback_phone)); 72 73 case BluetoothClass.Device.Major.PERIPHERAL: 74 return new Pair<>( 75 getBluetoothDrawable(context, HidProfile.getHidClassDrawable(btClass), 76 level, iconScale), 77 context.getString(R.string.bluetooth_talkback_input_peripheral)); 78 79 case BluetoothClass.Device.Major.IMAGING: 80 return new Pair<>( 81 getBluetoothDrawable(context, R.drawable.ic_settings_print, level, 82 iconScale), 83 context.getString(R.string.bluetooth_talkback_imaging)); 84 85 default: 86 // unrecognized device class; continue 87 } 88 } 89 90 List<LocalBluetoothProfile> profiles = cachedDevice.getProfiles(); 91 for (LocalBluetoothProfile profile : profiles) { 92 int resId = profile.getDrawableResource(btClass); 93 if (resId != 0) { 94 return new Pair<>(getBluetoothDrawable(context, resId, level, iconScale), null); 95 } 96 } 97 if (btClass != null) { 98 if (btClass.doesClassMatch(BluetoothClass.PROFILE_HEADSET)) { 99 return new Pair<>( 100 getBluetoothDrawable(context, R.drawable.ic_bt_headset_hfp, level, 101 iconScale), 102 context.getString(R.string.bluetooth_talkback_headset)); 103 } 104 if (btClass.doesClassMatch(BluetoothClass.PROFILE_A2DP)) { 105 return new Pair<>( 106 getBluetoothDrawable(context, R.drawable.ic_bt_headphones_a2dp, level, 107 iconScale), 108 context.getString(R.string.bluetooth_talkback_headphone)); 109 } 110 } 111 return new Pair<>( 112 getBluetoothDrawable(context, R.drawable.ic_settings_bluetooth, level, iconScale), 113 context.getString(R.string.bluetooth_talkback_bluetooth)); 114 } 115 getBluetoothDrawable(Context context, @DrawableRes int resId, int batteryLevel, float iconScale)116 public static Drawable getBluetoothDrawable(Context context, @DrawableRes int resId, 117 int batteryLevel, float iconScale) { 118 if (batteryLevel != BluetoothDevice.BATTERY_LEVEL_UNKNOWN) { 119 return BluetoothDeviceLayerDrawable.createLayerDrawable(context, resId, batteryLevel, 120 iconScale); 121 } else { 122 return context.getDrawable(resId); 123 } 124 } 125 } 126