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

Commit 0cfaa0fb authored by Yeabkal Wubshit's avatar Yeabkal Wubshit
Browse files

Create HapticFeedbackRequest

This is a build-able class that allows to encapsulate different
components of a client's request to play a haptic feedback.

Bug: 397602072
Bug: 408393305
Test: atest HapticFeedbackRequestTest
Flag: android.os.vibrator.haptic_feedback_with_custom_usage
Change-Id: If58296b77c7f17f6b7607eb14f1a793d057e4139
parent a4e6d185
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -35597,6 +35597,20 @@ package android.os.strictmode {
package android.os.vibrator {
  @FlaggedApi("android.os.vibrator.haptic_feedback_with_custom_usage") public final class HapticFeedbackRequest {
    method public int getFeedbackConstant();
    method public int getFlags();
    method public int getUsage();
  }
  public static final class HapticFeedbackRequest.Builder {
    ctor public HapticFeedbackRequest.Builder(int);
    ctor public HapticFeedbackRequest.Builder(@NonNull android.os.vibrator.HapticFeedbackRequest);
    method @NonNull public android.os.vibrator.HapticFeedbackRequest build();
    method @NonNull public android.os.vibrator.HapticFeedbackRequest.Builder setFlags(int);
    method @NonNull public android.os.vibrator.HapticFeedbackRequest.Builder setUsage(int);
  }
  @FlaggedApi("android.os.vibrator.normalized_pwle_effects") public final class VibratorEnvelopeEffectInfo implements android.os.Parcelable {
    method public int describeContents();
    method public long getMaxControlPointDurationMillis();
+150 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.os.vibrator;

import static android.os.VibrationAttributes.USAGE_UNKNOWN;

import android.annotation.FlaggedApi;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.VibrationAttributes;
import android.view.HapticFeedbackConstants;

import java.util.Objects;

/**
 * Encapsulates a request to perform a haptic feedback.
 *
 * <p>Use {@link Builder} to create a new instance of this class.
 */
@FlaggedApi(Flags.FLAG_HAPTIC_FEEDBACK_WITH_CUSTOM_USAGE)
public final class HapticFeedbackRequest {
    private final int mFeedbackConstant;
    private final int mUsage;
    private final int mFlags;

    private HapticFeedbackRequest(
            int feedbackConstant,
            @VibrationAttributes.Usage int usage,
            @HapticFeedbackConstants.Flags int flags) {
        mFeedbackConstant = feedbackConstant;
        mUsage = usage;
        mFlags = flags;
    }

    /**
     * Returns the haptic feedback constant used to define the vibration effect to be played by this
     * request.
     *
     * @see HapticFeedbackConstants
     */
    public int getFeedbackConstant() {
        return mFeedbackConstant;
    }

    /**
     * Returns the {@link VibrationAttributes} usage for the haptic feedback request.
     *
     * @see VibrationAttributes#getUsage
     */
    @VibrationAttributes.Usage
    public int getUsage() {
        return mUsage;
    }

    /** Returns the {@link HapticFeedbackConstants} flags for the haptic feedback request. */
    @HapticFeedbackConstants.Flags
    public int getFlags() {
        return mFlags;
    }

    @Override
    public boolean equals(@Nullable Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        HapticFeedbackRequest rhs = (HapticFeedbackRequest) o;
        return mFeedbackConstant == rhs.mFeedbackConstant
                && mUsage == rhs.mUsage
                && mFlags == rhs.mFlags;
    }

    @Override
    public int hashCode() {
        return Objects.hash(mFeedbackConstant, mUsage, mFlags);
    }

    /** Builder for {@link HapticFeedbackRequest}. */
    public static final class Builder {
        private final int mFeedbackConstant;

        private int mUsage = USAGE_UNKNOWN;
        private int mFlags = 0x0;

        /**
         * Constructs a new builder for {@link HapticFeedbackRequest}.
         *
         * @param constant the haptic feedback constant for {@link HapticFeedbackRequest} that
         *      will be constructed from the builder. This needs to be one of the constants
         *      defined in {@link HapticFeedbackConstants}.
         */
        public Builder(int constant) {
            mFeedbackConstant = constant;
        }

        /**
         * Constructs a builder that is already populated with the fields from a given
         * {@link HapticFeedbackRequest}.
         *
         * @param request the request to create a new builder from.
         */
        public Builder(@NonNull HapticFeedbackRequest request) {
            Objects.requireNonNull(request);
            mFeedbackConstant = request.mFeedbackConstant;
            mUsage = request.mUsage;
            mFlags = request.mFlags;
        }

        /**
         * Sets the {@link VibrationAttributes} usage for the haptic feedback request.
         *
         * @see VibrationAttributes#getUsage
         */
        @NonNull
        public Builder setUsage(@VibrationAttributes.Usage int usage) {
            mUsage = usage;
            return this;
        }

        /** Sets the {@link HapticFeedbackConstants} flags for the haptic feedback request. */
        @NonNull
        public Builder setFlags(@HapticFeedbackConstants.Flags int flags) {
            mFlags = flags;
            return this;
        }

        /** Builds a new {@link HapticFeedbackRequest} from this builder object. */
        @NonNull
        public HapticFeedbackRequest build() {
            return new HapticFeedbackRequest(mFeedbackConstant, mUsage, mFlags);
        }
    }
}
+11 −0
Original line number Diff line number Diff line
@@ -176,3 +176,14 @@ flag {
      purpose: PURPOSE_FEATURE
    }
}

flag {
    namespace: "haptics"
    name: "haptic_feedback_with_custom_usage"
    description: "API to play haptic feedback with custom usage types."
    bug: "408393305"
    is_exported: true
    metadata {
      purpose: PURPOSE_FEATURE
    }
}