CallNotificationController.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.talk.controllers;
  21. import android.annotation.SuppressLint;
  22. import android.content.Context;
  23. import android.graphics.Bitmap;
  24. import android.graphics.Color;
  25. import android.graphics.drawable.BitmapDrawable;
  26. import android.graphics.drawable.ColorDrawable;
  27. import android.media.AudioAttributes;
  28. import android.media.MediaPlayer;
  29. import android.net.Uri;
  30. import android.os.Bundle;
  31. import android.os.Handler;
  32. import android.renderscript.RenderScript;
  33. import android.text.TextUtils;
  34. import android.util.Log;
  35. import android.view.LayoutInflater;
  36. import android.view.View;
  37. import android.view.ViewGroup;
  38. import android.widget.ImageView;
  39. import android.widget.RelativeLayout;
  40. import android.widget.TextView;
  41. import com.bluelinelabs.conductor.RouterTransaction;
  42. import com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler;
  43. import com.bluelinelabs.logansquare.LoganSquare;
  44. import com.facebook.common.executors.UiThreadImmediateExecutorService;
  45. import com.facebook.common.references.CloseableReference;
  46. import com.facebook.datasource.DataSource;
  47. import com.facebook.drawee.backends.pipeline.Fresco;
  48. import com.facebook.drawee.view.SimpleDraweeView;
  49. import com.facebook.imagepipeline.core.ImagePipeline;
  50. import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber;
  51. import com.facebook.imagepipeline.image.CloseableImage;
  52. import com.facebook.imagepipeline.postprocessors.BlurPostProcessor;
  53. import com.facebook.imagepipeline.request.ImageRequest;
  54. import com.nextcloud.talk.R;
  55. import com.nextcloud.talk.api.NcApi;
  56. import com.nextcloud.talk.application.NextcloudTalkApplication;
  57. import com.nextcloud.talk.controllers.base.BaseController;
  58. import com.nextcloud.talk.events.CallNotificationClick;
  59. import com.nextcloud.talk.events.ConfigurationChangeEvent;
  60. import com.nextcloud.talk.models.RingtoneSettings;
  61. import com.nextcloud.talk.models.database.CapabilitiesUtil;
  62. import com.nextcloud.talk.models.database.UserEntity;
  63. import com.nextcloud.talk.models.json.conversations.Conversation;
  64. import com.nextcloud.talk.models.json.conversations.RoomOverall;
  65. import com.nextcloud.talk.models.json.participants.Participant;
  66. import com.nextcloud.talk.models.json.participants.ParticipantsOverall;
  67. import com.nextcloud.talk.utils.ApiUtils;
  68. import com.nextcloud.talk.utils.DisplayUtils;
  69. import com.nextcloud.talk.utils.DoNotDisturbUtils;
  70. import com.nextcloud.talk.utils.bundle.BundleKeys;
  71. import com.nextcloud.talk.utils.preferences.AppPreferences;
  72. import com.nextcloud.talk.utils.singletons.AvatarStatusCodeHolder;
  73. import org.greenrobot.eventbus.EventBus;
  74. import org.greenrobot.eventbus.Subscribe;
  75. import org.greenrobot.eventbus.ThreadMode;
  76. import org.michaelevans.colorart.library.ColorArt;
  77. import org.parceler.Parcels;
  78. import java.io.IOException;
  79. import java.util.ArrayList;
  80. import java.util.List;
  81. import javax.inject.Inject;
  82. import androidx.annotation.NonNull;
  83. import androidx.annotation.Nullable;
  84. import androidx.constraintlayout.widget.ConstraintLayout;
  85. import autodagger.AutoInjector;
  86. import butterknife.BindView;
  87. import butterknife.OnClick;
  88. import io.reactivex.Observer;
  89. import io.reactivex.android.schedulers.AndroidSchedulers;
  90. import io.reactivex.disposables.Disposable;
  91. import io.reactivex.schedulers.Schedulers;
  92. import okhttp3.Cache;
  93. @AutoInjector(NextcloudTalkApplication.class)
  94. public class CallNotificationController extends BaseController {
  95. public static final String TAG = "CallNotificationController";
  96. @Inject
  97. NcApi ncApi;
  98. @Inject
  99. AppPreferences appPreferences;
  100. @Inject
  101. Cache cache;
  102. @Inject
  103. EventBus eventBus;
  104. @Inject
  105. Context context;
  106. @BindView(R.id.incomingCallVoiceOrVideoTextView)
  107. TextView incomingCallVoiceOrVideoTextView;
  108. @BindView(R.id.conversationNameTextView)
  109. TextView conversationNameTextView;
  110. @BindView(R.id.avatarImageView)
  111. SimpleDraweeView avatarImageView;
  112. @BindView(R.id.callAnswerVoiceOnlyView)
  113. SimpleDraweeView callAnswerVoiceOnlyView;
  114. @BindView(R.id.callAnswerCameraView)
  115. SimpleDraweeView callAnswerCameraView;
  116. @BindView(R.id.backgroundImageView)
  117. ImageView backgroundImageView;
  118. @BindView(R.id.incomingTextRelativeLayout)
  119. RelativeLayout incomingTextRelativeLayout;
  120. private List<Disposable> disposablesList = new ArrayList<>();
  121. private Bundle originalBundle;
  122. private String roomId;
  123. private UserEntity userBeingCalled;
  124. private String credentials;
  125. private Conversation currentConversation;
  126. private MediaPlayer mediaPlayer;
  127. private boolean leavingScreen = false;
  128. private RenderScript renderScript;
  129. private Handler handler;
  130. public CallNotificationController(Bundle args) {
  131. super(args);
  132. NextcloudTalkApplication.Companion.getSharedApplication().getComponentApplication().inject(this);
  133. eventBus.post(new CallNotificationClick());
  134. this.roomId = args.getString(BundleKeys.INSTANCE.getKEY_ROOM_ID(), "");
  135. this.currentConversation = Parcels.unwrap(args.getParcelable(BundleKeys.INSTANCE.getKEY_ROOM()));
  136. this.userBeingCalled = args.getParcelable(BundleKeys.INSTANCE.getKEY_USER_ENTITY());
  137. this.originalBundle = args;
  138. credentials = ApiUtils.getCredentials(userBeingCalled.getUsername(), userBeingCalled.getToken());
  139. }
  140. @Override
  141. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  142. return inflater.inflate(R.layout.controller_call_notification, container, false);
  143. }
  144. private void showAnswerControls() {
  145. callAnswerCameraView.setVisibility(View.VISIBLE);
  146. callAnswerVoiceOnlyView.setVisibility(View.VISIBLE);
  147. }
  148. @OnClick(R.id.callControlHangupView)
  149. void hangup() {
  150. leavingScreen = true;
  151. if (getActivity() != null) {
  152. getActivity().finish();
  153. }
  154. }
  155. @OnClick(R.id.callAnswerCameraView)
  156. void answerWithCamera() {
  157. originalBundle.putBoolean(BundleKeys.INSTANCE.getKEY_CALL_VOICE_ONLY(), false);
  158. proceedToCall();
  159. }
  160. @OnClick(R.id.callAnswerVoiceOnlyView)
  161. void answerVoiceOnly() {
  162. originalBundle.putBoolean(BundleKeys.INSTANCE.getKEY_CALL_VOICE_ONLY(), true);
  163. proceedToCall();
  164. }
  165. private void proceedToCall() {
  166. originalBundle.putString(BundleKeys.INSTANCE.getKEY_ROOM_TOKEN(), currentConversation.getToken());
  167. originalBundle.putString(BundleKeys.INSTANCE.getKEY_CONVERSATION_NAME(), currentConversation.getDisplayName());
  168. getRouter().replaceTopController(RouterTransaction.with(new CallController(originalBundle))
  169. .popChangeHandler(new HorizontalChangeHandler())
  170. .pushChangeHandler(new HorizontalChangeHandler())
  171. .tag(CallController.TAG));
  172. }
  173. private void checkIfAnyParticipantsRemainInRoom() {
  174. int apiVersion = ApiUtils.getCallApiVersion(userBeingCalled, new int[] {ApiUtils.APIv4, 1});
  175. ncApi.getPeersForCall(credentials, ApiUtils.getUrlForCall(apiVersion, userBeingCalled.getBaseUrl(),
  176. currentConversation.getToken()))
  177. .subscribeOn(Schedulers.io())
  178. .takeWhile(observable -> !leavingScreen)
  179. .subscribe(new Observer<ParticipantsOverall>() {
  180. @Override
  181. public void onSubscribe(Disposable d) {
  182. disposablesList.add(d);
  183. }
  184. @Override
  185. public void onNext(ParticipantsOverall participantsOverall) {
  186. boolean hasParticipantsInCall = false;
  187. boolean inCallOnDifferentDevice = false;
  188. List<Participant> participantList = participantsOverall.getOcs().getData();
  189. hasParticipantsInCall = participantList.size() > 0;
  190. if (hasParticipantsInCall) {
  191. for (Participant participant : participantList) {
  192. if (participant.getActorType() == Participant.ActorType.USERS &&
  193. participant.getActorId().equals(userBeingCalled.getUserId())) {
  194. inCallOnDifferentDevice = true;
  195. break;
  196. }
  197. }
  198. }
  199. if (!hasParticipantsInCall || inCallOnDifferentDevice) {
  200. if (getActivity() != null) {
  201. getActivity().runOnUiThread(() -> hangup());
  202. }
  203. }
  204. }
  205. @Override
  206. public void onError(Throwable e) {
  207. }
  208. @Override
  209. public void onComplete() {
  210. if (!leavingScreen) {
  211. handler.postDelayed(() -> checkIfAnyParticipantsRemainInRoom(), 5000);
  212. }
  213. }
  214. });
  215. }
  216. private void handleFromNotification() {
  217. int apiVersion = ApiUtils.getConversationApiVersion(userBeingCalled, new int[] {ApiUtils.APIv4,
  218. ApiUtils.APIv3, 1});
  219. ncApi.getRoom(credentials, ApiUtils.getUrlForRoom(apiVersion, userBeingCalled.getBaseUrl(), roomId))
  220. .subscribeOn(Schedulers.io())
  221. .retry(3)
  222. .observeOn(AndroidSchedulers.mainThread())
  223. .subscribe(new Observer<RoomOverall>() {
  224. @Override
  225. public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
  226. disposablesList.add(d);
  227. }
  228. @Override
  229. public void onNext(@io.reactivex.annotations.NonNull RoomOverall roomOverall) {
  230. currentConversation = roomOverall.getOcs().data;
  231. runAllThings();
  232. if (apiVersion >= 3) {
  233. boolean hasCallFlags =
  234. CapabilitiesUtil.hasSpreedFeatureCapability(userBeingCalled,
  235. "conversation-call-flags");
  236. if (hasCallFlags) {
  237. if (isInCallWithVideo(currentConversation.callFlag)) {
  238. incomingCallVoiceOrVideoTextView.setText(
  239. String.format(getResources().getString(R.string.nc_call_video),
  240. getResources().getString(R.string.nc_app_product_name)));
  241. } else {
  242. incomingCallVoiceOrVideoTextView.setText(
  243. String.format(getResources().getString(R.string.nc_call_voice),
  244. getResources().getString(R.string.nc_app_product_name)));
  245. }
  246. }
  247. }
  248. }
  249. @SuppressLint("LongLogTag")
  250. @Override
  251. public void onError(@io.reactivex.annotations.NonNull Throwable e) {
  252. Log.e(TAG, e.getMessage(), e);
  253. }
  254. @Override
  255. public void onComplete() {
  256. }
  257. });
  258. }
  259. private boolean isInCallWithVideo(int callFlag) {
  260. return (Participant.ParticipantFlags.IN_CALL_WITH_VIDEO.getValue() == callFlag
  261. || Participant.ParticipantFlags.IN_CALL_WITH_AUDIO_AND_VIDEO.getValue() == callFlag);
  262. }
  263. private void runAllThings() {
  264. if (conversationNameTextView != null) {
  265. conversationNameTextView.setText(currentConversation.getDisplayName());
  266. }
  267. // TODO: load avatar, but don't block UI!
  268. // loadAvatar();
  269. checkIfAnyParticipantsRemainInRoom();
  270. showAnswerControls();
  271. }
  272. @SuppressLint({"LongLogTag"})
  273. @Override
  274. protected void onViewBound(@NonNull View view) {
  275. super.onViewBound(view);
  276. String callDescriptionWithoutTypeInfo =
  277. String.format(
  278. getResources().getString(R.string.nc_call_unknown),
  279. getResources().getString(R.string.nc_app_product_name));
  280. incomingCallVoiceOrVideoTextView.setText(callDescriptionWithoutTypeInfo);
  281. renderScript = RenderScript.create(getActivity());
  282. if (handler == null) {
  283. handler = new Handler();
  284. try {
  285. cache.evictAll();
  286. } catch (IOException e) {
  287. Log.e(TAG, "Failed to evict cache");
  288. }
  289. }
  290. if (currentConversation == null) {
  291. handleFromNotification();
  292. } else {
  293. runAllThings();
  294. }
  295. if (DoNotDisturbUtils.INSTANCE.shouldPlaySound()) {
  296. playRingtoneSound();
  297. }
  298. }
  299. @Subscribe(threadMode = ThreadMode.MAIN)
  300. public void onMessageEvent(ConfigurationChangeEvent configurationChangeEvent) {
  301. ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) avatarImageView.getLayoutParams();
  302. int dimen = (int) getResources().getDimension(R.dimen.avatar_size_very_big);
  303. layoutParams.width = dimen;
  304. layoutParams.height = dimen;
  305. avatarImageView.setLayoutParams(layoutParams);
  306. }
  307. @Override
  308. protected void onDetach(@NonNull View view) {
  309. super.onDetach(view);
  310. eventBus.unregister(this);
  311. }
  312. @Override
  313. protected void onAttach(@NonNull View view) {
  314. super.onAttach(view);
  315. eventBus.register(this);
  316. }
  317. private void loadAvatar() {
  318. switch (currentConversation.getType()) {
  319. case ROOM_TYPE_ONE_TO_ONE_CALL:
  320. avatarImageView.setVisibility(View.VISIBLE);
  321. ImageRequest imageRequest =
  322. DisplayUtils.getImageRequestForUrl(
  323. ApiUtils.getUrlForAvatarWithName(userBeingCalled.getBaseUrl(),
  324. currentConversation.getName(),
  325. R.dimen.avatar_size_very_big),
  326. null);
  327. ImagePipeline imagePipeline = Fresco.getImagePipeline();
  328. DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, null);
  329. dataSource.subscribe(new BaseBitmapDataSubscriber() {
  330. @Override
  331. protected void onNewResultImpl(@Nullable Bitmap bitmap) {
  332. if (avatarImageView != null) {
  333. avatarImageView.getHierarchy().setImage(new BitmapDrawable(bitmap), 100,
  334. true);
  335. if (getResources() != null) {
  336. incomingTextRelativeLayout.setBackground(
  337. getResources().getDrawable(R.drawable.incoming_gradient));
  338. }
  339. if (AvatarStatusCodeHolder.getInstance().getStatusCode() == 200 ||
  340. AvatarStatusCodeHolder.getInstance().getStatusCode() == 0) {
  341. if (getActivity() != null) {
  342. Bitmap backgroundBitmap = bitmap.copy(bitmap.getConfig(), true);
  343. new BlurPostProcessor(5, getActivity()).process(backgroundBitmap);
  344. backgroundImageView.setImageDrawable(new BitmapDrawable(backgroundBitmap));
  345. }
  346. } else if (AvatarStatusCodeHolder.getInstance().getStatusCode() == 201) {
  347. ColorArt colorArt = new ColorArt(bitmap);
  348. int color = colorArt.getBackgroundColor();
  349. float[] hsv = new float[3];
  350. Color.colorToHSV(color, hsv);
  351. hsv[2] *= 0.75f;
  352. color = Color.HSVToColor(hsv);
  353. backgroundImageView.setImageDrawable(new ColorDrawable(color));
  354. }
  355. }
  356. }
  357. @Override
  358. protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
  359. // unused atm
  360. }
  361. }, UiThreadImmediateExecutorService.getInstance());
  362. break;
  363. case ROOM_GROUP_CALL:
  364. avatarImageView.setImageResource(R.drawable.ic_circular_group);
  365. case ROOM_PUBLIC_CALL:
  366. avatarImageView.setImageResource(R.drawable.ic_circular_group);
  367. break;
  368. default:
  369. }
  370. }
  371. private void endMediaNotifications() {
  372. if (mediaPlayer != null) {
  373. if (mediaPlayer.isPlaying()) {
  374. mediaPlayer.stop();
  375. }
  376. mediaPlayer.release();
  377. mediaPlayer = null;
  378. }
  379. }
  380. @Override
  381. public void onDestroy() {
  382. AvatarStatusCodeHolder.getInstance().setStatusCode(0);
  383. leavingScreen = true;
  384. if (handler != null) {
  385. handler.removeCallbacksAndMessages(null);
  386. handler = null;
  387. }
  388. dispose();
  389. endMediaNotifications();
  390. super.onDestroy();
  391. }
  392. private void dispose() {
  393. if (disposablesList != null) {
  394. for (Disposable disposable : disposablesList) {
  395. if (!disposable.isDisposed()) {
  396. disposable.dispose();
  397. }
  398. }
  399. }
  400. }
  401. @SuppressLint("LongLogTag")
  402. private void playRingtoneSound() {
  403. String callRingtonePreferenceString = appPreferences.getCallRingtoneUri();
  404. Uri ringtoneUri;
  405. if (TextUtils.isEmpty(callRingtonePreferenceString)) {
  406. // play default sound
  407. ringtoneUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() +
  408. "/raw/librem_by_feandesign_call");
  409. } else {
  410. try {
  411. RingtoneSettings ringtoneSettings = LoganSquare.parse(
  412. callRingtonePreferenceString, RingtoneSettings.class);
  413. ringtoneUri = ringtoneSettings.getRingtoneUri();
  414. } catch (IOException e) {
  415. Log.e(TAG, "Failed to parse ringtone settings");
  416. ringtoneUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() +
  417. "/raw/librem_by_feandesign_call");
  418. }
  419. }
  420. if (ringtoneUri != null && getActivity() != null) {
  421. mediaPlayer = new MediaPlayer();
  422. try {
  423. mediaPlayer.setDataSource(getActivity(), ringtoneUri);
  424. mediaPlayer.setLooping(true);
  425. AudioAttributes audioAttributes = new AudioAttributes
  426. .Builder()
  427. .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
  428. .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
  429. .build();
  430. mediaPlayer.setAudioAttributes(audioAttributes);
  431. mediaPlayer.setOnPreparedListener(mp -> mediaPlayer.start());
  432. mediaPlayer.prepareAsync();
  433. } catch (IOException e) {
  434. Log.e(TAG, "Failed to set data source");
  435. }
  436. }
  437. }
  438. }