blob: 38eaf5c6ff608e358a4ac4306c80ea90f17e4e9a [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2009 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
17package com.android.phone;
18
19import android.telephony.PhoneNumberUtils;
20import android.util.Log;
21
22import com.android.internal.telephony.Call;
23import com.android.internal.telephony.Connection;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import com.android.internal.telephony.CallManager;
25import com.android.internal.telephony.PhoneConstants;
26import com.android.internal.telephony.TelephonyCapabilities;
27
28/**
29 * Helper class to keep track of enabledness, visibility, and "on/off"
30 * or "checked" state of the various controls available in the in-call
31 * UI, based on the current telephony state.
32 *
33 * This class is independent of the exact UI controls used on any given
34 * device. To avoid cluttering up the "view" code (i.e. InCallTouchUi)
35 * with logic about which functions are available right now, we instead
36 * have that logic here, and provide simple boolean flags to indicate the
37 * state and/or enabledness of all possible in-call user operations.
38 *
39 * (In other words, this is the "model" that corresponds to the "view"
40 * implemented by InCallTouchUi.)
41 */
42public class InCallControlState {
43 private static final String LOG_TAG = "InCallControlState";
44 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
45
Santos Cordon27a3c1f2013-08-06 07:49:27 -070046 private final BluetoothManager mBluetoothManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047 private InCallScreen mInCallScreen;
48 private CallManager mCM;
49
50 //
51 // Our "public API": Boolean flags to indicate the state and/or
52 // enabledness of all possible in-call user operations:
53 //
54
55 public boolean manageConferenceVisible;
56 public boolean manageConferenceEnabled;
57 //
58 public boolean canAddCall;
59 //
60 public boolean canEndCall;
61 //
62 public boolean canSwap;
63 public boolean canMerge;
64 //
65 public boolean bluetoothEnabled;
66 public boolean bluetoothIndicatorOn;
67 //
68 public boolean speakerEnabled;
69 public boolean speakerOn;
70 //
71 public boolean canMute;
72 public boolean muteIndicatorOn;
73 //
74 public boolean dialpadEnabled;
75 public boolean dialpadVisible;
76 //
77 /** True if the "Hold" function is *ever* available on this device */
78 public boolean supportsHold;
79 /** True if the call is currently on hold */
80 public boolean onHold;
81 /** True if the "Hold" or "Unhold" function should be available right now */
82 // TODO: this name is misleading. Let's break this apart into
83 // separate canHold and canUnhold flags, and have the caller look at
84 // "canHold || canUnhold" to decide whether the hold/unhold UI element
85 // should be visible.
86 public boolean canHold;
87
88
Santos Cordon27a3c1f2013-08-06 07:49:27 -070089 public InCallControlState(InCallScreen inCallScreen, CallManager cm,
90 BluetoothManager bluetoothManager) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070091 if (DBG) log("InCallControlState constructor...");
92 mInCallScreen = inCallScreen;
93 mCM = cm;
Santos Cordon27a3c1f2013-08-06 07:49:27 -070094 mBluetoothManager = bluetoothManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095 }
96
97 /**
98 * Updates all our public boolean flags based on the current state of
99 * the Phone.
100 */
101 public void update() {
102 final PhoneConstants.State state = mCM.getState(); // coarse-grained voice call state
103 final Call fgCall = mCM.getActiveFgCall();
104 final Call.State fgCallState = fgCall.getState();
105 final boolean hasActiveForegroundCall = (fgCallState == Call.State.ACTIVE);
106 final boolean hasHoldingCall = mCM.hasActiveBgCall();
107
108 // Manage conference:
109 if (TelephonyCapabilities.supportsConferenceCallManagement(fgCall.getPhone())) {
110 // This item is visible only if the foreground call is a
111 // conference call, and it's enabled unless the "Manage
112 // conference" UI is already up.
113 manageConferenceVisible = PhoneUtils.isConferenceCall(fgCall);
114 manageConferenceEnabled =
115 manageConferenceVisible && !mInCallScreen.isManageConferenceMode();
116 } else {
117 // This device has no concept of managing a conference call.
118 manageConferenceVisible = false;
119 manageConferenceEnabled = false;
120 }
121
122 // "Add call":
123 canAddCall = PhoneUtils.okToAddCall(mCM);
124
125 // "End call": always enabled unless the phone is totally idle.
126 // Note that while the phone is ringing, the InCallTouchUi widget isn't
127 // visible at all, so the state of the End button doesn't matter. However
128 // we *do* still set canEndCall to true in this case, purely to prevent a
129 // UI glitch when the InCallTouchUi widget first appears, immediately after
130 // answering an incoming call.
131 canEndCall = (mCM.hasActiveFgCall() || mCM.hasActiveRingingCall() || mCM.hasActiveBgCall());
132
133 // Swap / merge calls
134 canSwap = PhoneUtils.okToSwapCalls(mCM);
135 canMerge = PhoneUtils.okToMergeCalls(mCM);
136
137 // "Bluetooth":
Santos Cordon27a3c1f2013-08-06 07:49:27 -0700138 if (mBluetoothManager.isBluetoothAvailable()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700139 bluetoothEnabled = true;
Santos Cordon27a3c1f2013-08-06 07:49:27 -0700140 bluetoothIndicatorOn = mBluetoothManager.isBluetoothAudioConnectedOrPending();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700141 } else {
142 bluetoothEnabled = false;
143 bluetoothIndicatorOn = false;
144 }
145
146 // "Speaker": always enabled unless the phone is totally idle.
147 // The current speaker state comes from the AudioManager.
148 speakerEnabled = (state != PhoneConstants.State.IDLE);
149 speakerOn = PhoneUtils.isSpeakerOn(mInCallScreen);
150
151 // "Mute": only enabled when the foreground call is ACTIVE.
152 // (It's meaningless while on hold, or while DIALING/ALERTING.)
153 // It's also explicitly disabled during emergency calls or if
154 // emergency callback mode (ECM) is active.
155 Connection c = fgCall.getLatestConnection();
156 boolean isEmergencyCall = false;
157 if (c != null) isEmergencyCall =
158 PhoneNumberUtils.isLocalEmergencyNumber(c.getAddress(),
159 fgCall.getPhone().getContext());
160 boolean isECM = PhoneUtils.isPhoneInEcm(fgCall.getPhone());
161 if (isEmergencyCall || isECM) { // disable "Mute" item
162 canMute = false;
163 muteIndicatorOn = false;
164 } else {
165 canMute = hasActiveForegroundCall;
166 muteIndicatorOn = PhoneUtils.getMute();
167 }
168
169 // "Dialpad": Enabled only when it's OK to use the dialpad in the
170 // first place.
171 dialpadEnabled = mInCallScreen.okToShowDialpad();
172
173 // Also keep track of whether the dialpad is currently "opened"
174 // (i.e. visible).
175 dialpadVisible = mInCallScreen.isDialerOpened();
176
177 // "Hold:
178 if (TelephonyCapabilities.supportsHoldAndUnhold(fgCall.getPhone())) {
179 // This phone has the concept of explicit "Hold" and "Unhold" actions.
180 supportsHold = true;
181 // "On hold" means that there's a holding call and
182 // *no* foreground call. (If there *is* a foreground call,
183 // that's "two lines in use".)
184 onHold = hasHoldingCall && (fgCallState == Call.State.IDLE);
185 // The "Hold" control is disabled entirely if there's
186 // no way to either hold or unhold in the current state.
187 boolean okToHold = hasActiveForegroundCall && !hasHoldingCall;
188 boolean okToUnhold = onHold;
189 canHold = okToHold || okToUnhold;
190 } else if (hasHoldingCall && (fgCallState == Call.State.IDLE)) {
191 // Even when foreground phone device doesn't support hold/unhold, phone devices
192 // for background holding calls may do.
193 //
194 // If the foreground call is ACTIVE, we should turn on "swap" button instead.
195 final Call bgCall = mCM.getFirstActiveBgCall();
196 if (bgCall != null &&
197 TelephonyCapabilities.supportsHoldAndUnhold(bgCall.getPhone())) {
198 supportsHold = true;
199 onHold = true;
200 canHold = true;
201 }
202 } else {
203 // This device has no concept of "putting a call on hold."
204 supportsHold = false;
205 onHold = false;
206 canHold = false;
207 }
208
209 if (DBG) dumpState();
210 }
211
212 public void dumpState() {
213 log("InCallControlState:");
214 log(" manageConferenceVisible: " + manageConferenceVisible);
215 log(" manageConferenceEnabled: " + manageConferenceEnabled);
216 log(" canAddCall: " + canAddCall);
217 log(" canEndCall: " + canEndCall);
218 log(" canSwap: " + canSwap);
219 log(" canMerge: " + canMerge);
220 log(" bluetoothEnabled: " + bluetoothEnabled);
221 log(" bluetoothIndicatorOn: " + bluetoothIndicatorOn);
222 log(" speakerEnabled: " + speakerEnabled);
223 log(" speakerOn: " + speakerOn);
224 log(" canMute: " + canMute);
225 log(" muteIndicatorOn: " + muteIndicatorOn);
226 log(" dialpadEnabled: " + dialpadEnabled);
227 log(" dialpadVisible: " + dialpadVisible);
228 log(" onHold: " + onHold);
229 log(" canHold: " + canHold);
230 }
231
232 private void log(String msg) {
233 Log.d(LOG_TAG, msg);
234 }
235}