blob: 13f13198cf793c6705d3fc230491b2d2fbbaab21 [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
Santos Cordon493e8f22014-02-19 03:15:12 -080019import android.telecomm.CallInfo;
20import android.util.Log;
Santos Cordon80d9bdc2014-02-13 18:28:46 -080021
Santos Cordon80d9bdc2014-02-13 18:28:46 -080022import com.google.common.base.Preconditions;
23import com.google.common.collect.Maps;
24
25import java.util.Map;
26
27/**
Santos Cordon493e8f22014-02-19 03:15:12 -080028 * Utility class to retrieve details of an incoming call after receiving an incoming-call intent,
29 * see {@link CallActivity}. Binds with the specified call services and requests details of incoming
30 * calls. Upon receipt of the details, yields execution back to the switchboard to complete the
31 * incoming sequence. The entire process is timeboxed to protect against unresponsive call services.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080032 */
33final class IncomingCallsManager {
34
Santos Cordon493e8f22014-02-19 03:15:12 -080035 private static final String TAG = IncomingCallsManager.class.getSimpleName();
36
Santos Cordon80d9bdc2014-02-13 18:28:46 -080037 private final Switchboard mSwitchboard;
38
Santos Cordon493e8f22014-02-19 03:15:12 -080039 /** Maps call ID to the call. */
40 private final Map<String, Call> mPendingIncomingCalls = Maps.newHashMap();
Santos Cordon80d9bdc2014-02-13 18:28:46 -080041
42 /**
43 * Persists the specified parameters.
44 *
45 * @param switchboard The switchboard.
46 */
47 IncomingCallsManager(Switchboard switchboard) {
48 mSwitchboard = switchboard;
49 }
50
Santos Cordon80d9bdc2014-02-13 18:28:46 -080051 /**
Santos Cordon493e8f22014-02-19 03:15:12 -080052 * Retrieves details of an incoming call through its associated call service (asynchronously).
Santos Cordon80d9bdc2014-02-13 18:28:46 -080053 * Starts the timeout sequence in case the call service is unresponsive.
54 *
55 * @param call The call object.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080056 */
Ben Gilad8e55d1d2014-02-26 16:25:56 -080057 void retrieveIncomingCall(final Call call) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080058 ThreadUtil.checkOnMainThread();
Santos Cordon493e8f22014-02-19 03:15:12 -080059 Log.d(TAG, "retrieveIncomingCall");
Santos Cordon80d9bdc2014-02-13 18:28:46 -080060
Santos Cordon493e8f22014-02-19 03:15:12 -080061 final String callId = call.getId();
62 // Just to be safe, lets make sure we're not already processing this call.
63 Preconditions.checkState(!mPendingIncomingCalls.containsKey(callId));
64
65 mPendingIncomingCalls.put(callId, call);
66
Ben Gilad8e55d1d2014-02-26 16:25:56 -080067 Runnable errorCallback = new Runnable() {
68 @Override public void run() {
69 handleFailedIncomingCall(call);
70 }
71 };
Santos Cordon80d9bdc2014-02-13 18:28:46 -080072
Ben Gilad8e55d1d2014-02-26 16:25:56 -080073 // TODO(gilad): call.retrieve*Call() seems a bit unusual, consider revisiting.
Santos Cordon493e8f22014-02-19 03:15:12 -080074 call.getCallService().retrieveIncomingCall(callId, errorCallback);
Santos Cordon80d9bdc2014-02-13 18:28:46 -080075 }
76
77 /**
78 * Notifies the switchboard of a successful incoming call after removing it from the pending
79 * list.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080080 *
Santos Cordon493e8f22014-02-19 03:15:12 -080081 * @param callInfo The details of the call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080082 */
Santos Cordon493e8f22014-02-19 03:15:12 -080083 void handleSuccessfulIncomingCall(CallInfo callInfo) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080084 ThreadUtil.checkOnMainThread();
Santos Cordon493e8f22014-02-19 03:15:12 -080085
86 Call call = mPendingIncomingCalls.remove(callInfo.getId());
87 if (call != null) {
88 Log.d(TAG, "Incoming call " + call.getId() + " found.");
89 call.setHandle(callInfo.getHandle());
90 call.setState(callInfo.getState());
91
Santos Cordon80d9bdc2014-02-13 18:28:46 -080092 mSwitchboard.handleSuccessfulIncomingCall(call);
93 }
94 }
95
96 /**
97 * Notifies switchboard of the failed incoming call after removing it from the pending list.
98 *
99 * @param call The call.
100 */
Santos Cordon493e8f22014-02-19 03:15:12 -0800101 private void handleFailedIncomingCall(Call call) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800102 ThreadUtil.checkOnMainThread();
Santos Cordon493e8f22014-02-19 03:15:12 -0800103
104 if (mPendingIncomingCalls.remove(call.getId()) != null) {
105 Log.i(TAG, "Failed to get details for incoming call " + call);
106 // The call was found still waiting for details. Consider it failed.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800107 mSwitchboard.handleFailedIncomingCall(call);
108 }
109 }
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800110}