1 package org.robolectric.shadows; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 /** 7 * Manages remote address connections for {@link ShadowBluetoothGatt} and {@link 8 * ShadowBluetoothGattServer}. 9 */ 10 final class BluetoothConnectionManager { 11 12 private static volatile BluetoothConnectionManager instance; 13 14 /** Connection metadata for Gatt Server and Client connections. */ 15 private static class BluetoothConnectionMetadata { 16 boolean hasGattClientConnection = false; 17 boolean hasGattServerConnection = false; 18 setHasGattClientConnection(boolean hasGattClientConnection)19 void setHasGattClientConnection(boolean hasGattClientConnection) { 20 this.hasGattClientConnection = hasGattClientConnection; 21 } 22 setHasGattServerConnection(boolean hasGattServerConnection)23 void setHasGattServerConnection(boolean hasGattServerConnection) { 24 this.hasGattServerConnection = hasGattServerConnection; 25 } 26 hasGattClientConnection()27 boolean hasGattClientConnection() { 28 return hasGattClientConnection; 29 } 30 hasGattServerConnection()31 boolean hasGattServerConnection() { 32 return hasGattServerConnection; 33 } 34 isConnected()35 boolean isConnected() { 36 return hasGattClientConnection || hasGattServerConnection; 37 } 38 } 39 BluetoothConnectionManager()40 private BluetoothConnectionManager() {} 41 getInstance()42 static BluetoothConnectionManager getInstance() { 43 if (instance == null) { 44 synchronized (BluetoothConnectionManager.class) { 45 if (instance == null) { 46 instance = new BluetoothConnectionManager(); 47 } 48 } 49 } 50 return instance; 51 } 52 53 /** 54 * Map representing remote address connections, mapping a remote address to a {@link 55 * BluetoothConnectionMetadata}. 56 */ 57 private final Map<String, BluetoothConnectionMetadata> remoteAddressConnectionMap = 58 new HashMap<>(); 59 60 /** 61 * Register a Gatt Client Connection. Intended for use by {@link 62 * ShadowBluetoothGatt#notifyConnection} when simulating a successful Gatt Client Connection. 63 */ registerGattClientConnection(String remoteAddress)64 void registerGattClientConnection(String remoteAddress) { 65 if (!remoteAddressConnectionMap.containsKey(remoteAddress)) { 66 remoteAddressConnectionMap.put(remoteAddress, new BluetoothConnectionMetadata()); 67 } 68 remoteAddressConnectionMap.get(remoteAddress).setHasGattClientConnection(true); 69 } 70 71 /** 72 * Unregister a Gatt Client Connection. Intended for use by {@link 73 * ShadowBluetoothGatt#notifyDisconnection} when simulating a successful Gatt client 74 * disconnection. 75 */ unregisterGattClientConnection(String remoteAddress)76 void unregisterGattClientConnection(String remoteAddress) { 77 if (remoteAddressConnectionMap.containsKey(remoteAddress)) { 78 remoteAddressConnectionMap.get(remoteAddress).setHasGattClientConnection(false); 79 } 80 } 81 82 /** 83 * Register a Gatt Server Connection. Intended for use by {@link 84 * ShadowBluetoothGattServer#notifyConnection} when simulating a successful Gatt server 85 * connection. 86 */ registerGattServerConnection(String remoteAddress)87 void registerGattServerConnection(String remoteAddress) { 88 if (!remoteAddressConnectionMap.containsKey(remoteAddress)) { 89 remoteAddressConnectionMap.put(remoteAddress, new BluetoothConnectionMetadata()); 90 } 91 remoteAddressConnectionMap.get(remoteAddress).setHasGattServerConnection(true); 92 } 93 94 /** 95 * Unregister a Gatt Server Connection. Intended for use by {@link 96 * ShadowBluetoothGattServer#notifyDisconnection} when simulating a successful Gatt server 97 * disconnection. 98 */ unregisterGattServerConnection(String remoteAddress)99 void unregisterGattServerConnection(String remoteAddress) { 100 if (remoteAddressConnectionMap.containsKey(remoteAddress)) { 101 remoteAddressConnectionMap.get(remoteAddress).setHasGattServerConnection(false); 102 } 103 } 104 105 /** 106 * Returns true if remote address has an active gatt client connection. 107 * 108 * @param remoteAddress remote address 109 */ hasGattClientConnection(String remoteAddress)110 boolean hasGattClientConnection(String remoteAddress) { 111 return remoteAddressConnectionMap.containsKey(remoteAddress) 112 && remoteAddressConnectionMap.get(remoteAddress).hasGattClientConnection(); 113 } 114 115 /** 116 * Returns true if remote address has an active gatt server connection. 117 * 118 * @param remoteAddress remote address 119 */ hasGattServerConnection(String remoteAddress)120 boolean hasGattServerConnection(String remoteAddress) { 121 return remoteAddressConnectionMap.containsKey(remoteAddress) 122 && remoteAddressConnectionMap.get(remoteAddress).hasGattServerConnection(); 123 } 124 125 /** 126 * Returns true if remote address has an active connection. 127 * 128 * @param remoteAddress remote address 129 */ isConnected(String remoteAddress)130 boolean isConnected(String remoteAddress) { 131 return remoteAddressConnectionMap.containsKey(remoteAddress) 132 && remoteAddressConnectionMap.get(remoteAddress).isConnected(); 133 } 134 135 /** Clears all connection information */ resetConnections()136 void resetConnections() { 137 this.remoteAddressConnectionMap.clear(); 138 } 139 } 140