blob: 41b98f238cbf2c3b2b5f37c1fdc9fe04f680a7ff [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
Santos Cordon80d9bdc2014-02-13 18:28:46 -080034 private final Switchboard mSwitchboard;
Sailesh Nepale59bb192014-04-01 18:33:59 -070035 private final Set<Call> mPendingIncomingCalls = Sets.newLinkedHashSet();
Santos Cordon80d9bdc2014-02-13 18:28:46 -080036
37 /**
38 * Persists the specified parameters.
39 *
40 * @param switchboard The switchboard.
41 */
42 IncomingCallsManager(Switchboard switchboard) {
43 mSwitchboard = switchboard;
44 }
45
Santos Cordon80d9bdc2014-02-13 18:28:46 -080046 /**
Sailesh Nepald2dbf122014-03-17 21:19:42 -070047 * Retrieves details of an incoming call through its associated call service.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080048 *
49 * @param call The call object.
Evan Charltona05805b2014-03-05 08:21:46 -080050 * @param extras The optional extras passed with the incoming call intent (to be returned to
51 * the call service via {@link CallService#setIncomingCallId(String, android.os.Bundle)}).
Santos Cordon80d9bdc2014-02-13 18:28:46 -080052 */
Evan Charltona05805b2014-03-05 08:21:46 -080053 void retrieveIncomingCall(final Call call, Bundle extras) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080054 ThreadUtil.checkOnMainThread();
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080055 Log.d(this, "retrieveIncomingCall");
Santos Cordon80d9bdc2014-02-13 18:28:46 -080056
Santos Cordon493e8f22014-02-19 03:15:12 -080057 // Just to be safe, lets make sure we're not already processing this call.
Sailesh Nepale59bb192014-04-01 18:33:59 -070058 Preconditions.checkState(!mPendingIncomingCalls.contains(call));
Santos Cordon493e8f22014-02-19 03:15:12 -080059
Sailesh Nepale59bb192014-04-01 18:33:59 -070060 mPendingIncomingCalls.add(call);
Santos Cordon493e8f22014-02-19 03:15:12 -080061
Ben Gilad8e55d1d2014-02-26 16:25:56 -080062 Runnable errorCallback = new Runnable() {
63 @Override public void run() {
Sailesh Nepale59bb192014-04-01 18:33:59 -070064 handleFailedIncomingCall(call);
Ben Gilad8e55d1d2014-02-26 16:25:56 -080065 }
66 };
Santos Cordon80d9bdc2014-02-13 18:28:46 -080067
Sailesh Nepale59bb192014-04-01 18:33:59 -070068 call.getCallService().setIncomingCallId(call, extras, errorCallback);
Santos Cordon80d9bdc2014-02-13 18:28:46 -080069 }
70
71 /**
72 * Notifies the switchboard of a successful incoming call after removing it from the pending
73 * list.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080074 *
Santos Cordon493e8f22014-02-19 03:15:12 -080075 * @param callInfo The details of the call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080076 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070077 void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080078 ThreadUtil.checkOnMainThread();
Santos Cordon493e8f22014-02-19 03:15:12 -080079
Sailesh Nepale59bb192014-04-01 18:33:59 -070080 if (mPendingIncomingCalls.contains(call)) {
81 Log.d(this, "Incoming call %s found.", call);
82 mPendingIncomingCalls.remove(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070083 mSwitchboard.handleSuccessfulIncomingCall(call, callInfo);
Santos Cordon80d9bdc2014-02-13 18:28:46 -080084 }
85 }
86
87 /**
88 * Notifies switchboard of the failed incoming call after removing it from the pending list.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080089 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070090 void handleFailedIncomingCall(Call call) {
Santos Cordon80d9bdc2014-02-13 18:28:46 -080091 ThreadUtil.checkOnMainThread();
Santos Cordon493e8f22014-02-19 03:15:12 -080092
Sailesh Nepale59bb192014-04-01 18:33:59 -070093 if (mPendingIncomingCalls.contains(call)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080094 Log.i(this, "Failed to get details for incoming call %s", call);
Sailesh Nepale59bb192014-04-01 18:33:59 -070095 mPendingIncomingCalls.remove(call);
Santos Cordon493e8f22014-02-19 03:15:12 -080096 // The call was found still waiting for details. Consider it failed.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080097 mSwitchboard.handleFailedIncomingCall(call);
98 }
99 }
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800100}