aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/android/internal/telephony/satellite/SatelliteSOSMessageRecommender.java
blob: 25657b3d7c7ae3e6b461f76a00dc305e3b2c2058 (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
/*
 * Copyright (C) 2023 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.internal.telephony.satellite;

import static android.telephony.satellite.SatelliteManager.KEY_SATELLITE_COMMUNICATION_ALLOWED;
import static android.telephony.satellite.SatelliteManager.SATELLITE_ERROR_NONE;

import android.annotation.NonNull;
import android.os.AsyncResult;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.ResultReceiver;
import android.provider.DeviceConfig;
import android.telecom.Call;
import android.telecom.Connection;
import android.telephony.Rlog;
import android.telephony.ServiceState;
import android.telephony.SubscriptionManager;
import android.telephony.ims.ImsReasonInfo;
import android.telephony.ims.ImsRegistrationAttributes;
import android.telephony.ims.RegistrationManager;
import android.telephony.satellite.ISatelliteProvisionStateCallback;
import android.util.Pair;

import com.android.ims.ImsException;
import com.android.ims.ImsManager;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.metrics.SatelliteStats;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * This module is responsible for monitoring the cellular service state and IMS registration state
 * during an emergency call and notify Dialer when Telephony is not able to find any network and
 * the call likely will not get connected so that Dialer will prompt the user if they would like to
 * switch to satellite messaging.
 */
public class SatelliteSOSMessageRecommender extends Handler {
    private static final String TAG = "SatelliteSOSMessageRecommender";

    /**
     * Device config for the timeout duration in milliseconds to determine whether to recommend
     * Dialer to show the SOS button to users.
     * <p>
     * The timer is started when there is an ongoing emergency call, and the IMS is not registered,
     * and cellular service is not available. When the timer expires, SatelliteSOSMessageRecommender
     * will send the event EVENT_DISPLAY_SOS_MESSAGE to Dialer, which will then prompt user to
     * switch to using satellite SOS messaging.
     */
    public static final String EMERGENCY_CALL_TO_SOS_MSG_HYSTERESIS_TIMEOUT_MILLIS =
            "emergency_call_to_sos_msg_hysteresis_timeout_millis";
    /**
     * The default value of {@link #EMERGENCY_CALL_TO_SOS_MSG_HYSTERESIS_TIMEOUT_MILLIS} when it is
     * not provided in the device config.
     */
    public static final long DEFAULT_EMERGENCY_CALL_TO_SOS_MSG_HYSTERESIS_TIMEOUT_MILLIS = 20000;

    private static final int EVENT_EMERGENCY_CALL_STARTED = 1;
    protected static final int EVENT_CELLULAR_SERVICE_STATE_CHANGED = 2;
    private static final int EVENT_IMS_REGISTRATION_STATE_CHANGED = 3;
    protected static final int EVENT_TIME_OUT = 4;
    private static final int EVENT_SATELLITE_PROVISIONED_STATE_CHANGED = 5;
    private static final int EVENT_EMERGENCY_CALL_CONNECTION_STATE_CHANGED = 6;

    @NonNull
    private final SatelliteController mSatelliteController;
    private ImsManager mImsManager;

    private Connection mEmergencyConnection = null;
    /* The phone used for emergency call */
    private Phone mPhone = null;
    private final ISatelliteProvisionStateCallback mISatelliteProvisionStateCallback;
    @ServiceState.RegState
    private AtomicInteger mCellularServiceState = new AtomicInteger();
    private AtomicBoolean mIsImsRegistered = new AtomicBoolean();
    private AtomicBoolean mIsSatelliteAllowedInCurrentLocation = new AtomicBoolean();
    private final ResultReceiver mReceiverForRequestIsSatelliteAllowedForCurrentLocation;
    private final long mTimeoutMillis;
    protected int mCountOfTimerStarted = 0;

    private RegistrationManager.RegistrationCallback mImsRegistrationCallback =
            new RegistrationManager.RegistrationCallback() {
                @Override
                public void onRegistered(ImsRegistrationAttributes attributes) {
                    sendMessage(obtainMessage(EVENT_IMS_REGISTRATION_STATE_CHANGED, true));
                }

                @Override
                public void onUnregistered(ImsReasonInfo info) {
                    sendMessage(obtainMessage(EVENT_IMS_REGISTRATION_STATE_CHANGED, false));
                }
            };

    /**
     * Create an instance of SatelliteSOSMessageRecommender.
     *
     * @param looper The looper used with the handler of this class.
     */
    public SatelliteSOSMessageRecommender(@NonNull Looper looper) {
        this(looper, SatelliteController.getInstance(), null,
                getEmergencyCallToSosMsgHysteresisTimeoutMillis());
    }

    /**
     * Create an instance of SatelliteSOSMessageRecommender. This constructor should be used in
     * only unit tests.
     *
     * @param looper The looper used with the handler of this class.
     * @param satelliteController The SatelliteController singleton instance.
     * @param imsManager The ImsManager instance associated with the phone, which is used for making
     *                   the emergency call. This argument is not null only in unit tests.
     * @param timeoutMillis The timeout duration of the timer.
     */
    @VisibleForTesting
    protected SatelliteSOSMessageRecommender(@NonNull Looper looper,
            @NonNull SatelliteController satelliteController, ImsManager imsManager,
            long timeoutMillis) {
        super(looper);
        mSatelliteController = satelliteController;
        mImsManager = imsManager;
        mTimeoutMillis = timeoutMillis;
        mISatelliteProvisionStateCallback = new ISatelliteProvisionStateCallback.Stub() {
            @Override
            public void onSatelliteProvisionStateChanged(boolean provisioned) {
                logd("onSatelliteProvisionStateChanged: provisioned=" + provisioned);
                sendMessage(obtainMessage(EVENT_SATELLITE_PROVISIONED_STATE_CHANGED, provisioned));
            }
        };
        mReceiverForRequestIsSatelliteAllowedForCurrentLocation = new ResultReceiver(this) {
            @Override
            protected void onReceiveResult(int resultCode, Bundle resultData) {
                if (resultCode == SATELLITE_ERROR_NONE) {
                    if (resultData.containsKey(KEY_SATELLITE_COMMUNICATION_ALLOWED)) {
                        boolean isSatelliteCommunicationAllowed =
                                resultData.getBoolean(KEY_SATELLITE_COMMUNICATION_ALLOWED);
                        mIsSatelliteAllowedInCurrentLocation.set(isSatelliteCommunicationAllowed);
                        if (!isSatelliteCommunicationAllowed) {
                            logd("Satellite is not allowed for current location.");
                            cleanUpResources();
                        }
                    } else {
                        loge("KEY_SATELLITE_COMMUNICATION_ALLOWED does not exist.");
                        mIsSatelliteAllowedInCurrentLocation.set(false);
                        cleanUpResources();
                    }
                } else {
                    loge("requestIsSatelliteCommunicationAllowedForCurrentLocation() resultCode="
                            + resultCode);
                    mIsSatelliteAllowedInCurrentLocation.set(false);
                    cleanUpResources();
                }
            }
        };
    }

    @Override
    public void handleMessage(@NonNull Message msg) {
        switch (msg.what) {
            case EVENT_EMERGENCY_CALL_STARTED:
                handleEmergencyCallStartedEvent((Pair<Connection, Phone>) msg.obj);
                break;
            case EVENT_TIME_OUT:
                handleTimeoutEvent();
                break;
            case EVENT_SATELLITE_PROVISIONED_STATE_CHANGED:
                handleSatelliteProvisionStateChangedEvent((boolean) msg.obj);
                break;
            case EVENT_EMERGENCY_CALL_CONNECTION_STATE_CHANGED:
                handleEmergencyCallConnectionStateChangedEvent((Pair<String, Integer>) msg.obj);
                break;
            case EVENT_IMS_REGISTRATION_STATE_CHANGED:
                handleImsRegistrationStateChangedEvent((boolean) msg.obj);
                break;
            case EVENT_CELLULAR_SERVICE_STATE_CHANGED:
                AsyncResult ar = (AsyncResult) msg.obj;
                handleCellularServiceStateChangedEvent((ServiceState) ar.result);
                break;
            default:
                logd("handleMessage: unexpected message code: " + msg.what);
                break;
        }
    }

    /**
     * Inform SatelliteSOSMessageRecommender that an emergency call has just started.
     *
     * @param connection The connection created by TelephonyConnectionService for the emergency
     *                   call.
     * @param phone The phone used for the emergency call.
     */
    public void onEmergencyCallStarted(@NonNull Connection connection, @NonNull Phone phone) {
        if (!mSatelliteController.isSatelliteSupported()) {
            logd("onEmergencyCallStarted: satellite is not supported");
            return;
        }
        Pair<Connection, Phone> argument = new Pair<>(connection, phone);
        sendMessage(obtainMessage(EVENT_EMERGENCY_CALL_STARTED, argument));
    }

    /**
     * Inform SatelliteSOSMessageRecommender that the state of the emergency call connection has
     * changed.
     *
     * @param callId The ID of the emergency call.
     * @param state The connection state of the emergency call.
     */
    public void onEmergencyCallConnectionStateChanged(
            String callId, @Connection.ConnectionState int state) {
        Pair<String, Integer> argument = new Pair<>(callId, state);
        sendMessage(obtainMessage(EVENT_EMERGENCY_CALL_CONNECTION_STATE_CHANGED, argument));
    }

    private void handleEmergencyCallStartedEvent(@NonNull Pair<Connection, Phone> arg) {
        mSatelliteController.requestIsSatelliteCommunicationAllowedForCurrentLocation(
                SubscriptionManager.DEFAULT_SUBSCRIPTION_ID,
                mReceiverForRequestIsSatelliteAllowedForCurrentLocation);
        if (mPhone != null) {
            logd("handleEmergencyCallStartedEvent: new emergency call started while there is "
                    + " an ongoing call");
            unregisterForInterestedStateChangedEvents(mPhone);
        }
        mPhone = arg.second;
        mEmergencyConnection = arg.first;
        mCellularServiceState.set(mPhone.getServiceState().getState());
        mIsImsRegistered.set(mPhone.isImsRegistered());
        handleStateChangedEventForHysteresisTimer();
        registerForInterestedStateChangedEvents(mPhone);
    }

    private void handleSatelliteProvisionStateChangedEvent(boolean provisioned) {
        if (!provisioned) {
            cleanUpResources();
        }
    }

    private void handleTimeoutEvent() {
        boolean isDialerNotified = false;
        if (!mIsImsRegistered.get() && !isCellularAvailable()
                && mIsSatelliteAllowedInCurrentLocation.get()
                && mSatelliteController.isSatelliteProvisioned()
                && shouldTrackCall(mEmergencyConnection.getState())) {
            logd("handleTimeoutEvent: Sending EVENT_DISPLAY_SOS_MESSAGE to Dialer...");
            mEmergencyConnection.sendConnectionEvent(Call.EVENT_DISPLAY_SOS_MESSAGE, null);
            isDialerNotified = true;
        }
        reportEsosRecommenderDecision(isDialerNotified);
        cleanUpResources();
    }

    private void handleEmergencyCallConnectionStateChangedEvent(
            @NonNull Pair<String, Integer> arg) {
        if (mEmergencyConnection == null) {
            // Either the call was not created or the timer already timed out.
            return;
        }

        String callId = arg.first;
        int state = arg.second;
        if (!mEmergencyConnection.getTelecomCallId().equals(callId)) {
            loge("handleEmergencyCallConnectionStateChangedEvent: unexpected state changed event "
                    + ", mEmergencyConnection=" + mEmergencyConnection + ", callId=" + callId
                    + ", state=" + state);
            /**
             * TelephonyConnectionService sent us a connection state changed event for a call that
             * we're not tracking. There must be some unexpected things happened in
             * TelephonyConnectionService. Thus, we need to clean up the resources.
             */
            cleanUpResources();
            return;
        }

        if (!shouldTrackCall(state)) {
            reportEsosRecommenderDecision(false);
            cleanUpResources();
        }
    }

    private void handleImsRegistrationStateChangedEvent(boolean registered) {
        if (registered != mIsImsRegistered.get()) {
            mIsImsRegistered.set(registered);
            handleStateChangedEventForHysteresisTimer();
        }
    }

    private void handleCellularServiceStateChangedEvent(@NonNull ServiceState serviceState) {
        int state = serviceState.getState();
        if (mCellularServiceState.get() != state) {
            mCellularServiceState.set(state);
            handleStateChangedEventForHysteresisTimer();
        }
    }

    private void reportEsosRecommenderDecision(boolean isDialerNotified) {
        SatelliteStats.getInstance().onSatelliteSosMessageRecommender(
                new SatelliteStats.SatelliteSosMessageRecommenderParams.Builder()
                        .setDisplaySosMessageSent(isDialerNotified)
                        .setCountOfTimerStarted(mCountOfTimerStarted)
                        .setImsRegistered(mIsImsRegistered.get())
                        .setCellularServiceState(mCellularServiceState.get())
                        .build());
    }

    private void cleanUpResources() {
        stopTimer();
        if (mPhone != null) {
            unregisterForInterestedStateChangedEvents(mPhone);
            mPhone = null;
        }
        mEmergencyConnection = null;
        mCountOfTimerStarted = 0;
    }

    private void registerForInterestedStateChangedEvents(@NonNull Phone phone) {
        mSatelliteController.registerForSatelliteProvisionStateChanged(
                SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, mISatelliteProvisionStateCallback);
        phone.registerForServiceStateChanged(this, EVENT_CELLULAR_SERVICE_STATE_CHANGED, null);
        registerForImsRegistrationStateChanged(phone);
    }

    private void registerForImsRegistrationStateChanged(@NonNull Phone phone) {
        ImsManager imsManager = (mImsManager != null) ? mImsManager : ImsManager.getInstance(
                phone.getContext(), phone.getPhoneId());
        try {
            imsManager.addRegistrationCallback(mImsRegistrationCallback, this::post);
        } catch (ImsException ex) {
            loge("registerForImsRegistrationStateChanged: ex=" + ex);
        }
    }

    private void unregisterForInterestedStateChangedEvents(@NonNull Phone phone) {
        mSatelliteController.unregisterForSatelliteProvisionStateChanged(
                SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, mISatelliteProvisionStateCallback);
        phone.unregisterForServiceStateChanged(this);
        unregisterForImsRegistrationStateChanged(phone);
    }

    private void unregisterForImsRegistrationStateChanged(@NonNull Phone phone) {
        ImsManager imsManager = (mImsManager != null) ? mImsManager : ImsManager.getInstance(
                phone.getContext(), phone.getPhoneId());
        imsManager.removeRegistrationListener(mImsRegistrationCallback);
    }

    private boolean isCellularAvailable() {
        return (mCellularServiceState.get() == ServiceState.STATE_IN_SERVICE
                || mCellularServiceState.get() == ServiceState.STATE_EMERGENCY_ONLY);
    }

    private void handleStateChangedEventForHysteresisTimer() {
        if (!mIsImsRegistered.get() && !isCellularAvailable()) {
            startTimer();
        } else {
            stopTimer();
        }
    }

    private void startTimer() {
        if (hasMessages(EVENT_TIME_OUT)) {
            return;
        }
        sendMessageDelayed(obtainMessage(EVENT_TIME_OUT), mTimeoutMillis);
        mCountOfTimerStarted++;
    }

    private void stopTimer() {
        removeMessages(EVENT_TIME_OUT);
    }

    private static long getEmergencyCallToSosMsgHysteresisTimeoutMillis() {
        return DeviceConfig.getLong(DeviceConfig.NAMESPACE_TELEPHONY,
                EMERGENCY_CALL_TO_SOS_MSG_HYSTERESIS_TIMEOUT_MILLIS,
                DEFAULT_EMERGENCY_CALL_TO_SOS_MSG_HYSTERESIS_TIMEOUT_MILLIS);
    }

    private boolean shouldTrackCall(int connectionState) {
        /**
         * An active connection state means both parties are connected to the call and can actively
         * communicate. A disconnected connection state means the emergency call has ended. In both
         * cases, we don't need to track the call anymore.
         */
        return (connectionState != Connection.STATE_ACTIVE
                && connectionState != Connection.STATE_DISCONNECTED);
    }

    private static void logd(@NonNull String log) {
        Rlog.d(TAG, log);
    }

    private static void loge(@NonNull String log) {
        Rlog.e(TAG, log);
    }
}