aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/android/internal/telephony/satellite/PointingAppController.java
blob: 9dba6f209c5dde34c31b712770ef42981322dd97 (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
/*
 * 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 android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncResult;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.os.SystemProperties;
import android.telephony.Rlog;
import android.telephony.satellite.ISatelliteTransmissionUpdateCallback;
import android.telephony.satellite.PointingInfo;
import android.telephony.satellite.SatelliteManager;
import android.text.TextUtils;

import com.android.internal.R;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.telephony.Phone;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

/**
 * PointingApp controller to manage interactions with PointingUI app.
 */
public class PointingAppController {
    private static final String TAG = "PointingAppController";
    private static final String ALLOW_MOCK_MODEM_PROPERTY = "persist.radio.allow_mock_modem";
    private static final boolean DEBUG = !"user".equals(Build.TYPE);

    @NonNull
    private static PointingAppController sInstance;
    @NonNull private final Context mContext;
    private boolean mStartedSatelliteTransmissionUpdates;
    @NonNull private String mPointingUiPackageName = "";
    @NonNull private String mPointingUiClassName = "";

    /**
     * Map key: subId, value: SatelliteTransmissionUpdateHandler to notify registrants.
     */
    private final ConcurrentHashMap<Integer, SatelliteTransmissionUpdateHandler>
            mSatelliteTransmissionUpdateHandlers = new ConcurrentHashMap<>();

    /**
     * @return The singleton instance of PointingAppController.
     */
    public static PointingAppController getInstance() {
        if (sInstance == null) {
            loge("PointingAppController was not yet initialized.");
        }
        return sInstance;
    }

    /**
     * Create the PointingAppController singleton instance.
     * @param context The Context to use to create the PointingAppController.
     * @return The singleton instance of PointingAppController.
     */
    public static PointingAppController make(@NonNull Context context) {
        if (sInstance == null) {
            sInstance = new PointingAppController(context);
        }
        return sInstance;
    }

    /**
     * Create a PointingAppController to manage interactions with PointingUI app.
     *
     * @param context The Context for the PointingUIController.
     */
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
    public PointingAppController(@NonNull Context context) {
        mContext = context;
        mStartedSatelliteTransmissionUpdates = false;
    }

    /**
     * Set the flag mStartedSatelliteTransmissionUpdates to true or false based on the state of
     * transmission updates
     * @param startedSatelliteTransmissionUpdates boolean to set the flag
     */
    @VisibleForTesting
    public void setStartedSatelliteTransmissionUpdates(
            boolean startedSatelliteTransmissionUpdates) {
        mStartedSatelliteTransmissionUpdates = startedSatelliteTransmissionUpdates;
    }

    /**
     * Get the flag mStartedSatelliteTransmissionUpdates
     * @return returns mStartedSatelliteTransmissionUpdates
     */
    @VisibleForTesting
    public boolean getStartedSatelliteTransmissionUpdates() {
        return mStartedSatelliteTransmissionUpdates;
    }

    private static final class DatagramTransferStateHandlerRequest {
        public int datagramTransferState;
        public int pendingCount;
        public int errorCode;

        DatagramTransferStateHandlerRequest(int datagramTransferState, int pendingCount,
                int errorCode) {
            this.datagramTransferState = datagramTransferState;
            this.pendingCount = pendingCount;
            this.errorCode = errorCode;
        }
    }


    private static final class SatelliteTransmissionUpdateHandler extends Handler {
        public static final int EVENT_POSITION_INFO_CHANGED = 1;
        public static final int EVENT_SEND_DATAGRAM_STATE_CHANGED = 2;
        public static final int EVENT_RECEIVE_DATAGRAM_STATE_CHANGED = 3;
        public static final int EVENT_DATAGRAM_TRANSFER_STATE_CHANGED = 4;

        private final ConcurrentHashMap<IBinder, ISatelliteTransmissionUpdateCallback> mListeners;

        SatelliteTransmissionUpdateHandler(Looper looper) {
            super(looper);
            mListeners = new ConcurrentHashMap<>();
        }

        public void addListener(ISatelliteTransmissionUpdateCallback listener) {
            mListeners.put(listener.asBinder(), listener);
        }

        public void removeListener(ISatelliteTransmissionUpdateCallback listener) {
            mListeners.remove(listener.asBinder());
        }

        public boolean hasListeners() {
            return !mListeners.isEmpty();
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case EVENT_POSITION_INFO_CHANGED: {
                    AsyncResult ar = (AsyncResult) msg.obj;
                    PointingInfo pointingInfo = (PointingInfo) ar.result;
                    List<IBinder> toBeRemoved = new ArrayList<>();
                    mListeners.values().forEach(listener -> {
                        try {
                            listener.onSatellitePositionChanged(pointingInfo);
                        } catch (RemoteException e) {
                            logd("EVENT_POSITION_INFO_CHANGED RemoteException: " + e);
                            toBeRemoved.add(listener.asBinder());
                        }
                    });
                    toBeRemoved.forEach(listener -> {
                        mListeners.remove(listener);
                    });
                    break;
                }

                case EVENT_DATAGRAM_TRANSFER_STATE_CHANGED: {
                    AsyncResult ar = (AsyncResult) msg.obj;
                    logd("Receive EVENT_DATAGRAM_TRANSFER_STATE_CHANGED state=" + (int) ar.result);
                    break;
                }

                case EVENT_SEND_DATAGRAM_STATE_CHANGED: {
                    logd("Received EVENT_SEND_DATAGRAM_STATE_CHANGED");
                    DatagramTransferStateHandlerRequest request =
                            (DatagramTransferStateHandlerRequest) msg.obj;
                    List<IBinder> toBeRemoved = new ArrayList<>();
                    mListeners.values().forEach(listener -> {
                        try {
                            listener.onSendDatagramStateChanged(request.datagramTransferState,
                                    request.pendingCount, request.errorCode);
                        } catch (RemoteException e) {
                            logd("EVENT_SEND_DATAGRAM_STATE_CHANGED RemoteException: " + e);
                            toBeRemoved.add(listener.asBinder());
                        }
                    });
                    toBeRemoved.forEach(listener -> {
                        mListeners.remove(listener);
                    });
                    break;
                }

                case EVENT_RECEIVE_DATAGRAM_STATE_CHANGED: {
                    logd("Received EVENT_RECEIVE_DATAGRAM_STATE_CHANGED");
                    DatagramTransferStateHandlerRequest request =
                            (DatagramTransferStateHandlerRequest) msg.obj;
                    List<IBinder> toBeRemoved = new ArrayList<>();
                    mListeners.values().forEach(listener -> {
                        try {
                            listener.onReceiveDatagramStateChanged(request.datagramTransferState,
                                    request.pendingCount, request.errorCode);
                        } catch (RemoteException e) {
                            logd("EVENT_RECEIVE_DATAGRAM_STATE_CHANGED RemoteException: " + e);
                            toBeRemoved.add(listener.asBinder());
                        }
                    });
                    toBeRemoved.forEach(listener -> {
                        mListeners.remove(listener);
                    });
                    break;
                }

                default:
                    loge("SatelliteTransmissionUpdateHandler unknown event: " + msg.what);
            }
        }
    }

    /**
     * Register to start receiving updates for satellite position and datagram transfer state
     * @param subId The subId of the subscription to register for receiving the updates.
     * @param callback The callback to notify of satellite transmission updates.
     * @param phone The Phone object to unregister for receiving the updates.
     */
    public void registerForSatelliteTransmissionUpdates(int subId,
            ISatelliteTransmissionUpdateCallback callback, Phone phone) {
        SatelliteTransmissionUpdateHandler handler =
                mSatelliteTransmissionUpdateHandlers.get(subId);
        if (handler != null) {
            handler.addListener(callback);
            return;
        } else {
            handler = new SatelliteTransmissionUpdateHandler(Looper.getMainLooper());
            handler.addListener(callback);
            mSatelliteTransmissionUpdateHandlers.put(subId, handler);
            if (SatelliteModemInterface.getInstance().isSatelliteServiceSupported()) {
                SatelliteModemInterface.getInstance().registerForSatellitePositionInfoChanged(
                        handler, SatelliteTransmissionUpdateHandler.EVENT_POSITION_INFO_CHANGED,
                        null);
                SatelliteModemInterface.getInstance().registerForDatagramTransferStateChanged(
                        handler,
                        SatelliteTransmissionUpdateHandler.EVENT_DATAGRAM_TRANSFER_STATE_CHANGED,
                        null);
            } else {
                phone.registerForSatellitePositionInfoChanged(handler,
                        SatelliteTransmissionUpdateHandler.EVENT_POSITION_INFO_CHANGED, null);
            }
        }
    }

    /**
     * Unregister to stop receiving updates on satellite position and datagram transfer state
     * If the callback was not registered before, it is ignored
     * @param subId The subId of the subscription to unregister for receiving the updates.
     * @param result The callback to get the error code in case of failure
     * @param callback The callback that was passed to {@link
     * #registerForSatelliteTransmissionUpdates(int, ISatelliteTransmissionUpdateCallback, Phone)}.
     * @param phone The Phone object to unregister for receiving the updates
     */
    public void unregisterForSatelliteTransmissionUpdates(int subId, Consumer<Integer> result,
            ISatelliteTransmissionUpdateCallback callback, Phone phone) {
        SatelliteTransmissionUpdateHandler handler =
                mSatelliteTransmissionUpdateHandlers.get(subId);
        if (handler != null) {
            handler.removeListener(callback);
            if (handler.hasListeners()) {
                result.accept(SatelliteManager.SATELLITE_ERROR_NONE);
                return;
            }

            mSatelliteTransmissionUpdateHandlers.remove(subId);
            if (SatelliteModemInterface.getInstance().isSatelliteServiceSupported()) {
                SatelliteModemInterface.getInstance().unregisterForSatellitePositionInfoChanged(
                        handler);
                SatelliteModemInterface.getInstance().unregisterForDatagramTransferStateChanged(
                        handler);
            } else {
                if (phone == null) {
                    result.accept(SatelliteManager.SATELLITE_INVALID_TELEPHONY_STATE);
                    return;
                }
                phone.unregisterForSatellitePositionInfoChanged(handler);
            }
        }
    }

    /**
     * Start receiving satellite trasmission updates.
     * This can be called by the pointing UI when the user starts pointing to the satellite.
     * Modem should continue to report the pointing input as the device or satellite moves.
     * The transmission updates will be received via
     * {@link android.telephony.satellite.SatelliteTransmissionUpdateCallback
     * #onSatellitePositionChanged(pointingInfo)}.
     */
    public void startSatelliteTransmissionUpdates(@NonNull Message message, @Nullable Phone phone) {
        if (mStartedSatelliteTransmissionUpdates) {
            logd("startSatelliteTransmissionUpdates: already started");
            AsyncResult.forMessage(message, null, new SatelliteManager.SatelliteException(
                    SatelliteManager.SATELLITE_ERROR_NONE));
            message.sendToTarget();
            return;
        }
        if (SatelliteModemInterface.getInstance().isSatelliteServiceSupported()) {
            SatelliteModemInterface.getInstance().startSendingSatellitePointingInfo(message);
            mStartedSatelliteTransmissionUpdates = true;
            return;
        }
        if (phone != null) {
            phone.startSatellitePositionUpdates(message);
            mStartedSatelliteTransmissionUpdates = true;
        } else {
            loge("startSatelliteTransmissionUpdates: No phone object");
            AsyncResult.forMessage(message, null, new SatelliteManager.SatelliteException(
                    SatelliteManager.SATELLITE_INVALID_TELEPHONY_STATE));
            message.sendToTarget();
        }
    }

    /**
     * Stop receiving satellite transmission updates.
     * Reset the flag mStartedSatelliteTransmissionUpdates
     * This can be called by the pointing UI when the user stops pointing to the satellite.
     */
    public void stopSatelliteTransmissionUpdates(@NonNull Message message, @Nullable Phone phone) {
        setStartedSatelliteTransmissionUpdates(false);
        if (SatelliteModemInterface.getInstance().isSatelliteServiceSupported()) {
            SatelliteModemInterface.getInstance().stopSendingSatellitePointingInfo(message);
            return;
        }
        if (phone != null) {
            phone.stopSatellitePositionUpdates(message);
        } else {
            loge("startSatelliteTransmissionUpdates: No phone object");
            AsyncResult.forMessage(message, null, new SatelliteManager.SatelliteException(
                    SatelliteManager.SATELLITE_INVALID_TELEPHONY_STATE));
            message.sendToTarget();
        }
    }

    /**
     * Check if Pointing is needed and Launch Pointing UI
     * @param needFullScreenPointingUI if pointing UI has to be launchd with Full screen
     */
    public void startPointingUI(boolean needFullScreenPointingUI) {
        String packageName = getPointingUiPackageName();
        if (TextUtils.isEmpty(packageName)) {
            logd("startPointingUI: config_pointing_ui_package is not set. Ignore the request");
            return;
        }

        Intent launchIntent;
        String className = getPointingUiClassName();
        if (!TextUtils.isEmpty(className)) {
            launchIntent = new Intent()
                    .setComponent(new ComponentName(packageName, className))
                    .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        } else {
            launchIntent = mContext.getPackageManager().getLaunchIntentForPackage(packageName);
        }
        if (launchIntent == null) {
            loge("startPointingUI: launchIntent is null");
            return;
        }
        launchIntent.putExtra("needFullScreen", needFullScreenPointingUI);

        try {
            mContext.startActivity(launchIntent);
        } catch (ActivityNotFoundException ex) {
            loge("startPointingUI: Pointing UI app activity is not found, ex=" + ex);
        }
    }

    public void updateSendDatagramTransferState(int subId,
            @SatelliteManager.SatelliteDatagramTransferState int datagramTransferState,
            int sendPendingCount, int errorCode) {
        DatagramTransferStateHandlerRequest request = new DatagramTransferStateHandlerRequest(
                datagramTransferState, sendPendingCount, errorCode);
        SatelliteTransmissionUpdateHandler handler =
                mSatelliteTransmissionUpdateHandlers.get(subId);

        if (handler != null) {
            Message msg = handler.obtainMessage(
                    SatelliteTransmissionUpdateHandler.EVENT_SEND_DATAGRAM_STATE_CHANGED,
                    request);
            msg.sendToTarget();
        } else {
            loge("SatelliteTransmissionUpdateHandler not found for subId: " + subId);
        }
    }

    public void updateReceiveDatagramTransferState(int subId,
            @SatelliteManager.SatelliteDatagramTransferState int datagramTransferState,
            int receivePendingCount, int errorCode) {
        DatagramTransferStateHandlerRequest request = new DatagramTransferStateHandlerRequest(
                datagramTransferState, receivePendingCount, errorCode);
        SatelliteTransmissionUpdateHandler handler =
                mSatelliteTransmissionUpdateHandlers.get(subId);

        if (handler != null) {
            Message msg = handler.obtainMessage(
                    SatelliteTransmissionUpdateHandler.EVENT_RECEIVE_DATAGRAM_STATE_CHANGED,
                    request);
            msg.sendToTarget();
        } else {
            loge(" SatelliteTransmissionUpdateHandler not found for subId: " + subId);
        }
    }

    /**
     * This API can be used by only CTS to update satellite pointing UI app package and class names.
     *
     * @param packageName The package name of the satellite pointing UI app.
     * @param className The class name of the satellite pointing UI app.
     * @return {@code true} if the satellite pointing UI app package and class is set successfully,
     * {@code false} otherwise.
     */
    boolean setSatellitePointingUiClassName(
            @Nullable String packageName, @Nullable String className) {
        if (!isMockModemAllowed()) {
            loge("setSatellitePointingUiClassName: modifying satellite pointing UI package and "
                    + "class name is not allowed");
            return false;
        }

        logd("setSatellitePointingUiClassName: config_pointing_ui_package is updated, new "
                + "packageName=" + packageName
                + ", config_pointing_ui_class new className=" + className);

        if (packageName == null || packageName.equals("null")) {
            mPointingUiPackageName = "";
            mPointingUiClassName = "";
        } else {
            mPointingUiPackageName = packageName;
            if (className == null || className.equals("null")) {
                mPointingUiClassName = "";
            } else {
                mPointingUiClassName = className;
            }
        }

        return true;
    }

    @NonNull private String getPointingUiPackageName() {
        if (!TextUtils.isEmpty(mPointingUiPackageName)) {
            return mPointingUiPackageName;
        }
        return TextUtils.emptyIfNull(mContext.getResources().getString(
                R.string.config_pointing_ui_package));
    }

    @NonNull private String getPointingUiClassName() {
        if (!TextUtils.isEmpty(mPointingUiClassName)) {
            return mPointingUiClassName;
        }
        return TextUtils.emptyIfNull(mContext.getResources().getString(
                R.string.config_pointing_ui_class));
    }

    private boolean isMockModemAllowed() {
        return (DEBUG || SystemProperties.getBoolean(ALLOW_MOCK_MODEM_PROPERTY, false));
    }

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

    private static void loge(@NonNull String log) {
        Rlog.e(TAG, log);
    }
    /**
     * TODO: The following needs to be added in this class:
     * - check if pointingUI crashes - then restart it
     */
}