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, |
| 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 VAN | 7b92ff2 | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 80 | public static QosCallbackException createException(@ExceptionType final int type) { |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 81 | 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 | |
sewookseo | e7c4814 | 2022-03-10 03:06:43 +0000 | [diff] [blame] | 99 | @VisibleForTesting |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 100 | public QosCallbackException(@NonNull final String message) { |
| 101 | super(message); |
| 102 | } |
| 103 | |
sewookseo | e7c4814 | 2022-03-10 03:06:43 +0000 | [diff] [blame] | 104 | @VisibleForTesting |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 105 | public QosCallbackException(@NonNull final Throwable cause) { |
| 106 | super(cause); |
| 107 | } |
| 108 | } |