blob: 4d735b1875543643de59f83eb54aa73258756141 [file] [log] [blame]
Ben Gilad9f2bed32013-12-12 17:43:26 -08001package com.android.telecomm;
2
3import com.android.telecomm.exceptions.CallServiceUnavailableException;
4import com.android.telecomm.exceptions.OutgoingCallException;
5
6import java.util.ArrayList;
7import java.util.List;
8
9/** Package private */
10class Switchboard {
11
12 private List<CallService> callServices = new ArrayList<CallService>();
13
14 /** Package private */
15 void addCallService(CallService callService) {
16 if (callService != null && !callServices.contains(callService)) {
17 callServices.add(callService);
18 }
19 }
20
21 /** Package private */
22 void placeOutgoingCall(String userInput, ContactInfo contactInfo)
23 throws CallServiceUnavailableException {
24
25 if (callServices.isEmpty()) {
26 // No call services, bail out.
27 // TODO(contacts-team): Add logging?
28 throw new CallServiceUnavailableException();
29 }
30
31 List<CallService> compatibleCallServices = new ArrayList<CallService>();
32 for (CallService service : callServices) {
33 if (service.isCompatibleWith(userInput, contactInfo)) {
34 // NOTE(android-contacts): If we end up taking the liberty to issue
35 // calls not using the explicit user input (in case one is provided)
36 // and instead pull an alternative method of communication from the
37 // specified user-info object, it may be desirable to give precedence
38 // to services that can in fact respect the user's intent.
39 compatibleCallServices.add(service);
40 }
41 }
42
43 if (compatibleCallServices.isEmpty()) {
44 // None of the available call services is suitable for making this call.
45 // TODO(contacts-team): Same here re logging.
46 throw new CallServiceUnavailableException();
47 }
48
49 // NOTE(android-team): At this point we can also prompt the user for
50 // preference, i.e. instead of the logic just below.
51 if (compatibleCallServices.size() > 1) {
52 compatibleCallServices = sort(compatibleCallServices);
53 }
54 for (CallService service : compatibleCallServices) {
55 try {
56 service.placeOutgoingCall(userInput, contactInfo);
57 return;
58 } catch (OutgoingCallException ignored) { }
59 }
60 }
61
62 private List<CallService> sort(List<CallService> callServices) {
63 // TODO(android-contacts): Sort by reliability, cost, and ultimately
64 // the desirability to issue a given call over each of the specified
65 // call services.
66 return callServices;
67 }
68}