1 /*
2  * Copyright (c) 2019 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.telephony.ims;
18 
19 import android.annotation.IntDef;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.content.pm.PackageManager;
23 import android.text.TextUtils;
24 
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 
28 /**
29  * This class defines an IMS-related exception that has been thrown while interacting with a
30  * device or carrier provided ImsService implementation.
31  * @hide
32  */
33 @SystemApi
34 public final class ImsException extends Exception {
35 
36     /**
37      * The operation has failed due to an unknown or unspecified error.
38      */
39     public static final int CODE_ERROR_UNSPECIFIED = 0;
40     /**
41      * The operation has failed because there is no {@link ImsService} available to service it. This
42      * may be due to an {@link ImsService} crash or other illegal state.
43      * <p>
44      * This is a temporary error and the operation may be retried until the connection to the
45      * {@link ImsService} is restored.
46      */
47     public static final int CODE_ERROR_SERVICE_UNAVAILABLE = 1;
48 
49     /**
50      * This device or carrier configuration does not support IMS for this subscription.
51      * <p>
52      * This is a permanent configuration error and there should be no retry. Usually this is
53      * because {@link PackageManager#FEATURE_TELEPHONY_IMS} is not available
54      * or the device has no ImsService implementation to service this request.
55      */
56     public static final int CODE_ERROR_UNSUPPORTED_OPERATION = 2;
57 
58     /**@hide*/
59     @Retention(RetentionPolicy.SOURCE)
60     @IntDef(prefix = "CODE_ERROR_", value = {
61             CODE_ERROR_UNSPECIFIED,
62             CODE_ERROR_SERVICE_UNAVAILABLE,
63             CODE_ERROR_UNSUPPORTED_OPERATION
64     })
65     public @interface ImsErrorCode {}
66 
67     private int mCode = CODE_ERROR_UNSPECIFIED;
68 
69     /**
70      * A new {@link ImsException} with an unspecified {@link ImsErrorCode} code.
71      * @param message an optional message to detail the error condition more specifically.
72      */
ImsException(@ullable String message)73     public ImsException(@Nullable String message) {
74         super(getMessage(message, CODE_ERROR_UNSPECIFIED));
75     }
76 
77     /**
78      * A new {@link ImsException} that includes an {@link ImsErrorCode} error code.
79      * @param message an optional message to detail the error condition more specifically.
80      */
ImsException(@ullable String message, @ImsErrorCode int code)81     public ImsException(@Nullable String message, @ImsErrorCode int code) {
82         super(getMessage(message, code));
83         mCode = code;
84     }
85 
86     /**
87      * A new {@link ImsException} that includes an {@link ImsErrorCode} error code and a
88      * {@link Throwable} that contains the original error that was thrown to lead to this Exception.
89      * @param message an optional message to detail the error condition more specifically.
90      * @param cause the {@link Throwable} that caused this {@link ImsException} to be created.
91      */
ImsException(@ullable String message, @ImsErrorCode int code, @Nullable Throwable cause)92     public ImsException(@Nullable String message, @ImsErrorCode  int code,
93             @Nullable Throwable cause) {
94         super(getMessage(message, code), cause);
95         mCode = code;
96     }
97 
98     /**
99      * @return the IMS Error code that is associated with this {@link ImsException}.
100      */
getCode()101     public @ImsErrorCode int getCode() {
102         return mCode;
103     }
104 
getMessage(String message, int code)105     private static String getMessage(String message, int code) {
106         StringBuilder builder;
107         if (!TextUtils.isEmpty(message)) {
108             builder = new StringBuilder(message);
109             builder.append(" (code: ");
110             builder.append(code);
111             builder.append(")");
112             return builder.toString();
113         } else {
114             return "code: " + code;
115         }
116     }
117 }
118