blob: 0d98dc4c2033bed5119c53391eb101d4f50bdf69 [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() {
Santos Cordon4b2c1192014-03-19 18:15:38 -070066 handleFailedIncomingCall(callId);
Ben Gilad8e55d1d2014-02-26 16:25:56 -080067 }
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());
Sailesh Nepal810735e2014-03-18 18:15:46 -070085 mSwitchboard.handleSuccessfulIncomingCall(call, callInfo);
Santos Cordon80d9bdc2014-02-13 18:28:46 -080086 }
87 }
88
89 /**
90 * Notifies switchboard of the failed incoming call after removing it from the pending list.
91 *
Santos Cordon4b2c1192014-03-19 18:15:38 -070092 * @param callId The ID of the call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080093 */
Santos Cordon4b2c1192014-03-19 18:15:38 -070094 void handleFailedIncomingCall(String callId) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080095 ThreadUtil.checkOnMainThread();
Santos Cordon493e8f22014-02-19 03:15:12 -080096
Santos Cordon4b2c1192014-03-19 18:15:38 -070097 Call call = mPendingIncomingCalls.remove(callId);
98 if (call != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080099 Log.i(this, "Failed to get details for incoming call %s", call);
Santos Cordon493e8f22014-02-19 03:15:12 -0800100 // The call was found still waiting for details. Consider it failed.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800101 mSwitchboard.handleFailedIncomingCall(call);
102 }
103 }
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800104}