blob: b75f487b536840869cfa6aa994993dacfe7e39cc [file] [log] [blame]
Santos Cordon681663d2014-01-30 04:32:15 -08001/*
2 * Copyright (C) 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
Ben Gilad9f2bed32013-12-12 17:43:26 -080017package com.android.telecomm;
18
Santos Cordon681663d2014-01-30 04:32:15 -080019import android.os.Handler;
20import android.os.Looper;
21import android.telecomm.CallInfo;
22import android.telecomm.ICallServiceAdapter;
23
24import com.google.common.base.Strings;
25
Ben Gilad9f2bed32013-12-12 17:43:26 -080026/**
Santos Cordon681663d2014-01-30 04:32:15 -080027 * Used by call services in order to update state and control calls while the call service is bound
28 * to Telecomm. Each call service is given its own instance for the lifetime of the binding between
29 * Telecomm and the call service.
30 * TODO(santoscordon): Whenever we get any method invocations from the call service, we need to
31 * check that the invocation is expected from that call service.
32 * TODO(santoscordon): Move away from Runnable objects and into messages so that we create fewer
33 * objects per IPC method call.
Ben Gilad9f2bed32013-12-12 17:43:26 -080034 */
Santos Cordon681663d2014-01-30 04:32:15 -080035public final class CallServiceAdapter extends ICallServiceAdapter.Stub {
Ben Gilad9f2bed32013-12-12 17:43:26 -080036
Santos Cordon681663d2014-01-30 04:32:15 -080037 private final CallsManager mCallsManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080038
Santos Cordon681663d2014-01-30 04:32:15 -080039 private final OutgoingCallsManager mOutgoingCallsManager;
40
41 /** Used to run code (e.g. messages, Runnables) on the main (UI) thread. */
42 private final Handler mHandler = new Handler(Looper.getMainLooper());
43
44 /**
45 * Persists the specified parameters.
46 */
47 CallServiceAdapter(OutgoingCallsManager outgoingCallsManager) {
48 mCallsManager = CallsManager.getInstance();
49 mOutgoingCallsManager = outgoingCallsManager;
50 }
51
52 /** {@inheritDoc} */
53 @Override public void getNextCallId() {
54 // TODO(santoscordon): needs response object.
55 }
56
57 /** {@inheritDoc} */
58 @Override public void setCompatibleWith(String callId, boolean isCompatible) {
59 // TODO(santoscordon): fill in.
60 }
61
62 /**
Santos Cordon681663d2014-01-30 04:32:15 -080063 * {@inheritDoc}
64 */
Ben Gilad13329fd2014-02-11 17:20:29 -080065 @Override public void handleIncomingCall(CallInfo callInfo) {
Santos Cordon681663d2014-01-30 04:32:15 -080066 // TODO(santoscordon): fill in.
67 }
68
69 /** {@inheritDoc} */
70 @Override public void handleSuccessfulOutgoingCall(final String callId) {
71 checkValidCallId(callId);
72 mHandler.post(new Runnable() {
73 @Override public void run() {
74 mOutgoingCallsManager.handleSuccessfulCallAttempt(callId);
75 }
76 });
77 }
78
79 /** {@inheritDoc} */
80 @Override public void handleFailedOutgoingCall(final String callId, final String reason) {
81 checkValidCallId(callId);
82 mHandler.post(new Runnable() {
83 @Override public void run() {
84 mOutgoingCallsManager.handleFailedCallAttempt(callId, reason);
85 }
86 });
87 }
88
89 /** {@inheritDoc} */
90 @Override public void setActive(final String callId) {
91 checkValidCallId(callId);
92 mHandler.post(new Runnable() {
93 @Override public void run() {
94 mCallsManager.markCallAsActive(callId);
95 }
96 });
97 }
98
99 /** {@inheritDoc} */
100 @Override public void setRinging(final String callId) {
101 checkValidCallId(callId);
102 mHandler.post(new Runnable() {
103 @Override public void run() {
104 mCallsManager.markCallAsRinging(callId);
105 }
106 });
107 }
108
109 /** {@inheritDoc} */
110 @Override public void setDialing(final String callId) {
111 checkValidCallId(callId);
112 mHandler.post(new Runnable() {
113 @Override public void run() {
114 mCallsManager.markCallAsDialing(callId);
115 }
116 });
117 }
118
119 /** {@inheritDoc} */
120 @Override public void setDisconnected(final String callId) {
121 checkValidCallId(callId);
122 mHandler.post(new Runnable() {
123 @Override public void run() {
124 mCallsManager.markCallAsDisconnected(callId);
125 }
126 });
127 }
128
129 /**
130 * Throws an IllegalArgumentException if the specified call ID is invalid.
131 *
132 * @param callId The call ID to check.
133 */
134 private void checkValidCallId(String callId) {
135 if (Strings.isNullOrEmpty(callId)) {
136 throw new IllegalArgumentException();
137 }
138 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800139}