1 /*
2  * Copyright (C) 2023 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.telephony.cts.externalsatellitegatewayservice;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.app.Service;
22 import android.content.Intent;
23 import android.os.IBinder;
24 import android.os.RemoteException;
25 import android.util.Log;
26 
27 import java.util.concurrent.atomic.AtomicBoolean;
28 
29 /**
30  * A mock SatelliteGatewayService that is used for testing service crash scenarios.
31  */
32 public class ExternalMockSatelliteGatewayService extends Service {
33     private static final String TAG = "ExternalMockSatelliteGatewayService";
34     private static final String SERVICE_INTERFACE =
35             "android.telephony.satellite.SatelliteGatewayService";
36 
37     private final IBinder mTelephonyBinder = new ISatelliteGateway.Stub() {};
38     private final AtomicBoolean mIsBound = new AtomicBoolean(false);
39     private final CtsConnection mCtsBinder = new CtsConnection();
40     @Nullable private IExternalSatelliteGatewayListener mExternalListener;
41 
42     // For CTS to access this Service.
43     public class CtsConnection extends IExternalMockSatelliteGatewayService.Stub {
setExternalSatelliteGatewayListener( @onNull IExternalSatelliteGatewayListener listener)44         public void setExternalSatelliteGatewayListener(
45                 @NonNull IExternalSatelliteGatewayListener listener) {
46             logd("setExternalSatelliteGatewayListener: listener=" + listener);
47             mExternalListener = listener;
48             if (mIsBound.get()) {
49                 notifyTelephonyServiceBound();
50             }
51         }
52     }
53 
54     @Override
onBind(Intent intent)55     public IBinder onBind(Intent intent) {
56         if (SERVICE_INTERFACE.equals(intent.getAction())) {
57             logd("Telephony service bound");
58             mIsBound.set(true);
59             notifyTelephonyServiceBound();
60             return mTelephonyBinder;
61         }
62         logd("CTS service bound");
63         return mCtsBinder;
64     }
65 
notifyTelephonyServiceBound()66     private void notifyTelephonyServiceBound() {
67         logd("notifyTelephonyServiceBound");
68         if (mExternalListener != null) {
69             try {
70                 mExternalListener.onRemoteServiceConnected();
71             } catch (RemoteException e) {
72                 loge("notifyTelephonyServiceBound: e=" + e);
73             }
74         } else {
75             logd("notifyTelephonyServiceBound: mExternalListener is null. Waiting for CTS to bind");
76         }
77     }
78 
79     /**
80      * Log the message to the radio buffer with {@code DEBUG} priority.
81      *
82      * @param log The message to log.
83      */
logd(@onNull String log)84     private static void logd(@NonNull String log) {
85         Log.d(TAG, log);
86     }
87 
88     /**
89      * Log with error attribute
90      *
91      * @param s is string log
92      */
loge(@onNull String s)93     private static void loge(@NonNull String s) {
94         Log.e(TAG, s);
95     }
96 }
97