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 static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED; 20 import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; 21 22 import android.bluetooth.BluetoothAdapter; 23 import android.bluetooth.BluetoothClass; 24 import android.bluetooth.BluetoothDevice; 25 import android.bluetooth.BluetoothProfile; 26 import android.bluetooth.BluetoothSap; 27 import android.bluetooth.BluetoothUuid; 28 import android.content.Context; 29 import android.os.ParcelUuid; 30 import android.util.Log; 31 32 import com.android.settingslib.R; 33 34 import java.util.ArrayList; 35 import java.util.List; 36 37 /** 38 * SapProfile handles Bluetooth SAP profile. 39 */ 40 final class SapProfile implements LocalBluetoothProfile { 41 private static final String TAG = "SapProfile"; 42 43 private BluetoothSap mService; 44 private boolean mIsProfileReady; 45 46 private final CachedBluetoothDeviceManager mDeviceManager; 47 private final LocalBluetoothProfileManager mProfileManager; 48 49 static final ParcelUuid[] UUIDS = { 50 BluetoothUuid.SAP, 51 }; 52 53 static final String NAME = "SAP"; 54 55 // Order of this profile in device profiles list 56 private static final int ORDINAL = 10; 57 58 // These callbacks run on the main thread. 59 private final class SapServiceListener 60 implements BluetoothProfile.ServiceListener { 61 onServiceConnected(int profile, BluetoothProfile proxy)62 public void onServiceConnected(int profile, BluetoothProfile proxy) { 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(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 mProfileManager.callServiceDisconnectedListeners(); 85 mIsProfileReady=false; 86 } 87 } 88 isProfileReady()89 public boolean isProfileReady() { 90 return mIsProfileReady; 91 } 92 93 @Override getProfileId()94 public int getProfileId() { 95 return BluetoothProfile.SAP; 96 } 97 SapProfile(Context context, CachedBluetoothDeviceManager deviceManager, LocalBluetoothProfileManager profileManager)98 SapProfile(Context context, CachedBluetoothDeviceManager deviceManager, 99 LocalBluetoothProfileManager profileManager) { 100 mDeviceManager = deviceManager; 101 mProfileManager = profileManager; 102 BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, new SapServiceListener(), 103 BluetoothProfile.SAP); 104 } 105 accessProfileEnabled()106 public boolean accessProfileEnabled() { 107 return true; 108 } 109 isAutoConnectable()110 public boolean isAutoConnectable() { 111 return true; 112 } 113 getConnectionStatus(BluetoothDevice device)114 public int getConnectionStatus(BluetoothDevice device) { 115 if (mService == null) { 116 return BluetoothProfile.STATE_DISCONNECTED; 117 } 118 return mService.getConnectionState(device); 119 } 120 121 @Override isEnabled(BluetoothDevice device)122 public boolean isEnabled(BluetoothDevice device) { 123 if (mService == null) { 124 return false; 125 } 126 return mService.getConnectionPolicy(device) > CONNECTION_POLICY_FORBIDDEN; 127 } 128 129 @Override getConnectionPolicy(BluetoothDevice device)130 public int getConnectionPolicy(BluetoothDevice device) { 131 if (mService == null) { 132 return CONNECTION_POLICY_FORBIDDEN; 133 } 134 return mService.getConnectionPolicy(device); 135 } 136 137 @Override setEnabled(BluetoothDevice device, boolean enabled)138 public boolean setEnabled(BluetoothDevice device, boolean enabled) { 139 boolean isSuccessful = false; 140 if (mService == null) { 141 return false; 142 } 143 if (enabled) { 144 if (mService.getConnectionPolicy(device) < CONNECTION_POLICY_ALLOWED) { 145 isSuccessful = mService.setConnectionPolicy(device, CONNECTION_POLICY_ALLOWED); 146 } 147 } else { 148 isSuccessful = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN); 149 } 150 151 return isSuccessful; 152 } 153 getConnectedDevices()154 public List<BluetoothDevice> getConnectedDevices() { 155 if (mService == null) { 156 return new ArrayList<BluetoothDevice>(0); 157 } 158 return mService.getDevicesMatchingConnectionStates( 159 new int[] {BluetoothProfile.STATE_CONNECTED, 160 BluetoothProfile.STATE_CONNECTING, 161 BluetoothProfile.STATE_DISCONNECTING}); 162 } 163 toString()164 public String toString() { 165 return NAME; 166 } 167 getOrdinal()168 public int getOrdinal() { 169 return ORDINAL; 170 } 171 getNameResource(BluetoothDevice device)172 public int getNameResource(BluetoothDevice device) { 173 return R.string.bluetooth_profile_sap; 174 } 175 getSummaryResourceForDevice(BluetoothDevice device)176 public int getSummaryResourceForDevice(BluetoothDevice device) { 177 int state = getConnectionStatus(device); 178 switch (state) { 179 case BluetoothProfile.STATE_DISCONNECTED: 180 return R.string.bluetooth_sap_profile_summary_use_for; 181 182 case BluetoothProfile.STATE_CONNECTED: 183 return R.string.bluetooth_sap_profile_summary_connected; 184 185 default: 186 return BluetoothUtils.getConnectionStateSummary(state); 187 } 188 } 189 getDrawableResource(BluetoothClass btClass)190 public int getDrawableResource(BluetoothClass btClass) { 191 return com.android.internal.R.drawable.ic_phone; 192 } 193 finalize()194 protected void finalize() { 195 Log.d(TAG, "finalize()"); 196 if (mService != null) { 197 try { 198 BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.SAP, 199 mService); 200 mService = null; 201 }catch (Throwable t) { 202 Log.w(TAG, "Error cleaning up SAP proxy", t); 203 } 204 } 205 } 206 } 207