summaryrefslogtreecommitdiffstats
path: root/src/com/android/incallui/AnswerPresenter.java
blob: 184b30967532144713ab0d458714cee15791ff31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.incallui;

import android.content.Context;
import android.os.SystemProperties;
import android.telecom.TelecomManager;
import android.telecom.VideoProfile;

import java.util.List;

/**
 * Presenter for the Incoming call widget.
 */
public class AnswerPresenter extends Presenter<AnswerPresenter.AnswerUi>
        implements CallList.CallUpdateListener, CallList.Listener,
        CallList.ActiveSubChangeListener {

    private static final String TAG = AnswerPresenter.class.getSimpleName();

    private String mCallId[] = new String[CallList.PHONE_COUNT];
    private Call mCall[] = new Call[CallList.PHONE_COUNT];
    private boolean mHasTextMessages = false;

    @Override
    public void onUiReady(AnswerUi ui) {
        Log.d(this, "onUiReady ui=" + ui);
        super.onUiReady(ui);

        final CallList calls = CallList.getInstance();
        Call call = calls.getVideoUpgradeRequestCall();
        Log.d(this, "getVideoUpgradeRequestCall call =" + call);

        if (call != null && calls.getIncomingCall() == null) {
            processVideoUpgradeRequestCall(call);
        }
        for (int i = 0; i < CallList.PHONE_COUNT; i++) {
            int[] subId = CallList.getInstance().getSubId(i);
            call = calls.getCallWithState(Call.State.INCOMING, 0, subId[0]);
            if (call == null) {
                call = calls.getCallWithState(Call.State.CALL_WAITING, 0, subId[0]);
            }
            if (call != null) {
                processIncomingCall(call);
            }
        }

        // Listen for incoming calls.
        calls.addListener(this);
        CallList.getInstance().addActiveSubChangeListener(this);
    }

    @Override
    public void onUiUnready(AnswerUi ui) {
        super.onUiUnready(ui);

        CallList.getInstance().removeListener(this);

        // This is necessary because the activity can be destroyed while an incoming call exists.
        // This happens when back button is pressed while incoming call is still being shown.
        for (int i = 0; i < CallList.PHONE_COUNT; i++) {
            if (mCallId[i] != null) {
                CallList.getInstance().removeCallUpdateListener(mCallId[i], this);
            }
        }
        CallList.getInstance().removeActiveSubChangeListener(this);
    }

    @Override
    public void onCallListChange(CallList callList) {
        Log.d(this, "onCallListChange callList=" + callList);
        // no-op
    }

    @Override
    public void onDisconnect(Call call) {
        // no-op
    }

    @Override
    public void onIncomingCall(Call call) {
        int subId = call.getSubId();
        int phoneId = CallList.getInstance().getPhoneId(subId);
        // TODO: Ui is being destroyed when the fragment detaches.  Need clean up step to stop
        // getting updates here.
        Log.d(this, "onIncomingCall: " + this);
        if (getUi() != null) {
            Call modifyCall = CallList.getInstance().getVideoUpgradeRequestCall();
            if (modifyCall != null) {
                getUi().showAnswerUi(false);
                int modifyPhoneId = CallList.getInstance().getPhoneId(modifyCall.getSubId());
                Log.d(this, "declining upgrade request id: " + modifyPhoneId);
                CallList.getInstance().removeCallUpdateListener(mCallId[modifyPhoneId], this);
                InCallPresenter.getInstance().declineUpgradeRequest(getUi().getContext());
            }
            if (!call.getId().equals(mCallId[phoneId])) {
                // A new call is coming in.
                processIncomingCall(call);
            }
        }
    }

    private boolean isVideoUpgradePending(Call call) {
        boolean isUpgradePending = false;
        if (call.getSessionModificationState()
                == Call.SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
            isUpgradePending = true;
        }
        return isUpgradePending;
    }

    @Override
    public void onUpgradeToVideo(Call call) {
        Log.d(this, "onUpgradeToVideo: " + this + " call=" + call);
        if (getUi() == null) {
            Log.d(this, "onUpgradeToVideo ui is null");
            return;
        }
        boolean isUpgradePending = isVideoUpgradePending(call);
        InCallPresenter inCallPresenter = InCallPresenter.getInstance();
        if (isUpgradePending
                && inCallPresenter.getInCallState() == InCallPresenter.InCallState.INCOMING) {
            Log.d(this, "declining upgrade request");
            inCallPresenter.declineUpgradeRequest(getUi().getContext());
        } else if (isUpgradePending) {
            Log.d(this, "process upgrade request as no MT call");
            processVideoUpgradeRequestCall(call);
        }
    }

    private void processIncomingCall(Call call) {
        int subId = call.getSubId();
        int phoneId = CallList.getInstance().getPhoneId(subId);
        mCallId[phoneId] = call.getId();
        mCall[phoneId] = call;

        // Listen for call updates for the current call.
        CallList.getInstance().addCallUpdateListener(mCallId[phoneId], this);

        Log.d(TAG, "Showing incoming for call id: " + mCallId[phoneId] + " " + this);
        final List<String> textMsgs = CallList.getInstance().getTextResponses(call.getId());
        getUi().showAnswerUi(true);
        configureAnswerTargetsForSms(call, textMsgs);
    }

    private void processVideoUpgradeRequestCall(Call call) {
        Log.d(this, " processVideoUpgradeRequestCall call=" + call);
        final int currentVideoState = call.getVideoState();
        final int modifyToVideoState = call.getModifyToVideoState();

        if (currentVideoState == modifyToVideoState) {
            Log.w(this, "processVideoUpgradeRequestCall: Video states are same. Return.");
            return;
        }

        int subId = call.getSubId();
        int phoneId = CallList.getInstance().getPhoneId(subId);
        mCallId[phoneId] = call.getId();
        mCall[phoneId] = call;

        // Listen for call updates for the current call.
        CallList.getInstance().addCallUpdateListener(mCallId[phoneId], this);
        AnswerUi ui = getUi();

        if (ui == null) {
            Log.e(this, "Ui is null. Can't process upgrade request");
            return;
        }
        ui.showAnswerUi(true);
        ui.showTargets(getUiTarget(currentVideoState, modifyToVideoState));

    }

    private int getUiTarget(int currentVideoState, int modifyToVideoState) {
        if (showVideoUpgradeOptions(currentVideoState, modifyToVideoState)) {
            return AnswerFragment.TARGET_SET_FOR_VIDEO_UPGRADE_REQUEST;
        } else if (isEnabled(modifyToVideoState, VideoProfile.VideoState.BIDIRECTIONAL)) {
            return AnswerFragment.TARGET_SET_FOR_BIDIRECTIONAL_VIDEO_ACCEPT_REJECT_REQUEST;
        }  else if (isEnabled(modifyToVideoState, VideoProfile.VideoState.TX_ENABLED)) {
            return AnswerFragment.TARGET_SET_FOR_VIDEO_TRANSMIT_ACCEPT_REJECT_REQUEST;
        }  else if (isEnabled(modifyToVideoState, VideoProfile.VideoState.RX_ENABLED)) {
            return AnswerFragment.TARGET_SET_FOR_VIDEO_RECEIVE_ACCEPT_REJECT_REQUEST;
        }
        return AnswerFragment.TARGET_SET_FOR_VIDEO_UPGRADE_REQUEST;
    }

    private boolean showVideoUpgradeOptions(int currentVideoState, int modifyToVideoState) {
        return currentVideoState == VideoProfile.VideoState.AUDIO_ONLY &&
            isEnabled(modifyToVideoState, VideoProfile.VideoState.BIDIRECTIONAL);
    }

    private boolean isEnabled(int videoState, int mask) {
        return (videoState & mask) == mask;
    }

    @Override
    public void onCallChanged(Call call) {
        Log.d(this, "onCallStateChange() " + call + " " + this);
        if (call.getState() != Call.State.INCOMING) {
            int subId = call.getSubId();
            int phoneId = CallList.getInstance().getPhoneId(subId);

            boolean isUpgradePending = isVideoUpgradePending(call);
            if (!isUpgradePending) {
                // Stop listening for updates.
                CallList.getInstance().removeCallUpdateListener(mCallId[phoneId], this);
            }

            final Call incall = CallList.getInstance().getIncomingCall();
            if (incall != null || isUpgradePending) {
                getUi().showAnswerUi(true);
            } else {
                getUi().showAnswerUi(false);
            }

            // mCallId will hold the state of the call. We don't clear the mCall variable here as
            // it may be useful for sending text messages after phone disconnects.
            mCallId[phoneId] = null;
            mHasTextMessages = false;
        } else if (!mHasTextMessages) {
            final List<String> textMsgs = CallList.getInstance().getTextResponses(call.getId());
            if (textMsgs != null) {
                configureAnswerTargetsForSms(call, textMsgs);
            }
        }
    }

    // get active phoneId, for which call is visible to user
    private int getActivePhoneId() {
        int phoneId = -1;
        if (CallList.getInstance().isDsdaEnabled()) {
            int subId = CallList.getInstance().getActiveSubscription();
            phoneId = CallList.getInstance().getPhoneId(subId);
        } else {
            for (int i = 0; i < mCall.length; i++) {
                if (mCall[i] != null) {
                    phoneId = i;
                }
            }
        }
        return phoneId;
    }

    public void onAnswer(int videoState, Context context) {
        int phoneId = getActivePhoneId();
        Log.i(this, "onAnswer  mCallId:" + mCallId + "phoneId:" + phoneId + " videoState="
                + videoState);
        if (mCallId == null || phoneId == -1) {
            return;
        }

        /**
         * To test call deflection this property has to be set with the
         * number to which the call should be deflected. If this property is
         * set to a number, on pressing the UI answer button, call deflect
         * request will be sent. This is done to provide hooks to test call
         * deflection through the UI answer button. For commercialization UI
         * should be customized to call this API through the Call deflect UI
         * button By default this property is not set and Answer button will
         * work as expected.
         * Example:
         * To deflect call to number 12345
         * adb shell setprop persist.radio.deflect.number 12345
         *
         * Toggle above property and to invoke answerCallWithCallType
         * adb shell setprop persist.radio.deflect.number ""
         */
        String deflectcall = SystemProperties.get("persist.radio.deflect.number");
        if (deflectcall != null && !deflectcall.isEmpty()) {
            Log.i(this, "deflectCall " + mCallId + "to" + deflectcall);
            TelecomAdapter.getInstance().deflectCall(mCall[phoneId].getId(), deflectcall);
            return;
        }

        if (mCall[phoneId].getSessionModificationState()
                == Call.SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
            InCallPresenter.getInstance().acceptUpgradeRequest(videoState, context);
        } else {
            TelecomAdapter.getInstance().answerCall(mCall[phoneId].getId(), videoState);
        }
    }

    public void onDeflect(String number) {
        int phoneId = getActivePhoneId();
        Log.i(this, "onDeflect  mCallId:" + mCallId + "phoneId:" + phoneId + "to" + number);
        if (mCallId == null || phoneId == -1 || number == null || number.isEmpty()) {
            return;
        }

        TelecomAdapter.getInstance().deflectCall(mCall[phoneId].getId(), number);

    }

    /**
     * TODO: We are using reject and decline interchangeably. We should settle on
     * reject since it seems to be more prevalent.
     */
    public void onDecline(Context context) {
        int phoneId = getActivePhoneId();
        Log.i(this, "onDecline mCallId:" + mCallId + "phoneId:" + phoneId);
        if (mCall[phoneId].getSessionModificationState()
                == Call.SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
            InCallPresenter.getInstance().declineUpgradeRequest(context);
        } else {
            TelecomAdapter.getInstance().rejectCall(mCall[phoneId].getId(), false, null);
        }
    }

    public void onText() {
        if (getUi() != null) {
            InCallPresenter.getInstance().getTelecomManager().silenceRinger();
            getUi().showMessageDialog();
        }
    }

    public void rejectCallWithMessage(String message) {
        int phoneId = getActivePhoneId();
        Log.i(this, "sendTextToDefaultActivity()...phoneId:" + phoneId);
        TelecomAdapter.getInstance().rejectCall(mCall[phoneId].getId(), true, message);

        onDismissDialog();
    }

    public void onDismissDialog() {
        InCallPresenter.getInstance().onDismissDialog();
    }

    private void configureAnswerTargetsForSms(Call call, List<String> textMsgs) {
        final Context context = getUi().getContext();

        mHasTextMessages = textMsgs != null;
        boolean withSms =
                call.can(android.telecom.Call.Details.CAPABILITY_RESPOND_VIA_TEXT)
                && mHasTextMessages;
        if (call.isVideoCall(context)) {
            if (withSms) {
                getUi().showTargets(AnswerFragment.TARGET_SET_FOR_VIDEO_WITH_SMS);
                getUi().configureMessageDialog(textMsgs);
            } else {
                getUi().showTargets(AnswerFragment.TARGET_SET_FOR_VIDEO_WITHOUT_SMS);
            }
        } else {
            if (withSms) {
                getUi().showTargets(AnswerFragment.TARGET_SET_FOR_AUDIO_WITH_SMS);
                getUi().configureMessageDialog(textMsgs);
            } else {
                getUi().showTargets(AnswerFragment.TARGET_SET_FOR_AUDIO_WITHOUT_SMS);
            }
        }
    }

    interface AnswerUi extends Ui {
        public void showAnswerUi(boolean show);
        public void showTargets(int targetSet);
        public void showMessageDialog();
        public void configureMessageDialog(List<String> textResponses);
        public Context getContext();
    }

    @Override
    public void onActiveSubChanged(int subId) {
        final CallList calls = CallList.getInstance();
        final Call call = calls.getIncomingCall();
        int phoneId = CallList.getInstance().getPhoneId(subId);
        if ((call != null) && (call.getId() == mCallId[phoneId])) {
            Log.i(this, "Show incoming for call id: " + mCallId[phoneId] + " " + this);
            final List<String> textMsgs = CallList.getInstance().getTextResponses(
                    call.getId());
            getUi().showAnswerUi(true);

            boolean withSms = call.can(android.telecom.Call.Details.CAPABILITY_RESPOND_VIA_TEXT)
                    && textMsgs != null;
            if (call.isVideoCall(getUi().getContext())) {
                if (withSms) {
                    getUi().showTargets(AnswerFragment.TARGET_SET_FOR_VIDEO_WITH_SMS);
                    getUi().configureMessageDialog(textMsgs);
                } else {
                    getUi().showTargets(AnswerFragment.TARGET_SET_FOR_VIDEO_WITHOUT_SMS);
                }
            } else {
                if (withSms) {
                    getUi().showTargets(AnswerFragment.TARGET_SET_FOR_AUDIO_WITH_SMS);
                    getUi().configureMessageDialog(textMsgs);
                } else {
                    getUi().showTargets(AnswerFragment.TARGET_SET_FOR_AUDIO_WITHOUT_SMS);
                }
            }
        } else if ((call == null) && (calls.hasAnyLiveCall(subId))) {
            Log.i(this, "Hide incoming for call id: " + mCallId[phoneId] + " " + this);
            getUi().showAnswerUi(false);
        } else {
            Log.i(this, "No incoming call present for sub = " + subId + " " + this);
        }
    }
}