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