ChatController.java 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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.HorizontalChangeHandler;
  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 (conversationUser.hasSpreedCapabilityWithName("mention-flag") && getActivity() != null) {
  379. getActivity().findViewById(R.id.toolbar).setOnClickListener(v -> {
  380. Bundle bundle = new Bundle();
  381. bundle.putParcelable(BundleKeys.KEY_USER_ENTITY, Parcels.wrap(conversationUser));
  382. bundle.putString(BundleKeys.KEY_BASE_URL, baseUrl);
  383. bundle.putString(BundleKeys.KEY_ROOM_TOKEN, roomToken);
  384. getRouter().pushController((RouterTransaction.with(new ConversationInfoController(bundle))
  385. .pushChangeHandler(new HorizontalChangeHandler())
  386. .popChangeHandler(new HorizontalChangeHandler())));
  387. });
  388. }
  389. if (adapterWasNull) {
  390. // we're starting
  391. if (TextUtils.isEmpty(roomToken)) {
  392. handleFromNotification();
  393. } else if (TextUtils.isEmpty(conversationName)) {
  394. getRoomInfo();
  395. } else {
  396. setupMentionAutocomplete();
  397. joinRoomWithPassword();
  398. }
  399. }
  400. }
  401. private void setupMentionAutocomplete() {
  402. float elevation = 6f;
  403. Drawable backgroundDrawable = new ColorDrawable(Color.WHITE);
  404. AutocompletePresenter<Mention> presenter = new MentionAutocompletePresenter(getApplicationContext(), roomToken);
  405. AutocompleteCallback<Mention> callback = new MentionAutocompleteCallback();
  406. if (messageInputView != null && messageInputView.getInputEditText() != null) {
  407. mentionAutocomplete = Autocomplete.<Mention>on(messageInputView.getInputEditText())
  408. .with(elevation)
  409. .with(backgroundDrawable)
  410. .with(new CharPolicy('@'))
  411. .with(presenter)
  412. .with(callback)
  413. .build();
  414. }
  415. }
  416. @Override
  417. protected void onAttach(@NonNull View view) {
  418. super.onAttach(view);
  419. ApplicationWideCurrentRoomHolder.getInstance().setCurrentRoomId(roomId);
  420. ApplicationWideCurrentRoomHolder.getInstance().setCurrentRoomToken(roomId);
  421. ApplicationWideCurrentRoomHolder.getInstance().setInCall(false);
  422. ApplicationWideCurrentRoomHolder.getInstance().setUserInRoom(conversationUser);
  423. if (mentionAutocomplete != null && mentionAutocomplete.isPopupShowing()) {
  424. mentionAutocomplete.dismissPopup();
  425. }
  426. if (getActivity() != null) {
  427. new KeyboardUtils(getActivity(), getView(), false);
  428. }
  429. NotificationUtils.cancelExistingNotifications(getApplicationContext(), conversationUser);
  430. if (inChat) {
  431. if (wasDetached & conversationUser.hasSpreedCapabilityWithName("no-ping")) {
  432. wasDetached = false;
  433. joinRoomWithPassword();
  434. }
  435. }
  436. }
  437. @Override
  438. protected void onDetach(@NonNull View view) {
  439. super.onDetach(view);
  440. if (conversationUser.hasSpreedCapabilityWithName("no-ping")
  441. && getActivity() != null && !getActivity().isChangingConfigurations()) {
  442. wasDetached = true;
  443. leaveRoom();
  444. }
  445. }
  446. @Override
  447. protected String getTitle() {
  448. return conversationName;
  449. }
  450. @Override
  451. public void onDestroy() {
  452. super.onDestroy();
  453. if (getActivity() != null) {
  454. getActivity().findViewById(R.id.toolbar).setOnClickListener(null);
  455. }
  456. adapter = null;
  457. inChat = false;
  458. ApplicationWideCurrentRoomHolder.getInstance().clear();
  459. }
  460. private void dispose() {
  461. Disposable disposable;
  462. for (int i = 0; i < disposableList.size(); i++) {
  463. if (!(disposable = disposableList.get(i)).isDisposed()) {
  464. disposable.dispose();
  465. }
  466. }
  467. }
  468. private void startPing() {
  469. if (!conversationUser.hasSpreedCapabilityWithName("no-ping")) {
  470. ncApi.pingCall(credentials, ApiUtils.getUrlForCallPing(baseUrl, roomToken))
  471. .subscribeOn(Schedulers.newThread())
  472. .observeOn(AndroidSchedulers.mainThread())
  473. .repeatWhen(observable -> observable.delay(5000, TimeUnit.MILLISECONDS))
  474. .takeWhile(observable -> inChat)
  475. .retry(3, observable -> inChat)
  476. .subscribe(new Observer<GenericOverall>() {
  477. @Override
  478. public void onSubscribe(Disposable d) {
  479. disposableList.add(d);
  480. }
  481. @Override
  482. public void onNext(GenericOverall genericOverall) {
  483. }
  484. @Override
  485. public void onError(Throwable e) {
  486. }
  487. @Override
  488. public void onComplete() {
  489. }
  490. });
  491. }
  492. }
  493. @OnClick(R.id.emptyLayout)
  494. public void sendHello() {
  495. if (!isHelloClicked) {
  496. isHelloClicked = true;
  497. sendMessage(getResources().getString(R.string.nc_hello) + " 👋", 1);
  498. }
  499. }
  500. private void joinRoomWithPassword() {
  501. if (currentCall == null) {
  502. ncApi.joinRoom(credentials, ApiUtils.getUrlForSettingMyselfAsActiveParticipant(baseUrl, roomToken), roomPassword)
  503. .subscribeOn(Schedulers.newThread())
  504. .observeOn(AndroidSchedulers.mainThread())
  505. .retry(3)
  506. .subscribe(new Observer<CallOverall>() {
  507. @Override
  508. public void onSubscribe(Disposable d) {
  509. disposableList.add(d);
  510. }
  511. @Override
  512. public void onNext(CallOverall callOverall) {
  513. inChat = true;
  514. currentCall = callOverall.getOcs().getData();
  515. ApplicationWideCurrentRoomHolder.getInstance().setSession(currentCall.getSessionId());
  516. startPing();
  517. if (isFirstMessagesProcessing) {
  518. pullChatMessages(0);
  519. } else {
  520. pullChatMessages(1);
  521. }
  522. if (startCallFromNotification != null && startCallFromNotification) {
  523. startCallFromNotification = false;
  524. startACall(voiceOnly);
  525. }
  526. }
  527. @Override
  528. public void onError(Throwable e) {
  529. }
  530. @Override
  531. public void onComplete() {
  532. }
  533. });
  534. } else {
  535. inChat = true;
  536. ApplicationWideCurrentRoomHolder.getInstance().setSession(currentCall.getSessionId());
  537. startPing();
  538. if (isFirstMessagesProcessing) {
  539. pullChatMessages(0);
  540. } else {
  541. pullChatMessages(1);
  542. }
  543. }
  544. }
  545. private void leaveRoom() {
  546. ncApi.leaveRoom(credentials, ApiUtils.getUrlForSettingMyselfAsActiveParticipant(baseUrl, roomToken))
  547. .subscribeOn(Schedulers.newThread())
  548. .observeOn(AndroidSchedulers.mainThread())
  549. .subscribe(new Observer<GenericOverall>() {
  550. @Override
  551. public void onSubscribe(Disposable d) {
  552. disposableList.add(d);
  553. }
  554. @Override
  555. public void onNext(GenericOverall genericOverall) {
  556. dispose();
  557. currentCall = null;
  558. if (!isDestroyed() && !isBeingDestroyed() && !wasDetached) {
  559. getRouter().popCurrentController();
  560. }
  561. }
  562. @Override
  563. public void onError(Throwable e) {
  564. }
  565. @Override
  566. public void onComplete() {
  567. dispose();
  568. }
  569. });
  570. }
  571. private void setSenderId() {
  572. try {
  573. final Field senderId = adapter.getClass().getDeclaredField("senderId");
  574. senderId.setAccessible(true);
  575. senderId.set(adapter, conversationUser.getUserId());
  576. } catch (NoSuchFieldException e) {
  577. Log.e(TAG, "Failed to set sender id");
  578. } catch (IllegalAccessException e) {
  579. Log.e(TAG, "Failed to access and set field");
  580. }
  581. }
  582. private void sendMessage(CharSequence message, int attempt) {
  583. if (attempt < 4) {
  584. ncApi.sendChatMessage(credentials, ApiUtils.getUrlForChat(baseUrl, roomToken), message, conversationUser
  585. .getDisplayName())
  586. .subscribeOn(Schedulers.newThread())
  587. .observeOn(AndroidSchedulers.mainThread())
  588. .subscribe(new Observer<GenericOverall>() {
  589. @Override
  590. public void onSubscribe(Disposable d) {
  591. }
  592. @Override
  593. public void onNext(GenericOverall genericOverall) {
  594. if (conversationUser.getUserId().equals("?") && TextUtils.isEmpty(myFirstMessage.toString())) {
  595. myFirstMessage = message;
  596. }
  597. if (popupBubble != null && popupBubble.isShown()) {
  598. popupBubble.hide();
  599. }
  600. if (messagesListView != null) {
  601. messagesListView.smoothScrollToPosition(0);
  602. }
  603. }
  604. @Override
  605. public void onError(Throwable e) {
  606. if (e instanceof HttpException) {
  607. int code = ((HttpException) e).code();
  608. if (Integer.toString(code).startsWith("2")) {
  609. if (conversationUser.getUserId().equals("?") && TextUtils.isEmpty(myFirstMessage.toString())) {
  610. myFirstMessage = message;
  611. }
  612. if (popupBubble != null && popupBubble.isShown()) {
  613. popupBubble.hide();
  614. }
  615. messagesListView.smoothScrollToPosition(0);
  616. } else {
  617. sendMessage(message, attempt + 1);
  618. }
  619. } else {
  620. sendMessage(message, attempt + 1);
  621. }
  622. }
  623. @Override
  624. public void onComplete() {
  625. }
  626. });
  627. }
  628. }
  629. private void pullChatMessages(int lookIntoFuture) {
  630. if (!inChat) {
  631. return;
  632. }
  633. if (!lookingIntoFuture && lookIntoFuture == 1) {
  634. lookingIntoFuture = true;
  635. }
  636. Map<String, Integer> fieldMap = new HashMap<>();
  637. fieldMap.put("lookIntoFuture", lookIntoFuture);
  638. fieldMap.put("limit", 25);
  639. int lastKnown;
  640. if (lookIntoFuture == 1) {
  641. lastKnown = globalLastKnownFutureMessageId;
  642. } else {
  643. lastKnown = globalLastKnownPastMessageId;
  644. }
  645. if (lastKnown != -1) {
  646. fieldMap.put("lastKnownMessageId", lastKnown);
  647. }
  648. if (!wasDetached) {
  649. if (lookIntoFuture == 1) {
  650. ncApi.pullChatMessages(credentials, ApiUtils.getUrlForChat(baseUrl, roomToken), fieldMap)
  651. .subscribeOn(Schedulers.newThread())
  652. .observeOn(AndroidSchedulers.mainThread())
  653. .takeWhile(observable -> inChat && !wasDetached)
  654. .retry(3, observable -> inChat && !wasDetached)
  655. .subscribe(new Observer<Response>() {
  656. @Override
  657. public void onSubscribe(Disposable d) {
  658. disposableList.add(d);
  659. }
  660. @Override
  661. public void onNext(Response response) {
  662. processMessages(response, true);
  663. }
  664. @Override
  665. public void onError(Throwable e) {
  666. }
  667. @Override
  668. public void onComplete() {
  669. pullChatMessages(1);
  670. }
  671. });
  672. } else {
  673. ncApi.pullChatMessages(credentials,
  674. ApiUtils.getUrlForChat(baseUrl, roomToken), fieldMap)
  675. .subscribeOn(Schedulers.newThread())
  676. .observeOn(AndroidSchedulers.mainThread())
  677. .retry(3, observable -> inChat && !wasDetached)
  678. .takeWhile(observable -> inChat && !wasDetached)
  679. .subscribe(new Observer<Response>() {
  680. @Override
  681. public void onSubscribe(Disposable d) {
  682. disposableList.add(d);
  683. }
  684. @Override
  685. public void onNext(Response response) {
  686. processMessages(response, false);
  687. }
  688. @Override
  689. public void onError(Throwable e) {
  690. }
  691. @Override
  692. public void onComplete() {
  693. }
  694. });
  695. }
  696. }
  697. }
  698. private void processMessages(Response response, boolean isFromTheFuture) {
  699. if (response.code() == 200) {
  700. ChatOverall chatOverall = (ChatOverall) response.body();
  701. List<ChatMessage> chatMessageList = chatOverall.getOcs().getData();
  702. if (isFirstMessagesProcessing) {
  703. NotificationUtils.cancelExistingNotifications(getApplicationContext(), conversationUser);
  704. isFirstMessagesProcessing = false;
  705. if (loadingProgressBar != null) {
  706. loadingProgressBar.setVisibility(View.GONE);
  707. }
  708. if (chatMessageList.size() == 0) {
  709. if (emptyLayout != null) {
  710. emptyLayout.setVisibility(View.VISIBLE);
  711. }
  712. if (messagesListView != null) {
  713. messagesListView.setVisibility(View.GONE);
  714. }
  715. } else {
  716. if (emptyLayout != null) {
  717. emptyLayout.setVisibility(View.GONE);
  718. }
  719. if (messagesListView != null) {
  720. messagesListView.setVisibility(View.VISIBLE);
  721. }
  722. }
  723. } else {
  724. if (emptyLayout != null) {
  725. emptyLayout.setVisibility(View.GONE);
  726. }
  727. if (messagesListView != null) {
  728. messagesListView.setVisibility(View.VISIBLE);
  729. }
  730. }
  731. int countGroupedMessages = 0;
  732. if (!isFromTheFuture) {
  733. for (int i = 0; i < chatMessageList.size(); i++) {
  734. if (chatMessageList.size() > i + 1) {
  735. if (TextUtils.isEmpty(chatMessageList.get(i).getSystemMessage()) &&
  736. TextUtils.isEmpty(chatMessageList.get(i + 1).getSystemMessage()) &&
  737. chatMessageList.get(i + 1).getActorId().equals(chatMessageList.get(i).getActorId()) &&
  738. countGroupedMessages < 4 && DateFormatter.isSameDay(chatMessageList.get(i).getCreatedAt(),
  739. chatMessageList.get(i + 1).getCreatedAt())) {
  740. chatMessageList.get(i).setGrouped(true);
  741. countGroupedMessages++;
  742. } else {
  743. countGroupedMessages = 0;
  744. }
  745. }
  746. chatMessageList.get(i).setBaseUrl(conversationUser.getBaseUrl());
  747. chatMessageList.get(i).setActiveUserId(conversationUser.getUserId());
  748. if (globalLastKnownPastMessageId == -1 || chatMessageList.get(i).getJsonMessageId() <
  749. globalLastKnownPastMessageId) {
  750. globalLastKnownPastMessageId = chatMessageList.get(i).getJsonMessageId();
  751. }
  752. if (globalLastKnownFutureMessageId == -1) {
  753. if (chatMessageList.get(i).getJsonMessageId() > globalLastKnownFutureMessageId) {
  754. globalLastKnownFutureMessageId = chatMessageList.get(i).getJsonMessageId();
  755. }
  756. }
  757. }
  758. adapter.addToEnd(chatMessageList, false);
  759. } else {
  760. ChatMessage chatMessage;
  761. for (int i = 0; i < chatMessageList.size(); i++) {
  762. chatMessage = chatMessageList.get(i);
  763. chatMessage.setBaseUrl(conversationUser.getBaseUrl());
  764. chatMessageList.get(i).setActiveUserId(conversationUser.getUserId());
  765. if (conversationUser.getUserId().equals("?") && myFirstMessage != null &&
  766. !TextUtils.isEmpty(myFirstMessage.toString())) {
  767. if (chatMessage.getActorType().equals("guests") &&
  768. chatMessage.getActorDisplayName().equals(conversationUser.getDisplayName())) {
  769. conversationUser.setUserId(chatMessage.getActorId());
  770. setSenderId();
  771. }
  772. }
  773. boolean shouldScroll = layoutManager.findFirstVisibleItemPosition() == 0 ||
  774. adapter.getItemCount() == 0;
  775. if (!shouldScroll && popupBubble != null) {
  776. if (!popupBubble.isShown()) {
  777. newMessagesCount = 1;
  778. popupBubble.show();
  779. } else if (popupBubble.isShown()) {
  780. newMessagesCount++;
  781. }
  782. } else {
  783. newMessagesCount = 0;
  784. }
  785. chatMessage.setGrouped(adapter.isPreviousSameAuthor(chatMessage.getActorId(), -1) && (adapter.getSameAuthorLastMessagesCount(chatMessage.getActorId()) % 5) > 0);
  786. adapter.addToStart(chatMessage, shouldScroll);
  787. }
  788. String xChatLastGivenHeader;
  789. if (response.headers().size() > 0 && !TextUtils.isEmpty((xChatLastGivenHeader = response.headers().get
  790. ("X-Chat-Last-Given")))) {
  791. globalLastKnownFutureMessageId = Integer.parseInt(xChatLastGivenHeader);
  792. }
  793. }
  794. if (!lookingIntoFuture && inChat) {
  795. pullChatMessages(1);
  796. }
  797. } else if (response.code() == 304 && !isFromTheFuture) {
  798. if (isFirstMessagesProcessing) {
  799. NotificationUtils.cancelExistingNotifications(getApplicationContext(), conversationUser);
  800. isFirstMessagesProcessing = false;
  801. if (loadingProgressBar != null) {
  802. loadingProgressBar.setVisibility(View.GONE);
  803. }
  804. if (emptyLayout != null && emptyLayout.getVisibility() != View.VISIBLE) {
  805. emptyLayout.setVisibility(View.VISIBLE);
  806. }
  807. }
  808. historyRead = true;
  809. if (!lookingIntoFuture && inChat) {
  810. pullChatMessages(1);
  811. }
  812. }
  813. }
  814. @Override
  815. public void onLoadMore(int page, int totalItemsCount) {
  816. if (!historyRead && inChat) {
  817. pullChatMessages(0);
  818. }
  819. }
  820. @Override
  821. public String format(Date date) {
  822. if (DateFormatter.isToday(date)) {
  823. return getResources().getString(R.string.nc_date_header_today);
  824. } else if (DateFormatter.isYesterday(date)) {
  825. return getResources().getString(R.string.nc_date_header_yesterday);
  826. } else {
  827. return DateFormatter.format(date, DateFormatter.Template.STRING_DAY_MONTH_YEAR);
  828. }
  829. }
  830. @Override
  831. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  832. super.onCreateOptionsMenu(menu, inflater);
  833. inflater.inflate(R.menu.menu_conversation, menu);
  834. }
  835. @Override
  836. public boolean onOptionsItemSelected(@NonNull MenuItem item) {
  837. switch (item.getItemId()) {
  838. case android.R.id.home:
  839. getRouter().popCurrentController();
  840. return true;
  841. case R.id.conversation_video_call:
  842. startACall(false);
  843. return true;
  844. case R.id.conversation_voice_call:
  845. startACall(true);
  846. return true;
  847. default:
  848. return super.onOptionsItemSelected(item);
  849. }
  850. }
  851. private void startACall(boolean isVoiceOnlyCall) {
  852. if (!isVoiceOnlyCall) {
  853. Intent videoCallIntent = getIntentForCall(false);
  854. if (videoCallIntent != null) {
  855. startActivity(videoCallIntent);
  856. }
  857. } else {
  858. Intent voiceCallIntent = getIntentForCall(true);
  859. if (voiceCallIntent != null) {
  860. startActivity(voiceCallIntent);
  861. }
  862. }
  863. }
  864. private Intent getIntentForCall(boolean isVoiceOnlyCall) {
  865. if (currentCall != null) {
  866. Bundle bundle = new Bundle();
  867. bundle.putString(BundleKeys.KEY_ROOM_TOKEN, roomToken);
  868. bundle.putString(BundleKeys.KEY_ROOM_ID, roomId);
  869. bundle.putParcelable(BundleKeys.KEY_USER_ENTITY, Parcels.wrap(conversationUser));
  870. bundle.putString(BundleKeys.KEY_CONVERSATION_PASSWORD, roomPassword);
  871. bundle.putString(BundleKeys.KEY_MODIFIED_BASE_URL, baseUrl);
  872. if (isVoiceOnlyCall) {
  873. bundle.putBoolean(BundleKeys.KEY_CALL_VOICE_ONLY, true);
  874. }
  875. if (getActivity() != null) {
  876. Intent callIntent = new Intent(getActivity(), MagicCallActivity.class);
  877. callIntent.putExtras(bundle);
  878. return callIntent;
  879. } else {
  880. return null;
  881. }
  882. } else {
  883. return null;
  884. }
  885. }
  886. @Override
  887. public void onMessageLongClick(IMessage message) {
  888. if (getActivity() != null) {
  889. ClipboardManager clipboardManager = (android.content.ClipboardManager)
  890. getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
  891. ClipData clipData = android.content.ClipData.newPlainText(
  892. getResources().getString(R.string.nc_app_name), message.getText());
  893. if (clipboardManager != null) {
  894. clipboardManager.setPrimaryClip(clipData);
  895. }
  896. }
  897. }
  898. @Override
  899. public boolean hasContentFor(IMessage message, byte type) {
  900. switch (type) {
  901. case CONTENT_TYPE_SYSTEM_MESSAGE:
  902. return !TextUtils.isEmpty(message.getSystemMessage());
  903. }
  904. return false;
  905. }
  906. }