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

Commit 461a62b3 authored by lpeter's avatar lpeter
Browse files

Validate the HotwordDetectedResult

Implementation as below:
1.Implemet the getMaxBundleSize()
2.Validate if the bundle data of setExtras() is more than
getMaxBundleSize()
3.Validate if the value of setHotwordPhraseId() is between 0
and getMaxHotwordPhraseId()
3.Validate if the value of setScore() is between 0
and getMaxScore()
4.Validate if the value of setPersonalizedScore() is between 0
and getMaxScore()
5.Validate if the data of HotwordDetectedResult is more than 100 bytes

Bug: 190688148
Test: atest CtsVoiceInteractionTestCases
Test: atest CtsVoiceInteractionTestCases --instant
Change-Id: Ie23e8a831053f14ff0ea795fef356508b9fe126e
parent 1a49d4f7
Loading
Loading
Loading
Loading
+55 −16
Original line number Diff line number Diff line
@@ -20,11 +20,18 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.content.res.Resources;
import android.media.AudioRecord;
import android.media.MediaSyncEvent;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.PersistableBundle;

import com.android.internal.R;
import com.android.internal.util.DataClass;
import com.android.internal.util.Preconditions;

import java.util.Objects;

/**
 * Represents a result supporting the hotword detection.
@@ -187,16 +194,20 @@ public final class HotwordDetectedResult implements Parcelable {
        return new PersistableBundle();
    }

    private static int sMaxBundleSize = -1;

    /**
     * Returns the maximum byte size of the information contained in the bundle.
     *
     * <p>The total size will be calculated as a sum of byte sizes over all bundle keys.
     *
     * <p>For example, for a bundle containing a single key: {@code "example_key" -> 42.0f}, the
     * bundle size will be {@code 11 + Float.BYTES = 15} bytes.
     * <p>The total size will be calculated by how much bundle data should be written into the
     * Parcel.
     */
    public static int getMaxBundleSize() {
        return 50;
        if (sMaxBundleSize < 0) {
            sMaxBundleSize = Resources.getSystem().getInteger(
                    R.integer.config_hotwordDetectedResultMaxBundleSize);
        }
        return sMaxBundleSize;
    }

    /**
@@ -212,6 +223,34 @@ public final class HotwordDetectedResult implements Parcelable {
        return mMediaSyncEvent;
    }

    /**
     * Returns how many bytes should be written into the Parcel
     *
     * @hide
     */
    public static int getParcelableSize(@NonNull Parcelable parcelable) {
        final Parcel p = Parcel.obtain();
        parcelable.writeToParcel(p, 0);
        p.setDataPosition(0);
        final int size = p.dataSize();
        p.recycle();
        return size;
    }

    private void onConstructed() {
        Preconditions.checkArgumentInRange(mScore, 0, getMaxScore(), "score");
        Preconditions.checkArgumentInRange(mPersonalizedScore, 0, getMaxScore(),
                "personalizedScore");
        Preconditions.checkArgumentInRange(mHotwordPhraseId, 0, getMaxHotwordPhraseId(),
                "hotwordPhraseId");
        Preconditions.checkArgumentInRange((long) mHotwordDurationMillis, 0,
                AudioRecord.getMaxSharedAudioHistoryMillis(), "hotwordDurationMillis");
        if (!mExtras.isEmpty()) {
            Preconditions.checkArgumentInRange(getParcelableSize(mExtras), 0, getMaxBundleSize(),
                    "extras");
        }
    }



    // Code below generated by codegen v1.0.23.
@@ -290,7 +329,7 @@ public final class HotwordDetectedResult implements Parcelable {
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, mExtras);

        // onConstructed(); // You can define this method to get a callback
        onConstructed();
    }

    /**
@@ -422,7 +461,7 @@ public final class HotwordDetectedResult implements Parcelable {
        //noinspection PointlessBooleanExpression
        return true
                && mConfidenceLevel == that.mConfidenceLevel
                && java.util.Objects.equals(mMediaSyncEvent, that.mMediaSyncEvent)
                && Objects.equals(mMediaSyncEvent, that.mMediaSyncEvent)
                && mHotwordOffsetMillis == that.mHotwordOffsetMillis
                && mHotwordDurationMillis == that.mHotwordDurationMillis
                && mAudioChannel == that.mAudioChannel
@@ -430,7 +469,7 @@ public final class HotwordDetectedResult implements Parcelable {
                && mScore == that.mScore
                && mPersonalizedScore == that.mPersonalizedScore
                && mHotwordPhraseId == that.mHotwordPhraseId
                && java.util.Objects.equals(mExtras, that.mExtras);
                && Objects.equals(mExtras, that.mExtras);
    }

    @Override
@@ -441,7 +480,7 @@ public final class HotwordDetectedResult implements Parcelable {

        int _hash = 1;
        _hash = 31 * _hash + mConfidenceLevel;
        _hash = 31 * _hash + java.util.Objects.hashCode(mMediaSyncEvent);
        _hash = 31 * _hash + Objects.hashCode(mMediaSyncEvent);
        _hash = 31 * _hash + mHotwordOffsetMillis;
        _hash = 31 * _hash + mHotwordDurationMillis;
        _hash = 31 * _hash + mAudioChannel;
@@ -449,13 +488,13 @@ public final class HotwordDetectedResult implements Parcelable {
        _hash = 31 * _hash + mScore;
        _hash = 31 * _hash + mPersonalizedScore;
        _hash = 31 * _hash + mHotwordPhraseId;
        _hash = 31 * _hash + java.util.Objects.hashCode(mExtras);
        _hash = 31 * _hash + Objects.hashCode(mExtras);
        return _hash;
    }

    @Override
    @DataClass.Generated.Member
    public void writeToParcel(@NonNull android.os.Parcel dest, int flags) {
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        // You can override field parcelling by defining methods like:
        // void parcelFieldName(Parcel dest, int flags) { ... }

@@ -481,7 +520,7 @@ public final class HotwordDetectedResult implements Parcelable {
    /** @hide */
    @SuppressWarnings({"unchecked", "RedundantCast"})
    @DataClass.Generated.Member
    /* package-private */ HotwordDetectedResult(@NonNull android.os.Parcel in) {
    /* package-private */ HotwordDetectedResult(@NonNull Parcel in) {
        // You can override field unparcelling by defining methods like:
        // static FieldType unparcelFieldName(Parcel in) { ... }

@@ -512,7 +551,7 @@ public final class HotwordDetectedResult implements Parcelable {
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, mExtras);

        // onConstructed(); // You can define this method to get a callback
        onConstructed();
    }

    @DataClass.Generated.Member
@@ -524,7 +563,7 @@ public final class HotwordDetectedResult implements Parcelable {
        }

        @Override
        public HotwordDetectedResult createFromParcel(@NonNull android.os.Parcel in) {
        public HotwordDetectedResult createFromParcel(@NonNull Parcel in) {
            return new HotwordDetectedResult(in);
        }
    };
@@ -745,10 +784,10 @@ public final class HotwordDetectedResult implements Parcelable {
    }

    @DataClass.Generated(
            time = 1621943150502L,
            time = 1624361647985L,
            codegenVersion = "1.0.23",
            sourceFile = "frameworks/base/core/java/android/service/voice/HotwordDetectedResult.java",
            inputSignatures = "public static final  int CONFIDENCE_LEVEL_NONE\npublic static final  int CONFIDENCE_LEVEL_LOW\npublic static final  int CONFIDENCE_LEVEL_LOW_MEDIUM\npublic static final  int CONFIDENCE_LEVEL_MEDIUM\npublic static final  int CONFIDENCE_LEVEL_MEDIUM_HIGH\npublic static final  int CONFIDENCE_LEVEL_HIGH\npublic static final  int CONFIDENCE_LEVEL_VERY_HIGH\npublic static final  int HOTWORD_OFFSET_UNSET\npublic static final  int AUDIO_CHANNEL_UNSET\nprivate final @android.service.voice.HotwordDetectedResult.HotwordConfidenceLevelValue int mConfidenceLevel\nprivate @android.annotation.Nullable android.media.MediaSyncEvent mMediaSyncEvent\nprivate  int mHotwordOffsetMillis\nprivate  int mHotwordDurationMillis\nprivate  int mAudioChannel\nprivate  boolean mHotwordDetectionPersonalized\nprivate final  int mScore\nprivate final  int mPersonalizedScore\nprivate final  int mHotwordPhraseId\nprivate final @android.annotation.NonNull android.os.PersistableBundle mExtras\nprivate static  int defaultConfidenceLevel()\nprivate static  int defaultScore()\nprivate static  int defaultPersonalizedScore()\npublic static  int getMaxScore()\nprivate static  int defaultHotwordPhraseId()\npublic static  int getMaxHotwordPhraseId()\nprivate static  android.os.PersistableBundle defaultExtras()\npublic static  int getMaxBundleSize()\npublic @android.annotation.Nullable android.media.MediaSyncEvent getMediaSyncEvent()\nclass HotwordDetectedResult extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genConstructor=false, genBuilder=true, genEqualsHashCode=true, genHiddenConstDefs=true, genParcelable=true, genToString=true)")
            inputSignatures = "public static final  int CONFIDENCE_LEVEL_NONE\npublic static final  int CONFIDENCE_LEVEL_LOW\npublic static final  int CONFIDENCE_LEVEL_LOW_MEDIUM\npublic static final  int CONFIDENCE_LEVEL_MEDIUM\npublic static final  int CONFIDENCE_LEVEL_MEDIUM_HIGH\npublic static final  int CONFIDENCE_LEVEL_HIGH\npublic static final  int CONFIDENCE_LEVEL_VERY_HIGH\npublic static final  int HOTWORD_OFFSET_UNSET\npublic static final  int AUDIO_CHANNEL_UNSET\nprivate final @android.service.voice.HotwordDetectedResult.HotwordConfidenceLevelValue int mConfidenceLevel\nprivate @android.annotation.Nullable android.media.MediaSyncEvent mMediaSyncEvent\nprivate  int mHotwordOffsetMillis\nprivate  int mHotwordDurationMillis\nprivate  int mAudioChannel\nprivate  boolean mHotwordDetectionPersonalized\nprivate final  int mScore\nprivate final  int mPersonalizedScore\nprivate final  int mHotwordPhraseId\nprivate final @android.annotation.NonNull android.os.PersistableBundle mExtras\nprivate static  int sMaxBundleSize\nprivate static  int defaultConfidenceLevel()\nprivate static  int defaultScore()\nprivate static  int defaultPersonalizedScore()\npublic static  int getMaxScore()\nprivate static  int defaultHotwordPhraseId()\npublic static  int getMaxHotwordPhraseId()\nprivate static  android.os.PersistableBundle defaultExtras()\npublic static  int getMaxBundleSize()\npublic @android.annotation.Nullable android.media.MediaSyncEvent getMediaSyncEvent()\npublic static  int getParcelableSize(android.os.Parcelable)\nprivate  void onConstructed()\nclass HotwordDetectedResult extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genConstructor=false, genBuilder=true, genEqualsHashCode=true, genHiddenConstDefs=true, genParcelable=true, genToString=true)")
    @Deprecated
    private void __metadata() {}

+9 −0
Original line number Diff line number Diff line
@@ -75,6 +75,7 @@ public abstract class HotwordDetectionService extends Service {
    private static final boolean DBG = true;

    private static final long UPDATE_TIMEOUT_MILLIS = 5000;

    /** @hide */
    public static final String KEY_INITIALIZATION_STATUS = "initialization_status";

@@ -384,6 +385,14 @@ public abstract class HotwordDetectionService extends Service {
         */
        public void onDetected(@NonNull HotwordDetectedResult result) {
            requireNonNull(result);
            final PersistableBundle persistableBundle = result.getExtras();
            if (!persistableBundle.isEmpty() && HotwordDetectedResult.getParcelableSize(
                    persistableBundle) > HotwordDetectedResult.getMaxBundleSize()) {
                throw new IllegalArgumentException(
                        "The bundle size of result is larger than max bundle size ("
                                + HotwordDetectedResult.getMaxBundleSize()
                                + ") of HotwordDetectedResult");
            }
            try {
                mRemoteCallback.onDetected(result);
            } catch (RemoteException e) {
+4 −0
Original line number Diff line number Diff line
@@ -4985,6 +4985,10 @@
    <!-- Default value for Settings.ASSIST_TOUCH_GESTURE_ENABLED -->
    <bool name="config_assistTouchGestureEnabledDefault">true</bool>

    <!-- The maximum byte size of the information contained in the bundle of
    HotwordDetectedResult. -->
    <integer translatable="false" name="config_hotwordDetectedResultMaxBundleSize">0</integer>

    <!-- The amount of dimming to apply to wallpapers with mid range luminance. 0 displays
         the wallpaper at full brightness. 1 displays the wallpaper as fully black. -->
    <item name="config_wallpaperDimAmount" format="float" type="dimen">0.05</item>
+2 −0
Original line number Diff line number Diff line
@@ -4397,5 +4397,7 @@
  <java-symbol type="bool" name="config_assistLongPressHomeEnabledDefault" />
  <java-symbol type="bool" name="config_assistTouchGestureEnabledDefault" />

  <java-symbol type="integer" name="config_hotwordDetectedResultMaxBundleSize" />

  <java-symbol type="dimen" name="config_wallpaperDimAmount" />
</resources>