AnswerPresenter.java revision f05f341d8bf54a1052129a741cb6e2b4c2ff8ac8
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
52        CallList.getInstance().removeListener(this);
53
54        // This is necessary because the activity can be destroyed while an incoming call exists.
55        // This happens when back button is pressed while incoming call is still being shown.
56        if (mCallId != Call.INVALID_CALL_ID) {
57            CallList.getInstance().removeCallUpdateListener(mCallId, this);
58        }
59    }
60
61    @Override
62    public void onCallListChange(CallList callList) {
63        // no-op
64    }
65
66    @Override
67    public void onIncomingCall(Call call) {
68        // TODO: Ui is being destroyed when the fragment detaches.  Need clean up step to stop
69        // getting updates here.
70        if (getUi() != null) {
71            if (call.getCallId() != mCallId) {
72                // A new call is coming in.
73                processIncomingCall(call);
74            }
75        }
76    }
77
78    private void processIncomingCall(Call call) {
79        mCallId = call.getCallId();
80
81        // Listen for call updates for the current call.
82        CallList.getInstance().addCallUpdateListener(mCallId, this);
83
84        Log.d(TAG, "Showing incoming for call id: " + mCallId);
85        final ArrayList<String> textMsgs = CallList.getInstance().getTextResponses(
86                call.getCallId());
87        getUi().showAnswerUi(true);
88
89        if (call.can(Call.Capabilities.RESPOND_VIA_TEXT) && textMsgs != null) {
90            getUi().showTextButton(true);
91            getUi().configureMessageDialogue(textMsgs);
92        } else {
93            getUi().showTextButton(false);
94        }
95    }
96
97
98    @Override
99    public void onCallStateChanged(Call call) {
100        Log.d(this, "onCallStateChange() " + call);
101        if (call.getState() != Call.State.INCOMING && call.getState() != Call.State.CALL_WAITING) {
102            // Stop listening for updates.
103            CallList.getInstance().removeCallUpdateListener(mCallId, this);
104
105            getUi().showAnswerUi(false);
106            mCallId = Call.INVALID_CALL_ID;
107        }
108    }
109
110    public void onAnswer() {
111        if (mCallId == Call.INVALID_CALL_ID) {
112            return;
113        }
114
115        Log.d(this, "onAnswer " + mCallId);
116
117        CallCommandClient.getInstance().answerCall(mCallId);
118    }
119
120    public void onDecline() {
121        if (mCallId == Call.INVALID_CALL_ID) {
122            return;
123        }
124
125        Log.d(this, "onDecline " + mCallId);
126
127        CallCommandClient.getInstance().rejectCall(mCallId, false, null);
128    }
129
130    public void onText() {
131        if (getUi() != null) {
132            getUi().showMessageDialogue();
133        }
134    }
135
136    public void rejectCallWithMessage(String message) {
137        Log.d(this, "sendTextToDefaultActivity()...");
138        if (getUi() != null) {
139            getUi().dismissPopup();
140        }
141        CallCommandClient.getInstance().rejectCall(mCallId, true, message);
142    }
143
144    interface AnswerUi extends Ui {
145        public void showAnswerUi(boolean show);
146        public void showTextButton(boolean show);
147        public boolean isMessageDialogueShowing();
148        public void showMessageDialogue();
149        public void dismissPopup();
150        public void configureMessageDialogue(ArrayList<String> textResponses);
151    }
152}
153