blob: 5a874ea62b23683e523e783e0647630ceb6a0ec1 [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;
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 Nepald2dbf122014-03-17 21:19:42 -070037 * Retrieves details of an incoming call through its associated call 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
41 * the call service via {@link CallService#setIncomingCallId(String, android.os.Bundle)}).
Santos Cordon80d9bdc2014-02-13 18:28:46 -080042 */
Evan Charltona05805b2014-03-05 08:21:46 -080043 void retrieveIncomingCall(final Call call, Bundle extras) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080044 ThreadUtil.checkOnMainThread();
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080045 Log.d(this, "retrieveIncomingCall");
Santos Cordon80d9bdc2014-02-13 18:28:46 -080046
Santos Cordon493e8f22014-02-19 03:15:12 -080047 // Just to be safe, lets make sure we're not already processing this call.
Sailesh Nepale59bb192014-04-01 18:33:59 -070048 Preconditions.checkState(!mPendingIncomingCalls.contains(call));
Santos Cordon493e8f22014-02-19 03:15:12 -080049
Sailesh Nepale59bb192014-04-01 18:33:59 -070050 mPendingIncomingCalls.add(call);
Santos Cordon493e8f22014-02-19 03:15:12 -080051
Ben Gilad8e55d1d2014-02-26 16:25:56 -080052 Runnable errorCallback = new Runnable() {
53 @Override public void run() {
Sailesh Nepale59bb192014-04-01 18:33:59 -070054 handleFailedIncomingCall(call);
Ben Gilad8e55d1d2014-02-26 16:25:56 -080055 }
56 };
Santos Cordon80d9bdc2014-02-13 18:28:46 -080057
Sailesh Nepale59bb192014-04-01 18:33:59 -070058 call.getCallService().setIncomingCallId(call, extras, errorCallback);
Santos Cordon80d9bdc2014-02-13 18:28:46 -080059 }
60
61 /**
Santos Cordon74d420b2014-05-07 14:38:47 -070062 * Notifies the incoming call of success after removing it from the pending
Santos Cordon80d9bdc2014-02-13 18:28:46 -080063 * list.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080064 *
Santos Cordon493e8f22014-02-19 03:15:12 -080065 * @param callInfo The details of the call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080066 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070067 void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080068 ThreadUtil.checkOnMainThread();
Santos Cordon493e8f22014-02-19 03:15:12 -080069
Sailesh Nepale59bb192014-04-01 18:33:59 -070070 if (mPendingIncomingCalls.contains(call)) {
71 Log.d(this, "Incoming call %s found.", call);
72 mPendingIncomingCalls.remove(call);
Santos Cordon2174fb52014-05-29 08:22:56 -070073 call.handleVerifiedIncoming(callInfo);
Santos Cordon80d9bdc2014-02-13 18:28:46 -080074 }
75 }
76
77 /**
Santos Cordon74d420b2014-05-07 14:38:47 -070078 * Notifies incoming call of failure after removing it from the pending list.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080079 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070080 void handleFailedIncomingCall(Call call) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080081 ThreadUtil.checkOnMainThread();
Santos Cordon493e8f22014-02-19 03:15:12 -080082
Sailesh Nepale59bb192014-04-01 18:33:59 -070083 if (mPendingIncomingCalls.contains(call)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080084 Log.i(this, "Failed to get details for incoming call %s", call);
Sailesh Nepale59bb192014-04-01 18:33:59 -070085 mPendingIncomingCalls.remove(call);
Santos Cordon493e8f22014-02-19 03:15:12 -080086 // The call was found still waiting for details. Consider it failed.
Santos Cordon74d420b2014-05-07 14:38:47 -070087 call.handleFailedIncoming();
Santos Cordon80d9bdc2014-02-13 18:28:46 -080088 }
89 }
Santos Cordon80d9bdc2014-02-13 18:28:46 -080090}