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

Commit 7b21fb6b authored by Lili Zou's avatar Lili Zou Committed by Android (Google) Code Review
Browse files

Merge "AmbientContextManager API changes to support quick-tap detection" into tm-qpr-dev

parents 1ae9003c 81f52b05
Loading
Loading
Loading
Loading
+47 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.app.ambientcontext;

import android.annotation.NonNull;

import java.util.List;
import java.util.concurrent.Executor;

/**
 * Callback for listening to Ambient Context events and status changes. See {@link
 * AmbientContextManager#registerObserver(AmbientContextEventRequest, AmbientContextCallback,
 * Executor)}
 *
 * @hide
 */
public interface AmbientContextCallback {
    /**
     * Called when AmbientContextManager service detects events.
     *
     * @param events a list of detected events.
     */
    void onEvents(@NonNull List<AmbientContextEvent> events);

    /**
     * Called with a statusCode when
     * {@link AmbientContextManager#registerObserver(AmbientContextEventRequest,
     * Executor, AmbientContextCallback)} completes, to indicate if the registration is successful
     *
     * @param statusCode the status of the service.
     */
    void onRegistrationComplete(@NonNull @AmbientContextManager.StatusCode int statusCode);
}
+16 −3
Original line number Diff line number Diff line
@@ -59,11 +59,21 @@ public final class AmbientContextEvent implements Parcelable {
     */
    public static final int EVENT_SNORE = 2;

    /**
     * The integer indicating a double-tap event was detected.
     * For detecting this event type, there's no specific consent activity to request access, but
     * the consent is implied through the double tap toggle in the Settings app.
     *
     * @hide
     */
    public static final int EVENT_BACK_DOUBLE_TAP = 3;

    /** @hide */
    @IntDef(prefix = { "EVENT_" }, value = {
            EVENT_UNKNOWN,
            EVENT_COUGH,
            EVENT_SNORE,
            EVENT_BACK_DOUBLE_TAP,
    }) public @interface EventCode {}

    /** The integer indicating an unknown level. */
@@ -150,7 +160,8 @@ public final class AmbientContextEvent implements Parcelable {
    @IntDef(prefix = "EVENT_", value = {
        EVENT_UNKNOWN,
        EVENT_COUGH,
        EVENT_SNORE
        EVENT_SNORE,
        EVENT_BACK_DOUBLE_TAP
    })
    @Retention(RetentionPolicy.SOURCE)
    @DataClass.Generated.Member
@@ -166,6 +177,8 @@ public final class AmbientContextEvent implements Parcelable {
                    return "EVENT_COUGH";
            case EVENT_SNORE:
                    return "EVENT_SNORE";
            case EVENT_BACK_DOUBLE_TAP:
                    return "EVENT_BACK_DOUBLE_TAP";
            default: return Integer.toHexString(value);
        }
    }
@@ -478,10 +491,10 @@ public final class AmbientContextEvent implements Parcelable {
    }

    @DataClass.Generated(
            time = 1642040319323L,
            time = 1659950304931L,
            codegenVersion = "1.0.23",
            sourceFile = "frameworks/base/core/java/android/app/ambientcontext/AmbientContextEvent.java",
            inputSignatures = "public static final  int EVENT_UNKNOWN\npublic static final  int EVENT_COUGH\npublic static final  int EVENT_SNORE\npublic static final  int LEVEL_UNKNOWN\npublic static final  int LEVEL_LOW\npublic static final  int LEVEL_MEDIUM_LOW\npublic static final  int LEVEL_MEDIUM\npublic static final  int LEVEL_MEDIUM_HIGH\npublic static final  int LEVEL_HIGH\nprivate final @android.app.ambientcontext.AmbientContextEvent.EventCode int mEventType\nprivate final @com.android.internal.util.DataClass.ParcelWith(com.android.internal.util.Parcelling.BuiltIn.ForInstant.class) @android.annotation.NonNull java.time.Instant mStartTime\nprivate final @com.android.internal.util.DataClass.ParcelWith(com.android.internal.util.Parcelling.BuiltIn.ForInstant.class) @android.annotation.NonNull java.time.Instant mEndTime\nprivate final @android.app.ambientcontext.AmbientContextEvent.LevelValue int mConfidenceLevel\nprivate final @android.app.ambientcontext.AmbientContextEvent.LevelValue int mDensityLevel\nprivate static  int defaultEventType()\nprivate static @android.annotation.NonNull java.time.Instant defaultStartTime()\nprivate static @android.annotation.NonNull java.time.Instant defaultEndTime()\nprivate static  int defaultConfidenceLevel()\nprivate static  int defaultDensityLevel()\nclass AmbientContextEvent extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genBuilder=true, genConstructor=false, genHiddenConstDefs=true, genParcelable=true, genToString=true)")
            inputSignatures = "public static final  int EVENT_UNKNOWN\npublic static final  int EVENT_COUGH\npublic static final  int EVENT_SNORE\npublic static final  int EVENT_BACK_DOUBLE_TAP\npublic static final  int LEVEL_UNKNOWN\npublic static final  int LEVEL_LOW\npublic static final  int LEVEL_MEDIUM_LOW\npublic static final  int LEVEL_MEDIUM\npublic static final  int LEVEL_MEDIUM_HIGH\npublic static final  int LEVEL_HIGH\nprivate final @android.app.ambientcontext.AmbientContextEvent.EventCode int mEventType\nprivate final @com.android.internal.util.DataClass.ParcelWith(com.android.internal.util.Parcelling.BuiltIn.ForInstant.class) @android.annotation.NonNull java.time.Instant mStartTime\nprivate final @com.android.internal.util.DataClass.ParcelWith(com.android.internal.util.Parcelling.BuiltIn.ForInstant.class) @android.annotation.NonNull java.time.Instant mEndTime\nprivate final @android.app.ambientcontext.AmbientContextEvent.LevelValue int mConfidenceLevel\nprivate final @android.app.ambientcontext.AmbientContextEvent.LevelValue int mDensityLevel\nprivate static  int defaultEventType()\nprivate static @android.annotation.NonNull java.time.Instant defaultStartTime()\nprivate static @android.annotation.NonNull java.time.Instant defaultEndTime()\nprivate static  int defaultConfidenceLevel()\nprivate static  int defaultDensityLevel()\nclass AmbientContextEvent extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genBuilder=true, genConstructor=false, genHiddenConstDefs=true, genParcelable=true, genToString=true)")
    @Deprecated
    private void __metadata() {}

+67 −1
Original line number Diff line number Diff line
@@ -296,6 +296,72 @@ public final class AmbientContextManager {
        }
    }

    /**
     * Allows app to register as a {@link AmbientContextEvent} observer. Same as {@link
     * #registerObserver(AmbientContextEventRequest, PendingIntent, Executor, Consumer)},
     * but use {@link AmbientContextCallback} instead of {@link PendingIntent} as a callback on
     * detected events.
     * Registering another observer from the same package that has already been
     * registered will override the previous observer. If the same app previously calls
     * {@link #registerObserver(AmbientContextEventRequest, AmbientContextCallback, Executor)},
     * and now calls
     * {@link #registerObserver(AmbientContextEventRequest, PendingIntent, Executor, Consumer)},
     * the previous observer will be replaced with the new observer with the PendingIntent callback.
     * Or vice versa.
     *
     * When the registration completes, a status will be returned to client through
     * {@link AmbientContextCallback#onRegistrationComplete(int)}.
     * If the AmbientContextManager service is not enabled yet, or the underlying detection service
     * is not running yet, {@link AmbientContextManager#STATUS_SERVICE_UNAVAILABLE} will be
     * returned, and the detection won't be really started.
     * If the underlying detection service feature is not enabled, or the requested event type is
     * not enabled yet, {@link AmbientContextManager#STATUS_NOT_SUPPORTED} will be returned, and the
     * detection won't be really started.
     * If there is no user consent,  {@link AmbientContextManager#STATUS_ACCESS_DENIED} will be
     * returned, and the detection won't be really started.
     * Otherwise, it will try to start the detection. And if it starts successfully, it will return
     * {@link AmbientContextManager#STATUS_SUCCESS}. If it fails to start the detection, then
     * it will return {@link AmbientContextManager#STATUS_SERVICE_UNAVAILABLE}
     * After registerObserver succeeds and when the service detects an event, the service will
     * trigger {@link AmbientContextCallback#onEvents(List)}.
     *
     * @hide
     */
    @RequiresPermission(Manifest.permission.ACCESS_AMBIENT_CONTEXT_EVENT)
    public void registerObserver(
            @NonNull AmbientContextEventRequest request,
            @NonNull @CallbackExecutor Executor executor,
            @NonNull AmbientContextCallback ambientContextCallback) {
        try {
            IAmbientContextObserver observer = new IAmbientContextObserver.Stub() {
                @Override
                public void onEvents(List<AmbientContextEvent> events) throws RemoteException {
                    final long identity = Binder.clearCallingIdentity();
                    try {
                        executor.execute(() -> ambientContextCallback.onEvents(events));
                    } finally {
                        Binder.restoreCallingIdentity(identity);
                    }
                }

                @Override
                public void onRegistrationComplete(int statusCode) throws RemoteException {
                    final long identity = Binder.clearCallingIdentity();
                    try {
                        executor.execute(
                                () -> ambientContextCallback.onRegistrationComplete(statusCode));
                    } finally {
                        Binder.restoreCallingIdentity(identity);
                    }
                }
            };

            mService.registerObserverWithCallback(request, mContext.getPackageName(), observer);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Unregisters the requesting app as an {@code AmbientContextEvent} observer. Unregistering an
     * observer that was already unregistered or never registered will have no effect.
+6 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.app.ambientcontext;

import android.app.PendingIntent;
import android.app.ambientcontext.AmbientContextEventRequest;
import android.app.ambientcontext.IAmbientContextObserver;
import android.os.RemoteCallback;

/**
@@ -29,6 +30,11 @@ interface IAmbientContextManager {
    void registerObserver(in AmbientContextEventRequest request,
        in PendingIntent resultPendingIntent,
        in RemoteCallback statusCallback);

    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.ACCESS_AMBIENT_CONTEXT_EVENT)")
    void registerObserverWithCallback(in AmbientContextEventRequest request,
        String packageName,
        in IAmbientContextObserver observer);
    void unregisterObserver(in String callingPackage);
    void queryServiceStatus(in int[] eventTypes, in String callingPackage,
        in RemoteCallback statusCallback);
+30 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.app.ambientcontext;

import android.app.ambientcontext.AmbientContextEvent;

/**
 * Callback interface of AmbientContextManager.
 *
 * @hide
 */
oneway interface IAmbientContextObserver {
  void onEvents(in List<AmbientContextEvent> events);
  void onRegistrationComplete(in int statusCode);
}
Loading