Remi NGUYEN VAN | 070ff8b | 2021-03-01 17:53:45 +0900 | [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.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.telephony.data.EpsBearerQosSessionAttributes; |
| 22 | |
| 23 | import com.android.internal.annotations.VisibleForTesting; |
| 24 | |
| 25 | import java.util.Objects; |
| 26 | import java.util.concurrent.Executor; |
| 27 | |
| 28 | /** |
| 29 | * Sends messages from {@link com.android.server.ConnectivityService} to the registered |
| 30 | * {@link QosCallback}. |
| 31 | * <p/> |
| 32 | * This is a satellite class of {@link ConnectivityManager} and not meant |
| 33 | * to be used in other contexts. |
| 34 | * |
| 35 | * @hide |
| 36 | */ |
| 37 | class QosCallbackConnection extends android.net.IQosCallback.Stub { |
| 38 | |
| 39 | @NonNull private final ConnectivityManager mConnectivityManager; |
| 40 | @Nullable private volatile QosCallback mCallback; |
| 41 | @NonNull private final Executor mExecutor; |
| 42 | |
| 43 | @VisibleForTesting |
| 44 | @Nullable |
| 45 | public QosCallback getCallback() { |
| 46 | return mCallback; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * The constructor for the connection |
| 51 | * |
| 52 | * @param connectivityManager the mgr that created this connection |
| 53 | * @param callback the callback to send messages back to |
| 54 | * @param executor The executor on which the callback will be invoked. The provided |
| 55 | * {@link Executor} must run callback sequentially, otherwise the order of |
| 56 | * callbacks cannot be guaranteed. |
| 57 | */ |
| 58 | QosCallbackConnection(@NonNull final ConnectivityManager connectivityManager, |
| 59 | @NonNull final QosCallback callback, |
| 60 | @NonNull final Executor executor) { |
| 61 | mConnectivityManager = Objects.requireNonNull(connectivityManager, |
| 62 | "connectivityManager must be non-null"); |
| 63 | mCallback = Objects.requireNonNull(callback, "callback must be non-null"); |
| 64 | mExecutor = Objects.requireNonNull(executor, "executor must be non-null"); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Called when either the {@link EpsBearerQosSessionAttributes} has changed or on the first time |
| 69 | * the attributes have become available. |
| 70 | * |
| 71 | * @param session the session that is now available |
| 72 | * @param attributes the corresponding attributes of session |
| 73 | */ |
| 74 | @Override |
| 75 | public void onQosEpsBearerSessionAvailable(@NonNull final QosSession session, |
| 76 | @NonNull final EpsBearerQosSessionAttributes attributes) { |
| 77 | |
| 78 | mExecutor.execute(() -> { |
| 79 | final QosCallback callback = mCallback; |
| 80 | if (callback != null) { |
| 81 | callback.onQosSessionAvailable(session, attributes); |
| 82 | } |
| 83 | }); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Called when the session is lost. |
| 88 | * |
| 89 | * @param session the session that was lost |
| 90 | */ |
| 91 | @Override |
| 92 | public void onQosSessionLost(@NonNull final QosSession session) { |
| 93 | mExecutor.execute(() -> { |
| 94 | final QosCallback callback = mCallback; |
| 95 | if (callback != null) { |
| 96 | callback.onQosSessionLost(session); |
| 97 | } |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Called when there is an error on the registered callback. |
| 103 | * |
| 104 | * @param errorType the type of error |
| 105 | */ |
| 106 | @Override |
| 107 | public void onError(@QosCallbackException.ExceptionType final int errorType) { |
| 108 | mExecutor.execute(() -> { |
| 109 | final QosCallback callback = mCallback; |
| 110 | if (callback != null) { |
| 111 | // Messages no longer need to be received since there was an error. |
| 112 | stopReceivingMessages(); |
| 113 | mConnectivityManager.unregisterQosCallback(callback); |
| 114 | callback.onError(QosCallbackException.createException(errorType)); |
| 115 | } |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * The callback will stop receiving messages. |
| 121 | * <p/> |
| 122 | * There are no synchronization guarantees on exactly when the callback will stop receiving |
| 123 | * messages. |
| 124 | */ |
| 125 | void stopReceivingMessages() { |
| 126 | mCallback = null; |
| 127 | } |
| 128 | } |