17df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng/*
27df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng * Copyright (C) 2013 The Android Open Source Project
37df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng *
47df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License");
57df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng * you may not use this file except in compliance with the License.
67df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng * You may obtain a copy of the License at
77df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng *
87df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng *      http://www.apache.org/licenses/LICENSE-2.0
97df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng *
107df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng * Unless required by applicable law or agreed to in writing, software
117df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS,
127df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng * See the License for the specific language governing permissions and
147df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng * limitations under the License
157df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng */
167df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng
17dba5a7df121b4904b94e14439cca8996d843c1c9Chiao Chengpackage com.android.incallui;
18dba5a7df121b4904b94e14439cca8996d843c1c9Chiao Cheng
19671b221ccadea34fb9327ef5342b26419eb5ca99Santos Cordonimport com.android.services.telephony.common.Call;
20671b221ccadea34fb9327ef5342b26419eb5ca99Santos Cordon
2110196fd2f7cc922693e2a7c6c932725a52157943Christine Chenimport java.util.ArrayList;
2210196fd2f7cc922693e2a7c6c932725a52157943Christine Chen
23dba5a7df121b4904b94e14439cca8996d843c1c9Chiao Cheng/**
24150a5c58c67f230c8fd7293b180bbf50aa761480Santos Cordon * Presenter for the Incoming call widget.
25dba5a7df121b4904b94e14439cca8996d843c1c9Chiao Cheng */
26671b221ccadea34fb9327ef5342b26419eb5ca99Santos Cordonpublic class AnswerPresenter extends Presenter<AnswerPresenter.AnswerUi>
278b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        implements CallList.CallUpdateListener, CallList.Listener {
28671b221ccadea34fb9327ef5342b26419eb5ca99Santos Cordon
298b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng    private static final String TAG = AnswerPresenter.class.getSimpleName();
308b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng
318b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng    private int mCallId = Call.INVALID_CALL_ID;
32647462d7ca1cfaf28c76d573bd9a4e11d493a623Christine Chen    private Call mCall = null;
33dba5a7df121b4904b94e14439cca8996d843c1c9Chiao Cheng
34150a5c58c67f230c8fd7293b180bbf50aa761480Santos Cordon    @Override
35150a5c58c67f230c8fd7293b180bbf50aa761480Santos Cordon    public void onUiReady(AnswerUi ui) {
36150a5c58c67f230c8fd7293b180bbf50aa761480Santos Cordon        super.onUiReady(ui);
378b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng
388b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        final CallList calls = CallList.getInstance();
398b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        final Call call = calls.getIncomingCall();
408b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        // TODO: change so that answer presenter never starts up if it's not incoming.
418b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        if (call != null) {
428b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng            processIncomingCall(call);
438b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        }
44bcceccb84a73f1fa045180b8e61ad6595211f721Santos Cordon
45bcceccb84a73f1fa045180b8e61ad6595211f721Santos Cordon        // Listen for incoming calls.
46bcceccb84a73f1fa045180b8e61ad6595211f721Santos Cordon        calls.addListener(this);
47dba5a7df121b4904b94e14439cca8996d843c1c9Chiao Cheng    }
48dba5a7df121b4904b94e14439cca8996d843c1c9Chiao Cheng
49671b221ccadea34fb9327ef5342b26419eb5ca99Santos Cordon    @Override
500b03b7a58d1e1432a8380c2bfc72b3a425fd6646Chiao Cheng    public void onUiUnready(AnswerUi ui) {
510b03b7a58d1e1432a8380c2bfc72b3a425fd6646Chiao Cheng        super.onUiUnready(ui);
52b0d08a16da088e01f416f52a7b74ee9630c1493aSantos Cordon
530b03b7a58d1e1432a8380c2bfc72b3a425fd6646Chiao Cheng        CallList.getInstance().removeListener(this);
540b03b7a58d1e1432a8380c2bfc72b3a425fd6646Chiao Cheng
550b03b7a58d1e1432a8380c2bfc72b3a425fd6646Chiao Cheng        // This is necessary because the activity can be destroyed while an incoming call exists.
560b03b7a58d1e1432a8380c2bfc72b3a425fd6646Chiao Cheng        // This happens when back button is pressed while incoming call is still being shown.
570b03b7a58d1e1432a8380c2bfc72b3a425fd6646Chiao Cheng        if (mCallId != Call.INVALID_CALL_ID) {
580b03b7a58d1e1432a8380c2bfc72b3a425fd6646Chiao Cheng            CallList.getInstance().removeCallUpdateListener(mCallId, this);
590b03b7a58d1e1432a8380c2bfc72b3a425fd6646Chiao Cheng        }
600b03b7a58d1e1432a8380c2bfc72b3a425fd6646Chiao Cheng    }
610b03b7a58d1e1432a8380c2bfc72b3a425fd6646Chiao Cheng
620b03b7a58d1e1432a8380c2bfc72b3a425fd6646Chiao Cheng    @Override
638b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng    public void onCallListChange(CallList callList) {
648b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        // no-op
658b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng    }
668b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng
678b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng    @Override
687fff6a25f65e0949c8e2682d6470cf065e76a63fSantos Cordon    public void onDisconnect(Call call) {
697fff6a25f65e0949c8e2682d6470cf065e76a63fSantos Cordon        // no-op
707fff6a25f65e0949c8e2682d6470cf065e76a63fSantos Cordon    }
717fff6a25f65e0949c8e2682d6470cf065e76a63fSantos Cordon
727fff6a25f65e0949c8e2682d6470cf065e76a63fSantos Cordon    @Override
738b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng    public void onIncomingCall(Call call) {
748b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        // TODO: Ui is being destroyed when the fragment detaches.  Need clean up step to stop
758b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        // getting updates here.
76871698737d5d9fdd878ead5399da465f0d887924Christine Chen        Log.d(this, "onIncomingCall: " + this);
778b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        if (getUi() != null) {
788b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng            if (call.getCallId() != mCallId) {
798b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng                // A new call is coming in.
808b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng                processIncomingCall(call);
8110196fd2f7cc922693e2a7c6c932725a52157943Christine Chen            }
828b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        }
838b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng    }
848b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng
858b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng    private void processIncomingCall(Call call) {
868b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        mCallId = call.getCallId();
87647462d7ca1cfaf28c76d573bd9a4e11d493a623Christine Chen        mCall = call;
88bcceccb84a73f1fa045180b8e61ad6595211f721Santos Cordon
89bcceccb84a73f1fa045180b8e61ad6595211f721Santos Cordon        // Listen for call updates for the current call.
90bcceccb84a73f1fa045180b8e61ad6595211f721Santos Cordon        CallList.getInstance().addCallUpdateListener(mCallId, this);
91bcceccb84a73f1fa045180b8e61ad6595211f721Santos Cordon
92871698737d5d9fdd878ead5399da465f0d887924Christine Chen        Log.d(TAG, "Showing incoming for call id: " + mCallId + " " + this);
938b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        final ArrayList<String> textMsgs = CallList.getInstance().getTextResponses(
948b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng                call.getCallId());
958b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        getUi().showAnswerUi(true);
968b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng
97c9e5af452871582679bceae82948e2aa2a26292dYorke Lee        if (call.can(Call.Capabilities.RESPOND_VIA_TEXT) && textMsgs != null) {
988b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng            getUi().showTextButton(true);
99ef1b5e0afd8e824979d1acda015eda314e61955bDavid Braun            getUi().configureMessageDialog(textMsgs);
100671b221ccadea34fb9327ef5342b26419eb5ca99Santos Cordon        } else {
1018b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng            getUi().showTextButton(false);
1028b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        }
1038b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng    }
1048b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng
1058b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng
1068b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng    @Override
1078b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng    public void onCallStateChanged(Call call) {
108871698737d5d9fdd878ead5399da465f0d887924Christine Chen        Log.d(this, "onCallStateChange() " + call + " " + this);
109bcceccb84a73f1fa045180b8e61ad6595211f721Santos Cordon        if (call.getState() != Call.State.INCOMING && call.getState() != Call.State.CALL_WAITING) {
1108b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng            // Stop listening for updates.
1118b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng            CallList.getInstance().removeCallUpdateListener(mCallId, this);
1128b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng
113671b221ccadea34fb9327ef5342b26419eb5ca99Santos Cordon            getUi().showAnswerUi(false);
114647462d7ca1cfaf28c76d573bd9a4e11d493a623Christine Chen
115647462d7ca1cfaf28c76d573bd9a4e11d493a623Christine Chen            // mCallId will hold the state of the call. We don't clear the mCall variable here as
116647462d7ca1cfaf28c76d573bd9a4e11d493a623Christine Chen            // it may be useful for sending text messages after phone disconnects.
1178b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng            mCallId = Call.INVALID_CALL_ID;
118671b221ccadea34fb9327ef5342b26419eb5ca99Santos Cordon        }
119671b221ccadea34fb9327ef5342b26419eb5ca99Santos Cordon    }
120671b221ccadea34fb9327ef5342b26419eb5ca99Santos Cordon
121dba5a7df121b4904b94e14439cca8996d843c1c9Chiao Cheng    public void onAnswer() {
1228b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        if (mCallId == Call.INVALID_CALL_ID) {
123e286f91abd12dee037de19c3836d8002e450a9c9Santos Cordon            return;
124e286f91abd12dee037de19c3836d8002e450a9c9Santos Cordon        }
125e286f91abd12dee037de19c3836d8002e450a9c9Santos Cordon
1268b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        Log.d(this, "onAnswer " + mCallId);
127671b221ccadea34fb9327ef5342b26419eb5ca99Santos Cordon
1288b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        CallCommandClient.getInstance().answerCall(mCallId);
129dba5a7df121b4904b94e14439cca8996d843c1c9Chiao Cheng    }
130dba5a7df121b4904b94e14439cca8996d843c1c9Chiao Cheng
131dba5a7df121b4904b94e14439cca8996d843c1c9Chiao Cheng    public void onDecline() {
1328b6c8d0dbfba6eabe3d835f8cdcec8a13e253d0cChiao Cheng        Log.d(this, "onDecline " + mCallId);
133671b221ccadea34fb9327ef5342b26419eb5ca99Santos Cordon
134647462d7ca1cfaf28c76d573bd9a4e11d493a623Christine Chen        CallCommandClient.getInstance().rejectCall(mCall, false, null);
135dba5a7df121b4904b94e14439cca8996d843c1c9Chiao Cheng    }
136dba5a7df121b4904b94e14439cca8996d843c1c9Chiao Cheng
137dba5a7df121b4904b94e14439cca8996d843c1c9Chiao Cheng    public void onText() {
13854601f653bb8cc6b37b56b8de6515a2d6aa9fbb1Christine Chen        if (getUi() != null) {
139ef1b5e0afd8e824979d1acda015eda314e61955bDavid Braun            getUi().showMessageDialog();
14054601f653bb8cc6b37b56b8de6515a2d6aa9fbb1Christine Chen        }
14110196fd2f7cc922693e2a7c6c932725a52157943Christine Chen    }
14210196fd2f7cc922693e2a7c6c932725a52157943Christine Chen
14310196fd2f7cc922693e2a7c6c932725a52157943Christine Chen    public void rejectCallWithMessage(String message) {
1441a7f2bcab2d2023f2ee4cfb0bc57bc265b5aab87Chiao Cheng        Log.d(this, "sendTextToDefaultActivity()...");
145647462d7ca1cfaf28c76d573bd9a4e11d493a623Christine Chen
146647462d7ca1cfaf28c76d573bd9a4e11d493a623Christine Chen        CallCommandClient.getInstance().rejectCall(mCall, true, message);
147647462d7ca1cfaf28c76d573bd9a4e11d493a623Christine Chen
148647462d7ca1cfaf28c76d573bd9a4e11d493a623Christine Chen        onDismissDialog();
149647462d7ca1cfaf28c76d573bd9a4e11d493a623Christine Chen    }
150647462d7ca1cfaf28c76d573bd9a4e11d493a623Christine Chen
151647462d7ca1cfaf28c76d573bd9a4e11d493a623Christine Chen    public void onDismissDialog() {
152647462d7ca1cfaf28c76d573bd9a4e11d493a623Christine Chen        InCallPresenter.getInstance().onDismissDialog();
153dba5a7df121b4904b94e14439cca8996d843c1c9Chiao Cheng    }
1547df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng
155150a5c58c67f230c8fd7293b180bbf50aa761480Santos Cordon    interface AnswerUi extends Ui {
156671b221ccadea34fb9327ef5342b26419eb5ca99Santos Cordon        public void showAnswerUi(boolean show);
15710196fd2f7cc922693e2a7c6c932725a52157943Christine Chen        public void showTextButton(boolean show);
158ef1b5e0afd8e824979d1acda015eda314e61955bDavid Braun        public void showMessageDialog();
159ef1b5e0afd8e824979d1acda015eda314e61955bDavid Braun        public void configureMessageDialog(ArrayList<String> textResponses);
1607df3ac3f95c9d23396e1319beb6ca34435d965f5Chiao Cheng    }
161e286f91abd12dee037de19c3836d8002e450a9c9Santos Cordon}
162