blob: ea62da498a6aaf903ac7e38439af9b3f023e0dd0 [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 Cordon049b7b62014-01-30 05:34:26 -080022import android.util.Log;
Santos Cordon0b03b4b2014-01-29 18:01:59 -080023
Ben Gilad0407fb22014-01-09 16:18:41 -080024import java.util.Date;
25
Ben Gilad2495d572014-01-09 17:26:19 -080026/**
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 Gilad0407fb22014-01-09 16:18:41 -080031final class Call {
Santos Cordon049b7b62014-01-30 05:34:26 -080032 private static final String TAG = Call.class.getSimpleName();
Ben Gilad0407fb22014-01-09 16:18:41 -080033
Ben Gilad0bf5b912014-01-28 17:55:57 -080034 /**
35 * Unique identifier for the call as a UUID string.
36 */
37 private final String mId;
38
Santos Cordon0b03b4b2014-01-29 18:01:59 -080039 /**
40 * The state of the call.
41 */
42 private CallState mState;
43
Ben Gilad0407fb22014-01-09 16:18:41 -080044 /** The handle with which to establish this call. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080045 private final String mHandle;
Ben Gilad0407fb22014-01-09 16:18:41 -080046
47 /** Additional contact information beyond handle above, optional. */
Ben Gilad0bf5b912014-01-28 17:55:57 -080048 private final ContactInfo mContactInfo;
Ben Gilad0407fb22014-01-09 16:18:41 -080049
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 Cordon681663d2014-01-30 04:32:15 -080059 * The call service which is currently connecting this call, null as long as the call is not
60 * connected.
61 */
Santos Cordonc195e362014-02-11 17:05:31 -080062 private CallServiceWrapper mCallService;
Santos Cordon681663d2014-01-30 04:32:15 -080063
64 /**
Santos Cordon0b03b4b2014-01-29 18:01:59 -080065 * Read-only and parcelable version of this call.
66 */
67 private CallInfo mCallInfo;
68
69 /**
Ben Gilad0407fb22014-01-09 16:18:41 -080070 * 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 Gilad0bf5b912014-01-28 17:55:57 -080075 Call(String handle, ContactInfo contactInfo) {
76 // TODO(gilad): Pass this in etc.
77 mId = "dummy";
Santos Cordon0b03b4b2014-01-29 18:01:59 -080078 mState = CallState.NEW;
Ben Gilad0407fb22014-01-09 16:18:41 -080079 mHandle = handle;
80 mContactInfo = contactInfo;
Ben Gilad0407fb22014-01-09 16:18:41 -080081 mCreationTime = new Date();
82 }
83
Ben Gilad0bf5b912014-01-28 17:55:57 -080084 String getId() {
85 return mId;
86 }
87
Santos Cordon0b03b4b2014-01-29 18:01:59 -080088 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 Gilad0bf5b912014-01-28 17:55:57 -0800103 String getHandle() {
104 return mHandle;
105 }
106
107 ContactInfo getContactInfo() {
108 return mContactInfo;
109 }
110
Ben Gilad0407fb22014-01-09 16:18:41 -0800111 /**
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 Gilad0bf5b912014-01-28 17:55:57 -0800115 long getAgeInMilliseconds() {
Ben Gilad0407fb22014-01-09 16:18:41 -0800116 return new Date().getTime() - mCreationTime.getTime();
117 }
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800118
Santos Cordonc195e362014-02-11 17:05:31 -0800119 CallServiceWrapper getCallService() {
Santos Cordon681663d2014-01-30 04:32:15 -0800120 return mCallService;
121 }
122
Santos Cordonc195e362014-02-11 17:05:31 -0800123 void setCallService(CallServiceWrapper callService) {
Santos Cordon681663d2014-01-30 04:32:15 -0800124 mCallService = callService;
125 }
126
127 /**
128 * Clears the associated call service.
129 */
130 void clearCallService() {
131 setCallService(null);
132 }
133
Santos Cordonc195e362014-02-11 17:05:31 -0800134 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800135 * 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 Cordonc195e362014-02-11 17:05:31 -0800141 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 Cordon049b7b62014-01-30 05:34:26 -0800146 }
147 }
148
Santos Cordon0b03b4b2014-01-29 18:01:59 -0800149 /**
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 Gilad9f2bed32013-12-12 17:43:26 -0800165}