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 com.android.internal.annotations.VisibleForTesting; 25 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_SOCKET_NOT_CONNECTED, 50 EX_TYPE_FILTER_NOT_SUPPORTED, 51 EX_TYPE_FILTER_SOCKET_LOCAL_ADDRESS_CHANGED, 52 EX_TYPE_FILTER_SOCKET_REMOTE_ADDRESS_CHANGED, 53 }) 54 @Retention(RetentionPolicy.SOURCE) 55 public @interface ExceptionType {} 56 57 private static final String TAG = "QosCallbackException"; 58 59 // Types of exceptions supported // 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. 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} */ 73 public static final int EX_TYPE_FILTER_NOT_SUPPORTED = 3; 74 75 /** {@hide} */ 76 public static final int EX_TYPE_FILTER_SOCKET_LOCAL_ADDRESS_CHANGED = 4; 77 78 /** {@hide} */ 79 public static final int EX_TYPE_FILTER_SOCKET_NOT_CONNECTED = 5; 80 81 /** {@hide} */ 82 public static final int EX_TYPE_FILTER_SOCKET_REMOTE_ADDRESS_CHANGED = 6; 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 createException(@xceptionType final int type)91 public static QosCallbackException createException(@ExceptionType final int type) { 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()); 97 case EX_TYPE_FILTER_SOCKET_NOT_CONNECTED: 98 return new QosCallbackException(new SocketNotConnectedException()); 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()); 105 case EX_TYPE_FILTER_SOCKET_REMOTE_ADDRESS_CHANGED: 106 return new QosCallbackException( 107 new SocketRemoteAddressChangedException()); 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 115 @VisibleForTesting QosCallbackException(@onNull final String message)116 public QosCallbackException(@NonNull final String message) { 117 super(message); 118 } 119 120 @VisibleForTesting QosCallbackException(@onNull final Throwable cause)121 public QosCallbackException(@NonNull final Throwable cause) { 122 super(cause); 123 } 124 } 125