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()) { |
Adrien Béraud | 22ec8b8 | 2019-07-27 21:53:50 -0400 | [diff] [blame] | 95 | mCallService.restartAudioLayer(); |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 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 | 34a6fa2 | 2019-07-28 16:02:59 -0400 | [diff] [blame^] | 121 | mCompositeDisposable.add(mHardwareService.getAudioState() |
| 122 | .observeOn(mUiScheduler) |
| 123 | .subscribe(state -> getView().updateAudioState(state))); |
| 124 | |
Adrien Béraud | 22ec8b8 | 2019-07-27 21:53:50 -0400 | [diff] [blame] | 125 | /*mCompositeDisposable.add(mHardwareService |
Adrien Béraud | 057e9a3 | 2018-09-30 17:48:55 -0400 | [diff] [blame] | 126 | .getBluetoothEvents() |
| 127 | .subscribe(event -> { |
Rayan Osseiran | 7f77c8a | 2019-07-17 16:40:50 -0400 | [diff] [blame] | 128 | if (!event.connected && mSipCall == null) { |
Adrien Béraud | 057e9a3 | 2018-09-30 17:48:55 -0400 | [diff] [blame] | 129 | hangupCall(); |
| 130 | } |
Adrien Béraud | 22ec8b8 | 2019-07-27 21:53:50 -0400 | [diff] [blame] | 131 | }));*/ |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 132 | } |
| 133 | |
Hadrien De Sousa | 203164c | 2017-12-05 17:41:42 -0500 | [diff] [blame] | 134 | public void initOutGoing(String accountId, String contactRingId, boolean audioOnly) { |
Adrien Béraud | 85683c2 | 2018-05-23 11:18:43 -0400 | [diff] [blame] | 135 | if (accountId == null || contactRingId == null) { |
| 136 | Log.e(TAG, "initOutGoing: null account or contact"); |
| 137 | getView().finish(); |
| 138 | return; |
| 139 | } |
Hadrien De Sousa | fcf2a99 | 2017-12-15 11:08:56 -0500 | [diff] [blame] | 140 | if (mHardwareService.getCameraCount() == 0) { |
Adrien Béraud | d3bf4e4 | 2018-02-14 14:44:41 +0100 | [diff] [blame] | 141 | audioOnly = true; |
Hadrien De Sousa | fcf2a99 | 2017-12-15 11:08:56 -0500 | [diff] [blame] | 142 | } |
Adrien Béraud | eb4bb00 | 2019-03-14 15:52:48 -0400 | [diff] [blame] | 143 | //getView().blockScreenRotation(); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 144 | |
| 145 | mCompositeDisposable.add(mCallService |
| 146 | .placeCallObservable(accountId, StringUtils.toNumber(contactRingId), audioOnly) |
| 147 | .observeOn(mUiScheduler) |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 148 | .subscribe(call -> { |
Adrien Béraud | 2c378b3 | 2018-07-11 15:24:02 -0400 | [diff] [blame] | 149 | contactUpdate(call); |
Adrien Béraud | 99fd420 | 2018-12-13 13:02:21 -0500 | [diff] [blame] | 150 | confUpdate(call); |
Rayan Osseiran | db4667e | 2019-05-31 09:41:54 -0400 | [diff] [blame] | 151 | }, e -> { |
| 152 | hangupCall(); |
| 153 | Log.e(TAG, "Error with initOutgoing: " + e.getMessage()); |
| 154 | })); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 155 | } |
| 156 | |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 157 | |
Rayan Osseiran | ed9feeb | 2019-07-05 16:04:30 -0400 | [diff] [blame] | 158 | /** |
| 159 | * Returns to or starts an incoming call |
| 160 | * |
| 161 | * @param confId the call id |
| 162 | * @param actionViewOnly true if only returning to call or if using full screen intent |
| 163 | */ |
| 164 | public void initIncomingCall(String confId, boolean actionViewOnly) { |
| 165 | //getView().blockScreenRotation(); |
| 166 | |
| 167 | // if the call is incoming through a full intent, this allows the incoming call to display |
| 168 | incomingIsFullIntent = actionViewOnly; |
| 169 | |
| 170 | Observable<SipCall> callObservable = mCallService.getCallUpdates(confId).observeOn(mUiScheduler).share(); |
| 171 | |
| 172 | // Handles the case where the call has been accepted, emits a single so as to only check for permissions and start the call once |
| 173 | mCompositeDisposable.add(callObservable.firstOrError().subscribe(call -> { |
| 174 | if (!actionViewOnly) { |
| 175 | contactUpdate(call); |
| 176 | confUpdate(call); |
| 177 | callInitialized = true; |
| 178 | getView().prepareCall(true); |
| 179 | } |
Rayan Osseiran | 3dd0466 | 2019-06-28 16:47:48 -0400 | [diff] [blame] | 180 | }, e -> { |
| 181 | hangupCall(); |
Rayan Osseiran | ed9feeb | 2019-07-05 16:04:30 -0400 | [diff] [blame] | 182 | Log.e(TAG, "Error with initIncoming, preparing call flow :" , e); |
| 183 | })); |
| 184 | |
| 185 | // Handles retrieving call updates. Items emitted are only used if call is already in process or if user is returning to a call. |
| 186 | mCompositeDisposable.add(callObservable.subscribe(call -> { |
| 187 | if (callInitialized || actionViewOnly) { |
| 188 | contactUpdate(call); |
| 189 | confUpdate(call); |
| 190 | } |
| 191 | }, e -> { |
| 192 | hangupCall(); |
| 193 | Log.e(TAG, "Error with initIncoming, action view flow: ", e); |
Rayan Osseiran | 3dd0466 | 2019-06-28 16:47:48 -0400 | [diff] [blame] | 194 | })); |
| 195 | } |
| 196 | |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 197 | public void prepareOptionMenu() { |
| 198 | boolean isSpeakerOn = mHardwareService.isSpeakerPhoneOn(); |
| 199 | boolean hasContact = mSipCall != null && null != mSipCall.getContact() && mSipCall.getContact().isUnknown(); |
| 200 | boolean canDial = mOnGoingCall && mSipCall != null && !mSipCall.isIncoming(); |
Hadrien De Sousa | 203164c | 2017-12-05 17:41:42 -0500 | [diff] [blame] | 201 | boolean hasMultipleCamera = mHardwareService.getCameraCount() > 1 && mOnGoingCall && !mAudioOnly; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 202 | getView().initMenu(isSpeakerOn, hasContact, hasMultipleCamera, canDial, mOnGoingCall); |
| 203 | } |
| 204 | |
| 205 | public void chatClick() { |
| 206 | if (mSipCall == null |
| 207 | || mSipCall.getContact() == null |
| 208 | || mSipCall.getContact().getIds() == null |
| 209 | || mSipCall.getContact().getIds().isEmpty()) { |
| 210 | return; |
| 211 | } |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 212 | getView().goToConversation(mSipCall.getAccount(), mSipCall.getContact().getIds().get(0)); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Adrien Béraud | bc0898e | 2018-08-23 14:31:11 -0400 | [diff] [blame] | 215 | public void speakerClick(boolean checked) { |
| 216 | mHardwareService.toggleSpeakerphone(checked); |
| 217 | } |
| 218 | |
| 219 | public void muteMicrophoneToggled(boolean checked) { |
| 220 | mCallService.setMuted(checked); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 221 | } |
| 222 | |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 223 | |
Adrien Béraud | ec19fad | 2018-12-30 16:51:05 -0500 | [diff] [blame] | 224 | public boolean isMicrophoneMuted() { |
| 225 | return mCallService.isCaptureMuted(); |
| 226 | } |
| 227 | |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 228 | public void switchVideoInputClick() { |
Rayan Osseiran | ca0436c | 2019-07-16 10:11:06 -0400 | [diff] [blame] | 229 | if(mSipCall == null) |
| 230 | return; |
| 231 | |
Adrien Béraud | 335d32e | 2019-05-31 09:31:40 -0400 | [diff] [blame] | 232 | mHardwareService.switchInput(mSipCall.getCallId(), false); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 233 | getView().switchCameraIcon(mHardwareService.isPreviewFromFrontCamera()); |
| 234 | } |
| 235 | |
Adrien Béraud | eb4bb00 | 2019-03-14 15:52:48 -0400 | [diff] [blame] | 236 | public void configurationChanged(int rotation) { |
| 237 | mHardwareService.setDeviceOrientation(rotation); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | public void dialpadClick() { |
| 241 | getView().displayDialPadKeyboard(); |
| 242 | } |
| 243 | |
| 244 | public void acceptCall() { |
| 245 | if (mSipCall == null) { |
| 246 | return; |
| 247 | } |
| 248 | mCallService.accept(mSipCall.getCallId()); |
| 249 | } |
| 250 | |
| 251 | public void hangupCall() { |
| 252 | if (mSipCall != null) { |
| 253 | mCallService.hangUp(mSipCall.getCallId()); |
| 254 | } |
| 255 | finish(); |
| 256 | } |
| 257 | |
| 258 | public void refuseCall() { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 259 | final SipCall call = mSipCall; |
| 260 | if (call != null) { |
| 261 | mCallService.refuse(call.getCallId()); |
| 262 | mNotificationService.cancelCallNotification(call.getCallId().hashCode()); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 263 | } |
| 264 | finish(); |
| 265 | } |
| 266 | |
| 267 | public void videoSurfaceCreated(Object holder) { |
Alexandre Lision | e4d4026 | 2016-12-12 14:30:09 -0500 | [diff] [blame] | 268 | if (mSipCall == null) { |
| 269 | return; |
| 270 | } |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 271 | mHardwareService.addVideoSurface(mSipCall.getCallId(), holder); |
| 272 | getView().displayContactBubble(false); |
| 273 | } |
| 274 | |
| 275 | public void previewVideoSurfaceCreated(Object holder) { |
Adrien Béraud | 0c14a9e | 2019-06-01 19:28:14 -0400 | [diff] [blame] | 276 | mHardwareService.addPreviewVideoSurface(holder, mSipCall); |
Adrien Béraud | 30532a5 | 2018-09-11 15:10:05 -0400 | [diff] [blame] | 277 | //mHardwareService.startCapture(null); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | public void videoSurfaceDestroyed() { |
| 281 | if (mSipCall == null) { |
| 282 | return; |
| 283 | } |
| 284 | mHardwareService.removeVideoSurface(mSipCall.getCallId()); |
| 285 | } |
| 286 | |
| 287 | public void previewVideoSurfaceDestroyed() { |
| 288 | mHardwareService.removePreviewVideoSurface(); |
Adrien Béraud | 30532a5 | 2018-09-11 15:10:05 -0400 | [diff] [blame] | 289 | mHardwareService.endCapture(); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | public void displayChanged() { |
Adrien Béraud | 335d32e | 2019-05-31 09:31:40 -0400 | [diff] [blame] | 293 | mHardwareService.switchInput(mSipCall.getCallId(), false); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | public void layoutChanged() { |
Adrien Béraud | de0d452 | 2018-12-12 12:11:02 -0500 | [diff] [blame] | 297 | //getView().resetVideoSize(videoWidth, videoHeight, previewWidth, previewHeight); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 298 | } |
| 299 | |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 300 | |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 301 | public void uiVisibilityChanged(boolean displayed) { |
Adrien Béraud | 665faa2 | 2018-07-26 16:51:20 -0400 | [diff] [blame] | 302 | CallView view = getView(); |
| 303 | if (view != null) |
| 304 | view.displayHangupButton(mOnGoingCall && displayed); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | private void finish() { |
Adrien Béraud | 3aeed5d | 2018-07-26 11:26:13 -0400 | [diff] [blame] | 308 | if (timeUpdateTask != null && !timeUpdateTask.isDisposed()) { |
| 309 | timeUpdateTask.dispose(); |
| 310 | timeUpdateTask = null; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 311 | } |
| 312 | mSipCall = null; |
Adrien Béraud | 8d6baf3 | 2018-08-03 23:37:31 -0400 | [diff] [blame] | 313 | CallView view = getView(); |
| 314 | if (view != null) |
| 315 | view.finish(); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 316 | } |
| 317 | |
Adrien Béraud | 2c378b3 | 2018-07-11 15:24:02 -0400 | [diff] [blame] | 318 | private void contactUpdate(final SipCall call) { |
| 319 | if (mSipCall != call) { |
| 320 | mSipCall = call; |
Adrien Béraud | 8d71075 | 2019-02-20 13:53:59 -0500 | [diff] [blame] | 321 | mCompositeDisposable.add(mContactService.observeContact(call.getAccount(), call.getContact()) |
Adrien Béraud | 2c378b3 | 2018-07-11 15:24:02 -0400 | [diff] [blame] | 322 | .observeOn(mUiScheduler) |
Rayan Osseiran | db4667e | 2019-05-31 09:41:54 -0400 | [diff] [blame] | 323 | .subscribe(c -> getView().updateContactBubble(c), e -> Log.e(TAG, e.getMessage()))); |
Adrien Béraud | 2c378b3 | 2018-07-11 15:24:02 -0400 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Adrien Béraud | 99fd420 | 2018-12-13 13:02:21 -0500 | [diff] [blame] | 327 | private void confUpdate(SipCall call) { |
| 328 | mAudioOnly = call.isAudioOnly(); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 329 | CallView view = getView(); |
Adrien Béraud | 99fd420 | 2018-12-13 13:02:21 -0500 | [diff] [blame] | 330 | if (view == null) |
| 331 | return; |
| 332 | view.updateMenu(); |
| 333 | if (call.isOnGoing()) { |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 334 | mOnGoingCall = true; |
Adrien Béraud | 34a6fa2 | 2019-07-28 16:02:59 -0400 | [diff] [blame^] | 335 | view.initNormalStateDisplay(mAudioOnly, isMicrophoneMuted()); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 336 | view.updateMenu(); |
Hadrien De Sousa | 203164c | 2017-12-05 17:41:42 -0500 | [diff] [blame] | 337 | if (!mAudioOnly) { |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 338 | mHardwareService.setPreviewSettings(); |
Rayan Osseiran | c1532d4 | 2019-06-05 13:17:53 -0400 | [diff] [blame] | 339 | view.displayVideoSurface(true, mDeviceRuntimeService.hasVideoPermission()); |
Rayan Osseiran | 3dd0466 | 2019-06-28 16:47:48 -0400 | [diff] [blame] | 340 | if (permissionChanged) { |
Adrien Béraud | 335d32e | 2019-05-31 09:31:40 -0400 | [diff] [blame] | 341 | mHardwareService.switchInput(mSipCall.getCallId(), permissionChanged); |
| 342 | permissionChanged = false; |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 343 | } |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 344 | } |
Adrien Béraud | 3aeed5d | 2018-07-26 11:26:13 -0400 | [diff] [blame] | 345 | if (timeUpdateTask != null) |
| 346 | timeUpdateTask.dispose(); |
Adrien Béraud | 99fd420 | 2018-12-13 13:02:21 -0500 | [diff] [blame] | 347 | timeUpdateTask = mUiScheduler.schedulePeriodicallyDirect(this::updateTime, 0, 1, TimeUnit.SECONDS); |
| 348 | } else if (call.isRinging()) { |
Rayan Osseiran | de9ac14 | 2019-07-02 13:01:32 -0400 | [diff] [blame] | 349 | view.handleCallWakelock(mAudioOnly); |
Adrien Béraud | 99fd420 | 2018-12-13 13:02:21 -0500 | [diff] [blame] | 350 | if (call.isIncoming()) { |
| 351 | if (mAccountService.getAccount(call.getAccount()).isAutoanswerEnabled()) { |
| 352 | mCallService.accept(call.getCallId()); |
Rayan Osseiran | 3dd0466 | 2019-06-28 16:47:48 -0400 | [diff] [blame] | 353 | // only display the incoming call screen if the notification is a full screen intent |
| 354 | } else if (incomingIsFullIntent) { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 355 | view.initIncomingCallDisplay(); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 356 | } |
| 357 | } else { |
| 358 | mOnGoingCall = false; |
Adrien Béraud | 99fd420 | 2018-12-13 13:02:21 -0500 | [diff] [blame] | 359 | view.updateCallStatus(call.getCallState()); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 360 | view.initOutGoingCallDisplay(); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 361 | } |
| 362 | } else { |
| 363 | finish(); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | private void updateTime() { |
Adrien Béraud | 6325fad | 2018-08-15 15:34:11 -0400 | [diff] [blame] | 368 | CallView view = getView(); |
| 369 | if (view != null && mSipCall != null) { |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 370 | long duration = System.currentTimeMillis() - mSipCall.getTimestampStart(); |
| 371 | duration = duration / 1000; |
| 372 | if (mSipCall.isOnGoing()) { |
Adrien Béraud | 6325fad | 2018-08-15 15:34:11 -0400 | [diff] [blame] | 373 | view.updateTime(duration); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 378 | private void onVideoEvent(HardwareService.VideoEvent event) { |
Adrien Béraud | eb4bb00 | 2019-03-14 15:52:48 -0400 | [diff] [blame] | 379 | 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] | 380 | |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 381 | if (event.start) { |
Rayan Osseiran | c1532d4 | 2019-06-05 13:17:53 -0400 | [diff] [blame] | 382 | getView().displayVideoSurface(true, !isPipMode() && mDeviceRuntimeService.hasVideoPermission()); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 383 | } else if (mSipCall != null && mSipCall.getCallId().equals(event.callId)) { |
Rayan Osseiran | c1532d4 | 2019-06-05 13:17:53 -0400 | [diff] [blame] | 384 | getView().displayVideoSurface(event.started, event.started && !isPipMode() && mDeviceRuntimeService.hasVideoPermission()); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 385 | if (event.started) { |
| 386 | videoWidth = event.w; |
| 387 | videoHeight = event.h; |
Adrien Béraud | eb4bb00 | 2019-03-14 15:52:48 -0400 | [diff] [blame] | 388 | getView().resetVideoSize(videoWidth, videoHeight); |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 389 | } |
Adrien Béraud | 30532a5 | 2018-09-11 15:10:05 -0400 | [diff] [blame] | 390 | } else if (event.callId == null) { |
| 391 | if (event.started) { |
| 392 | previewWidth = event.w; |
| 393 | previewHeight = event.h; |
Adrien Béraud | eb4bb00 | 2019-03-14 15:52:48 -0400 | [diff] [blame] | 394 | getView().resetPreviewVideoSize(previewWidth, previewHeight, event.rot); |
Adrien Béraud | 30532a5 | 2018-09-11 15:10:05 -0400 | [diff] [blame] | 395 | } |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 396 | } |
Adrien Béraud | eb4bb00 | 2019-03-14 15:52:48 -0400 | [diff] [blame] | 397 | /*if (event.started || event.start) { |
Adrien Béraud | de0d452 | 2018-12-12 12:11:02 -0500 | [diff] [blame] | 398 | getView().resetVideoSize(videoWidth, videoHeight, previewWidth, previewHeight); |
Adrien Béraud | eb4bb00 | 2019-03-14 15:52:48 -0400 | [diff] [blame] | 399 | }*/ |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 400 | } |
Pierre Duchemin | 8b2c1b5 | 2017-12-29 17:17:13 -0500 | [diff] [blame] | 401 | |
| 402 | public void positiveButtonClicked() { |
| 403 | if (mSipCall.isRinging() && mSipCall.isIncoming()) { |
| 404 | acceptCall(); |
| 405 | } else { |
| 406 | hangupCall(); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | public void negativeButtonClicked() { |
| 411 | if (mSipCall.isRinging() && mSipCall.isIncoming()) { |
| 412 | refuseCall(); |
| 413 | } else { |
| 414 | hangupCall(); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | public void toggleButtonClicked() { |
| 419 | if (!(mSipCall.isRinging() && mSipCall.isIncoming())) { |
| 420 | hangupCall(); |
| 421 | } |
| 422 | } |
Adrien Béraud | 79808d0 | 2018-02-20 00:16:28 +0100 | [diff] [blame] | 423 | |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 424 | public boolean isAudioOnly() { |
| 425 | return mAudioOnly; |
| 426 | } |
| 427 | |
Adrien Béraud | 79808d0 | 2018-02-20 00:16:28 +0100 | [diff] [blame] | 428 | public void requestPipMode() { |
Adrien Béraud | 6325fad | 2018-08-15 15:34:11 -0400 | [diff] [blame] | 429 | if (mSipCall != null && mSipCall.isOnGoing() && !mSipCall.isAudioOnly()) { |
Adrien Béraud | 79808d0 | 2018-02-20 00:16:28 +0100 | [diff] [blame] | 430 | getView().enterPipMode(mSipCall); |
| 431 | } |
| 432 | } |
| 433 | |
Rayan Osseiran | c1532d4 | 2019-06-05 13:17:53 -0400 | [diff] [blame] | 434 | public boolean isPipMode() { |
| 435 | return pipIsActive; |
| 436 | } |
| 437 | |
Adrien Béraud | 79808d0 | 2018-02-20 00:16:28 +0100 | [diff] [blame] | 438 | public void pipModeChanged(boolean pip) { |
Rayan Osseiran | c1532d4 | 2019-06-05 13:17:53 -0400 | [diff] [blame] | 439 | pipIsActive = pip; |
Adrien Béraud | 79808d0 | 2018-02-20 00:16:28 +0100 | [diff] [blame] | 440 | if (pip) { |
| 441 | getView().displayHangupButton(false); |
| 442 | getView().displayPreviewSurface(false); |
Rayan Osseiran | c1532d4 | 2019-06-05 13:17:53 -0400 | [diff] [blame] | 443 | getView().displayVideoSurface(true, false); |
Adrien Béraud | 79808d0 | 2018-02-20 00:16:28 +0100 | [diff] [blame] | 444 | } else { |
| 445 | getView().displayPreviewSurface(true); |
Rayan Osseiran | c1532d4 | 2019-06-05 13:17:53 -0400 | [diff] [blame] | 446 | getView().displayVideoSurface(true, mDeviceRuntimeService.hasVideoPermission()); |
Adrien Béraud | 79808d0 | 2018-02-20 00:16:28 +0100 | [diff] [blame] | 447 | } |
| 448 | } |
Adrien Béraud | bc0898e | 2018-08-23 14:31:11 -0400 | [diff] [blame] | 449 | |
| 450 | public boolean isSpeakerphoneOn() { |
| 451 | return mHardwareService.isSpeakerPhoneOn(); |
| 452 | } |
Adrien Béraud | bcbb4d4 | 2018-12-08 16:06:39 -0500 | [diff] [blame] | 453 | |
| 454 | public void sendDtmf(CharSequence s) { |
| 455 | mCallService.playDtmf(s.toString()); |
| 456 | } |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 457 | } |