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

Commit 8c6af16b authored by Evan Chen's avatar Evan Chen
Browse files

Implement OnAssociationsChangedListener

Bug: 209584206
Test: atest CtsCompanionDevicesTestCases:AssociationsChangedListenerTest
Change-Id: I7a57b49c8cdee496a4920317255f896509dc75c0
parent 0f75b8e3
Loading
Loading
Loading
Loading
+21 −2
Original line number Diff line number Diff line
@@ -90,6 +90,7 @@ import android.os.Environment;
import android.os.Handler;
import android.os.Parcel;
import android.os.PowerWhitelistManager;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.os.ResultReceiver;
import android.os.ServiceManager;
@@ -183,6 +184,8 @@ public class CompanionDeviceManagerService extends SystemService {
            mUnbindDeviceListenersRunnable = new UnbindDeviceListenersRunnable();
    private ArrayMap<String, TriggerDeviceDisappearedRunnable> mTriggerDeviceDisappearedRunnables =
            new ArrayMap<>();
    private final RemoteCallbackList<IOnAssociationsChangedListener> mListeners =
            new RemoteCallbackList<>();

    final Object mLock = new Object();
    final Handler mMainHandler = Handler.getMain();
@@ -441,13 +444,17 @@ public class CompanionDeviceManagerService extends SystemService {
            enforceCallerCanManageCompanionDevice(getContext(),
                    "addOnAssociationsChangedListener");

            //TODO: Implement.
            mListeners.register(listener, userId);
        }

        @Override
        public void removeOnAssociationsChangedListener(IOnAssociationsChangedListener listener,
                int userId) {
            //TODO: Implement.
            enforceCallerCanInteractWithUserId(getContext(), userId);
            enforceCallerCanManageCompanionDevice(
                    getContext(), "removeOnAssociationsChangedListener");

            mListeners.unregister(listener);
        }

        @Override
@@ -941,6 +948,7 @@ public class CompanionDeviceManagerService extends SystemService {

    private void updateAssociations(Function<Set<AssociationInfo>, Set<AssociationInfo>> update,
            int userId) {
        final List<AssociationInfo> associationList;
        synchronized (mLock) {
            if (DEBUG) Slog.d(LOG_TAG, "Updating Associations set...");

@@ -951,6 +959,8 @@ public class CompanionDeviceManagerService extends SystemService {
                    new ArraySet<>(prevAssociations));
            if (DEBUG) Slog.d(LOG_TAG, "  > After: " + updatedAssociations);

            associationList = new ArrayList<>(updatedAssociations);

            mCachedAssociations.put(userId, unmodifiableSet(updatedAssociations));

            BackgroundThread.getHandler().sendMessage(
@@ -962,6 +972,15 @@ public class CompanionDeviceManagerService extends SystemService {

            updateAtm(userId, updatedAssociations);
        }

        mListeners.broadcast((listener, callbackUserId) -> {
            if ((int) callbackUserId == userId) {
                try {
                    listener.onAssociationsChanged(associationList);
                } catch (RemoteException ignored) {
                }
            }
        });
    }

    private void updateAtm(int userId, Set<AssociationInfo> associations) {