blob: 5fa80f53366dc02a652212458393f6beb8bc608e [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;
Sailesh Nepalc92c4362014-07-04 18:33:21 -070020import android.telecomm.ConnectionService;
21import android.telecomm.ConnectionRequest;
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;
Sailesh Nepale59bb192014-04-01 18:33:59 -070025import com.google.common.collect.Sets;
Santos Cordon80d9bdc2014-02-13 18:28:46 -080026
Sailesh Nepale59bb192014-04-01 18:33:59 -070027import java.util.Set;
Santos Cordon80d9bdc2014-02-13 18:28:46 -080028
29/**
Sailesh Nepald2dbf122014-03-17 21:19:42 -070030 * Used to retrieve details about an incoming call. This is invoked after an incoming call intent.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080031 */
32final class IncomingCallsManager {
33
Sailesh Nepale59bb192014-04-01 18:33:59 -070034 private final Set<Call> mPendingIncomingCalls = Sets.newLinkedHashSet();
Santos Cordon80d9bdc2014-02-13 18:28:46 -080035
36 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -070037 * Retrieves details of an incoming call through its associated connection service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080038 *
39 * @param call The call object.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080040 */
Sailesh Nepal905dfba2014-07-14 08:20:41 -070041 void retrieveIncomingCall(final Call call) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080042 ThreadUtil.checkOnMainThread();
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080043 Log.d(this, "retrieveIncomingCall");
Santos Cordon80d9bdc2014-02-13 18:28:46 -080044
Santos Cordon493e8f22014-02-19 03:15:12 -080045 // Just to be safe, lets make sure we're not already processing this call.
Sailesh Nepale59bb192014-04-01 18:33:59 -070046 Preconditions.checkState(!mPendingIncomingCalls.contains(call));
Santos Cordon493e8f22014-02-19 03:15:12 -080047
Sailesh Nepale59bb192014-04-01 18:33:59 -070048 mPendingIncomingCalls.add(call);
Santos Cordon493e8f22014-02-19 03:15:12 -080049
Ben Gilad8e55d1d2014-02-26 16:25:56 -080050 Runnable errorCallback = new Runnable() {
51 @Override public void run() {
Sailesh Nepale59bb192014-04-01 18:33:59 -070052 handleFailedIncomingCall(call);
Ben Gilad8e55d1d2014-02-26 16:25:56 -080053 }
54 };
Santos Cordon80d9bdc2014-02-13 18:28:46 -080055
Sailesh Nepal905dfba2014-07-14 08:20:41 -070056 call.getConnectionService().createIncomingCall(call, errorCallback);
Santos Cordon80d9bdc2014-02-13 18:28:46 -080057 }
58
59 /**
Santos Cordon74d420b2014-05-07 14:38:47 -070060 * Notifies the incoming call of success after removing it from the pending
Santos Cordon80d9bdc2014-02-13 18:28:46 -080061 * list.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080062 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -070063 * @param request The details of the call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080064 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -070065 void handleSuccessfulIncomingCall(Call call, ConnectionRequest request) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080066 ThreadUtil.checkOnMainThread();
Santos Cordon493e8f22014-02-19 03:15:12 -080067
Sailesh Nepale59bb192014-04-01 18:33:59 -070068 if (mPendingIncomingCalls.contains(call)) {
69 Log.d(this, "Incoming call %s found.", call);
70 mPendingIncomingCalls.remove(call);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070071 call.handleVerifiedIncoming(request);
Santos Cordon80d9bdc2014-02-13 18:28:46 -080072 }
73 }
74
75 /**
Santos Cordon74d420b2014-05-07 14:38:47 -070076 * Notifies incoming call of failure after removing it from the pending list.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080077 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070078 void handleFailedIncomingCall(Call call) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080079 ThreadUtil.checkOnMainThread();
Santos Cordon493e8f22014-02-19 03:15:12 -080080
Sailesh Nepale59bb192014-04-01 18:33:59 -070081 if (mPendingIncomingCalls.contains(call)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080082 Log.i(this, "Failed to get details for incoming call %s", call);
Sailesh Nepale59bb192014-04-01 18:33:59 -070083 mPendingIncomingCalls.remove(call);
Santos Cordon493e8f22014-02-19 03:15:12 -080084 // The call was found still waiting for details. Consider it failed.
Santos Cordon74d420b2014-05-07 14:38:47 -070085 call.handleFailedIncoming();
Santos Cordon80d9bdc2014-02-13 18:28:46 -080086 }
87 }
Santos Cordon80d9bdc2014-02-13 18:28:46 -080088}