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