1 /*
2  * Copyright (C) 2021 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 com.android.telephony.qns;
18 
19 import android.util.Log;
20 
21 import java.lang.ref.WeakReference;
22 import java.util.concurrent.Executor;
23 
24 class ThresholdCallback {
25     static final String sLogTag = ThresholdCallback.class.getSimpleName();
26     IThresholdListener mCallback;
27 
init(Executor executor)28     void init(Executor executor) {
29         if (executor == null) throw new IllegalArgumentException("executor cannot be null");
30         mCallback = new ThresholdListener(executor, this);
31     }
32 
33     interface WifiThresholdListener {
onWifiThresholdChanged(Threshold[] thresholds)34         void onWifiThresholdChanged(Threshold[] thresholds);
35     }
36 
37     interface CellularThresholdListener {
onCellularThresholdChanged(Threshold[] thresholds)38         void onCellularThresholdChanged(Threshold[] thresholds);
39     }
40 
41     private static class ThresholdListener implements IThresholdListener {
42         private WeakReference<ThresholdCallback> mThresholdCallbackWeakRef;
43         private Executor mExecutor;
44 
ThresholdListener(Executor executor, ThresholdCallback callback)45         ThresholdListener(Executor executor, ThresholdCallback callback) {
46             mExecutor = executor;
47             mThresholdCallbackWeakRef = new WeakReference<>(callback);
48         }
49 
50         @Override
onWifiThresholdChanged(Threshold[] thresholds)51         public void onWifiThresholdChanged(Threshold[] thresholds) {
52             WifiThresholdListener listener =
53                     (WifiThresholdListener) mThresholdCallbackWeakRef.get();
54             if (listener == null) {
55                 Log.w(sLogTag, "Listener is null for wifi threshold notification");
56                 return;
57             }
58             mExecutor.execute(() -> listener.onWifiThresholdChanged(thresholds));
59         }
60 
61         @Override
onCellularThresholdChanged(Threshold[] thresholds)62         public void onCellularThresholdChanged(Threshold[] thresholds) {
63             CellularThresholdListener listener =
64                     (CellularThresholdListener) mThresholdCallbackWeakRef.get();
65             if (listener == null) {
66                 Log.w(sLogTag, "Listener is null for cellular threshold notification");
67                 return;
68             }
69             mExecutor.execute(() -> listener.onCellularThresholdChanged(thresholds));
70         }
71     }
72 }
73