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.service.voice; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.SystemApi; 22 import android.annotation.TestApi; 23 import android.os.Parcel; 24 import android.os.Parcelable; 25 import android.text.TextUtils; 26 27 import java.lang.annotation.Retention; 28 import java.lang.annotation.RetentionPolicy; 29 30 /** 31 * This class is used by the assistant application to know what went wrong during using 32 * {@link VisualQueryDetectionService} and which action that the application should take. 33 * 34 * @hide 35 */ 36 @SystemApi 37 public final class VisualQueryDetectionServiceFailure implements Parcelable { 38 39 /** 40 * An error code which means an unknown error occurs. 41 */ 42 public static final int ERROR_CODE_UNKNOWN = 0; 43 44 /** 45 * Indicates that the system server binds visual query detection service failure. 46 */ 47 public static final int ERROR_CODE_BIND_FAILURE = 1; 48 49 /** 50 * Indicates that the visual query detection service is dead. 51 */ 52 public static final int ERROR_CODE_BINDING_DIED = 2; 53 54 /** 55 * Indicates that the detection service has no attention listener registered. 56 */ 57 public static final int ERROR_CODE_ILLEGAL_ATTENTION_STATE = 3; 58 59 /** 60 * Indicates that the detection service is not egressing and should not be streaming queries. 61 */ 62 public static final int ERROR_CODE_ILLEGAL_STREAMING_STATE = 4; 63 64 /** 65 * Indicates that the remote exception occurs when calling callback method. 66 */ 67 public static final int ERROR_CODE_REMOTE_EXCEPTION = 5; 68 69 /** 70 * @hide 71 */ 72 @IntDef(prefix = {"ERROR_CODE_"}, value = { 73 ERROR_CODE_UNKNOWN, 74 ERROR_CODE_BIND_FAILURE, 75 ERROR_CODE_BINDING_DIED, 76 ERROR_CODE_ILLEGAL_ATTENTION_STATE, 77 ERROR_CODE_ILLEGAL_STREAMING_STATE, 78 ERROR_CODE_REMOTE_EXCEPTION 79 }) 80 @Retention(RetentionPolicy.SOURCE) 81 public @interface VisualQueryDetectionServiceErrorCode {} 82 83 private int mErrorCode = ERROR_CODE_UNKNOWN; 84 private String mErrorMessage = "Unknown"; 85 86 /** 87 * @hide 88 */ 89 @TestApi VisualQueryDetectionServiceFailure(int errorCode, @NonNull String errorMessage)90 public VisualQueryDetectionServiceFailure(int errorCode, @NonNull String errorMessage) { 91 if (TextUtils.isEmpty(errorMessage)) { 92 throw new IllegalArgumentException("errorMessage is empty or null."); 93 } 94 mErrorCode = errorCode; 95 mErrorMessage = errorMessage; 96 } 97 98 /** 99 * Returns the error code. 100 */ 101 @VisualQueryDetectionServiceErrorCode getErrorCode()102 public int getErrorCode() { 103 return mErrorCode; 104 } 105 106 /** 107 * Returns the error message. 108 */ 109 @NonNull getErrorMessage()110 public String getErrorMessage() { 111 return mErrorMessage; 112 } 113 114 /** 115 * Returns the suggested action. 116 */ 117 @FailureSuggestedAction.FailureSuggestedActionDef getSuggestedAction()118 public int getSuggestedAction() { 119 switch (mErrorCode) { 120 case ERROR_CODE_BIND_FAILURE: 121 case ERROR_CODE_BINDING_DIED: 122 case ERROR_CODE_ILLEGAL_ATTENTION_STATE: 123 case ERROR_CODE_REMOTE_EXCEPTION: 124 return FailureSuggestedAction.RECREATE_DETECTOR; 125 case ERROR_CODE_ILLEGAL_STREAMING_STATE: 126 return FailureSuggestedAction.RESTART_RECOGNITION; 127 default: 128 return FailureSuggestedAction.NONE; 129 } 130 } 131 132 @Override describeContents()133 public int describeContents() { 134 return 0; 135 } 136 137 @Override writeToParcel(@onNull Parcel dest, int flags)138 public void writeToParcel(@NonNull Parcel dest, int flags) { 139 dest.writeInt(mErrorCode); 140 dest.writeString8(mErrorMessage); 141 } 142 143 @Override toString()144 public String toString() { 145 return "VisualQueryDetectionServiceFailure { errorCode = " + mErrorCode 146 + ", errorMessage = " + mErrorMessage + " }"; 147 } 148 149 public static final @NonNull Parcelable.Creator<VisualQueryDetectionServiceFailure> CREATOR = 150 new Parcelable.Creator<VisualQueryDetectionServiceFailure>() { 151 @Override 152 public VisualQueryDetectionServiceFailure[] newArray(int size) { 153 return new VisualQueryDetectionServiceFailure[size]; 154 } 155 156 @Override 157 public VisualQueryDetectionServiceFailure createFromParcel(@NonNull Parcel in) { 158 return new VisualQueryDetectionServiceFailure(in.readInt(), in.readString8()); 159 } 160 }; 161 } 162