1 /*
2  * Copyright (C) 2020 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.net;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.SystemApi;
22 import android.util.Log;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 
27 /**
28  * This is the exception type passed back through the onError method on {@link QosCallback}.
29  * {@link QosCallbackException#getCause()} contains the actual error that caused this exception.
30  *
31  * The possible exception types as causes are:
32  * 1. {@link NetworkReleasedException}
33  * 2. {@link SocketNotBoundException}
34  * 3. {@link UnsupportedOperationException}
35  * 4. {@link SocketLocalAddressChangedException}
36  *
37  * @hide
38  */
39 @SystemApi
40 public final class QosCallbackException extends Exception {
41 
42     /** @hide */
43     @IntDef(prefix = {"EX_TYPE_"}, value = {
44             EX_TYPE_FILTER_NONE,
45             EX_TYPE_FILTER_NETWORK_RELEASED,
46             EX_TYPE_FILTER_SOCKET_NOT_BOUND,
47             EX_TYPE_FILTER_NOT_SUPPORTED,
48             EX_TYPE_FILTER_SOCKET_LOCAL_ADDRESS_CHANGED,
49     })
50     @Retention(RetentionPolicy.SOURCE)
51     public @interface ExceptionType {}
52 
53     private static final String TAG = "QosCallbackException";
54 
55     // Types of exceptions supported //
56     /** {@hide} */
57     public static final int EX_TYPE_FILTER_NONE = 0;
58 
59     /** {@hide} */
60     public static final int EX_TYPE_FILTER_NETWORK_RELEASED = 1;
61 
62     /** {@hide} */
63     public static final int EX_TYPE_FILTER_SOCKET_NOT_BOUND = 2;
64 
65     /** {@hide} */
66     public static final int EX_TYPE_FILTER_NOT_SUPPORTED = 3;
67 
68     /** {@hide} */
69     public static final int EX_TYPE_FILTER_SOCKET_LOCAL_ADDRESS_CHANGED = 4;
70 
71     /**
72      * Creates exception based off of a type and message.  Not all types of exceptions accept a
73      * custom message.
74      *
75      * {@hide}
76      */
77     @NonNull
78     static QosCallbackException createException(@ExceptionType final int type) {
79         switch (type) {
80             case EX_TYPE_FILTER_NETWORK_RELEASED:
81                 return new QosCallbackException(new NetworkReleasedException());
82             case EX_TYPE_FILTER_SOCKET_NOT_BOUND:
83                 return new QosCallbackException(new SocketNotBoundException());
84             case EX_TYPE_FILTER_NOT_SUPPORTED:
85                 return new QosCallbackException(new UnsupportedOperationException(
86                         "This device does not support the specified filter"));
87             case EX_TYPE_FILTER_SOCKET_LOCAL_ADDRESS_CHANGED:
88                 return new QosCallbackException(
89                         new SocketLocalAddressChangedException());
90             default:
91                 Log.wtf(TAG, "create: No case setup for exception type: '" + type + "'");
92                 return new QosCallbackException(
93                         new RuntimeException("Unknown exception code: " + type));
94         }
95     }
96 
97     /**
98      * @hide
99      */
100     public QosCallbackException(@NonNull final String message) {
101         super(message);
102     }
103 
104     /**
105      * @hide
106      */
107     public QosCallbackException(@NonNull final Throwable cause) {
108         super(cause);
109     }
110 }
111