blob: 0493df8f6eb2d970155d99a458df520a717be3d4 [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 Cordon80d9bdc2014-02-13 18:28:46 -080022
Santos Cordon80d9bdc2014-02-13 18:28:46 -080023import com.google.common.base.Preconditions;
24import com.google.common.collect.Maps;
25
26import java.util.Map;
27
28/**
Sailesh Nepald2dbf122014-03-17 21:19:42 -070029 * Used to retrieve details about an incoming call. This is invoked after an incoming call intent.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080030 */
31final class IncomingCallsManager {
32
Santos Cordon80d9bdc2014-02-13 18:28:46 -080033 private final Switchboard mSwitchboard;
34
Santos Cordon493e8f22014-02-19 03:15:12 -080035 /** Maps call ID to the call. */
36 private final Map<String, Call> mPendingIncomingCalls = Maps.newHashMap();
Santos Cordon80d9bdc2014-02-13 18:28:46 -080037
38 /**
39 * Persists the specified parameters.
40 *
41 * @param switchboard The switchboard.
42 */
43 IncomingCallsManager(Switchboard switchboard) {
44 mSwitchboard = switchboard;
45 }
46
Santos Cordon80d9bdc2014-02-13 18:28:46 -080047 /**
Sailesh Nepald2dbf122014-03-17 21:19:42 -070048 * Retrieves details of an incoming call through its associated call service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080049 *
50 * @param call The call object.
Evan Charltona05805b2014-03-05 08:21:46 -080051 * @param extras The optional extras passed with the incoming call intent (to be returned to
52 * the call service via {@link CallService#setIncomingCallId(String, android.os.Bundle)}).
Santos Cordon80d9bdc2014-02-13 18:28:46 -080053 */
Evan Charltona05805b2014-03-05 08:21:46 -080054 void retrieveIncomingCall(final Call call, Bundle extras) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080055 ThreadUtil.checkOnMainThread();
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080056 Log.d(this, "retrieveIncomingCall");
Santos Cordon80d9bdc2014-02-13 18:28:46 -080057
Santos Cordon493e8f22014-02-19 03:15:12 -080058 final String callId = call.getId();
59 // Just to be safe, lets make sure we're not already processing this call.
60 Preconditions.checkState(!mPendingIncomingCalls.containsKey(callId));
61
62 mPendingIncomingCalls.put(callId, call);
63
Ben Gilad8e55d1d2014-02-26 16:25:56 -080064 Runnable errorCallback = new Runnable() {
65 @Override public void run() {
66 handleFailedIncomingCall(call);
67 }
68 };
Santos Cordon80d9bdc2014-02-13 18:28:46 -080069
Ben Gilad61925612014-03-11 19:06:36 -070070 call.getCallService().setIncomingCallId(callId, extras, errorCallback);
Santos Cordon80d9bdc2014-02-13 18:28:46 -080071 }
72
73 /**
74 * Notifies the switchboard of a successful incoming call after removing it from the pending
75 * list.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080076 *
Santos Cordon493e8f22014-02-19 03:15:12 -080077 * @param callInfo The details of the call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080078 */
Santos Cordon493e8f22014-02-19 03:15:12 -080079 void handleSuccessfulIncomingCall(CallInfo callInfo) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080080 ThreadUtil.checkOnMainThread();
Santos Cordon493e8f22014-02-19 03:15:12 -080081
82 Call call = mPendingIncomingCalls.remove(callInfo.getId());
83 if (call != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080084 Log.d(this, "Incoming call %s found.", call.getId());
Santos Cordon493e8f22014-02-19 03:15:12 -080085 call.setHandle(callInfo.getHandle());
86 call.setState(callInfo.getState());
87
Santos Cordon80d9bdc2014-02-13 18:28:46 -080088 mSwitchboard.handleSuccessfulIncomingCall(call);
89 }
90 }
91
92 /**
93 * Notifies switchboard of the failed incoming call after removing it from the pending list.
94 *
95 * @param call The call.
96 */
Santos Cordon493e8f22014-02-19 03:15:12 -080097 private void handleFailedIncomingCall(Call call) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080098 ThreadUtil.checkOnMainThread();
Santos Cordon493e8f22014-02-19 03:15:12 -080099
100 if (mPendingIncomingCalls.remove(call.getId()) != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800101 Log.i(this, "Failed to get details for incoming call %s", call);
Santos Cordon493e8f22014-02-19 03:15:12 -0800102 // The call was found still waiting for details. Consider it failed.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800103 mSwitchboard.handleFailedIncomingCall(call);
104 }
105 }
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800106}