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