ChatController.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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.content.ClipData;
  22. import android.content.ClipboardManager;
  23. import android.content.Context;
  24. import android.content.Intent;
  25. import android.graphics.Color;
  26. import android.graphics.drawable.ColorDrawable;
  27. import android.graphics.drawable.Drawable;
  28. import android.os.Bundle;
  29. import android.os.Handler;
  30. import android.support.annotation.NonNull;
  31. import android.support.v7.widget.LinearLayoutManager;
  32. import android.support.v7.widget.RecyclerView;
  33. import android.text.TextUtils;
  34. import android.util.Log;
  35. import android.view.LayoutInflater;
  36. import android.view.Menu;
  37. import android.view.MenuInflater;
  38. import android.view.MenuItem;
  39. import android.view.View;
  40. import android.view.ViewGroup;
  41. import android.view.inputmethod.EditorInfo;
  42. import android.widget.AbsListView;
  43. import android.widget.ImageView;
  44. import com.bluelinelabs.conductor.RouterTransaction;
  45. import com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler;
  46. import com.bumptech.glide.load.engine.DiskCacheStrategy;
  47. import com.bumptech.glide.load.resource.bitmap.CircleCrop;
  48. import com.bumptech.glide.request.RequestOptions;
  49. import com.nextcloud.talk.R;
  50. import com.nextcloud.talk.activities.CallActivity;
  51. import com.nextcloud.talk.adapters.messages.MagicIncomingTextMessageViewHolder;
  52. import com.nextcloud.talk.adapters.messages.MagicOutcomingTextMessageViewHolder;
  53. import com.nextcloud.talk.api.NcApi;
  54. import com.nextcloud.talk.application.NextcloudTalkApplication;
  55. import com.nextcloud.talk.callbacks.MentionAutocompleteCallback;
  56. import com.nextcloud.talk.controllers.base.BaseController;
  57. import com.nextcloud.talk.models.database.UserEntity;
  58. import com.nextcloud.talk.models.json.call.Call;
  59. import com.nextcloud.talk.models.json.call.CallOverall;
  60. import com.nextcloud.talk.models.json.chat.ChatMessage;
  61. import com.nextcloud.talk.models.json.chat.ChatOverall;
  62. import com.nextcloud.talk.models.json.generic.GenericOverall;
  63. import com.nextcloud.talk.models.json.mention.Mention;
  64. import com.nextcloud.talk.presenters.MentionAutocompletePresenter;
  65. import com.nextcloud.talk.utils.ApiUtils;
  66. import com.nextcloud.talk.utils.KeyboardUtils;
  67. import com.nextcloud.talk.utils.bundle.BundleKeys;
  68. import com.nextcloud.talk.utils.database.user.UserUtils;
  69. import com.nextcloud.talk.utils.glide.GlideApp;
  70. import com.otaliastudios.autocomplete.Autocomplete;
  71. import com.otaliastudios.autocomplete.AutocompleteCallback;
  72. import com.otaliastudios.autocomplete.AutocompletePresenter;
  73. import com.otaliastudios.autocomplete.CharPolicy;
  74. import com.stfalcon.chatkit.commons.ImageLoader;
  75. import com.stfalcon.chatkit.commons.models.IMessage;
  76. import com.stfalcon.chatkit.messages.MessageInput;
  77. import com.stfalcon.chatkit.messages.MessagesList;
  78. import com.stfalcon.chatkit.messages.MessagesListAdapter;
  79. import com.stfalcon.chatkit.utils.DateFormatter;
  80. import com.webianks.library.PopupBubble;
  81. import org.parceler.Parcels;
  82. import java.io.IOException;
  83. import java.util.ArrayList;
  84. import java.util.Date;
  85. import java.util.HashMap;
  86. import java.util.List;
  87. import java.util.Map;
  88. import java.util.Objects;
  89. import java.util.concurrent.TimeUnit;
  90. import javax.inject.Inject;
  91. import autodagger.AutoInjector;
  92. import butterknife.BindView;
  93. import io.reactivex.Observer;
  94. import io.reactivex.android.schedulers.AndroidSchedulers;
  95. import io.reactivex.disposables.Disposable;
  96. import io.reactivex.schedulers.Schedulers;
  97. import okhttp3.Cache;
  98. import retrofit2.Response;
  99. @AutoInjector(NextcloudTalkApplication.class)
  100. public class ChatController extends BaseController implements MessagesListAdapter.OnLoadMoreListener,
  101. MessagesListAdapter.Formatter<Date>, MessagesListAdapter.OnMessageLongClickListener{
  102. private static final String TAG = "ChatController";
  103. @Inject
  104. NcApi ncApi;
  105. @Inject
  106. UserUtils userUtils;
  107. @Inject
  108. Cache cache;
  109. @BindView(R.id.input)
  110. MessageInput messageInput;
  111. @BindView(R.id.messagesList)
  112. MessagesList messagesList;
  113. @BindView(R.id.popupBubble)
  114. PopupBubble popupBubble;
  115. private List<Disposable> disposableList = new ArrayList<>();
  116. private String conversationName;
  117. private String roomToken;
  118. private UserEntity conversationUser;
  119. private String roomPassword;
  120. private String credentials;
  121. private String baseUrl;
  122. private Call currentCall;
  123. private boolean inChat = false;
  124. private boolean historyRead = false;
  125. private int globalLastKnownFutureMessageId = -1;
  126. private int globalLastKnownPastMessageId = -1;
  127. private MessagesListAdapter<ChatMessage> adapter;
  128. private Autocomplete mentionAutocomplete;
  129. private LinearLayoutManager layoutManager;
  130. private boolean lookingIntoFuture = false;
  131. private int newMessagesCount = 0;
  132. /*
  133. TODO:
  134. - check push notifications
  135. */
  136. public ChatController(Bundle args) {
  137. super(args);
  138. setHasOptionsMenu(true);
  139. this.conversationName = args.getString(BundleKeys.KEY_CONVERSATION_NAME);
  140. if (args.containsKey(BundleKeys.KEY_USER_ENTITY)) {
  141. this.conversationUser = Parcels.unwrap(args.getParcelable(BundleKeys.KEY_USER_ENTITY));
  142. }
  143. this.roomToken = args.getString(BundleKeys.KEY_ROOM_TOKEN);
  144. if (args.containsKey(BundleKeys.KEY_ACTIVE_CONVERSATION)) {
  145. this.currentCall = Parcels.unwrap(args.getParcelable(BundleKeys.KEY_ACTIVE_CONVERSATION));
  146. }
  147. this.baseUrl = args.getString(BundleKeys.KEY_MODIFIED_BASE_URL, "");
  148. }
  149. @Override
  150. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  151. return inflater.inflate(R.layout.controller_chat, container, false);
  152. }
  153. @Override
  154. protected void onViewBound(@NonNull View view) {
  155. super.onViewBound(view);
  156. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  157. boolean adapterWasNull = false;
  158. if (adapter == null) {
  159. try {
  160. cache.evictAll();
  161. } catch (IOException e) {
  162. Log.e(TAG, "Failed to evict cache");
  163. }
  164. adapterWasNull = true;
  165. MessagesListAdapter.HoldersConfig holdersConfig = new MessagesListAdapter.HoldersConfig();
  166. holdersConfig.setIncoming(MagicIncomingTextMessageViewHolder.class,
  167. R.layout.item_custom_incoming_text_message);
  168. holdersConfig.setOutcoming(MagicOutcomingTextMessageViewHolder.class,
  169. R.layout.item_custom_outcoming_text_message);
  170. adapter = new MessagesListAdapter<>(conversationUser.getUserId(), holdersConfig, new ImageLoader() {
  171. @Override
  172. public void loadImage(ImageView imageView, String url) {
  173. GlideApp.with(NextcloudTalkApplication.getSharedApplication().getApplicationContext())
  174. .asBitmap()
  175. .diskCacheStrategy(DiskCacheStrategy.NONE)
  176. .load(url)
  177. .centerInside()
  178. .override(imageView.getMeasuredWidth(), imageView.getMeasuredHeight())
  179. .apply(RequestOptions.bitmapTransform(new CircleCrop()))
  180. .into(imageView);
  181. }
  182. });
  183. }
  184. messagesList.setAdapter(adapter);
  185. adapter.setLoadMoreListener(this);
  186. adapter.setDateHeadersFormatter(this::format);
  187. adapter.setOnMessageLongClickListener(this);
  188. layoutManager = (LinearLayoutManager) messagesList.getLayoutManager();
  189. popupBubble.setRecyclerView(messagesList);
  190. popupBubble.setPopupBubbleListener(context -> {
  191. if (newMessagesCount != 0) {
  192. new Handler().postDelayed(() -> messagesList.smoothScrollToPosition(newMessagesCount - 1), 200);
  193. }
  194. });
  195. messagesList.addOnScrollListener(new RecyclerView.OnScrollListener() {
  196. @Override
  197. public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
  198. super.onScrollStateChanged(recyclerView, newState);
  199. if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
  200. if (newMessagesCount != 0) {
  201. if (layoutManager.findFirstCompletelyVisibleItemPosition() < newMessagesCount) {
  202. newMessagesCount = 0;
  203. if (popupBubble.isShown()) {
  204. popupBubble.hide();
  205. }
  206. }
  207. }
  208. }
  209. }
  210. });
  211. setupMentionAutocomplete();
  212. messageInput.getInputEditText().setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
  213. messageInput.setInputListener(input -> {
  214. sendMessage(input.toString());
  215. return true;
  216. });
  217. if (adapterWasNull) {
  218. UserEntity currentUser = userUtils.getCurrentUser();
  219. if (conversationUser != null && !currentUser.equals(conversationUser)) {
  220. userUtils.createOrUpdateUser(null,
  221. null, null, null,
  222. null, true, null, currentUser.getId(), null, null)
  223. .subscribe(new Observer<UserEntity>() {
  224. @Override
  225. public void onSubscribe(Disposable d) {
  226. }
  227. @Override
  228. public void onNext(UserEntity userEntity) {
  229. joinRoomWithPassword();
  230. }
  231. @Override
  232. public void onError(Throwable e) {
  233. }
  234. @Override
  235. public void onComplete() {
  236. }
  237. });
  238. } else {
  239. if (conversationUser == null) {
  240. conversationUser = new UserEntity();
  241. conversationUser.setDisplayName(currentUser.getDisplayName());
  242. }
  243. joinRoomWithPassword();
  244. }
  245. }
  246. }
  247. private void setupMentionAutocomplete() {
  248. float elevation = 6f;
  249. Drawable backgroundDrawable = new ColorDrawable(Color.WHITE);
  250. AutocompletePresenter<Mention> presenter = new MentionAutocompletePresenter(getApplicationContext(), roomToken);
  251. AutocompleteCallback<Mention> callback = new MentionAutocompleteCallback();
  252. mentionAutocomplete = Autocomplete.<Mention>on(messageInput.getInputEditText())
  253. .with(elevation)
  254. .with(backgroundDrawable)
  255. .with(new CharPolicy('@'))
  256. .with(presenter)
  257. .with(callback)
  258. .build();
  259. }
  260. @Override
  261. protected void onAttach(@NonNull View view) {
  262. super.onAttach(view);
  263. if (getActionBar() != null) {
  264. getActionBar().setDisplayHomeAsUpEnabled(true);
  265. }
  266. if (mentionAutocomplete != null && mentionAutocomplete.isPopupShowing()) {
  267. mentionAutocomplete.dismissPopup();
  268. }
  269. if (getActivity() != null) {
  270. new KeyboardUtils(getActivity(), getView());
  271. }
  272. }
  273. @Override
  274. protected String getTitle() {
  275. return conversationName;
  276. }
  277. @Override
  278. public boolean handleBack() {
  279. if (getRouter().hasRootController()) {
  280. getRouter().popToRoot(new HorizontalChangeHandler());
  281. } else {
  282. getRouter().setRoot(RouterTransaction.with(new MagicBottomNavigationController())
  283. .pushChangeHandler(new HorizontalChangeHandler())
  284. .popChangeHandler(new HorizontalChangeHandler()));
  285. }
  286. return true;
  287. }
  288. @Override
  289. public void onDestroy() {
  290. inChat = false;
  291. dispose();
  292. super.onDestroy();
  293. }
  294. private void dispose() {
  295. Disposable disposable;
  296. for (int i = 0; i < disposableList.size(); i++) {
  297. if ((disposable = disposableList.get(i)).isDisposed()) {
  298. disposable.dispose();
  299. }
  300. }
  301. }
  302. private void joinRoomWithPassword() {
  303. String password = "";
  304. if (TextUtils.isEmpty(roomPassword)) {
  305. password = roomPassword;
  306. }
  307. if (TextUtils.isEmpty(baseUrl)) {
  308. baseUrl = conversationUser.getBaseUrl();
  309. }
  310. if (TextUtils.isEmpty(conversationUser.getUserId())) {
  311. credentials = null;
  312. } else {
  313. credentials = ApiUtils.getCredentials(conversationUser.getUserId(), conversationUser.getToken());
  314. }
  315. if (currentCall == null) {
  316. ncApi.joinRoom(credentials, ApiUtils.getUrlForRoomParticipants(baseUrl, roomToken), password)
  317. .subscribeOn(Schedulers.newThread())
  318. .observeOn(AndroidSchedulers.mainThread())
  319. .retry(3)
  320. .subscribe(new Observer<CallOverall>() {
  321. @Override
  322. public void onSubscribe(Disposable d) {
  323. disposableList.add(d);
  324. }
  325. @Override
  326. public void onNext(CallOverall callOverall) {
  327. inChat = true;
  328. startPing();
  329. pullChatMessages(0);
  330. currentCall = callOverall.getOcs().getData();
  331. }
  332. @Override
  333. public void onError(Throwable e) {
  334. }
  335. @Override
  336. public void onComplete() {
  337. }
  338. });
  339. } else {
  340. inChat = true;
  341. startPing();
  342. pullChatMessages(0);
  343. }
  344. }
  345. private void sendMessage(String message) {
  346. Map<String, String> fieldMap = new HashMap<>();
  347. fieldMap.put("message", message);
  348. fieldMap.put("actorDisplayName", conversationUser.getDisplayName());
  349. ncApi.sendChatMessage(ApiUtils.getCredentials(conversationUser.getUserId(), conversationUser.getToken()),
  350. ApiUtils.getUrlForChat(conversationUser.getBaseUrl(), roomToken), fieldMap)
  351. .subscribeOn(Schedulers.newThread())
  352. .observeOn(AndroidSchedulers.mainThread())
  353. .retry(3, observable -> inChat)
  354. .subscribe(new Observer<GenericOverall>() {
  355. @Override
  356. public void onSubscribe(Disposable d) {
  357. }
  358. @Override
  359. public void onNext(GenericOverall genericOverall) {
  360. if (popupBubble.isShown()) {
  361. popupBubble.hide();
  362. }
  363. messagesList.smoothScrollToPosition(0);
  364. }
  365. @Override
  366. public void onError(Throwable e) {
  367. }
  368. @Override
  369. public void onComplete() {
  370. }
  371. });
  372. }
  373. private void startPing() {
  374. ncApi.pingCall(credentials, ApiUtils.getUrlForCallPing(baseUrl, roomToken))
  375. .subscribeOn(Schedulers.newThread())
  376. .observeOn(AndroidSchedulers.mainThread())
  377. .repeatWhen(observable -> observable.delay(5000, TimeUnit.MILLISECONDS))
  378. .takeWhile(observable -> inChat)
  379. .retry(3, observable -> inChat)
  380. .subscribe(new Observer<GenericOverall>() {
  381. @Override
  382. public void onSubscribe(Disposable d) {
  383. disposableList.add(d);
  384. }
  385. @Override
  386. public void onNext(GenericOverall genericOverall) {
  387. }
  388. @Override
  389. public void onError(Throwable e) {
  390. }
  391. @Override
  392. public void onComplete() {
  393. }
  394. });
  395. }
  396. private void pullChatMessages(int lookIntoFuture) {
  397. if (!lookingIntoFuture && lookIntoFuture == 1) {
  398. lookingIntoFuture = true;
  399. }
  400. Map<String, Integer> fieldMap = new HashMap<>();
  401. fieldMap.put("lookIntoFuture", lookIntoFuture);
  402. fieldMap.put("limit", 25);
  403. int lastKnown;
  404. if (lookIntoFuture == 1) {
  405. lastKnown = globalLastKnownFutureMessageId;
  406. } else {
  407. lastKnown = globalLastKnownPastMessageId;
  408. }
  409. if (lastKnown != -1) {
  410. fieldMap.put("lastKnownMessageId", lastKnown);
  411. }
  412. if (lookIntoFuture == 1) {
  413. ncApi.pullChatMessages(ApiUtils.getCredentials(conversationUser.getUserId(), conversationUser.getToken()),
  414. ApiUtils.getUrlForChat(conversationUser.getBaseUrl(), roomToken), fieldMap)
  415. .subscribeOn(Schedulers.newThread())
  416. .observeOn(AndroidSchedulers.mainThread())
  417. .takeWhile(observable -> inChat)
  418. .retry(3, observable -> inChat)
  419. .subscribe(new Observer<Response>() {
  420. @Override
  421. public void onSubscribe(Disposable d) {
  422. disposableList.add(d);
  423. }
  424. @Override
  425. public void onNext(Response response) {
  426. processMessages(response, true);
  427. }
  428. @Override
  429. public void onError(Throwable e) {
  430. }
  431. @Override
  432. public void onComplete() {
  433. pullChatMessages(1);
  434. }
  435. });
  436. } else {
  437. ncApi.pullChatMessages(ApiUtils.getCredentials(conversationUser.getUserId(), conversationUser.getToken()),
  438. ApiUtils.getUrlForChat(conversationUser.getBaseUrl(), roomToken), fieldMap)
  439. .subscribeOn(Schedulers.newThread())
  440. .observeOn(AndroidSchedulers.mainThread())
  441. .retry(3, observable -> inChat)
  442. .subscribe(new Observer<Response>() {
  443. @Override
  444. public void onSubscribe(Disposable d) {
  445. disposableList.add(d);
  446. }
  447. @Override
  448. public void onNext(Response response) {
  449. processMessages(response, false);
  450. }
  451. @Override
  452. public void onError(Throwable e) {
  453. }
  454. @Override
  455. public void onComplete() {
  456. }
  457. });
  458. }
  459. }
  460. private void processMessages(Response response, boolean isFromTheFuture) {
  461. if (response.code() == 200) {
  462. ChatOverall chatOverall = (ChatOverall) response.body();
  463. List<ChatMessage> chatMessageList = chatOverall.getOcs().getData();
  464. if (!isFromTheFuture) {
  465. for (int i = 0; i < chatMessageList.size(); i++) {
  466. chatMessageList.get(i).setBaseUrl(conversationUser.getBaseUrl());
  467. if (globalLastKnownPastMessageId == -1 || chatMessageList.get(i).getJsonMessageId() <
  468. globalLastKnownPastMessageId) {
  469. globalLastKnownPastMessageId = chatMessageList.get(i).getJsonMessageId();
  470. }
  471. if (globalLastKnownFutureMessageId == -1) {
  472. if (chatMessageList.get(i).getJsonMessageId() > globalLastKnownFutureMessageId) {
  473. globalLastKnownFutureMessageId = chatMessageList.get(i).getJsonMessageId();
  474. }
  475. }
  476. }
  477. adapter.addToEnd(chatMessageList, false);
  478. } else {
  479. for (int i = 0; i < chatMessageList.size(); i++) {
  480. chatMessageList.get(i).setBaseUrl(conversationUser.getBaseUrl());
  481. boolean shouldScroll = layoutManager.findFirstVisibleItemPosition() == 0 ||
  482. adapter.getItemCount() == 0;
  483. if (!shouldScroll) {
  484. if (!popupBubble.isShown()) {
  485. newMessagesCount = 1;
  486. popupBubble.show();
  487. } else if (popupBubble.isShown()) {
  488. newMessagesCount++;
  489. }
  490. } else {
  491. newMessagesCount = 0;
  492. }
  493. adapter.addToStart(chatMessageList.get(i), shouldScroll);
  494. }
  495. String xChatLastGivenHeader;
  496. if (response.headers().size() > 0 && !TextUtils.isEmpty((xChatLastGivenHeader = response.headers().get
  497. ("X-Chat-Last-Given")))) {
  498. globalLastKnownFutureMessageId = Integer.parseInt(xChatLastGivenHeader);
  499. }
  500. }
  501. if (!lookingIntoFuture) {
  502. pullChatMessages(1);
  503. }
  504. } else if (response.code() == 304 && !isFromTheFuture) {
  505. historyRead = true;
  506. if (!lookingIntoFuture) {
  507. pullChatMessages(1);
  508. }
  509. }
  510. }
  511. @Override
  512. public void onLoadMore(int page, int totalItemsCount) {
  513. if (!historyRead) {
  514. pullChatMessages(0);
  515. }
  516. }
  517. @Override
  518. public String format(Date date) {
  519. if (DateFormatter.isToday(date)) {
  520. return getResources().getString(R.string.nc_date_header_today);
  521. } else if (DateFormatter.isYesterday(date)) {
  522. return getResources().getString(R.string.nc_date_header_yesterday);
  523. } else {
  524. return DateFormatter.format(date, DateFormatter.Template.STRING_DAY_MONTH_YEAR);
  525. }
  526. }
  527. @Override
  528. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  529. super.onCreateOptionsMenu(menu, inflater);
  530. inflater.inflate(R.menu.menu_conversation, menu);
  531. }
  532. @Override
  533. public boolean onOptionsItemSelected(@NonNull MenuItem item) {
  534. switch (item.getItemId()) {
  535. case android.R.id.home:
  536. inChat = false;
  537. if (getRouter().hasRootController()) {
  538. getRouter().popToRoot(new HorizontalChangeHandler());
  539. } else {
  540. getRouter().setRoot(RouterTransaction.with(new MagicBottomNavigationController())
  541. .pushChangeHandler(new HorizontalChangeHandler())
  542. .popChangeHandler(new HorizontalChangeHandler()));
  543. }
  544. return true;
  545. case R.id.conversation_video_call:
  546. startActivity(Objects.requireNonNull(getIntentForCall(false)));
  547. return true;
  548. case R.id.conversation_voice_call:
  549. startActivity(Objects.requireNonNull(getIntentForCall(true)));
  550. return true;
  551. default:
  552. return super.onOptionsItemSelected(item);
  553. }
  554. }
  555. private Intent getIntentForCall(boolean isVoiceOnlyCall) {
  556. if (currentCall != null && !TextUtils.isEmpty(currentCall.getSessionId())) {
  557. Bundle bundle = new Bundle();
  558. bundle.putString(BundleKeys.KEY_ROOM_TOKEN, roomToken);
  559. bundle.putParcelable(BundleKeys.KEY_USER_ENTITY, Parcels.wrap(conversationUser));
  560. bundle.putString(BundleKeys.KEY_CALL_SESSION, currentCall.getSessionId());
  561. if (isVoiceOnlyCall) {
  562. bundle.putBoolean(BundleKeys.KEY_CALL_VOICE_ONLY, true);
  563. }
  564. Intent callIntent = new Intent(getActivity(), CallActivity.class);
  565. callIntent.putExtras(bundle);
  566. return callIntent;
  567. } else {
  568. return null;
  569. }
  570. }
  571. @Override
  572. public void onMessageLongClick(IMessage message) {
  573. if (getActivity() != null) {
  574. ClipboardManager clipboardManager = (android.content.ClipboardManager)
  575. getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
  576. ClipData clipData = android.content.ClipData.newPlainText(
  577. getResources().getString(R.string.nc_app_name), message.getText());
  578. if (clipboardManager != null) {
  579. clipboardManager.setPrimaryClip(clipData);
  580. }
  581. }
  582. }
  583. }