Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -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 | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 19 | import android.os.RemoteException; |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 20 | import android.telecomm.CallInfo; |
| 21 | import android.telecomm.CallState; |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 22 | import android.util.Log; |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 23 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 24 | import java.util.Date; |
| 25 | |
Ben Gilad | 2495d57 | 2014-01-09 17:26:19 -0800 | [diff] [blame] | 26 | /** |
| 27 | * Encapsulates all aspects of a given phone call throughout its lifecycle, starting |
| 28 | * from the time the call intent was received by Telecomm (vs. the time the call was |
| 29 | * connected etc). |
| 30 | */ |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 31 | final class Call { |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 32 | private static final String TAG = Call.class.getSimpleName(); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 33 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 34 | /** |
| 35 | * Unique identifier for the call as a UUID string. |
| 36 | */ |
| 37 | private final String mId; |
| 38 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 39 | /** |
| 40 | * The state of the call. |
| 41 | */ |
| 42 | private CallState mState; |
| 43 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 44 | /** The handle with which to establish this call. */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 45 | private final String mHandle; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 46 | |
| 47 | /** Additional contact information beyond handle above, optional. */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 48 | private final ContactInfo mContactInfo; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * The time this call was created, typically also the time this call was added to the set |
| 52 | * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard. |
| 53 | * Beyond logging and such, may also be used for bookkeeping and specifically for marking |
| 54 | * certain call attempts as failed attempts. |
| 55 | */ |
| 56 | private final Date mCreationTime; |
| 57 | |
| 58 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 59 | * The call service which is currently connecting this call, null as long as the call is not |
| 60 | * connected. |
| 61 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 62 | private CallServiceWrapper mCallService; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 63 | |
| 64 | /** |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 65 | * Read-only and parcelable version of this call. |
| 66 | */ |
| 67 | private CallInfo mCallInfo; |
| 68 | |
| 69 | /** |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 70 | * Persists the specified parameters and initializes the new instance. |
| 71 | * |
| 72 | * @param handle The handle to dial. |
| 73 | * @param contactInfo Information about the entity being called. |
| 74 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 75 | Call(String handle, ContactInfo contactInfo) { |
| 76 | // TODO(gilad): Pass this in etc. |
| 77 | mId = "dummy"; |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 78 | mState = CallState.NEW; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 79 | mHandle = handle; |
| 80 | mContactInfo = contactInfo; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 81 | mCreationTime = new Date(); |
| 82 | } |
| 83 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 84 | String getId() { |
| 85 | return mId; |
| 86 | } |
| 87 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 88 | CallState getState() { |
| 89 | return mState; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Sets the call state. Although there exists the notion of appropriate state transitions |
| 94 | * (see {@link CallState}), in practice those expectations break down when cellular systems |
| 95 | * misbehave and they do this very often. The result is that we do not enforce state transitions |
| 96 | * and instead keep the code resilient to unexpected state changes. |
| 97 | */ |
| 98 | void setState(CallState state) { |
| 99 | mState = state; |
| 100 | clearCallInfo(); |
| 101 | } |
| 102 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 103 | String getHandle() { |
| 104 | return mHandle; |
| 105 | } |
| 106 | |
| 107 | ContactInfo getContactInfo() { |
| 108 | return mContactInfo; |
| 109 | } |
| 110 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 111 | /** |
| 112 | * @return The "age" of this call object in milliseconds, which typically also represents the |
| 113 | * period since this call was added to the set pending outgoing calls, see mCreationTime. |
| 114 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 115 | long getAgeInMilliseconds() { |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 116 | return new Date().getTime() - mCreationTime.getTime(); |
| 117 | } |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 118 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 119 | CallServiceWrapper getCallService() { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 120 | return mCallService; |
| 121 | } |
| 122 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 123 | void setCallService(CallServiceWrapper callService) { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 124 | mCallService = callService; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Clears the associated call service. |
| 129 | */ |
| 130 | void clearCallService() { |
| 131 | setCallService(null); |
| 132 | } |
| 133 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 134 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 135 | * Attempts to disconnect the call through the call service. |
| 136 | */ |
| 137 | void disconnect() { |
| 138 | if (mCallService == null) { |
| 139 | Log.w(TAG, "disconnect() request on a call without a call service."); |
| 140 | } else { |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 141 | Log.i(TAG, "Send disconnect to call service for call with id " + mId); |
| 142 | // The call isn't officially disconnected until the call service confirms that the call |
| 143 | // was actually disconnected. Only then is the association between call and call service |
| 144 | // severed, see {@link CallsManager#markCallAsDisconnected}. |
| 145 | mCallService.disconnect(mId); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Santos Cordon | 0b03b4b | 2014-01-29 18:01:59 -0800 | [diff] [blame] | 149 | /** |
| 150 | * @return An object containing read-only information about this call. |
| 151 | */ |
| 152 | CallInfo toCallInfo() { |
| 153 | if (mCallInfo == null) { |
| 154 | mCallInfo = new CallInfo(mId, mState, mHandle); |
| 155 | } |
| 156 | return mCallInfo; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Resets the cached read-only version of this call. |
| 161 | */ |
| 162 | private void clearCallInfo() { |
| 163 | mCallInfo = null; |
| 164 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 165 | } |