Home | History | Annotate | Download | only in incallui
      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 
     17 package com.android.incallui;
     18 
     19 import com.android.incallui.service.PhoneNumberService;
     20 import com.google.android.collect.Sets;
     21 import com.google.common.base.Preconditions;
     22 
     23 import android.content.Context;
     24 import android.content.Intent;
     25 
     26 import com.android.services.telephony.common.Call;
     27 import com.android.services.telephony.common.Call.Capabilities;
     28 import com.google.common.collect.Lists;
     29 
     30 import java.util.ArrayList;
     31 import 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  */
     42 public 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         mInCallActivity.showPostCharWaitDialog(callId, chars);
    429     }
    430 
    431     /**
    432      * Handles the green CALL key while in-call.
    433      * @return true if we consumed the event.
    434      */
    435     public boolean handleCallKey() {
    436         Log.v(this, "handleCallKey");
    437 
    438         // The green CALL button means either "Answer", "Unhold", or
    439         // "Swap calls", or can be a no-op, depending on the current state
    440         // of the Phone.
    441 
    442         /**
    443          * INCOMING CALL
    444          */
    445         final CallList calls = CallList.getInstance();
    446         final Call incomingCall = calls.getIncomingCall();
    447         Log.v(this, "incomingCall: " + incomingCall);
    448 
    449         // (1) Attempt to answer a call
    450         if (incomingCall != null) {
    451             CallCommandClient.getInstance().answerCall(incomingCall.getCallId());
    452             return true;
    453         }
    454 
    455         /**
    456          * ACTIVE CALL
    457          */
    458         final Call activeCall = calls.getActiveCall();
    459         if (activeCall != null) {
    460             // TODO: This logic is repeated from CallButtonPresenter.java. We should
    461             // consolidate this logic.
    462             final boolean isGeneric = activeCall.can(Capabilities.GENERIC_CONFERENCE);
    463             final boolean canMerge = activeCall.can(Capabilities.MERGE_CALLS);
    464             final boolean canSwap = activeCall.can(Capabilities.SWAP_CALLS);
    465 
    466             Log.v(this, "activeCall: " + activeCall + ", isGeneric: " + isGeneric + ", canMerge: " +
    467                     canMerge + ", canSwap: " + canSwap);
    468 
    469             // (2) Attempt actions on Generic conference calls
    470             if (activeCall.isConferenceCall() && isGeneric) {
    471                 if (canMerge) {
    472                     CallCommandClient.getInstance().merge();
    473                     return true;
    474                 } else if (canSwap) {
    475                     CallCommandClient.getInstance().swap();
    476                     return true;
    477                 }
    478             }
    479 
    480             // (3) Swap calls
    481             if (canSwap) {
    482                 CallCommandClient.getInstance().swap();
    483                 return true;
    484             }
    485         }
    486 
    487         /**
    488          * BACKGROUND CALL
    489          */
    490         final Call heldCall = calls.getBackgroundCall();
    491         if (heldCall != null) {
    492             // We have a hold call so presumeable it will always support HOLD...but
    493             // there is no harm in double checking.
    494             final boolean canHold = heldCall.can(Capabilities.HOLD);
    495 
    496             Log.v(this, "heldCall: " + heldCall + ", canHold: " + canHold);
    497 
    498             // (4) unhold call
    499             if (heldCall.getState() == Call.State.ONHOLD && canHold) {
    500                 CallCommandClient.getInstance().hold(heldCall.getCallId(), false);
    501                 return true;
    502             }
    503         }
    504 
    505         // Always consume hard keys
    506         return true;
    507     }
    508 
    509     /**
    510      * A dialog could have prevented in-call screen from being previously finished.
    511      * This function checks to see if there should be any UI left and if not attempts
    512      * to tear down the UI.
    513      */
    514     public void onDismissDialog() {
    515         Log.i(this, "Dialog dismissed");
    516         if (mInCallState == InCallState.NO_CALLS) {
    517             attemptFinishActivity();
    518             attemptCleanup();
    519         }
    520     }
    521 
    522     /**
    523      * For some disconnected causes, we show a dialog.  This calls into the activity to show
    524      * the dialog if appropriate for the call.
    525      */
    526     private void maybeShowErrorDialogOnDisconnect(Call call) {
    527         // For newly disconnected calls, we may want to show a dialog on specific error conditions
    528         if (isActivityStarted() && call.getState() == Call.State.DISCONNECTED) {
    529             mInCallActivity.maybeShowErrorDialogOnDisconnect(call.getDisconnectCause());
    530         }
    531     }
    532 
    533     /**
    534      * Hides the dialpad.  Called when a call is disconnected (Requires hiding dialpad).
    535      */
    536     private void hideDialpadForDisconnect() {
    537         if (isActivityStarted()) {
    538             mInCallActivity.hideDialpadForDisconnect();
    539         }
    540     }
    541 
    542     /**
    543      * When the state of in-call changes, this is the first method to get called. It determines if
    544      * the UI needs to be started or finished depending on the new state and does it.
    545      */
    546     private InCallState startOrFinishUi(InCallState newState) {
    547         Log.d(this, "startOrFinishUi: " + mInCallState + " -> " + newState);
    548 
    549         // TODO: Consider a proper state machine implementation
    550 
    551         // If the state isn't changing, we have already done any starting/stopping of
    552         // activities in a previous pass...so lets cut out early
    553         if (newState == mInCallState) {
    554             return newState;
    555         }
    556 
    557         // A new Incoming call means that the user needs to be notified of the the call (since
    558         // it wasn't them who initiated it).  We do this through full screen notifications and
    559         // happens indirectly through {@link StatusBarListener}.
    560         //
    561         // The process for incoming calls is as follows:
    562         //
    563         // 1) CallList          - Announces existence of new INCOMING call
    564         // 2) InCallPresenter   - Gets announcement and calculates that the new InCallState
    565         //                      - should be set to INCOMING.
    566         // 3) InCallPresenter   - This method is called to see if we need to start or finish
    567         //                        the app given the new state.
    568         // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
    569         //                        StatusBarNotifier explicitly to issue a FullScreen Notification
    570         //                        that will either start the InCallActivity or show the user a
    571         //                        top-level notification dialog if the user is in an immersive app.
    572         //                        That notification can also start the InCallActivity.
    573         // 5) InCallActivity    - Main activity starts up and at the end of its onCreate will
    574         //                        call InCallPresenter::setActivity() to let the presenter
    575         //                        know that start-up is complete.
    576         //
    577         //          [ AND NOW YOU'RE IN THE CALL. voila! ]
    578         //
    579         // Our app is started using a fullScreen notification.  We need to do this whenever
    580         // we get an incoming call.
    581         final boolean startStartupSequence = (InCallState.INCOMING == newState);
    582 
    583         // A new outgoing call indicates that the user just now dialed a number and when that
    584         // happens we need to display the screen immediateley.
    585         //
    586         // This is different from the incoming call sequence because we do not need to shock the
    587         // user with a top-level notification.  Just show the call UI normally.
    588         final boolean showCallUi = (InCallState.OUTGOING == newState);
    589 
    590         // TODO: Can we be suddenly in a call without it having been in the outgoing or incoming
    591         // state?  I havent seen that but if it can happen, the code below should be enabled.
    592         // showCallUi |= (InCallState.INCALL && !isActivityStarted());
    593 
    594         // The only time that we have an instance of mInCallActivity and it isn't started is
    595         // when it is being destroyed.  In that case, lets avoid bringing up another instance of
    596         // the activity.  When it is finally destroyed, we double check if we should bring it back
    597         // up so we aren't going to lose anything by avoiding a second startup here.
    598         boolean activityIsFinishing = mInCallActivity != null && !isActivityStarted();
    599         if (activityIsFinishing) {
    600             Log.i(this, "Undo the state change: " + newState + " -> " + mInCallState);
    601             return mInCallState;
    602         }
    603 
    604         if (showCallUi) {
    605             Log.i(this, "Start in call UI");
    606             showInCall(false);
    607         } else if (startStartupSequence) {
    608             Log.i(this, "Start Full Screen in call UI");
    609 
    610             // We're about the bring up the in-call UI for an incoming call. If we still have
    611             // dialogs up, we need to clear them out before showing incoming screen.
    612             if (isActivityStarted()) {
    613                 mInCallActivity.dismissPendingDialogs();
    614             }
    615             startUi(newState);
    616         } else if (newState == InCallState.NO_CALLS) {
    617             // The new state is the no calls state.  Tear everything down.
    618             attemptFinishActivity();
    619             attemptCleanup();
    620         }
    621 
    622         return newState;
    623     }
    624 
    625     private void startUi(InCallState inCallState) {
    626         final Call incomingCall = mCallList.getIncomingCall();
    627         final boolean isCallWaiting = (incomingCall != null &&
    628                 incomingCall.getState() == Call.State.CALL_WAITING);
    629 
    630         // If the screen is off, we need to make sure it gets turned on for incoming calls.
    631         // This normally works just fine thanks to FLAG_TURN_SCREEN_ON but that only works
    632         // when the activity is first created. Therefore, to ensure the screen is turned on
    633         // for the call waiting case, we finish() the current activity and start a new one.
    634         // There should be no jank from this since the screen is already off and will remain so
    635         // until our new activity is up.
    636         if (mProximitySensor.isScreenReallyOff() && isCallWaiting) {
    637             if (isActivityStarted()) {
    638                 mInCallActivity.finish();
    639             }
    640             mInCallActivity = null;
    641         }
    642 
    643         mStatusBarNotifier.updateNotificationAndLaunchIncomingCallUi(inCallState, mCallList);
    644     }
    645 
    646     /**
    647      * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all
    648      * down.
    649      */
    650     private void attemptCleanup() {
    651         boolean shouldCleanup = (mInCallActivity == null && !mServiceConnected &&
    652                 mInCallState == InCallState.NO_CALLS);
    653         Log.i(this, "attemptCleanup? " + shouldCleanup);
    654 
    655         if (shouldCleanup) {
    656             mIsActivityPreviouslyStarted = false;
    657 
    658             // blow away stale contact info so that we get fresh data on
    659             // the next set of calls
    660             if (mContactInfoCache != null) {
    661                 mContactInfoCache.clearCache();
    662             }
    663             mContactInfoCache = null;
    664 
    665             if (mProximitySensor != null) {
    666                 removeListener(mProximitySensor);
    667                 mProximitySensor.tearDown();
    668             }
    669             mProximitySensor = null;
    670 
    671             mAudioModeProvider = null;
    672 
    673             if (mStatusBarNotifier != null) {
    674                 removeListener(mStatusBarNotifier);
    675             }
    676             mStatusBarNotifier = null;
    677 
    678             if (mCallList != null) {
    679                 mCallList.removeListener(this);
    680             }
    681             mCallList = null;
    682 
    683             mContext = null;
    684             mInCallActivity = null;
    685 
    686             mListeners.clear();
    687             mIncomingCallListeners.clear();
    688 
    689             Log.d(this, "Finished InCallPresenter.CleanUp");
    690         }
    691     }
    692 
    693     private void showInCall(boolean showDialpad) {
    694         mContext.startActivity(getInCallIntent(showDialpad));
    695     }
    696 
    697     public Intent getInCallIntent(boolean showDialpad) {
    698         final Intent intent = new Intent(Intent.ACTION_MAIN, null);
    699         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
    700                 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
    701                 | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
    702         intent.setClass(mContext, InCallActivity.class);
    703         if (showDialpad) {
    704             intent.putExtra(InCallActivity.SHOW_DIALPAD_EXTRA, true);
    705         }
    706 
    707         return intent;
    708     }
    709 
    710     /**
    711      * Private constructor. Must use getInstance() to get this singleton.
    712      */
    713     private InCallPresenter() {
    714     }
    715 
    716     /**
    717      * All the main states of InCallActivity.
    718      */
    719     public enum InCallState {
    720         // InCall Screen is off and there are no calls
    721         NO_CALLS,
    722 
    723         // Incoming-call screen is up
    724         INCOMING,
    725 
    726         // In-call experience is showing
    727         INCALL,
    728 
    729         // User is dialing out
    730         OUTGOING;
    731 
    732         public boolean isIncoming() {
    733             return (this == INCOMING);
    734         }
    735 
    736         public boolean isConnectingOrConnected() {
    737             return (this == INCOMING ||
    738                     this == OUTGOING ||
    739                     this == INCALL);
    740         }
    741     }
    742 
    743     /**
    744      * Interface implemented by classes that need to know about the InCall State.
    745      */
    746     public interface InCallStateListener {
    747         // TODO: Enhance state to contain the call objects instead of passing CallList
    748         public void onStateChange(InCallState state, CallList callList);
    749     }
    750 
    751     public interface IncomingCallListener {
    752         public void onIncomingCall(InCallState state, Call call);
    753     }
    754 }
    755