summaryrefslogtreecommitdiff
path: root/src/com/android/incallui/InCallMessageController.java
blob: 259ff754dd450c5105ec0016ca97688221e9c99f (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
/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of The Linux Foundation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.android.incallui;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;

import com.android.incallui.InCallPresenter.InCallDetailsListener;
import com.android.incallui.InCallVideoCallCallbackNotifier.VideoEventListener;
import com.android.incallui.InCallVideoCallCallbackNotifier.SessionModificationListener;
import org.codeaurora.QtiVideoCallConstants;

/**
 * This class listens to incoming events for the listener classes it implements. It should
 * handle all UI notification to be shown to the user for any indication that is required to be
 * shown like call substate indication, video quality indication, etc.
 * For e.g., this class implements {@class InCallSubstateListener} and when call substate changes,
 * {@class CallSubstateNotifier} notifies it through the onCallSubstateChanged callback.
 */
public class InCallMessageController implements InCallSubstateListener, VideoEventListener,
        CallList.Listener, SessionModificationListener, InCallSessionModificationCauseListener {


    private static InCallMessageController sInCallMessageController;

    private PrimaryCallTracker mPrimaryCallTracker;

    private Context mContext;

    /**
     * Private constructor. Must use getInstance() to get this singleton.
     */
    private InCallMessageController() {
    }

    /**
     * Handles set up of the {@class InCallMessageController}. Instantiates the context needed by
     * the class and adds a listener to listen to call substate changes, video event changes,
     * session modification cause changes, call state changes.
     */
    public void setUp(Context context) {
        mContext = context;
        mPrimaryCallTracker = new PrimaryCallTracker();
        CallSubstateNotifier.getInstance().addListener(this);
        InCallVideoCallCallbackNotifier.getInstance().addVideoEventListener(this);
        CallList.getInstance().addListener(this);
        InCallVideoCallCallbackNotifier.getInstance().addSessionModificationListener(this);
        SessionModificationCauseNotifier.getInstance().addListener(this);
        InCallPresenter.getInstance().addListener(mPrimaryCallTracker);
    }

    /**
     * Handles tear down of the {@class InCallMessageController}. Sets the context to null and
     * unregisters it's call substate,  video event, session modification cause, call state
     * listeners.
     */
    public void tearDown() {
        mContext = null;
        CallSubstateNotifier.getInstance().removeListener(this);
        InCallVideoCallCallbackNotifier.getInstance().removeVideoEventListener(this);
        CallList.getInstance().removeListener(this);
        InCallVideoCallCallbackNotifier.getInstance().removeSessionModificationListener(this);
        SessionModificationCauseNotifier.getInstance().removeListener(this);
        InCallPresenter.getInstance().removeListener(mPrimaryCallTracker);
        mPrimaryCallTracker = null;
    }

    /**
     * This method returns a singleton instance of {@class InCallMessageController}
     */
    public static synchronized InCallMessageController getInstance() {
        if (sInCallMessageController == null) {
            sInCallMessageController = new InCallMessageController();
        }
        return sInCallMessageController;
    }

    /**
     * This method overrides onCallSubstateChanged method of {@interface InCallSubstateListener}
     * We are notified when call substate changes and display a toast message on the UI.
     */
    @Override
    public void onCallSubstateChanged(final Call call, final int callSubstate) {
        Log.d(this, "onCallSubstateChanged - Call : " + call + " call substate changed to " +
                callSubstate);

        if (mContext == null || !mPrimaryCallTracker.isPrimaryCall(call)) {
            Log.e(this, "onCallSubstateChanged - Context is null/not primary call.");
            return;
        }

        String callSubstateChangedText = "";

        if (QtiCallUtils.isEnabled(
                QtiVideoCallConstants.CALL_SUBSTATE_AUDIO_CONNECTED_SUSPENDED, callSubstate)) {
            callSubstateChangedText +=
                    mContext.getResources().getString(
                    R.string.call_substate_connected_suspended_audio);
        }

        if (QtiCallUtils.isEnabled(
                QtiVideoCallConstants.CALL_SUBSTATE_VIDEO_CONNECTED_SUSPENDED, callSubstate)) {
            callSubstateChangedText +=
                    mContext.getResources().getString(
                    R.string.call_substate_connected_suspended_video);
        }

        if (QtiCallUtils.isEnabled(QtiVideoCallConstants.CALL_SUBSTATE_AVP_RETRY, callSubstate)) {
            callSubstateChangedText +=
                    mContext.getResources().getString(R.string.call_substate_avp_retry);
        }

        if (QtiCallUtils.isNotEnabled(QtiVideoCallConstants.CALL_SUBSTATE_ALL, callSubstate)) {
            callSubstateChangedText =
                    mContext.getResources().getString(R.string.call_substate_call_resumed);
        }

        if (!callSubstateChangedText.isEmpty()) {
            String callSubstateLabelText = mContext.getResources().getString(
                    R.string.call_substate_label, call.getId(), callSubstateChangedText);
            QtiCallUtils.displayToast(mContext, callSubstateLabelText);
        }
    }

    /**
     * This method overrides onVideoQualityChanged method of {@interface VideoEventListener}
     * We are notified when video quality of the call changed and display a message on the UI.
     */
    @Override
    public void onVideoQualityChanged(final Call call, final int videoQuality) {
        Log.d(this, "onVideoQualityChanged: - Call : " + call + " Video quality changed to " +
                videoQuality);

        if (mContext == null || !mPrimaryCallTracker.isPrimaryCall(call)) {
            Log.e(this, "onVideoQualityChanged - Context is null/not primary call.");
            return;
        }

        final Resources resources = mContext.getResources();
        final String videoQualityChangedText = resources.getString(R.string.video_quality_changed,
                call.getId(), QtiCallUtils.getVideoQualityResourceId(videoQuality));
        QtiCallUtils.displayToast(mContext, videoQualityChangedText);
    }

    /**
     * This method overrides onCallSessionEvent method of {@interface VideoEventListener}
     * We are notified when a new call session event is sent and display a message on the UI.
     */
    @Override
    public void onCallSessionEvent(final int event) {
        Log.d(this, "onCallSessionEvent: event = " + event);

        if (mContext == null) {
            Log.e(this, "onCallSessionEvent - Context is null.");
            return;
        }
        QtiCallUtils.displayToast(mContext, QtiCallUtils.getCallSessionResourceId(event));
    }

    /**
     * This method overrides onCallDataUsageChange method of {@interface VideoEventListener}
     *  We are notified when data usage is changed and display a message on the UI.
     */
    @Override
    public void onCallDataUsageChange(final long dataUsage) {
        Log.d(this, "onCallDataUsageChange: dataUsage = " + dataUsage);
        final String dataUsageChangedText = mContext.getResources().getString(
                R.string.data_usage_label, dataUsage);
        QtiCallUtils.displayToast(mContext, dataUsageChangedText);
    }

    /**
     * This method overrides onPeerPauseStateChanged method of {@interface VideoEventListener}
     * Added for completeness. No implementation yet.
     */
    @Override
    public void onPeerPauseStateChanged(final Call call, final boolean paused) {
        //no-op
    }

    /**
     * This method overrides onIncomingCall method of {@interface CallList.Listener}
     * Added for completeness. No implementation yet.
     */
    @Override
    public void onIncomingCall(Call call) {
        // no-op
    }

    /**
     * This method overrides onCallListChange method of {@interface CallList.Listener}
     * Added for completeness. No implementation yet.
     */
    @Override
    public void onCallListChange(CallList list) {
        // no-op
    }

    /**
     * This method overrides onUpgradeToVideo method of {@interface CallList.Listener}
     * Added for completeness. No implementation yet.
     */
    @Override
    public void onUpgradeToVideo(Call call) {
        // no-op
    }

    /**
     * This method overrides onDisconnect method of {@interface CallList.Listener}
     */
    @Override
    public void onDisconnect(final Call call) {
        Bundle extras = call.getExtras();
        if (extras == null) {
            Log.w(this, "onDisconnect: null Extras");
            return;
        }
        final int errorCode = extras.getInt(QtiVideoCallConstants.EXTRAS_KEY_CALL_FAIL_EXTRA_CODE,
                QtiVideoCallConstants.DISCONNECT_CAUSE_UNSPECIFIED);
        Log.d(this, "onDisconnect: code = " + errorCode);
        showCallDisconnectInfo(errorCode);
    }

    /**
     * This method displays specific disconnect information to user
     */
    private void showCallDisconnectInfo(int errorCode) {
       if(errorCode == QtiVideoCallConstants.DISCONNECT_CAUSE_UNSPECIFIED) {
          return;
       }

       switch (errorCode) {
         case QtiVideoCallConstants.CALL_FAIL_EXTRA_CODE_LTE_3G_HA_FAILED:
             QtiCallUtils.displayToast(mContext, R.string.call_failed_ho_not_feasible);
             break;
         default:
             break;
       }
    }

    @Override
    public void onUpgradeToVideoRequest(Call call, int videoState) {
        //no-op
    }

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

    @Override
    public void onUpgradeToVideoFail(int error, Call call) {
        Log.d(this, "onUpgradeToVideoFail: error = " + error);
        showUpgradeFailInfo(error);
    }

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

    private void showUpgradeFailInfo(int errorCode) {
        switch (errorCode) {
            case QtiVideoCallConstants.CAUSE_CODE_SESSION_MODIFY_REQUEST_LOW_BATTERY:
                QtiCallUtils.displayToast(mContext,
                        R.string.modify_call_failed_due_to_low_battery);
                break;
            default:
                break;
        }
    }

    /*
     * Handles any session modifictaion cause changes in the call.
     *
     * @param call The call for which orientation mode changed.
     * @param sessionModificationCause The new session modifictaion cause
     */
    @Override
    public void onSessionModificationCauseChanged(Call call, int sessionModificationCause) {
        Log.d(this, "onSessionModificationCauseChanged: Call : " + call +
                " Call modified due to "  + sessionModificationCause);

        if (mContext == null || !mPrimaryCallTracker.isPrimaryCall(call)) {
            Log.e(this,
                    "onSessionModificationCauseChanged- Context is null/not primary call.");
            return;
        }

        QtiCallUtils.displayToast(mContext,
                getSessionModificationCauseResourceId(sessionModificationCause));
    }

    /**
     * This method returns the string resource id (i.e. display string) that corresponds to the
     * session modification cause code.
     */
    private static int getSessionModificationCauseResourceId(int cause) {
        switch(cause) {
            case QtiVideoCallConstants.CAUSE_CODE_UNSPECIFIED:
                return R.string.session_modify_cause_unspecified;
            case QtiVideoCallConstants.CAUSE_CODE_SESSION_MODIFY_UPGRADE_LOCAL_REQ:
                return R.string.session_modify_cause_upgrade_local_request;
            case QtiVideoCallConstants.CAUSE_CODE_SESSION_MODIFY_UPGRADE_REMOTE_REQ:
                return R.string.session_modify_cause_upgrade_remote_request;
            case QtiVideoCallConstants.CAUSE_CODE_SESSION_MODIFY_DOWNGRADE_LOCAL_REQ:
                return R.string.session_modify_cause_downgrade_local_request;
            case QtiVideoCallConstants.CAUSE_CODE_SESSION_MODIFY_DOWNGRADE_REMOTE_REQ:
                return R.string.session_modify_cause_downgrade_remote_request;
            case QtiVideoCallConstants.CAUSE_CODE_SESSION_MODIFY_DOWNGRADE_RTP_TIMEOUT:
                return R.string.session_modify_cause_downgrade_rtp_timeout;
            case QtiVideoCallConstants.CAUSE_CODE_SESSION_MODIFY_DOWNGRADE_QOS:
                return R.string.session_modify_cause_downgrade_qos;
            case QtiVideoCallConstants.CAUSE_CODE_SESSION_MODIFY_DOWNGRADE_PACKET_LOSS:
                return R.string.session_modify_cause_downgrade_packet_loss;
            case QtiVideoCallConstants.CAUSE_CODE_SESSION_MODIFY_DOWNGRADE_LOW_THRPUT:
                return R.string.session_modify_cause_downgrade_low_thrput;
            case QtiVideoCallConstants.CAUSE_CODE_SESSION_MODIFY_DOWNGRADE_THERM_MITIGATION:
                return R.string.session_modify_cause_downgrade_thermal_mitigation;
            case QtiVideoCallConstants.CAUSE_CODE_SESSION_MODIFY_DOWNGRADE_LIPSYNC:
                return R.string.session_modify_cause_downgrade_lipsync;
            case QtiVideoCallConstants.CAUSE_CODE_SESSION_MODIFY_DOWNGRADE_GENERIC_ERROR:
            default:
                return R.string.session_modify_cause_downgrade_generic_error;
        }
    }
}