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

Commit 842ca2d8 authored by Arthur Ishiguro's avatar Arthur Ishiguro
Browse files

Avoid automatically closing of persistent ContextHubClients

Previously, we automatically closed the connection when the
resource was GC'd (process died), but for PendingIntent clients
we need to preserve the client endpoint.

Bug: 117612105
Test: Compile only
Change-Id: I6a6cffdeb980073b4ae5ad51c0d91df422e86fc4
parent 82ed3afe
Loading
Loading
Loading
Loading
+21 −5
Original line number Diff line number Diff line
@@ -48,14 +48,26 @@ public class ContextHubClient implements Closeable {
     */
    private final ContextHubInfo mAttachedHub;

    private final CloseGuard mCloseGuard = CloseGuard.get();
    private final CloseGuard mCloseGuard;

    private final AtomicBoolean mIsClosed = new AtomicBoolean(false);

    /* package */ ContextHubClient(ContextHubInfo hubInfo) {
    /*
     * True if this is a persistent client (i.e. does not have to close the connection when the
     * resource is freed from the system).
     */
    private final boolean mPersistent;

    /* package */ ContextHubClient(ContextHubInfo hubInfo, boolean persistent) {
        mAttachedHub = hubInfo;
        mPersistent = persistent;
        if (mPersistent) {
            mCloseGuard = null;
        } else {
            mCloseGuard = CloseGuard.get();
            mCloseGuard.open("close");
        }
    }

    /**
     * Sets the proxy interface of the client at the service. This method should always be called
@@ -96,7 +108,9 @@ public class ContextHubClient implements Closeable {
     */
    public void close() {
        if (!mIsClosed.getAndSet(true)) {
            if (mCloseGuard != null) {
                mCloseGuard.close();
            }
            try {
                mClientProxy.close();
            } catch (RemoteException e) {
@@ -138,7 +152,9 @@ public class ContextHubClient implements Closeable {
            if (mCloseGuard != null) {
                mCloseGuard.warnIfOpen();
            }
            if (!mPersistent) {
                close();
            }
        } finally {
            super.finalize();
        }
+2 −2
Original line number Diff line number Diff line
@@ -758,7 +758,7 @@ public final class ContextHubManager {
        Preconditions.checkNotNull(hubInfo, "ContextHubInfo cannot be null");
        Preconditions.checkNotNull(executor, "Executor cannot be null");

        ContextHubClient client = new ContextHubClient(hubInfo);
        ContextHubClient client = new ContextHubClient(hubInfo, false /* persistent */);
        IContextHubClientCallback clientInterface = createClientCallback(
                client, callback, executor);

@@ -837,7 +837,7 @@ public final class ContextHubManager {
        Preconditions.checkNotNull(pendingIntent);
        Preconditions.checkNotNull(hubInfo);

        ContextHubClient client = new ContextHubClient(hubInfo);
        ContextHubClient client = new ContextHubClient(hubInfo, true /* persistent */);

        IContextHubClient clientProxy;
        try {