JLChen
2020-12-10 a8c5f79b0d93adfa7f23601dd0fee30edc14f0d4
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
/*
 * Copyright (c) 2010-2019 Belledonne Communications SARL.
 *
 * This file is part of linphone-android
 * (see https://www.linphone.org).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.linphone.call;
 
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.widget.Toast;
import org.linphone.LinphoneContext;
import org.linphone.LinphoneManager;
import org.linphone.R;
import org.linphone.contacts.ContactsManager;
import org.linphone.contacts.LinphoneContact;
import org.linphone.core.Address;
import org.linphone.core.Call;
import org.linphone.core.CallParams;
import org.linphone.core.Core;
import org.linphone.core.MediaEncryption;
import org.linphone.core.ProxyConfig;
import org.linphone.core.tools.Log;
import org.linphone.dialer.views.AddressType;
import org.linphone.mediastream.Version;
import org.linphone.settings.LinphonePreferences;
import org.linphone.utils.FileUtils;
import org.linphone.utils.LinphoneUtils;
 
/** Handle call updating, reinvites. */
public class CallManager {
    private Context mContext;
    private CallActivityInterface mCallInterface;
    private BandwidthManager mBandwidthManager;
 
    public CallManager(Context context) {
        mContext = context;
        mBandwidthManager = new BandwidthManager();
    }
 
    public void destroy() {
        mBandwidthManager.destroy();
    }
 
    public void terminateCurrentCallOrConferenceOrAll() {
        Core core = LinphoneManager.getCore();
        Call call = core.getCurrentCall();
        if (call != null) {
            call.terminate();
        } else if (core.isInConference()) {
            core.terminateConference();
        } else {
            core.terminateAllCalls();
        }
    }
 
    public void addVideo() {
        Call call = LinphoneManager.getCore().getCurrentCall();
        if (call.getState() == Call.State.End || call.getState() == Call.State.Released) return;
        if (!call.getCurrentParams().videoEnabled()) {
            enableCamera(call, true);
            reinviteWithVideo();
        }
    }
 
    public void removeVideo() {
        Core core = LinphoneManager.getCore();
        Call call = core.getCurrentCall();
        CallParams params = core.createCallParams(call);
        params.enableVideo(false);
        call.update(params);
    }
 
    public void switchCamera() {
        Core core = LinphoneManager.getCore();
        if (core == null) return;
 
        String currentDevice = core.getVideoDevice();
        Log.i("[Call Manager] Current camera device is " + currentDevice);
 
        String[] devices = core.getVideoDevicesList();
        for (String d : devices) {
            if (!d.equals(currentDevice) && !d.equals("StaticImage: Static picture")) {
                Log.i("[Call Manager] New camera device will be " + d);
                core.setVideoDevice(d);
                break;
            }
        }
 
        Call call = core.getCurrentCall();
        if (call == null) {
            Log.i("[Call Manager] Switching camera while not in call");
            return;
        }
        call.update(null);
    }
 
    public boolean acceptCall(Call call) {
        if (call == null) return false;
 
        Core core = LinphoneManager.getCore();
        CallParams params = core.createCallParams(call);
 
        boolean isLowBandwidthConnection =
                !LinphoneUtils.isHighBandwidthConnection(
                        LinphoneContext.instance().getApplicationContext());
 
        if (params != null) {
            params.enableLowBandwidth(isLowBandwidthConnection);
            params.setRecordFile(
                    FileUtils.getCallRecordingFilename(mContext, call.getRemoteAddress()));
        } else {
            Log.e("[Call Manager] Could not create call params for call");
            return false;
        }
 
        call.acceptWithParams(params);
        return true;
    }
 
    public void acceptCallUpdate(boolean accept) {
        Core core = LinphoneManager.getCore();
        Call call = core.getCurrentCall();
        if (call == null) {
            return;
        }
 
        CallParams params = core.createCallParams(call);
        if (accept) {
            params.enableVideo(true);
            core.enableVideoCapture(true);
            core.enableVideoDisplay(true);
        }
 
        call.acceptUpdate(params);
    }
 
    public void inviteAddress(Address address, boolean forceZRTP) {
        boolean isLowBandwidthConnection =
                !LinphoneUtils.isHighBandwidthConnection(
                        LinphoneContext.instance().getApplicationContext());
 
        inviteAddress(address, false, isLowBandwidthConnection, forceZRTP);
    }
 
    public void inviteAddress(Address address, boolean videoEnabled, boolean lowBandwidth) {
        inviteAddress(address, videoEnabled, lowBandwidth, false);
    }
 
    public void newOutgoingCall(AddressType address) {
        String to = address.getText().toString();
        newOutgoingCall(to, address.getDisplayedName());
    }
 
    public void newOutgoingCall(String to, String displayName) {
        if (to == null) return;
 
        // If to is only a username, try to find the contact to get an alias if existing
        if (!to.startsWith("sip:") || !to.contains("@")) {
            LinphoneContact contact = ContactsManager.getInstance().findContactFromPhoneNumber(to);
            if (contact != null) {
                String alias = contact.getContactFromPresenceModelForUriOrTel(to);
                if (alias != null) {
                    to = alias;
                }
            }
        }
 
        LinphonePreferences preferences = LinphonePreferences.instance();
        Core core = LinphoneManager.getCore();
        Address address;
        address = core.interpretUrl(to); // InterpretUrl does normalizePhoneNumber
        if (address == null) {
            Log.e("[Call Manager] Couldn't convert to String to Address : " + to);
            return;
        }
 
        ProxyConfig lpc = core.getDefaultProxyConfig();
        if (mContext.getResources().getBoolean(R.bool.forbid_self_call)
                && lpc != null
                && address.weakEqual(lpc.getIdentityAddress())) {
            return;
        }
        address.setDisplayName(displayName);
 
        boolean isLowBandwidthConnection =
                !LinphoneUtils.isHighBandwidthConnection(
                        LinphoneContext.instance().getApplicationContext());
 
        if (core.isNetworkReachable()) {
            if (Version.isVideoCapable()) {
                boolean prefVideoEnable = preferences.isVideoEnabled();
                boolean prefInitiateWithVideo = preferences.shouldInitiateVideoCall();
                inviteAddress(
                        address,
                        prefVideoEnable && prefInitiateWithVideo,
                        isLowBandwidthConnection);
            } else {
                inviteAddress(address, false, isLowBandwidthConnection);
            }
        } else {
            Toast.makeText(
                            mContext,
                            mContext.getString(R.string.error_network_unreachable),
                            Toast.LENGTH_LONG)
                    .show();
            Log.e(
                    "[Call Manager] Error: "
                            + mContext.getString(R.string.error_network_unreachable));
        }
    }
 
    public void playDtmf(ContentResolver r, char dtmf) {
        try {
            if (Settings.System.getInt(r, Settings.System.DTMF_TONE_WHEN_DIALING) == 0) {
                // audible touch disabled: don't play on speaker, only send in outgoing stream
                return;
            }
        } catch (Settings.SettingNotFoundException e) {
            Log.e("[Call Manager] playDtmf exception: " + e);
        }
 
        LinphoneManager.getCore().playDtmf(dtmf, -1);
    }
 
    public boolean shouldShowAcceptCallUpdateDialog(Call call) {
        if (call == null) return true;
 
        boolean remoteVideo = call.getRemoteParams().videoEnabled();
        boolean localVideo = call.getCurrentParams().videoEnabled();
        boolean autoAcceptCameraPolicy =
                LinphonePreferences.instance().shouldAutomaticallyAcceptVideoRequests();
        return remoteVideo
                && !localVideo
                && !autoAcceptCameraPolicy
                && !call.getCore().isInConference();
    }
 
    public void setCallInterface(CallActivityInterface callInterface) {
        mCallInterface = callInterface;
    }
 
    public void resetCallControlsHidingTimer() {
        if (mCallInterface != null) {
            mCallInterface.resetCallControlsHidingTimer();
        }
    }
 
    public void refreshInCallActions() {
        if (mCallInterface != null) {
            mCallInterface.refreshInCallActions();
        }
    }
 
    public void removeCallFromConference(Call call) {
        if (call == null || call.getConference() == null) {
            return;
        }
        call.getConference().removeParticipant(call.getRemoteAddress());
 
        if (call.getCore().getConferenceSize() <= 1) {
            call.getCore().leaveConference();
        }
    }
 
    public void pauseConference() {
        Core core = LinphoneManager.getCore();
        if (core == null) return;
        if (core.isInConference()) {
            Log.i("[Call Manager] Pausing conference");
            core.leaveConference();
        } else {
            Log.w("[Call Manager] Core isn't in a conference, can't pause it");
        }
    }
 
    public void resumeConference() {
        Core core = LinphoneManager.getCore();
        if (core == null) return;
        if (!core.isInConference()) {
            Log.i("[Call Manager] Resuming conference");
            core.enterConference();
        } else {
            Log.w("[Call Manager] Core is already in a conference, can't resume it");
        }
    }
 
    private void inviteAddress(
            Address address, boolean videoEnabled, boolean lowBandwidth, boolean forceZRTP) {
        Core core = LinphoneManager.getCore();
 
        CallParams params = core.createCallParams(null);
        mBandwidthManager.updateWithProfileSettings(params);
 
        if (videoEnabled && params.videoEnabled()) {
            params.enableVideo(true);
        } else {
            params.enableVideo(false);
        }
 
        if (lowBandwidth) {
            params.enableLowBandwidth(true);
            Log.d("[Call Manager] Low bandwidth enabled in call params");
        }
 
        if (forceZRTP) {
            params.setMediaEncryption(MediaEncryption.ZRTP);
        }
 
        String recordFile =
                FileUtils.getCallRecordingFilename(
                        LinphoneContext.instance().getApplicationContext(), address);
        params.setRecordFile(recordFile);
 
        core.inviteAddressWithParams(address, params);
    }
 
    private boolean reinviteWithVideo() {
        Core core = LinphoneManager.getCore();
        Call call = core.getCurrentCall();
        if (call == null) {
            Log.e("[Call Manager] Trying to add video while not in call");
            return false;
        }
        if (call.getRemoteParams().lowBandwidthEnabled()) {
            Log.e("[Call Manager] Remote has low bandwidth, won't be able to do video");
            return false;
        }
 
        CallParams params = core.createCallParams(call);
        if (params.videoEnabled()) return false;
 
        // Check if video possible regarding bandwidth limitations
        mBandwidthManager.updateWithProfileSettings(params);
 
        // Abort if not enough bandwidth...
        if (!params.videoEnabled()) {
            return false;
        }
 
        // Not yet in video call: try to re-invite with video
        call.update(params);
        return true;
    }
 
    private void enableCamera(Call call, boolean enable) {
        if (call != null) {
            call.enableCamera(enable);
            if (mContext.getResources().getBoolean(R.bool.enable_call_notification))
                LinphoneContext.instance()
                        .getNotificationManager()
                        .displayCallNotification(LinphoneManager.getCore().getCurrentCall());
        }
    }
}