ChatController.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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.Bitmap;
  26. import android.graphics.Color;
  27. import android.graphics.drawable.ColorDrawable;
  28. import android.graphics.drawable.Drawable;
  29. import android.os.Bundle;
  30. import android.os.Handler;
  31. import android.support.annotation.NonNull;
  32. import android.support.annotation.Nullable;
  33. import android.support.v7.widget.LinearLayoutManager;
  34. import android.support.v7.widget.RecyclerView;
  35. import android.text.TextUtils;
  36. import android.util.Log;
  37. import android.view.LayoutInflater;
  38. import android.view.Menu;
  39. import android.view.MenuInflater;
  40. import android.view.MenuItem;
  41. import android.view.View;
  42. import android.view.ViewGroup;
  43. import android.view.inputmethod.EditorInfo;
  44. import android.widget.AbsListView;
  45. import android.widget.ImageView;
  46. import com.amulyakhare.textdrawable.TextDrawable;
  47. import com.bumptech.glide.load.DataSource;
  48. import com.bumptech.glide.load.engine.DiskCacheStrategy;
  49. import com.bumptech.glide.load.engine.GlideException;
  50. import com.bumptech.glide.load.resource.bitmap.CircleCrop;
  51. import com.bumptech.glide.request.RequestListener;
  52. import com.bumptech.glide.request.RequestOptions;
  53. import com.bumptech.glide.request.target.Target;
  54. import com.nextcloud.talk.R;
  55. import com.nextcloud.talk.activities.MagicCallActivity;
  56. import com.nextcloud.talk.adapters.messages.MagicIncomingTextMessageViewHolder;
  57. import com.nextcloud.talk.adapters.messages.MagicOutcomingTextMessageViewHolder;
  58. import com.nextcloud.talk.api.NcApi;
  59. import com.nextcloud.talk.application.NextcloudTalkApplication;
  60. import com.nextcloud.talk.callbacks.MentionAutocompleteCallback;
  61. import com.nextcloud.talk.controllers.base.BaseController;
  62. import com.nextcloud.talk.models.database.UserEntity;
  63. import com.nextcloud.talk.models.json.call.Call;
  64. import com.nextcloud.talk.models.json.call.CallOverall;
  65. import com.nextcloud.talk.models.json.chat.ChatMessage;
  66. import com.nextcloud.talk.models.json.chat.ChatOverall;
  67. import com.nextcloud.talk.models.json.generic.GenericOverall;
  68. import com.nextcloud.talk.models.json.mention.Mention;
  69. import com.nextcloud.talk.models.json.rooms.Room;
  70. import com.nextcloud.talk.models.json.rooms.RoomOverall;
  71. import com.nextcloud.talk.models.json.rooms.RoomsOverall;
  72. import com.nextcloud.talk.presenters.MentionAutocompletePresenter;
  73. import com.nextcloud.talk.utils.ApiUtils;
  74. import com.nextcloud.talk.utils.singletons.ApplicationWideCurrentRoomHolder;
  75. import com.nextcloud.talk.utils.KeyboardUtils;
  76. import com.nextcloud.talk.utils.bundle.BundleKeys;
  77. import com.nextcloud.talk.utils.database.user.UserUtils;
  78. import com.nextcloud.talk.utils.glide.GlideApp;
  79. import com.otaliastudios.autocomplete.Autocomplete;
  80. import com.otaliastudios.autocomplete.AutocompleteCallback;
  81. import com.otaliastudios.autocomplete.AutocompletePresenter;
  82. import com.otaliastudios.autocomplete.CharPolicy;
  83. import com.stfalcon.chatkit.commons.ImageLoader;
  84. import com.stfalcon.chatkit.commons.models.IMessage;
  85. import com.stfalcon.chatkit.messages.MessageInput;
  86. import com.stfalcon.chatkit.messages.MessagesList;
  87. import com.stfalcon.chatkit.messages.MessagesListAdapter;
  88. import com.stfalcon.chatkit.utils.DateFormatter;
  89. import com.webianks.library.PopupBubble;
  90. import org.parceler.Parcels;
  91. import java.io.IOException;
  92. import java.lang.reflect.Field;
  93. import java.util.ArrayList;
  94. import java.util.Date;
  95. import java.util.HashMap;
  96. import java.util.List;
  97. import java.util.Map;
  98. import java.util.concurrent.TimeUnit;
  99. import javax.inject.Inject;
  100. import autodagger.AutoInjector;
  101. import butterknife.BindView;
  102. import io.reactivex.Observer;
  103. import io.reactivex.android.schedulers.AndroidSchedulers;
  104. import io.reactivex.disposables.Disposable;
  105. import io.reactivex.schedulers.Schedulers;
  106. import okhttp3.Cache;
  107. import retrofit2.HttpException;
  108. import retrofit2.Response;
  109. @AutoInjector(NextcloudTalkApplication.class)
  110. public class ChatController extends BaseController implements MessagesListAdapter.OnLoadMoreListener,
  111. MessagesListAdapter.Formatter<Date>, MessagesListAdapter.OnMessageLongClickListener {
  112. private static final String TAG = "ChatController";
  113. @Inject
  114. NcApi ncApi;
  115. @Inject
  116. UserUtils userUtils;
  117. @Inject
  118. Cache cache;
  119. @BindView(R.id.messagesListView)
  120. MessagesList messagesListView;
  121. @BindView(R.id.messageInputView)
  122. MessageInput messageInputView;
  123. @BindView(R.id.popupBubbleView)
  124. PopupBubble popupBubble;
  125. private List<Disposable> disposableList = new ArrayList<>();
  126. private String conversationName;
  127. private String roomToken;
  128. private UserEntity conversationUser;
  129. private String roomPassword;
  130. private String credentials;
  131. private String baseUrl;
  132. private Call currentCall;
  133. private boolean inChat = false;
  134. private boolean historyRead = false;
  135. private int globalLastKnownFutureMessageId = -1;
  136. private int globalLastKnownPastMessageId = -1;
  137. private MessagesListAdapter<ChatMessage> adapter;
  138. private String myFirstMessage;
  139. private Autocomplete mentionAutocomplete;
  140. private LinearLayoutManager layoutManager;
  141. private boolean lookingIntoFuture = false;
  142. private int newMessagesCount = 0;
  143. private Boolean startCallFromNotification;
  144. private String roomId;
  145. private boolean voiceOnly;
  146. public ChatController(Bundle args) {
  147. super(args);
  148. setHasOptionsMenu(true);
  149. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  150. UserEntity currentUser = userUtils.getCurrentUser();
  151. this.conversationName = args.getString(BundleKeys.KEY_CONVERSATION_NAME, "");
  152. if (args.containsKey(BundleKeys.KEY_USER_ENTITY)) {
  153. this.conversationUser = Parcels.unwrap(args.getParcelable(BundleKeys.KEY_USER_ENTITY));
  154. } else {
  155. this.conversationUser = currentUser;
  156. }
  157. this.roomId = args.getString(BundleKeys.KEY_ROOM_ID, "");
  158. this.roomToken = args.getString(BundleKeys.KEY_ROOM_TOKEN, "");
  159. if (args.containsKey(BundleKeys.KEY_ACTIVE_CONVERSATION)) {
  160. this.currentCall = Parcels.unwrap(args.getParcelable(BundleKeys.KEY_ACTIVE_CONVERSATION));
  161. }
  162. this.baseUrl = args.getString(BundleKeys.KEY_MODIFIED_BASE_URL, "");
  163. if (!TextUtils.isEmpty(baseUrl)) {
  164. conversationUser.setBaseUrl(baseUrl);
  165. conversationUser.setUserId("?");
  166. conversationUser.setDisplayName(currentUser.getDisplayName());
  167. } else {
  168. baseUrl = conversationUser.getBaseUrl();
  169. }
  170. this.roomPassword = args.getString(BundleKeys.KEY_CONVERSATION_PASSWORD, "");
  171. if (conversationUser.getUserId().equals("?")) {
  172. credentials = null;
  173. } else {
  174. credentials = ApiUtils.getCredentials(conversationUser.getUserId(), conversationUser.getToken());
  175. }
  176. this.startCallFromNotification = args.getBoolean(BundleKeys.KEY_FROM_NOTIFICATION_START_CALL);
  177. this.voiceOnly = args.getBoolean(BundleKeys.KEY_CALL_VOICE_ONLY, false);
  178. }
  179. private void getRoomInfo() {
  180. ncApi.getRoom(credentials, ApiUtils.getRoom(baseUrl, roomToken))
  181. .subscribeOn(Schedulers.newThread())
  182. .observeOn(AndroidSchedulers.mainThread())
  183. .subscribe(new Observer<RoomOverall>() {
  184. @Override
  185. public void onSubscribe(Disposable d) {
  186. }
  187. @Override
  188. public void onNext(RoomOverall roomOverall) {
  189. conversationName = roomOverall.getOcs().getData().getDisplayName();
  190. setTitle();
  191. setupMentionAutocomplete();
  192. joinRoomWithPassword();
  193. }
  194. @Override
  195. public void onError(Throwable e) {
  196. }
  197. @Override
  198. public void onComplete() {
  199. }
  200. });
  201. }
  202. private void handleFromNotification() {
  203. ncApi.getRooms(credentials, ApiUtils.getUrlForGetRooms(baseUrl))
  204. .subscribeOn(Schedulers.newThread())
  205. .observeOn(AndroidSchedulers.mainThread())
  206. .subscribe(new Observer<RoomsOverall>() {
  207. @Override
  208. public void onSubscribe(Disposable d) {
  209. }
  210. @Override
  211. public void onNext(RoomsOverall roomsOverall) {
  212. for (Room room : roomsOverall.getOcs().getData()) {
  213. if (roomId.equals(room.getRoomId())) {
  214. roomToken = room.getToken();
  215. conversationName = room.getDisplayName();
  216. setTitle();
  217. break;
  218. }
  219. }
  220. if (!TextUtils.isEmpty(roomToken)) {
  221. setupMentionAutocomplete();
  222. joinRoomWithPassword();
  223. }
  224. }
  225. @Override
  226. public void onError(Throwable e) {
  227. }
  228. @Override
  229. public void onComplete() {
  230. }
  231. });
  232. }
  233. @Override
  234. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  235. return inflater.inflate(R.layout.controller_chat, container, false);
  236. }
  237. @Override
  238. protected void onViewBound(@NonNull View view) {
  239. super.onViewBound(view);
  240. getActionBar().show();
  241. boolean adapterWasNull = false;
  242. if (adapter == null) {
  243. try {
  244. cache.evictAll();
  245. } catch (IOException e) {
  246. Log.e(TAG, "Failed to evict cache");
  247. }
  248. adapterWasNull = true;
  249. MessagesListAdapter.HoldersConfig holdersConfig = new MessagesListAdapter.HoldersConfig();
  250. holdersConfig.setIncoming(MagicIncomingTextMessageViewHolder.class,
  251. R.layout.item_custom_incoming_text_message);
  252. holdersConfig.setOutcoming(MagicOutcomingTextMessageViewHolder.class,
  253. R.layout.item_custom_outcoming_text_message);
  254. adapter = new MessagesListAdapter<>(conversationUser.getUserId(), holdersConfig, new ImageLoader() {
  255. @Override
  256. public void loadImage(ImageView imageView, String url) {
  257. GlideApp.with(NextcloudTalkApplication.getSharedApplication().getApplicationContext())
  258. .asBitmap()
  259. .diskCacheStrategy(DiskCacheStrategy.NONE)
  260. .load(url)
  261. .centerInside()
  262. .override(imageView.getMeasuredWidth(), imageView.getMeasuredHeight())
  263. .apply(RequestOptions.bitmapTransform(new CircleCrop()))
  264. .listener(new RequestListener<Bitmap>() {
  265. @Override
  266. public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
  267. TextDrawable drawable = TextDrawable.builder().beginConfig().bold()
  268. .width(imageView.getMeasuredWidth()).height(imageView.getMeasuredHeight())
  269. .endConfig().buildRound("?", getResources().getColor(R.color.nc_grey));
  270. imageView.setImageDrawable(drawable);
  271. return true;
  272. }
  273. @Override
  274. public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
  275. return false;
  276. }
  277. })
  278. .into(imageView);
  279. }
  280. });
  281. }
  282. messagesListView.setAdapter(adapter);
  283. adapter.setLoadMoreListener(this);
  284. adapter.setDateHeadersFormatter(this::format);
  285. adapter.setOnMessageLongClickListener(this);
  286. layoutManager = (LinearLayoutManager) messagesListView.getLayoutManager();
  287. popupBubble.setRecyclerView(messagesListView);
  288. popupBubble.setPopupBubbleListener(context -> {
  289. if (newMessagesCount != 0) {
  290. int scrollPosition;
  291. if (newMessagesCount - 1 < 0) {
  292. scrollPosition = 0;
  293. } else {
  294. scrollPosition = newMessagesCount - 1;
  295. }
  296. new Handler().postDelayed(() -> messagesListView.smoothScrollToPosition(scrollPosition), 200);
  297. }
  298. });
  299. messagesListView.addOnScrollListener(new RecyclerView.OnScrollListener() {
  300. @Override
  301. public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
  302. super.onScrollStateChanged(recyclerView, newState);
  303. if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
  304. if (newMessagesCount != 0) {
  305. if (layoutManager.findFirstCompletelyVisibleItemPosition() < newMessagesCount) {
  306. newMessagesCount = 0;
  307. if (popupBubble != null && popupBubble.isShown()) {
  308. popupBubble.hide();
  309. }
  310. }
  311. }
  312. }
  313. }
  314. });
  315. messageInputView.getInputEditText().setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
  316. messageInputView.setInputListener(input -> {
  317. sendMessage(input.toString(), 1);
  318. return true;
  319. });
  320. if (adapterWasNull && startCallFromNotification == null) {
  321. getRoomInfo();
  322. } else {
  323. handleFromNotification();
  324. }
  325. }
  326. private void setupMentionAutocomplete() {
  327. float elevation = 6f;
  328. Drawable backgroundDrawable = new ColorDrawable(Color.WHITE);
  329. AutocompletePresenter<Mention> presenter = new MentionAutocompletePresenter(getApplicationContext(), roomToken);
  330. AutocompleteCallback<Mention> callback = new MentionAutocompleteCallback();
  331. if (messageInputView != null && messageInputView.getInputEditText() != null) {
  332. mentionAutocomplete = Autocomplete.<Mention>on(messageInputView.getInputEditText())
  333. .with(elevation)
  334. .with(backgroundDrawable)
  335. .with(new CharPolicy('@'))
  336. .with(presenter)
  337. .with(callback)
  338. .build();
  339. }
  340. }
  341. @Override
  342. protected void onAttach(@NonNull View view) {
  343. super.onAttach(view);
  344. if (getActionBar() != null) {
  345. getActionBar().setDisplayHomeAsUpEnabled(true);
  346. }
  347. ApplicationWideCurrentRoomHolder.getInstance().setCurrentRoomId(roomId);
  348. ApplicationWideCurrentRoomHolder.getInstance().setInCall(false);
  349. ApplicationWideCurrentRoomHolder.getInstance().setUserInRoom(conversationUser);
  350. if (mentionAutocomplete != null && mentionAutocomplete.isPopupShowing()) {
  351. mentionAutocomplete.dismissPopup();
  352. }
  353. if (getActivity() != null) {
  354. new KeyboardUtils(getActivity(), getView());
  355. }
  356. }
  357. @Override
  358. protected String getTitle() {
  359. return conversationName;
  360. }
  361. @Override
  362. public void onDestroy() {
  363. inChat = false;
  364. dispose();
  365. ApplicationWideCurrentRoomHolder.getInstance().clear();
  366. leaveRoom();
  367. }
  368. private void dispose() {
  369. Disposable disposable;
  370. for (int i = 0; i < disposableList.size(); i++) {
  371. if ((disposable = disposableList.get(i)).isDisposed()) {
  372. disposable.dispose();
  373. }
  374. }
  375. }
  376. private void startPing() {
  377. ncApi.pingCall(credentials, ApiUtils.getUrlForCallPing(baseUrl, roomToken))
  378. .subscribeOn(Schedulers.newThread())
  379. .observeOn(AndroidSchedulers.mainThread())
  380. .repeatWhen(observable -> observable.delay(5000, TimeUnit.MILLISECONDS))
  381. .takeWhile(observable -> inChat)
  382. .retry(3, observable -> inChat)
  383. .subscribe(new Observer<GenericOverall>() {
  384. @Override
  385. public void onSubscribe(Disposable d) {
  386. disposableList.add(d);
  387. }
  388. @Override
  389. public void onNext(GenericOverall genericOverall) {
  390. }
  391. @Override
  392. public void onError(Throwable e) {
  393. }
  394. @Override
  395. public void onComplete() {
  396. }
  397. });
  398. }
  399. private void joinRoomWithPassword() {
  400. if (currentCall == null) {
  401. ncApi.joinRoom(credentials, ApiUtils.getUrlForSettingMyselfAsActiveParticipant(baseUrl, roomToken), roomPassword)
  402. .subscribeOn(Schedulers.newThread())
  403. .observeOn(AndroidSchedulers.mainThread())
  404. .retry(3)
  405. .subscribe(new Observer<CallOverall>() {
  406. @Override
  407. public void onSubscribe(Disposable d) {
  408. disposableList.add(d);
  409. }
  410. @Override
  411. public void onNext(CallOverall callOverall) {
  412. inChat = true;
  413. currentCall = callOverall.getOcs().getData();
  414. startPing();
  415. pullChatMessages(0);
  416. if (startCallFromNotification != null && startCallFromNotification) {
  417. startCallFromNotification = false;
  418. startACall(voiceOnly);
  419. }
  420. }
  421. @Override
  422. public void onError(Throwable e) {
  423. }
  424. @Override
  425. public void onComplete() {
  426. }
  427. });
  428. } else {
  429. inChat = true;
  430. startPing();
  431. pullChatMessages(0);
  432. }
  433. }
  434. private void leaveRoom() {
  435. ncApi.leaveRoom(credentials, ApiUtils.getUrlForSettingMyselfAsActiveParticipant(baseUrl, roomToken))
  436. .subscribeOn(Schedulers.newThread())
  437. .observeOn(AndroidSchedulers.mainThread())
  438. .subscribe(new Observer<GenericOverall>() {
  439. @Override
  440. public void onSubscribe(Disposable d) {
  441. }
  442. @Override
  443. public void onNext(GenericOverall genericOverall) {
  444. getRouter().popToRoot();
  445. }
  446. @Override
  447. public void onError(Throwable e) {
  448. }
  449. @Override
  450. public void onComplete() {
  451. }
  452. });
  453. }
  454. private void setSenderId() {
  455. try {
  456. final Field senderId = adapter.getClass().getDeclaredField("senderId");
  457. senderId.setAccessible(true);
  458. senderId.set(adapter, conversationUser.getUserId());
  459. } catch (NoSuchFieldException e) {
  460. Log.e(TAG, "Failed to set sender id");
  461. } catch (IllegalAccessException e) {
  462. Log.e(TAG, "Failed to access and set field");
  463. }
  464. }
  465. private void sendMessage(String message, int attempt) {
  466. if (attempt < 4) {
  467. Map<String, String> fieldMap = new HashMap<>();
  468. fieldMap.put("message", message);
  469. fieldMap.put("actorDisplayName", conversationUser.getDisplayName());
  470. ncApi.sendChatMessage(credentials, ApiUtils.getUrlForChat(baseUrl, roomToken), fieldMap)
  471. .subscribeOn(Schedulers.newThread())
  472. .observeOn(AndroidSchedulers.mainThread())
  473. .subscribe(new Observer<GenericOverall>() {
  474. @Override
  475. public void onSubscribe(Disposable d) {
  476. }
  477. @Override
  478. public void onNext(GenericOverall genericOverall) {
  479. if (conversationUser.getUserId().equals("?") && TextUtils.isEmpty(myFirstMessage)) {
  480. myFirstMessage = message;
  481. }
  482. if (popupBubble != null && popupBubble.isShown()) {
  483. popupBubble.hide();
  484. }
  485. if (messagesListView != null) {
  486. messagesListView.smoothScrollToPosition(0);
  487. }
  488. }
  489. @Override
  490. public void onError(Throwable e) {
  491. if (e instanceof HttpException && ((HttpException) e).code() == 201) {
  492. if (conversationUser.getUserId().equals("?") && TextUtils.isEmpty(myFirstMessage)) {
  493. myFirstMessage = message;
  494. }
  495. if (popupBubble != null && popupBubble.isShown()) {
  496. popupBubble.hide();
  497. }
  498. messagesListView.smoothScrollToPosition(0);
  499. } else {
  500. sendMessage(message, attempt + 1);
  501. }
  502. }
  503. @Override
  504. public void onComplete() {
  505. }
  506. });
  507. }
  508. }
  509. private void pullChatMessages(int lookIntoFuture) {
  510. if (!lookingIntoFuture && lookIntoFuture == 1) {
  511. lookingIntoFuture = true;
  512. }
  513. Map<String, Integer> fieldMap = new HashMap<>();
  514. fieldMap.put("lookIntoFuture", lookIntoFuture);
  515. fieldMap.put("limit", 25);
  516. int lastKnown;
  517. if (lookIntoFuture == 1) {
  518. lastKnown = globalLastKnownFutureMessageId;
  519. } else {
  520. lastKnown = globalLastKnownPastMessageId;
  521. }
  522. if (lastKnown != -1) {
  523. fieldMap.put("lastKnownMessageId", lastKnown);
  524. }
  525. if (lookIntoFuture == 1) {
  526. ncApi.pullChatMessages(credentials, ApiUtils.getUrlForChat(baseUrl, roomToken), fieldMap)
  527. .subscribeOn(Schedulers.newThread())
  528. .observeOn(AndroidSchedulers.mainThread())
  529. .takeWhile(observable -> inChat)
  530. .retry(3, observable -> inChat)
  531. .subscribe(new Observer<Response>() {
  532. @Override
  533. public void onSubscribe(Disposable d) {
  534. disposableList.add(d);
  535. }
  536. @Override
  537. public void onNext(Response response) {
  538. processMessages(response, true);
  539. }
  540. @Override
  541. public void onError(Throwable e) {
  542. }
  543. @Override
  544. public void onComplete() {
  545. pullChatMessages(1);
  546. }
  547. });
  548. } else {
  549. ncApi.pullChatMessages(credentials,
  550. ApiUtils.getUrlForChat(baseUrl, roomToken), fieldMap)
  551. .subscribeOn(Schedulers.newThread())
  552. .observeOn(AndroidSchedulers.mainThread())
  553. .retry(3, observable -> inChat)
  554. .subscribe(new Observer<Response>() {
  555. @Override
  556. public void onSubscribe(Disposable d) {
  557. disposableList.add(d);
  558. }
  559. @Override
  560. public void onNext(Response response) {
  561. processMessages(response, false);
  562. }
  563. @Override
  564. public void onError(Throwable e) {
  565. }
  566. @Override
  567. public void onComplete() {
  568. }
  569. });
  570. }
  571. }
  572. private void processMessages(Response response, boolean isFromTheFuture) {
  573. if (response.code() == 200) {
  574. ChatOverall chatOverall = (ChatOverall) response.body();
  575. List<ChatMessage> chatMessageList = chatOverall.getOcs().getData();
  576. if (!isFromTheFuture) {
  577. for (int i = 0; i < chatMessageList.size(); i++) {
  578. chatMessageList.get(i).setBaseUrl(conversationUser.getBaseUrl());
  579. if (globalLastKnownPastMessageId == -1 || chatMessageList.get(i).getJsonMessageId() <
  580. globalLastKnownPastMessageId) {
  581. globalLastKnownPastMessageId = chatMessageList.get(i).getJsonMessageId();
  582. }
  583. if (globalLastKnownFutureMessageId == -1) {
  584. if (chatMessageList.get(i).getJsonMessageId() > globalLastKnownFutureMessageId) {
  585. globalLastKnownFutureMessageId = chatMessageList.get(i).getJsonMessageId();
  586. }
  587. }
  588. }
  589. adapter.addToEnd(chatMessageList, false);
  590. } else {
  591. for (int i = 0; i < chatMessageList.size(); i++) {
  592. chatMessageList.get(i).setBaseUrl(conversationUser.getBaseUrl());
  593. if (conversationUser.getUserId().equals("?") && !TextUtils.isEmpty(myFirstMessage)) {
  594. ChatMessage chatMessage = chatMessageList.get(i);
  595. if (chatMessage.getActorType().equals("guests") &&
  596. chatMessage.getActorDisplayName().equals(conversationUser.getDisplayName())) {
  597. conversationUser.setUserId(chatMessage.getActorId());
  598. setSenderId();
  599. }
  600. }
  601. boolean shouldScroll = layoutManager.findFirstVisibleItemPosition() == 0 ||
  602. adapter.getItemCount() == 0;
  603. if (!shouldScroll && popupBubble != null) {
  604. if (!popupBubble.isShown()) {
  605. newMessagesCount = 1;
  606. popupBubble.show();
  607. } else if (popupBubble.isShown()) {
  608. newMessagesCount++;
  609. }
  610. } else {
  611. newMessagesCount = 0;
  612. }
  613. adapter.addToStart(chatMessageList.get(i), shouldScroll);
  614. }
  615. String xChatLastGivenHeader;
  616. if (response.headers().size() > 0 && !TextUtils.isEmpty((xChatLastGivenHeader = response.headers().get
  617. ("X-Chat-Last-Given")))) {
  618. globalLastKnownFutureMessageId = Integer.parseInt(xChatLastGivenHeader);
  619. }
  620. }
  621. if (!lookingIntoFuture) {
  622. pullChatMessages(1);
  623. }
  624. } else if (response.code() == 304 && !isFromTheFuture) {
  625. historyRead = true;
  626. if (!lookingIntoFuture) {
  627. pullChatMessages(1);
  628. }
  629. }
  630. }
  631. @Override
  632. public void onLoadMore(int page, int totalItemsCount) {
  633. if (!historyRead) {
  634. pullChatMessages(0);
  635. }
  636. }
  637. @Override
  638. public String format(Date date) {
  639. if (DateFormatter.isToday(date)) {
  640. return getResources().getString(R.string.nc_date_header_today);
  641. } else if (DateFormatter.isYesterday(date)) {
  642. return getResources().getString(R.string.nc_date_header_yesterday);
  643. } else {
  644. return DateFormatter.format(date, DateFormatter.Template.STRING_DAY_MONTH_YEAR);
  645. }
  646. }
  647. @Override
  648. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  649. super.onCreateOptionsMenu(menu, inflater);
  650. inflater.inflate(R.menu.menu_conversation, menu);
  651. }
  652. @Override
  653. public boolean onOptionsItemSelected(@NonNull MenuItem item) {
  654. switch (item.getItemId()) {
  655. case android.R.id.home:
  656. onDestroy();
  657. return true;
  658. case R.id.conversation_video_call:
  659. startACall(false);
  660. return true;
  661. case R.id.conversation_voice_call:
  662. startACall(true);
  663. return true;
  664. default:
  665. return super.onOptionsItemSelected(item);
  666. }
  667. }
  668. private void startACall(boolean isVoiceOnlyCall) {
  669. if (!isVoiceOnlyCall) {
  670. Intent videoCallIntent = getIntentForCall(false);
  671. if (videoCallIntent != null) {
  672. startActivity(videoCallIntent);
  673. }
  674. } else {
  675. Intent voiceCallIntent = getIntentForCall(true);
  676. if (voiceCallIntent != null) {
  677. startActivity(voiceCallIntent);
  678. }
  679. }
  680. }
  681. private Intent getIntentForCall(boolean isVoiceOnlyCall) {
  682. if (currentCall != null && !TextUtils.isEmpty(currentCall.getSessionId())) {
  683. Bundle bundle = new Bundle();
  684. bundle.putString(BundleKeys.KEY_ROOM_TOKEN, roomToken);
  685. bundle.putString(BundleKeys.KEY_ROOM_ID, roomId);
  686. bundle.putParcelable(BundleKeys.KEY_USER_ENTITY, Parcels.wrap(conversationUser));
  687. bundle.putString(BundleKeys.KEY_CONVERSATION_PASSWORD, roomPassword);
  688. bundle.putString(BundleKeys.KEY_CALL_SESSION, currentCall.getSessionId());
  689. bundle.putString(BundleKeys.KEY_MODIFIED_BASE_URL, baseUrl);
  690. if (isVoiceOnlyCall) {
  691. bundle.putBoolean(BundleKeys.KEY_CALL_VOICE_ONLY, true);
  692. }
  693. Intent callIntent = new Intent(getActivity(), MagicCallActivity.class);
  694. callIntent.putExtras(bundle);
  695. return callIntent;
  696. } else {
  697. return null;
  698. }
  699. }
  700. @Override
  701. public void onMessageLongClick(IMessage message) {
  702. if (getActivity() != null) {
  703. ClipboardManager clipboardManager = (android.content.ClipboardManager)
  704. getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
  705. ClipData clipData = android.content.ClipData.newPlainText(
  706. getResources().getString(R.string.nc_app_name), message.getText());
  707. if (clipboardManager != null) {
  708. clipboardManager.setPrimaryClip(clipData);
  709. }
  710. }
  711. }
  712. }