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