Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 1 | /* |
| 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 Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 17 | package com.android.telecomm; |
| 18 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 19 | import android.os.Handler; |
| 20 | import android.os.Looper; |
| 21 | import android.telecomm.CallInfo; |
| 22 | import android.telecomm.ICallServiceAdapter; |
| 23 | |
| 24 | import com.google.common.base.Strings; |
| 25 | |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 26 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 27 | * 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. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame^] | 34 | * TODO(santoscordon): Do we need Binder.clear/restoreCallingIdentity() in the service methods? |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 35 | */ |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 36 | public final class CallServiceAdapter extends ICallServiceAdapter.Stub { |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 37 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 38 | private final CallsManager mCallsManager; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 39 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 40 | private final OutgoingCallsManager mOutgoingCallsManager; |
| 41 | |
| 42 | /** Used to run code (e.g. messages, Runnables) on the main (UI) thread. */ |
| 43 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
| 44 | |
| 45 | /** |
| 46 | * Persists the specified parameters. |
| 47 | */ |
| 48 | CallServiceAdapter(OutgoingCallsManager outgoingCallsManager) { |
| 49 | mCallsManager = CallsManager.getInstance(); |
| 50 | mOutgoingCallsManager = outgoingCallsManager; |
| 51 | } |
| 52 | |
| 53 | /** {@inheritDoc} */ |
| 54 | @Override public void getNextCallId() { |
| 55 | // TODO(santoscordon): needs response object. |
| 56 | } |
| 57 | |
| 58 | /** {@inheritDoc} */ |
| 59 | @Override public void setCompatibleWith(String callId, boolean isCompatible) { |
| 60 | // TODO(santoscordon): fill in. |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * TODO(santoscordon): Rename to handleIncomingCall. |
| 65 | * {@inheritDoc} |
| 66 | */ |
| 67 | @Override public void newIncomingCall(CallInfo callInfo) { |
| 68 | // TODO(santoscordon): fill in. |
| 69 | } |
| 70 | |
| 71 | /** {@inheritDoc} */ |
| 72 | @Override public void handleSuccessfulOutgoingCall(final String callId) { |
| 73 | checkValidCallId(callId); |
| 74 | mHandler.post(new Runnable() { |
| 75 | @Override public void run() { |
| 76 | mOutgoingCallsManager.handleSuccessfulCallAttempt(callId); |
| 77 | } |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | /** {@inheritDoc} */ |
| 82 | @Override public void handleFailedOutgoingCall(final String callId, final String reason) { |
| 83 | checkValidCallId(callId); |
| 84 | mHandler.post(new Runnable() { |
| 85 | @Override public void run() { |
| 86 | mOutgoingCallsManager.handleFailedCallAttempt(callId, reason); |
| 87 | } |
| 88 | }); |
| 89 | } |
| 90 | |
| 91 | /** {@inheritDoc} */ |
| 92 | @Override public void setActive(final String callId) { |
| 93 | checkValidCallId(callId); |
| 94 | mHandler.post(new Runnable() { |
| 95 | @Override public void run() { |
| 96 | mCallsManager.markCallAsActive(callId); |
| 97 | } |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | /** {@inheritDoc} */ |
| 102 | @Override public void setRinging(final String callId) { |
| 103 | checkValidCallId(callId); |
| 104 | mHandler.post(new Runnable() { |
| 105 | @Override public void run() { |
| 106 | mCallsManager.markCallAsRinging(callId); |
| 107 | } |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | /** {@inheritDoc} */ |
| 112 | @Override public void setDialing(final String callId) { |
| 113 | checkValidCallId(callId); |
| 114 | mHandler.post(new Runnable() { |
| 115 | @Override public void run() { |
| 116 | mCallsManager.markCallAsDialing(callId); |
| 117 | } |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | /** {@inheritDoc} */ |
| 122 | @Override public void setDisconnected(final String callId) { |
| 123 | checkValidCallId(callId); |
| 124 | mHandler.post(new Runnable() { |
| 125 | @Override public void run() { |
| 126 | mCallsManager.markCallAsDisconnected(callId); |
| 127 | } |
| 128 | }); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Throws an IllegalArgumentException if the specified call ID is invalid. |
| 133 | * |
| 134 | * @param callId The call ID to check. |
| 135 | */ |
| 136 | private void checkValidCallId(String callId) { |
| 137 | if (Strings.isNullOrEmpty(callId)) { |
| 138 | throw new IllegalArgumentException(); |
| 139 | } |
| 140 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 141 | } |