1  /*
2   * Copyright (C) 2023 The Android Open Source Project
3   *
4   * Licensed under the Apache License, Version 2.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   *      http://www.apache.org/licenses/LICENSE-2.0
9   *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package android.speech;
18  
19  import android.annotation.NonNull;
20  import android.annotation.Nullable;
21  import android.os.Parcelable;
22  
23  import com.android.internal.util.DataClass;
24  import com.android.internal.util.Preconditions;
25  
26  /**
27   * Info about a single recognition part.
28   *
29   * <p> A recognition part represents a recognized word or character, as well as any potential
30   * adjacent punctuation, that is returned by the {@link SpeechRecognizer}.
31   *
32   * <p> Each recognition part is described with a {@link String} denoting the raw text.
33   * Additionally, if formatting is enabled with {@link RecognizerIntent#EXTRA_ENABLE_FORMATTING},
34   * another {@link String} representation exists denoting the formatted text.
35   *
36   * <p> If the timestamps are requested with {@link RecognizerIntent#EXTRA_REQUEST_WORD_TIMING}, each
37   * recognition part will contain a value representing the offset of the beginning of this part from
38   * the start of the recognition session in milliseconds.
39   *
40   * <p> If the confidence levels are requested with
41   * {@link RecognizerIntent#EXTRA_REQUEST_WORD_CONFIDENCE}, each recognition part will contain
42   * a value describing the level of recognition confidence.
43   */
44  @DataClass(
45          genBuilder = true,
46          genEqualsHashCode = true,
47          genHiddenConstDefs = true,
48          genToString = true)
49  public final class RecognitionPart implements Parcelable {
50  
51      /** Confidence level not requested. */
52      public static final int CONFIDENCE_LEVEL_UNKNOWN = 0;
53  
54      /** Lowest level of confidence out of five levels. */
55      public static final int CONFIDENCE_LEVEL_LOW = 1;
56  
57      /** Second-lowest level of confidence out of five levels. */
58      public static final int CONFIDENCE_LEVEL_MEDIUM_LOW = 2;
59  
60      /** Medium level of confidence out of five levels. */
61      public static final int CONFIDENCE_LEVEL_MEDIUM = 3;
62  
63      /** Second-highest level of confidence out of five levels. */
64      public static final int CONFIDENCE_LEVEL_MEDIUM_HIGH = 4;
65  
66      /** Highest level of confidence out of five levels. */
67      public static final int CONFIDENCE_LEVEL_HIGH = 5;
68  
69      /** The {@code non-null} raw text version of the recognized part of the result. */
70      @NonNull
71      private final String mRawText;
72  
73      /**
74       * The formatted text version of the recognized part of the result. If formatting is enabled
75       * with {@link RecognizerIntent#EXTRA_ENABLE_FORMATTING}, it has a {@code non-null} value.
76       *
77       * <p> Otherwise, it should be {@code null} by default.
78       */
79      @Nullable
80      private final String mFormattedText;
defaultFormattedText()81      private static String defaultFormattedText() {
82          return null;
83      }
84  
85      /**
86       * Non-negative offset of the beginning of this part from
87       * the start of the recognition session in milliseconds
88       * if requested with {@link RecognizerIntent#EXTRA_REQUEST_WORD_TIMING}.
89       *
90       * <p> Otherwise, this should equal 0.
91       */
92      private final long mTimestampMillis;
defaultTimestampMillis()93      private static long defaultTimestampMillis() {
94          return 0;
95      }
96  
97      /**
98       * The level of confidence for this part if requested
99       * with {@link RecognizerIntent#EXTRA_REQUEST_WORD_CONFIDENCE}.
100       *
101       * <p> Otherwise, this should equal {@link #CONFIDENCE_LEVEL_UNKNOWN}.
102       */
103      @ConfidenceLevel
104      private final int mConfidenceLevel;
105      @ConfidenceLevel
defaultConfidenceLevel()106      private static int defaultConfidenceLevel() {
107          return CONFIDENCE_LEVEL_UNKNOWN;
108      }
109  
onConstructed()110      private void onConstructed() {
111          Preconditions.checkArgumentNonnegative(mTimestampMillis,
112                  "The timestamp must be non-negative.");
113      }
114  
115      @DataClass.Suppress("setFormattedText")
116      abstract static class BaseBuilder {
117          /**
118           * The formatted text version of the recognized part of the result. If formatting is enabled
119           * with {@link RecognizerIntent#EXTRA_ENABLE_FORMATTING}, it has a {@code non-null} value.
120           *
121           * <p> Otherwise, it should be {@code null} by default.
122           */
123          @NonNull
setFormattedText(@onNull String value)124          public Builder setFormattedText(@NonNull String value) {
125              // Method explicitly defined, so that the argument can be checked for non-null.
126              com.android.internal.util.AnnotationValidations.validate(NonNull.class, null, value);
127  
128              final Builder builder = (Builder) this;
129              builder.checkNotUsed();
130              builder.mBuilderFieldsSet |= 0x2;
131              builder.mFormattedText = value;
132              return builder;
133          }
134      }
135  
136  
137  
138      // Code below generated by codegen v1.0.23.
139      //
140      // DO NOT MODIFY!
141      // CHECKSTYLE:OFF Generated code
142      //
143      // To regenerate run:
144      // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/speech/RecognitionPart.java
145      //
146      // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
147      //   Settings > Editor > Code Style > Formatter Control
148      //@formatter:off
149  
150  
151      /** @hide */
152      @android.annotation.IntDef(prefix = "CONFIDENCE_LEVEL_", value = {
153          CONFIDENCE_LEVEL_UNKNOWN,
154          CONFIDENCE_LEVEL_LOW,
155          CONFIDENCE_LEVEL_MEDIUM_LOW,
156          CONFIDENCE_LEVEL_MEDIUM,
157          CONFIDENCE_LEVEL_MEDIUM_HIGH,
158          CONFIDENCE_LEVEL_HIGH
159      })
160      @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.SOURCE)
161      @DataClass.Generated.Member
162      public @interface ConfidenceLevel {}
163  
164      /** @hide */
165      @DataClass.Generated.Member
confidenceLevelToString(@onfidenceLevel int value)166      public static String confidenceLevelToString(@ConfidenceLevel int value) {
167          switch (value) {
168              case CONFIDENCE_LEVEL_UNKNOWN:
169                      return "CONFIDENCE_LEVEL_UNKNOWN";
170              case CONFIDENCE_LEVEL_LOW:
171                      return "CONFIDENCE_LEVEL_LOW";
172              case CONFIDENCE_LEVEL_MEDIUM_LOW:
173                      return "CONFIDENCE_LEVEL_MEDIUM_LOW";
174              case CONFIDENCE_LEVEL_MEDIUM:
175                      return "CONFIDENCE_LEVEL_MEDIUM";
176              case CONFIDENCE_LEVEL_MEDIUM_HIGH:
177                      return "CONFIDENCE_LEVEL_MEDIUM_HIGH";
178              case CONFIDENCE_LEVEL_HIGH:
179                      return "CONFIDENCE_LEVEL_HIGH";
180              default: return Integer.toHexString(value);
181          }
182      }
183  
184      @DataClass.Generated.Member
RecognitionPart( @onNull String rawText, @Nullable String formattedText, long timestampMillis, @ConfidenceLevel int confidenceLevel)185      /* package-private */ RecognitionPart(
186              @NonNull String rawText,
187              @Nullable String formattedText,
188              long timestampMillis,
189              @ConfidenceLevel int confidenceLevel) {
190          this.mRawText = rawText;
191          com.android.internal.util.AnnotationValidations.validate(
192                  NonNull.class, null, mRawText);
193          this.mFormattedText = formattedText;
194          this.mTimestampMillis = timestampMillis;
195          this.mConfidenceLevel = confidenceLevel;
196  
197          if (!(mConfidenceLevel == CONFIDENCE_LEVEL_UNKNOWN)
198                  && !(mConfidenceLevel == CONFIDENCE_LEVEL_LOW)
199                  && !(mConfidenceLevel == CONFIDENCE_LEVEL_MEDIUM_LOW)
200                  && !(mConfidenceLevel == CONFIDENCE_LEVEL_MEDIUM)
201                  && !(mConfidenceLevel == CONFIDENCE_LEVEL_MEDIUM_HIGH)
202                  && !(mConfidenceLevel == CONFIDENCE_LEVEL_HIGH)) {
203              throw new java.lang.IllegalArgumentException(
204                      "confidenceLevel was " + mConfidenceLevel + " but must be one of: "
205                              + "CONFIDENCE_LEVEL_UNKNOWN(" + CONFIDENCE_LEVEL_UNKNOWN + "), "
206                              + "CONFIDENCE_LEVEL_LOW(" + CONFIDENCE_LEVEL_LOW + "), "
207                              + "CONFIDENCE_LEVEL_MEDIUM_LOW(" + CONFIDENCE_LEVEL_MEDIUM_LOW + "), "
208                              + "CONFIDENCE_LEVEL_MEDIUM(" + CONFIDENCE_LEVEL_MEDIUM + "), "
209                              + "CONFIDENCE_LEVEL_MEDIUM_HIGH(" + CONFIDENCE_LEVEL_MEDIUM_HIGH + "), "
210                              + "CONFIDENCE_LEVEL_HIGH(" + CONFIDENCE_LEVEL_HIGH + ")");
211          }
212  
213  
214          onConstructed();
215      }
216  
217      /**
218       * The {@code non-null} raw text version of the recognized part of the result.
219       */
220      @DataClass.Generated.Member
getRawText()221      public @NonNull String getRawText() {
222          return mRawText;
223      }
224  
225      /**
226       * The formatted text version of the recognized part of the result. If formatting is enabled
227       * with {@link RecognizerIntent#EXTRA_ENABLE_FORMATTING}, it has a {@code non-null} value.
228       *
229       * <p> Otherwise, it should be {@code null} by default.
230       */
231      @DataClass.Generated.Member
getFormattedText()232      public @Nullable String getFormattedText() {
233          return mFormattedText;
234      }
235  
236      /**
237       * Non-negative offset of the beginning of this part from
238       * the start of the recognition session in milliseconds
239       * if requested with {@link RecognizerIntent#EXTRA_REQUEST_WORD_TIMING}.
240       *
241       * <p> Otherwise, this should equal 0.
242       */
243      @DataClass.Generated.Member
getTimestampMillis()244      public long getTimestampMillis() {
245          return mTimestampMillis;
246      }
247  
248      /**
249       * The level of confidence for this part if requested
250       * with {@link RecognizerIntent#EXTRA_REQUEST_WORD_CONFIDENCE}.
251       *
252       * <p> Otherwise, this should equal {@link #CONFIDENCE_LEVEL_UNKNOWN}.
253       */
254      @DataClass.Generated.Member
getConfidenceLevel()255      public @ConfidenceLevel int getConfidenceLevel() {
256          return mConfidenceLevel;
257      }
258  
259      @Override
260      @DataClass.Generated.Member
toString()261      public String toString() {
262          // You can override field toString logic by defining methods like:
263          // String fieldNameToString() { ... }
264  
265          return "RecognitionPart { " +
266                  "rawText = " + mRawText + ", " +
267                  "formattedText = " + mFormattedText + ", " +
268                  "timestampMillis = " + mTimestampMillis + ", " +
269                  "confidenceLevel = " + confidenceLevelToString(mConfidenceLevel) +
270          " }";
271      }
272  
273      @Override
274      @DataClass.Generated.Member
equals(@ullable Object o)275      public boolean equals(@Nullable Object o) {
276          // You can override field equality logic by defining either of the methods like:
277          // boolean fieldNameEquals(RecognitionPart other) { ... }
278          // boolean fieldNameEquals(FieldType otherValue) { ... }
279  
280          if (this == o) return true;
281          if (o == null || getClass() != o.getClass()) return false;
282          @SuppressWarnings("unchecked")
283          RecognitionPart that = (RecognitionPart) o;
284          //noinspection PointlessBooleanExpression
285          return true
286                  && java.util.Objects.equals(mRawText, that.mRawText)
287                  && java.util.Objects.equals(mFormattedText, that.mFormattedText)
288                  && mTimestampMillis == that.mTimestampMillis
289                  && mConfidenceLevel == that.mConfidenceLevel;
290      }
291  
292      @Override
293      @DataClass.Generated.Member
hashCode()294      public int hashCode() {
295          // You can override field hashCode logic by defining methods like:
296          // int fieldNameHashCode() { ... }
297  
298          int _hash = 1;
299          _hash = 31 * _hash + java.util.Objects.hashCode(mRawText);
300          _hash = 31 * _hash + java.util.Objects.hashCode(mFormattedText);
301          _hash = 31 * _hash + Long.hashCode(mTimestampMillis);
302          _hash = 31 * _hash + mConfidenceLevel;
303          return _hash;
304      }
305  
306      @Override
307      @DataClass.Generated.Member
writeToParcel(@onNull android.os.Parcel dest, int flags)308      public void writeToParcel(@NonNull android.os.Parcel dest, int flags) {
309          // You can override field parcelling by defining methods like:
310          // void parcelFieldName(Parcel dest, int flags) { ... }
311  
312          byte flg = 0;
313          if (mFormattedText != null) flg |= 0x2;
314          dest.writeByte(flg);
315          dest.writeString(mRawText);
316          if (mFormattedText != null) dest.writeString(mFormattedText);
317          dest.writeLong(mTimestampMillis);
318          dest.writeInt(mConfidenceLevel);
319      }
320  
321      @Override
322      @DataClass.Generated.Member
describeContents()323      public int describeContents() { return 0; }
324  
325      /** @hide */
326      @SuppressWarnings({"unchecked", "RedundantCast"})
327      @DataClass.Generated.Member
RecognitionPart(@onNull android.os.Parcel in)328      /* package-private */ RecognitionPart(@NonNull android.os.Parcel in) {
329          // You can override field unparcelling by defining methods like:
330          // static FieldType unparcelFieldName(Parcel in) { ... }
331  
332          byte flg = in.readByte();
333          String rawText = in.readString();
334          String formattedText = (flg & 0x2) == 0 ? null : in.readString();
335          long timestampMillis = in.readLong();
336          int confidenceLevel = in.readInt();
337  
338          this.mRawText = rawText;
339          com.android.internal.util.AnnotationValidations.validate(
340                  NonNull.class, null, mRawText);
341          this.mFormattedText = formattedText;
342          this.mTimestampMillis = timestampMillis;
343          this.mConfidenceLevel = confidenceLevel;
344  
345          if (!(mConfidenceLevel == CONFIDENCE_LEVEL_UNKNOWN)
346                  && !(mConfidenceLevel == CONFIDENCE_LEVEL_LOW)
347                  && !(mConfidenceLevel == CONFIDENCE_LEVEL_MEDIUM_LOW)
348                  && !(mConfidenceLevel == CONFIDENCE_LEVEL_MEDIUM)
349                  && !(mConfidenceLevel == CONFIDENCE_LEVEL_MEDIUM_HIGH)
350                  && !(mConfidenceLevel == CONFIDENCE_LEVEL_HIGH)) {
351              throw new java.lang.IllegalArgumentException(
352                      "confidenceLevel was " + mConfidenceLevel + " but must be one of: "
353                              + "CONFIDENCE_LEVEL_UNKNOWN(" + CONFIDENCE_LEVEL_UNKNOWN + "), "
354                              + "CONFIDENCE_LEVEL_LOW(" + CONFIDENCE_LEVEL_LOW + "), "
355                              + "CONFIDENCE_LEVEL_MEDIUM_LOW(" + CONFIDENCE_LEVEL_MEDIUM_LOW + "), "
356                              + "CONFIDENCE_LEVEL_MEDIUM(" + CONFIDENCE_LEVEL_MEDIUM + "), "
357                              + "CONFIDENCE_LEVEL_MEDIUM_HIGH(" + CONFIDENCE_LEVEL_MEDIUM_HIGH + "), "
358                              + "CONFIDENCE_LEVEL_HIGH(" + CONFIDENCE_LEVEL_HIGH + ")");
359          }
360  
361  
362          onConstructed();
363      }
364  
365      @DataClass.Generated.Member
366      public static final @NonNull Parcelable.Creator<RecognitionPart> CREATOR
367              = new Parcelable.Creator<RecognitionPart>() {
368          @Override
369          public RecognitionPart[] newArray(int size) {
370              return new RecognitionPart[size];
371          }
372  
373          @Override
374          public RecognitionPart createFromParcel(@NonNull android.os.Parcel in) {
375              return new RecognitionPart(in);
376          }
377      };
378  
379      /**
380       * A builder for {@link RecognitionPart}
381       */
382      @SuppressWarnings("WeakerAccess")
383      @DataClass.Generated.Member
384      public static final class Builder extends BaseBuilder {
385  
386          private @NonNull String mRawText;
387          private @Nullable String mFormattedText;
388          private long mTimestampMillis;
389          private @ConfidenceLevel int mConfidenceLevel;
390  
391          private long mBuilderFieldsSet = 0L;
392  
393          /**
394           * Creates a new Builder.
395           *
396           * @param rawText
397           *   The {@code non-null} raw text version of the recognized part of the result.
398           */
Builder( @onNull String rawText)399          public Builder(
400                  @NonNull String rawText) {
401              mRawText = rawText;
402              com.android.internal.util.AnnotationValidations.validate(
403                      NonNull.class, null, mRawText);
404          }
405  
406          /**
407           * The {@code non-null} raw text version of the recognized part of the result.
408           */
409          @DataClass.Generated.Member
setRawText(@onNull String value)410          public @NonNull Builder setRawText(@NonNull String value) {
411              checkNotUsed();
412              mBuilderFieldsSet |= 0x1;
413              mRawText = value;
414              return this;
415          }
416  
417          /**
418           * Non-negative offset of the beginning of this part from
419           * the start of the recognition session in milliseconds
420           * if requested with {@link RecognizerIntent#EXTRA_REQUEST_WORD_TIMING}.
421           *
422           * <p> Otherwise, this should equal 0.
423           */
424          @DataClass.Generated.Member
setTimestampMillis(long value)425          public @NonNull Builder setTimestampMillis(long value) {
426              checkNotUsed();
427              mBuilderFieldsSet |= 0x4;
428              mTimestampMillis = value;
429              return this;
430          }
431  
432          /**
433           * The level of confidence for this part if requested
434           * with {@link RecognizerIntent#EXTRA_REQUEST_WORD_CONFIDENCE}.
435           *
436           * <p> Otherwise, this should equal {@link #CONFIDENCE_LEVEL_UNKNOWN}.
437           */
438          @DataClass.Generated.Member
setConfidenceLevel(@onfidenceLevel int value)439          public @NonNull Builder setConfidenceLevel(@ConfidenceLevel int value) {
440              checkNotUsed();
441              mBuilderFieldsSet |= 0x8;
442              mConfidenceLevel = value;
443              return this;
444          }
445  
446          /** Builds the instance. This builder should not be touched after calling this! */
build()447          public @NonNull RecognitionPart build() {
448              checkNotUsed();
449              mBuilderFieldsSet |= 0x10; // Mark builder used
450  
451              if ((mBuilderFieldsSet & 0x2) == 0) {
452                  mFormattedText = defaultFormattedText();
453              }
454              if ((mBuilderFieldsSet & 0x4) == 0) {
455                  mTimestampMillis = defaultTimestampMillis();
456              }
457              if ((mBuilderFieldsSet & 0x8) == 0) {
458                  mConfidenceLevel = defaultConfidenceLevel();
459              }
460              RecognitionPart o = new RecognitionPart(
461                      mRawText,
462                      mFormattedText,
463                      mTimestampMillis,
464                      mConfidenceLevel);
465              return o;
466          }
467  
checkNotUsed()468          private void checkNotUsed() {
469              if ((mBuilderFieldsSet & 0x10) != 0) {
470                  throw new IllegalStateException(
471                          "This Builder should not be reused. Use a new Builder instance instead");
472              }
473          }
474      }
475  
476      @DataClass.Generated(
477              time = 1677008539189L,
478              codegenVersion = "1.0.23",
479              sourceFile = "frameworks/base/core/java/android/speech/RecognitionPart.java",
480              inputSignatures = "public static final  int CONFIDENCE_LEVEL_UNKNOWN\npublic static final  int CONFIDENCE_LEVEL_LOW\npublic static final  int CONFIDENCE_LEVEL_MEDIUM_LOW\npublic static final  int CONFIDENCE_LEVEL_MEDIUM\npublic static final  int CONFIDENCE_LEVEL_MEDIUM_HIGH\npublic static final  int CONFIDENCE_LEVEL_HIGH\nprivate final @android.annotation.NonNull java.lang.String mRawText\nprivate final @android.annotation.Nullable java.lang.String mFormattedText\nprivate final  long mTimestampMillis\nprivate final @android.speech.RecognitionPart.ConfidenceLevel int mConfidenceLevel\nprivate static  java.lang.String defaultFormattedText()\nprivate static  long defaultTimestampMillis()\nprivate static @android.speech.RecognitionPart.ConfidenceLevel int defaultConfidenceLevel()\nprivate  void onConstructed()\nclass RecognitionPart extends java.lang.Object implements [android.os.Parcelable]\npublic @android.annotation.NonNull android.speech.RecognitionPart.Builder setFormattedText(java.lang.String)\nclass BaseBuilder extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genBuilder=true, genEqualsHashCode=true, genHiddenConstDefs=true, genToString=true)\npublic @android.annotation.NonNull android.speech.RecognitionPart.Builder setFormattedText(java.lang.String)\nclass BaseBuilder extends java.lang.Object implements []")
481      @Deprecated
__metadata()482      private void __metadata() {}
483  
484  
485      //@formatter:on
486      // End of generated code
487  
488  }
489