Loading android/app/src/com/android/bluetooth/gatt/ContextMap.java +37 −73 Original line number Diff line number Diff line Loading @@ -46,6 +46,7 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; import java.util.UUID; import java.util.function.Predicate; /** * Helper class that keeps track of registered GATT applications. Loading Loading @@ -309,9 +310,7 @@ public class ContextMap<C, T> { public List<Integer> getAllAppsIds() { List<Integer> appIds = new ArrayList(); synchronized (mAppsLock) { Iterator<App> i = mApps.iterator(); while (i.hasNext()) { App entry = i.next(); for (App entry : mApps) { appIds.add(entry.id); } } Loading Loading @@ -351,76 +350,58 @@ public class ContextMap<C, T> { */ void removeConnectionsByAppId(int appId) { synchronized (mConnectionsLock) { Iterator<Connection> i = mConnections.iterator(); while (i.hasNext()) { Connection connection = i.next(); if (connection.appId == appId) { i.remove(); } } mConnections.removeIf(conn -> conn.appId == appId); } } /** Get an application context by ID. */ public App getById(int id) { private App getAppByPredicate(Predicate<App> predicate) { synchronized (mAppsLock) { Iterator<App> i = mApps.iterator(); while (i.hasNext()) { App entry = i.next(); if (entry.id == id) { return entry; // Intentionally using a for-loop over a stream for performance. for (App app : mApps) { if (predicate.test(app)) { return app; } } return null; } } /** Get an application context by ID. */ public App getById(int id) { App app = getAppByPredicate(entry -> entry.id == id); if (app == null) { Log.e(TAG, "Context not found for ID " + id); return null; } return app; } /** Get an application context by UUID. */ public App getByUuid(UUID uuid) { synchronized (mAppsLock) { Iterator<App> i = mApps.iterator(); while (i.hasNext()) { App entry = i.next(); if (entry.uuid.equals(uuid)) { return entry; } } } App app = getAppByPredicate(entry -> entry.uuid.equals(uuid)); if (app == null) { Log.e(TAG, "Context not found for UUID " + uuid); return null; } return app; } /** * Get an application context by the calling Apps name. */ public App getByName(String name) { synchronized (mAppsLock) { Iterator<App> i = mApps.iterator(); while (i.hasNext()) { App entry = i.next(); if (entry.name.equals(name)) { return entry; } } } App app = getAppByPredicate(entry -> entry.name.equals(name)); if (app == null) { Log.e(TAG, "Context not found for name " + name); return null; } return app; } /** Get an application context by the context info object. */ public App getByContextInfo(T contextInfo) { synchronized (mAppsLock) { Iterator<App> i = mApps.iterator(); while (i.hasNext()) { App entry = i.next(); if (entry.info != null && entry.info.equals(contextInfo)) { return entry; } } } App app = getAppByPredicate(entry -> entry.info != null && entry.info.equals(contextInfo)); if (app == null) { Log.e(TAG, "Context not found for info " + contextInfo); return null; } return app; } /** Get Logging info by ID */ Loading Loading @@ -577,9 +558,7 @@ public class ContextMap<C, T> { Set<String> getConnectedDevices() { Set<String> addresses = new HashSet<String>(); synchronized (mConnectionsLock) { Iterator<Connection> i = mConnections.iterator(); while (i.hasNext()) { Connection connection = i.next(); for (Connection connection : mConnections) { addresses.add(connection.address); } } Loading @@ -592,9 +571,7 @@ public class ContextMap<C, T> { App getByConnId(int connId) { int appId = -1; synchronized (mConnectionsLock) { Iterator<Connection> ii = mConnections.iterator(); while (ii.hasNext()) { Connection connection = ii.next(); for (Connection connection : mConnections) { if (connection.connId == connId) { appId = connection.appId; break; Loading @@ -616,9 +593,7 @@ public class ContextMap<C, T> { return null; } synchronized (mConnectionsLock) { Iterator<Connection> i = mConnections.iterator(); while (i.hasNext()) { Connection connection = i.next(); for (Connection connection : mConnections) { if (connection.address.equalsIgnoreCase(address) && connection.appId == id) { return connection.connId; } Loading @@ -632,9 +607,7 @@ public class ContextMap<C, T> { */ String addressByConnId(int connId) { synchronized (mConnectionsLock) { Iterator<Connection> i = mConnections.iterator(); while (i.hasNext()) { Connection connection = i.next(); for (Connection connection : mConnections) { if (connection.connId == connId) { return connection.address; } Loading @@ -646,9 +619,7 @@ public class ContextMap<C, T> { public List<Connection> getConnectionByApp(int appId) { List<Connection> currentConnections = new ArrayList<Connection>(); synchronized (mConnectionsLock) { Iterator<Connection> i = mConnections.iterator(); while (i.hasNext()) { Connection connection = i.next(); for (Connection connection : mConnections) { if (connection.appId == appId) { currentConnections.add(connection); } Loading @@ -660,15 +631,13 @@ public class ContextMap<C, T> { /** Erases all application context entries. */ public void clear() { synchronized (mAppsLock) { Iterator<App> i = mApps.iterator(); while (i.hasNext()) { App entry = i.next(); for (App entry : mApps) { entry.unlinkToDeath(); if (entry.appScanStats != null) { entry.appScanStats.isRegistered = false; } i.remove(); } mApps.clear(); } synchronized (mConnectionsLock) { Loading Loading @@ -699,12 +668,7 @@ public class ContextMap<C, T> { */ protected void dump(StringBuilder sb) { sb.append(" Entries: " + mAppScanStats.size() + "\n\n"); Iterator<Map.Entry<Integer, AppScanStats>> it = mAppScanStats.entrySet().iterator(); while (it.hasNext()) { Map.Entry<Integer, AppScanStats> entry = it.next(); AppScanStats appScanStats = entry.getValue(); for (AppScanStats appScanStats : mAppScanStats.values()) { appScanStats.dumpToString(sb); } } Loading Loading
android/app/src/com/android/bluetooth/gatt/ContextMap.java +37 −73 Original line number Diff line number Diff line Loading @@ -46,6 +46,7 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; import java.util.UUID; import java.util.function.Predicate; /** * Helper class that keeps track of registered GATT applications. Loading Loading @@ -309,9 +310,7 @@ public class ContextMap<C, T> { public List<Integer> getAllAppsIds() { List<Integer> appIds = new ArrayList(); synchronized (mAppsLock) { Iterator<App> i = mApps.iterator(); while (i.hasNext()) { App entry = i.next(); for (App entry : mApps) { appIds.add(entry.id); } } Loading Loading @@ -351,76 +350,58 @@ public class ContextMap<C, T> { */ void removeConnectionsByAppId(int appId) { synchronized (mConnectionsLock) { Iterator<Connection> i = mConnections.iterator(); while (i.hasNext()) { Connection connection = i.next(); if (connection.appId == appId) { i.remove(); } } mConnections.removeIf(conn -> conn.appId == appId); } } /** Get an application context by ID. */ public App getById(int id) { private App getAppByPredicate(Predicate<App> predicate) { synchronized (mAppsLock) { Iterator<App> i = mApps.iterator(); while (i.hasNext()) { App entry = i.next(); if (entry.id == id) { return entry; // Intentionally using a for-loop over a stream for performance. for (App app : mApps) { if (predicate.test(app)) { return app; } } return null; } } /** Get an application context by ID. */ public App getById(int id) { App app = getAppByPredicate(entry -> entry.id == id); if (app == null) { Log.e(TAG, "Context not found for ID " + id); return null; } return app; } /** Get an application context by UUID. */ public App getByUuid(UUID uuid) { synchronized (mAppsLock) { Iterator<App> i = mApps.iterator(); while (i.hasNext()) { App entry = i.next(); if (entry.uuid.equals(uuid)) { return entry; } } } App app = getAppByPredicate(entry -> entry.uuid.equals(uuid)); if (app == null) { Log.e(TAG, "Context not found for UUID " + uuid); return null; } return app; } /** * Get an application context by the calling Apps name. */ public App getByName(String name) { synchronized (mAppsLock) { Iterator<App> i = mApps.iterator(); while (i.hasNext()) { App entry = i.next(); if (entry.name.equals(name)) { return entry; } } } App app = getAppByPredicate(entry -> entry.name.equals(name)); if (app == null) { Log.e(TAG, "Context not found for name " + name); return null; } return app; } /** Get an application context by the context info object. */ public App getByContextInfo(T contextInfo) { synchronized (mAppsLock) { Iterator<App> i = mApps.iterator(); while (i.hasNext()) { App entry = i.next(); if (entry.info != null && entry.info.equals(contextInfo)) { return entry; } } } App app = getAppByPredicate(entry -> entry.info != null && entry.info.equals(contextInfo)); if (app == null) { Log.e(TAG, "Context not found for info " + contextInfo); return null; } return app; } /** Get Logging info by ID */ Loading Loading @@ -577,9 +558,7 @@ public class ContextMap<C, T> { Set<String> getConnectedDevices() { Set<String> addresses = new HashSet<String>(); synchronized (mConnectionsLock) { Iterator<Connection> i = mConnections.iterator(); while (i.hasNext()) { Connection connection = i.next(); for (Connection connection : mConnections) { addresses.add(connection.address); } } Loading @@ -592,9 +571,7 @@ public class ContextMap<C, T> { App getByConnId(int connId) { int appId = -1; synchronized (mConnectionsLock) { Iterator<Connection> ii = mConnections.iterator(); while (ii.hasNext()) { Connection connection = ii.next(); for (Connection connection : mConnections) { if (connection.connId == connId) { appId = connection.appId; break; Loading @@ -616,9 +593,7 @@ public class ContextMap<C, T> { return null; } synchronized (mConnectionsLock) { Iterator<Connection> i = mConnections.iterator(); while (i.hasNext()) { Connection connection = i.next(); for (Connection connection : mConnections) { if (connection.address.equalsIgnoreCase(address) && connection.appId == id) { return connection.connId; } Loading @@ -632,9 +607,7 @@ public class ContextMap<C, T> { */ String addressByConnId(int connId) { synchronized (mConnectionsLock) { Iterator<Connection> i = mConnections.iterator(); while (i.hasNext()) { Connection connection = i.next(); for (Connection connection : mConnections) { if (connection.connId == connId) { return connection.address; } Loading @@ -646,9 +619,7 @@ public class ContextMap<C, T> { public List<Connection> getConnectionByApp(int appId) { List<Connection> currentConnections = new ArrayList<Connection>(); synchronized (mConnectionsLock) { Iterator<Connection> i = mConnections.iterator(); while (i.hasNext()) { Connection connection = i.next(); for (Connection connection : mConnections) { if (connection.appId == appId) { currentConnections.add(connection); } Loading @@ -660,15 +631,13 @@ public class ContextMap<C, T> { /** Erases all application context entries. */ public void clear() { synchronized (mAppsLock) { Iterator<App> i = mApps.iterator(); while (i.hasNext()) { App entry = i.next(); for (App entry : mApps) { entry.unlinkToDeath(); if (entry.appScanStats != null) { entry.appScanStats.isRegistered = false; } i.remove(); } mApps.clear(); } synchronized (mConnectionsLock) { Loading Loading @@ -699,12 +668,7 @@ public class ContextMap<C, T> { */ protected void dump(StringBuilder sb) { sb.append(" Entries: " + mAppScanStats.size() + "\n\n"); Iterator<Map.Entry<Integer, AppScanStats>> it = mAppScanStats.entrySet().iterator(); while (it.hasNext()) { Map.Entry<Integer, AppScanStats> entry = it.next(); AppScanStats appScanStats = entry.getValue(); for (AppScanStats appScanStats : mAppScanStats.values()) { appScanStats.dumpToString(sb); } } Loading