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