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