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

Commit 1a2c1e97 authored by Dorin Drimus's avatar Dorin Drimus
Browse files

Fully synchronize access to gatt connections

java.util.ConcurrentModificationException could be thrown when accessing
mConnections using an iterator. To prevent this, fully guard synchronize
access to it and also make it private to avoid unsynchronized external
access.

Tag: #stability
Bug: 178334266
Test: manual, build ADT3 on internal repo and connected remote
Change-Id: Idb14c7e5aeab076db765aca88b1067148f35ec81
parent c9e4f1c2
Loading
Loading
Loading
Loading
+47 −34
Original line number Diff line number Diff line
@@ -172,10 +172,10 @@ import java.util.UUID;
    private List<App> mApps = new ArrayList<App>();

    /** Internal map to keep track of logging information by app name */
    HashMap<Integer, AppScanStats> mAppScanStats = new HashMap<Integer, AppScanStats>();
    private HashMap<Integer, AppScanStats> mAppScanStats = new HashMap<Integer, AppScanStats>();

    /** Internal list of connected devices **/
    Set<Connection> mConnections = new HashSet<Connection>();
    private Set<Connection> mConnections = new HashSet<Connection>();

    /**
     * Add an entry to the application context list.
@@ -281,6 +281,7 @@ import java.util.UUID;
     * Remove all connections for a given application ID.
     */
    void removeConnectionsByAppId(int appId) {
        synchronized (mConnections) {
            Iterator<Connection> i = mConnections.iterator();
            while (i.hasNext()) {
                Connection connection = i.next();
@@ -289,6 +290,7 @@ import java.util.UUID;
                }
            }
        }
    }

    /**
     * Get an application context by ID.
@@ -381,11 +383,13 @@ import java.util.UUID;
     */
    Set<String> getConnectedDevices() {
        Set<String> addresses = new HashSet<String>();
        synchronized (mConnections) {
            Iterator<Connection> i = mConnections.iterator();
            while (i.hasNext()) {
                Connection connection = i.next();
                addresses.add(connection.address);
            }
        }
        return addresses;
    }

@@ -393,6 +397,7 @@ import java.util.UUID;
     * Get an application context by a connection ID.
     */
    App getByConnId(int connId) {
        synchronized (mConnections) {
            Iterator<Connection> ii = mConnections.iterator();
            while (ii.hasNext()) {
                Connection connection = ii.next();
@@ -400,6 +405,7 @@ import java.util.UUID;
                    return getById(connection.appId);
                }
            }
        }
        return null;
    }

@@ -411,7 +417,7 @@ import java.util.UUID;
        if (entry == null) {
            return null;
        }

        synchronized (mConnections) {
            Iterator<Connection> i = mConnections.iterator();
            while (i.hasNext()) {
                Connection connection = i.next();
@@ -419,6 +425,7 @@ import java.util.UUID;
                    return connection.connId;
                }
            }
        }
        return null;
    }

@@ -426,6 +433,7 @@ import java.util.UUID;
     * Returns the device address for a given connection ID.
     */
    String addressByConnId(int connId) {
        synchronized (mConnections) {
            Iterator<Connection> i = mConnections.iterator();
            while (i.hasNext()) {
                Connection connection = i.next();
@@ -433,11 +441,13 @@ import java.util.UUID;
                    return connection.address;
                }
            }
        }
        return null;
    }

    List<Connection> getConnectionByApp(int appId) {
        List<Connection> currentConnections = new ArrayList<Connection>();
        synchronized (mConnections) {
            Iterator<Connection> i = mConnections.iterator();
            while (i.hasNext()) {
                Connection connection = i.next();
@@ -445,6 +455,7 @@ import java.util.UUID;
                    currentConnections.add(connection);
                }
            }
        }
        return currentConnections;
    }

@@ -472,9 +483,11 @@ import java.util.UUID;
     */
    Map<Integer, String> getConnectedMap() {
        Map<Integer, String> connectedmap = new HashMap<Integer, String>();
        synchronized (mConnections) {
            for (Connection conn : mConnections) {
                connectedmap.put(conn.appId, conn.address);
            }
        }
        return connectedmap;
    }