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