blob: 197f6992fc1b883ae8ea6367e9b60d88b8cf638e [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.
Evan Charltona05805b2014-03-05 08:21:46 -080040 * @param extras The optional extras passed with the incoming call intent (to be returned to
Sailesh Nepalc92c4362014-07-04 18:33:21 -070041 * the connection service via
42 * {@link ConnectionService#createIncomingCall(ConnectionRequest)}.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080043 */
Evan Charltona05805b2014-03-05 08:21:46 -080044 void retrieveIncomingCall(final Call call, Bundle extras) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080045 ThreadUtil.checkOnMainThread();
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080046 Log.d(this, "retrieveIncomingCall");
Santos Cordon80d9bdc2014-02-13 18:28:46 -080047
Santos Cordon493e8f22014-02-19 03:15:12 -080048 // Just to be safe, lets make sure we're not already processing this call.
Sailesh Nepale59bb192014-04-01 18:33:59 -070049 Preconditions.checkState(!mPendingIncomingCalls.contains(call));
Santos Cordon493e8f22014-02-19 03:15:12 -080050
Sailesh Nepale59bb192014-04-01 18:33:59 -070051 mPendingIncomingCalls.add(call);
Santos Cordon493e8f22014-02-19 03:15:12 -080052
Ben Gilad8e55d1d2014-02-26 16:25:56 -080053 Runnable errorCallback = new Runnable() {
54 @Override public void run() {
Sailesh Nepale59bb192014-04-01 18:33:59 -070055 handleFailedIncomingCall(call);
Ben Gilad8e55d1d2014-02-26 16:25:56 -080056 }
57 };
Santos Cordon80d9bdc2014-02-13 18:28:46 -080058
Sailesh Nepalc92c4362014-07-04 18:33:21 -070059 call.getConnectionService().createIncomingCall(call, extras, errorCallback);
Santos Cordon80d9bdc2014-02-13 18:28:46 -080060 }
61
62 /**
Santos Cordon74d420b2014-05-07 14:38:47 -070063 * Notifies the incoming call of success after removing it from the pending
Santos Cordon80d9bdc2014-02-13 18:28:46 -080064 * list.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080065 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -070066 * @param request The details of the call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080067 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -070068 void handleSuccessfulIncomingCall(Call call, ConnectionRequest request) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080069 ThreadUtil.checkOnMainThread();
Santos Cordon493e8f22014-02-19 03:15:12 -080070
Sailesh Nepale59bb192014-04-01 18:33:59 -070071 if (mPendingIncomingCalls.contains(call)) {
72 Log.d(this, "Incoming call %s found.", call);
73 mPendingIncomingCalls.remove(call);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070074 call.handleVerifiedIncoming(request);
Santos Cordon80d9bdc2014-02-13 18:28:46 -080075 }
76 }
77
78 /**
Santos Cordon74d420b2014-05-07 14:38:47 -070079 * Notifies incoming call of failure after removing it from the pending list.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080080 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070081 void handleFailedIncomingCall(Call call) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080082 ThreadUtil.checkOnMainThread();
Santos Cordon493e8f22014-02-19 03:15:12 -080083
Sailesh Nepale59bb192014-04-01 18:33:59 -070084 if (mPendingIncomingCalls.contains(call)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080085 Log.i(this, "Failed to get details for incoming call %s", call);
Sailesh Nepale59bb192014-04-01 18:33:59 -070086 mPendingIncomingCalls.remove(call);
Santos Cordon493e8f22014-02-19 03:15:12 -080087 // The call was found still waiting for details. Consider it failed.
Santos Cordon74d420b2014-05-07 14:38:47 -070088 call.handleFailedIncoming();
Santos Cordon80d9bdc2014-02-13 18:28:46 -080089 }
90 }
Santos Cordon80d9bdc2014-02-13 18:28:46 -080091}