Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 1 | /* |
Sébastien Blin | ab53ada | 2019-01-02 17:23:00 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2019 Savoir-faire Linux Inc. |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 3 | * |
| 4 | * Author: Hadrien De Sousa <hadrien.desousa@savoirfairelinux.com> |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 5 | * Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com> |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | package cx.ring.call; |
| 22 | |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 23 | import java.util.concurrent.TimeUnit; |
| 24 | |
| 25 | import javax.inject.Inject; |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 26 | import javax.inject.Named; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 27 | |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 28 | import cx.ring.model.SipCall; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 29 | import cx.ring.mvp.RootPresenter; |
| 30 | import cx.ring.services.AccountService; |
| 31 | import cx.ring.services.CallService; |
Adrien Béraud | 8d71075 | 2019-02-20 13:53:59 -0500 | [diff] [blame] | 32 | import cx.ring.services.ContactService; |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 33 | import cx.ring.services.DeviceRuntimeService; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 34 | import cx.ring.services.HardwareService; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 35 | import cx.ring.services.NotificationService; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 36 | import cx.ring.utils.Log; |
Hadrien De Sousa | c165e9b | 2017-11-24 12:33:32 -0500 | [diff] [blame] | 37 | import cx.ring.utils.StringUtils; |
Rayan Osseiran | ed9feeb | 2019-07-05 16:04:30 -0400 | [diff] [blame] | 38 | import io.reactivex.Observable; |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 39 | import io.reactivex.Scheduler; |
Adrien Béraud | 3aeed5d | 2018-07-26 11:26:13 -0400 | [diff] [blame] | 40 | import io.reactivex.disposables.Disposable; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 41 | |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 42 | public class CallPresenter extends RootPresenter<CallView> { |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 43 | |
| 44 | public final static String TAG = CallPresenter.class.getSimpleName(); |
| 45 | |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 46 | private AccountService mAccountService; |
Adrien Béraud | 8d71075 | 2019-02-20 13:53:59 -0500 | [diff] [blame] | 47 | private ContactService mContactService; |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 48 | private NotificationService mNotificationService; |
| 49 | private HardwareService mHardwareService; |
| 50 | private CallService mCallService; |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 51 | private DeviceRuntimeService mDeviceRuntimeService; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 52 | |
| 53 | private SipCall mSipCall; |
| 54 | private boolean mOnGoingCall = false; |
Hadrien De Sousa | 203164c | 2017-12-05 17:41:42 -0500 | [diff] [blame] | 55 | private boolean mAudioOnly = true; |
Adrien Béraud | 335d32e | 2019-05-31 09:31:40 -0400 | [diff] [blame] | 56 | private boolean permissionChanged = false; |
Rayan Osseiran | c1532d4 | 2019-06-05 13:17:53 -0400 | [diff] [blame] | 57 | private boolean pipIsActive = false; |
Rayan Osseiran | 3dd0466 | 2019-06-28 16:47:48 -0400 | [diff] [blame] | 58 | private boolean incomingIsFullIntent = true; |
Rayan Osseiran | ed9feeb | 2019-07-05 16:04:30 -0400 | [diff] [blame] | 59 | private boolean callInitialized = false; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 60 | |
| 61 | private int videoWidth = -1; |
| 62 | private int videoHeight = -1; |
| 63 | private int previewWidth = -1; |
| 64 | private int previewHeight = -1; |
| 65 | |
Adrien Béraud | 3aeed5d | 2018-07-26 11:26:13 -0400 | [diff] [blame] | 66 | private Disposable timeUpdateTask = null; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 67 | |
| 68 | @Inject |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 69 | @Named("UiScheduler") |
| 70 | protected Scheduler mUiScheduler; |
| 71 | |
| 72 | @Inject |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 73 | public CallPresenter(AccountService accountService, |
Adrien Béraud | 8d71075 | 2019-02-20 13:53:59 -0500 | [diff] [blame] | 74 | ContactService contactService, |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 75 | NotificationService notificationService, |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 76 | HardwareService hardwareService, |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 77 | CallService callService, DeviceRuntimeService deviceRuntimeService) { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 78 | mAccountService = accountService; |
Adrien Béraud | 8d71075 | 2019-02-20 13:53:59 -0500 | [diff] [blame] | 79 | mContactService = contactService; |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 80 | mNotificationService = notificationService; |
| 81 | mHardwareService = hardwareService; |
| 82 | mCallService = callService; |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 83 | mDeviceRuntimeService = deviceRuntimeService; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 84 | } |
| 85 | |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 86 | public void cameraPermissionChanged(boolean isGranted) { |
| 87 | if (isGranted && mHardwareService.isVideoAvailable()) { |
| 88 | mHardwareService.initVideo(); |
Adrien Béraud | 335d32e | 2019-05-31 09:31:40 -0400 | [diff] [blame] | 89 | permissionChanged = true; |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | public void audioPermissionChanged(boolean isGranted) { |
Rayan Osseiran | db4667e | 2019-05-31 09:41:54 -0400 | [diff] [blame] | 94 | if (isGranted && mHardwareService.hasMicrophone()) { |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 95 | mCallService.setAudioPlugin(mCallService.getCurrentAudioOutputPlugin()); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 100 | @Override |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 101 | public void unbindView() { |
Hadrien De Sousa | 203164c | 2017-12-05 17:41:42 -0500 | [diff] [blame] | 102 | if (!mAudioOnly) { |
Adrien Béraud | 30532a5 | 2018-09-11 15:10:05 -0400 | [diff] [blame] | 103 | mHardwareService.endCapture(); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 104 | } |
Adrien Béraud | 99fd420 | 2018-12-13 13:02:21 -0500 | [diff] [blame] | 105 | super.unbindView(); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void bindView(CallView view) { |
| 110 | super.bindView(view); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 111 | mCompositeDisposable.add(mAccountService.getRegisteredNames() |
| 112 | .observeOn(mUiScheduler) |
| 113 | .subscribe(r -> { |
| 114 | if (mSipCall != null && mSipCall.getContact() != null) { |
| 115 | getView().updateContactBubble(mSipCall.getContact()); |
| 116 | } |
| 117 | })); |
| 118 | mCompositeDisposable.add(mHardwareService.getVideoEvents() |
| 119 | .observeOn(mUiScheduler) |
| 120 | .subscribe(this::onVideoEvent)); |
Adrien Béraud | 057e9a3 | 2018-09-30 17:48:55 -0400 | [diff] [blame] | 121 | mCompositeDisposable.add(mHardwareService |
| 122 | .getBluetoothEvents() |
| 123 | .subscribe(event -> { |
Rayan Osseiran | 7f77c8a | 2019-07-17 16:40:50 -0400 | [diff] [blame^] | 124 | if (!event.connected && mSipCall == null) { |
Adrien Béraud | 057e9a3 | 2018-09-30 17:48:55 -0400 | [diff] [blame] | 125 | hangupCall(); |
| 126 | } |
| 127 | })); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Hadrien De Sousa | 203164c | 2017-12-05 17:41:42 -0500 | [diff] [blame] | 130 | public void initOutGoing(String accountId, String contactRingId, boolean audioOnly) { |
Adrien Béraud | 85683c2 | 2018-05-23 11:18:43 -0400 | [diff] [blame] | 131 | if (accountId == null || contactRingId == null) { |
| 132 | Log.e(TAG, "initOutGoing: null account or contact"); |
| 133 | getView().finish(); |
| 134 | return; |
| 135 | } |
Hadrien De Sousa | fcf2a99 | 2017-12-15 11:08:56 -0500 | [diff] [blame] | 136 | if (mHardwareService.getCameraCount() == 0) { |
Adrien Béraud | d3bf4e4 | 2018-02-14 14:44:41 +0100 | [diff] [blame] | 137 | audioOnly = true; |
Hadrien De Sousa | fcf2a99 | 2017-12-15 11:08:56 -0500 | [diff] [blame] | 138 | } |
Adrien Béraud | eb4bb00 | 2019-03-14 15:52:48 -0400 | [diff] [blame] | 139 | //getView().blockScreenRotation(); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 140 | |
| 141 | mCompositeDisposable.add(mCallService |
| 142 | .placeCallObservable(accountId, StringUtils.toNumber(contactRingId), audioOnly) |
| 143 | .observeOn(mUiScheduler) |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 144 | .subscribe(call -> { |
Adrien Béraud | 2c378b3 | 2018-07-11 15:24:02 -0400 | [diff] [blame] | 145 | contactUpdate(call); |
Adrien Béraud | 99fd420 | 2018-12-13 13:02:21 -0500 | [diff] [blame] | 146 | confUpdate(call); |
Rayan Osseiran | db4667e | 2019-05-31 09:41:54 -0400 | [diff] [blame] | 147 | }, e -> { |
| 148 | hangupCall(); |
| 149 | Log.e(TAG, "Error with initOutgoing: " + e.getMessage()); |
| 150 | })); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 151 | } |
| 152 | |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 153 | |
Rayan Osseiran | ed9feeb | 2019-07-05 16:04:30 -0400 | [diff] [blame] | 154 | /** |
| 155 | * Returns to or starts an incoming call |
| 156 | * |
| 157 | * @param confId the call id |
| 158 | * @param actionViewOnly true if only returning to call or if using full screen intent |
| 159 | */ |
| 160 | public void initIncomingCall(String confId, boolean actionViewOnly) { |
| 161 | //getView().blockScreenRotation(); |
| 162 | |
| 163 | // if the call is incoming through a full intent, this allows the incoming call to display |
| 164 | incomingIsFullIntent = actionViewOnly; |
| 165 | |
| 166 | Observable<SipCall> callObservable = mCallService.getCallUpdates(confId).observeOn(mUiScheduler).share(); |
| 167 | |
| 168 | // Handles the case where the call has been accepted, emits a single so as to only check for permissions and start the call once |
| 169 | mCompositeDisposable.add(callObservable.firstOrError().subscribe(call -> { |
| 170 | if (!actionViewOnly) { |
| 171 | contactUpdate(call); |
| 172 | confUpdate(call); |
| 173 | callInitialized = true; |
| 174 | getView().prepareCall(true); |
| 175 | } |
Rayan Osseiran | 3dd0466 | 2019-06-28 16:47:48 -0400 | [diff] [blame] | 176 | }, e -> { |
| 177 | hangupCall(); |
Rayan Osseiran | ed9feeb | 2019-07-05 16:04:30 -0400 | [diff] [blame] | 178 | Log.e(TAG, "Error with initIncoming, preparing call flow :" , e); |
| 179 | })); |
| 180 | |
| 181 | // Handles retrieving call updates. Items emitted are only used if call is already in process or if user is returning to a call. |
| 182 | mCompositeDisposable.add(callObservable.subscribe(call -> { |
| 183 | if (callInitialized || actionViewOnly) { |
| 184 | contactUpdate(call); |
| 185 | confUpdate(call); |
| 186 | } |
| 187 | }, e -> { |
| 188 | hangupCall(); |
| 189 | Log.e(TAG, "Error with initIncoming, action view flow: ", e); |
Rayan Osseiran | 3dd0466 | 2019-06-28 16:47:48 -0400 | [diff] [blame] | 190 | })); |
| 191 | } |
| 192 | |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 193 | public void prepareOptionMenu() { |
| 194 | boolean isSpeakerOn = mHardwareService.isSpeakerPhoneOn(); |
| 195 | boolean hasContact = mSipCall != null && null != mSipCall.getContact() && mSipCall.getContact().isUnknown(); |
| 196 | boolean canDial = mOnGoingCall && mSipCall != null && !mSipCall.isIncoming(); |
Hadrien De Sousa | 203164c | 2017-12-05 17:41:42 -0500 | [diff] [blame] | 197 | boolean hasMultipleCamera = mHardwareService.getCameraCount() > 1 && mOnGoingCall && !mAudioOnly; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 198 | getView().initMenu(isSpeakerOn, hasContact, hasMultipleCamera, canDial, mOnGoingCall); |
| 199 | } |
| 200 | |
| 201 | public void chatClick() { |
| 202 | if (mSipCall == null |
| 203 | || mSipCall.getContact() == null |
| 204 | || mSipCall.getContact().getIds() == null |
| 205 | || mSipCall.getContact().getIds().isEmpty()) { |
| 206 | return; |
| 207 | } |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 208 | getView().goToConversation(mSipCall.getAccount(), mSipCall.getContact().getIds().get(0)); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 209 | } |
| 210 | |
Adrien Béraud | bc0898e | 2018-08-23 14:31:11 -0400 | [diff] [blame] | 211 | public void speakerClick(boolean checked) { |
Rayan Osseiran | 7f77c8a | 2019-07-17 16:40:50 -0400 | [diff] [blame^] | 212 | mCallService.restartAudioLayer(); |
Adrien Béraud | bc0898e | 2018-08-23 14:31:11 -0400 | [diff] [blame] | 213 | mHardwareService.toggleSpeakerphone(checked); |
| 214 | } |
| 215 | |
| 216 | public void muteMicrophoneToggled(boolean checked) { |
| 217 | mCallService.setMuted(checked); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 220 | |
Adrien Béraud | ec19fad | 2018-12-30 16:51:05 -0500 | [diff] [blame] | 221 | public boolean isMicrophoneMuted() { |
| 222 | return mCallService.isCaptureMuted(); |
| 223 | } |
| 224 | |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 225 | public void switchVideoInputClick() { |
Rayan Osseiran | ca0436c | 2019-07-16 10:11:06 -0400 | [diff] [blame] | 226 | if(mSipCall == null) |
| 227 | return; |
| 228 | |
Adrien Béraud | 335d32e | 2019-05-31 09:31:40 -0400 | [diff] [blame] | 229 | mHardwareService.switchInput(mSipCall.getCallId(), false); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 230 | getView().switchCameraIcon(mHardwareService.isPreviewFromFrontCamera()); |
| 231 | } |
| 232 | |
Adrien Béraud | eb4bb00 | 2019-03-14 15:52:48 -0400 | [diff] [blame] | 233 | public void configurationChanged(int rotation) { |
| 234 | mHardwareService.setDeviceOrientation(rotation); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | public void dialpadClick() { |
| 238 | getView().displayDialPadKeyboard(); |
| 239 | } |
| 240 | |
| 241 | public void acceptCall() { |
| 242 | if (mSipCall == null) { |
| 243 | return; |
| 244 | } |
| 245 | mCallService.accept(mSipCall.getCallId()); |
| 246 | } |
| 247 | |
| 248 | public void hangupCall() { |
| 249 | if (mSipCall != null) { |
| 250 | mCallService.hangUp(mSipCall.getCallId()); |
| 251 | } |
| 252 | finish(); |
| 253 | } |
| 254 | |
| 255 | public void refuseCall() { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 256 | final SipCall call = mSipCall; |
| 257 | if (call != null) { |
| 258 | mCallService.refuse(call.getCallId()); |
| 259 | mNotificationService.cancelCallNotification(call.getCallId().hashCode()); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 260 | } |
| 261 | finish(); |
| 262 | } |
| 263 | |
| 264 | public void videoSurfaceCreated(Object holder) { |
Alexandre Lision | e4d4026 | 2016-12-12 14:30:09 -0500 | [diff] [blame] | 265 | if (mSipCall == null) { |
| 266 | return; |
| 267 | } |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 268 | mHardwareService.addVideoSurface(mSipCall.getCallId(), holder); |
| 269 | getView().displayContactBubble(false); |
| 270 | } |
| 271 | |
| 272 | public void previewVideoSurfaceCreated(Object holder) { |
Adrien Béraud | 0c14a9e | 2019-06-01 19:28:14 -0400 | [diff] [blame] | 273 | mHardwareService.addPreviewVideoSurface(holder, mSipCall); |
Adrien Béraud | 30532a5 | 2018-09-11 15:10:05 -0400 | [diff] [blame] | 274 | //mHardwareService.startCapture(null); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | public void videoSurfaceDestroyed() { |
| 278 | if (mSipCall == null) { |
| 279 | return; |
| 280 | } |
| 281 | mHardwareService.removeVideoSurface(mSipCall.getCallId()); |
| 282 | } |
| 283 | |
| 284 | public void previewVideoSurfaceDestroyed() { |
| 285 | mHardwareService.removePreviewVideoSurface(); |
Adrien Béraud | 30532a5 | 2018-09-11 15:10:05 -0400 | [diff] [blame] | 286 | mHardwareService.endCapture(); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | public void displayChanged() { |
Adrien Béraud | 335d32e | 2019-05-31 09:31:40 -0400 | [diff] [blame] | 290 | mHardwareService.switchInput(mSipCall.getCallId(), false); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | public void layoutChanged() { |
Adrien Béraud | de0d452 | 2018-12-12 12:11:02 -0500 | [diff] [blame] | 294 | //getView().resetVideoSize(videoWidth, videoHeight, previewWidth, previewHeight); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 295 | } |
| 296 | |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 297 | |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 298 | public void uiVisibilityChanged(boolean displayed) { |
Adrien Béraud | 665faa2 | 2018-07-26 16:51:20 -0400 | [diff] [blame] | 299 | CallView view = getView(); |
| 300 | if (view != null) |
| 301 | view.displayHangupButton(mOnGoingCall && displayed); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | private void finish() { |
Adrien Béraud | 3aeed5d | 2018-07-26 11:26:13 -0400 | [diff] [blame] | 305 | if (timeUpdateTask != null && !timeUpdateTask.isDisposed()) { |
| 306 | timeUpdateTask.dispose(); |
| 307 | timeUpdateTask = null; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 308 | } |
| 309 | mSipCall = null; |
Adrien Béraud | 8d6baf3 | 2018-08-03 23:37:31 -0400 | [diff] [blame] | 310 | CallView view = getView(); |
| 311 | if (view != null) |
| 312 | view.finish(); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 313 | } |
| 314 | |
Adrien Béraud | 2c378b3 | 2018-07-11 15:24:02 -0400 | [diff] [blame] | 315 | private void contactUpdate(final SipCall call) { |
| 316 | if (mSipCall != call) { |
| 317 | mSipCall = call; |
Adrien Béraud | 8d71075 | 2019-02-20 13:53:59 -0500 | [diff] [blame] | 318 | mCompositeDisposable.add(mContactService.observeContact(call.getAccount(), call.getContact()) |
Adrien Béraud | 2c378b3 | 2018-07-11 15:24:02 -0400 | [diff] [blame] | 319 | .observeOn(mUiScheduler) |
Rayan Osseiran | db4667e | 2019-05-31 09:41:54 -0400 | [diff] [blame] | 320 | .subscribe(c -> getView().updateContactBubble(c), e -> Log.e(TAG, e.getMessage()))); |
Adrien Béraud | 2c378b3 | 2018-07-11 15:24:02 -0400 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Adrien Béraud | 99fd420 | 2018-12-13 13:02:21 -0500 | [diff] [blame] | 324 | private void confUpdate(SipCall call) { |
| 325 | mAudioOnly = call.isAudioOnly(); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 326 | CallView view = getView(); |
Adrien Béraud | 99fd420 | 2018-12-13 13:02:21 -0500 | [diff] [blame] | 327 | if (view == null) |
| 328 | return; |
| 329 | view.updateMenu(); |
| 330 | if (call.isOnGoing()) { |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 331 | mOnGoingCall = true; |
Rayan Osseiran | 7f77c8a | 2019-07-17 16:40:50 -0400 | [diff] [blame^] | 332 | mCallService.restartAudioLayer(); |
| 333 | mHardwareService.toggleSpeakerphone(mHardwareService.shouldPlaySpeaker()); |
Adrien Béraud | ec19fad | 2018-12-30 16:51:05 -0500 | [diff] [blame] | 334 | view.initNormalStateDisplay(mAudioOnly, mHardwareService.isSpeakerPhoneOn(), isMicrophoneMuted()); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 335 | view.updateMenu(); |
Hadrien De Sousa | 203164c | 2017-12-05 17:41:42 -0500 | [diff] [blame] | 336 | if (!mAudioOnly) { |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 337 | mHardwareService.setPreviewSettings(); |
Rayan Osseiran | c1532d4 | 2019-06-05 13:17:53 -0400 | [diff] [blame] | 338 | view.displayVideoSurface(true, mDeviceRuntimeService.hasVideoPermission()); |
Rayan Osseiran | 3dd0466 | 2019-06-28 16:47:48 -0400 | [diff] [blame] | 339 | if (permissionChanged) { |
Adrien Béraud | 335d32e | 2019-05-31 09:31:40 -0400 | [diff] [blame] | 340 | mHardwareService.switchInput(mSipCall.getCallId(), permissionChanged); |
| 341 | permissionChanged = false; |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 342 | } |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 343 | } |
Adrien Béraud | 3aeed5d | 2018-07-26 11:26:13 -0400 | [diff] [blame] | 344 | if (timeUpdateTask != null) |
| 345 | timeUpdateTask.dispose(); |
Adrien Béraud | 99fd420 | 2018-12-13 13:02:21 -0500 | [diff] [blame] | 346 | timeUpdateTask = mUiScheduler.schedulePeriodicallyDirect(this::updateTime, 0, 1, TimeUnit.SECONDS); |
| 347 | } else if (call.isRinging()) { |
Rayan Osseiran | de9ac14 | 2019-07-02 13:01:32 -0400 | [diff] [blame] | 348 | view.handleCallWakelock(mAudioOnly); |
Adrien Béraud | 99fd420 | 2018-12-13 13:02:21 -0500 | [diff] [blame] | 349 | if (call.isIncoming()) { |
| 350 | if (mAccountService.getAccount(call.getAccount()).isAutoanswerEnabled()) { |
| 351 | mCallService.accept(call.getCallId()); |
Rayan Osseiran | 3dd0466 | 2019-06-28 16:47:48 -0400 | [diff] [blame] | 352 | // only display the incoming call screen if the notification is a full screen intent |
| 353 | } else if (incomingIsFullIntent) { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 354 | view.initIncomingCallDisplay(); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 355 | } |
| 356 | } else { |
| 357 | mOnGoingCall = false; |
Adrien Béraud | 99fd420 | 2018-12-13 13:02:21 -0500 | [diff] [blame] | 358 | view.updateCallStatus(call.getCallState()); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 359 | view.initOutGoingCallDisplay(); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 360 | } |
| 361 | } else { |
| 362 | finish(); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | private void updateTime() { |
Adrien Béraud | 6325fad | 2018-08-15 15:34:11 -0400 | [diff] [blame] | 367 | CallView view = getView(); |
| 368 | if (view != null && mSipCall != null) { |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 369 | long duration = System.currentTimeMillis() - mSipCall.getTimestampStart(); |
| 370 | duration = duration / 1000; |
| 371 | if (mSipCall.isOnGoing()) { |
Adrien Béraud | 6325fad | 2018-08-15 15:34:11 -0400 | [diff] [blame] | 372 | view.updateTime(duration); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 377 | private void onVideoEvent(HardwareService.VideoEvent event) { |
Adrien Béraud | eb4bb00 | 2019-03-14 15:52:48 -0400 | [diff] [blame] | 378 | Log.d(TAG, "VIDEO_EVENT: " + event.start + " " + event.callId + " " + event.w + "x" + event.h); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 379 | |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 380 | if (event.start) { |
Rayan Osseiran | c1532d4 | 2019-06-05 13:17:53 -0400 | [diff] [blame] | 381 | getView().displayVideoSurface(true, !isPipMode() && mDeviceRuntimeService.hasVideoPermission()); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 382 | } else if (mSipCall != null && mSipCall.getCallId().equals(event.callId)) { |
Rayan Osseiran | c1532d4 | 2019-06-05 13:17:53 -0400 | [diff] [blame] | 383 | getView().displayVideoSurface(event.started, event.started && !isPipMode() && mDeviceRuntimeService.hasVideoPermission()); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 384 | if (event.started) { |
| 385 | videoWidth = event.w; |
| 386 | videoHeight = event.h; |
Adrien Béraud | eb4bb00 | 2019-03-14 15:52:48 -0400 | [diff] [blame] | 387 | getView().resetVideoSize(videoWidth, videoHeight); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 388 | } |
Adrien Béraud | 30532a5 | 2018-09-11 15:10:05 -0400 | [diff] [blame] | 389 | } else if (event.callId == null) { |
| 390 | if (event.started) { |
| 391 | previewWidth = event.w; |
| 392 | previewHeight = event.h; |
Adrien Béraud | eb4bb00 | 2019-03-14 15:52:48 -0400 | [diff] [blame] | 393 | getView().resetPreviewVideoSize(previewWidth, previewHeight, event.rot); |
Adrien Béraud | 30532a5 | 2018-09-11 15:10:05 -0400 | [diff] [blame] | 394 | } |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 395 | } |
Adrien Béraud | eb4bb00 | 2019-03-14 15:52:48 -0400 | [diff] [blame] | 396 | /*if (event.started || event.start) { |
Adrien Béraud | de0d452 | 2018-12-12 12:11:02 -0500 | [diff] [blame] | 397 | getView().resetVideoSize(videoWidth, videoHeight, previewWidth, previewHeight); |
Adrien Béraud | eb4bb00 | 2019-03-14 15:52:48 -0400 | [diff] [blame] | 398 | }*/ |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 399 | } |
Pierre Duchemin | 8b2c1b5 | 2017-12-29 17:17:13 -0500 | [diff] [blame] | 400 | |
| 401 | public void positiveButtonClicked() { |
| 402 | if (mSipCall.isRinging() && mSipCall.isIncoming()) { |
| 403 | acceptCall(); |
| 404 | } else { |
| 405 | hangupCall(); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | public void negativeButtonClicked() { |
| 410 | if (mSipCall.isRinging() && mSipCall.isIncoming()) { |
| 411 | refuseCall(); |
| 412 | } else { |
| 413 | hangupCall(); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | public void toggleButtonClicked() { |
| 418 | if (!(mSipCall.isRinging() && mSipCall.isIncoming())) { |
| 419 | hangupCall(); |
| 420 | } |
| 421 | } |
Adrien Béraud | 79808d0 | 2018-02-20 00:16:28 +0100 | [diff] [blame] | 422 | |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 423 | public boolean isAudioOnly() { |
| 424 | return mAudioOnly; |
| 425 | } |
| 426 | |
Adrien Béraud | 79808d0 | 2018-02-20 00:16:28 +0100 | [diff] [blame] | 427 | public void requestPipMode() { |
Adrien Béraud | 6325fad | 2018-08-15 15:34:11 -0400 | [diff] [blame] | 428 | if (mSipCall != null && mSipCall.isOnGoing() && !mSipCall.isAudioOnly()) { |
Adrien Béraud | 79808d0 | 2018-02-20 00:16:28 +0100 | [diff] [blame] | 429 | getView().enterPipMode(mSipCall); |
| 430 | } |
| 431 | } |
| 432 | |
Rayan Osseiran | c1532d4 | 2019-06-05 13:17:53 -0400 | [diff] [blame] | 433 | public boolean isPipMode() { |
| 434 | return pipIsActive; |
| 435 | } |
| 436 | |
Adrien Béraud | 79808d0 | 2018-02-20 00:16:28 +0100 | [diff] [blame] | 437 | public void pipModeChanged(boolean pip) { |
Rayan Osseiran | c1532d4 | 2019-06-05 13:17:53 -0400 | [diff] [blame] | 438 | pipIsActive = pip; |
Adrien Béraud | 79808d0 | 2018-02-20 00:16:28 +0100 | [diff] [blame] | 439 | if (pip) { |
| 440 | getView().displayHangupButton(false); |
| 441 | getView().displayPreviewSurface(false); |
Rayan Osseiran | c1532d4 | 2019-06-05 13:17:53 -0400 | [diff] [blame] | 442 | getView().displayVideoSurface(true, false); |
Adrien Béraud | 79808d0 | 2018-02-20 00:16:28 +0100 | [diff] [blame] | 443 | } else { |
| 444 | getView().displayPreviewSurface(true); |
Rayan Osseiran | c1532d4 | 2019-06-05 13:17:53 -0400 | [diff] [blame] | 445 | getView().displayVideoSurface(true, mDeviceRuntimeService.hasVideoPermission()); |
Adrien Béraud | 79808d0 | 2018-02-20 00:16:28 +0100 | [diff] [blame] | 446 | } |
| 447 | } |
Adrien Béraud | bc0898e | 2018-08-23 14:31:11 -0400 | [diff] [blame] | 448 | |
| 449 | public boolean isSpeakerphoneOn() { |
| 450 | return mHardwareService.isSpeakerPhoneOn(); |
| 451 | } |
Adrien Béraud | bcbb4d4 | 2018-12-08 16:06:39 -0500 | [diff] [blame] | 452 | |
| 453 | public void sendDtmf(CharSequence s) { |
| 454 | mCallService.playDtmf(s.toString()); |
| 455 | } |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 456 | } |