Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.telecomm; |
| 18 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 19 | import android.telecomm.CallInfo; |
| 20 | import android.util.Log; |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 21 | |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 22 | import com.google.common.base.Preconditions; |
| 23 | import com.google.common.collect.Maps; |
| 24 | |
| 25 | import java.util.Map; |
| 26 | |
| 27 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 28 | * 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 Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 32 | */ |
| 33 | final class IncomingCallsManager { |
| 34 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 35 | private static final String TAG = IncomingCallsManager.class.getSimpleName(); |
| 36 | |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 37 | private final Switchboard mSwitchboard; |
| 38 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 39 | /** Maps call ID to the call. */ |
| 40 | private final Map<String, Call> mPendingIncomingCalls = Maps.newHashMap(); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Persists the specified parameters. |
| 44 | * |
| 45 | * @param switchboard The switchboard. |
| 46 | */ |
| 47 | IncomingCallsManager(Switchboard switchboard) { |
| 48 | mSwitchboard = switchboard; |
| 49 | } |
| 50 | |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 51 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 52 | * Retrieves details of an incoming call through its associated call service (asynchronously). |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 53 | * Starts the timeout sequence in case the call service is unresponsive. |
| 54 | * |
| 55 | * @param call The call object. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 56 | */ |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 57 | void retrieveIncomingCall(final Call call) { |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 58 | ThreadUtil.checkOnMainThread(); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 59 | Log.d(TAG, "retrieveIncomingCall"); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 60 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 61 | 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 Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 67 | Runnable errorCallback = new Runnable() { |
| 68 | @Override public void run() { |
| 69 | handleFailedIncomingCall(call); |
| 70 | } |
| 71 | }; |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 72 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 73 | // TODO(gilad): call.retrieve*Call() seems a bit unusual, consider revisiting. |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 74 | call.getCallService().retrieveIncomingCall(callId, errorCallback); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Notifies the switchboard of a successful incoming call after removing it from the pending |
| 79 | * list. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 80 | * |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 81 | * @param callInfo The details of the call. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 82 | */ |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 83 | void handleSuccessfulIncomingCall(CallInfo callInfo) { |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 84 | ThreadUtil.checkOnMainThread(); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 85 | |
| 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 Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 92 | 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 Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 101 | private void handleFailedIncomingCall(Call call) { |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 102 | ThreadUtil.checkOnMainThread(); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 103 | |
| 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 Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 107 | mSwitchboard.handleFailedIncomingCall(call); |
| 108 | } |
| 109 | } |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 110 | } |