blob: 905fa6a4fab4ddd619ae52c3c33def74accdd4ac [file] [log] [blame]
Ben Gilad0407fb22014-01-09 16:18:41 -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 Cordon049b7b62014-01-30 05:34:26 -080019import android.os.RemoteException;
Santos Cordon0b03b4b2014-01-29 18:01:59 -080020import android.telecomm.CallInfo;
21import android.telecomm.CallState;
Santos Cordon681663d2014-01-30 04:32:15 -080022import android.telecomm.ICallService;
Santos Cordon049b7b62014-01-30 05:34:26 -080023import android.util.Log;
Santos Cordon0b03b4b2014-01-29 18:01:59 -080024
Ben Gilad0407fb22014-01-09 16:18:41 -080025import java.util.Date;
26
Ben Gilad2495d572014-01-09 17:26:19 -080027/**
28 * Encapsulates all aspects of a given phone call throughout its lifecycle, starting
29 * from the time the call intent was received by Telecomm (vs. the time the call was
30 * connected etc).
31 */
Ben Gilad0407fb22014-01-09 16:18:41 -080032final class Call {
Santos Cordon049b7b62014-01-30 05:34:26 -080033 private static final String TAG = Call.class.getSimpleName();
Ben Gilad0407fb22014-01-09 16:18:41 -080034
Ben Gilad0bf5b912014-01-28 17:55:57 -080035 /**
36 * Unique identifier for the call as a UUID string.
37 */
38 private final String mId;
39
Santos Cordon0b03b4b2014-01-29 18:01:59 -080040 /**
41 * The state of the call.
42 */
43 private CallState mState;
44
Ben Gilad0407fb22014-01-09 16:18:41 -080045 /** The handle with which to establish this call. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080046 private final String mHandle;
Ben Gilad0407fb22014-01-09 16:18:41 -080047
48 /** Additional contact information beyond handle above, optional. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080049 private final ContactInfo mContactInfo;
Ben Gilad0407fb22014-01-09 16:18:41 -080050
51 /**
52 * The time this call was created, typically also the time this call was added to the set
53 * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard.
54 * Beyond logging and such, may also be used for bookkeeping and specifically for marking
55 * certain call attempts as failed attempts.
56 */
57 private final Date mCreationTime;
58
59 /**
Santos Cordon681663d2014-01-30 04:32:15 -080060 * The call service which is currently connecting this call, null as long as the call is not
61 * connected.
62 */
63 private ICallService mCallService;
64
65 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -080066 * Read-only and parcelable version of this call.
67 */
68 private CallInfo mCallInfo;
69
70 /**
Ben Gilad0407fb22014-01-09 16:18:41 -080071 * Persists the specified parameters and initializes the new instance.
72 *
73 * @param handle The handle to dial.
74 * @param contactInfo Information about the entity being called.
75 */
Ben Gilad0bf5b912014-01-28 17:55:57 -080076 Call(String handle, ContactInfo contactInfo) {
77 // TODO(gilad): Pass this in etc.
78 mId = "dummy";
Santos Cordon0b03b4b2014-01-29 18:01:59 -080079 mState = CallState.NEW;
Ben Gilad0407fb22014-01-09 16:18:41 -080080 mHandle = handle;
81 mContactInfo = contactInfo;
Ben Gilad0407fb22014-01-09 16:18:41 -080082 mCreationTime = new Date();
83 }
84
Ben Gilad0bf5b912014-01-28 17:55:57 -080085 String getId() {
86 return mId;
87 }
88
Santos Cordon0b03b4b2014-01-29 18:01:59 -080089 CallState getState() {
90 return mState;
91 }
92
93 /**
94 * Sets the call state. Although there exists the notion of appropriate state transitions
95 * (see {@link CallState}), in practice those expectations break down when cellular systems
96 * misbehave and they do this very often. The result is that we do not enforce state transitions
97 * and instead keep the code resilient to unexpected state changes.
98 */
99 void setState(CallState state) {
100 mState = state;
101 clearCallInfo();
102 }
103
Ben Gilad0bf5b912014-01-28 17:55:57 -0800104 String getHandle() {
105 return mHandle;
106 }
107
108 ContactInfo getContactInfo() {
109 return mContactInfo;
110 }
111
Ben Gilad0407fb22014-01-09 16:18:41 -0800112 /**
113 * @return The "age" of this call object in milliseconds, which typically also represents the
114 * period since this call was added to the set pending outgoing calls, see mCreationTime.
115 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800116 long getAgeInMilliseconds() {
Ben Gilad0407fb22014-01-09 16:18:41 -0800117 return new Date().getTime() - mCreationTime.getTime();
118 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800119
Santos Cordon681663d2014-01-30 04:32:15 -0800120 ICallService getCallService() {
121 return mCallService;
122 }
123
124 void setCallService(ICallService callService) {
125 mCallService = callService;
126 }
127
128 /**
129 * Clears the associated call service.
130 */
131 void clearCallService() {
132 setCallService(null);
133 }
134
Santos Cordon049b7b62014-01-30 05:34:26 -0800135 /*
136 * Attempts to disconnect the call through the call service.
137 */
138 void disconnect() {
139 if (mCallService == null) {
140 Log.w(TAG, "disconnect() request on a call without a call service.");
141 } else {
142 try {
143 Log.i(TAG, "Send disconnect to call service for call with id " + mId);
144 mCallService.disconnect(mId);
145 } catch (RemoteException e) {
146 Log.e(TAG, "Disconnect attempt failed.", e);
147 }
148 }
149 }
150
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800151 /**
152 * @return An object containing read-only information about this call.
153 */
154 CallInfo toCallInfo() {
155 if (mCallInfo == null) {
156 mCallInfo = new CallInfo(mId, mState, mHandle);
157 }
158 return mCallInfo;
159 }
160
161 /**
162 * Resets the cached read-only version of this call.
163 */
164 private void clearCallInfo() {
165 mCallInfo = null;
166 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800167}