Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 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 | |
sewookseo | e7c4814 | 2022-03-10 03:06:43 +0000 | [diff] [blame] | 24 | import com.android.internal.annotations.VisibleForTesting; |
| 25 | |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 26 | import java.lang.annotation.Retention; |
| 27 | import 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 |
| 42 | public 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, |
sewookseo | afc22b0 | 2022-03-07 11:03:13 +0000 | [diff] [blame] | 49 | EX_TYPE_FILTER_SOCKET_NOT_CONNECTED, |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 50 | EX_TYPE_FILTER_NOT_SUPPORTED, |
| 51 | EX_TYPE_FILTER_SOCKET_LOCAL_ADDRESS_CHANGED, |
sewookseo | afc22b0 | 2022-03-07 11:03:13 +0000 | [diff] [blame] | 52 | EX_TYPE_FILTER_SOCKET_REMOTE_ADDRESS_CHANGED, |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 53 | }) |
| 54 | @Retention(RetentionPolicy.SOURCE) |
| 55 | public @interface ExceptionType {} |
| 56 | |
| 57 | private static final String TAG = "QosCallbackException"; |
| 58 | |
| 59 | // Types of exceptions supported // |
Remi NGUYEN VAN | bd7a485 | 2022-07-13 20:42:13 +0900 | [diff] [blame] | 60 | // The constants are used for the sendQosCallbackError system API, so they must not be changed |
| 61 | // as there may be callers relying on their historical values to call that API. |
| 62 | // TODO: mark the constants as @SystemApi, since they are necessary to call a system API. |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 63 | /** {@hide} */ |
| 64 | public static final int EX_TYPE_FILTER_NONE = 0; |
| 65 | |
| 66 | /** {@hide} */ |
| 67 | public static final int EX_TYPE_FILTER_NETWORK_RELEASED = 1; |
| 68 | |
| 69 | /** {@hide} */ |
| 70 | public static final int EX_TYPE_FILTER_SOCKET_NOT_BOUND = 2; |
| 71 | |
| 72 | /** {@hide} */ |
Remi NGUYEN VAN | bd7a485 | 2022-07-13 20:42:13 +0900 | [diff] [blame] | 73 | public static final int EX_TYPE_FILTER_NOT_SUPPORTED = 3; |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 74 | |
| 75 | /** {@hide} */ |
Remi NGUYEN VAN | bd7a485 | 2022-07-13 20:42:13 +0900 | [diff] [blame] | 76 | public static final int EX_TYPE_FILTER_SOCKET_LOCAL_ADDRESS_CHANGED = 4; |
sewookseo | afc22b0 | 2022-03-07 11:03:13 +0000 | [diff] [blame] | 77 | |
| 78 | /** {@hide} */ |
Remi NGUYEN VAN | bd7a485 | 2022-07-13 20:42:13 +0900 | [diff] [blame] | 79 | public static final int EX_TYPE_FILTER_SOCKET_NOT_CONNECTED = 5; |
sewookseo | afc22b0 | 2022-03-07 11:03:13 +0000 | [diff] [blame] | 80 | |
| 81 | /** {@hide} */ |
| 82 | public static final int EX_TYPE_FILTER_SOCKET_REMOTE_ADDRESS_CHANGED = 6; |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Creates exception based off of a type and message. Not all types of exceptions accept a |
| 86 | * custom message. |
| 87 | * |
| 88 | * {@hide} |
| 89 | */ |
| 90 | @NonNull |
Remi NGUYEN VAN | e55a88d | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 91 | public static QosCallbackException createException(@ExceptionType final int type) { |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 92 | switch (type) { |
| 93 | case EX_TYPE_FILTER_NETWORK_RELEASED: |
| 94 | return new QosCallbackException(new NetworkReleasedException()); |
| 95 | case EX_TYPE_FILTER_SOCKET_NOT_BOUND: |
| 96 | return new QosCallbackException(new SocketNotBoundException()); |
sewookseo | afc22b0 | 2022-03-07 11:03:13 +0000 | [diff] [blame] | 97 | case EX_TYPE_FILTER_SOCKET_NOT_CONNECTED: |
| 98 | return new QosCallbackException(new SocketNotConnectedException()); |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 99 | case EX_TYPE_FILTER_NOT_SUPPORTED: |
| 100 | return new QosCallbackException(new UnsupportedOperationException( |
| 101 | "This device does not support the specified filter")); |
| 102 | case EX_TYPE_FILTER_SOCKET_LOCAL_ADDRESS_CHANGED: |
| 103 | return new QosCallbackException( |
| 104 | new SocketLocalAddressChangedException()); |
sewookseo | afc22b0 | 2022-03-07 11:03:13 +0000 | [diff] [blame] | 105 | case EX_TYPE_FILTER_SOCKET_REMOTE_ADDRESS_CHANGED: |
| 106 | return new QosCallbackException( |
| 107 | new SocketRemoteAddressChangedException()); |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 108 | default: |
| 109 | Log.wtf(TAG, "create: No case setup for exception type: '" + type + "'"); |
| 110 | return new QosCallbackException( |
| 111 | new RuntimeException("Unknown exception code: " + type)); |
| 112 | } |
| 113 | } |
| 114 | |
sewookseo | e7c4814 | 2022-03-10 03:06:43 +0000 | [diff] [blame] | 115 | @VisibleForTesting |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 116 | public QosCallbackException(@NonNull final String message) { |
| 117 | super(message); |
| 118 | } |
| 119 | |
sewookseo | e7c4814 | 2022-03-10 03:06:43 +0000 | [diff] [blame] | 120 | @VisibleForTesting |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 121 | public QosCallbackException(@NonNull final Throwable cause) { |
| 122 | super(cause); |
| 123 | } |
| 124 | } |