Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9135a4b0 authored by Omair Kamil's avatar Omair Kamil Committed by Gerrit Code Review
Browse files

Merge "Remove use of iterators from ContextMap where unneeded." into main

parents cef77050 60c7e635
Loading
Loading
Loading
Loading
+37 −73
Original line number Diff line number Diff line
@@ -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.
@@ -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);
            }
        }
@@ -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 */
@@ -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);
            }
        }
@@ -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;
@@ -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;
                }
@@ -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;
                }
@@ -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);
                }
@@ -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) {
@@ -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);
        }
    }