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

Commit 54da7075 authored by Arthur Ishiguro's avatar Arthur Ishiguro
Browse files

Close active sessions on endpoint unregistrations

Bug: 383747125
Flag: android.chre.flags.offload_implementation
Test: atest FrameworksServicesTests_contexthub_presubmit pass
Change-Id: Ib62b350ae7c8386a8c9383f2afb2cef2a2bace0a
parent 32b1bbc4
Loading
Loading
Loading
Loading
+8 −7
Original line number Diff line number Diff line
@@ -237,6 +237,14 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
    @android.annotation.EnforcePermission(android.Manifest.permission.ACCESS_CONTEXT_HUB)
    public void unregister() {
        super.unregister_enforcePermission();
        synchronized (mOpenSessionLock) {
            // Iterate in reverse since cleanupSessionResources will remove the entry
            for (int i = mSessionInfoMap.size() - 1; i >= 0; i--) {
                int id = mSessionInfoMap.keyAt(i);
                halCloseEndpointSessionNoThrow(id, Reason.ENDPOINT_GONE);
                cleanupSessionResources(id);
            }
        }
        synchronized (mRegistrationLock) {
            if (!isRegistered()) {
                Log.w(TAG, "Attempting to unregister when already unregistered");
@@ -249,13 +257,6 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
                Log.e(TAG, "RemoteException while calling HAL unregisterEndpoint", e);
            }
        }
        synchronized (mOpenSessionLock) {
            // Iterate in reverse since cleanupSessionResources will remove the entry
            for (int i = mSessionInfoMap.size() - 1; i >= 0; i--) {
                int id = mSessionInfoMap.keyAt(i);
                cleanupSessionResources(id);
            }
        }
        mEndpointManager.unregisterEndpoint(mEndpointInfo.getIdentifier().getEndpoint());
        releaseWakeLockOnExit();
    }
+20 −0
Original line number Diff line number Diff line
@@ -186,6 +186,26 @@ public class ContextHubEndpointTest {
        assertThat(mEndpointManager.getNumAvailableSessions()).isEqualTo(SESSION_ID_RANGE);
    }

    @Test
    public void testOpenSessionOnUnregistration() throws RemoteException {
        assertThat(mEndpointManager.getNumAvailableSessions()).isEqualTo(SESSION_ID_RANGE);
        IContextHubEndpoint endpoint = registerExampleEndpoint();

        HubEndpointInfo targetInfo =
                new HubEndpointInfo(
                        TARGET_ENDPOINT_NAME,
                        TARGET_ENDPOINT_ID,
                        ENDPOINT_PACKAGE_NAME,
                        Collections.emptyList());
        int sessionId = endpoint.openSession(targetInfo, /* serviceDescriptor= */ null);
        mEndpointManager.onEndpointSessionOpenComplete(sessionId);
        assertThat(mEndpointManager.getNumAvailableSessions()).isEqualTo(SESSION_ID_RANGE - 1);

        unregisterExampleEndpoint(endpoint);
        verify(mMockEndpointCommunications).closeEndpointSession(sessionId, Reason.ENDPOINT_GONE);
        assertThat(mEndpointManager.getNumAvailableSessions()).isEqualTo(SESSION_ID_RANGE);
    }

    private IContextHubEndpoint registerExampleEndpoint() throws RemoteException {
        HubEndpointInfo info =
                new HubEndpointInfo(