summaryrefslogtreecommitdiff
path: root/src/com/android/server/telecom/callredirection/CallRedirectionProcessor.java
blob: 05e73d544ad038edce466e9b2ae8d63fca74daec (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
/*
 * Copyright (C) 2018 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.callredirection;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.os.UserHandle;
import android.telecom.CallRedirectionService;
import android.telecom.GatewayInfo;
import android.telecom.Log;
import android.telecom.Logging.Runnable;
import android.telecom.PhoneAccountHandle;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.telecom.ICallRedirectionAdapter;
import com.android.internal.telecom.ICallRedirectionService;
import com.android.server.telecom.Call;
import com.android.server.telecom.CallsManager;
import com.android.server.telecom.LogUtils;
import com.android.server.telecom.PhoneAccountRegistrar;
import com.android.server.telecom.TelecomSystem;
import com.android.server.telecom.Timeouts;

/**
 * A single instance of call redirection processor that handles the call redirection with
 * user-defined {@link CallRedirectionService} and carrier {@link CallRedirectionService} for a
 * single call.
 *
 * A user-defined call redirection will be performed firstly and a carrier call redirection will be
 * performed after that; there will be a total of two call redirection cycles.
 *
 * A call redirection cycle is a cycle:
 * 1) Telecom requests a call redirection of a call with a specific {@link CallRedirectionService},
 * 2) Telecom receives the response either from a specific {@link CallRedirectionService} or from
 * the timeout.
 *
 * Telecom should return to {@link CallsManager} at the end of current call redirection
 * cycle, if
 * 1) {@link CallRedirectionService} sends {@link CallRedirectionService#cancelCall()} response
 * before timeout;
 * or 2) Telecom finishes call redirection with carrier {@link CallRedirectionService}.
 */
public class CallRedirectionProcessor implements CallRedirectionCallback {

    private class CallRedirectionAttempt {
        private final ComponentName mComponentName;
        private final String mServiceType;
        private ServiceConnection mConnection;
        private ICallRedirectionService mService;

        private CallRedirectionAttempt(ComponentName componentName, String serviceType) {
            mComponentName = componentName;
            mServiceType = serviceType;
        }

        private void process(UserHandle userHandleForCallRedirection) {
            Intent intent = new Intent(CallRedirectionService.SERVICE_INTERFACE)
                    .setComponent(mComponentName);
            ServiceConnection connection = new CallRedirectionServiceConnection();
            if (mContext.bindServiceAsUser(
                    intent,
                    connection,
                    Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE
                    | Context.BIND_ALLOW_BACKGROUND_ACTIVITY_STARTS,
                    userHandleForCallRedirection)) {
                Log.d(this, "bindService, found " + mServiceType + " call redirection service,"
                        + " waiting for it to connect");
                mConnection = connection;
            }
        }

        private void onServiceBound(ICallRedirectionService service) {
            mService = service;
            try {
                // Telecom does not perform user interactions for carrier call redirection.
                mService.placeCall(new CallRedirectionAdapter(), mProcessedDestinationUri,
                        mPhoneAccountHandle, mAllowInteractiveResponse
                                && mServiceType.equals(SERVICE_TYPE_USER_DEFINED));
                Log.addEvent(mCall, mServiceType.equals(SERVICE_TYPE_USER_DEFINED)
                        ? LogUtils.Events.REDIRECTION_SENT_USER
                        : LogUtils.Events.REDIRECTION_SENT_CARRIER, mComponentName);
                Log.d(this, "Requested placeCall with [Destination Uri] "
                        + Log.pii(mProcessedDestinationUri)
                        + " [phoneAccountHandle]" + mPhoneAccountHandle);
            } catch (RemoteException e) {
                Log.e(this, e, "Failed to request with the found " + mServiceType + " call"
                        + " redirection service");
                finishCallRedirection();
            }
        }

        private void finishCallRedirection() {
            if (((mServiceType.equals(SERVICE_TYPE_CARRIER)) && mIsCarrierRedirectionPending)
                || ((mServiceType.equals(SERVICE_TYPE_USER_DEFINED))
                    && mIsUserDefinedRedirectionPending)) {
                if (mConnection != null) {
                    // We still need to call unbind even if the service disconnected.
                    mContext.unbindService(mConnection);
                    mConnection = null;
                }
                mService = null;
                onCallRedirectionComplete(mCall);
            }
        }

        public void notifyTimeout() {
            if (mService != null) {
                try {
                    mService.notifyTimeout();
                } catch (RemoteException e) {
                    Log.e(this, e, "Failed to notify call redirection timed out to "
                            + mServiceType + " call redirection service");
                }
            }
        }

        private class CallRedirectionServiceConnection implements ServiceConnection {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder service) {
                Log.startSession("CRSC.oSC");
                try {
                    synchronized (mTelecomLock) {
                        Log.addEvent(mCall, mServiceType.equals(SERVICE_TYPE_USER_DEFINED)
                                ? LogUtils.Events.REDIRECTION_BOUND_USER
                                : LogUtils.Events.REDIRECTION_BOUND_CARRIER, componentName);
                        onServiceBound(ICallRedirectionService.Stub.asInterface(service));
                    }
                } finally {
                    Log.endSession();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                Log.startSession("CRSC.oSD");
                try {
                    synchronized (mTelecomLock) {
                        finishCallRedirection();
                    }
                } finally {
                    Log.endSession();
                }
            }

            @Override
            public void onNullBinding(ComponentName componentName) {
                // Make sure we unbind the service if onBind returns null
                Log.startSession("CRSC.oNB");
                try {
                    synchronized (mTelecomLock) {
                        finishCallRedirection();
                    }
                } finally {
                    Log.endSession();
                }
            }

            @Override
            public void onBindingDied(ComponentName componentName) {
                // Make sure we unbind the service if binding died to avoid background stating
                // activity leaks
                Log.startSession("CRSC.oBD");
                try {
                    synchronized (mTelecomLock) {
                        finishCallRedirection();
                    }
                } finally {
                    Log.endSession();
                }
            }
        }

        private class CallRedirectionAdapter extends ICallRedirectionAdapter.Stub {
            @Override
            public void cancelCall() {
                Log.startSession("CRA.cC");
                long token = Binder.clearCallingIdentity();
                try {
                    synchronized (mTelecomLock) {
                        Log.d(this, "Received cancelCall from " +  mServiceType + " call"
                                + " redirection service");
                        mShouldCancelCall = true;
                        finishCallRedirection();
                    }
                } finally {
                    Binder.restoreCallingIdentity(token);
                    Log.endSession();
                }
            }

            @Override
            public void placeCallUnmodified() {
                Log.startSession("CRA.pCU");
                long token = Binder.clearCallingIdentity();
                try {
                    synchronized (mTelecomLock) {
                        Log.d(this, "Received placeCallUnmodified from " +  mServiceType + " call"
                                + " redirection service");
                        finishCallRedirection();
                    }
                } finally {
                    Binder.restoreCallingIdentity(token);
                    Log.endSession();
                }
            }

            @Override
            public void redirectCall(Uri gatewayUri, PhoneAccountHandle targetPhoneAccount,
                                     boolean confirmFirst) {
                Log.startSession("CRA.rC");
                long token = Binder.clearCallingIdentity();
                try {
                    synchronized (mTelecomLock) {
                        mRedirectionGatewayInfo = mCallRedirectionProcessorHelper
                                .getGatewayInfoFromGatewayUri(mComponentName.getPackageName(),
                                        gatewayUri, mDestinationUri, mPostDialDigits);
                        mPhoneAccountHandle = targetPhoneAccount;
                        // If carrier redirects call, we should skip to notify users about
                        // the user-defined call redirection service.
                        mUiAction = (confirmFirst && mServiceType.equals(SERVICE_TYPE_USER_DEFINED)
                                && mAllowInteractiveResponse)
                                ? UI_TYPE_USER_DEFINED_ASK_FOR_CONFIRM : UI_TYPE_NO_ACTION;
                        Log.d(this, "Received redirectCall with [gatewayUri]"
                                + Log.pii(gatewayUri) + " [phoneAccountHandle]"
                                + mPhoneAccountHandle + "[confirmFirst]" + confirmFirst + " from "
                                + mServiceType + " call redirection service");
                        finishCallRedirection();
                    }
                } finally {
                    Binder.restoreCallingIdentity(token);
                    Log.endSession();
                }
            }
        }
    }

    private final Context mContext;
    private final CallsManager mCallsManager;
    private final Call mCall;
    private final boolean mAllowInteractiveResponse;
    private GatewayInfo mRedirectionGatewayInfo;
    private final boolean mSpeakerphoneOn;
    private final int mVideoState;
    private final Timeouts.Adapter mTimeoutsAdapter;
    private final TelecomSystem.SyncRoot mTelecomLock;
    private final Handler mHandler = new Handler(Looper.getMainLooper());

    private CallRedirectionAttempt mAttempt;
    private CallRedirectionProcessorHelper mCallRedirectionProcessorHelper;

    public static final String SERVICE_TYPE_CARRIER = "carrier";
    public static final String SERVICE_TYPE_USER_DEFINED = "user_defined";
    public static final String UI_TYPE_NO_ACTION = "no_action";
    public static final String UI_TYPE_USER_DEFINED_TIMEOUT = "user_defined_timeout";
    public static final String UI_TYPE_USER_DEFINED_ASK_FOR_CONFIRM
            = "user_defined_ask_for_confirm";

    private PhoneAccountHandle mPhoneAccountHandle;
    private Uri mDestinationUri;
    /**
     * Try to send the implemented service with processed destination uri by formatting it to E.164
     * and removing post dial digits.
     */
    private Uri mProcessedDestinationUri;

    /**
     * The post dial digits which were removed from {@link #mDestinationUri} when determining
     * {@link #mProcessedDestinationUri}.
     */
    private String mPostDialDigits;

    /**
     * Indicates if Telecom should cancel the call when the whole call redirection finishes.
     */
    private boolean mShouldCancelCall = false;
    /**
     * Indicates Telecom should handle different types of UI if need.
     */
    private String mUiAction = UI_TYPE_NO_ACTION;
    /**
     * Indicates if Telecom is waiting for a callback from a user-defined
     * {@link CallRedirectionService}.
     */
    private boolean mIsUserDefinedRedirectionPending = false;
    /**
     * Indicates if Telecom is waiting for a callback from a carrier
     * {@link CallRedirectionService}.
     */
    private boolean mIsCarrierRedirectionPending = false;

    public CallRedirectionProcessor(
            Context context,
            CallsManager callsManager,
            Call call,
            Uri handle,
            PhoneAccountRegistrar phoneAccountRegistrar,
            GatewayInfo gatewayInfo,
            boolean speakerphoneOn,
            int videoState) {
        mContext = context;
        mCallsManager = callsManager;
        mCall = call;
        mDestinationUri = handle;
        mPhoneAccountHandle = call.getTargetPhoneAccount();
        mRedirectionGatewayInfo = gatewayInfo;
        mSpeakerphoneOn = speakerphoneOn;
        mVideoState = videoState;
        mTimeoutsAdapter = callsManager.getTimeoutsAdapter();
        mTelecomLock = callsManager.getLock();
        /**
         * The current rule to decide whether the implemented {@link CallRedirectionService} should
         * allow interactive responses with users is only based on whether it is in car mode.
         */
        mAllowInteractiveResponse = !callsManager.getSystemStateHelper().isCarModeOrProjectionActive();
        mCallRedirectionProcessorHelper = new CallRedirectionProcessorHelper(
                context, callsManager, phoneAccountRegistrar);
        mProcessedDestinationUri = mCallRedirectionProcessorHelper.formatNumberForRedirection(
                mDestinationUri);
        mPostDialDigits = mCallRedirectionProcessorHelper.getPostDialDigits(mDestinationUri);
    }

    @Override
    public void onCallRedirectionComplete(Call call) {
        // synchronized on mTelecomLock to enter into Telecom.
        mHandler.post(new Runnable("CRP.oCRC", mTelecomLock) {
            @Override
            public void loggedRun() {
                if (mIsUserDefinedRedirectionPending) {
                    Log.addEvent(mCall, LogUtils.Events.REDIRECTION_COMPLETED_USER);
                    mIsUserDefinedRedirectionPending = false;
                    if (mShouldCancelCall) {
                        mCallsManager.onCallRedirectionComplete(mCall, mDestinationUri,
                                mPhoneAccountHandle, mRedirectionGatewayInfo, mSpeakerphoneOn,
                                mVideoState, mShouldCancelCall, mUiAction);
                    } else {
                        // Use the current user for carrier call redirection
                        performCarrierCallRedirection(UserHandle.CURRENT);
                    }
                } else if (mIsCarrierRedirectionPending) {
                    Log.addEvent(mCall, LogUtils.Events.REDIRECTION_COMPLETED_CARRIER);
                    mIsCarrierRedirectionPending = false;
                    mCallsManager.onCallRedirectionComplete(mCall, mDestinationUri,
                            mPhoneAccountHandle, mRedirectionGatewayInfo, mSpeakerphoneOn,
                            mVideoState, mShouldCancelCall, mUiAction);
                }
            }
        }.prepare());
    }

    /**
     * The entry to perform call redirection of the call from (@link CallsManager)
     */
    public void performCallRedirection(UserHandle userHandleForCallRedirection) {
        // If the Gateway Info is set with intent, only request with carrier call redirection.
        if (mRedirectionGatewayInfo != null) {
            // Use the current user for carrier call redirection
            performCarrierCallRedirection(UserHandle.CURRENT);
        } else {
            performUserDefinedCallRedirection(userHandleForCallRedirection);
        }
    }

    private void performUserDefinedCallRedirection(UserHandle userHandleForCallRedirection) {
        Log.d(this, "performUserDefinedCallRedirection");
        ComponentName componentName =
                mCallRedirectionProcessorHelper.
                        getUserDefinedCallRedirectionService(userHandleForCallRedirection);
        if (componentName != null) {
            mAttempt = new CallRedirectionAttempt(componentName, SERVICE_TYPE_USER_DEFINED);
            mAttempt.process(userHandleForCallRedirection);
            mIsUserDefinedRedirectionPending = true;
            processTimeoutForCallRedirection(SERVICE_TYPE_USER_DEFINED);
        } else {
            Log.i(this, "There are no user-defined call redirection services installed on this"
                    + " device.");
            performCarrierCallRedirection(UserHandle.CURRENT);
        }
    }

    private void performCarrierCallRedirection(UserHandle userHandleForCallRedirection) {
        Log.d(this, "performCarrierCallRedirection");
        ComponentName componentName =
                mCallRedirectionProcessorHelper.getCarrierCallRedirectionService(
                        mPhoneAccountHandle);
        if (componentName != null) {
            mAttempt = new CallRedirectionAttempt(componentName, SERVICE_TYPE_CARRIER);
            mAttempt.process(userHandleForCallRedirection);
            mIsCarrierRedirectionPending = true;
            processTimeoutForCallRedirection(SERVICE_TYPE_CARRIER);
        } else {
            Log.i(this, "There are no carrier call redirection services installed on this"
                    + " device.");
            mCallsManager.onCallRedirectionComplete(mCall, mDestinationUri,
                    mPhoneAccountHandle, mRedirectionGatewayInfo, mSpeakerphoneOn, mVideoState,
                    mShouldCancelCall, mUiAction);
        }
    }

    private void processTimeoutForCallRedirection(String serviceType) {
        long timeout = serviceType.equals(SERVICE_TYPE_USER_DEFINED) ?
            mTimeoutsAdapter.getUserDefinedCallRedirectionTimeoutMillis(
                mContext.getContentResolver()) : mTimeoutsAdapter
            .getCarrierCallRedirectionTimeoutMillis(mContext.getContentResolver());

        mHandler.postDelayed(new Runnable("CRP.pTFCR", null) {
            @Override
            public void loggedRun() {
                boolean isCurrentRedirectionPending =
                        serviceType.equals(SERVICE_TYPE_USER_DEFINED) ?
                                mIsUserDefinedRedirectionPending : mIsCarrierRedirectionPending;
                if (isCurrentRedirectionPending) {
                    Log.i(this, serviceType + " call redirection has timed out.");
                    Log.addEvent(mCall, serviceType.equals(SERVICE_TYPE_USER_DEFINED)
                            ? LogUtils.Events.REDIRECTION_TIMED_OUT_USER
                            : LogUtils.Events.REDIRECTION_TIMED_OUT_CARRIER);
                    mAttempt.notifyTimeout();
                    if (serviceType.equals(SERVICE_TYPE_USER_DEFINED)) {
                        mUiAction = UI_TYPE_USER_DEFINED_TIMEOUT;
                        mShouldCancelCall = true;
                    }
                    onCallRedirectionComplete(mCall);
                }
            }
        }.prepare(), timeout);
    }

    /**
     * Checks if Telecom can make call redirection with any available call redirection service
     * as the specified user.
     *
     * @return {@code true} if it can; {@code false} otherwise.
     */
    public boolean canMakeCallRedirectionWithServiceAsUser(
            UserHandle userHandleForCallRedirection) {
        boolean canMakeCallRedirectionWithServiceAsUser =
                mCallRedirectionProcessorHelper
                        .getUserDefinedCallRedirectionService(userHandleForCallRedirection) != null
                        || mCallRedirectionProcessorHelper.getCarrierCallRedirectionService(
                                mPhoneAccountHandle) != null;
        Log.i(this, "Can make call redirection with any "
                + "available service as user (" + userHandleForCallRedirection
                + ") : " + canMakeCallRedirectionWithServiceAsUser);
        return canMakeCallRedirectionWithServiceAsUser;
    }

    /**
     * Returns the handler, for testing purposes.
     */
    @VisibleForTesting
    public Handler getHandler() {
        return mHandler;
    }

    /**
     * Set CallRedirectionProcessorHelper for testing purposes.
     */
    @VisibleForTesting
    public void setCallRedirectionServiceHelper(
            CallRedirectionProcessorHelper callRedirectionProcessorHelper) {
        mCallRedirectionProcessorHelper = callRedirectionProcessorHelper;
    }
}