blob: d82a7dec4d4dc4729c173af4e7da86d3d9185293 [file] [log] [blame]
Santos Cordon80d9bdc2014-02-13 18:28:46 -08001/*
2 * Copyright 2014, 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
17package com.android.telecomm;
18
Evan Charltona05805b2014-03-05 08:21:46 -080019import android.os.Bundle;
Santos Cordon493e8f22014-02-19 03:15:12 -080020import android.telecomm.CallInfo;
Evan Charltona05805b2014-03-05 08:21:46 -080021import android.telecomm.CallService;
Santos Cordon493e8f22014-02-19 03:15:12 -080022import android.util.Log;
Santos Cordon80d9bdc2014-02-13 18:28:46 -080023
Santos Cordon80d9bdc2014-02-13 18:28:46 -080024import com.google.common.base.Preconditions;
25import com.google.common.collect.Maps;
26
27import java.util.Map;
28
29/**
Santos Cordon493e8f22014-02-19 03:15:12 -080030 * Utility class to retrieve details of an incoming call after receiving an incoming-call intent,
31 * see {@link CallActivity}. Binds with the specified call services and requests details of incoming
32 * calls. Upon receipt of the details, yields execution back to the switchboard to complete the
33 * incoming sequence. The entire process is timeboxed to protect against unresponsive call services.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080034 */
35final class IncomingCallsManager {
36
Santos Cordon493e8f22014-02-19 03:15:12 -080037 private static final String TAG = IncomingCallsManager.class.getSimpleName();
38
Santos Cordon80d9bdc2014-02-13 18:28:46 -080039 private final Switchboard mSwitchboard;
40
Santos Cordon493e8f22014-02-19 03:15:12 -080041 /** Maps call ID to the call. */
42 private final Map<String, Call> mPendingIncomingCalls = Maps.newHashMap();
Santos Cordon80d9bdc2014-02-13 18:28:46 -080043
44 /**
45 * Persists the specified parameters.
46 *
47 * @param switchboard The switchboard.
48 */
49 IncomingCallsManager(Switchboard switchboard) {
50 mSwitchboard = switchboard;
51 }
52
Santos Cordon80d9bdc2014-02-13 18:28:46 -080053 /**
Santos Cordon493e8f22014-02-19 03:15:12 -080054 * Retrieves details of an incoming call through its associated call service (asynchronously).
Santos Cordon80d9bdc2014-02-13 18:28:46 -080055 * Starts the timeout sequence in case the call service is unresponsive.
56 *
57 * @param call The call object.
Evan Charltona05805b2014-03-05 08:21:46 -080058 * @param extras The optional extras passed with the incoming call intent (to be returned to
59 * the call service via {@link CallService#setIncomingCallId(String, android.os.Bundle)}).
Santos Cordon80d9bdc2014-02-13 18:28:46 -080060 */
Evan Charltona05805b2014-03-05 08:21:46 -080061 void retrieveIncomingCall(final Call call, Bundle extras) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080062 ThreadUtil.checkOnMainThread();
Santos Cordon493e8f22014-02-19 03:15:12 -080063 Log.d(TAG, "retrieveIncomingCall");
Santos Cordon80d9bdc2014-02-13 18:28:46 -080064
Santos Cordon493e8f22014-02-19 03:15:12 -080065 final String callId = call.getId();
66 // Just to be safe, lets make sure we're not already processing this call.
67 Preconditions.checkState(!mPendingIncomingCalls.containsKey(callId));
68
69 mPendingIncomingCalls.put(callId, call);
70
Ben Gilad8e55d1d2014-02-26 16:25:56 -080071 Runnable errorCallback = new Runnable() {
72 @Override public void run() {
73 handleFailedIncomingCall(call);
74 }
75 };
Santos Cordon80d9bdc2014-02-13 18:28:46 -080076
Ben Gilad8e55d1d2014-02-26 16:25:56 -080077 // TODO(gilad): call.retrieve*Call() seems a bit unusual, consider revisiting.
Evan Charltona05805b2014-03-05 08:21:46 -080078 call.getCallService().retrieveIncomingCall(callId, extras, errorCallback);
Santos Cordon80d9bdc2014-02-13 18:28:46 -080079 }
80
81 /**
82 * Notifies the switchboard of a successful incoming call after removing it from the pending
83 * list.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080084 *
Santos Cordon493e8f22014-02-19 03:15:12 -080085 * @param callInfo The details of the call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080086 */
Santos Cordon493e8f22014-02-19 03:15:12 -080087 void handleSuccessfulIncomingCall(CallInfo callInfo) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080088 ThreadUtil.checkOnMainThread();
Santos Cordon493e8f22014-02-19 03:15:12 -080089
90 Call call = mPendingIncomingCalls.remove(callInfo.getId());
91 if (call != null) {
92 Log.d(TAG, "Incoming call " + call.getId() + " found.");
93 call.setHandle(callInfo.getHandle());
94 call.setState(callInfo.getState());
95
Santos Cordon80d9bdc2014-02-13 18:28:46 -080096 mSwitchboard.handleSuccessfulIncomingCall(call);
97 }
98 }
99
100 /**
101 * Notifies switchboard of the failed incoming call after removing it from the pending list.
102 *
103 * @param call The call.
104 */
Santos Cordon493e8f22014-02-19 03:15:12 -0800105 private void handleFailedIncomingCall(Call call) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800106 ThreadUtil.checkOnMainThread();
Santos Cordon493e8f22014-02-19 03:15:12 -0800107
108 if (mPendingIncomingCalls.remove(call.getId()) != null) {
109 Log.i(TAG, "Failed to get details for incoming call " + call);
110 // The call was found still waiting for details. Consider it failed.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800111 mSwitchboard.handleFailedIncomingCall(call);
112 }
113 }
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800114}