1/*
2 * Copyright (C) 2013 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.incallui;
18
19import com.android.incallui.service.PhoneNumberService;
20import com.google.android.collect.Sets;
21import com.google.common.base.Preconditions;
22
23import android.content.Context;
24import android.content.Intent;
25
26import com.android.services.telephony.common.Call;
27import com.android.services.telephony.common.Call.Capabilities;
28import com.google.common.collect.Lists;
29
30import java.util.ArrayList;
31import java.util.Set;
32
33/**
34 * Takes updates from the CallList and notifies the InCallActivity (UI)
35 * of the changes.
36 * Responsible for starting the activity for a new call and finishing the activity when all calls
37 * are disconnected.
38 * Creates and manages the in-call state and provides a listener pattern for the presenters
39 * that want to listen in on the in-call state changes.
40 * TODO: This class has become more of a state machine at this point.  Consider renaming.
41 */
42public class InCallPresenter implements CallList.Listener {
43
44    private static InCallPresenter sInCallPresenter;
45
46    private final Set<InCallStateListener> mListeners = Sets.newHashSet();
47    private final ArrayList<IncomingCallListener> mIncomingCallListeners = Lists.newArrayList();
48
49    private AudioModeProvider mAudioModeProvider;
50    private StatusBarNotifier mStatusBarNotifier;
51    private ContactInfoCache mContactInfoCache;
52    private Context mContext;
53    private CallList mCallList;
54    private InCallActivity mInCallActivity;
55    private InCallState mInCallState = InCallState.NO_CALLS;
56    private ProximitySensor mProximitySensor;
57    private boolean mServiceConnected = false;
58
59    /**
60     * Is true when the activity has been previously started. Some code needs to know not just if
61     * the activity is currently up, but if it had been previously shown in foreground for this
62     * in-call session (e.g., StatusBarNotifier). This gets reset when the session ends in the
63     * tear-down method.
64     */
65    private boolean mIsActivityPreviouslyStarted = false;
66
67    public static synchronized InCallPresenter getInstance() {
68        if (sInCallPresenter == null) {
69            sInCallPresenter = new InCallPresenter();
70        }
71        return sInCallPresenter;
72    }
73
74    public InCallState getInCallState() {
75        return mInCallState;
76    }
77
78    public CallList getCallList() {
79        return mCallList;
80    }
81
82    public void setUp(Context context, CallList callList, AudioModeProvider audioModeProvider) {
83        if (mServiceConnected) {
84            Log.i(this, "New service connection replacing existing one.");
85            // retain the current resources, no need to create new ones.
86            Preconditions.checkState(context == mContext);
87            Preconditions.checkState(callList == mCallList);
88            Preconditions.checkState(audioModeProvider == mAudioModeProvider);
89            return;
90        }
91
92        Preconditions.checkNotNull(context);
93        mContext = context;
94
95        mContactInfoCache = ContactInfoCache.getInstance(context);
96
97        mStatusBarNotifier = new StatusBarNotifier(context, mContactInfoCache);
98        addListener(mStatusBarNotifier);
99
100        mAudioModeProvider = audioModeProvider;
101
102        mProximitySensor = new ProximitySensor(context, mAudioModeProvider);
103        addListener(mProximitySensor);
104
105        mCallList = callList;
106
107        // This only gets called by the service so this is okay.
108        mServiceConnected = true;
109
110        // The final thing we do in this set up is add ourselves as a listener to CallList.  This
111        // will kick off an update and the whole process can start.
112        mCallList.addListener(this);
113
114        Log.d(this, "Finished InCallPresenter.setUp");
115    }
116
117    /**
118     * Called when the telephony service has disconnected from us.  This will happen when there are
119     * no more active calls. However, we may still want to continue showing the UI for
120     * certain cases like showing "Call Ended".
121     * What we really want is to wait for the activity and the service to both disconnect before we
122     * tear things down. This method sets a serviceConnected boolean and calls a secondary method
123     * that performs the aforementioned logic.
124     */
125    public void tearDown() {
126        Log.d(this, "tearDown");
127        mServiceConnected = false;
128        attemptCleanup();
129    }
130
131    private void attemptFinishActivity() {
132        final boolean doFinish = (mInCallActivity != null && isActivityStarted());
133        Log.i(this, "Hide in call UI: " + doFinish);
134
135        if (doFinish) {
136            mInCallActivity.finish();
137        }
138    }
139
140    /**
141     * Called when the UI begins or ends. Starts the callstate callbacks if the UI just began.
142     * Attempts to tear down everything if the UI just ended. See #tearDown for more insight on
143     * the tear-down process.
144     */
145    public void setActivity(InCallActivity inCallActivity) {
146        boolean updateListeners = false;
147        boolean doAttemptCleanup = false;
148
149        if (inCallActivity != null) {
150            if (mInCallActivity == null) {
151                updateListeners = true;
152                Log.i(this, "UI Initialized");
153            } else if (mInCallActivity != inCallActivity) {
154                Log.wtf(this, "Setting a second activity before destroying the first.");
155            } else {
156                // since setActivity is called onStart(), it can be called multiple times.
157                // This is fine and ignorable, but we do not want to update the world every time
158                // this happens (like going to/from background) so we do not set updateListeners.
159            }
160
161            mInCallActivity = inCallActivity;
162
163            // By the time the UI finally comes up, the call may already be disconnected.
164            // If that's the case, we may need to show an error dialog.
165            if (mCallList != null && mCallList.getDisconnectedCall() != null) {
166                maybeShowErrorDialogOnDisconnect(mCallList.getDisconnectedCall());
167            }
168
169            // When the UI comes up, we need to first check the in-call state.
170            // If we are showing NO_CALLS, that means that a call probably connected and
171            // then immediately disconnected before the UI was able to come up.
172            // If we dont have any calls, start tearing down the UI instead.
173            // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
174            // it has been set.
175            if (mInCallState == InCallState.NO_CALLS) {
176                Log.i(this, "UI Intialized, but no calls left.  shut down.");
177                attemptFinishActivity();
178                return;
179            }
180        } else {
181            Log.i(this, "UI Destroyed)");
182            updateListeners = true;
183            mInCallActivity = null;
184
185            // We attempt cleanup for the destroy case but only after we recalculate the state
186            // to see if we need to come back up or stay shut down. This is why we do the cleanup
187            // after the call to onCallListChange() instead of directly here.
188            doAttemptCleanup = true;
189        }
190
191        // Messages can come from the telephony layer while the activity is coming up
192        // and while the activity is going down.  So in both cases we need to recalculate what
193        // state we should be in after they complete.
194        // Examples: (1) A new incoming call could come in and then get disconnected before
195        //               the activity is created.
196        //           (2) All calls could disconnect and then get a new incoming call before the
197        //               activity is destroyed.
198        //
199        // b/1122139 - We previously had a check for mServiceConnected here as well, but there are
200        // cases where we need to recalculate the current state even if the service in not
201        // connected.  In particular the case where startOrFinish() is called while the app is
202        // already finish()ing. In that case, we skip updating the state with the knowledge that
203        // we will check again once the activity has finished. That means we have to recalculate the
204        // state here even if the service is disconnected since we may not have finished a state
205        // transition while finish()ing.
206        if (updateListeners) {
207            onCallListChange(mCallList);
208        }
209
210        if (doAttemptCleanup) {
211            attemptCleanup();
212        }
213    }
214
215    /**
216     * Called when there is a change to the call list.
217     * Sets the In-Call state for the entire in-call app based on the information it gets from
218     * CallList. Dispatches the in-call state to all listeners. Can trigger the creation or
219     * destruction of the UI based on the states that is calculates.
220     */
221    @Override
222    public void onCallListChange(CallList callList) {
223        if (callList == null) {
224            return;
225        }
226        InCallState newState = getPotentialStateFromCallList(callList);
227        newState = startOrFinishUi(newState);
228
229        // Renable notification shade and soft navigation buttons, if we are no longer in the
230        // incoming call screen
231        if (!newState.isIncoming()) {
232            CallCommandClient.getInstance().setSystemBarNavigationEnabled(true);
233        }
234
235        // Set the new state before announcing it to the world
236        Log.i(this, "Phone switching state: " + mInCallState + " -> " + newState);
237        mInCallState = newState;
238
239        // notify listeners of new state
240        for (InCallStateListener listener : mListeners) {
241            Log.d(this, "Notify " + listener + " of state " + mInCallState.toString());
242            listener.onStateChange(mInCallState, callList);
243        }
244
245        if (isActivityStarted()) {
246            final boolean hasCall = callList.getActiveOrBackgroundCall() != null ||
247                    callList.getOutgoingCall() != null;
248            mInCallActivity.dismissKeyguard(hasCall);
249        }
250    }
251
252    /**
253     * Called when there is a new incoming call.
254     *
255     * @param call
256     */
257    @Override
258    public void onIncomingCall(Call call) {
259        InCallState newState = startOrFinishUi(InCallState.INCOMING);
260
261        Log.i(this, "Phone switching state: " + mInCallState + " -> " + newState);
262        mInCallState = newState;
263
264        // Disable notification shade and soft navigation buttons
265        if (newState.isIncoming()) {
266            CallCommandClient.getInstance().setSystemBarNavigationEnabled(false);
267        }
268
269        for (IncomingCallListener listener : mIncomingCallListeners) {
270            listener.onIncomingCall(mInCallState, call);
271        }
272    }
273
274    /**
275     * Called when a call becomes disconnected. Called everytime an existing call
276     * changes from being connected (incoming/outgoing/active) to disconnected.
277     */
278    @Override
279    public void onDisconnect(Call call) {
280        hideDialpadForDisconnect();
281        maybeShowErrorDialogOnDisconnect(call);
282
283        // We need to do the run the same code as onCallListChange.
284        onCallListChange(CallList.getInstance());
285
286        if (isActivityStarted()) {
287            mInCallActivity.dismissKeyguard(false);
288        }
289    }
290
291    /**
292     * Given the call list, return the state in which the in-call screen should be.
293     */
294    public static InCallState getPotentialStateFromCallList(CallList callList) {
295
296        InCallState newState = InCallState.NO_CALLS;
297
298        if (callList == null) {
299            return newState;
300        }
301        if (callList.getIncomingCall() != null) {
302            newState = InCallState.INCOMING;
303        } else if (callList.getOutgoingCall() != null) {
304            newState = InCallState.OUTGOING;
305        } else if (callList.getActiveCall() != null ||
306                callList.getBackgroundCall() != null ||
307                callList.getDisconnectedCall() != null ||
308                callList.getDisconnectingCall() != null) {
309            newState = InCallState.INCALL;
310        }
311
312        return newState;
313    }
314
315    public void addIncomingCallListener(IncomingCallListener listener) {
316        Preconditions.checkNotNull(listener);
317        mIncomingCallListeners.add(listener);
318    }
319
320    public void removeIncomingCallListener(IncomingCallListener listener) {
321        Preconditions.checkNotNull(listener);
322        mIncomingCallListeners.remove(listener);
323    }
324
325    public void addListener(InCallStateListener listener) {
326        Preconditions.checkNotNull(listener);
327        mListeners.add(listener);
328    }
329
330    public void removeListener(InCallStateListener listener) {
331        Preconditions.checkNotNull(listener);
332        mListeners.remove(listener);
333    }
334
335    public AudioModeProvider getAudioModeProvider() {
336        return mAudioModeProvider;
337    }
338
339    public ContactInfoCache getContactInfoCache() {
340        return mContactInfoCache;
341    }
342
343    public ProximitySensor getProximitySensor() {
344        return mProximitySensor;
345    }
346
347    /**
348     * Hangs up any active or outgoing calls.
349     */
350    public void hangUpOngoingCall(Context context) {
351        // By the time we receive this intent, we could be shut down and call list
352        // could be null.  Bail in those cases.
353        if (mCallList == null) {
354            if (mStatusBarNotifier == null) {
355                // The In Call UI has crashed but the notification still stayed up. We should not
356                // come to this stage.
357                StatusBarNotifier.clearInCallNotification(context);
358            }
359            return;
360        }
361
362        Call call = mCallList.getOutgoingCall();
363        if (call == null) {
364            call = mCallList.getActiveOrBackgroundCall();
365        }
366
367        if (call != null) {
368            CallCommandClient.getInstance().disconnectCall(call.getCallId());
369        }
370    }
371
372    /**
373     * Returns true if the incall app is the foreground application.
374     */
375    public boolean isShowingInCallUi() {
376        return (isActivityStarted() && mInCallActivity.isForegroundActivity());
377    }
378
379    /**
380     * Returns true of the activity has been created and is running.
381     * Returns true as long as activity is not destroyed or finishing.  This ensures that we return
382     * true even if the activity is paused (not in foreground).
383     */
384    public boolean isActivityStarted() {
385        return (mInCallActivity != null &&
386                !mInCallActivity.isDestroyed() &&
387                !mInCallActivity.isFinishing());
388    }
389
390    public boolean isActivityPreviouslyStarted() {
391        return mIsActivityPreviouslyStarted;
392    }
393
394    /**
395     * Called when the activity goes in/out of the foreground.
396     */
397    public void onUiShowing(boolean showing) {
398        // We need to update the notification bar when we leave the UI because that
399        // could trigger it to show again.
400        if (mStatusBarNotifier != null) {
401            mStatusBarNotifier.updateNotification(mInCallState, mCallList);
402        }
403
404        if (mProximitySensor != null) {
405            mProximitySensor.onInCallShowing(showing);
406        }
407
408        if (showing) {
409            mIsActivityPreviouslyStarted = true;
410        }
411    }
412
413    /**
414     * Brings the app into the foreground if possible.
415     */
416    public void bringToForeground(boolean showDialpad) {
417        // Before we bring the incall UI to the foreground, we check to see if:
418        // 1. We've already started the activity once for this session
419        // 2. If it exists, the activity is not already in the foreground
420        // 3. We are in a state where we want to show the incall ui
421        if (mIsActivityPreviouslyStarted && !isShowingInCallUi() &&
422                mInCallState != InCallState.NO_CALLS) {
423            showInCall(showDialpad);
424        }
425    }
426
427    public void onPostDialCharWait(int callId, String chars) {
428        if (isActivityStarted()) {
429            mInCallActivity.showPostCharWaitDialog(callId, chars);
430        }
431    }
432
433    /**
434     * Handles the green CALL key while in-call.
435     * @return true if we consumed the event.
436     */
437    public boolean handleCallKey() {
438        Log.v(this, "handleCallKey");
439
440        // The green CALL button means either "Answer", "Unhold", or
441        // "Swap calls", or can be a no-op, depending on the current state
442        // of the Phone.
443
444        /**
445         * INCOMING CALL
446         */
447        final CallList calls = CallList.getInstance();
448        final Call incomingCall = calls.getIncomingCall();
449        Log.v(this, "incomingCall: " + incomingCall);
450
451        // (1) Attempt to answer a call
452        if (incomingCall != null) {
453            CallCommandClient.getInstance().answerCall(incomingCall.getCallId());
454            return true;
455        }
456
457        /**
458         * ACTIVE CALL
459         */
460        final Call activeCall = calls.getActiveCall();
461        if (activeCall != null) {
462            // TODO: This logic is repeated from CallButtonPresenter.java. We should
463            // consolidate this logic.
464            final boolean isGeneric = activeCall.can(Capabilities.GENERIC_CONFERENCE);
465            final boolean canMerge = activeCall.can(Capabilities.MERGE_CALLS);
466            final boolean canSwap = activeCall.can(Capabilities.SWAP_CALLS);
467
468            Log.v(this, "activeCall: " + activeCall + ", isGeneric: " + isGeneric + ", canMerge: " +
469                    canMerge + ", canSwap: " + canSwap);
470
471            // (2) Attempt actions on Generic conference calls
472            if (activeCall.isConferenceCall() && isGeneric) {
473                if (canMerge) {
474                    CallCommandClient.getInstance().merge();
475                    return true;
476                } else if (canSwap) {
477                    CallCommandClient.getInstance().swap();
478                    return true;
479                }
480            }
481
482            // (3) Swap calls
483            if (canSwap) {
484                CallCommandClient.getInstance().swap();
485                return true;
486            }
487        }
488
489        /**
490         * BACKGROUND CALL
491         */
492        final Call heldCall = calls.getBackgroundCall();
493        if (heldCall != null) {
494            // We have a hold call so presumeable it will always support HOLD...but
495            // there is no harm in double checking.
496            final boolean canHold = heldCall.can(Capabilities.HOLD);
497
498            Log.v(this, "heldCall: " + heldCall + ", canHold: " + canHold);
499
500            // (4) unhold call
501            if (heldCall.getState() == Call.State.ONHOLD && canHold) {
502                CallCommandClient.getInstance().hold(heldCall.getCallId(), false);
503                return true;
504            }
505        }
506
507        // Always consume hard keys
508        return true;
509    }
510
511    /**
512     * A dialog could have prevented in-call screen from being previously finished.
513     * This function checks to see if there should be any UI left and if not attempts
514     * to tear down the UI.
515     */
516    public void onDismissDialog() {
517        Log.i(this, "Dialog dismissed");
518        if (mInCallState == InCallState.NO_CALLS) {
519            attemptFinishActivity();
520            attemptCleanup();
521        }
522    }
523
524    /**
525     * For some disconnected causes, we show a dialog.  This calls into the activity to show
526     * the dialog if appropriate for the call.
527     */
528    private void maybeShowErrorDialogOnDisconnect(Call call) {
529        // For newly disconnected calls, we may want to show a dialog on specific error conditions
530        if (isActivityStarted() && call.getState() == Call.State.DISCONNECTED) {
531            mInCallActivity.maybeShowErrorDialogOnDisconnect(call.getDisconnectCause());
532        }
533    }
534
535    /**
536     * Hides the dialpad.  Called when a call is disconnected (Requires hiding dialpad).
537     */
538    private void hideDialpadForDisconnect() {
539        if (isActivityStarted()) {
540            mInCallActivity.hideDialpadForDisconnect();
541        }
542    }
543
544    /**
545     * When the state of in-call changes, this is the first method to get called. It determines if
546     * the UI needs to be started or finished depending on the new state and does it.
547     */
548    private InCallState startOrFinishUi(InCallState newState) {
549        Log.d(this, "startOrFinishUi: " + mInCallState + " -> " + newState);
550
551        // TODO: Consider a proper state machine implementation
552
553        // If the state isn't changing, we have already done any starting/stopping of
554        // activities in a previous pass...so lets cut out early
555        if (newState == mInCallState) {
556            return newState;
557        }
558
559        // A new Incoming call means that the user needs to be notified of the the call (since
560        // it wasn't them who initiated it).  We do this through full screen notifications and
561        // happens indirectly through {@link StatusBarListener}.
562        //
563        // The process for incoming calls is as follows:
564        //
565        // 1) CallList          - Announces existence of new INCOMING call
566        // 2) InCallPresenter   - Gets announcement and calculates that the new InCallState
567        //                      - should be set to INCOMING.
568        // 3) InCallPresenter   - This method is called to see if we need to start or finish
569        //                        the app given the new state.
570        // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
571        //                        StatusBarNotifier explicitly to issue a FullScreen Notification
572        //                        that will either start the InCallActivity or show the user a
573        //                        top-level notification dialog if the user is in an immersive app.
574        //                        That notification can also start the InCallActivity.
575        // 5) InCallActivity    - Main activity starts up and at the end of its onCreate will
576        //                        call InCallPresenter::setActivity() to let the presenter
577        //                        know that start-up is complete.
578        //
579        //          [ AND NOW YOU'RE IN THE CALL. voila! ]
580        //
581        // Our app is started using a fullScreen notification.  We need to do this whenever
582        // we get an incoming call.
583        final boolean startStartupSequence = (InCallState.INCOMING == newState);
584
585        // A new outgoing call indicates that the user just now dialed a number and when that
586        // happens we need to display the screen immediateley.
587        //
588        // This is different from the incoming call sequence because we do not need to shock the
589        // user with a top-level notification.  Just show the call UI normally.
590        final boolean showCallUi = (InCallState.OUTGOING == newState);
591
592        // TODO: Can we be suddenly in a call without it having been in the outgoing or incoming
593        // state?  I havent seen that but if it can happen, the code below should be enabled.
594        // showCallUi |= (InCallState.INCALL && !isActivityStarted());
595
596        // The only time that we have an instance of mInCallActivity and it isn't started is
597        // when it is being destroyed.  In that case, lets avoid bringing up another instance of
598        // the activity.  When it is finally destroyed, we double check if we should bring it back
599        // up so we aren't going to lose anything by avoiding a second startup here.
600        boolean activityIsFinishing = mInCallActivity != null && !isActivityStarted();
601        if (activityIsFinishing) {
602            Log.i(this, "Undo the state change: " + newState + " -> " + mInCallState);
603            return mInCallState;
604        }
605
606        if (showCallUi) {
607            Log.i(this, "Start in call UI");
608            showInCall(false);
609        } else if (startStartupSequence) {
610            Log.i(this, "Start Full Screen in call UI");
611
612            // We're about the bring up the in-call UI for an incoming call. If we still have
613            // dialogs up, we need to clear them out before showing incoming screen.
614            if (isActivityStarted()) {
615                mInCallActivity.dismissPendingDialogs();
616            }
617            startUi(newState);
618        } else if (newState == InCallState.NO_CALLS) {
619            // The new state is the no calls state.  Tear everything down.
620            attemptFinishActivity();
621            attemptCleanup();
622        }
623
624        return newState;
625    }
626
627    private void startUi(InCallState inCallState) {
628        final Call incomingCall = mCallList.getIncomingCall();
629        final boolean isCallWaiting = (incomingCall != null &&
630                incomingCall.getState() == Call.State.CALL_WAITING);
631
632        // If the screen is off, we need to make sure it gets turned on for incoming calls.
633        // This normally works just fine thanks to FLAG_TURN_SCREEN_ON but that only works
634        // when the activity is first created. Therefore, to ensure the screen is turned on
635        // for the call waiting case, we finish() the current activity and start a new one.
636        // There should be no jank from this since the screen is already off and will remain so
637        // until our new activity is up.
638        if (mProximitySensor.isScreenReallyOff() && isCallWaiting) {
639            if (isActivityStarted()) {
640                mInCallActivity.finish();
641            }
642            mInCallActivity = null;
643        }
644
645        mStatusBarNotifier.updateNotificationAndLaunchIncomingCallUi(inCallState, mCallList);
646    }
647
648    /**
649     * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all
650     * down.
651     */
652    private void attemptCleanup() {
653        boolean shouldCleanup = (mInCallActivity == null && !mServiceConnected &&
654                mInCallState == InCallState.NO_CALLS);
655        Log.i(this, "attemptCleanup? " + shouldCleanup);
656
657        if (shouldCleanup) {
658            mIsActivityPreviouslyStarted = false;
659
660            // blow away stale contact info so that we get fresh data on
661            // the next set of calls
662            if (mContactInfoCache != null) {
663                mContactInfoCache.clearCache();
664            }
665            mContactInfoCache = null;
666
667            if (mProximitySensor != null) {
668                removeListener(mProximitySensor);
669                mProximitySensor.tearDown();
670            }
671            mProximitySensor = null;
672
673            mAudioModeProvider = null;
674
675            if (mStatusBarNotifier != null) {
676                removeListener(mStatusBarNotifier);
677            }
678            mStatusBarNotifier = null;
679
680            if (mCallList != null) {
681                mCallList.removeListener(this);
682            }
683            mCallList = null;
684
685            mContext = null;
686            mInCallActivity = null;
687
688            mListeners.clear();
689            mIncomingCallListeners.clear();
690
691            Log.d(this, "Finished InCallPresenter.CleanUp");
692        }
693    }
694
695    private void showInCall(boolean showDialpad) {
696        mContext.startActivity(getInCallIntent(showDialpad));
697    }
698
699    public Intent getInCallIntent(boolean showDialpad) {
700        final Intent intent = new Intent(Intent.ACTION_MAIN, null);
701        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
702                | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
703                | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
704        intent.setClass(mContext, InCallActivity.class);
705        if (showDialpad) {
706            intent.putExtra(InCallActivity.SHOW_DIALPAD_EXTRA, true);
707        }
708
709        return intent;
710    }
711
712    /**
713     * Private constructor. Must use getInstance() to get this singleton.
714     */
715    private InCallPresenter() {
716    }
717
718    /**
719     * All the main states of InCallActivity.
720     */
721    public enum InCallState {
722        // InCall Screen is off and there are no calls
723        NO_CALLS,
724
725        // Incoming-call screen is up
726        INCOMING,
727
728        // In-call experience is showing
729        INCALL,
730
731        // User is dialing out
732        OUTGOING;
733
734        public boolean isIncoming() {
735            return (this == INCOMING);
736        }
737
738        public boolean isConnectingOrConnected() {
739            return (this == INCOMING ||
740                    this == OUTGOING ||
741                    this == INCALL);
742        }
743    }
744
745    /**
746     * Interface implemented by classes that need to know about the InCall State.
747     */
748    public interface InCallStateListener {
749        // TODO: Enhance state to contain the call objects instead of passing CallList
750        public void onStateChange(InCallState state, CallList callList);
751    }
752
753    public interface IncomingCallListener {
754        public void onIncomingCall(InCallState state, Call call);
755    }
756}
757