summaryrefslogtreecommitdiff
path: root/src/com/android/server/telecom/CallStreamingController.java
blob: 1323633fca080104a905eacd7a2c4869f09f6b2c (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
/*
 * Copyright (C) 2022 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.server.telecom;

import static android.telecom.CallStreamingService.STREAMING_FAILED_SENDER_BINDING_ERROR;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.role.RoleManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.os.OutcomeReceiver;
import android.os.RemoteException;
import android.os.UserHandle;
import android.telecom.CallException;
import android.telecom.CallStreamingService;
import android.telecom.StreamingCall;
import android.telecom.Log;

import com.android.internal.telecom.ICallStreamingService;
import com.android.server.telecom.voip.ParallelTransaction;
import com.android.server.telecom.voip.SerialTransaction;
import com.android.server.telecom.voip.VoipCallTransaction;
import com.android.server.telecom.voip.VoipCallTransactionResult;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

public class CallStreamingController extends CallsManagerListenerBase {
    private Call mStreamingCall;
    private TransactionalServiceWrapper mTransactionalServiceWrapper;
    private ICallStreamingService mService;
    private final Context mContext;
    private CallStreamingServiceConnection mConnection;
    private boolean mIsStreaming;
    private final Object mLock;
    private TelecomSystem.SyncRoot mTelecomLock;

    public CallStreamingController(Context context, TelecomSystem.SyncRoot telecomLock) {
        mLock = new Object();
        mContext = context;
        mTelecomLock = telecomLock;
    }

    private void onConnectedInternal(Call call, TransactionalServiceWrapper wrapper,
            IBinder service) throws RemoteException {
        synchronized (mLock) {
            Log.i(this, "onConnectedInternal: callid=%s", call.getId());
            Bundle extras = new Bundle();
            extras.putString(StreamingCall.EXTRA_CALL_ID, call.getId());
            mStreamingCall = call;
            mTransactionalServiceWrapper = wrapper;
            mService = ICallStreamingService.Stub.asInterface(service);
            mService.setStreamingCallAdapter(new StreamingCallAdapter(mTransactionalServiceWrapper,
                    mStreamingCall,
                    mStreamingCall.getTargetPhoneAccount().getComponentName().getPackageName()));
            mService.onCallStreamingStarted(new StreamingCall(
                    mTransactionalServiceWrapper.getComponentName(),
                    mStreamingCall.getCallerDisplayName(),
                    mStreamingCall.getHandle(), extras));
            mIsStreaming = true;
        }
    }

    private void resetController() {
        synchronized (mLock) {
            mStreamingCall = null;
            mTransactionalServiceWrapper = null;
            if (mConnection != null) {
                // Notify service streaming stopped and then unbind.
                try {
                    mService.onCallStreamingStopped();
                } catch (RemoteException e) {
                    // Could not notify stop streaming; we're about to just unbind so this is
                    // unfortunate but not the end of the world.
                    Log.e(this, e, "resetController: failed to notify stop streaming.");
                }
                mContext.unbindService(mConnection);
                mConnection = null;
            }
            mService = null;
            mIsStreaming = false;
        }
    }

    public boolean isStreaming() {
        synchronized (mLock) {
            return mIsStreaming;
        }
    }

    public static class QueryCallStreamingTransaction extends VoipCallTransaction {
        private final CallsManager mCallsManager;

        public QueryCallStreamingTransaction(CallsManager callsManager) {
            super(callsManager.getLock());
            mCallsManager = callsManager;
        }

        @Override
        public CompletableFuture<VoipCallTransactionResult> processTransaction(Void v) {
            Log.i(this, "processTransaction");
            CompletableFuture<VoipCallTransactionResult> future = new CompletableFuture<>();

            if (mCallsManager.getCallStreamingController().isStreaming()) {
                future.complete(new VoipCallTransactionResult(
                        VoipCallTransactionResult.RESULT_FAILED,
                        "STREAMING_FAILED_ALREADY_STREAMING"));
            } else {
                future.complete(new VoipCallTransactionResult(
                        VoipCallTransactionResult.RESULT_SUCCEED, null));
            }

            return future;
        }
    }

    public static class AudioInterceptionTransaction extends VoipCallTransaction {
        private Call mCall;
        private boolean mEnterInterception;

        public AudioInterceptionTransaction(Call call, boolean enterInterception,
                TelecomSystem.SyncRoot lock) {
            super(lock);
            mCall = call;
            mEnterInterception = enterInterception;
        }

        @Override
        public CompletableFuture<VoipCallTransactionResult> processTransaction(Void v) {
            Log.i(this, "processTransaction");
            CompletableFuture<VoipCallTransactionResult> future = new CompletableFuture<>();

            if (mEnterInterception) {
                mCall.startStreaming();
            } else {
                mCall.stopStreaming();
            }
            future.complete(new VoipCallTransactionResult(VoipCallTransactionResult.RESULT_SUCCEED,
                    null));
            return future;
        }
    }

    public StreamingServiceTransaction getCallStreamingServiceTransaction(Context context,
            TransactionalServiceWrapper wrapper, Call call) {
        return new StreamingServiceTransaction(context, wrapper, call);
    }

    public class StreamingServiceTransaction extends VoipCallTransaction {
        public static final String MESSAGE = "STREAMING_FAILED_NO_SENDER";
        private final TransactionalServiceWrapper mWrapper;
        private final Context mContext;
        private final UserHandle mUserHandle;
        private final Call mCall;

        public StreamingServiceTransaction(Context context, TransactionalServiceWrapper wrapper,
                Call call) {
            super(mTelecomLock);
            mWrapper = wrapper;
            mCall = call;
            mUserHandle = mCall.getAssociatedUser();
            mContext = context;
        }

        @SuppressLint("LongLogTag")
        @Override
        public CompletionStage<VoipCallTransactionResult> processTransaction(Void v) {
            Log.i(this, "processTransaction");
            CompletableFuture<VoipCallTransactionResult> future = new CompletableFuture<>();
            RoleManager roleManager = mContext.getSystemService(RoleManager.class);
            PackageManager packageManager = mContext.getPackageManager();
            if (roleManager == null || packageManager == null) {
                Log.w(this, "processTransaction: Can't find system service");
                future.complete(new VoipCallTransactionResult(
                        VoipCallTransactionResult.RESULT_FAILED, MESSAGE));
                return future;
            }

            List<String> holders = roleManager.getRoleHoldersAsUser(
                    RoleManager.ROLE_SYSTEM_CALL_STREAMING, mUserHandle);
            if (holders.isEmpty()) {
                Log.w(this, "processTransaction: Can't find streaming app");
                future.complete(new VoipCallTransactionResult(
                        VoipCallTransactionResult.RESULT_FAILED, MESSAGE));
                return future;
            }
            Log.i(this, "processTransaction: servicePackage=%s", holders.get(0));
            Intent serviceIntent = new Intent(CallStreamingService.SERVICE_INTERFACE);
            serviceIntent.setPackage(holders.get(0));
            List<ResolveInfo> infos = packageManager.queryIntentServicesAsUser(serviceIntent,
                    PackageManager.GET_META_DATA, mUserHandle);
            if (infos.isEmpty()) {
                Log.w(this, "processTransaction: Can't find streaming service");
                future.complete(new VoipCallTransactionResult(
                        VoipCallTransactionResult.RESULT_FAILED, MESSAGE));
                return future;
            }

            ServiceInfo serviceInfo = infos.get(0).serviceInfo;

            if (serviceInfo.permission == null || !serviceInfo.permission.equals(
                    Manifest.permission.BIND_CALL_STREAMING_SERVICE)) {
                Log.w(this, "Must require BIND_CALL_STREAMING_SERVICE: " +
                        serviceInfo.packageName);
                future.complete(new VoipCallTransactionResult(
                        VoipCallTransactionResult.RESULT_FAILED, MESSAGE));
                return future;
            }
            Intent intent = new Intent(CallStreamingService.SERVICE_INTERFACE);
            intent.setComponent(serviceInfo.getComponentName());

            mConnection = new CallStreamingServiceConnection(mCall, mWrapper, future);
            if (!mContext.bindServiceAsUser(intent, mConnection, Context.BIND_AUTO_CREATE
                    | Context.BIND_FOREGROUND_SERVICE
                    | Context.BIND_SCHEDULE_LIKE_TOP_APP, mUserHandle)) {
                Log.w(this, "Can't bind to streaming service");
                future.complete(new VoipCallTransactionResult(
                        VoipCallTransactionResult.RESULT_FAILED,
                        "STREAMING_FAILED_SENDER_BINDING_ERROR"));
            }
            return future;
        }
    }

    public UnbindStreamingServiceTransaction getUnbindStreamingServiceTransaction() {
        return new UnbindStreamingServiceTransaction();
    }

    public class UnbindStreamingServiceTransaction extends VoipCallTransaction {
        public UnbindStreamingServiceTransaction() {
            super(mTelecomLock);
        }

        @SuppressLint("LongLogTag")
        @Override
        public CompletionStage<VoipCallTransactionResult> processTransaction(Void v) {
            Log.i(this, "processTransaction (unbindStreaming");
            CompletableFuture<VoipCallTransactionResult> future = new CompletableFuture<>();

            resetController();
            future.complete(new VoipCallTransactionResult(VoipCallTransactionResult.RESULT_SUCCEED,
                    null));
            return future;
        }
    }

    public class StartStreamingTransaction extends SerialTransaction {
        private Call mCall;

        public StartStreamingTransaction(List<VoipCallTransaction> subTransactions, Call call,
                TelecomSystem.SyncRoot lock) {
            super(subTransactions, lock);
            mCall = call;
        }

        @Override
        public void handleTransactionFailure() {
            mTransactionalServiceWrapper.stopCallStreaming(mCall);
        }
    }

    public VoipCallTransaction getStartStreamingTransaction(CallsManager callsManager,
            TransactionalServiceWrapper wrapper, Call call, TelecomSystem.SyncRoot lock) {
        // start streaming transaction flow:
        //     1. make sure there's no ongoing streaming call --> bind to EXO
        //     2. change audio mode
        //     3. bind to EXO
        // If bind to EXO failed, add transaction for stop the streaming

        // create list for multiple transactions
        List<VoipCallTransaction> transactions = new ArrayList<>();
        transactions.add(new QueryCallStreamingTransaction(callsManager));
        transactions.add(new AudioInterceptionTransaction(call, true, lock));
        transactions.add(getCallStreamingServiceTransaction(
                callsManager.getContext(), wrapper, call));
        return new StartStreamingTransaction(transactions, call, lock);
    }

    public VoipCallTransaction getStopStreamingTransaction(Call call, TelecomSystem.SyncRoot lock) {
        // TODO: implement this
        // Stop streaming transaction flow:
        List<VoipCallTransaction> transactions = new ArrayList<>();

        // 1. unbind to call streaming service
        transactions.add(getUnbindStreamingServiceTransaction());
        // 2. audio route operations
        transactions.add(new CallStreamingController.AudioInterceptionTransaction(call,
                false, lock));
        return new ParallelTransaction(transactions, lock);
    }

    @Override
    public void onCallRemoved(Call call) {
        if (mStreamingCall == call) {
            mTransactionalServiceWrapper.stopCallStreaming(call);
        }
    }

    @Override
    public void onCallStateChanged(Call call, int oldState, int newState) {
        // TODO: make sure we are only able to stream the one call and not switch focus to another
        // and have it streamed too
        if (mStreamingCall == call && oldState != newState) {
            CallStreamingStateChangeTransaction transaction = null;
            switch (newState) {
                case CallState.ACTIVE:
                    transaction = new CallStreamingStateChangeTransaction(
                            StreamingCall.STATE_STREAMING);
                    break;
                case CallState.ON_HOLD:
                    transaction = new CallStreamingStateChangeTransaction(
                            StreamingCall.STATE_HOLDING);
                    break;
                case CallState.DISCONNECTING:
                case CallState.DISCONNECTED:
                    Log.addEvent(call, LogUtils.Events.STOP_STREAMING);
                    transaction = new CallStreamingStateChangeTransaction(
                            StreamingCall.STATE_DISCONNECTED);
                    break;
                default:
                    // ignore
            }
            if (transaction != null) {
                mTransactionalServiceWrapper.getTransactionManager().addTransaction(transaction,
                        new OutcomeReceiver<>() {
                            @Override
                            public void onResult(VoipCallTransactionResult result) {
                                // ignore
                            }

                            @Override
                            public void onError(CallException exception) {
                                Log.e(this, exception, "Exception when set call "
                                        + "streaming state to streaming app");
                            }
                        });
            }
        }
    }

    private class CallStreamingStateChangeTransaction extends VoipCallTransaction {
        @StreamingCall.StreamingCallState int mState;

        public CallStreamingStateChangeTransaction(@StreamingCall.StreamingCallState int state) {
            super(mTelecomLock);
            mState = state;
        }

        @Override
        public CompletionStage<VoipCallTransactionResult> processTransaction(Void v) {
            CompletableFuture<VoipCallTransactionResult> future = new CompletableFuture<>();
            try {
                mService.onCallStreamingStateChanged(mState);
                future.complete(new VoipCallTransactionResult(
                        VoipCallTransactionResult.RESULT_SUCCEED, null));
            } catch (RemoteException e) {
                future.complete(new VoipCallTransactionResult(
                        VoipCallTransactionResult.RESULT_FAILED, "Exception when request "
                        + "setting state to streaming app."));
            }
            return future;
        }
    }

    private class CallStreamingServiceConnection implements
            ServiceConnection {
        private Call mCall;
        private TransactionalServiceWrapper mWrapper;
        private CompletableFuture<VoipCallTransactionResult> mFuture;

        public CallStreamingServiceConnection(Call call, TransactionalServiceWrapper wrapper,
                CompletableFuture<VoipCallTransactionResult> future) {
            mCall = call;
            mWrapper = wrapper;
            mFuture = future;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            try {
                Log.i(this, "onServiceConnected: " + name);
                onConnectedInternal(mCall, mWrapper, service);
                mFuture.complete(new VoipCallTransactionResult(
                        VoipCallTransactionResult.RESULT_SUCCEED, null));
            } catch (RemoteException e) {
                resetController();
                mFuture.complete(new VoipCallTransactionResult(
                        VoipCallTransactionResult.RESULT_FAILED,
                        StreamingServiceTransaction.MESSAGE));
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            clearBinding();
        }

        @Override
        public void onBindingDied(ComponentName name) {
            clearBinding();
        }

        @Override
        public void onNullBinding(ComponentName name) {
            clearBinding();
        }

        private void clearBinding() {
            resetController();
            if (!mFuture.isDone()) {
                mFuture.complete(new VoipCallTransactionResult(
                        VoipCallTransactionResult.RESULT_FAILED,
                        "STREAMING_FAILED_SENDER_BINDING_ERROR"));
            } else {
                mWrapper.onCallStreamingFailed(mCall, STREAMING_FAILED_SENDER_BINDING_ERROR);
            }
        }
    }
}