1 /*
2  * Copyright (C) 2024 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.app.ondeviceintelligence;
18 
19 
20 import static android.app.ondeviceintelligence.flags.Flags.FLAG_ENABLE_ON_DEVICE_INTELLIGENCE;
21 
22 import android.annotation.FlaggedApi;
23 import android.annotation.NonNull;
24 import android.annotation.SystemApi;
25 import android.os.PersistableBundle;
26 
27 import androidx.annotation.IntDef;
28 
29 import java.lang.annotation.ElementType;
30 import java.lang.annotation.Target;
31 
32 /**
33  * Exception type to be used for errors related to on-device intelligence system service with
34  * appropriate error code.
35  *
36  * @hide
37  */
38 @SystemApi
39 @FlaggedApi(FLAG_ENABLE_ON_DEVICE_INTELLIGENCE)
40 public class OnDeviceIntelligenceException extends Exception {
41 
42     public static final int PROCESSING_ERROR_UNKNOWN = 1;
43 
44     /** Request passed contains bad data for e.g. format. */
45     public static final int PROCESSING_ERROR_BAD_DATA = 2;
46 
47     /** Bad request for inputs. */
48     public static final int PROCESSING_ERROR_BAD_REQUEST = 3;
49 
50     /** Whole request was classified as not safe, and no response will be generated. */
51     public static final int PROCESSING_ERROR_REQUEST_NOT_SAFE = 4;
52 
53     /** Underlying processing encountered an error and failed to compute results. */
54     public static final int PROCESSING_ERROR_COMPUTE_ERROR = 5;
55 
56     /** Encountered an error while performing IPC */
57     public static final int PROCESSING_ERROR_IPC_ERROR = 6;
58 
59     /** Request was cancelled either by user signal or by the underlying implementation. */
60     public static final int PROCESSING_ERROR_CANCELLED = 7;
61 
62     /** Underlying processing in the remote implementation is not available. */
63     public static final int PROCESSING_ERROR_NOT_AVAILABLE = 8;
64 
65     /** The service is currently busy. Callers should retry with exponential backoff. */
66     public static final int PROCESSING_ERROR_BUSY = 9;
67 
68     /** Something went wrong with safety classification service. */
69     public static final int PROCESSING_ERROR_SAFETY_ERROR = 10;
70 
71     /** Response generated was classified unsafe. */
72     public static final int PROCESSING_ERROR_RESPONSE_NOT_SAFE = 11;
73 
74     /** Request is too large to be processed. */
75     public static final int PROCESSING_ERROR_REQUEST_TOO_LARGE = 12;
76 
77     /** Inference suspended so that higher-priority inference can run. */
78     public static final int PROCESSING_ERROR_SUSPENDED = 13;
79 
80     /**
81      * Underlying processing encountered an internal error, like a violated precondition
82      * .
83      */
84     public static final int PROCESSING_ERROR_INTERNAL = 14;
85 
86     /**
87      * The processing was not able to be passed on to the remote implementation, as the
88      * service
89      * was unavailable.
90      */
91     public static final int PROCESSING_ERROR_SERVICE_UNAVAILABLE = 15;
92     /**
93      * Error code returned when the OnDeviceIntelligenceManager service is unavailable.
94      */
95     public static final int ON_DEVICE_INTELLIGENCE_SERVICE_UNAVAILABLE = 100;
96 
97     /**
98      * The connection to remote service failed and the processing state could not be updated.
99      */
100     public static final int PROCESSING_UPDATE_STATUS_CONNECTION_FAILED = 200;
101 
102 
103     /**
104      * Error code associated with the on-device intelligence failure.
105      *
106      * @hide
107      */
108     @IntDef(
109             value = {
110                     PROCESSING_ERROR_UNKNOWN,
111                     PROCESSING_ERROR_BAD_DATA,
112                     PROCESSING_ERROR_BAD_REQUEST,
113                     PROCESSING_ERROR_REQUEST_NOT_SAFE,
114                     PROCESSING_ERROR_COMPUTE_ERROR,
115                     PROCESSING_ERROR_IPC_ERROR,
116                     PROCESSING_ERROR_CANCELLED,
117                     PROCESSING_ERROR_NOT_AVAILABLE,
118                     PROCESSING_ERROR_BUSY,
119                     PROCESSING_ERROR_SAFETY_ERROR,
120                     PROCESSING_ERROR_RESPONSE_NOT_SAFE,
121                     PROCESSING_ERROR_REQUEST_TOO_LARGE,
122                     PROCESSING_ERROR_SUSPENDED,
123                     PROCESSING_ERROR_INTERNAL,
124                     PROCESSING_ERROR_SERVICE_UNAVAILABLE,
125                     ON_DEVICE_INTELLIGENCE_SERVICE_UNAVAILABLE,
126                     PROCESSING_UPDATE_STATUS_CONNECTION_FAILED
127             }, open = true)
128     @Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})
129     @interface OnDeviceIntelligenceError {
130     }
131 
132     private final int mErrorCode;
133     private final PersistableBundle mErrorParams;
134 
135     /** Returns the error code of the exception. */
getErrorCode()136     public int getErrorCode() {
137         return mErrorCode;
138     }
139 
140     /** Returns the error params of the exception. */
141     @NonNull
getErrorParams()142     public PersistableBundle getErrorParams() {
143         return mErrorParams;
144     }
145 
146     /**
147      * Creates a new OnDeviceIntelligenceException with the specified error code, error message and
148      * error params.
149      *
150      * @param errorCode The error code.
151      * @param errorMessage The error message.
152      * @param errorParams The error params.
153      */
OnDeviceIntelligenceException( @nDeviceIntelligenceError int errorCode, @NonNull String errorMessage, @NonNull PersistableBundle errorParams)154     public OnDeviceIntelligenceException(
155             @OnDeviceIntelligenceError int errorCode, @NonNull String errorMessage,
156             @NonNull PersistableBundle errorParams) {
157         super(errorMessage);
158         this.mErrorCode = errorCode;
159         this.mErrorParams = errorParams;
160     }
161 
162     /**
163      * Creates a new OnDeviceIntelligenceException with the specified error code and error params.
164      *
165      * @param errorCode The error code.
166      * @param errorParams The error params.
167      */
OnDeviceIntelligenceException( @nDeviceIntelligenceError int errorCode, @NonNull PersistableBundle errorParams)168     public OnDeviceIntelligenceException(
169             @OnDeviceIntelligenceError int errorCode,
170             @NonNull PersistableBundle errorParams) {
171         this.mErrorCode = errorCode;
172         this.mErrorParams = errorParams;
173     }
174 
175     /**
176      * Creates a new OnDeviceIntelligenceException with the specified error code and error message.
177      *
178      * @param errorCode The error code.
179      * @param errorMessage The error message.
180      */
OnDeviceIntelligenceException( @nDeviceIntelligenceError int errorCode, @NonNull String errorMessage)181     public OnDeviceIntelligenceException(
182             @OnDeviceIntelligenceError int errorCode, @NonNull String errorMessage) {
183         super(errorMessage);
184         this.mErrorCode = errorCode;
185         this.mErrorParams = new PersistableBundle();
186     }
187 
188     /**
189      * Creates a new OnDeviceIntelligenceException with the specified error code.
190      *
191      * @param errorCode The error code.
192      */
OnDeviceIntelligenceException( @nDeviceIntelligenceError int errorCode)193     public OnDeviceIntelligenceException(
194             @OnDeviceIntelligenceError int errorCode) {
195         this.mErrorCode = errorCode;
196         this.mErrorParams = new PersistableBundle();
197     }
198 }
199