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

Commit 16985ae8 authored by Yifei Zhang's avatar Yifei Zhang
Browse files

contexthub: add HubEndpointSessionResult to allow accept/reject open session request

- HubEndpointSessionResult.accept() and reject(String) for results
- Call service.closeSession if rejected

Test: manual
API-Coverage-Bug: 377554469
Bug: 375487784
Flag: android.chre.flags.offload_api
Change-Id: I24065c9111cf3c321dec5f138e1487781c7ab8f0
parent 0fcefc1d
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -5204,8 +5204,16 @@ package android.hardware.contexthub {
    method public void close();
  }
  @FlaggedApi("android.chre.flags.offload_api") public class HubEndpointSessionResult {
    method @NonNull public static android.hardware.contexthub.HubEndpointSessionResult accept();
    method @Nullable public String getReason();
    method public boolean isAccepted();
    method @NonNull public static android.hardware.contexthub.HubEndpointSessionResult reject(@NonNull String);
  }
  @FlaggedApi("android.chre.flags.offload_api") public interface IHubEndpointLifecycleCallback {
    method public void onSessionClosed(@NonNull android.hardware.contexthub.HubEndpointSession, int);
    method @NonNull public android.hardware.contexthub.HubEndpointSessionResult onSessionOpenRequest(@NonNull android.hardware.contexthub.HubEndpointInfo);
    method public void onSessionOpened(@NonNull android.hardware.contexthub.HubEndpointSession);
    field public static final int REASON_CLOSE_ENDPOINT_SESSION_REQUESTED = 4; // 0x4
    field public static final int REASON_OPEN_ENDPOINT_SESSION_REQUEST_REJECTED = 3; // 0x3
+48 −7
Original line number Diff line number Diff line
@@ -69,15 +69,40 @@ public class HubEndpoint {
                        }
                    }

                    if (mLifecycleCallback != null) {
                        mLifecycleCallbackExecutor.execute(
                            () -> {
                                // TODO(b/375487784): create API and connect for this
                                onSessionOpenRequestResult(sessionId, initiator);
                            });
                                () ->
                                        processSessionOpenRequestResult(
                                                sessionId,
                                                initiator,
                                                mLifecycleCallback.onSessionOpenRequest(
                                                        initiator)));
                    }
                }

                private void processSessionOpenRequestResult(
                        int sessionId, HubEndpointInfo initiator, HubEndpointSessionResult result) {
                    if (result == null) {
                        throw new IllegalArgumentException(
                                "HubEndpointSessionResult shouldn't be null.");
                    }

                    if (result.isAccepted()) {
                        acceptSession(sessionId, initiator);
                    } else {
                        Log.i(
                                TAG,
                                "Session "
                                        + sessionId
                                        + " from "
                                        + initiator
                                        + " was rejected, reason="
                                        + result.getReason());
                        rejectSession(sessionId);
                    }
                }

                // TODO(b/375487784): Process the result, always accept for now
                private void onSessionOpenRequestResult(int sessionId, HubEndpointInfo initiator) {
                private void acceptSession(int sessionId, HubEndpointInfo initiator) {
                    if (mServiceToken == null || mAssignedHubEndpointInfo == null) {
                        // No longer registered?
                        return;
@@ -122,6 +147,22 @@ public class HubEndpoint {
                    }
                }

                private void rejectSession(int sessionId) {
                    if (mServiceToken == null || mAssignedHubEndpointInfo == null) {
                        // No longer registered?
                        return;
                    }

                    try {
                        mServiceToken.closeSession(
                                sessionId,
                                IHubEndpointLifecycleCallback
                                        .REASON_OPEN_ENDPOINT_SESSION_REQUEST_REJECTED);
                    } catch (RemoteException e) {
                        e.rethrowFromSystemServer();
                    }
                }

                @Override
                public void onSessionOpenComplete(int sessionId) throws RemoteException {
                    final HubEndpointSession activeSession;
+79 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.hardware.contexthub;

import android.annotation.FlaggedApi;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.chre.flags.Flags;

/**
 * Return type of {@link IHubEndpointLifecycleCallback#onSessionOpenRequest}. The value determines
 * whether a open session request from the remote is accepted or not.
 *
 * @hide
 */
@SystemApi
@FlaggedApi(Flags.FLAG_OFFLOAD_API)
public class HubEndpointSessionResult {
    private final boolean mAccepted;

    @Nullable private final String mReason;

    private HubEndpointSessionResult(boolean accepted, @Nullable String reason) {
        mAccepted = accepted;
        mReason = reason;
    }

    /**
     * Retrieve the decision of the session request.
     *
     * @return Whether a session request was accepted or not, previously set with {@link #accept()}
     *     or {@link #reject(String)}.
     */
    public boolean isAccepted() {
        return mAccepted;
    }

    /**
     * Retrieve the decision of the session request.
     *
     * @return The reason previously set in {@link #reject(String)}. If the result was {@link
     *     #accept()}, the reason will be null.
     */
    @Nullable
    public String getReason() {
        return mReason;
    }

    /** Accept the request. */
    @NonNull
    public static HubEndpointSessionResult accept() {
        return new HubEndpointSessionResult(true, null);
    }

    /**
     * Reject the request with a reason.
     *
     * @param reason Reason why the request was rejected, for diagnostic purposes.
     */
    @NonNull
    public static HubEndpointSessionResult reject(@NonNull String reason) {
        return new HubEndpointSessionResult(false, reason);
    }
}
+8 −0
Original line number Diff line number Diff line
@@ -51,6 +51,14 @@ public interface IHubEndpointLifecycleCallback {
    })
    @interface EndpointLifecycleReason {}

    /**
     * Called when an endpoint is requesting a session be opened with another endpoint.
     *
     * @param requester The {@link HubEndpointInfo} object representing the requester
     */
    @NonNull
    HubEndpointSessionResult onSessionOpenRequest(@NonNull HubEndpointInfo requester);

    /**
     * Called when a communication session is opened and ready to be used.
     *