ChatController.java 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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.text.Editable;
  32. import android.text.InputFilter;
  33. import android.text.TextUtils;
  34. import android.text.TextWatcher;
  35. import android.util.Log;
  36. import android.view.LayoutInflater;
  37. import android.view.Menu;
  38. import android.view.MenuInflater;
  39. import android.view.MenuItem;
  40. import android.view.View;
  41. import android.view.ViewGroup;
  42. import android.widget.AbsListView;
  43. import android.widget.ImageView;
  44. import android.widget.ProgressBar;
  45. import android.widget.RelativeLayout;
  46. import android.widget.TextView;
  47. import com.amulyakhare.textdrawable.TextDrawable;
  48. import com.bluelinelabs.conductor.RouterTransaction;
  49. import com.bluelinelabs.conductor.changehandler.VerticalChangeHandler;
  50. import com.bumptech.glide.load.DataSource;
  51. import com.bumptech.glide.load.engine.DiskCacheStrategy;
  52. import com.bumptech.glide.load.engine.GlideException;
  53. import com.bumptech.glide.load.resource.bitmap.CircleCrop;
  54. import com.bumptech.glide.request.RequestListener;
  55. import com.bumptech.glide.request.RequestOptions;
  56. import com.bumptech.glide.request.target.Target;
  57. import com.nextcloud.talk.R;
  58. import com.nextcloud.talk.activities.MagicCallActivity;
  59. import com.nextcloud.talk.adapters.messages.MagicIncomingTextMessageViewHolder;
  60. import com.nextcloud.talk.adapters.messages.MagicOutcomingTextMessageViewHolder;
  61. import com.nextcloud.talk.adapters.messages.MagicPreviewMessageViewHolder;
  62. import com.nextcloud.talk.adapters.messages.MagicSystemMessageViewHolder;
  63. import com.nextcloud.talk.api.NcApi;
  64. import com.nextcloud.talk.application.NextcloudTalkApplication;
  65. import com.nextcloud.talk.callbacks.MentionAutocompleteCallback;
  66. import com.nextcloud.talk.controllers.base.BaseController;
  67. import com.nextcloud.talk.models.database.UserEntity;
  68. import com.nextcloud.talk.models.json.call.Call;
  69. import com.nextcloud.talk.models.json.call.CallOverall;
  70. import com.nextcloud.talk.models.json.chat.ChatMessage;
  71. import com.nextcloud.talk.models.json.chat.ChatOverall;
  72. import com.nextcloud.talk.models.json.generic.GenericOverall;
  73. import com.nextcloud.talk.models.json.mention.Mention;
  74. import com.nextcloud.talk.models.json.rooms.Conversation;
  75. import com.nextcloud.talk.models.json.rooms.RoomOverall;
  76. import com.nextcloud.talk.models.json.rooms.RoomsOverall;
  77. import com.nextcloud.talk.presenters.MentionAutocompletePresenter;
  78. import com.nextcloud.talk.utils.ApiUtils;
  79. import com.nextcloud.talk.utils.KeyboardUtils;
  80. import com.nextcloud.talk.utils.NotificationUtils;
  81. import com.nextcloud.talk.utils.bundle.BundleKeys;
  82. import com.nextcloud.talk.utils.database.user.UserUtils;
  83. import com.nextcloud.talk.utils.glide.GlideApp;
  84. import com.nextcloud.talk.utils.singletons.ApplicationWideCurrentRoomHolder;
  85. import com.otaliastudios.autocomplete.Autocomplete;
  86. import com.otaliastudios.autocomplete.AutocompleteCallback;
  87. import com.otaliastudios.autocomplete.AutocompletePresenter;
  88. import com.otaliastudios.autocomplete.CharPolicy;
  89. import com.stfalcon.chatkit.commons.ImageLoader;
  90. import com.stfalcon.chatkit.commons.models.IMessage;
  91. import com.stfalcon.chatkit.messages.MessageHolders;
  92. import com.stfalcon.chatkit.messages.MessageInput;
  93. import com.stfalcon.chatkit.messages.MessagesList;
  94. import com.stfalcon.chatkit.messages.MessagesListAdapter;
  95. import com.stfalcon.chatkit.utils.DateFormatter;
  96. import com.stfalcon.chatkit.utils.RoundedImageView;
  97. import com.webianks.library.PopupBubble;
  98. import org.parceler.Parcels;
  99. import java.lang.reflect.Field;
  100. import java.util.ArrayList;
  101. import java.util.Date;
  102. import java.util.HashMap;
  103. import java.util.List;
  104. import java.util.Map;
  105. import java.util.concurrent.TimeUnit;
  106. import javax.inject.Inject;
  107. import androidx.annotation.NonNull;
  108. import androidx.annotation.Nullable;
  109. import androidx.recyclerview.widget.LinearLayoutManager;
  110. import androidx.recyclerview.widget.RecyclerView;
  111. import autodagger.AutoInjector;
  112. import butterknife.BindView;
  113. import butterknife.OnClick;
  114. import io.reactivex.Observer;
  115. import io.reactivex.android.schedulers.AndroidSchedulers;
  116. import io.reactivex.disposables.Disposable;
  117. import io.reactivex.schedulers.Schedulers;
  118. import retrofit2.HttpException;
  119. import retrofit2.Response;
  120. @AutoInjector(NextcloudTalkApplication.class)
  121. public class ChatController extends BaseController implements MessagesListAdapter.OnLoadMoreListener,
  122. MessagesListAdapter.Formatter<Date>, MessagesListAdapter.OnMessageLongClickListener, MessageHolders.ContentChecker {
  123. private static final String TAG = "ChatController";
  124. @Inject
  125. NcApi ncApi;
  126. @Inject
  127. UserUtils userUtils;
  128. @BindView(R.id.messagesListView)
  129. MessagesList messagesListView;
  130. @BindView(R.id.messageInputView)
  131. MessageInput messageInputView;
  132. @BindView(R.id.popupBubbleView)
  133. PopupBubble popupBubble;
  134. @BindView(R.id.emptyLayout)
  135. RelativeLayout emptyLayout;
  136. @BindView(R.id.sendHiTextView)
  137. TextView sendHiTextView;
  138. @BindView(R.id.progressBar)
  139. ProgressBar loadingProgressBar;
  140. private List<Disposable> disposableList = new ArrayList<>();
  141. private String conversationName;
  142. private String roomToken;
  143. private UserEntity conversationUser;
  144. private String roomPassword;
  145. private String credentials;
  146. private String baseUrl;
  147. private Call currentCall;
  148. private boolean inChat = false;
  149. private boolean historyRead = false;
  150. private int globalLastKnownFutureMessageId = -1;
  151. private int globalLastKnownPastMessageId = -1;
  152. private MessagesListAdapter<ChatMessage> adapter;
  153. private CharSequence myFirstMessage;
  154. private Autocomplete mentionAutocomplete;
  155. private LinearLayoutManager layoutManager;
  156. private boolean lookingIntoFuture = false;
  157. private int newMessagesCount = 0;
  158. private Boolean startCallFromNotification = null;
  159. private String roomId;
  160. private boolean voiceOnly;
  161. private boolean isFirstMessagesProcessing = true;
  162. private boolean isHelloClicked;
  163. private static final byte CONTENT_TYPE_SYSTEM_MESSAGE = 1;
  164. private boolean wasDetached;
  165. public ChatController(Bundle args) {
  166. super(args);
  167. setHasOptionsMenu(true);
  168. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  169. UserEntity currentUser = userUtils.getCurrentUser();
  170. this.conversationName = args.getString(BundleKeys.KEY_CONVERSATION_NAME, "");
  171. if (args.containsKey(BundleKeys.KEY_USER_ENTITY)) {
  172. this.conversationUser = Parcels.unwrap(args.getParcelable(BundleKeys.KEY_USER_ENTITY));
  173. } else {
  174. this.conversationUser = currentUser;
  175. }
  176. this.roomId = args.getString(BundleKeys.KEY_ROOM_ID, "");
  177. this.roomToken = args.getString(BundleKeys.KEY_ROOM_TOKEN, "");
  178. if (args.containsKey(BundleKeys.KEY_ACTIVE_CONVERSATION)) {
  179. this.currentCall = Parcels.unwrap(args.getParcelable(BundleKeys.KEY_ACTIVE_CONVERSATION));
  180. }
  181. this.baseUrl = args.getString(BundleKeys.KEY_MODIFIED_BASE_URL, "");
  182. if (!TextUtils.isEmpty(baseUrl)) {
  183. conversationUser.setBaseUrl(baseUrl);
  184. conversationUser.setUserId("?");
  185. conversationUser.setDisplayName(currentUser.getDisplayName());
  186. } else {
  187. baseUrl = conversationUser.getBaseUrl();
  188. }
  189. this.roomPassword = args.getString(BundleKeys.KEY_CONVERSATION_PASSWORD, "");
  190. if (conversationUser.getUserId().equals("?")) {
  191. credentials = null;
  192. } else {
  193. credentials = ApiUtils.getCredentials(conversationUser.getUsername(), conversationUser.getToken());
  194. }
  195. if (args.containsKey(BundleKeys.KEY_FROM_NOTIFICATION_START_CALL)) {
  196. this.startCallFromNotification = args.getBoolean(BundleKeys.KEY_FROM_NOTIFICATION_START_CALL);
  197. }
  198. this.voiceOnly = args.getBoolean(BundleKeys.KEY_CALL_VOICE_ONLY, false);
  199. }
  200. private void getRoomInfo() {
  201. ncApi.getRoom(credentials, ApiUtils.getRoom(baseUrl, roomToken))
  202. .subscribeOn(Schedulers.newThread())
  203. .observeOn(AndroidSchedulers.mainThread())
  204. .subscribe(new Observer<RoomOverall>() {
  205. @Override
  206. public void onSubscribe(Disposable d) {
  207. disposableList.add(d);
  208. }
  209. @Override
  210. public void onNext(RoomOverall roomOverall) {
  211. conversationName = roomOverall.getOcs().getData().getDisplayName();
  212. setTitle();
  213. setupMentionAutocomplete();
  214. joinRoomWithPassword();
  215. }
  216. @Override
  217. public void onError(Throwable e) {
  218. }
  219. @Override
  220. public void onComplete() {
  221. }
  222. });
  223. }
  224. private void handleFromNotification() {
  225. ncApi.getRooms(credentials, ApiUtils.getUrlForGetRooms(baseUrl))
  226. .subscribeOn(Schedulers.newThread())
  227. .observeOn(AndroidSchedulers.mainThread())
  228. .subscribe(new Observer<RoomsOverall>() {
  229. @Override
  230. public void onSubscribe(Disposable d) {
  231. disposableList.add(d);
  232. }
  233. @Override
  234. public void onNext(RoomsOverall roomsOverall) {
  235. for (Conversation conversation : roomsOverall.getOcs().getData()) {
  236. if (roomId.equals(conversation.getRoomId())) {
  237. roomToken = conversation.getToken();
  238. conversationName = conversation.getDisplayName();
  239. setTitle();
  240. break;
  241. }
  242. }
  243. if (!TextUtils.isEmpty(roomToken)) {
  244. setupMentionAutocomplete();
  245. joinRoomWithPassword();
  246. }
  247. }
  248. @Override
  249. public void onError(Throwable e) {
  250. }
  251. @Override
  252. public void onComplete() {
  253. }
  254. });
  255. }
  256. @Override
  257. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  258. return inflater.inflate(R.layout.controller_chat, container, false);
  259. }
  260. @Override
  261. protected void onViewBound(@NonNull View view) {
  262. super.onViewBound(view);
  263. getActionBar().show();
  264. boolean adapterWasNull = false;
  265. sendHiTextView.setText(String.format(getResources().getString(R.string.nc_chat_empty), getResources()
  266. .getString(R.string.nc_hello)));
  267. if (adapter == null) {
  268. loadingProgressBar.setVisibility(View.VISIBLE);
  269. adapterWasNull = true;
  270. MessageHolders messageHolders = new MessageHolders();
  271. messageHolders.setIncomingTextConfig(MagicIncomingTextMessageViewHolder.class, R.layout.item_custom_incoming_text_message);
  272. messageHolders.setOutcomingTextConfig(MagicOutcomingTextMessageViewHolder.class, R.layout.item_custom_outcoming_text_message);
  273. messageHolders.setIncomingImageConfig(MagicPreviewMessageViewHolder.class, R.layout.item_custom_incoming_preview_message);
  274. messageHolders.setOutcomingImageConfig(MagicPreviewMessageViewHolder.class, R.layout.item_custom_outcoming_preview_message);
  275. messageHolders.registerContentType(CONTENT_TYPE_SYSTEM_MESSAGE, MagicSystemMessageViewHolder.class,
  276. R.layout.item_system_message, MagicSystemMessageViewHolder.class, R.layout.item_system_message,
  277. this);
  278. adapter = new MessagesListAdapter<>(conversationUser.getUserId(), messageHolders, new ImageLoader() {
  279. @Override
  280. public void loadImage(ImageView imageView, String url) {
  281. if (!(imageView instanceof RoundedImageView)) {
  282. GlideApp.with(NextcloudTalkApplication.getSharedApplication().getApplicationContext())
  283. .asBitmap()
  284. .diskCacheStrategy(DiskCacheStrategy.NONE)
  285. .load(url)
  286. .centerInside()
  287. .override(imageView.getMeasuredWidth(), imageView.getMeasuredHeight())
  288. .apply(RequestOptions.bitmapTransform(new CircleCrop()))
  289. .listener(new RequestListener<Bitmap>() {
  290. @Override
  291. public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
  292. TextDrawable drawable = TextDrawable.builder().beginConfig().bold()
  293. .endConfig().buildRound("?", getResources().getColor(R.color.nc_grey));
  294. imageView.setImageDrawable(drawable);
  295. return true;
  296. }
  297. @Override
  298. public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
  299. return false;
  300. }
  301. })
  302. .into(imageView);
  303. } else {
  304. GlideApp.with(NextcloudTalkApplication.getSharedApplication().getApplicationContext())
  305. .asBitmap()
  306. .diskCacheStrategy(DiskCacheStrategy.NONE)
  307. .override(480, 480)
  308. .load(url)
  309. .into(imageView);
  310. }
  311. }
  312. });
  313. } else {
  314. if (adapter.getItemCount() == 0) {
  315. emptyLayout.setVisibility(View.VISIBLE);
  316. } else {
  317. messagesListView.setVisibility(View.VISIBLE);
  318. }
  319. }
  320. messagesListView.setAdapter(adapter);
  321. adapter.setLoadMoreListener(this);
  322. adapter.setDateHeadersFormatter(this::format);
  323. adapter.setOnMessageLongClickListener(this);
  324. layoutManager = (LinearLayoutManager) messagesListView.getLayoutManager();
  325. popupBubble.setRecyclerView(messagesListView);
  326. popupBubble.setPopupBubbleListener(context -> {
  327. if (newMessagesCount != 0) {
  328. int scrollPosition;
  329. if (newMessagesCount - 1 < 0) {
  330. scrollPosition = 0;
  331. } else {
  332. scrollPosition = newMessagesCount - 1;
  333. }
  334. new Handler().postDelayed(() -> messagesListView.smoothScrollToPosition(scrollPosition), 200);
  335. }
  336. });
  337. messagesListView.addOnScrollListener(new RecyclerView.OnScrollListener() {
  338. @Override
  339. public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
  340. super.onScrollStateChanged(recyclerView, newState);
  341. if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
  342. if (newMessagesCount != 0) {
  343. if (layoutManager.findFirstCompletelyVisibleItemPosition() < newMessagesCount) {
  344. newMessagesCount = 0;
  345. if (popupBubble != null && popupBubble.isShown()) {
  346. popupBubble.hide();
  347. }
  348. }
  349. }
  350. }
  351. }
  352. });
  353. InputFilter[] filters = new InputFilter[1];
  354. filters[0] = new InputFilter.LengthFilter(1000);
  355. messageInputView.getInputEditText().setFilters(filters);
  356. messageInputView.getInputEditText().addTextChangedListener(new TextWatcher() {
  357. @Override
  358. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  359. }
  360. @Override
  361. public void onTextChanged(CharSequence s, int start, int before, int count) {
  362. if (s.length() == 1000) {
  363. messageInputView.getInputEditText().setError(getResources().getString(R.string.nc_limit_hit));
  364. } else {
  365. messageInputView.getInputEditText().setError(null);
  366. }
  367. }
  368. @Override
  369. public void afterTextChanged(Editable s) {
  370. }
  371. });
  372. messageInputView.setInputListener(input -> {
  373. sendMessage(input, 1);
  374. return true;
  375. });
  376. messageInputView.getButton().setContentDescription(getResources()
  377. .getString(R.string.nc_description_send_message_button));
  378. if (adapterWasNull) {
  379. // we're starting
  380. if (TextUtils.isEmpty(roomToken)) {
  381. handleFromNotification();
  382. } else if (TextUtils.isEmpty(conversationName)) {
  383. getRoomInfo();
  384. } else {
  385. setupMentionAutocomplete();
  386. joinRoomWithPassword();
  387. }
  388. }
  389. }
  390. private void setupMentionAutocomplete() {
  391. float elevation = 6f;
  392. Drawable backgroundDrawable = new ColorDrawable(Color.WHITE);
  393. AutocompletePresenter<Mention> presenter = new MentionAutocompletePresenter(getApplicationContext(), roomToken);
  394. AutocompleteCallback<Mention> callback = new MentionAutocompleteCallback();
  395. if (messageInputView != null && messageInputView.getInputEditText() != null) {
  396. mentionAutocomplete = Autocomplete.<Mention>on(messageInputView.getInputEditText())
  397. .with(elevation)
  398. .with(backgroundDrawable)
  399. .with(new CharPolicy('@'))
  400. .with(presenter)
  401. .with(callback)
  402. .build();
  403. }
  404. }
  405. @Override
  406. protected void onAttach(@NonNull View view) {
  407. super.onAttach(view);
  408. ApplicationWideCurrentRoomHolder.getInstance().setCurrentRoomId(roomId);
  409. ApplicationWideCurrentRoomHolder.getInstance().setCurrentRoomToken(roomId);
  410. ApplicationWideCurrentRoomHolder.getInstance().setInCall(false);
  411. ApplicationWideCurrentRoomHolder.getInstance().setUserInRoom(conversationUser);
  412. if (mentionAutocomplete != null && mentionAutocomplete.isPopupShowing()) {
  413. mentionAutocomplete.dismissPopup();
  414. }
  415. if (getActivity() != null) {
  416. new KeyboardUtils(getActivity(), getView(), false);
  417. }
  418. if (inChat) {
  419. NotificationUtils.cancelExistingNotifications(getApplicationContext(), conversationUser);
  420. if (wasDetached & conversationUser.hasSpreedCapabilityWithName("no-ping")) {
  421. joinRoomWithPassword();
  422. }
  423. }
  424. }
  425. @Override
  426. protected void onDetach(@NonNull View view) {
  427. super.onDetach(view);
  428. if (conversationUser.hasSpreedCapabilityWithName("no-ping")) {
  429. wasDetached = true;
  430. }
  431. }
  432. @Override
  433. protected String getTitle() {
  434. return conversationName;
  435. }
  436. @Override
  437. public void onDestroy() {
  438. super.onDestroy();
  439. adapter = null;
  440. inChat = false;
  441. ApplicationWideCurrentRoomHolder.getInstance().clear();
  442. leaveRoom();
  443. }
  444. private void dispose() {
  445. Disposable disposable;
  446. for (int i = 0; i < disposableList.size(); i++) {
  447. if (!(disposable = disposableList.get(i)).isDisposed()) {
  448. disposable.dispose();
  449. }
  450. }
  451. }
  452. private void startPing() {
  453. if (!conversationUser.hasSpreedCapabilityWithName("no-ping")) {
  454. ncApi.pingCall(credentials, ApiUtils.getUrlForCallPing(baseUrl, roomToken))
  455. .subscribeOn(Schedulers.newThread())
  456. .observeOn(AndroidSchedulers.mainThread())
  457. .repeatWhen(observable -> observable.delay(5000, TimeUnit.MILLISECONDS))
  458. .takeWhile(observable -> inChat)
  459. .retry(3, observable -> inChat)
  460. .subscribe(new Observer<GenericOverall>() {
  461. @Override
  462. public void onSubscribe(Disposable d) {
  463. disposableList.add(d);
  464. }
  465. @Override
  466. public void onNext(GenericOverall genericOverall) {
  467. }
  468. @Override
  469. public void onError(Throwable e) {
  470. }
  471. @Override
  472. public void onComplete() {
  473. }
  474. });
  475. }
  476. }
  477. @OnClick(R.id.emptyLayout)
  478. public void sendHello() {
  479. if (!isHelloClicked) {
  480. isHelloClicked = true;
  481. sendMessage(getResources().getString(R.string.nc_hello) + " 👋", 1);
  482. }
  483. }
  484. private void joinRoomWithPassword() {
  485. wasDetached = false;
  486. if (currentCall == null) {
  487. ncApi.joinRoom(credentials, ApiUtils.getUrlForSettingMyselfAsActiveParticipant(baseUrl, roomToken), roomPassword)
  488. .subscribeOn(Schedulers.newThread())
  489. .observeOn(AndroidSchedulers.mainThread())
  490. .retry(3)
  491. .subscribe(new Observer<CallOverall>() {
  492. @Override
  493. public void onSubscribe(Disposable d) {
  494. disposableList.add(d);
  495. }
  496. @Override
  497. public void onNext(CallOverall callOverall) {
  498. inChat = true;
  499. currentCall = callOverall.getOcs().getData();
  500. ApplicationWideCurrentRoomHolder.getInstance().setSession(currentCall.getSessionId());
  501. startPing();
  502. if (isFirstMessagesProcessing) {
  503. pullChatMessages(0);
  504. } else {
  505. pullChatMessages(1);
  506. }
  507. if (startCallFromNotification != null && startCallFromNotification) {
  508. startCallFromNotification = false;
  509. startACall(voiceOnly);
  510. }
  511. }
  512. @Override
  513. public void onError(Throwable e) {
  514. }
  515. @Override
  516. public void onComplete() {
  517. }
  518. });
  519. } else {
  520. inChat = true;
  521. ApplicationWideCurrentRoomHolder.getInstance().setSession(currentCall.getSessionId());
  522. startPing();
  523. if (isFirstMessagesProcessing) {
  524. pullChatMessages(0);
  525. } else {
  526. pullChatMessages(1);
  527. }
  528. }
  529. }
  530. private void leaveRoom() {
  531. ncApi.leaveRoom(credentials, ApiUtils.getUrlForSettingMyselfAsActiveParticipant(baseUrl, roomToken))
  532. .subscribeOn(Schedulers.newThread())
  533. .observeOn(AndroidSchedulers.mainThread())
  534. .subscribe(new Observer<GenericOverall>() {
  535. @Override
  536. public void onSubscribe(Disposable d) {
  537. disposableList.add(d);
  538. }
  539. @Override
  540. public void onNext(GenericOverall genericOverall) {
  541. dispose();
  542. if (!isDestroyed()) {
  543. getRouter().popToRoot();
  544. }
  545. }
  546. @Override
  547. public void onError(Throwable e) {
  548. }
  549. @Override
  550. public void onComplete() {
  551. dispose();
  552. }
  553. });
  554. }
  555. private void setSenderId() {
  556. try {
  557. final Field senderId = adapter.getClass().getDeclaredField("senderId");
  558. senderId.setAccessible(true);
  559. senderId.set(adapter, conversationUser.getUserId());
  560. } catch (NoSuchFieldException e) {
  561. Log.e(TAG, "Failed to set sender id");
  562. } catch (IllegalAccessException e) {
  563. Log.e(TAG, "Failed to access and set field");
  564. }
  565. }
  566. private void sendMessage(CharSequence message, int attempt) {
  567. if (attempt < 4) {
  568. ncApi.sendChatMessage(credentials, ApiUtils.getUrlForChat(baseUrl, roomToken), message, conversationUser
  569. .getDisplayName())
  570. .subscribeOn(Schedulers.newThread())
  571. .observeOn(AndroidSchedulers.mainThread())
  572. .subscribe(new Observer<GenericOverall>() {
  573. @Override
  574. public void onSubscribe(Disposable d) {
  575. }
  576. @Override
  577. public void onNext(GenericOverall genericOverall) {
  578. if (conversationUser.getUserId().equals("?") && TextUtils.isEmpty(myFirstMessage.toString())) {
  579. myFirstMessage = message;
  580. }
  581. if (popupBubble != null && popupBubble.isShown()) {
  582. popupBubble.hide();
  583. }
  584. if (messagesListView != null) {
  585. messagesListView.smoothScrollToPosition(0);
  586. }
  587. }
  588. @Override
  589. public void onError(Throwable e) {
  590. if (e instanceof HttpException) {
  591. int code = ((HttpException) e).code();
  592. if (Integer.toString(code).startsWith("2")) {
  593. if (conversationUser.getUserId().equals("?") && TextUtils.isEmpty(myFirstMessage.toString())) {
  594. myFirstMessage = message;
  595. }
  596. if (popupBubble != null && popupBubble.isShown()) {
  597. popupBubble.hide();
  598. }
  599. messagesListView.smoothScrollToPosition(0);
  600. } else {
  601. sendMessage(message, attempt + 1);
  602. }
  603. } else {
  604. sendMessage(message, attempt + 1);
  605. }
  606. }
  607. @Override
  608. public void onComplete() {
  609. }
  610. });
  611. }
  612. }
  613. private void pullChatMessages(int lookIntoFuture) {
  614. if (!inChat) {
  615. return;
  616. }
  617. if (!lookingIntoFuture && lookIntoFuture == 1) {
  618. lookingIntoFuture = true;
  619. }
  620. Map<String, Integer> fieldMap = new HashMap<>();
  621. fieldMap.put("lookIntoFuture", lookIntoFuture);
  622. fieldMap.put("limit", 25);
  623. int lastKnown;
  624. if (lookIntoFuture == 1) {
  625. lastKnown = globalLastKnownFutureMessageId;
  626. } else {
  627. lastKnown = globalLastKnownPastMessageId;
  628. }
  629. if (lastKnown != -1) {
  630. fieldMap.put("lastKnownMessageId", lastKnown);
  631. }
  632. if (!wasDetached) {
  633. if (lookIntoFuture == 1) {
  634. ncApi.pullChatMessages(credentials, ApiUtils.getUrlForChat(baseUrl, roomToken), fieldMap)
  635. .subscribeOn(Schedulers.newThread())
  636. .observeOn(AndroidSchedulers.mainThread())
  637. .takeWhile(observable -> inChat && !wasDetached)
  638. .retry(3, observable -> inChat && !wasDetached)
  639. .subscribe(new Observer<Response>() {
  640. @Override
  641. public void onSubscribe(Disposable d) {
  642. disposableList.add(d);
  643. }
  644. @Override
  645. public void onNext(Response response) {
  646. processMessages(response, true);
  647. }
  648. @Override
  649. public void onError(Throwable e) {
  650. }
  651. @Override
  652. public void onComplete() {
  653. pullChatMessages(1);
  654. }
  655. });
  656. } else {
  657. ncApi.pullChatMessages(credentials,
  658. ApiUtils.getUrlForChat(baseUrl, roomToken), fieldMap)
  659. .subscribeOn(Schedulers.newThread())
  660. .observeOn(AndroidSchedulers.mainThread())
  661. .retry(3, observable -> inChat && !wasDetached)
  662. .takeWhile(observable -> inChat && !wasDetached)
  663. .subscribe(new Observer<Response>() {
  664. @Override
  665. public void onSubscribe(Disposable d) {
  666. disposableList.add(d);
  667. }
  668. @Override
  669. public void onNext(Response response) {
  670. processMessages(response, false);
  671. }
  672. @Override
  673. public void onError(Throwable e) {
  674. }
  675. @Override
  676. public void onComplete() {
  677. }
  678. });
  679. }
  680. }
  681. }
  682. private void processMessages(Response response, boolean isFromTheFuture) {
  683. if (response.code() == 200) {
  684. ChatOverall chatOverall = (ChatOverall) response.body();
  685. List<ChatMessage> chatMessageList = chatOverall.getOcs().getData();
  686. if (isFirstMessagesProcessing) {
  687. NotificationUtils.cancelExistingNotifications(getApplicationContext(), conversationUser);
  688. isFirstMessagesProcessing = false;
  689. if (loadingProgressBar != null) {
  690. loadingProgressBar.setVisibility(View.GONE);
  691. }
  692. if (chatMessageList.size() == 0) {
  693. if (emptyLayout != null) {
  694. emptyLayout.setVisibility(View.VISIBLE);
  695. }
  696. if (messagesListView != null) {
  697. messagesListView.setVisibility(View.GONE);
  698. }
  699. } else {
  700. if (emptyLayout != null) {
  701. emptyLayout.setVisibility(View.GONE);
  702. }
  703. if (messagesListView != null) {
  704. messagesListView.setVisibility(View.VISIBLE);
  705. }
  706. }
  707. } else {
  708. if (emptyLayout != null) {
  709. emptyLayout.setVisibility(View.GONE);
  710. }
  711. if (messagesListView != null) {
  712. messagesListView.setVisibility(View.VISIBLE);
  713. }
  714. }
  715. int countGroupedMessages = 0;
  716. if (!isFromTheFuture) {
  717. for (int i = 0; i < chatMessageList.size(); i++) {
  718. if (chatMessageList.size() > i + 1) {
  719. if (TextUtils.isEmpty(chatMessageList.get(i).getSystemMessage()) &&
  720. TextUtils.isEmpty(chatMessageList.get(i + 1).getSystemMessage()) &&
  721. chatMessageList.get(i + 1).getActorId().equals(chatMessageList.get(i).getActorId()) &&
  722. countGroupedMessages < 4 && DateFormatter.isSameDay(chatMessageList.get(i).getCreatedAt(),
  723. chatMessageList.get(i + 1).getCreatedAt())) {
  724. chatMessageList.get(i).setGrouped(true);
  725. countGroupedMessages++;
  726. } else {
  727. countGroupedMessages = 0;
  728. }
  729. }
  730. chatMessageList.get(i).setBaseUrl(conversationUser.getBaseUrl());
  731. chatMessageList.get(i).setActiveUserId(conversationUser.getUserId());
  732. if (globalLastKnownPastMessageId == -1 || chatMessageList.get(i).getJsonMessageId() <
  733. globalLastKnownPastMessageId) {
  734. globalLastKnownPastMessageId = chatMessageList.get(i).getJsonMessageId();
  735. }
  736. if (globalLastKnownFutureMessageId == -1) {
  737. if (chatMessageList.get(i).getJsonMessageId() > globalLastKnownFutureMessageId) {
  738. globalLastKnownFutureMessageId = chatMessageList.get(i).getJsonMessageId();
  739. }
  740. }
  741. }
  742. adapter.addToEnd(chatMessageList, false);
  743. } else {
  744. ChatMessage chatMessage;
  745. for (int i = 0; i < chatMessageList.size(); i++) {
  746. chatMessage = chatMessageList.get(i);
  747. chatMessage.setBaseUrl(conversationUser.getBaseUrl());
  748. chatMessageList.get(i).setActiveUserId(conversationUser.getUserId());
  749. if (conversationUser.getUserId().equals("?") && myFirstMessage != null &&
  750. !TextUtils.isEmpty(myFirstMessage.toString())) {
  751. if (chatMessage.getActorType().equals("guests") &&
  752. chatMessage.getActorDisplayName().equals(conversationUser.getDisplayName())) {
  753. conversationUser.setUserId(chatMessage.getActorId());
  754. setSenderId();
  755. }
  756. }
  757. boolean shouldScroll = layoutManager.findFirstVisibleItemPosition() == 0 ||
  758. adapter.getItemCount() == 0;
  759. if (!shouldScroll && popupBubble != null) {
  760. if (!popupBubble.isShown()) {
  761. newMessagesCount = 1;
  762. popupBubble.show();
  763. } else if (popupBubble.isShown()) {
  764. newMessagesCount++;
  765. }
  766. } else {
  767. newMessagesCount = 0;
  768. }
  769. chatMessage.setGrouped(adapter.isPreviousSameAuthor(chatMessage.getActorId(), -1) && (adapter.getSameAuthorLastMessagesCount(chatMessage.getActorId()) % 5) > 0);
  770. adapter.addToStart(chatMessage, shouldScroll);
  771. }
  772. String xChatLastGivenHeader;
  773. if (response.headers().size() > 0 && !TextUtils.isEmpty((xChatLastGivenHeader = response.headers().get
  774. ("X-Chat-Last-Given")))) {
  775. globalLastKnownFutureMessageId = Integer.parseInt(xChatLastGivenHeader);
  776. }
  777. }
  778. if (!lookingIntoFuture && inChat) {
  779. pullChatMessages(1);
  780. }
  781. } else if (response.code() == 304 && !isFromTheFuture) {
  782. if (isFirstMessagesProcessing) {
  783. NotificationUtils.cancelExistingNotifications(getApplicationContext(), conversationUser);
  784. isFirstMessagesProcessing = false;
  785. if (loadingProgressBar != null) {
  786. loadingProgressBar.setVisibility(View.GONE);
  787. }
  788. if (emptyLayout != null && emptyLayout.getVisibility() != View.VISIBLE) {
  789. emptyLayout.setVisibility(View.VISIBLE);
  790. }
  791. }
  792. historyRead = true;
  793. if (!lookingIntoFuture && inChat) {
  794. pullChatMessages(1);
  795. }
  796. }
  797. }
  798. @Override
  799. public void onLoadMore(int page, int totalItemsCount) {
  800. if (!historyRead && inChat) {
  801. pullChatMessages(0);
  802. }
  803. }
  804. @Override
  805. public String format(Date date) {
  806. if (DateFormatter.isToday(date)) {
  807. return getResources().getString(R.string.nc_date_header_today);
  808. } else if (DateFormatter.isYesterday(date)) {
  809. return getResources().getString(R.string.nc_date_header_yesterday);
  810. } else {
  811. return DateFormatter.format(date, DateFormatter.Template.STRING_DAY_MONTH_YEAR);
  812. }
  813. }
  814. @Override
  815. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  816. super.onCreateOptionsMenu(menu, inflater);
  817. inflater.inflate(R.menu.menu_conversation, menu);
  818. if (conversationUser.hasSpreedCapabilityWithName("mention-flag")) {
  819. menu.findItem(R.id.nc_conversation_info).setVisible(true);
  820. } else {
  821. menu.findItem(R.id.nc_conversation_info).setVisible(false);
  822. }
  823. }
  824. @Override
  825. public boolean onOptionsItemSelected(@NonNull MenuItem item) {
  826. switch (item.getItemId()) {
  827. case android.R.id.home:
  828. onDestroy();
  829. return true;
  830. case R.id.conversation_video_call:
  831. startACall(false);
  832. return true;
  833. case R.id.conversation_voice_call:
  834. startACall(true);
  835. return true;
  836. case R.id.nc_conversation_info:
  837. Bundle bundle = new Bundle();
  838. bundle.putParcelable(BundleKeys.KEY_USER_ENTITY, Parcels.wrap(conversationUser));
  839. bundle.putString(BundleKeys.KEY_BASE_URL, baseUrl);
  840. bundle.putString(BundleKeys.KEY_ROOM_TOKEN, roomToken);
  841. getRouter().pushController((RouterTransaction.with(new ConversationInfoController(bundle))
  842. .pushChangeHandler(new VerticalChangeHandler())
  843. .popChangeHandler(new VerticalChangeHandler())));
  844. return true;
  845. default:
  846. return super.onOptionsItemSelected(item);
  847. }
  848. }
  849. private void startACall(boolean isVoiceOnlyCall) {
  850. if (!isVoiceOnlyCall) {
  851. Intent videoCallIntent = getIntentForCall(false);
  852. if (videoCallIntent != null) {
  853. startActivity(videoCallIntent);
  854. }
  855. } else {
  856. Intent voiceCallIntent = getIntentForCall(true);
  857. if (voiceCallIntent != null) {
  858. startActivity(voiceCallIntent);
  859. }
  860. }
  861. }
  862. private Intent getIntentForCall(boolean isVoiceOnlyCall) {
  863. if (currentCall != null) {
  864. Bundle bundle = new Bundle();
  865. bundle.putString(BundleKeys.KEY_ROOM_TOKEN, roomToken);
  866. bundle.putString(BundleKeys.KEY_ROOM_ID, roomId);
  867. bundle.putParcelable(BundleKeys.KEY_USER_ENTITY, Parcels.wrap(conversationUser));
  868. bundle.putString(BundleKeys.KEY_CONVERSATION_PASSWORD, roomPassword);
  869. bundle.putString(BundleKeys.KEY_MODIFIED_BASE_URL, baseUrl);
  870. if (isVoiceOnlyCall) {
  871. bundle.putBoolean(BundleKeys.KEY_CALL_VOICE_ONLY, true);
  872. }
  873. if (getActivity() != null) {
  874. Intent callIntent = new Intent(getActivity(), MagicCallActivity.class);
  875. callIntent.putExtras(bundle);
  876. return callIntent;
  877. } else {
  878. return null;
  879. }
  880. } else {
  881. return null;
  882. }
  883. }
  884. @Override
  885. public void onMessageLongClick(IMessage message) {
  886. if (getActivity() != null) {
  887. ClipboardManager clipboardManager = (android.content.ClipboardManager)
  888. getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
  889. ClipData clipData = android.content.ClipData.newPlainText(
  890. getResources().getString(R.string.nc_app_name), message.getText());
  891. if (clipboardManager != null) {
  892. clipboardManager.setPrimaryClip(clipData);
  893. }
  894. }
  895. }
  896. @Override
  897. public boolean hasContentFor(IMessage message, byte type) {
  898. switch (type) {
  899. case CONTENT_TYPE_SYSTEM_MESSAGE:
  900. return !TextUtils.isEmpty(message.getSystemMessage());
  901. }
  902. return false;
  903. }
  904. }