CallActivity.java 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * @author Tim Krüger
  6. * Copyright (C) 2022 Tim Krüger <t@timkrueger.me>
  7. * Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. package com.nextcloud.talk.activities;
  23. import android.Manifest;
  24. import android.animation.Animator;
  25. import android.animation.AnimatorListenerAdapter;
  26. import android.annotation.SuppressLint;
  27. import android.app.PendingIntent;
  28. import android.app.RemoteAction;
  29. import android.content.BroadcastReceiver;
  30. import android.content.Context;
  31. import android.content.Intent;
  32. import android.content.IntentFilter;
  33. import android.content.pm.PackageManager;
  34. import android.content.res.Configuration;
  35. import android.graphics.Color;
  36. import android.graphics.drawable.Icon;
  37. import android.media.AudioAttributes;
  38. import android.media.MediaPlayer;
  39. import android.net.Uri;
  40. import android.os.Build;
  41. import android.os.Bundle;
  42. import android.os.Handler;
  43. import android.os.Looper;
  44. import android.text.TextUtils;
  45. import android.util.DisplayMetrics;
  46. import android.util.Log;
  47. import android.view.MotionEvent;
  48. import android.view.View;
  49. import android.view.ViewGroup;
  50. import android.view.ViewTreeObserver;
  51. import android.widget.FrameLayout;
  52. import android.widget.RelativeLayout;
  53. import android.widget.Toast;
  54. import com.bluelinelabs.logansquare.LoganSquare;
  55. import com.nextcloud.talk.R;
  56. import com.nextcloud.talk.adapters.ParticipantDisplayItem;
  57. import com.nextcloud.talk.adapters.ParticipantsAdapter;
  58. import com.nextcloud.talk.api.NcApi;
  59. import com.nextcloud.talk.application.NextcloudTalkApplication;
  60. import com.nextcloud.talk.data.user.model.User;
  61. import com.nextcloud.talk.databinding.CallActivityBinding;
  62. import com.nextcloud.talk.events.ConfigurationChangeEvent;
  63. import com.nextcloud.talk.events.MediaStreamEvent;
  64. import com.nextcloud.talk.events.NetworkEvent;
  65. import com.nextcloud.talk.events.PeerConnectionEvent;
  66. import com.nextcloud.talk.events.SessionDescriptionSendEvent;
  67. import com.nextcloud.talk.events.WebSocketCommunicationEvent;
  68. import com.nextcloud.talk.models.ExternalSignalingServer;
  69. import com.nextcloud.talk.models.json.capabilities.CapabilitiesOverall;
  70. import com.nextcloud.talk.models.json.conversations.Conversation;
  71. import com.nextcloud.talk.models.json.conversations.RoomOverall;
  72. import com.nextcloud.talk.models.json.conversations.RoomsOverall;
  73. import com.nextcloud.talk.models.json.generic.GenericOverall;
  74. import com.nextcloud.talk.models.json.participants.Participant;
  75. import com.nextcloud.talk.models.json.participants.ParticipantsOverall;
  76. import com.nextcloud.talk.models.json.signaling.DataChannelMessage;
  77. import com.nextcloud.talk.models.json.signaling.DataChannelMessageNick;
  78. import com.nextcloud.talk.models.json.signaling.NCMessagePayload;
  79. import com.nextcloud.talk.models.json.signaling.NCMessageWrapper;
  80. import com.nextcloud.talk.models.json.signaling.NCSignalingMessage;
  81. import com.nextcloud.talk.models.json.signaling.Signaling;
  82. import com.nextcloud.talk.models.json.signaling.SignalingOverall;
  83. import com.nextcloud.talk.models.json.signaling.settings.IceServer;
  84. import com.nextcloud.talk.models.json.signaling.settings.SignalingSettingsOverall;
  85. import com.nextcloud.talk.signaling.SignalingMessageReceiver;
  86. import com.nextcloud.talk.ui.dialog.AudioOutputDialog;
  87. import com.nextcloud.talk.users.UserManager;
  88. import com.nextcloud.talk.utils.ApiUtils;
  89. import com.nextcloud.talk.utils.DisplayUtils;
  90. import com.nextcloud.talk.utils.NotificationUtils;
  91. import com.nextcloud.talk.utils.animations.PulseAnimation;
  92. import com.nextcloud.talk.utils.permissions.PlatformPermissionUtil;
  93. import com.nextcloud.talk.utils.power.PowerManagerUtils;
  94. import com.nextcloud.talk.utils.preferences.AppPreferences;
  95. import com.nextcloud.talk.utils.singletons.ApplicationWideCurrentRoomHolder;
  96. import com.nextcloud.talk.webrtc.MagicWebRTCUtils;
  97. import com.nextcloud.talk.webrtc.MagicWebSocketInstance;
  98. import com.nextcloud.talk.webrtc.PeerConnectionWrapper;
  99. import com.nextcloud.talk.webrtc.WebRtcAudioManager;
  100. import com.nextcloud.talk.webrtc.WebSocketConnectionHelper;
  101. import com.wooplr.spotlight.SpotlightView;
  102. import org.apache.commons.lang3.StringEscapeUtils;
  103. import org.greenrobot.eventbus.EventBus;
  104. import org.greenrobot.eventbus.Subscribe;
  105. import org.greenrobot.eventbus.ThreadMode;
  106. import org.webrtc.AudioSource;
  107. import org.webrtc.AudioTrack;
  108. import org.webrtc.Camera1Enumerator;
  109. import org.webrtc.Camera2Enumerator;
  110. import org.webrtc.CameraEnumerator;
  111. import org.webrtc.CameraVideoCapturer;
  112. import org.webrtc.DefaultVideoDecoderFactory;
  113. import org.webrtc.DefaultVideoEncoderFactory;
  114. import org.webrtc.EglBase;
  115. import org.webrtc.Logging;
  116. import org.webrtc.MediaConstraints;
  117. import org.webrtc.MediaStream;
  118. import org.webrtc.PeerConnection;
  119. import org.webrtc.PeerConnectionFactory;
  120. import org.webrtc.RendererCommon;
  121. import org.webrtc.SurfaceTextureHelper;
  122. import org.webrtc.VideoCapturer;
  123. import org.webrtc.VideoSource;
  124. import org.webrtc.VideoTrack;
  125. import java.io.IOException;
  126. import java.util.ArrayList;
  127. import java.util.HashMap;
  128. import java.util.HashSet;
  129. import java.util.List;
  130. import java.util.Map;
  131. import java.util.Objects;
  132. import java.util.Set;
  133. import java.util.concurrent.TimeUnit;
  134. import java.util.concurrent.atomic.AtomicInteger;
  135. import javax.inject.Inject;
  136. import androidx.activity.result.ActivityResultLauncher;
  137. import androidx.activity.result.contract.ActivityResultContracts;
  138. import androidx.annotation.DrawableRes;
  139. import androidx.annotation.NonNull;
  140. import androidx.annotation.Nullable;
  141. import androidx.annotation.RequiresApi;
  142. import androidx.appcompat.app.AppCompatActivity;
  143. import androidx.core.content.ContextCompat;
  144. import androidx.core.graphics.drawable.DrawableCompat;
  145. import autodagger.AutoInjector;
  146. import io.reactivex.Observable;
  147. import io.reactivex.Observer;
  148. import io.reactivex.android.schedulers.AndroidSchedulers;
  149. import io.reactivex.disposables.Disposable;
  150. import io.reactivex.schedulers.Schedulers;
  151. import me.zhanghai.android.effortlesspermissions.AfterPermissionDenied;
  152. import me.zhanghai.android.effortlesspermissions.EffortlessPermissions;
  153. import me.zhanghai.android.effortlesspermissions.OpenAppDetailsDialogFragment;
  154. import okhttp3.Cache;
  155. import pub.devrel.easypermissions.AfterPermissionGranted;
  156. import static android.app.PendingIntent.FLAG_IMMUTABLE;
  157. import static com.nextcloud.talk.utils.bundle.BundleKeys.KEY_CALL_VOICE_ONLY;
  158. import static com.nextcloud.talk.utils.bundle.BundleKeys.KEY_CALL_WITHOUT_NOTIFICATION;
  159. import static com.nextcloud.talk.utils.bundle.BundleKeys.KEY_CONVERSATION_NAME;
  160. import static com.nextcloud.talk.utils.bundle.BundleKeys.KEY_CONVERSATION_PASSWORD;
  161. import static com.nextcloud.talk.utils.bundle.BundleKeys.KEY_FROM_NOTIFICATION_START_CALL;
  162. import static com.nextcloud.talk.utils.bundle.BundleKeys.KEY_MODIFIED_BASE_URL;
  163. import static com.nextcloud.talk.utils.bundle.BundleKeys.KEY_PARTICIPANT_PERMISSION_CAN_PUBLISH_AUDIO;
  164. import static com.nextcloud.talk.utils.bundle.BundleKeys.KEY_PARTICIPANT_PERMISSION_CAN_PUBLISH_VIDEO;
  165. import static com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ROOM_ID;
  166. import static com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ROOM_TOKEN;
  167. import static com.nextcloud.talk.utils.bundle.BundleKeys.KEY_USER_ENTITY;
  168. import static com.nextcloud.talk.webrtc.Globals.JOB_ID;
  169. import static com.nextcloud.talk.webrtc.Globals.PARTICIPANTS_UPDATE;
  170. import static com.nextcloud.talk.webrtc.Globals.ROOM_TOKEN;
  171. import static com.nextcloud.talk.webrtc.Globals.UPDATE_ALL;
  172. import static com.nextcloud.talk.webrtc.Globals.UPDATE_IN_CALL;
  173. @AutoInjector(NextcloudTalkApplication.class)
  174. public class CallActivity extends CallBaseActivity {
  175. public static final String VIDEO_STREAM_TYPE_SCREEN = "screen";
  176. public static final String VIDEO_STREAM_TYPE_VIDEO = "video";
  177. @Inject
  178. NcApi ncApi;
  179. @Inject
  180. EventBus eventBus;
  181. @Inject
  182. UserManager userManager;
  183. @Inject
  184. AppPreferences appPreferences;
  185. @Inject
  186. Cache cache;
  187. @Inject
  188. PlatformPermissionUtil permissionUtil;
  189. public static final String TAG = "CallActivity";
  190. public WebRtcAudioManager audioManager;
  191. private static final String[] PERMISSIONS_CALL = {
  192. Manifest.permission.CAMERA,
  193. Manifest.permission.RECORD_AUDIO
  194. };
  195. private static final String[] PERMISSIONS_CAMERA = {
  196. Manifest.permission.CAMERA
  197. };
  198. private static final String[] PERMISSIONS_MICROPHONE = {
  199. Manifest.permission.RECORD_AUDIO
  200. };
  201. private static final String MICROPHONE_PIP_INTENT_NAME = "microphone_pip_intent";
  202. private static final String MICROPHONE_PIP_INTENT_EXTRA_ACTION = "microphone_pip_action";
  203. private static final int MICROPHONE_PIP_REQUEST_MUTE = 1;
  204. private static final int MICROPHONE_PIP_REQUEST_UNMUTE = 2;
  205. private BroadcastReceiver mReceiver;
  206. private PeerConnectionFactory peerConnectionFactory;
  207. private MediaConstraints audioConstraints;
  208. private MediaConstraints videoConstraints;
  209. private MediaConstraints sdpConstraints;
  210. private MediaConstraints sdpConstraintsForMCU;
  211. private VideoSource videoSource;
  212. private VideoTrack localVideoTrack;
  213. private AudioSource audioSource;
  214. private AudioTrack localAudioTrack;
  215. private VideoCapturer videoCapturer;
  216. private EglBase rootEglBase;
  217. private Disposable signalingDisposable;
  218. private List<PeerConnection.IceServer> iceServers;
  219. private CameraEnumerator cameraEnumerator;
  220. private String roomToken;
  221. private User conversationUser;
  222. private String conversationName;
  223. private String callSession;
  224. private MediaStream localStream;
  225. private String credentials;
  226. private List<PeerConnectionWrapper> peerConnectionWrapperList = new ArrayList<>();
  227. private Map<String, Participant> participantMap = new HashMap<>();
  228. private boolean videoOn = false;
  229. private boolean microphoneOn = false;
  230. private boolean isVoiceOnlyCall;
  231. private boolean isCallWithoutNotification;
  232. private boolean isIncomingCallFromNotification;
  233. private Handler callControlHandler = new Handler();
  234. private Handler callInfosHandler = new Handler();
  235. private Handler cameraSwitchHandler = new Handler();
  236. // push to talk
  237. private boolean isPushToTalkActive = false;
  238. private PulseAnimation pulseAnimation;
  239. private String baseUrl;
  240. private String roomId;
  241. private SpotlightView spotlightView;
  242. private CallActivitySignalingMessageReceiver signalingMessageReceiver = new CallActivitySignalingMessageReceiver();
  243. private ExternalSignalingServer externalSignalingServer;
  244. private MagicWebSocketInstance webSocketClient;
  245. private WebSocketConnectionHelper webSocketConnectionHelper;
  246. private boolean hasMCU;
  247. private boolean hasExternalSignalingServer;
  248. private String conversationPassword;
  249. private PowerManagerUtils powerManagerUtils;
  250. private Handler handler;
  251. private CallStatus currentCallStatus;
  252. private MediaPlayer mediaPlayer;
  253. private Map<String, ParticipantDisplayItem> participantDisplayItems;
  254. private ParticipantsAdapter participantsAdapter;
  255. private CallActivityBinding binding;
  256. private AudioOutputDialog audioOutputDialog;
  257. private final ActivityResultLauncher<String> requestBluetoothPermissionLauncher =
  258. registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
  259. if (isGranted) {
  260. enableBluetoothManager();
  261. }
  262. });
  263. private boolean canPublishAudioStream;
  264. private boolean canPublishVideoStream;
  265. @SuppressLint("ClickableViewAccessibility")
  266. @Override
  267. public void onCreate(Bundle savedInstanceState) {
  268. Log.d(TAG, "onCreate");
  269. super.onCreate(savedInstanceState);
  270. NextcloudTalkApplication.Companion.getSharedApplication().getComponentApplication().inject(this);
  271. binding = CallActivityBinding.inflate(getLayoutInflater());
  272. setContentView(binding.getRoot());
  273. hideNavigationIfNoPipAvailable();
  274. Bundle extras = getIntent().getExtras();
  275. roomId = extras.getString(KEY_ROOM_ID, "");
  276. roomToken = extras.getString(KEY_ROOM_TOKEN, "");
  277. conversationUser = extras.getParcelable(KEY_USER_ENTITY);
  278. conversationPassword = extras.getString(KEY_CONVERSATION_PASSWORD, "");
  279. conversationName = extras.getString(KEY_CONVERSATION_NAME, "");
  280. isVoiceOnlyCall = extras.getBoolean(KEY_CALL_VOICE_ONLY, false);
  281. isCallWithoutNotification = extras.getBoolean(KEY_CALL_WITHOUT_NOTIFICATION, false);
  282. canPublishAudioStream = extras.getBoolean(KEY_PARTICIPANT_PERMISSION_CAN_PUBLISH_AUDIO);
  283. canPublishVideoStream = extras.getBoolean(KEY_PARTICIPANT_PERMISSION_CAN_PUBLISH_VIDEO);
  284. if (extras.containsKey(KEY_FROM_NOTIFICATION_START_CALL)) {
  285. isIncomingCallFromNotification = extras.getBoolean(KEY_FROM_NOTIFICATION_START_CALL);
  286. }
  287. credentials = ApiUtils.getCredentials(conversationUser.getUsername(), conversationUser.getToken());
  288. baseUrl = extras.getString(KEY_MODIFIED_BASE_URL, "");
  289. if (TextUtils.isEmpty(baseUrl)) {
  290. baseUrl = conversationUser.getBaseUrl();
  291. }
  292. powerManagerUtils = new PowerManagerUtils();
  293. if (extras.getString("state", "").equalsIgnoreCase("resume")) {
  294. setCallState(CallStatus.IN_CONVERSATION);
  295. } else {
  296. setCallState(CallStatus.CONNECTING);
  297. }
  298. initClickListeners();
  299. binding.microphoneButton.setOnTouchListener(new MicrophoneButtonTouchListener());
  300. pulseAnimation = PulseAnimation.create().with(binding.microphoneButton)
  301. .setDuration(310)
  302. .setRepeatCount(PulseAnimation.INFINITE)
  303. .setRepeatMode(PulseAnimation.REVERSE);
  304. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
  305. requestBluetoothPermission();
  306. }
  307. basicInitialization();
  308. participantDisplayItems = new HashMap<>();
  309. initViews();
  310. if (!isConnectionEstablished()) {
  311. initiateCall();
  312. }
  313. updateSelfVideoViewPosition();
  314. }
  315. @RequiresApi(api = Build.VERSION_CODES.S)
  316. private void requestBluetoothPermission() {
  317. if (ContextCompat.checkSelfPermission(
  318. getContext(), Manifest.permission.BLUETOOTH_CONNECT) ==
  319. PackageManager.PERMISSION_DENIED) {
  320. requestBluetoothPermissionLauncher.launch(Manifest.permission.BLUETOOTH_CONNECT);
  321. }
  322. }
  323. private void enableBluetoothManager() {
  324. if (audioManager != null) {
  325. audioManager.startBluetoothManager();
  326. }
  327. }
  328. @Override
  329. public void onStart() {
  330. super.onStart();
  331. try {
  332. cache.evictAll();
  333. } catch (IOException e) {
  334. Log.e(TAG, "Failed to evict cache");
  335. }
  336. }
  337. private void initClickListeners() {
  338. binding.pictureInPictureButton.setOnClickListener(l -> enterPipMode());
  339. binding.audioOutputButton.setOnClickListener(v -> {
  340. audioOutputDialog = new AudioOutputDialog(this);
  341. audioOutputDialog.show();
  342. });
  343. if (canPublishAudioStream) {
  344. binding.microphoneButton.setOnClickListener(l -> onMicrophoneClick());
  345. binding.microphoneButton.setOnLongClickListener(l -> {
  346. if (!microphoneOn) {
  347. callControlHandler.removeCallbacksAndMessages(null);
  348. callInfosHandler.removeCallbacksAndMessages(null);
  349. cameraSwitchHandler.removeCallbacksAndMessages(null);
  350. isPushToTalkActive = true;
  351. binding.callControls.setVisibility(View.VISIBLE);
  352. if (!isVoiceOnlyCall) {
  353. binding.switchSelfVideoButton.setVisibility(View.VISIBLE);
  354. }
  355. }
  356. onMicrophoneClick();
  357. return true;
  358. });
  359. } else {
  360. binding.microphoneButton.setOnClickListener(
  361. l -> Toast.makeText(context,
  362. R.string.nc_not_allowed_to_activate_audio,
  363. Toast.LENGTH_SHORT
  364. ).show()
  365. );
  366. }
  367. if (canPublishVideoStream) {
  368. binding.cameraButton.setOnClickListener(l -> onCameraClick());
  369. } else {
  370. binding.cameraButton.setOnClickListener(
  371. l -> Toast.makeText(context,
  372. R.string.nc_not_allowed_to_activate_video,
  373. Toast.LENGTH_SHORT
  374. ).show()
  375. );
  376. }
  377. binding.hangupButton.setOnClickListener(l -> {
  378. hangup(true);
  379. });
  380. binding.switchSelfVideoButton.setOnClickListener(l -> switchCamera());
  381. binding.gridview.setOnItemClickListener((parent, view, position, id) -> animateCallControls(true, 0));
  382. binding.callStates.callStateRelativeLayout.setOnClickListener(l -> {
  383. if (currentCallStatus == CallStatus.CALLING_TIMEOUT) {
  384. setCallState(CallStatus.RECONNECTING);
  385. hangupNetworkCalls(false);
  386. }
  387. });
  388. }
  389. private void createCameraEnumerator() {
  390. boolean camera2EnumeratorIsSupported = false;
  391. try {
  392. camera2EnumeratorIsSupported = Camera2Enumerator.isSupported(this);
  393. } catch (final Throwable t) {
  394. Log.w(TAG, "Camera2Enumerator threw an error", t);
  395. }
  396. if (camera2EnumeratorIsSupported) {
  397. cameraEnumerator = new Camera2Enumerator(this);
  398. } else {
  399. cameraEnumerator = new Camera1Enumerator(MagicWebRTCUtils.shouldEnableVideoHardwareAcceleration());
  400. }
  401. }
  402. private void basicInitialization() {
  403. rootEglBase = EglBase.create();
  404. createCameraEnumerator();
  405. //Create a new PeerConnectionFactory instance.
  406. PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();
  407. DefaultVideoEncoderFactory defaultVideoEncoderFactory = new DefaultVideoEncoderFactory(
  408. rootEglBase.getEglBaseContext(), true, true);
  409. DefaultVideoDecoderFactory defaultVideoDecoderFactory = new DefaultVideoDecoderFactory(
  410. rootEglBase.getEglBaseContext());
  411. peerConnectionFactory = PeerConnectionFactory.builder()
  412. .setOptions(options)
  413. .setVideoEncoderFactory(defaultVideoEncoderFactory)
  414. .setVideoDecoderFactory(defaultVideoDecoderFactory)
  415. .createPeerConnectionFactory();
  416. //Create MediaConstraints - Will be useful for specifying video and audio constraints.
  417. audioConstraints = new MediaConstraints();
  418. videoConstraints = new MediaConstraints();
  419. localStream = peerConnectionFactory.createLocalMediaStream("NCMS");
  420. // Create and audio manager that will take care of audio routing,
  421. // audio modes, audio device enumeration etc.
  422. audioManager = WebRtcAudioManager.create(getApplicationContext(), isVoiceOnlyCall);
  423. // Store existing audio settings and change audio mode to
  424. // MODE_IN_COMMUNICATION for best possible VoIP performance.
  425. Log.d(TAG, "Starting the audio manager...");
  426. audioManager.start(this::onAudioManagerDevicesChanged);
  427. if (isVoiceOnlyCall) {
  428. setAudioOutputChannel(WebRtcAudioManager.AudioDevice.EARPIECE);
  429. } else {
  430. setAudioOutputChannel(WebRtcAudioManager.AudioDevice.SPEAKER_PHONE);
  431. }
  432. iceServers = new ArrayList<>();
  433. //create sdpConstraints
  434. sdpConstraints = new MediaConstraints();
  435. sdpConstraintsForMCU = new MediaConstraints();
  436. sdpConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveAudio", "true"));
  437. String offerToReceiveVideoString = "true";
  438. if (isVoiceOnlyCall) {
  439. offerToReceiveVideoString = "false";
  440. }
  441. sdpConstraints.mandatory.add(
  442. new MediaConstraints.KeyValuePair("OfferToReceiveVideo", offerToReceiveVideoString));
  443. sdpConstraintsForMCU.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveAudio", "false"));
  444. sdpConstraintsForMCU.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveVideo", "false"));
  445. sdpConstraintsForMCU.optional.add(new MediaConstraints.KeyValuePair("internalSctpDataChannels", "true"));
  446. sdpConstraintsForMCU.optional.add(new MediaConstraints.KeyValuePair("DtlsSrtpKeyAgreement", "true"));
  447. sdpConstraints.optional.add(new MediaConstraints.KeyValuePair("internalSctpDataChannels", "true"));
  448. sdpConstraints.optional.add(new MediaConstraints.KeyValuePair("DtlsSrtpKeyAgreement", "true"));
  449. if (!isVoiceOnlyCall) {
  450. cameraInitialization();
  451. }
  452. microphoneInitialization();
  453. }
  454. public void setAudioOutputChannel(WebRtcAudioManager.AudioDevice selectedAudioDevice) {
  455. if (audioManager != null) {
  456. audioManager.selectAudioDevice(selectedAudioDevice);
  457. updateAudioOutputButton(audioManager.getCurrentAudioDevice());
  458. }
  459. }
  460. private void updateAudioOutputButton(WebRtcAudioManager.AudioDevice activeAudioDevice) {
  461. switch (activeAudioDevice) {
  462. case BLUETOOTH:
  463. binding.audioOutputButton.setImageResource ( R.drawable.ic_baseline_bluetooth_audio_24);
  464. break;
  465. case SPEAKER_PHONE:
  466. binding.audioOutputButton.setImageResource(R.drawable.ic_volume_up_white_24dp);
  467. break;
  468. case EARPIECE:
  469. binding.audioOutputButton.setImageResource(R.drawable.ic_baseline_phone_in_talk_24);
  470. break;
  471. case WIRED_HEADSET:
  472. binding.audioOutputButton.setImageResource(R.drawable.ic_baseline_headset_mic_24);
  473. break;
  474. default:
  475. Log.e(TAG, "Icon for audio output not available");
  476. break;
  477. }
  478. DrawableCompat.setTint(binding.audioOutputButton.getDrawable(), Color.WHITE);
  479. }
  480. private void handleFromNotification() {
  481. int apiVersion = ApiUtils.getConversationApiVersion(conversationUser, new int[]{ApiUtils.APIv4, 1});
  482. ncApi.getRooms(credentials, ApiUtils.getUrlForRooms(apiVersion, baseUrl), false)
  483. .retry(3)
  484. .subscribeOn(Schedulers.io())
  485. .observeOn(AndroidSchedulers.mainThread())
  486. .subscribe(new Observer<RoomsOverall>() {
  487. @Override
  488. public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
  489. // unused atm
  490. }
  491. @Override
  492. public void onNext(@io.reactivex.annotations.NonNull RoomsOverall roomsOverall) {
  493. for (Conversation conversation : roomsOverall.getOcs().getData()) {
  494. if (roomId.equals(conversation.getRoomId())) {
  495. roomToken = conversation.getToken();
  496. break;
  497. }
  498. }
  499. checkDevicePermissions();
  500. }
  501. @Override
  502. public void onError(@io.reactivex.annotations.NonNull Throwable e) {
  503. // unused atm
  504. }
  505. @Override
  506. public void onComplete() {
  507. // unused atm
  508. }
  509. });
  510. }
  511. @SuppressLint("ClickableViewAccessibility")
  512. private void initViews() {
  513. Log.d(TAG, "initViews");
  514. binding.callInfosLinearLayout.setVisibility(View.VISIBLE);
  515. binding.selfVideoViewWrapper.setVisibility(View.VISIBLE);
  516. if (!isPipModePossible()) {
  517. binding.pictureInPictureButton.setVisibility(View.GONE);
  518. }
  519. if (isVoiceOnlyCall) {
  520. binding.switchSelfVideoButton.setVisibility(View.GONE);
  521. binding.cameraButton.setVisibility(View.GONE);
  522. binding.selfVideoRenderer.setVisibility(View.GONE);
  523. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
  524. ViewGroup.LayoutParams.WRAP_CONTENT);
  525. params.addRule(RelativeLayout.BELOW, R.id.callInfosLinearLayout);
  526. int callControlsHeight = Math.round(getApplicationContext().getResources().getDimension(R.dimen.call_controls_height));
  527. params.setMargins(0, 0, 0, callControlsHeight);
  528. binding.gridview.setLayoutParams(params);
  529. } else {
  530. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
  531. ViewGroup.LayoutParams.WRAP_CONTENT);
  532. params.setMargins(0, 0, 0, 0);
  533. binding.gridview.setLayoutParams(params);
  534. if (cameraEnumerator.getDeviceNames().length < 2) {
  535. binding.switchSelfVideoButton.setVisibility(View.GONE);
  536. }
  537. initSelfVideoView();
  538. }
  539. binding.gridview.setOnTouchListener(new View.OnTouchListener() {
  540. public boolean onTouch(View v, MotionEvent me) {
  541. int action = me.getActionMasked();
  542. if (action == MotionEvent.ACTION_DOWN) {
  543. animateCallControls(true, 0);
  544. }
  545. return false;
  546. }
  547. });
  548. binding.conversationRelativeLayout.setOnTouchListener(new View.OnTouchListener() {
  549. public boolean onTouch(View v, MotionEvent me) {
  550. int action = me.getActionMasked();
  551. if (action == MotionEvent.ACTION_DOWN) {
  552. animateCallControls(true, 0);
  553. }
  554. return false;
  555. }
  556. });
  557. animateCallControls(true, 0);
  558. initGridAdapter();
  559. }
  560. @SuppressLint("ClickableViewAccessibility")
  561. private void initSelfVideoView() {
  562. try {
  563. binding.selfVideoRenderer.init(rootEglBase.getEglBaseContext(), null);
  564. } catch (IllegalStateException e) {
  565. Log.d(TAG, "selfVideoRenderer already initialized", e);
  566. }
  567. binding.selfVideoRenderer.setZOrderMediaOverlay(true);
  568. // disabled because it causes some devices to crash
  569. binding.selfVideoRenderer.setEnableHardwareScaler(false);
  570. binding.selfVideoRenderer.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
  571. binding.selfVideoRenderer.setOnTouchListener(new SelfVideoTouchListener());
  572. }
  573. private void initGridAdapter() {
  574. Log.d(TAG, "initGridAdapter");
  575. int columns;
  576. int participantsInGrid = participantDisplayItems.size();
  577. if (getResources() != null
  578. && getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
  579. if (participantsInGrid > 2) {
  580. columns = 2;
  581. } else {
  582. columns = 1;
  583. }
  584. } else {
  585. if (participantsInGrid > 2) {
  586. columns = 3;
  587. } else if (participantsInGrid > 1) {
  588. columns = 2;
  589. } else {
  590. columns = 1;
  591. }
  592. }
  593. binding.gridview.setNumColumns(columns);
  594. binding.conversationRelativeLayout
  595. .getViewTreeObserver()
  596. .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  597. @Override
  598. public void onGlobalLayout() {
  599. binding.conversationRelativeLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
  600. int height = binding.conversationRelativeLayout.getMeasuredHeight();
  601. binding.gridview.setMinimumHeight(height);
  602. }
  603. });
  604. binding
  605. .callInfosLinearLayout
  606. .getViewTreeObserver()
  607. .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  608. @Override
  609. public void onGlobalLayout() {
  610. binding.callInfosLinearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
  611. }
  612. });
  613. participantsAdapter = new ParticipantsAdapter(
  614. this,
  615. participantDisplayItems,
  616. binding.conversationRelativeLayout,
  617. binding.callInfosLinearLayout,
  618. columns,
  619. isVoiceOnlyCall);
  620. binding.gridview.setAdapter(participantsAdapter);
  621. if (isInPipMode) {
  622. updateUiForPipMode();
  623. }
  624. }
  625. private void checkDevicePermissions() {
  626. if (isVoiceOnlyCall) {
  627. onMicrophoneClick();
  628. } else {
  629. if (EffortlessPermissions.hasPermissions(this, PERMISSIONS_CALL)) {
  630. onPermissionsGranted();
  631. } else {
  632. requestPermissions(PERMISSIONS_CALL, 100);
  633. }
  634. }
  635. }
  636. private boolean isConnectionEstablished() {
  637. return (currentCallStatus == CallStatus.JOINED || currentCallStatus == CallStatus.IN_CONVERSATION);
  638. }
  639. @AfterPermissionGranted(100)
  640. private void onPermissionsGranted() {
  641. if (EffortlessPermissions.hasPermissions(this, PERMISSIONS_CALL)) {
  642. if (!videoOn && !isVoiceOnlyCall) {
  643. onCameraClick();
  644. }
  645. if (!microphoneOn) {
  646. onMicrophoneClick();
  647. }
  648. if (!isVoiceOnlyCall) {
  649. if (cameraEnumerator.getDeviceNames().length == 0) {
  650. binding.cameraButton.setVisibility(View.GONE);
  651. }
  652. if (cameraEnumerator.getDeviceNames().length > 1) {
  653. binding.switchSelfVideoButton.setVisibility(View.VISIBLE);
  654. }
  655. }
  656. if (!isConnectionEstablished()) {
  657. fetchSignalingSettings();
  658. }
  659. } else if (EffortlessPermissions.somePermissionPermanentlyDenied(this, PERMISSIONS_CALL)) {
  660. checkIfSomeAreApproved();
  661. }
  662. }
  663. private void checkIfSomeAreApproved() {
  664. if (!isVoiceOnlyCall) {
  665. if (cameraEnumerator.getDeviceNames().length == 0) {
  666. binding.cameraButton.setVisibility(View.GONE);
  667. }
  668. if (cameraEnumerator.getDeviceNames().length > 1) {
  669. binding.switchSelfVideoButton.setVisibility(View.VISIBLE);
  670. }
  671. if (EffortlessPermissions.hasPermissions(this, PERMISSIONS_CAMERA) && canPublishVideoStream) {
  672. if (!videoOn) {
  673. onCameraClick();
  674. }
  675. } else {
  676. binding.cameraButton.setImageResource(R.drawable.ic_videocam_off_white_24px);
  677. binding.cameraButton.setAlpha(0.7f);
  678. binding.switchSelfVideoButton.setVisibility(View.GONE);
  679. }
  680. }
  681. if (EffortlessPermissions.hasPermissions(this, PERMISSIONS_MICROPHONE) && canPublishAudioStream) {
  682. if (!microphoneOn) {
  683. onMicrophoneClick();
  684. }
  685. } else {
  686. binding.microphoneButton.setImageResource(R.drawable.ic_mic_off_white_24px);
  687. }
  688. if (!isConnectionEstablished()) {
  689. fetchSignalingSettings();
  690. }
  691. }
  692. @AfterPermissionDenied(100)
  693. private void onPermissionsDenied() {
  694. if (!isVoiceOnlyCall) {
  695. if (cameraEnumerator.getDeviceNames().length == 0) {
  696. binding.cameraButton.setVisibility(View.GONE);
  697. } else if (cameraEnumerator.getDeviceNames().length == 1) {
  698. binding.switchSelfVideoButton.setVisibility(View.GONE);
  699. }
  700. }
  701. if ((EffortlessPermissions.hasPermissions(this, PERMISSIONS_CAMERA) ||
  702. EffortlessPermissions.hasPermissions(this, PERMISSIONS_MICROPHONE))) {
  703. checkIfSomeAreApproved();
  704. } else if (!isConnectionEstablished()) {
  705. fetchSignalingSettings();
  706. }
  707. }
  708. private void onAudioManagerDevicesChanged(
  709. final WebRtcAudioManager.AudioDevice currentDevice,
  710. final Set<WebRtcAudioManager.AudioDevice> availableDevices) {
  711. Log.d(TAG, "onAudioManagerDevicesChanged: " + availableDevices + ", "
  712. + "currentDevice: " + currentDevice);
  713. final boolean shouldDisableProximityLock = (currentDevice == WebRtcAudioManager.AudioDevice.WIRED_HEADSET
  714. || currentDevice == WebRtcAudioManager.AudioDevice.SPEAKER_PHONE
  715. || currentDevice == WebRtcAudioManager.AudioDevice.BLUETOOTH);
  716. if (shouldDisableProximityLock) {
  717. powerManagerUtils.updatePhoneState(PowerManagerUtils.PhoneState.WITHOUT_PROXIMITY_SENSOR_LOCK);
  718. } else {
  719. powerManagerUtils.updatePhoneState(PowerManagerUtils.PhoneState.WITH_PROXIMITY_SENSOR_LOCK);
  720. }
  721. if (audioOutputDialog != null) {
  722. audioOutputDialog.updateOutputDeviceList();
  723. }
  724. updateAudioOutputButton(currentDevice);
  725. }
  726. private void cameraInitialization() {
  727. videoCapturer = createCameraCapturer(cameraEnumerator);
  728. //Create a VideoSource instance
  729. if (videoCapturer != null) {
  730. SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread",
  731. rootEglBase.getEglBaseContext());
  732. videoSource = peerConnectionFactory.createVideoSource(false);
  733. videoCapturer.initialize(surfaceTextureHelper, getApplicationContext(), videoSource.getCapturerObserver());
  734. }
  735. localVideoTrack = peerConnectionFactory.createVideoTrack("NCv0", videoSource);
  736. localStream.addTrack(localVideoTrack);
  737. localVideoTrack.setEnabled(false);
  738. localVideoTrack.addSink(binding.selfVideoRenderer);
  739. }
  740. private void microphoneInitialization() {
  741. //create an AudioSource instance
  742. audioSource = peerConnectionFactory.createAudioSource(audioConstraints);
  743. localAudioTrack = peerConnectionFactory.createAudioTrack("NCa0", audioSource);
  744. localAudioTrack.setEnabled(false);
  745. localStream.addTrack(localAudioTrack);
  746. }
  747. private VideoCapturer createCameraCapturer(CameraEnumerator enumerator) {
  748. final String[] deviceNames = enumerator.getDeviceNames();
  749. // First, try to find front facing camera
  750. Logging.d(TAG, "Looking for front facing cameras.");
  751. for (String deviceName : deviceNames) {
  752. if (enumerator.isFrontFacing(deviceName)) {
  753. Logging.d(TAG, "Creating front facing camera capturer.");
  754. VideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
  755. if (videoCapturer != null) {
  756. binding.selfVideoRenderer.setMirror(true);
  757. return videoCapturer;
  758. }
  759. }
  760. }
  761. // Front facing camera not found, try something else
  762. Logging.d(TAG, "Looking for other cameras.");
  763. for (String deviceName : deviceNames) {
  764. if (!enumerator.isFrontFacing(deviceName)) {
  765. Logging.d(TAG, "Creating other camera capturer.");
  766. VideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
  767. if (videoCapturer != null) {
  768. binding.selfVideoRenderer.setMirror(false);
  769. return videoCapturer;
  770. }
  771. }
  772. }
  773. return null;
  774. }
  775. public void onMicrophoneClick() {
  776. if (!canPublishAudioStream) {
  777. microphoneOn = false;
  778. binding.microphoneButton.setImageResource(R.drawable.ic_mic_off_white_24px);
  779. toggleMedia(false, false);
  780. }
  781. if (isVoiceOnlyCall && !isConnectionEstablished()) {
  782. fetchSignalingSettings();
  783. }
  784. if (!canPublishAudioStream) {
  785. // In the case no audio stream will be published it's not needed to check microphone permissions
  786. return;
  787. }
  788. if (EffortlessPermissions.hasPermissions(this, PERMISSIONS_MICROPHONE)) {
  789. if (!appPreferences.getPushToTalkIntroShown()) {
  790. int primary = viewThemeUtils.getScheme(binding.audioOutputButton.getContext()).getPrimary();
  791. spotlightView = new SpotlightView.Builder(this)
  792. .introAnimationDuration(300)
  793. .enableRevealAnimation(true)
  794. .performClick(false)
  795. .fadeinTextDuration(400)
  796. .headingTvColor(primary)
  797. .headingTvSize(20)
  798. .headingTvText(getResources().getString(R.string.nc_push_to_talk))
  799. .subHeadingTvColor(getResources().getColor(R.color.bg_default))
  800. .subHeadingTvSize(16)
  801. .subHeadingTvText(getResources().getString(R.string.nc_push_to_talk_desc))
  802. .maskColor(Color.parseColor("#dc000000"))
  803. .target(binding.microphoneButton)
  804. .lineAnimDuration(400)
  805. .lineAndArcColor(primary)
  806. .enableDismissAfterShown(true)
  807. .dismissOnBackPress(true)
  808. .usageId("pushToTalk")
  809. .show();
  810. appPreferences.setPushToTalkIntroShown(true);
  811. }
  812. if (!isPushToTalkActive) {
  813. microphoneOn = !microphoneOn;
  814. if (microphoneOn) {
  815. binding.microphoneButton.setImageResource(R.drawable.ic_mic_white_24px);
  816. updatePictureInPictureActions(R.drawable.ic_mic_white_24px,
  817. getResources().getString(R.string.nc_pip_microphone_mute),
  818. MICROPHONE_PIP_REQUEST_MUTE);
  819. } else {
  820. binding.microphoneButton.setImageResource(R.drawable.ic_mic_off_white_24px);
  821. updatePictureInPictureActions(R.drawable.ic_mic_off_white_24px,
  822. getResources().getString(R.string.nc_pip_microphone_unmute),
  823. MICROPHONE_PIP_REQUEST_UNMUTE);
  824. }
  825. toggleMedia(microphoneOn, false);
  826. } else {
  827. binding.microphoneButton.setImageResource(R.drawable.ic_mic_white_24px);
  828. pulseAnimation.start();
  829. toggleMedia(true, false);
  830. }
  831. } else if (EffortlessPermissions.somePermissionPermanentlyDenied(this, PERMISSIONS_MICROPHONE)) {
  832. // Microphone permission is permanently denied so we cannot request it normally.
  833. OpenAppDetailsDialogFragment.show(
  834. R.string.nc_microphone_permission_permanently_denied,
  835. R.string.nc_permissions_settings, (AppCompatActivity) this);
  836. } else {
  837. requestPermissions(PERMISSIONS_MICROPHONE, 100);
  838. }
  839. }
  840. public void onCameraClick() {
  841. if (!canPublishVideoStream) {
  842. videoOn = false;
  843. binding.cameraButton.setImageResource(R.drawable.ic_videocam_off_white_24px);
  844. binding.switchSelfVideoButton.setVisibility(View.GONE);
  845. return;
  846. }
  847. if (EffortlessPermissions.hasPermissions(this, PERMISSIONS_CAMERA)) {
  848. videoOn = !videoOn;
  849. if (videoOn) {
  850. binding.cameraButton.setImageResource(R.drawable.ic_videocam_white_24px);
  851. if (cameraEnumerator.getDeviceNames().length > 1) {
  852. binding.switchSelfVideoButton.setVisibility(View.VISIBLE);
  853. }
  854. } else {
  855. binding.cameraButton.setImageResource(R.drawable.ic_videocam_off_white_24px);
  856. binding.switchSelfVideoButton.setVisibility(View.GONE);
  857. }
  858. toggleMedia(videoOn, true);
  859. } else if (EffortlessPermissions.somePermissionPermanentlyDenied(this, PERMISSIONS_CAMERA)) {
  860. // Camera permission is permanently denied so we cannot request it normally.
  861. OpenAppDetailsDialogFragment.show(
  862. R.string.nc_camera_permission_permanently_denied,
  863. R.string.nc_permissions_settings, (AppCompatActivity) this);
  864. } else {
  865. requestPermissions(PERMISSIONS_CAMERA, 100);
  866. }
  867. }
  868. public void switchCamera() {
  869. CameraVideoCapturer cameraVideoCapturer = (CameraVideoCapturer) videoCapturer;
  870. if (cameraVideoCapturer != null) {
  871. cameraVideoCapturer.switchCamera(new CameraVideoCapturer.CameraSwitchHandler() {
  872. @Override
  873. public void onCameraSwitchDone(boolean currentCameraIsFront) {
  874. binding.selfVideoRenderer.setMirror(currentCameraIsFront);
  875. }
  876. @Override
  877. public void onCameraSwitchError(String s) {
  878. }
  879. });
  880. }
  881. }
  882. private void toggleMedia(boolean enable, boolean video) {
  883. String message;
  884. if (video) {
  885. message = "videoOff";
  886. if (enable) {
  887. binding.cameraButton.setAlpha(1.0f);
  888. message = "videoOn";
  889. startVideoCapture();
  890. } else {
  891. binding.cameraButton.setAlpha(0.7f);
  892. if (videoCapturer != null) {
  893. try {
  894. videoCapturer.stopCapture();
  895. } catch (InterruptedException e) {
  896. Log.d(TAG, "Failed to stop capturing video while sensor is near the ear");
  897. }
  898. }
  899. }
  900. if (localStream != null && localStream.videoTracks.size() > 0) {
  901. localStream.videoTracks.get(0).setEnabled(enable);
  902. }
  903. if (enable) {
  904. binding.selfVideoRenderer.setVisibility(View.VISIBLE);
  905. } else {
  906. binding.selfVideoRenderer.setVisibility(View.INVISIBLE);
  907. }
  908. } else {
  909. message = "audioOff";
  910. if (enable) {
  911. message = "audioOn";
  912. binding.microphoneButton.setAlpha(1.0f);
  913. } else {
  914. binding.microphoneButton.setAlpha(0.7f);
  915. }
  916. if (localStream != null && localStream.audioTracks.size() > 0) {
  917. localStream.audioTracks.get(0).setEnabled(enable);
  918. }
  919. }
  920. if (isConnectionEstablished() && peerConnectionWrapperList != null) {
  921. if (!hasMCU) {
  922. for (PeerConnectionWrapper peerConnectionWrapper : peerConnectionWrapperList) {
  923. peerConnectionWrapper.sendChannelData(new DataChannelMessage(message));
  924. }
  925. } else {
  926. for (PeerConnectionWrapper peerConnectionWrapper : peerConnectionWrapperList) {
  927. if (peerConnectionWrapper.getSessionId().equals(webSocketClient.getSessionId())) {
  928. peerConnectionWrapper.sendChannelData(new DataChannelMessage(message));
  929. break;
  930. }
  931. }
  932. }
  933. }
  934. }
  935. private void animateCallControls(boolean show, long startDelay) {
  936. if (isVoiceOnlyCall) {
  937. if (spotlightView != null && spotlightView.getVisibility() != View.GONE) {
  938. spotlightView.setVisibility(View.GONE);
  939. }
  940. } else if (!isPushToTalkActive) {
  941. float alpha;
  942. long duration;
  943. if (show) {
  944. callControlHandler.removeCallbacksAndMessages(null);
  945. callInfosHandler.removeCallbacksAndMessages(null);
  946. cameraSwitchHandler.removeCallbacksAndMessages(null);
  947. alpha = 1.0f;
  948. duration = 1000;
  949. if (binding.callControls.getVisibility() != View.VISIBLE) {
  950. binding.callControls.setAlpha(0.0f);
  951. binding.callControls.setVisibility(View.VISIBLE);
  952. binding.callInfosLinearLayout.setAlpha(0.0f);
  953. binding.callInfosLinearLayout.setVisibility(View.VISIBLE);
  954. binding.switchSelfVideoButton.setAlpha(0.0f);
  955. if (videoOn) {
  956. binding.switchSelfVideoButton.setVisibility(View.VISIBLE);
  957. }
  958. } else {
  959. callControlHandler.postDelayed(() -> animateCallControls(false, 0), 5000);
  960. return;
  961. }
  962. } else {
  963. alpha = 0.0f;
  964. duration = 1000;
  965. }
  966. binding.callControls.setEnabled(false);
  967. binding.callControls.animate()
  968. .translationY(0)
  969. .alpha(alpha)
  970. .setDuration(duration)
  971. .setStartDelay(startDelay)
  972. .setListener(new AnimatorListenerAdapter() {
  973. @Override
  974. public void onAnimationEnd(Animator animation) {
  975. super.onAnimationEnd(animation);
  976. if (!show) {
  977. binding.callControls.setVisibility(View.GONE);
  978. if (spotlightView != null && spotlightView.getVisibility() != View.GONE) {
  979. spotlightView.setVisibility(View.GONE);
  980. }
  981. } else {
  982. callControlHandler.postDelayed(new Runnable() {
  983. @Override
  984. public void run() {
  985. if (!isPushToTalkActive) {
  986. animateCallControls(false, 0);
  987. }
  988. }
  989. }, 7500);
  990. }
  991. binding.callControls.setEnabled(true);
  992. }
  993. });
  994. binding.callInfosLinearLayout.setEnabled(false);
  995. binding.callInfosLinearLayout.animate()
  996. .translationY(0)
  997. .alpha(alpha)
  998. .setDuration(duration)
  999. .setStartDelay(startDelay)
  1000. .setListener(new AnimatorListenerAdapter() {
  1001. @Override
  1002. public void onAnimationEnd(Animator animation) {
  1003. super.onAnimationEnd(animation);
  1004. if (!show) {
  1005. binding.callInfosLinearLayout.setVisibility(View.GONE);
  1006. } else {
  1007. callInfosHandler.postDelayed(new Runnable() {
  1008. @Override
  1009. public void run() {
  1010. if (!isPushToTalkActive) {
  1011. animateCallControls(false, 0);
  1012. }
  1013. }
  1014. }, 7500);
  1015. }
  1016. binding.callInfosLinearLayout.setEnabled(true);
  1017. }
  1018. });
  1019. binding.switchSelfVideoButton.setEnabled(false);
  1020. binding.switchSelfVideoButton.animate()
  1021. .translationY(0)
  1022. .alpha(alpha)
  1023. .setDuration(duration)
  1024. .setStartDelay(startDelay)
  1025. .setListener(new AnimatorListenerAdapter() {
  1026. @Override
  1027. public void onAnimationEnd(Animator animation) {
  1028. super.onAnimationEnd(animation);
  1029. if (!show) {
  1030. binding.switchSelfVideoButton.setVisibility(View.GONE);
  1031. }
  1032. binding.switchSelfVideoButton.setEnabled(true);
  1033. }
  1034. });
  1035. }
  1036. }
  1037. @Override
  1038. public void onDestroy() {
  1039. if (localStream != null) {
  1040. localStream.dispose();
  1041. localStream = null;
  1042. Log.d(TAG, "Disposed localStream");
  1043. } else {
  1044. Log.d(TAG, "localStream is null");
  1045. }
  1046. if (currentCallStatus != CallStatus.LEAVING) {
  1047. hangup(true);
  1048. }
  1049. powerManagerUtils.updatePhoneState(PowerManagerUtils.PhoneState.IDLE);
  1050. super.onDestroy();
  1051. }
  1052. private void fetchSignalingSettings() {
  1053. Log.d(TAG, "fetchSignalingSettings");
  1054. int apiVersion = ApiUtils.getSignalingApiVersion(conversationUser, new int[]{ApiUtils.APIv3, 2, 1});
  1055. ncApi.getSignalingSettings(credentials, ApiUtils.getUrlForSignalingSettings(apiVersion, baseUrl))
  1056. .subscribeOn(Schedulers.io())
  1057. .retry(3)
  1058. .observeOn(AndroidSchedulers.mainThread())
  1059. .subscribe(new Observer<SignalingSettingsOverall>() {
  1060. @Override
  1061. public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
  1062. // unused atm
  1063. }
  1064. @Override
  1065. public void onNext(@io.reactivex.annotations.NonNull SignalingSettingsOverall signalingSettingsOverall) {
  1066. if (signalingSettingsOverall.getOcs() != null
  1067. && signalingSettingsOverall.getOcs().getSettings() != null) {
  1068. externalSignalingServer = new ExternalSignalingServer();
  1069. if (!TextUtils.isEmpty(
  1070. signalingSettingsOverall.getOcs().getSettings().getExternalSignalingServer()) &&
  1071. !TextUtils.isEmpty(
  1072. signalingSettingsOverall.getOcs().getSettings().getExternalSignalingTicket())) {
  1073. externalSignalingServer = new ExternalSignalingServer();
  1074. externalSignalingServer.setExternalSignalingServer(
  1075. signalingSettingsOverall.getOcs().getSettings().getExternalSignalingServer());
  1076. externalSignalingServer.setExternalSignalingTicket(
  1077. signalingSettingsOverall.getOcs().getSettings().getExternalSignalingTicket());
  1078. hasExternalSignalingServer = true;
  1079. } else {
  1080. hasExternalSignalingServer = false;
  1081. }
  1082. Log.d(TAG, " hasExternalSignalingServer: " + hasExternalSignalingServer);
  1083. if (!"?".equals(conversationUser.getUserId()) && conversationUser.getId() != null) {
  1084. Log.d(TAG, "Update externalSignalingServer for: " + conversationUser.getId() +
  1085. " / " + conversationUser.getUserId());
  1086. userManager.updateExternalSignalingServer(conversationUser.getId(), externalSignalingServer)
  1087. .subscribeOn(Schedulers.io())
  1088. .subscribe();
  1089. } else {
  1090. conversationUser.setExternalSignalingServer(externalSignalingServer);
  1091. }
  1092. if (signalingSettingsOverall.getOcs().getSettings().getStunServers() != null) {
  1093. List<IceServer> stunServers =
  1094. signalingSettingsOverall.getOcs().getSettings().getStunServers();
  1095. if (apiVersion == ApiUtils.APIv3) {
  1096. for (IceServer stunServer : stunServers) {
  1097. if (stunServer.getUrls() != null) {
  1098. for (String url : stunServer.getUrls()) {
  1099. Log.d(TAG, " STUN server url: " + url);
  1100. iceServers.add(new PeerConnection.IceServer(url));
  1101. }
  1102. }
  1103. }
  1104. } else {
  1105. if (signalingSettingsOverall.getOcs().getSettings().getStunServers() != null) {
  1106. for (IceServer stunServer : stunServers) {
  1107. Log.d(TAG, " STUN server url: " + stunServer.getUrl());
  1108. iceServers.add(new PeerConnection.IceServer(stunServer.getUrl()));
  1109. }
  1110. }
  1111. }
  1112. }
  1113. if (signalingSettingsOverall.getOcs().getSettings().getTurnServers() != null) {
  1114. List<IceServer> turnServers =
  1115. signalingSettingsOverall.getOcs().getSettings().getTurnServers();
  1116. for (IceServer turnServer : turnServers) {
  1117. if (turnServer.getUrls() != null) {
  1118. for (String url : turnServer.getUrls()) {
  1119. Log.d(TAG, " TURN server url: " + url);
  1120. iceServers.add(new PeerConnection.IceServer(
  1121. url, turnServer.getUsername(), turnServer.getCredential()
  1122. ));
  1123. }
  1124. }
  1125. }
  1126. }
  1127. }
  1128. checkCapabilities();
  1129. }
  1130. @Override
  1131. public void onError(@io.reactivex.annotations.NonNull Throwable e) {
  1132. Log.e(TAG, e.getMessage(), e);
  1133. }
  1134. @Override
  1135. public void onComplete() {
  1136. // unused atm
  1137. }
  1138. });
  1139. }
  1140. private void checkCapabilities() {
  1141. ncApi.getCapabilities(credentials, ApiUtils.getUrlForCapabilities(baseUrl))
  1142. .retry(3)
  1143. .subscribeOn(Schedulers.io())
  1144. .observeOn(AndroidSchedulers.mainThread())
  1145. .subscribe(new Observer<CapabilitiesOverall>() {
  1146. @Override
  1147. public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
  1148. // unused atm
  1149. }
  1150. @Override
  1151. public void onNext(@io.reactivex.annotations.NonNull CapabilitiesOverall capabilitiesOverall) {
  1152. // FIXME check for compatible Call API version
  1153. if (hasExternalSignalingServer) {
  1154. setupAndInitiateWebSocketsConnection();
  1155. } else {
  1156. joinRoomAndCall();
  1157. }
  1158. }
  1159. @Override
  1160. public void onError(@io.reactivex.annotations.NonNull Throwable e) {
  1161. // unused atm
  1162. }
  1163. @Override
  1164. public void onComplete() {
  1165. // unused atm
  1166. }
  1167. });
  1168. }
  1169. private void joinRoomAndCall() {
  1170. callSession = ApplicationWideCurrentRoomHolder.getInstance().getSession();
  1171. int apiVersion = ApiUtils.getConversationApiVersion(conversationUser, new int[]{ApiUtils.APIv4, 1});
  1172. Log.d(TAG, "joinRoomAndCall");
  1173. Log.d(TAG, " baseUrl= " + baseUrl);
  1174. Log.d(TAG, " roomToken= " + roomToken);
  1175. Log.d(TAG, " callSession= " + callSession);
  1176. String url = ApiUtils.getUrlForParticipantsActive(apiVersion, baseUrl, roomToken);
  1177. Log.d(TAG, " url= " + url);
  1178. if (TextUtils.isEmpty(callSession)) {
  1179. ncApi.joinRoom(credentials, url, conversationPassword)
  1180. .subscribeOn(Schedulers.io())
  1181. .observeOn(AndroidSchedulers.mainThread())
  1182. .retry(3)
  1183. .subscribe(new Observer<RoomOverall>() {
  1184. @Override
  1185. public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
  1186. // unused atm
  1187. }
  1188. @Override
  1189. public void onNext(@io.reactivex.annotations.NonNull RoomOverall roomOverall) {
  1190. callSession = roomOverall.getOcs().getData().getSessionId();
  1191. Log.d(TAG, " new callSession by joinRoom= " + callSession);
  1192. ApplicationWideCurrentRoomHolder.getInstance().setSession(callSession);
  1193. ApplicationWideCurrentRoomHolder.getInstance().setCurrentRoomId(roomId);
  1194. ApplicationWideCurrentRoomHolder.getInstance().setCurrentRoomToken(roomToken);
  1195. ApplicationWideCurrentRoomHolder.getInstance().setUserInRoom(conversationUser);
  1196. callOrJoinRoomViaWebSocket();
  1197. }
  1198. @Override
  1199. public void onError(@io.reactivex.annotations.NonNull Throwable e) {
  1200. Log.e(TAG, "joinRoom onError", e);
  1201. }
  1202. @Override
  1203. public void onComplete() {
  1204. Log.d(TAG, "joinRoom onComplete");
  1205. }
  1206. });
  1207. } else {
  1208. // we are in a room and start a call -> same session needs to be used
  1209. callOrJoinRoomViaWebSocket();
  1210. }
  1211. }
  1212. private void callOrJoinRoomViaWebSocket() {
  1213. if (hasExternalSignalingServer) {
  1214. webSocketClient.joinRoomWithRoomTokenAndSession(roomToken, callSession);
  1215. } else {
  1216. performCall();
  1217. }
  1218. }
  1219. private void performCall() {
  1220. int inCallFlag = Participant.InCallFlags.IN_CALL;
  1221. if (canPublishAudioStream) {
  1222. inCallFlag += Participant.InCallFlags.WITH_AUDIO;
  1223. }
  1224. if (!isVoiceOnlyCall && canPublishVideoStream) {
  1225. inCallFlag += Participant.InCallFlags.WITH_VIDEO;
  1226. }
  1227. int apiVersion = ApiUtils.getCallApiVersion(conversationUser, new int[]{ApiUtils.APIv4, 1});
  1228. ncApi.joinCall(
  1229. credentials,
  1230. ApiUtils.getUrlForCall(apiVersion, baseUrl, roomToken),
  1231. inCallFlag,
  1232. isCallWithoutNotification)
  1233. .subscribeOn(Schedulers.io())
  1234. .retry(3)
  1235. .observeOn(AndroidSchedulers.mainThread())
  1236. .subscribe(new Observer<GenericOverall>() {
  1237. @Override
  1238. public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
  1239. // unused atm
  1240. }
  1241. @Override
  1242. public void onNext(@io.reactivex.annotations.NonNull GenericOverall genericOverall) {
  1243. if (currentCallStatus != CallStatus.LEAVING) {
  1244. if (currentCallStatus != CallStatus.IN_CONVERSATION) {
  1245. setCallState(CallStatus.JOINED);
  1246. }
  1247. ApplicationWideCurrentRoomHolder.getInstance().setInCall(true);
  1248. ApplicationWideCurrentRoomHolder.getInstance().setDialing(false);
  1249. if (!TextUtils.isEmpty(roomToken)) {
  1250. NotificationUtils.INSTANCE.cancelExistingNotificationsForRoom(getApplicationContext(),
  1251. conversationUser,
  1252. roomToken);
  1253. }
  1254. if (!hasExternalSignalingServer) {
  1255. int apiVersion = ApiUtils.getSignalingApiVersion(conversationUser,
  1256. new int[]{ApiUtils.APIv3, 2, 1});
  1257. AtomicInteger delayOnError = new AtomicInteger(0);
  1258. ncApi.pullSignalingMessages(credentials,
  1259. ApiUtils.getUrlForSignaling(apiVersion,
  1260. baseUrl,
  1261. roomToken))
  1262. .subscribeOn(Schedulers.io())
  1263. .observeOn(AndroidSchedulers.mainThread())
  1264. .repeatWhen(observable -> observable)
  1265. .takeWhile(observable -> isConnectionEstablished())
  1266. .doOnNext(value -> delayOnError.set(0))
  1267. .retryWhen(errors -> errors
  1268. .flatMap(error -> {
  1269. if (!isConnectionEstablished()) {
  1270. return Observable.error(error);
  1271. }
  1272. if (delayOnError.get() == 0) {
  1273. delayOnError.set(1);
  1274. } else if (delayOnError.get() < 16) {
  1275. delayOnError.set(delayOnError.get() * 2);
  1276. }
  1277. return Observable.timer(delayOnError.get(), TimeUnit.SECONDS);
  1278. })
  1279. )
  1280. .subscribe(new Observer<SignalingOverall>() {
  1281. @Override
  1282. public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
  1283. signalingDisposable = d;
  1284. }
  1285. @Override
  1286. public void onNext(
  1287. @io.reactivex.annotations.NonNull
  1288. SignalingOverall signalingOverall) {
  1289. receivedSignalingMessages(signalingOverall.getOcs().getSignalings());
  1290. }
  1291. @Override
  1292. public void onError(@io.reactivex.annotations.NonNull Throwable e) {
  1293. dispose(signalingDisposable);
  1294. }
  1295. @Override
  1296. public void onComplete() {
  1297. dispose(signalingDisposable);
  1298. }
  1299. });
  1300. }
  1301. }
  1302. }
  1303. @Override
  1304. public void onError(@io.reactivex.annotations.NonNull Throwable e) {
  1305. // unused atm
  1306. }
  1307. @Override
  1308. public void onComplete() {
  1309. // unused atm
  1310. }
  1311. });
  1312. }
  1313. private void setupAndInitiateWebSocketsConnection() {
  1314. if (webSocketConnectionHelper == null) {
  1315. webSocketConnectionHelper = new WebSocketConnectionHelper();
  1316. }
  1317. if (webSocketClient == null) {
  1318. webSocketClient = WebSocketConnectionHelper.getExternalSignalingInstanceForServer(
  1319. externalSignalingServer.getExternalSignalingServer(),
  1320. conversationUser, externalSignalingServer.getExternalSignalingTicket(),
  1321. TextUtils.isEmpty(credentials));
  1322. } else {
  1323. if (webSocketClient.isConnected() && currentCallStatus == CallStatus.PUBLISHER_FAILED) {
  1324. webSocketClient.restartWebSocket();
  1325. }
  1326. }
  1327. joinRoomAndCall();
  1328. }
  1329. private void initiateCall() {
  1330. if (!TextUtils.isEmpty(roomToken)) {
  1331. checkDevicePermissions();
  1332. } else {
  1333. handleFromNotification();
  1334. }
  1335. }
  1336. @Subscribe(threadMode = ThreadMode.BACKGROUND)
  1337. public void onMessageEvent(WebSocketCommunicationEvent webSocketCommunicationEvent) {
  1338. if (currentCallStatus == CallStatus.LEAVING) {
  1339. return;
  1340. }
  1341. switch (webSocketCommunicationEvent.getType()) {
  1342. case "hello":
  1343. Log.d(TAG, "onMessageEvent 'hello'");
  1344. if (!webSocketCommunicationEvent.getHashMap().containsKey("oldResumeId")) {
  1345. if (currentCallStatus == CallStatus.RECONNECTING) {
  1346. hangup(false);
  1347. } else {
  1348. setCallState(CallStatus.RECONNECTING);
  1349. runOnUiThread(this::initiateCall);
  1350. }
  1351. }
  1352. break;
  1353. case "roomJoined":
  1354. Log.d(TAG, "onMessageEvent 'roomJoined'");
  1355. startSendingNick();
  1356. if (webSocketCommunicationEvent.getHashMap().get("roomToken").equals(roomToken)) {
  1357. performCall();
  1358. }
  1359. break;
  1360. case PARTICIPANTS_UPDATE:
  1361. Log.d(TAG, "onMessageEvent 'participantsUpdate'");
  1362. // See MagicWebSocketInstance#onMessage in case "participants" how the 'updateParameters' are created
  1363. Map<String, String> updateParameters = webSocketCommunicationEvent.getHashMap();
  1364. if (updateParameters == null) {
  1365. break;
  1366. }
  1367. String updateRoomToken = updateParameters.get(ROOM_TOKEN);
  1368. String updateAll = updateParameters.get(UPDATE_ALL);
  1369. String updateInCall = updateParameters.get(UPDATE_IN_CALL);
  1370. String jobId = updateParameters.get(JOB_ID);
  1371. if (roomToken.equals(updateRoomToken)) {
  1372. if (updateAll != null && Boolean.parseBoolean(updateAll)) {
  1373. if ("0".equals(updateInCall)) {
  1374. Log.d(TAG, "Most probably a moderator ended the call for all.");
  1375. hangup(true);
  1376. }
  1377. } else if (jobId != null) {
  1378. // In that case a list of users for the room is passed.
  1379. processUsersInRoom(
  1380. (List<HashMap<String, Object>>) webSocketClient
  1381. .getJobWithId(
  1382. Integer.valueOf(jobId)));
  1383. }
  1384. }
  1385. break;
  1386. case "signalingMessage":
  1387. Log.d(TAG, "onMessageEvent 'signalingMessage'");
  1388. processMessage((NCSignalingMessage) webSocketClient.getJobWithId(
  1389. Integer.valueOf(webSocketCommunicationEvent.getHashMap().get("jobId"))));
  1390. break;
  1391. case "peerReadyForRequestingOffer":
  1392. Log.d(TAG, "onMessageEvent 'peerReadyForRequestingOffer'");
  1393. webSocketClient.requestOfferForSessionIdWithType(
  1394. webSocketCommunicationEvent.getHashMap().get("sessionId"), "video");
  1395. break;
  1396. }
  1397. }
  1398. private void dispose(@Nullable Disposable disposable) {
  1399. if (disposable != null && !disposable.isDisposed()) {
  1400. disposable.dispose();
  1401. } else if (disposable == null) {
  1402. if (signalingDisposable != null && !signalingDisposable.isDisposed()) {
  1403. signalingDisposable.dispose();
  1404. signalingDisposable = null;
  1405. }
  1406. }
  1407. }
  1408. private void receivedSignalingMessages(@Nullable List<Signaling> signalingList) {
  1409. if (signalingList != null) {
  1410. for (Signaling signaling : signalingList) {
  1411. try {
  1412. receivedSignalingMessage(signaling);
  1413. } catch (IOException e) {
  1414. Log.e(TAG, "Failed to process received signaling message", e);
  1415. }
  1416. }
  1417. }
  1418. }
  1419. private void receivedSignalingMessage(Signaling signaling) throws IOException {
  1420. String messageType = signaling.getType();
  1421. if (!isConnectionEstablished() && currentCallStatus != CallStatus.CONNECTING) {
  1422. return;
  1423. }
  1424. if ("usersInRoom".equals(messageType)) {
  1425. processUsersInRoom((List<HashMap<String, Object>>) signaling.getMessageWrapper());
  1426. } else if ("message".equals(messageType)) {
  1427. NCSignalingMessage ncSignalingMessage = LoganSquare.parse(signaling.getMessageWrapper().toString(),
  1428. NCSignalingMessage.class);
  1429. processMessage(ncSignalingMessage);
  1430. } else {
  1431. Log.e(TAG, "unexpected message type when receiving signaling message");
  1432. }
  1433. }
  1434. private void processMessage(NCSignalingMessage ncSignalingMessage) {
  1435. if ("video".equals(ncSignalingMessage.getRoomType()) || "screen".equals(ncSignalingMessage.getRoomType())) {
  1436. String type = ncSignalingMessage.getType();
  1437. if ("unshareScreen".equals(type)) {
  1438. endPeerConnection(ncSignalingMessage.getFrom(), true);
  1439. return;
  1440. }
  1441. if ("offer".equals(type)) {
  1442. getOrCreatePeerConnectionWrapperForSessionIdAndType(ncSignalingMessage.getFrom(),
  1443. ncSignalingMessage.getRoomType(), false);
  1444. }
  1445. signalingMessageReceiver.process(ncSignalingMessage);
  1446. } else {
  1447. Log.e(TAG, "unexpected RoomType while processing NCSignalingMessage");
  1448. }
  1449. }
  1450. private void hangup(boolean shutDownView) {
  1451. Log.d(TAG, "hangup! shutDownView=" + shutDownView);
  1452. if (shutDownView) {
  1453. setCallState(CallStatus.LEAVING);
  1454. }
  1455. stopCallingSound();
  1456. dispose(null);
  1457. if (shutDownView) {
  1458. if (videoCapturer != null) {
  1459. try {
  1460. videoCapturer.stopCapture();
  1461. } catch (InterruptedException e) {
  1462. Log.e(TAG, "Failed to stop capturing while hanging up");
  1463. }
  1464. videoCapturer.dispose();
  1465. videoCapturer = null;
  1466. }
  1467. binding.selfVideoRenderer.release();
  1468. if (audioSource != null) {
  1469. audioSource.dispose();
  1470. audioSource = null;
  1471. }
  1472. runOnUiThread(() -> {
  1473. if (audioManager != null) {
  1474. audioManager.stop();
  1475. audioManager = null;
  1476. }
  1477. });
  1478. if (videoSource != null) {
  1479. videoSource = null;
  1480. }
  1481. if (peerConnectionFactory != null) {
  1482. peerConnectionFactory = null;
  1483. }
  1484. localAudioTrack = null;
  1485. localVideoTrack = null;
  1486. if (TextUtils.isEmpty(credentials) && hasExternalSignalingServer) {
  1487. WebSocketConnectionHelper.deleteExternalSignalingInstanceForUserEntity(-1);
  1488. }
  1489. }
  1490. List<String> sessionIdsToEnd = new ArrayList<String>(peerConnectionWrapperList.size());
  1491. for (PeerConnectionWrapper wrapper : peerConnectionWrapperList) {
  1492. sessionIdsToEnd.add(wrapper.getSessionId());
  1493. }
  1494. for (String sessionId : sessionIdsToEnd) {
  1495. endPeerConnection(sessionId, false);
  1496. }
  1497. hangupNetworkCalls(shutDownView);
  1498. ApplicationWideCurrentRoomHolder.getInstance().setInCall(false);
  1499. }
  1500. private void hangupNetworkCalls(boolean shutDownView) {
  1501. Log.d(TAG, "hangupNetworkCalls. shutDownView=" + shutDownView);
  1502. int apiVersion = ApiUtils.getCallApiVersion(conversationUser, new int[]{ApiUtils.APIv4, 1});
  1503. ncApi.leaveCall(credentials, ApiUtils.getUrlForCall(apiVersion, baseUrl, roomToken))
  1504. .subscribeOn(Schedulers.io())
  1505. .observeOn(AndroidSchedulers.mainThread())
  1506. .subscribe(new Observer<GenericOverall>() {
  1507. @Override
  1508. public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
  1509. // unused atm
  1510. }
  1511. @Override
  1512. public void onNext(@io.reactivex.annotations.NonNull GenericOverall genericOverall) {
  1513. if (shutDownView) {
  1514. finish();
  1515. } else if (currentCallStatus == CallStatus.RECONNECTING
  1516. || currentCallStatus == CallStatus.PUBLISHER_FAILED) {
  1517. initiateCall();
  1518. }
  1519. }
  1520. @Override
  1521. public void onError(@io.reactivex.annotations.NonNull Throwable e) {
  1522. // unused atm
  1523. }
  1524. @Override
  1525. public void onComplete() {
  1526. // unused atm
  1527. }
  1528. });
  1529. }
  1530. private void startVideoCapture() {
  1531. if (videoCapturer != null) {
  1532. videoCapturer.startCapture(1280, 720, 30);
  1533. }
  1534. }
  1535. private void processUsersInRoom(List<HashMap<String, Object>> users) {
  1536. Log.d(TAG, "processUsersInRoom");
  1537. List<String> newSessions = new ArrayList<>();
  1538. Set<String> oldSessions = new HashSet<>();
  1539. Map<String, String> userIdsBySessionId = new HashMap<>();
  1540. hasMCU = hasExternalSignalingServer && webSocketClient != null && webSocketClient.hasMCU();
  1541. Log.d(TAG, " hasMCU is " + hasMCU);
  1542. // The signaling session is the same as the Nextcloud session only when the MCU is not used.
  1543. String currentSessionId = callSession;
  1544. if (hasMCU) {
  1545. currentSessionId = webSocketClient.getSessionId();
  1546. }
  1547. Log.d(TAG, " currentSessionId is " + currentSessionId);
  1548. boolean isSelfInCall = false;
  1549. for (HashMap<String, Object> participant : users) {
  1550. long inCallFlag = (long) participant.get("inCall");
  1551. if (!participant.get("sessionId").equals(currentSessionId)) {
  1552. Log.d(TAG, " inCallFlag of participant "
  1553. + participant.get("sessionId").toString().substring(0, 4)
  1554. + " : "
  1555. + inCallFlag);
  1556. boolean isInCall = inCallFlag != 0;
  1557. if (isInCall) {
  1558. newSessions.add(participant.get("sessionId").toString());
  1559. }
  1560. // The property is "userId" when not using the external signaling server and "userid" when using it.
  1561. String userId = null;
  1562. if (participant.get("userId") != null) {
  1563. userId = participant.get("userId").toString();
  1564. } else if (participant.get("userid") != null) {
  1565. userId = participant.get("userid").toString();
  1566. }
  1567. userIdsBySessionId.put(participant.get("sessionId").toString(), userId);
  1568. } else {
  1569. Log.d(TAG, " inCallFlag of currentSessionId: " + inCallFlag);
  1570. isSelfInCall = inCallFlag != 0;
  1571. if (inCallFlag == 0 && currentCallStatus != CallStatus.LEAVING && ApplicationWideCurrentRoomHolder.getInstance().isInCall()) {
  1572. Log.d(TAG, "Most probably a moderator ended the call for all.");
  1573. hangup(true);
  1574. return;
  1575. }
  1576. }
  1577. }
  1578. for (PeerConnectionWrapper peerConnectionWrapper : peerConnectionWrapperList) {
  1579. if (!peerConnectionWrapper.isMCUPublisher()) {
  1580. oldSessions.add(peerConnectionWrapper.getSessionId());
  1581. }
  1582. }
  1583. if (!isSelfInCall) {
  1584. Log.d(TAG, "Self not in call, disconnecting from all other sessions");
  1585. for (String sessionId : oldSessions) {
  1586. Log.d(TAG, " oldSession that will be removed is: " + sessionId);
  1587. endPeerConnection(sessionId, false);
  1588. }
  1589. return;
  1590. }
  1591. // Calculate sessions that left the call
  1592. List<String> disconnectedSessions = new ArrayList<>(oldSessions);
  1593. disconnectedSessions.removeAll(newSessions);
  1594. // Calculate sessions that join the call
  1595. newSessions.removeAll(oldSessions);
  1596. if (currentCallStatus == CallStatus.LEAVING) {
  1597. return;
  1598. }
  1599. if (newSessions.size() > 0 && !hasMCU) {
  1600. getPeersForCall();
  1601. }
  1602. if (hasMCU) {
  1603. // Ensure that own publishing peer is set up.
  1604. getOrCreatePeerConnectionWrapperForSessionIdAndType(webSocketClient.getSessionId(), VIDEO_STREAM_TYPE_VIDEO, true);
  1605. }
  1606. for (String sessionId : newSessions) {
  1607. Log.d(TAG, " newSession joined: " + sessionId);
  1608. getOrCreatePeerConnectionWrapperForSessionIdAndType(sessionId, VIDEO_STREAM_TYPE_VIDEO, false);
  1609. String userId = userIdsBySessionId.get(sessionId);
  1610. runOnUiThread(() -> {
  1611. setupVideoStreamForLayout(
  1612. null,
  1613. sessionId,
  1614. userId,
  1615. false,
  1616. VIDEO_STREAM_TYPE_VIDEO);
  1617. });
  1618. }
  1619. if (newSessions.size() > 0 && currentCallStatus != CallStatus.IN_CONVERSATION) {
  1620. setCallState(CallStatus.IN_CONVERSATION);
  1621. }
  1622. for (String sessionId : disconnectedSessions) {
  1623. Log.d(TAG, " oldSession that will be removed is: " + sessionId);
  1624. endPeerConnection(sessionId, false);
  1625. }
  1626. }
  1627. private void getPeersForCall() {
  1628. Log.d(TAG, "getPeersForCall");
  1629. int apiVersion = ApiUtils.getCallApiVersion(conversationUser, new int[]{ApiUtils.APIv4, 1});
  1630. ncApi.getPeersForCall(
  1631. credentials,
  1632. ApiUtils.getUrlForCall(
  1633. apiVersion,
  1634. baseUrl,
  1635. roomToken))
  1636. .subscribeOn(Schedulers.io())
  1637. .subscribe(new Observer<ParticipantsOverall>() {
  1638. @Override
  1639. public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
  1640. // unused atm
  1641. }
  1642. @Override
  1643. public void onNext(@io.reactivex.annotations.NonNull ParticipantsOverall participantsOverall) {
  1644. participantMap = new HashMap<>();
  1645. for (Participant participant : participantsOverall.getOcs().getData()) {
  1646. participantMap.put(participant.getSessionId(), participant);
  1647. }
  1648. }
  1649. @Override
  1650. public void onError(@io.reactivex.annotations.NonNull Throwable e) {
  1651. Log.e(TAG, "error while executing getPeersForCall", e);
  1652. }
  1653. @Override
  1654. public void onComplete() {
  1655. // unused atm
  1656. }
  1657. });
  1658. }
  1659. private void deletePeerConnection(PeerConnectionWrapper peerConnectionWrapper) {
  1660. peerConnectionWrapper.removePeerConnection();
  1661. peerConnectionWrapperList.remove(peerConnectionWrapper);
  1662. }
  1663. private PeerConnectionWrapper getPeerConnectionWrapperForSessionIdAndType(String sessionId, String type) {
  1664. for (PeerConnectionWrapper wrapper : peerConnectionWrapperList) {
  1665. if (wrapper.getSessionId().equals(sessionId)
  1666. && wrapper.getVideoStreamType().equals(type)) {
  1667. return wrapper;
  1668. }
  1669. }
  1670. return null;
  1671. }
  1672. private PeerConnectionWrapper getOrCreatePeerConnectionWrapperForSessionIdAndType(String sessionId,
  1673. String type,
  1674. boolean publisher) {
  1675. PeerConnectionWrapper peerConnectionWrapper;
  1676. if ((peerConnectionWrapper = getPeerConnectionWrapperForSessionIdAndType(sessionId, type)) != null) {
  1677. return peerConnectionWrapper;
  1678. } else {
  1679. if (peerConnectionFactory == null) {
  1680. Log.e(TAG, "peerConnectionFactory was null in getOrCreatePeerConnectionWrapperForSessionIdAndType.");
  1681. Toast.makeText(context, context.getResources().getString(R.string.nc_common_error_sorry),
  1682. Toast.LENGTH_LONG).show();
  1683. hangup(true);
  1684. return null;
  1685. }
  1686. if (hasMCU && publisher) {
  1687. peerConnectionWrapper = new PeerConnectionWrapper(peerConnectionFactory,
  1688. iceServers,
  1689. sdpConstraintsForMCU,
  1690. sessionId,
  1691. callSession,
  1692. localStream,
  1693. true,
  1694. true,
  1695. type,
  1696. signalingMessageReceiver);
  1697. } else if (hasMCU) {
  1698. peerConnectionWrapper = new PeerConnectionWrapper(peerConnectionFactory,
  1699. iceServers,
  1700. sdpConstraints,
  1701. sessionId,
  1702. callSession,
  1703. null,
  1704. false,
  1705. true,
  1706. type,
  1707. signalingMessageReceiver);
  1708. } else {
  1709. if (!"screen".equals(type)) {
  1710. peerConnectionWrapper = new PeerConnectionWrapper(peerConnectionFactory,
  1711. iceServers,
  1712. sdpConstraints,
  1713. sessionId,
  1714. callSession,
  1715. localStream,
  1716. false,
  1717. false,
  1718. type,
  1719. signalingMessageReceiver);
  1720. } else {
  1721. peerConnectionWrapper = new PeerConnectionWrapper(peerConnectionFactory,
  1722. iceServers,
  1723. sdpConstraints,
  1724. sessionId,
  1725. callSession,
  1726. null,
  1727. false,
  1728. false,
  1729. type,
  1730. signalingMessageReceiver);
  1731. }
  1732. }
  1733. peerConnectionWrapperList.add(peerConnectionWrapper);
  1734. if (publisher) {
  1735. startSendingNick();
  1736. }
  1737. return peerConnectionWrapper;
  1738. }
  1739. }
  1740. private List<PeerConnectionWrapper> getPeerConnectionWrapperListForSessionId(String sessionId) {
  1741. List<PeerConnectionWrapper> internalList = new ArrayList<>();
  1742. for (PeerConnectionWrapper peerConnectionWrapper : peerConnectionWrapperList) {
  1743. if (peerConnectionWrapper.getSessionId().equals(sessionId)) {
  1744. internalList.add(peerConnectionWrapper);
  1745. }
  1746. }
  1747. return internalList;
  1748. }
  1749. private void endPeerConnection(String sessionId, boolean justScreen) {
  1750. List<PeerConnectionWrapper> peerConnectionWrappers;
  1751. if (!(peerConnectionWrappers = getPeerConnectionWrapperListForSessionId(sessionId)).isEmpty()) {
  1752. for (PeerConnectionWrapper peerConnectionWrapper : peerConnectionWrappers) {
  1753. if (peerConnectionWrapper.getSessionId().equals(sessionId)) {
  1754. String videoStreamType = peerConnectionWrapper.getVideoStreamType();
  1755. if (VIDEO_STREAM_TYPE_SCREEN.equals(videoStreamType) || !justScreen) {
  1756. runOnUiThread(() -> removeMediaStream(sessionId, videoStreamType));
  1757. deletePeerConnection(peerConnectionWrapper);
  1758. }
  1759. }
  1760. }
  1761. }
  1762. }
  1763. private void removeMediaStream(String sessionId, String videoStreamType) {
  1764. Log.d(TAG, "removeMediaStream");
  1765. participantDisplayItems.remove(sessionId + "-" + videoStreamType);
  1766. if (!isDestroyed()) {
  1767. initGridAdapter();
  1768. }
  1769. }
  1770. @Subscribe(threadMode = ThreadMode.MAIN)
  1771. public void onMessageEvent(ConfigurationChangeEvent configurationChangeEvent) {
  1772. powerManagerUtils.setOrientation(Objects.requireNonNull(getResources()).getConfiguration().orientation);
  1773. initGridAdapter();
  1774. updateSelfVideoViewPosition();
  1775. }
  1776. private void updateSelfVideoViewConnected(boolean connected) {
  1777. // FIXME In voice only calls there is no video view, so the progress bar would appear floating in the middle of
  1778. // nowhere. However, a way to signal that the local participant is not connected to the HPB is still need in
  1779. // that case.
  1780. if (!connected && !isVoiceOnlyCall) {
  1781. binding.selfVideoViewProgressBar.setVisibility(View.VISIBLE);
  1782. } else {
  1783. binding.selfVideoViewProgressBar.setVisibility(View.GONE);
  1784. }
  1785. }
  1786. private void updateSelfVideoViewPosition() {
  1787. Log.d(TAG, "updateSelfVideoViewPosition");
  1788. if (!isInPipMode) {
  1789. FrameLayout.LayoutParams layoutParams =
  1790. (FrameLayout.LayoutParams) binding.selfVideoRenderer.getLayoutParams();
  1791. DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
  1792. int screenWidthPx = displayMetrics.widthPixels;
  1793. int screenWidthDp = (int) DisplayUtils.convertPixelToDp(screenWidthPx, getApplicationContext());
  1794. float newXafterRotate = 0;
  1795. float newYafterRotate;
  1796. if (binding.callInfosLinearLayout.getVisibility() == View.VISIBLE) {
  1797. newYafterRotate = 250;
  1798. } else {
  1799. newYafterRotate = 20;
  1800. }
  1801. if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
  1802. layoutParams.height = (int) getResources().getDimension(R.dimen.call_self_video_short_side_length);
  1803. layoutParams.width = (int) getResources().getDimension(R.dimen.call_self_video_long_side_length);
  1804. newXafterRotate = (float) (screenWidthDp - getResources().getDimension(R.dimen.call_self_video_short_side_length) * 0.8);
  1805. } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
  1806. layoutParams.height = (int) getResources().getDimension(R.dimen.call_self_video_long_side_length);
  1807. layoutParams.width = (int) getResources().getDimension(R.dimen.call_self_video_short_side_length);
  1808. newXafterRotate = (float) (screenWidthDp - getResources().getDimension(R.dimen.call_self_video_short_side_length) * 0.5);
  1809. }
  1810. binding.selfVideoRenderer.setLayoutParams(layoutParams);
  1811. int newXafterRotatePx = (int) DisplayUtils.convertDpToPixel(newXafterRotate, getApplicationContext());
  1812. binding.selfVideoViewWrapper.setY(newYafterRotate);
  1813. binding.selfVideoViewWrapper.setX(newXafterRotatePx);
  1814. }
  1815. }
  1816. @Subscribe(threadMode = ThreadMode.MAIN)
  1817. public void onMessageEvent(PeerConnectionEvent peerConnectionEvent) {
  1818. String sessionId = peerConnectionEvent.getSessionId();
  1819. String participantDisplayItemId = sessionId + "-" + peerConnectionEvent.getVideoStreamType();
  1820. if (peerConnectionEvent.getPeerConnectionEventType() ==
  1821. PeerConnectionEvent.PeerConnectionEventType.PEER_CONNECTED) {
  1822. if (webSocketClient != null && webSocketClient.getSessionId() != null && webSocketClient.getSessionId().equals(sessionId)) {
  1823. updateSelfVideoViewConnected(true);
  1824. } else if (participantDisplayItems.get(participantDisplayItemId) != null) {
  1825. participantDisplayItems.get(participantDisplayItemId).setConnected(true);
  1826. participantsAdapter.notifyDataSetChanged();
  1827. }
  1828. } else if (peerConnectionEvent.getPeerConnectionEventType() ==
  1829. PeerConnectionEvent.PeerConnectionEventType.PEER_DISCONNECTED) {
  1830. if (webSocketClient != null && webSocketClient.getSessionId() != null && webSocketClient.getSessionId().equals(sessionId)) {
  1831. updateSelfVideoViewConnected(false);
  1832. } else if (participantDisplayItems.get(participantDisplayItemId) != null) {
  1833. participantDisplayItems.get(participantDisplayItemId).setConnected(false);
  1834. participantsAdapter.notifyDataSetChanged();
  1835. }
  1836. } else if (peerConnectionEvent.getPeerConnectionEventType() ==
  1837. PeerConnectionEvent.PeerConnectionEventType.PEER_CLOSED) {
  1838. endPeerConnection(sessionId, VIDEO_STREAM_TYPE_SCREEN.equals(peerConnectionEvent.getVideoStreamType()));
  1839. } else if (peerConnectionEvent.getPeerConnectionEventType() ==
  1840. PeerConnectionEvent.PeerConnectionEventType.SENSOR_FAR ||
  1841. peerConnectionEvent.getPeerConnectionEventType() ==
  1842. PeerConnectionEvent.PeerConnectionEventType.SENSOR_NEAR) {
  1843. if (!isVoiceOnlyCall) {
  1844. boolean enableVideo = peerConnectionEvent.getPeerConnectionEventType() ==
  1845. PeerConnectionEvent.PeerConnectionEventType.SENSOR_FAR && videoOn;
  1846. if (EffortlessPermissions.hasPermissions(this, PERMISSIONS_CAMERA) &&
  1847. (currentCallStatus == CallStatus.CONNECTING || isConnectionEstablished()) && videoOn
  1848. && enableVideo != localVideoTrack.enabled()) {
  1849. toggleMedia(enableVideo, true);
  1850. }
  1851. }
  1852. } else if (peerConnectionEvent.getPeerConnectionEventType() ==
  1853. PeerConnectionEvent.PeerConnectionEventType.NICK_CHANGE) {
  1854. if (participantDisplayItems.get(participantDisplayItemId) != null) {
  1855. participantDisplayItems.get(participantDisplayItemId).setNick(peerConnectionEvent.getNick());
  1856. participantsAdapter.notifyDataSetChanged();
  1857. }
  1858. } else if (peerConnectionEvent.getPeerConnectionEventType() ==
  1859. PeerConnectionEvent.PeerConnectionEventType.VIDEO_CHANGE && !isVoiceOnlyCall) {
  1860. if (participantDisplayItems.get(participantDisplayItemId) != null) {
  1861. participantDisplayItems.get(participantDisplayItemId).setStreamEnabled(peerConnectionEvent.getChangeValue());
  1862. participantsAdapter.notifyDataSetChanged();
  1863. }
  1864. } else if (peerConnectionEvent.getPeerConnectionEventType() ==
  1865. PeerConnectionEvent.PeerConnectionEventType.AUDIO_CHANGE) {
  1866. if (participantDisplayItems.get(participantDisplayItemId) != null) {
  1867. participantDisplayItems.get(participantDisplayItemId).setAudioEnabled(peerConnectionEvent.getChangeValue());
  1868. participantsAdapter.notifyDataSetChanged();
  1869. }
  1870. } else if (peerConnectionEvent.getPeerConnectionEventType() ==
  1871. PeerConnectionEvent.PeerConnectionEventType.PUBLISHER_FAILED) {
  1872. setCallState(CallStatus.PUBLISHER_FAILED);
  1873. webSocketClient.clearResumeId();
  1874. hangup(false);
  1875. }
  1876. }
  1877. private void startSendingNick() {
  1878. DataChannelMessageNick dataChannelMessage = new DataChannelMessageNick();
  1879. dataChannelMessage.setType("nickChanged");
  1880. HashMap<String, String> nickChangedPayload = new HashMap<>();
  1881. nickChangedPayload.put("userid", conversationUser.getUserId());
  1882. nickChangedPayload.put("name", conversationUser.getDisplayName());
  1883. dataChannelMessage.setPayload(nickChangedPayload);
  1884. for (PeerConnectionWrapper peerConnectionWrapper : peerConnectionWrapperList) {
  1885. if (peerConnectionWrapper.isMCUPublisher()) {
  1886. Observable
  1887. .interval(1, TimeUnit.SECONDS)
  1888. .repeatUntil(() -> (!isConnectionEstablished() || isDestroyed()))
  1889. .observeOn(Schedulers.io())
  1890. .subscribe(new Observer<Long>() {
  1891. @Override
  1892. public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
  1893. // unused atm
  1894. }
  1895. @Override
  1896. public void onNext(@io.reactivex.annotations.NonNull Long aLong) {
  1897. peerConnectionWrapper.sendNickChannelData(dataChannelMessage);
  1898. }
  1899. @Override
  1900. public void onError(@io.reactivex.annotations.NonNull Throwable e) {
  1901. // unused atm
  1902. }
  1903. @Override
  1904. public void onComplete() {
  1905. // unused atm
  1906. }
  1907. });
  1908. break;
  1909. }
  1910. }
  1911. }
  1912. @Subscribe(threadMode = ThreadMode.MAIN)
  1913. public void onMessageEvent(MediaStreamEvent mediaStreamEvent) {
  1914. if (mediaStreamEvent.getMediaStream() != null) {
  1915. boolean hasAtLeastOneVideoStream = mediaStreamEvent.getMediaStream().videoTracks != null
  1916. && mediaStreamEvent.getMediaStream().videoTracks.size() > 0;
  1917. setupVideoStreamForLayout(
  1918. mediaStreamEvent.getMediaStream(),
  1919. mediaStreamEvent.getSession(),
  1920. null,
  1921. hasAtLeastOneVideoStream,
  1922. mediaStreamEvent.getVideoStreamType());
  1923. } else {
  1924. setupVideoStreamForLayout(
  1925. null,
  1926. mediaStreamEvent.getSession(),
  1927. null,
  1928. false,
  1929. mediaStreamEvent.getVideoStreamType());
  1930. }
  1931. }
  1932. @Subscribe(threadMode = ThreadMode.BACKGROUND)
  1933. public void onMessageEvent(SessionDescriptionSendEvent sessionDescriptionSend) throws IOException {
  1934. NCMessageWrapper ncMessageWrapper = new NCMessageWrapper();
  1935. ncMessageWrapper.setEv("message");
  1936. ncMessageWrapper.setSessionId(callSession);
  1937. NCSignalingMessage ncSignalingMessage = new NCSignalingMessage();
  1938. ncSignalingMessage.setTo(sessionDescriptionSend.getPeerId());
  1939. ncSignalingMessage.setRoomType(sessionDescriptionSend.getVideoStreamType());
  1940. ncSignalingMessage.setType(sessionDescriptionSend.getType());
  1941. NCMessagePayload ncMessagePayload = new NCMessagePayload();
  1942. ncMessagePayload.setType(sessionDescriptionSend.getType());
  1943. if (!"candidate".equals(sessionDescriptionSend.getType())) {
  1944. ncMessagePayload.setSdp(sessionDescriptionSend.getSessionDescription().description);
  1945. ncMessagePayload.setNick(conversationUser.getDisplayName());
  1946. } else {
  1947. ncMessagePayload.setIceCandidate(sessionDescriptionSend.getNcIceCandidate());
  1948. }
  1949. // Set all we need
  1950. ncSignalingMessage.setPayload(ncMessagePayload);
  1951. ncMessageWrapper.setSignalingMessage(ncSignalingMessage);
  1952. if (!hasExternalSignalingServer) {
  1953. StringBuilder stringBuilder = new StringBuilder();
  1954. stringBuilder.append("{")
  1955. .append("\"fn\":\"")
  1956. .append(StringEscapeUtils.escapeJson(LoganSquare.serialize(ncMessageWrapper.getSignalingMessage())))
  1957. .append("\"")
  1958. .append(",")
  1959. .append("\"sessionId\":")
  1960. .append("\"").append(StringEscapeUtils.escapeJson(callSession)).append("\"")
  1961. .append(",")
  1962. .append("\"ev\":\"message\"")
  1963. .append("}");
  1964. List<String> strings = new ArrayList<>();
  1965. String stringToSend = stringBuilder.toString();
  1966. strings.add(stringToSend);
  1967. int apiVersion = ApiUtils.getSignalingApiVersion(conversationUser, new int[]{ApiUtils.APIv3, 2, 1});
  1968. ncApi.sendSignalingMessages(credentials, ApiUtils.getUrlForSignaling(apiVersion, baseUrl, roomToken),
  1969. strings.toString())
  1970. .retry(3)
  1971. .subscribeOn(Schedulers.io())
  1972. .subscribe(new Observer<SignalingOverall>() {
  1973. @Override
  1974. public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
  1975. // unused atm
  1976. }
  1977. @Override
  1978. public void onNext(@io.reactivex.annotations.NonNull SignalingOverall signalingOverall) {
  1979. receivedSignalingMessages(signalingOverall.getOcs().getSignalings());
  1980. }
  1981. @Override
  1982. public void onError(@io.reactivex.annotations.NonNull Throwable e) {
  1983. Log.e(TAG, "", e);
  1984. }
  1985. @Override
  1986. public void onComplete() {
  1987. // unused atm
  1988. }
  1989. });
  1990. } else {
  1991. webSocketClient.sendCallMessage(ncMessageWrapper);
  1992. }
  1993. }
  1994. @Override
  1995. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
  1996. @NonNull int[] grantResults) {
  1997. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  1998. EffortlessPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults,
  1999. this);
  2000. }
  2001. private void setupVideoStreamForLayout(@Nullable MediaStream mediaStream,
  2002. String session,
  2003. String userId,
  2004. boolean videoStreamEnabled,
  2005. String videoStreamType) {
  2006. PeerConnectionWrapper peerConnectionWrapper = getPeerConnectionWrapperForSessionIdAndType(session,
  2007. videoStreamType);
  2008. boolean connected = false;
  2009. if (peerConnectionWrapper != null) {
  2010. PeerConnection.IceConnectionState iceConnectionState = peerConnectionWrapper.getPeerConnection().iceConnectionState();
  2011. connected = iceConnectionState == PeerConnection.IceConnectionState.CONNECTED ||
  2012. iceConnectionState == PeerConnection.IceConnectionState.COMPLETED;
  2013. }
  2014. String nick;
  2015. if (hasExternalSignalingServer) {
  2016. nick = webSocketClient.getDisplayNameForSession(session);
  2017. } else {
  2018. nick = peerConnectionWrapper != null ? peerConnectionWrapper.getNick() : "";
  2019. }
  2020. String userId4Usage = userId;
  2021. if (userId4Usage == null) {
  2022. if (hasMCU) {
  2023. userId4Usage = webSocketClient.getUserIdForSession(session);
  2024. } else if (participantMap.get(session) != null && participantMap.get(session).getCalculatedActorType() == Participant.ActorType.USERS) {
  2025. userId4Usage = participantMap.get(session).getCalculatedActorId();
  2026. }
  2027. }
  2028. ParticipantDisplayItem participantDisplayItem = new ParticipantDisplayItem(baseUrl,
  2029. userId4Usage,
  2030. session,
  2031. connected,
  2032. nick,
  2033. mediaStream,
  2034. videoStreamType,
  2035. videoStreamEnabled,
  2036. rootEglBase);
  2037. participantDisplayItems.put(session + "-" + videoStreamType, participantDisplayItem);
  2038. initGridAdapter();
  2039. }
  2040. private void setCallState(CallStatus callState) {
  2041. if (currentCallStatus == null || currentCallStatus != callState) {
  2042. currentCallStatus = callState;
  2043. if (handler == null) {
  2044. handler = new Handler(Looper.getMainLooper());
  2045. } else {
  2046. handler.removeCallbacksAndMessages(null);
  2047. }
  2048. switch (callState) {
  2049. case CONNECTING:
  2050. handler.post(() -> {
  2051. playCallingSound();
  2052. if (isIncomingCallFromNotification) {
  2053. binding.callStates.callStateTextView.setText(R.string.nc_call_incoming);
  2054. } else {
  2055. binding.callStates.callStateTextView.setText(R.string.nc_call_ringing);
  2056. }
  2057. binding.callConversationNameTextView.setText(conversationName);
  2058. binding.callModeTextView.setText(getDescriptionForCallType());
  2059. if (binding.callStates.callStateRelativeLayout.getVisibility() != View.VISIBLE) {
  2060. binding.callStates.callStateRelativeLayout.setVisibility(View.VISIBLE);
  2061. }
  2062. if (binding.gridview.getVisibility() != View.INVISIBLE) {
  2063. binding.gridview.setVisibility(View.INVISIBLE);
  2064. }
  2065. if (binding.callStates.callStateProgressBar.getVisibility() != View.VISIBLE) {
  2066. binding.callStates.callStateProgressBar.setVisibility(View.VISIBLE);
  2067. }
  2068. if (binding.callStates.errorImageView.getVisibility() != View.GONE) {
  2069. binding.callStates.errorImageView.setVisibility(View.GONE);
  2070. }
  2071. });
  2072. break;
  2073. case CALLING_TIMEOUT:
  2074. handler.post(() -> {
  2075. hangup(false);
  2076. binding.callStates.callStateTextView.setText(R.string.nc_call_timeout);
  2077. binding.callModeTextView.setText(getDescriptionForCallType());
  2078. if (binding.callStates.callStateRelativeLayout.getVisibility() != View.VISIBLE) {
  2079. binding.callStates.callStateRelativeLayout.setVisibility(View.VISIBLE);
  2080. }
  2081. if (binding.callStates.callStateProgressBar.getVisibility() != View.GONE) {
  2082. binding.callStates.callStateProgressBar.setVisibility(View.GONE);
  2083. }
  2084. if (binding.gridview.getVisibility() != View.INVISIBLE) {
  2085. binding.gridview.setVisibility(View.INVISIBLE);
  2086. }
  2087. binding.callStates.errorImageView.setImageResource(R.drawable.ic_av_timer_timer_24dp);
  2088. if (binding.callStates.errorImageView.getVisibility() != View.VISIBLE) {
  2089. binding.callStates.errorImageView.setVisibility(View.VISIBLE);
  2090. }
  2091. });
  2092. break;
  2093. case PUBLISHER_FAILED:
  2094. handler.post(() -> {
  2095. // No calling sound when the publisher failed
  2096. binding.callStates.callStateTextView.setText(R.string.nc_call_reconnecting);
  2097. binding.callModeTextView.setText(getDescriptionForCallType());
  2098. if (binding.callStates.callStateRelativeLayout.getVisibility() != View.VISIBLE) {
  2099. binding.callStates.callStateRelativeLayout.setVisibility(View.VISIBLE);
  2100. }
  2101. if (binding.gridview.getVisibility() != View.INVISIBLE) {
  2102. binding.gridview.setVisibility(View.INVISIBLE);
  2103. }
  2104. if (binding.callStates.callStateProgressBar.getVisibility() != View.VISIBLE) {
  2105. binding.callStates.callStateProgressBar.setVisibility(View.VISIBLE);
  2106. }
  2107. if (binding.callStates.errorImageView.getVisibility() != View.GONE) {
  2108. binding.callStates.errorImageView.setVisibility(View.GONE);
  2109. }
  2110. });
  2111. break;
  2112. case RECONNECTING:
  2113. handler.post(() -> {
  2114. playCallingSound();
  2115. binding.callStates.callStateTextView.setText(R.string.nc_call_reconnecting);
  2116. binding.callModeTextView.setText(getDescriptionForCallType());
  2117. if (binding.callStates.callStateRelativeLayout.getVisibility() != View.VISIBLE) {
  2118. binding.callStates.callStateRelativeLayout.setVisibility(View.VISIBLE);
  2119. }
  2120. if (binding.gridview.getVisibility() != View.INVISIBLE) {
  2121. binding.gridview.setVisibility(View.INVISIBLE);
  2122. }
  2123. if (binding.callStates.callStateProgressBar.getVisibility() != View.VISIBLE) {
  2124. binding.callStates.callStateProgressBar.setVisibility(View.VISIBLE);
  2125. }
  2126. if (binding.callStates.errorImageView.getVisibility() != View.GONE) {
  2127. binding.callStates.errorImageView.setVisibility(View.GONE);
  2128. }
  2129. });
  2130. break;
  2131. case JOINED:
  2132. handler.postDelayed(() -> setCallState(CallStatus.CALLING_TIMEOUT), 45000);
  2133. handler.post(() -> {
  2134. binding.callModeTextView.setText(getDescriptionForCallType());
  2135. if (isIncomingCallFromNotification) {
  2136. binding.callStates.callStateTextView.setText(R.string.nc_call_incoming);
  2137. } else {
  2138. binding.callStates.callStateTextView.setText(R.string.nc_call_ringing);
  2139. }
  2140. if (binding.callStates.callStateRelativeLayout.getVisibility() != View.VISIBLE) {
  2141. binding.callStates.callStateRelativeLayout.setVisibility(View.VISIBLE);
  2142. }
  2143. if (binding.callStates.callStateProgressBar.getVisibility() != View.VISIBLE) {
  2144. binding.callStates.callStateProgressBar.setVisibility(View.VISIBLE);
  2145. }
  2146. if (binding.gridview.getVisibility() != View.INVISIBLE) {
  2147. binding.gridview.setVisibility(View.INVISIBLE);
  2148. }
  2149. if (binding.callStates.errorImageView.getVisibility() != View.GONE) {
  2150. binding.callStates.errorImageView.setVisibility(View.GONE);
  2151. }
  2152. });
  2153. break;
  2154. case IN_CONVERSATION:
  2155. handler.post(() -> {
  2156. stopCallingSound();
  2157. binding.callModeTextView.setText(getDescriptionForCallType());
  2158. if (!isVoiceOnlyCall) {
  2159. binding.callInfosLinearLayout.setVisibility(View.GONE);
  2160. }
  2161. if (!isPushToTalkActive) {
  2162. animateCallControls(false, 5000);
  2163. }
  2164. if (binding.callStates.callStateRelativeLayout.getVisibility() != View.INVISIBLE) {
  2165. binding.callStates.callStateRelativeLayout.setVisibility(View.INVISIBLE);
  2166. }
  2167. if (binding.callStates.callStateProgressBar.getVisibility() != View.GONE) {
  2168. binding.callStates.callStateProgressBar.setVisibility(View.GONE);
  2169. }
  2170. if (binding.gridview.getVisibility() != View.VISIBLE) {
  2171. binding.gridview.setVisibility(View.VISIBLE);
  2172. }
  2173. if (binding.callStates.errorImageView.getVisibility() != View.GONE) {
  2174. binding.callStates.errorImageView.setVisibility(View.GONE);
  2175. }
  2176. });
  2177. break;
  2178. case OFFLINE:
  2179. handler.post(() -> {
  2180. stopCallingSound();
  2181. binding.callStates.callStateTextView.setText(R.string.nc_offline);
  2182. if (binding.callStates.callStateRelativeLayout.getVisibility() != View.VISIBLE) {
  2183. binding.callStates.callStateRelativeLayout.setVisibility(View.VISIBLE);
  2184. }
  2185. if (binding.gridview.getVisibility() != View.INVISIBLE) {
  2186. binding.gridview.setVisibility(View.INVISIBLE);
  2187. }
  2188. if (binding.callStates.callStateProgressBar.getVisibility() != View.GONE) {
  2189. binding.callStates.callStateProgressBar.setVisibility(View.GONE);
  2190. }
  2191. binding.callStates.errorImageView.setImageResource(R.drawable.ic_signal_wifi_off_white_24dp);
  2192. if (binding.callStates.errorImageView.getVisibility() != View.VISIBLE) {
  2193. binding.callStates.errorImageView.setVisibility(View.VISIBLE);
  2194. }
  2195. });
  2196. break;
  2197. case LEAVING:
  2198. handler.post(() -> {
  2199. if (!isDestroyed()) {
  2200. stopCallingSound();
  2201. binding.callModeTextView.setText(getDescriptionForCallType());
  2202. binding.callStates.callStateTextView.setText(R.string.nc_leaving_call);
  2203. binding.callStates.callStateRelativeLayout.setVisibility(View.VISIBLE);
  2204. binding.gridview.setVisibility(View.INVISIBLE);
  2205. binding.callStates.callStateProgressBar.setVisibility(View.VISIBLE);
  2206. binding.callStates.errorImageView.setVisibility(View.GONE);
  2207. }
  2208. });
  2209. break;
  2210. default:
  2211. }
  2212. }
  2213. }
  2214. private String getDescriptionForCallType() {
  2215. String appName = getResources().getString(R.string.nc_app_product_name);
  2216. if (isVoiceOnlyCall) {
  2217. return String.format(getResources().getString(R.string.nc_call_voice), appName);
  2218. } else {
  2219. return String.format(getResources().getString(R.string.nc_call_video), appName);
  2220. }
  2221. }
  2222. private void playCallingSound() {
  2223. stopCallingSound();
  2224. Uri ringtoneUri;
  2225. if (isIncomingCallFromNotification) {
  2226. ringtoneUri = NotificationUtils.INSTANCE.getCallRingtoneUri(getApplicationContext(), appPreferences);
  2227. } else {
  2228. ringtoneUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/raw" +
  2229. "/tr110_1_kap8_3_freiton1");
  2230. }
  2231. if (ringtoneUri != null) {
  2232. mediaPlayer = new MediaPlayer();
  2233. try {
  2234. mediaPlayer.setDataSource(this, ringtoneUri);
  2235. mediaPlayer.setLooping(true);
  2236. AudioAttributes audioAttributes = new AudioAttributes.Builder().setContentType(
  2237. AudioAttributes.CONTENT_TYPE_SONIFICATION)
  2238. .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
  2239. .build();
  2240. mediaPlayer.setAudioAttributes(audioAttributes);
  2241. mediaPlayer.setOnPreparedListener(mp -> mediaPlayer.start());
  2242. mediaPlayer.prepareAsync();
  2243. } catch (IOException e) {
  2244. Log.e(TAG, "Failed to play sound");
  2245. }
  2246. }
  2247. }
  2248. private void stopCallingSound() {
  2249. if (mediaPlayer != null) {
  2250. if (mediaPlayer.isPlaying()) {
  2251. mediaPlayer.stop();
  2252. }
  2253. mediaPlayer.release();
  2254. mediaPlayer = null;
  2255. }
  2256. }
  2257. /**
  2258. * Temporary implementation of SignalingMessageReceiver until signaling related code is extracted from CallActivity.
  2259. */
  2260. private static class CallActivitySignalingMessageReceiver extends SignalingMessageReceiver {
  2261. public void process(NCSignalingMessage message) {
  2262. processSignalingMessage(message);
  2263. }
  2264. }
  2265. private class MicrophoneButtonTouchListener implements View.OnTouchListener {
  2266. @SuppressLint("ClickableViewAccessibility")
  2267. @Override
  2268. public boolean onTouch(View v, MotionEvent event) {
  2269. v.onTouchEvent(event);
  2270. if (event.getAction() == MotionEvent.ACTION_UP && isPushToTalkActive) {
  2271. isPushToTalkActive = false;
  2272. binding.microphoneButton.setImageResource(R.drawable.ic_mic_off_white_24px);
  2273. pulseAnimation.stop();
  2274. toggleMedia(false, false);
  2275. animateCallControls(false, 5000);
  2276. }
  2277. return true;
  2278. }
  2279. }
  2280. @Subscribe(threadMode = ThreadMode.BACKGROUND)
  2281. public void onMessageEvent(NetworkEvent networkEvent) {
  2282. if (networkEvent.getNetworkConnectionEvent() == NetworkEvent.NetworkConnectionEvent.NETWORK_CONNECTED) {
  2283. if (handler != null) {
  2284. handler.removeCallbacksAndMessages(null);
  2285. }
  2286. } else if (networkEvent.getNetworkConnectionEvent() ==
  2287. NetworkEvent.NetworkConnectionEvent.NETWORK_DISCONNECTED) {
  2288. if (handler != null) {
  2289. handler.removeCallbacksAndMessages(null);
  2290. }
  2291. }
  2292. }
  2293. @RequiresApi(api = Build.VERSION_CODES.O)
  2294. @Override
  2295. public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
  2296. super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
  2297. Log.d(TAG, "onPictureInPictureModeChanged");
  2298. Log.d(TAG, "isInPictureInPictureMode= " + isInPictureInPictureMode);
  2299. isInPipMode = isInPictureInPictureMode;
  2300. if (isInPictureInPictureMode) {
  2301. mReceiver =
  2302. new BroadcastReceiver() {
  2303. @Override
  2304. public void onReceive(Context context, Intent intent) {
  2305. if (intent == null || !MICROPHONE_PIP_INTENT_NAME.equals(intent.getAction())) {
  2306. return;
  2307. }
  2308. final int action = intent.getIntExtra(MICROPHONE_PIP_INTENT_EXTRA_ACTION, 0);
  2309. switch (action) {
  2310. case MICROPHONE_PIP_REQUEST_MUTE:
  2311. case MICROPHONE_PIP_REQUEST_UNMUTE:
  2312. onMicrophoneClick();
  2313. break;
  2314. }
  2315. }
  2316. };
  2317. registerReceiver(mReceiver,
  2318. new IntentFilter(MICROPHONE_PIP_INTENT_NAME),
  2319. permissionUtil.getPrivateBroadcastPermission(),
  2320. null);
  2321. updateUiForPipMode();
  2322. } else {
  2323. unregisterReceiver(mReceiver);
  2324. mReceiver = null;
  2325. updateUiForNormalMode();
  2326. }
  2327. }
  2328. void updatePictureInPictureActions(
  2329. @DrawableRes int iconId,
  2330. String title,
  2331. int requestCode) {
  2332. if (isGreaterEqualOreo() && isPipModePossible()) {
  2333. final ArrayList<RemoteAction> actions = new ArrayList<>();
  2334. final Icon icon = Icon.createWithResource(this, iconId);
  2335. int intentFlag;
  2336. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
  2337. intentFlag = FLAG_IMMUTABLE;
  2338. } else {
  2339. intentFlag = 0;
  2340. }
  2341. final PendingIntent intent =
  2342. PendingIntent.getBroadcast(
  2343. this,
  2344. requestCode,
  2345. new Intent(MICROPHONE_PIP_INTENT_NAME).putExtra(MICROPHONE_PIP_INTENT_EXTRA_ACTION, requestCode),
  2346. intentFlag);
  2347. actions.add(new RemoteAction(icon, title, title, intent));
  2348. mPictureInPictureParamsBuilder.setActions(actions);
  2349. setPictureInPictureParams(mPictureInPictureParamsBuilder.build());
  2350. }
  2351. }
  2352. public void updateUiForPipMode() {
  2353. Log.d(TAG, "updateUiForPipMode");
  2354. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
  2355. ViewGroup.LayoutParams.WRAP_CONTENT);
  2356. params.setMargins(0, 0, 0, 0);
  2357. binding.gridview.setLayoutParams(params);
  2358. binding.callControls.setVisibility(View.GONE);
  2359. binding.callInfosLinearLayout.setVisibility(View.GONE);
  2360. binding.selfVideoViewWrapper.setVisibility(View.GONE);
  2361. binding.callStates.callStateRelativeLayout.setVisibility(View.GONE);
  2362. if (participantDisplayItems.size() > 1) {
  2363. binding.pipCallConversationNameTextView.setText(conversationName);
  2364. binding.pipGroupCallOverlay.setVisibility(View.VISIBLE);
  2365. } else {
  2366. binding.pipGroupCallOverlay.setVisibility(View.GONE);
  2367. }
  2368. binding.selfVideoRenderer.release();
  2369. }
  2370. public void updateUiForNormalMode() {
  2371. Log.d(TAG, "updateUiForNormalMode");
  2372. if (isVoiceOnlyCall) {
  2373. binding.callControls.setVisibility(View.VISIBLE);
  2374. } else {
  2375. // animateCallControls needs this to be invisible for a check.
  2376. binding.callControls.setVisibility(View.INVISIBLE);
  2377. }
  2378. initViews();
  2379. binding.callInfosLinearLayout.setVisibility(View.VISIBLE);
  2380. binding.selfVideoViewWrapper.setVisibility(View.VISIBLE);
  2381. binding.pipGroupCallOverlay.setVisibility(View.GONE);
  2382. }
  2383. @Override
  2384. void suppressFitsSystemWindows() {
  2385. binding.controllerCallLayout.setFitsSystemWindows(false);
  2386. }
  2387. public void onConfigurationChanged(Configuration newConfig) {
  2388. super.onConfigurationChanged(newConfig);
  2389. eventBus.post(new ConfigurationChangeEvent());
  2390. }
  2391. private class SelfVideoTouchListener implements View.OnTouchListener {
  2392. @SuppressLint("ClickableViewAccessibility")
  2393. @Override
  2394. public boolean onTouch(View view, MotionEvent event) {
  2395. long duration = event.getEventTime() - event.getDownTime();
  2396. if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
  2397. float newY = event.getRawY() - binding.selfVideoViewWrapper.getHeight() / (float) 2;
  2398. float newX = event.getRawX() - binding.selfVideoViewWrapper.getWidth() / (float) 2;
  2399. binding.selfVideoViewWrapper.setY(newY);
  2400. binding.selfVideoViewWrapper.setX(newX);
  2401. } else if (event.getActionMasked() == MotionEvent.ACTION_UP && duration < 100) {
  2402. switchCamera();
  2403. }
  2404. return true;
  2405. }
  2406. }
  2407. }