summaryrefslogtreecommitdiff
path: root/services/QualifiedNetworksService/src/com/android/telephony/qns/AlternativeEventListener.java
blob: 36fdaa45d8076e06e457fb75e9e459dd47665a31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.telephony.qns;

import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_ACTIVE;
import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_ALERTING;
import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_DIALING;
import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_DISCONNECTED;
import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_HOLDING;
import static android.telephony.PreciseCallState.PRECISE_CALL_STATE_IDLE;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.net.NetworkCapabilities;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.telephony.Annotation;
import android.telephony.PreciseDataConnectionState;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.util.SparseArray;

import com.android.internal.annotations.VisibleForTesting;

class AlternativeEventListener {
    private static final int EVENT_SRVCC_STATE_CHANGED = 13001;
    private static final SparseArray<AlternativeEventListener> sAlternativeInterfaceManager =
            new SparseArray<>();
    private final String mLogTag;
    private Context mContext;
    private int mSlotIndex;
    private QnsTelephonyListener mQnsTelephonyListener;
    private QnsRegistrant mCallTypeChangedEventListener;
    private QnsRegistrant mEmergencyCallTypeChangedEventListener;
    private QnsRegistrant mEmergencyPreferredTransportTypeChanged;
    private QnsRegistrant mTryWfcConnectionState;
    private QnsRegistrant mLowRtpQuallityListener;
    private QnsRegistrant mEmcLowRtpQuallityListener;
    private AlternativeEventCb mEventCb;
    private AlternativeEventProvider mEventProvider;
    private Handler mHandler;
    private CallInfoManager mCallInfoManager = new CallInfoManager();

    class CallInfo {
        int mId;
        int mType;
        int mState;

        CallInfo(int id, int type, int state) {
            mId = id;
            mType = type;
            mState = state;
        }
    }

    /** Manager of CallInfo list. */
    private class CallInfoManager {
        private SparseArray<CallInfo> mCallInfos = new SparseArray<>();
        QnsCarrierConfigManager.RtpMetricsConfig mRtpMetricsConfig;
        int mEmergencyCallState = PRECISE_CALL_STATE_IDLE;
        int mLastReportedCallType = QnsConstants.CALL_TYPE_IDLE;

        CallInfoManager() {
            mRtpMetricsConfig = null;
        }

        void updateCallInfo(
                int id,
                @QnsConstants.QnsCallType int type,
                @Annotation.PreciseCallStates int state) {
            if (mRtpMetricsConfig != null && state == PRECISE_CALL_STATE_ACTIVE) {
                requestRtpThreshold(mRtpMetricsConfig);
            }
            if (type == QnsConstants.CALL_TYPE_EMERGENCY) {
                mEmergencyCallState = state;
                return;
            }
            if (type == QnsConstants.CALL_TYPE_VIDEO || type == QnsConstants.CALL_TYPE_VOICE) {
                CallInfo info = mCallInfos.get(id);
                if (info == null) {
                    if (state == PRECISE_CALL_STATE_ACTIVE) {
                        mCallInfos.put(id, new CallInfo(id, type, state));
                        log("add callinfo with id " + id);
                    }
                } else {
                    if (info.mType != type) {
                        log("CallId[" + info.mId + "] type changed " + info.mType + " > " + id);
                        info.mType = type;
                    }
                    if (state == PRECISE_CALL_STATE_HOLDING) {
                        log("CallId[" + info.mId + "] changed to HOLDING ");
                        info.mState = state;
                    } else if (state == PRECISE_CALL_STATE_ACTIVE) {
                        log("CallId[" + info.mId + "] changed to ACTIVE ");
                        info.mState = state;
                    } else if (state == PRECISE_CALL_STATE_DISCONNECTED) {
                        log("delete callInfo callId:" + id);
                        mCallInfos.remove(id);
                    }
                }
            }
        }

        boolean isCallIdle() {
            return mCallInfos.size() == 0;
        }

        CallInfo getActiveCallInfo() {
            for (int i = 0; i < mCallInfos.size(); i++) {
                int key = mCallInfos.keyAt(i);
                if (mCallInfos.get(key).mState == PRECISE_CALL_STATE_ACTIVE) {
                    return mCallInfos.get(key);
                }
            }
            return null;
        }

        void clearCallInfo() {
            mCallInfos.clear();
        }

        boolean hasVideoCall() {
            for (int i = 0; i < mCallInfos.size(); i++) {
                int key = mCallInfos.keyAt(i);
                if (mCallInfos.get(key).mType == QnsConstants.CALL_TYPE_VIDEO) {
                    return true;
                }
            }
            return false;
        }

        void requestRtpThreshold(QnsCarrierConfigManager.RtpMetricsConfig config) {
            if (config != null) {
                mRtpMetricsConfig =
                        new QnsCarrierConfigManager.RtpMetricsConfig(
                                config.mJitter,
                                config.mPktLossRate,
                                config.mPktLossTime,
                                config.mNoRtpInterval);
            }
            CallInfo info = getActiveCallInfo();
            if (mEventProvider != null && info != null) {
                log("requestRtpThreshold callId:" + info.mId + " " + config.toString());
                mEventProvider.requestRtpThreshold(
                        info.mId,
                        mRtpMetricsConfig.mJitter,
                        mRtpMetricsConfig.mPktLossRate,
                        mRtpMetricsConfig.mPktLossTime,
                        mRtpMetricsConfig.mNoRtpInterval);
            }
        }
    }

    class MessageHandler extends Handler {
        MessageHandler(Looper l) {
            super(l);
        }

        @Override
        public void handleMessage(Message message) {
            Log.d(mLogTag, "handleMessage msg=" + message.what);
            QnsAsyncResult ar = (QnsAsyncResult) message.obj;
            int state = (int) ar.mResult;
            switch (message.what) {
                case EVENT_SRVCC_STATE_CHANGED:
                    onSrvccStateChanged(state);
                    break;
                default:
                    Log.d(mLogTag, "Unknown message received!");
                    break;
            }
        }
    }

    /**
     * AlternativeEventListener constructor.
     */
    AlternativeEventListener(
            Context context, QnsTelephonyListener qnsTelephonyListener, int slotId) {
        mSlotIndex = slotId;
        mLogTag =
                QnsConstants.QNS_TAG
                        + "_"
                        + AlternativeEventListener.class.getSimpleName()
                        + "_"
                        + mSlotIndex;
        mContext = context;
        mQnsTelephonyListener = qnsTelephonyListener;
        mHandler = new AlternativeEventListener.MessageHandler(mContext.getMainLooper());
        mEventCb = new AlternativeEventCb();
    }

    /**
     * register emergency preferred transport type changed event.
     *
     * @param h Handler want to receive event.
     * @param what event Id to receive
     * @param userObj user object
     */
    void registerEmergencyPreferredTransportTypeChanged(
            @NonNull Handler h, int what, Object userObj) {
        mEmergencyPreferredTransportTypeChanged = new QnsRegistrant(h, what, userObj);
    }

    /** Unregister emergency preferred transport type changed event. */
    void unregisterEmergencyPreferredTransportTypeChanged() {
        mEmergencyPreferredTransportTypeChanged = null;
    }

    /**
     * register try WFC connection state change event.
     *
     * @param h Handler want to receive event
     * @param what event Id to receive
     * @param userObj user object
     */
    void registerTryWfcConnectionStateListener(@NonNull Handler h, int what, Object userObj) {
        mTryWfcConnectionState = new QnsRegistrant(h, what, userObj);
    }

    /**
     * Register low RTP quality event.
     *
     * @param netCapability Network Capability
     * @param h Handler want to receive event.
     * @param what event Id to receive
     * @param userObj user object
     */
    void registerLowRtpQualityEvent(
            int netCapability,
            @NonNull Handler h,
            int what,
            Object userObj,
            QnsCarrierConfigManager.RtpMetricsConfig config) {
        if (h != null) {
            QnsRegistrant r = new QnsRegistrant(h, what, userObj);
            if (netCapability == NetworkCapabilities.NET_CAPABILITY_IMS) {
                mLowRtpQuallityListener = r;
            } else if (netCapability == NetworkCapabilities.NET_CAPABILITY_EIMS) {
                mEmcLowRtpQuallityListener = r;
            }
            if (mEventProvider != null) {
                mCallInfoManager.requestRtpThreshold(config);
            }
        }
    }

    /**
     * Unregister low RTP quality event.
     *
     * @param netCapability Network Capability
     * @param h Handler want to receive event.
     */
    void unregisterLowRtpQualityEvent(int netCapability, @NonNull Handler h) {
        if (netCapability == NetworkCapabilities.NET_CAPABILITY_IMS) {
            mLowRtpQuallityListener = null;
        } else if (netCapability == NetworkCapabilities.NET_CAPABILITY_EIMS) {
            mEmcLowRtpQuallityListener = null;
        }
        mCallInfoManager.mRtpMetricsConfig = null;
    }

    /**
     * register call type changed event.
     *
     * @param netCapability Network Capability of caller
     * @param h Handler want to receive event.
     * @param what event Id to receive
     * @param userObj user object
     */
    void registerCallTypeChangedListener(
            int netCapability, @NonNull Handler h, int what, Object userObj) {
        if (netCapability != NetworkCapabilities.NET_CAPABILITY_IMS
                && netCapability != NetworkCapabilities.NET_CAPABILITY_EIMS) {
            log("registerCallTypeChangedListener : wrong netCapability");
            return;
        }
        if (h != null) {
            QnsRegistrant r = new QnsRegistrant(h, what, userObj);
            if (netCapability == NetworkCapabilities.NET_CAPABILITY_IMS) {
                mCallTypeChangedEventListener = r;
                mQnsTelephonyListener.registerSrvccStateListener(
                        mHandler, EVENT_SRVCC_STATE_CHANGED, null);
            } else if (netCapability == NetworkCapabilities.NET_CAPABILITY_EIMS) {
                mEmergencyCallTypeChangedEventListener = r;
            }
        } else {
            log("registerCallTypeChangedListener : Handler is Null");
        }
    }

    /**
     * Unregister call type changed event.
     *
     * @param netCapability Network Capability of caller
     * @param h Handler want to receive event.
     */
    void unregisterCallTypeChangedListener(int netCapability, @NonNull Handler h) {
        if (netCapability != NetworkCapabilities.NET_CAPABILITY_IMS
                && netCapability != NetworkCapabilities.NET_CAPABILITY_EIMS) {
            log("unregisterCallTypeChangedListener : wrong netCapability");
            return;
        }
        if (h != null) {
            if (netCapability == NetworkCapabilities.NET_CAPABILITY_IMS) {
                mCallTypeChangedEventListener = null;
                mQnsTelephonyListener.unregisterSrvccStateChanged(mHandler);
            } else if (netCapability == NetworkCapabilities.NET_CAPABILITY_EIMS) {
                mEmergencyCallTypeChangedEventListener = null;
            }
        } else {
            log("unregisterCallTypeChangedListener : Handler is Null");
        }
    }

    /**
     * register EventProvider to EventListener
     *
     * @param provider extends of AlternativeEventProvider
     */
    void setEventProvider(AlternativeEventProvider provider) {
        log("setEventProvider provider " + provider);
        mEventProvider = provider;
        provider.registerCallBack(mEventCb);
    }

    @VisibleForTesting
    void onSrvccStateChanged(int srvccState) {
        if (srvccState == TelephonyManager.SRVCC_STATE_HANDOVER_COMPLETED) {
            mCallInfoManager.clearCallInfo();
            int callType = QnsConstants.CALL_TYPE_IDLE;
            mCallTypeChangedEventListener.notifyResult(callType);
        }
    }

    void clearNormalCallInfo() {
        mCallInfoManager.clearCallInfo();
        mCallInfoManager.mLastReportedCallType = QnsConstants.CALL_TYPE_IDLE;
        unregisterLowRtpQualityEvent(NetworkCapabilities.NET_CAPABILITY_IMS, null);
    }

    @VisibleForTesting
    void notifyRtpLowQuality(int callType, int reason) {
        if (callType == QnsConstants.CALL_TYPE_VOICE) {
            if (mLowRtpQuallityListener != null) {
                mLowRtpQuallityListener.notifyResult(reason);
            } else {
                log("notifyRtpLowQuality mLowRtpQuallityListener is null.");
            }
        } else if (callType == QnsConstants.CALL_TYPE_EMERGENCY) {
            if (mEmcLowRtpQuallityListener != null) {
                mEmcLowRtpQuallityListener.notifyResult(reason);
            } else {
                log("notifyRtpLowQuality mEmcLowRtpQuallityListener is null.");
            }
            if (mCallInfoManager.mLastReportedCallType == QnsConstants.CALL_TYPE_EMERGENCY) {
                if (mLowRtpQuallityListener != null) {
                    log("notifyRtpLowQuality for emergency call to IMS ANE");
                    mLowRtpQuallityListener.notifyResult(reason);
                } else {
                    log("notifyRtpLowQuality mLowRtpQuallityListener is null.");
                }
            }
        }
    }

    class AlternativeEventCb implements AlternativeEventProvider.EventCallback {
        @Override
        public void onCallInfoChanged(
                int id,
                @QnsConstants.QnsCallType int type,
                @Annotation.PreciseCallStates int state) {
            log("onCallInfoChanged callId" + id + "  type" + type + "  state" + state);

            mCallInfoManager.updateCallInfo(id, type, state);
            if (type == QnsConstants.CALL_TYPE_EMERGENCY) {
                if (mEmergencyCallTypeChangedEventListener != null) {
                    if (state == PRECISE_CALL_STATE_DISCONNECTED) {
                        mEmergencyCallTypeChangedEventListener.notifyResult(
                                QnsConstants.CALL_TYPE_IDLE);
                    } else if (state == PRECISE_CALL_STATE_ACTIVE) {
                        mEmergencyCallTypeChangedEventListener.notifyResult(
                                QnsConstants.CALL_TYPE_EMERGENCY);
                    }
                }
                if (mCallTypeChangedEventListener != null) {
                    if ((state == PRECISE_CALL_STATE_ACTIVE
                                    || state == PRECISE_CALL_STATE_DIALING
                                    || state == PRECISE_CALL_STATE_ALERTING)
                            && !isDataNetworkConnected(NetworkCapabilities.NET_CAPABILITY_EIMS)
                            && isDataNetworkConnected(NetworkCapabilities.NET_CAPABILITY_IMS)) {
                        log("Emergency call is progressing without emergency PDN");
                        if (mCallInfoManager.mLastReportedCallType
                                != QnsConstants.CALL_TYPE_EMERGENCY) {
                            mCallTypeChangedEventListener.notifyResult(
                                    QnsConstants.CALL_TYPE_EMERGENCY);
                            mCallInfoManager.mLastReportedCallType =
                                    QnsConstants.CALL_TYPE_EMERGENCY;
                        }
                    } else if (state == PRECISE_CALL_STATE_DISCONNECTED) {
                        log("Emergency call disconnected");
                        if (mCallInfoManager.mLastReportedCallType
                                == QnsConstants.CALL_TYPE_EMERGENCY) {
                            mCallTypeChangedEventListener.notifyResult(QnsConstants.CALL_TYPE_IDLE);
                            mCallInfoManager.mLastReportedCallType = QnsConstants.CALL_TYPE_IDLE;
                        }
                    }
                }
                return;
            }
            if (mCallTypeChangedEventListener != null) {
                int callType = QnsConstants.CALL_TYPE_IDLE;
                if (mCallInfoManager.isCallIdle()) {
                    callType = QnsConstants.CALL_TYPE_IDLE;
                } else if (mCallInfoManager.hasVideoCall()) {
                    callType = QnsConstants.CALL_TYPE_VIDEO;
                } else {
                    callType = QnsConstants.CALL_TYPE_VOICE;
                }
                if (mCallTypeChangedEventListener != null
                        && mCallInfoManager.mLastReportedCallType != callType) {
                    mCallTypeChangedEventListener.notifyResult(callType);
                    mCallInfoManager.mLastReportedCallType = callType;
                }
            }
        }

        @Override
        public void onVoiceRtpLowQuality(@QnsConstants.RtpLowQualityReason int reason) {
            if (mCallInfoManager.mEmergencyCallState == PRECISE_CALL_STATE_ACTIVE) {
                notifyRtpLowQuality(QnsConstants.CALL_TYPE_EMERGENCY, reason);
            } else if (!mCallInfoManager.isCallIdle() && !mCallInfoManager.hasVideoCall()) {
                notifyRtpLowQuality(QnsConstants.CALL_TYPE_VOICE, reason);
            }
        }

        @Override
        public void onEmergencyPreferenceChanged(int preferredTransportType) {
            if (mEmergencyPreferredTransportTypeChanged != null) {
                mEmergencyPreferredTransportTypeChanged.notifyResult(preferredTransportType);
            }
        }

        @Override
        public void onTryWfcConnectionStateChanged(boolean isEnabled) {
            mTryWfcConnectionState.notifyResult(isEnabled);
        }
    }

    protected boolean isIdleState() {
        return mCallInfoManager.isCallIdle();
    }

    protected void setEcnoSignalThreshold(@Nullable int[] threshold) {
        if (mEventProvider != null) {
            mEventProvider.setEcnoSignalThreshold(threshold);
        }
    }

    private boolean isDataNetworkConnected(int netCapability) {
        PreciseDataConnectionState preciseDataStatus =
                mQnsTelephonyListener.getLastPreciseDataConnectionState(netCapability);

        if (preciseDataStatus == null) return false;
        int state = preciseDataStatus.getState();
        return (state == TelephonyManager.DATA_CONNECTED
                || state == TelephonyManager.DATA_HANDOVER_IN_PROGRESS
                || state == TelephonyManager.DATA_SUSPENDED);
    }

    @VisibleForTesting
    protected void close() {
        mCallTypeChangedEventListener = null;
        mEmergencyCallTypeChangedEventListener = null;
        mEmergencyPreferredTransportTypeChanged = null;
        mTryWfcConnectionState = null;
        mLowRtpQuallityListener = null;
        mEmcLowRtpQuallityListener = null;
        sAlternativeInterfaceManager.remove(mSlotIndex);
    }

    protected void log(String s) {
        Log.d(mLogTag, s);
    }
}