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

Commit ab088f6c authored by Raphael Kim's avatar Raphael Kim Committed by Android (Google) Code Review
Browse files

Merge "[CDM] Fix removeOnTransportsChangedListener API" into main

parents 7cb2114c 307c8d7f
Loading
Loading
Loading
Loading
+27 −12
Original line number Diff line number Diff line
@@ -382,6 +382,10 @@ public final class CompanionDeviceManager {
    @GuardedBy("mListeners")
    private final ArrayList<OnAssociationsChangedListenerProxy> mListeners = new ArrayList<>();

    @GuardedBy("mTransportsChangedListeners")
    private final ArrayList<OnTransportsChangedListenerProxy> mTransportsChangedListeners =
            new ArrayList<>();

    @GuardedBy("mTransports")
    private final SparseArray<Transport> mTransports = new SparseArray<>();

@@ -998,6 +1002,7 @@ public final class CompanionDeviceManager {
            return;
        }

        synchronized (mTransportsChangedListeners) {
            final OnTransportsChangedListenerProxy proxy = new OnTransportsChangedListenerProxy(
                    executor, listener);
            try {
@@ -1005,6 +1010,8 @@ public final class CompanionDeviceManager {
            } catch (RemoteException e) {
                throw e.rethrowFromSystemServer();
            }
            mTransportsChangedListeners.add(proxy);
        }
    }

    /**
@@ -1022,13 +1029,21 @@ public final class CompanionDeviceManager {
            return;
        }

        final OnTransportsChangedListenerProxy proxy = new OnTransportsChangedListenerProxy(
                null, listener);
        synchronized (mTransportsChangedListeners) {
            final Iterator<OnTransportsChangedListenerProxy> iterator =
                    mTransportsChangedListeners.iterator();
            while (iterator.hasNext()) {
                final OnTransportsChangedListenerProxy proxy = iterator.next();
                if (proxy.mListener == listener) {
                    try {
                        mService.removeOnTransportsChangedListener(proxy);
                    } catch (RemoteException e) {
                        throw e.rethrowFromSystemServer();
                    }
                    iterator.remove();
                }
            }
        }
    }

    /**
+28 −27
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ public class CompanionTransportManager {
    @GuardedBy("mTransports")
    private final SparseArray<Transport> mTransports = new SparseArray<>();
    @NonNull
    @GuardedBy("mTransportsListeners")
    private final RemoteCallbackList<IOnTransportsChangedListener> mTransportsListeners =
            new RemoteCallbackList<>();
    /** Message type -> IOnMessageReceivedListener */
@@ -84,35 +85,29 @@ public class CompanionTransportManager {
     */
    public void addListener(IOnTransportsChangedListener listener) {
        Slog.i(TAG, "Registering OnTransportsChangedListener");
        synchronized (mTransportsListeners) {
            mTransportsListeners.register(listener);
        List<AssociationInfo> associations = new ArrayList<>();
        synchronized (mTransports) {
            for (int i = 0; i < mTransports.size(); i++) {
                AssociationInfo association = mAssociationStore.getAssociationById(
                        mTransports.keyAt(i));
                if (association != null) {
                    associations.add(association);
                }
            }
        }
            mTransportsListeners.broadcast(listener1 -> {
                // callback to the current listener with all the associations of the transports
                // immediately
                if (listener1 == listener) {
                    try {
                    listener.onTransportsChanged(associations);
                        listener.onTransportsChanged(getAssociationsWithTransport());
                    } catch (RemoteException ignored) {
                    }
                }
            });
        }
    }

    /**
     * Remove the listener for receiving callbacks when any of the transports is changed
     */
    public void removeListener(IOnTransportsChangedListener listener) {
        synchronized (mTransportsListeners) {
            mTransportsListeners.unregister(listener);
        }
    }

    /**
     * Remove the listener to stop receiving calbacks when a message is received for the given type
@@ -179,7 +174,7 @@ public class CompanionTransportManager {
        Slog.i(TAG, "Transport detached.");
    }

    private void notifyOnTransportsChanged() {
    private List<AssociationInfo> getAssociationsWithTransport() {
        List<AssociationInfo> associations = new ArrayList<>();
        synchronized (mTransports) {
            for (int i = 0; i < mTransports.size(); i++) {
@@ -190,13 +185,19 @@ public class CompanionTransportManager {
                }
            }
        }
        return associations;
    }

    private void notifyOnTransportsChanged() {
        synchronized (mTransportsListeners) {
            mTransportsListeners.broadcast(listener -> {
                try {
                listener.onTransportsChanged(associations);
                    listener.onTransportsChanged(getAssociationsWithTransport());
                } catch (RemoteException ignored) {
                }
            });
        }
    }

    private void initializeTransport(int associationId,
                                     ParcelFileDescriptor fd,