ContextMap.java revision eb7b90f5b93db1230a5b64caa3d8d05a642e33a6
1/* 2 * Copyright (C) 2013 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 */ 16package com.android.bluetooth.gatt; 17 18import android.os.Binder; 19import android.os.IBinder; 20import android.os.IBinder.DeathRecipient; 21import android.os.IInterface; 22import android.os.RemoteException; 23import android.util.Log; 24import java.util.ArrayList; 25import java.util.HashSet; 26import java.util.Iterator; 27import java.util.List; 28import java.util.NoSuchElementException; 29import java.util.Set; 30import java.util.UUID; 31import java.util.HashMap; 32import java.util.Map; 33 34import com.android.bluetooth.btservice.BluetoothProto; 35/** 36 * Helper class that keeps track of registered GATT applications. 37 * This class manages application callbacks and keeps track of GATT connections. 38 * @hide 39 */ 40/*package*/ class ContextMap<T> { 41 private static final String TAG = GattServiceConfig.TAG_PREFIX + "ContextMap"; 42 43 /** 44 * Connection class helps map connection IDs to device addresses. 45 */ 46 class Connection { 47 int connId; 48 String address; 49 int appId; 50 long startTime; 51 52 Connection(int connId, String address,int appId) { 53 this.connId = connId; 54 this.address = address; 55 this.appId = appId; 56 this.startTime = System.currentTimeMillis(); 57 } 58 } 59 60 /** 61 * Application entry mapping UUIDs to appIDs and callbacks. 62 */ 63 class App { 64 /** The UUID of the application */ 65 UUID uuid; 66 67 /** The id of the application */ 68 int id; 69 70 /** The package name of the application */ 71 String name; 72 73 /** Statistics for this app */ 74 AppScanStats appScanStats; 75 76 /** Application callbacks */ 77 T callback; 78 79 /** Death receipient */ 80 private IBinder.DeathRecipient mDeathRecipient; 81 82 /** Flag to signal that transport is congested */ 83 Boolean isCongested = false; 84 85 /** Internal callback info queue, waiting to be send on congestion clear */ 86 private List<CallbackInfo> congestionQueue = new ArrayList<CallbackInfo>(); 87 88 /** 89 * Creates a new app context. 90 */ 91 App(UUID uuid, T callback, String name, AppScanStats appScanStats) { 92 this.uuid = uuid; 93 this.callback = callback; 94 this.name = name; 95 this.appScanStats = appScanStats; 96 } 97 98 /** 99 * Link death recipient 100 */ 101 void linkToDeath(IBinder.DeathRecipient deathRecipient) { 102 try { 103 IBinder binder = ((IInterface)callback).asBinder(); 104 binder.linkToDeath(deathRecipient, 0); 105 mDeathRecipient = deathRecipient; 106 } catch (RemoteException e) { 107 Log.e(TAG, "Unable to link deathRecipient for app id " + id); 108 } 109 } 110 111 /** 112 * Unlink death recipient 113 */ 114 void unlinkToDeath() { 115 if (mDeathRecipient != null) { 116 try { 117 IBinder binder = ((IInterface)callback).asBinder(); 118 binder.unlinkToDeath(mDeathRecipient,0); 119 } catch (NoSuchElementException e) { 120 Log.e(TAG, "Unable to unlink deathRecipient for app id " + id); 121 } 122 } 123 } 124 125 void queueCallback(CallbackInfo callbackInfo) { 126 congestionQueue.add(callbackInfo); 127 } 128 129 CallbackInfo popQueuedCallback() { 130 if (congestionQueue.size() == 0) return null; 131 return congestionQueue.remove(0); 132 } 133 } 134 135 /** Our internal application list */ 136 List<App> mApps = new ArrayList<App>(); 137 138 /** Internal map to keep track of logging information by app name */ 139 HashMap<String, AppScanStats> mAppScanStats = new HashMap<String, AppScanStats>(); 140 141 /** Internal list of connected devices **/ 142 Set<Connection> mConnections = new HashSet<Connection>(); 143 144 /** 145 * Add an entry to the application context list. 146 */ 147 void add(UUID uuid, T callback, GattService service) { 148 String appName = service.getPackageManager().getNameForUid( 149 Binder.getCallingUid()); 150 if (appName == null) { 151 // Assign an app name if one isn't found 152 appName = "Unknown App (UID: " + Binder.getCallingUid() + ")"; 153 } 154 synchronized (mApps) { 155 AppScanStats appScanStats = mAppScanStats.get(appName); 156 if (appScanStats == null) { 157 appScanStats = new AppScanStats(appName, this, service); 158 mAppScanStats.put(appName, appScanStats); 159 } 160 mApps.add(new App(uuid, callback, appName, appScanStats)); 161 appScanStats.isRegistered = true; 162 } 163 } 164 165 /** 166 * Remove the context for a given UUID 167 */ 168 void remove(UUID uuid) { 169 synchronized (mApps) { 170 Iterator<App> i = mApps.iterator(); 171 while (i.hasNext()) { 172 App entry = i.next(); 173 if (entry.uuid.equals(uuid)) { 174 entry.unlinkToDeath(); 175 entry.appScanStats.isRegistered = false; 176 i.remove(); 177 break; 178 } 179 } 180 } 181 } 182 183 /** 184 * Remove the context for a given application ID. 185 */ 186 void remove(int id) { 187 synchronized (mApps) { 188 Iterator<App> i = mApps.iterator(); 189 while (i.hasNext()) { 190 App entry = i.next(); 191 if (entry.id == id) { 192 entry.unlinkToDeath(); 193 entry.appScanStats.isRegistered = false; 194 i.remove(); 195 break; 196 } 197 } 198 } 199 } 200 201 /** 202 * Add a new connection for a given application ID. 203 */ 204 void addConnection(int id, int connId, String address) { 205 synchronized (mConnections) { 206 App entry = getById(id); 207 if (entry != null){ 208 mConnections.add(new Connection(connId, address, id)); 209 } 210 } 211 } 212 213 /** 214 * Remove a connection with the given ID. 215 */ 216 void removeConnection(int id, int connId) { 217 synchronized (mConnections) { 218 Iterator<Connection> i = mConnections.iterator(); 219 while (i.hasNext()) { 220 Connection connection = i.next(); 221 if (connection.connId == connId) { 222 i.remove(); 223 break; 224 } 225 } 226 } 227 } 228 229 /** 230 * Get an application context by ID. 231 */ 232 App getById(int id) { 233 Iterator<App> i = mApps.iterator(); 234 while (i.hasNext()) { 235 App entry = i.next(); 236 if (entry.id == id) return entry; 237 } 238 Log.e(TAG, "Context not found for ID " + id); 239 return null; 240 } 241 242 /** 243 * Get an application context by UUID. 244 */ 245 App getByUuid(UUID uuid) { 246 Iterator<App> i = mApps.iterator(); 247 while (i.hasNext()) { 248 App entry = i.next(); 249 if (entry.uuid.equals(uuid)) return entry; 250 } 251 Log.e(TAG, "Context not found for UUID " + uuid); 252 return null; 253 } 254 255 /** 256 * Get an application context by the calling Apps name. 257 */ 258 App getByName(String name) { 259 Iterator<App> i = mApps.iterator(); 260 while (i.hasNext()) { 261 App entry = i.next(); 262 if (entry.name.equals(name)) return entry; 263 } 264 Log.e(TAG, "Context not found for name " + name); 265 return null; 266 } 267 268 /** 269 * Get Logging info by ID 270 */ 271 AppScanStats getAppScanStatsById(int id) { 272 App temp = getById(id); 273 if (temp != null) { 274 return temp.appScanStats; 275 } 276 return null; 277 } 278 279 /** 280 * Get Logging info by application name 281 */ 282 AppScanStats getAppScanStatsByName(String name) { 283 return mAppScanStats.get(name); 284 } 285 286 /** 287 * Get the device addresses for all connected devices 288 */ 289 Set<String> getConnectedDevices() { 290 Set<String> addresses = new HashSet<String>(); 291 Iterator<Connection> i = mConnections.iterator(); 292 while (i.hasNext()) { 293 Connection connection = i.next(); 294 addresses.add(connection.address); 295 } 296 return addresses; 297 } 298 299 /** 300 * Get an application context by a connection ID. 301 */ 302 App getByConnId(int connId) { 303 Iterator<Connection> ii = mConnections.iterator(); 304 while (ii.hasNext()) { 305 Connection connection = ii.next(); 306 if (connection.connId == connId){ 307 return getById(connection.appId); 308 } 309 } 310 return null; 311 } 312 313 /** 314 * Returns a connection ID for a given device address. 315 */ 316 Integer connIdByAddress(int id, String address) { 317 App entry = getById(id); 318 if (entry == null) return null; 319 320 Iterator<Connection> i = mConnections.iterator(); 321 while (i.hasNext()) { 322 Connection connection = i.next(); 323 if (connection.address.equals(address) && connection.appId == id) 324 return connection.connId; 325 } 326 return null; 327 } 328 329 /** 330 * Returns the device address for a given connection ID. 331 */ 332 String addressByConnId(int connId) { 333 Iterator<Connection> i = mConnections.iterator(); 334 while (i.hasNext()) { 335 Connection connection = i.next(); 336 if (connection.connId == connId) return connection.address; 337 } 338 return null; 339 } 340 341 List<Connection> getConnectionByApp(int appId) { 342 List<Connection> currentConnections = new ArrayList<Connection>(); 343 Iterator<Connection> i = mConnections.iterator(); 344 while (i.hasNext()) { 345 Connection connection = i.next(); 346 if (connection.appId == appId) 347 currentConnections.add(connection); 348 } 349 return currentConnections; 350 } 351 352 /** 353 * Erases all application context entries. 354 */ 355 void clear() { 356 synchronized (mApps) { 357 Iterator<App> i = mApps.iterator(); 358 while (i.hasNext()) { 359 App entry = i.next(); 360 entry.unlinkToDeath(); 361 entry.appScanStats.isRegistered = false; 362 i.remove(); 363 } 364 } 365 366 synchronized (mConnections) { 367 mConnections.clear(); 368 } 369 } 370 371 /** 372 * Returns connect device map with addr and appid 373 */ 374 Map<Integer, String> getConnectedMap(){ 375 Map<Integer, String> connectedmap = new HashMap<Integer, String>(); 376 for(Connection conn: mConnections){ 377 connectedmap.put(conn.appId, conn.address); 378 } 379 return connectedmap; 380 } 381 382 /** 383 * Logs debug information. 384 */ 385 void dump(StringBuilder sb) { 386 sb.append(" Entries: " + mAppScanStats.size() + "\n\n"); 387 388 Iterator<Map.Entry<String, AppScanStats>> it = mAppScanStats.entrySet().iterator(); 389 while (it.hasNext()) { 390 Map.Entry<String, AppScanStats> entry = it.next(); 391 392 String name = entry.getKey(); 393 AppScanStats appScanStats = entry.getValue(); 394 appScanStats.dumpToString(sb); 395 } 396 } 397} 398