summaryrefslogtreecommitdiff
path: root/tests/tests/telephony/current/src/android/telephony/cts/MockModemServiceConnector.java
blob: 039a1e1feb3fd05496ca7bdc988dfd96a043f94a (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
/*
 * 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 android.telephony.cts;

import android.app.Instrumentation;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/** Connects Telephony Framework to MockModemService. */
class MockModemServiceConnector {

    private static final String TAG = "MockModemServiceConnector";

    private static final String DEFAULT_SERVICE_NAME = MockModemService.class.getClass().getName();
    private static final String COMMAND_BASE = "cmd phone ";
    private static final String COMMAND_SET_MODEM_SERVICE = "radio set-modem-service ";
    private static final String COMMAND_GET_MODEM_SERVICE = "radio get-modem-service ";
    private static final String COMMAND_SERVICE_IDENTIFIER = "-s ";
    private static final String COMMAND_MODEM_SERVICE_UNKNOWN = "unknown";
    private static final String COMMAND_MODEM_SERVICE_DEFAULT = "default";

    private static final int BIND_LOCAL_MOCKMODEM_SERVICE_TIMEOUT_MS = 5000;
    private static final int BIND_RADIO_INTERFACE_READY_TIMEOUT_MS = 5000;

    private class MockModemServiceConnection implements ServiceConnection {

        private final CountDownLatch mLatch;

        MockModemServiceConnection(CountDownLatch latch) {
            mLatch = latch;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            String serviceName;
            mMockModemService = ((MockModemService.LocalBinder) service).getService();
            serviceName = mMockModemService.getClass().getName();
            if (!isDefaultMockModemService(serviceName)) {
                updateModemServiceName(serviceName);
            }
            mLatch.countDown();
            Log.d(TAG, "MockModemServiceConnection - " + serviceName + " onServiceConnected");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mMockModemService = null;
            Log.d(TAG, "MockModemServiceConnection - onServiceDisconnected");
        }
    }

    private Instrumentation mInstrumentation;

    private MockModemService mMockModemService;
    private MockModemServiceConnection mMockModemServiceConn;
    private boolean mIsServiceOverridden;
    private String mModemServiceName;

    MockModemServiceConnector(Instrumentation instrumentation) {
        mInstrumentation = instrumentation;
    }

    private boolean setupLocalMockModemService() {
        Log.d(TAG, "setupLocalMockModemService");
        if (mMockModemService != null) {
            return true;
        }

        CountDownLatch latch = new CountDownLatch(1);
        if (mMockModemServiceConn == null) {
            mMockModemServiceConn = new MockModemServiceConnection(latch);
        }

        mInstrumentation
                .getContext()
                .bindService(
                        new Intent(mInstrumentation.getContext(), MockModemService.class),
                        mMockModemServiceConn,
                        Context.BIND_AUTO_CREATE);
        try {
            return latch.await(BIND_LOCAL_MOCKMODEM_SERVICE_TIMEOUT_MS, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            return false;
        }
    }

    private String constructSetModemServiceOverrideCommand() {
        return COMMAND_BASE
                + COMMAND_SET_MODEM_SERVICE
                + COMMAND_SERVICE_IDENTIFIER
                + mModemServiceName;
    }

    private String constructGetModemServiceCommand() {
        return COMMAND_BASE + COMMAND_GET_MODEM_SERVICE;
    }

    private String constructClearModemServiceOverrideCommand() {
        return COMMAND_BASE + COMMAND_SET_MODEM_SERVICE;
    }

    private boolean setModemService() throws Exception {
        String result =
                TelephonyUtils.executeShellCommand(
                        mInstrumentation, constructSetModemServiceOverrideCommand());
        Log.d(TAG, "setModemService result: " + result);
        return "true".equals(result);
    }

    private String getModemService() throws Exception {
        String result =
                TelephonyUtils.executeShellCommand(
                        mInstrumentation, constructGetModemServiceCommand());
        Log.d(TAG, "getModemService result: " + result);
        return result;
    }

    private boolean clearModemServiceOverride() throws Exception {
        String result =
                TelephonyUtils.executeShellCommand(
                        mInstrumentation, constructClearModemServiceOverrideCommand());
        Log.d(TAG, "clearModemServiceOverride result: " + result);
        return "true".equals(result);
    }

    private boolean isServiceTheSame(String serviceA, String serviceB) {
        if (TextUtils.isEmpty(serviceA) && TextUtils.isEmpty(serviceB)) {
            return true;
        }
        return TextUtils.equals(serviceA, serviceB);
    }

    private void updateModemServiceName(String serviceName) {
        mModemServiceName = serviceName;
    }

    private boolean overrideModemService() throws Exception {
        boolean result = setModemService();

        if (result) mIsServiceOverridden = true;

        return result;
    }

    private boolean isDefaultMockModemService(String serviceName) {
        return TextUtils.equals(DEFAULT_SERVICE_NAME, serviceName);
    }

    /**
     * Bind to the local implementation of MockModemService.
     *
     * @return true if this request succeeded, false otherwise.
     */
    boolean connectMockModemServiceLocally() {
        if (!setupLocalMockModemService()) {
            Log.w(TAG, "connectMockModemService: couldn't set up service.");
            return false;
        }
        return true;
    }

    /**
     * Trigger the telephony framework to bind to the local MockModemService implementation.
     *
     * @return true if this request succeeded, false otherwise.
     */
    boolean switchFrameworkConnectionToMockModemService(int simprofile) throws Exception {
        boolean isComplete = false;

        if (overrideModemService()) {
            isComplete =
                    mMockModemService.waitForLatchCountdown(
                            MockModemService.LATCH_RADIO_INTERFACES_READY,
                            BIND_RADIO_INTERFACE_READY_TIMEOUT_MS);

            if (isComplete) {
                // TODO: support DSDS
                isComplete = mMockModemService.initialize(simprofile);
            }
        }

        return isComplete;
    }

    boolean checkDefaultModemServiceConnected(String serviceName) throws Exception {
        return isServiceTheSame(COMMAND_MODEM_SERVICE_DEFAULT, serviceName);
    }

    boolean checkModemServiceOverridden(String serviceName) throws Exception {
        return isServiceTheSame(mModemServiceName, serviceName);
    }

    boolean connectMockModemService(int simprofile) throws Exception {
        boolean result = false;
        if (!connectMockModemServiceLocally()) return false;

        result = checkModemServiceOverridden(getModemService());
        if (result) mIsServiceOverridden = true;
        else result = switchFrameworkConnectionToMockModemService(simprofile);

        return result;
    }

    boolean triggerFrameworkDisconnectionFromMockModemService() throws Exception {
        boolean result = false;
        if (!mIsServiceOverridden) {
            Log.d(TAG, "Service didn't override.");
            return true;
        }

        result = clearModemServiceOverride();
        if (result) mIsServiceOverridden = false;

        return result;
    }

    boolean disconnectMockModemService() throws Exception {
        boolean isComplete;
        isComplete = triggerFrameworkDisconnectionFromMockModemService();

        if (isComplete) {
            // waiting for binding to default modem service
            TimeUnit.SECONDS.sleep(5);
            String serviceName = getModemService();
            isComplete =
                    (!checkModemServiceOverridden(serviceName)
                            && checkDefaultModemServiceConnected(serviceName));
        }

        // Remove local connection
        Log.d(TAG, "disconnectMockModemService");
        if (mMockModemServiceConn != null) {
            mInstrumentation.getContext().unbindService(mMockModemServiceConn);
            mMockModemService = null;
        }

        return isComplete;
    }

    MockModemService getMockModemService() {
        return mMockModemService;
    }
}