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

Commit f3aca030 authored by Charlie Wang's avatar Charlie Wang
Browse files

Synchronize Existing Client Requests.

There's a chance between the removal and addition of ClientRequests that
no ClientRequeset mathces a package name. And in this case the data
would be lost because there aren't any observers.

Bug: 270906186.
Test: Builds and atest CtsAmbientContextServiceTestCases atest CtsWearableSensingServiceTestCases
Change-Id: Iad05e9e6c0fe395fc6d7d395e08cd1a51143fb46
parent b6ab92fa
Loading
Loading
Loading
Loading
+18 −11
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
@@ -128,7 +129,7 @@ public class AmbientContextManagerService extends
                PACKAGE_UPDATE_POLICY_REFRESH_EAGER
                        | /*To avoid high latency*/ PACKAGE_RESTART_POLICY_REFRESH_EAGER);
        mContext = context;
        mExistingClientRequests = new ArraySet<>();
        mExistingClientRequests = ConcurrentHashMap.newKeySet();
    }

    @Override
@@ -157,6 +158,7 @@ public class AmbientContextManagerService extends
            String callingPackage, IAmbientContextObserver observer) {
        Slog.d(TAG, "New client added: " + callingPackage);

        synchronized (mExistingClientRequests) {
            // Remove any existing ClientRequest for this user and package.
            mExistingClientRequests.removeAll(
                    findExistingRequests(userId, callingPackage));
@@ -165,11 +167,14 @@ public class AmbientContextManagerService extends
            mExistingClientRequests.add(
                    new ClientRequest(userId, request, callingPackage, observer));
        }
    }

    void clientRemoved(int userId, String packageName) {
        Slog.d(TAG, "Remove client: " + packageName);
        synchronized (mExistingClientRequests) {
            mExistingClientRequests.removeAll(findExistingRequests(userId, packageName));
        }
    }

    private Set<ClientRequest> findExistingRequests(int userId, String packageName) {
        Set<ClientRequest> existingRequests = new ArraySet<>();
@@ -183,11 +188,13 @@ public class AmbientContextManagerService extends

    @Nullable
    IAmbientContextObserver getClientRequestObserver(int userId, String packageName) {
        synchronized (mExistingClientRequests) {
            for (ClientRequest clientRequest : mExistingClientRequests) {
                if (clientRequest.hasUserIdAndPackageName(userId, packageName)) {
                    return clientRequest.getObserver();
                }
            }
        }
        return null;
    }