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

Commit f1609bd7 authored by Tom Chan's avatar Tom Chan
Browse files

Update removeConnection API to throw instead of return boolean

Per API Council's request.

Bug: 376891743
Test: atest CtsWearableSensingServiceTestCases
Flag: android.app.wearable.enable_provide_wearable_connection_api
Change-Id: Ifc61a828350cde0221e05051c795f2beb52aecb4
parent 3daa7994
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -3399,7 +3399,7 @@ package android.app.wearable {
    method @FlaggedApi("android.app.wearable.enable_provide_read_only_pfd") @RequiresPermission(android.Manifest.permission.MANAGE_WEARABLE_SENSING_SERVICE) public void provideReadOnlyParcelFileDescriptor(@NonNull android.os.ParcelFileDescriptor, @NonNull android.os.PersistableBundle, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<java.lang.Integer>);
    method @RequiresPermission(android.Manifest.permission.MANAGE_WEARABLE_SENSING_SERVICE) public void registerDataRequestObserver(int, @NonNull android.app.PendingIntent, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<java.lang.Integer>);
    method @FlaggedApi("android.app.wearable.enable_concurrent_wearable_connections") @RequiresPermission(android.Manifest.permission.MANAGE_WEARABLE_SENSING_SERVICE) public void removeAllConnections();
    method @FlaggedApi("android.app.wearable.enable_concurrent_wearable_connections") @RequiresPermission(android.Manifest.permission.MANAGE_WEARABLE_SENSING_SERVICE) public boolean removeConnection(@NonNull android.app.wearable.WearableConnection);
    method @FlaggedApi("android.app.wearable.enable_concurrent_wearable_connections") @RequiresPermission(android.Manifest.permission.MANAGE_WEARABLE_SENSING_SERVICE) public void removeConnection(@NonNull android.app.wearable.WearableConnection);
    method @FlaggedApi("android.app.wearable.enable_hotword_wearable_sensing_api") @RequiresPermission(android.Manifest.permission.MANAGE_WEARABLE_SENSING_SERVICE) public void startHotwordRecognition(@Nullable android.content.ComponentName, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<java.lang.Integer>);
    method @FlaggedApi("android.app.wearable.enable_hotword_wearable_sensing_api") @RequiresPermission(android.Manifest.permission.MANAGE_WEARABLE_SENSING_SERVICE) public void stopHotwordRecognition(@NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<java.lang.Integer>);
    method @RequiresPermission(android.Manifest.permission.MANAGE_WEARABLE_SENSING_SERVICE) public void unregisterDataRequestObserver(int, @NonNull android.app.PendingIntent, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<java.lang.Integer>);
+18 −18
Original line number Diff line number Diff line
@@ -55,6 +55,7 @@ import java.io.OutputStream;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
@@ -348,9 +349,7 @@ public class WearableSensingManager {
                            wearableConnection.getMetadata(),
                            createWearableSensingCallback(executor),
                            statusCallback);
            if (connectionId != CONNECTION_ID_INVALID) {
            mWearableConnectionIdMap.put(wearableConnection, connectionId);
            }
            // For invalid connection IDs, the status callback will remove the connection from
            // mWearableConnectionIdMap
        } catch (RemoteException e) {
@@ -367,36 +366,37 @@ public class WearableSensingManager {
     * <p>After this method returns, there will be no new invocation to callback methods in the
     * removed {@link WearableConnection}. Ongoing invocations will continue to run.
     *
     * <p>This method does nothing if the provided {@code wearableConnection} does not match any
     * open connection.
     * <p>This method throws a {@link NoSuchElementException} if the provided {@code
     * wearableConnection} does not match any open connection.
     *
     * <p>This method should not be called before the corresponding {@link
     * #provideConnection(WearableConnection, Executor)} invocation returns. Otherwise, the
     * connection may not be removed.
     * connection may not be removed, and an {@link IllegalStateException} may be thrown.
     *
     * @param wearableConnection The WearableConnection instance previously provided to {@link
     *     #provideConnection(WearableConnection, Executor)}.
     * @return true if a concurrent connection quota has been freed due to this method invocation.
     *     Returns false otherwise.
     * @throws NoSuchElementException if the connection was never provided or was already removed.
     * @throws IllegalStateException if the {@link #provideConnection(WearableConnection, Executor)}
     *     invocation for the given connection has not returned.
     */
    @RequiresPermission(Manifest.permission.MANAGE_WEARABLE_SENSING_SERVICE)
    @FlaggedApi(Flags.FLAG_ENABLE_CONCURRENT_WEARABLE_CONNECTIONS)
    public boolean removeConnection(@NonNull WearableConnection wearableConnection) {
        int connectionId =
                mWearableConnectionIdMap.getOrDefault(wearableConnection, CONNECTION_ID_INVALID);
        if (connectionId == CONNECTION_ID_INVALID) {
            return false;
    public void removeConnection(@NonNull WearableConnection wearableConnection) {
        Integer connectionId = mWearableConnectionIdMap.remove(wearableConnection);
        if (connectionId == null || connectionId == CONNECTION_ID_INVALID) {
            throw new NoSuchElementException(
                    "The provided connection was never provided or was already removed.");
        }
        if (connectionId == CONNECTION_ID_PLACEHOLDER) {
            Slog.w(
                    TAG,
            throw new IllegalStateException(
                    "Attempt to remove connection before provideConnection returns. The connection"
                            + " will not be removed.");
            return false;
        }
        mWearableConnectionIdMap.remove(wearableConnection);
        try {
            return mService.removeConnection(connectionId);
            if (!mService.removeConnection(connectionId)) {
                throw new NoSuchElementException(
                        "The provided connection was never provided or was already removed.");
            }
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }