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 | |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 19 | import android.os.Bundle; |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 20 | import android.telecomm.CallInfo; |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 21 | import android.telecomm.CallService; |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 22 | |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 23 | import com.google.common.base.Preconditions; |
| 24 | import com.google.common.collect.Maps; |
| 25 | |
| 26 | import java.util.Map; |
| 27 | |
| 28 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 29 | * Utility class to retrieve details of an incoming call after receiving an incoming-call intent, |
| 30 | * see {@link CallActivity}. Binds with the specified call services and requests details of incoming |
| 31 | * calls. Upon receipt of the details, yields execution back to the switchboard to complete the |
| 32 | * 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] | 33 | */ |
| 34 | final class IncomingCallsManager { |
| 35 | |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 36 | private final Switchboard mSwitchboard; |
| 37 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 38 | /** Maps call ID to the call. */ |
| 39 | private final Map<String, Call> mPendingIncomingCalls = Maps.newHashMap(); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Persists the specified parameters. |
| 43 | * |
| 44 | * @param switchboard The switchboard. |
| 45 | */ |
| 46 | IncomingCallsManager(Switchboard switchboard) { |
| 47 | mSwitchboard = switchboard; |
| 48 | } |
| 49 | |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 50 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 51 | * Retrieves details of an incoming call through its associated call service (asynchronously). |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 52 | * Starts the timeout sequence in case the call service is unresponsive. |
| 53 | * |
| 54 | * @param call The call object. |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 55 | * @param extras The optional extras passed with the incoming call intent (to be returned to |
| 56 | * the call service via {@link CallService#setIncomingCallId(String, android.os.Bundle)}). |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 57 | */ |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 58 | void retrieveIncomingCall(final Call call, Bundle extras) { |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 59 | ThreadUtil.checkOnMainThread(); |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame^] | 60 | Log.d(this, "retrieveIncomingCall"); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 61 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 62 | final String callId = call.getId(); |
| 63 | // Just to be safe, lets make sure we're not already processing this call. |
| 64 | Preconditions.checkState(!mPendingIncomingCalls.containsKey(callId)); |
| 65 | |
| 66 | mPendingIncomingCalls.put(callId, call); |
| 67 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 68 | Runnable errorCallback = new Runnable() { |
| 69 | @Override public void run() { |
| 70 | handleFailedIncomingCall(call); |
| 71 | } |
| 72 | }; |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 73 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 74 | // TODO(gilad): call.retrieve*Call() seems a bit unusual, consider revisiting. |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 75 | call.getCallService().retrieveIncomingCall(callId, extras, errorCallback); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Notifies the switchboard of a successful incoming call after removing it from the pending |
| 80 | * list. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 81 | * |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 82 | * @param callInfo The details of the call. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 83 | */ |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 84 | void handleSuccessfulIncomingCall(CallInfo callInfo) { |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 85 | ThreadUtil.checkOnMainThread(); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 86 | |
| 87 | Call call = mPendingIncomingCalls.remove(callInfo.getId()); |
| 88 | if (call != null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame^] | 89 | Log.d(this, "Incoming call %s found.", call.getId()); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 90 | call.setHandle(callInfo.getHandle()); |
| 91 | call.setState(callInfo.getState()); |
| 92 | |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 93 | mSwitchboard.handleSuccessfulIncomingCall(call); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Notifies switchboard of the failed incoming call after removing it from the pending list. |
| 99 | * |
| 100 | * @param call The call. |
| 101 | */ |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 102 | private void handleFailedIncomingCall(Call call) { |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 103 | ThreadUtil.checkOnMainThread(); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 104 | |
| 105 | if (mPendingIncomingCalls.remove(call.getId()) != null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame^] | 106 | Log.i(this, "Failed to get details for incoming call %s", call); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 107 | // The call was found still waiting for details. Consider it failed. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 108 | mSwitchboard.handleFailedIncomingCall(call); |
| 109 | } |
| 110 | } |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 111 | } |