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.services.telephony.common.Call; 20 21 import java.util.ArrayList; 22 23 /** 24 * Presenter for the Incoming call widget. 25 */ 26 public class AnswerPresenter extends Presenter<AnswerPresenter.AnswerUi> 27 implements CallList.CallUpdateListener, CallList.Listener { 28 29 private static final String TAG = AnswerPresenter.class.getSimpleName(); 30 31 private int mCallId = Call.INVALID_CALL_ID; 32 private Call mCall = null; 33 34 @Override 35 public void onUiReady(AnswerUi ui) { 36 super.onUiReady(ui); 37 38 final CallList calls = CallList.getInstance(); 39 final Call call = calls.getIncomingCall(); 40 // TODO: change so that answer presenter never starts up if it's not incoming. 41 if (call != null) { 42 processIncomingCall(call); 43 } 44 45 // Listen for incoming calls. 46 calls.addListener(this); 47 } 48 49 @Override 50 public void onUiUnready(AnswerUi ui) { 51 super.onUiUnready(ui); 52 53 CallList.getInstance().removeListener(this); 54 55 // This is necessary because the activity can be destroyed while an incoming call exists. 56 // This happens when back button is pressed while incoming call is still being shown. 57 if (mCallId != Call.INVALID_CALL_ID) { 58 CallList.getInstance().removeCallUpdateListener(mCallId, this); 59 } 60 } 61 62 @Override 63 public void onCallListChange(CallList callList) { 64 // no-op 65 } 66 67 @Override 68 public void onDisconnect(Call call) { 69 // no-op 70 } 71 72 @Override 73 public void onIncomingCall(Call call) { 74 // TODO: Ui is being destroyed when the fragment detaches. Need clean up step to stop 75 // getting updates here. 76 Log.d(this, "onIncomingCall: " + this); 77 if (getUi() != null) { 78 if (call.getCallId() != mCallId) { 79 // A new call is coming in. 80 processIncomingCall(call); 81 } 82 } 83 } 84 85 private void processIncomingCall(Call call) { 86 mCallId = call.getCallId(); 87 mCall = call; 88 89 // Listen for call updates for the current call. 90 CallList.getInstance().addCallUpdateListener(mCallId, this); 91 92 Log.d(TAG, "Showing incoming for call id: " + mCallId + " " + this); 93 final ArrayList<String> textMsgs = CallList.getInstance().getTextResponses( 94 call.getCallId()); 95 getUi().showAnswerUi(true); 96 97 if (call.can(Call.Capabilities.RESPOND_VIA_TEXT) && textMsgs != null) { 98 getUi().showTextButton(true); 99 getUi().configureMessageDialog(textMsgs); 100 } else { 101 getUi().showTextButton(false); 102 } 103 } 104 105 106 @Override 107 public void onCallStateChanged(Call call) { 108 Log.d(this, "onCallStateChange() " + call + " " + this); 109 if (call.getState() != Call.State.INCOMING && call.getState() != Call.State.CALL_WAITING) { 110 // Stop listening for updates. 111 CallList.getInstance().removeCallUpdateListener(mCallId, this); 112 113 getUi().showAnswerUi(false); 114 115 // mCallId will hold the state of the call. We don't clear the mCall variable here as 116 // it may be useful for sending text messages after phone disconnects. 117 mCallId = Call.INVALID_CALL_ID; 118 } 119 } 120 121 public void onAnswer() { 122 if (mCallId == Call.INVALID_CALL_ID) { 123 return; 124 } 125 126 Log.d(this, "onAnswer " + mCallId); 127 128 CallCommandClient.getInstance().answerCall(mCallId); 129 } 130 131 public void onDecline() { 132 Log.d(this, "onDecline " + mCallId); 133 134 CallCommandClient.getInstance().rejectCall(mCall, false, null); 135 } 136 137 public void onText() { 138 if (getUi() != null) { 139 getUi().showMessageDialog(); 140 } 141 } 142 143 public void rejectCallWithMessage(String message) { 144 Log.d(this, "sendTextToDefaultActivity()..."); 145 146 CallCommandClient.getInstance().rejectCall(mCall, true, message); 147 148 onDismissDialog(); 149 } 150 151 public void onDismissDialog() { 152 InCallPresenter.getInstance().onDismissDialog(); 153 } 154 155 interface AnswerUi extends Ui { 156 public void showAnswerUi(boolean show); 157 public void showTextButton(boolean show); 158 public void showMessageDialog(); 159 public void configureMessageDialog(ArrayList<String> textResponses); 160 } 161 } 162