ConversationInfoController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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.app.Activity;
  22. import android.os.Bundle;
  23. import android.text.TextUtils;
  24. import android.view.LayoutInflater;
  25. import android.view.MenuItem;
  26. import android.view.View;
  27. import android.view.ViewGroup;
  28. import android.widget.ProgressBar;
  29. import com.facebook.drawee.backends.pipeline.Fresco;
  30. import com.facebook.drawee.interfaces.DraweeController;
  31. import com.facebook.drawee.view.SimpleDraweeView;
  32. import com.nextcloud.talk.R;
  33. import com.nextcloud.talk.adapters.items.UserItem;
  34. import com.nextcloud.talk.api.NcApi;
  35. import com.nextcloud.talk.application.NextcloudTalkApplication;
  36. import com.nextcloud.talk.controllers.base.BaseController;
  37. import com.nextcloud.talk.models.database.UserEntity;
  38. import com.nextcloud.talk.models.json.converters.EnumNotificationLevelConverter;
  39. import com.nextcloud.talk.models.json.participants.Participant;
  40. import com.nextcloud.talk.models.json.participants.ParticipantsOverall;
  41. import com.nextcloud.talk.models.json.rooms.Conversation;
  42. import com.nextcloud.talk.models.json.rooms.RoomOverall;
  43. import com.nextcloud.talk.utils.ApiUtils;
  44. import com.nextcloud.talk.utils.DisplayUtils;
  45. import com.nextcloud.talk.utils.bundle.BundleKeys;
  46. import com.nextcloud.talk.utils.preferencestorage.DatabaseStorageModule;
  47. import com.vanniktech.emoji.EmojiTextView;
  48. import com.yarolegovich.mp.MaterialChoicePreference;
  49. import com.yarolegovich.mp.MaterialPreferenceCategory;
  50. import com.yarolegovich.mp.MaterialPreferenceScreen;
  51. import org.parceler.Parcels;
  52. import java.util.ArrayList;
  53. import java.util.List;
  54. import javax.inject.Inject;
  55. import androidx.annotation.NonNull;
  56. import androidx.recyclerview.widget.RecyclerView;
  57. import autodagger.AutoInjector;
  58. import butterknife.BindView;
  59. import eu.davidea.flexibleadapter.FlexibleAdapter;
  60. import eu.davidea.flexibleadapter.common.SmoothScrollLinearLayoutManager;
  61. import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
  62. import io.reactivex.Observer;
  63. import io.reactivex.android.schedulers.AndroidSchedulers;
  64. import io.reactivex.disposables.Disposable;
  65. import io.reactivex.schedulers.Schedulers;
  66. @AutoInjector(NextcloudTalkApplication.class)
  67. public class ConversationInfoController extends BaseController {
  68. private String baseUrl;
  69. private String conversationToken;
  70. private UserEntity conversationUser;
  71. private String credentials;
  72. @BindView(R.id.notification_settings)
  73. MaterialPreferenceScreen materialPreferenceScreen;
  74. @BindView(R.id.progressBar)
  75. ProgressBar progressBar;
  76. @BindView(R.id.conversation_info_message_notifications)
  77. MaterialChoicePreference messageNotificationLevel;
  78. @BindView(R.id.conversation_info_name)
  79. MaterialPreferenceCategory nameCategoryView;
  80. @BindView(R.id.avatar_image)
  81. SimpleDraweeView conversationAvatarImageView;
  82. @BindView(R.id.display_name_text)
  83. EmojiTextView conversationDisplayName;
  84. @BindView(R.id.participants_list_category)
  85. MaterialPreferenceCategory participantsListCategory;
  86. @BindView(R.id.recycler_view)
  87. RecyclerView recyclerView;
  88. @Inject
  89. NcApi ncApi;
  90. private Disposable roomDisposable;
  91. private Disposable participantsDisposable;
  92. private Conversation conversation;
  93. private FlexibleAdapter<AbstractFlexibleItem> adapter;
  94. private List<AbstractFlexibleItem> recyclerViewItems = new ArrayList<>();
  95. public ConversationInfoController(Bundle args) {
  96. super(args);
  97. setHasOptionsMenu(true);
  98. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  99. conversationUser = Parcels.unwrap(args.getParcelable(BundleKeys.KEY_USER_ENTITY));
  100. conversationToken = args.getString(BundleKeys.KEY_ROOM_TOKEN);
  101. baseUrl = args.getString(BundleKeys.KEY_BASE_URL);
  102. credentials = ApiUtils.getCredentials(conversationUser.getUsername(), conversationUser.getToken());
  103. }
  104. @Override
  105. public boolean onOptionsItemSelected(@NonNull MenuItem item) {
  106. switch (item.getItemId()) {
  107. case android.R.id.home:
  108. getRouter().popCurrentController();
  109. return true;
  110. default:
  111. return super.onOptionsItemSelected(item);
  112. }
  113. }
  114. @Override
  115. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  116. return inflater.inflate(R.layout.controller_conversation_info, container, false);
  117. }
  118. @Override
  119. protected void onViewBound(@NonNull View view) {
  120. super.onViewBound(view);
  121. materialPreferenceScreen.setStorageModule(new DatabaseStorageModule(conversationUser, conversationToken));
  122. if (adapter == null) {
  123. fetchRoomInfo();
  124. } else {
  125. loadConversationAvatar();
  126. materialPreferenceScreen.setVisibility(View.VISIBLE);
  127. nameCategoryView.setVisibility(View.VISIBLE);
  128. participantsListCategory.setVisibility(View.VISIBLE);
  129. progressBar.setVisibility(View.GONE);
  130. conversationDisplayName.setText(conversation.getDisplayName());
  131. setupAdapter();
  132. }
  133. }
  134. private void setupAdapter() {
  135. Activity activity;
  136. if ((activity = getActivity()) != null) {
  137. if (adapter == null) {
  138. adapter = new FlexibleAdapter<>(recyclerViewItems, activity, true);
  139. }
  140. if (recyclerView != null) {
  141. SmoothScrollLinearLayoutManager layoutManager =
  142. new SmoothScrollLinearLayoutManager(activity);
  143. recyclerView.setLayoutManager(layoutManager);
  144. recyclerView.setHasFixedSize(true);
  145. recyclerView.setAdapter(adapter);
  146. }
  147. }
  148. }
  149. private void handleParticipants(List<Participant> participants) {
  150. UserItem userItem;
  151. Participant participant;
  152. recyclerViewItems = new ArrayList<>();
  153. UserItem ownUserItem = null;
  154. for (int i = 0; i < participants.size(); i++) {
  155. participant = participants.get(i);
  156. userItem = new UserItem(participant, conversationUser, null);
  157. userItem.setEnabled(!participant.getSessionId().equals("0"));
  158. if (!TextUtils.isEmpty(participant.getUserId()) && participant.getUserId().equals(conversationUser.getUserId())) {
  159. ownUserItem = userItem;
  160. userItem.getModel().setSessionId("-1");
  161. userItem.setEnabled(true);
  162. } else {
  163. recyclerViewItems.add(userItem);
  164. }
  165. }
  166. if (ownUserItem != null) {
  167. recyclerViewItems.add(0, ownUserItem);
  168. }
  169. setupAdapter();
  170. if (participantsListCategory != null) {
  171. participantsListCategory.setVisibility(View.VISIBLE);
  172. }
  173. adapter.notifyDataSetChanged();
  174. }
  175. @Override
  176. protected String getTitle() {
  177. return getResources().getString(R.string.nc_conversation_menu_conversation_info);
  178. }
  179. private void getListOfParticipants() {
  180. ncApi.getPeersForCall(credentials, ApiUtils.getUrlForParticipants(conversationUser.getBaseUrl(), conversationToken))
  181. .subscribeOn(Schedulers.newThread())
  182. .observeOn(AndroidSchedulers.mainThread())
  183. .subscribe(new Observer<ParticipantsOverall>() {
  184. @Override
  185. public void onSubscribe(Disposable d) {
  186. participantsDisposable = d;
  187. }
  188. @Override
  189. public void onNext(ParticipantsOverall participantsOverall) {
  190. handleParticipants(participantsOverall.getOcs().getData());
  191. }
  192. @Override
  193. public void onError(Throwable e) {
  194. }
  195. @Override
  196. public void onComplete() {
  197. participantsDisposable.dispose();
  198. }
  199. });
  200. }
  201. private void fetchRoomInfo() {
  202. ncApi.getRoom(credentials, ApiUtils.getRoom(conversationUser.getBaseUrl(), conversationToken))
  203. .subscribeOn(Schedulers.newThread())
  204. .observeOn(AndroidSchedulers.mainThread())
  205. .subscribe(new Observer<RoomOverall>() {
  206. @Override
  207. public void onSubscribe(Disposable d) {
  208. roomDisposable = d;
  209. }
  210. @Override
  211. public void onNext(RoomOverall roomOverall) {
  212. conversation = roomOverall.getOcs().getData();
  213. getListOfParticipants();
  214. if (progressBar != null) {
  215. progressBar.setVisibility(View.GONE);
  216. }
  217. if (nameCategoryView != null) {
  218. nameCategoryView.setVisibility(View.VISIBLE);
  219. }
  220. if (conversationDisplayName != null) {
  221. conversationDisplayName.setText(conversation.getDisplayName());
  222. }
  223. loadConversationAvatar();
  224. if (conversationUser.hasSpreedCapabilityWithName("notification-levels")) {
  225. if (messageNotificationLevel != null) {
  226. messageNotificationLevel.setEnabled(true);
  227. messageNotificationLevel.setAlpha(1.0f);
  228. }
  229. if (!conversation.getNotificationLevel().equals(Conversation.NotificationLevel.DEFAULT)) {
  230. String stringValue;
  231. switch (new EnumNotificationLevelConverter().convertToInt(conversation.getNotificationLevel())) {
  232. case 1:
  233. stringValue = "always";
  234. break;
  235. case 2:
  236. stringValue = "mention";
  237. break;
  238. case 3:
  239. stringValue = "never";
  240. break;
  241. default:
  242. stringValue = "mention";
  243. break;
  244. }
  245. if (messageNotificationLevel != null) {
  246. messageNotificationLevel.setValue(stringValue);
  247. }
  248. } else {
  249. setProperNotificationValue(conversation);
  250. }
  251. } else {
  252. messageNotificationLevel.setEnabled(false);
  253. messageNotificationLevel.setAlpha(0.38f);
  254. setProperNotificationValue(conversation);
  255. }
  256. materialPreferenceScreen.setVisibility(View.VISIBLE);
  257. }
  258. @Override
  259. public void onError(Throwable e) {
  260. }
  261. @Override
  262. public void onComplete() {
  263. roomDisposable.dispose();
  264. }
  265. });
  266. }
  267. private void setProperNotificationValue(Conversation conversation) {
  268. if (messageNotificationLevel != null) {
  269. if (conversation.getType().equals(Conversation.RoomType.ROOM_TYPE_ONE_TO_ONE_CALL)) {
  270. // hack to see if we get mentioned always or just on mention
  271. if (conversationUser.hasSpreedCapabilityWithName("mention-flag")) {
  272. messageNotificationLevel.setValue("always");
  273. } else {
  274. messageNotificationLevel.setValue("mention");
  275. }
  276. } else {
  277. messageNotificationLevel.setValue("mention");
  278. }
  279. }
  280. }
  281. private void loadConversationAvatar() {
  282. if (conversationAvatarImageView != null) {
  283. switch (conversation.getType()) {
  284. case ROOM_TYPE_ONE_TO_ONE_CALL:
  285. if (!TextUtils.isEmpty(conversation.getName())) {
  286. DraweeController draweeController = Fresco.newDraweeControllerBuilder()
  287. .setOldController(conversationAvatarImageView.getController())
  288. .setAutoPlayAnimations(true)
  289. .setImageRequest(DisplayUtils.getImageRequestForUrl(ApiUtils.getUrlForAvatarWithName(conversationUser.getBaseUrl(),
  290. conversation.getName(), R.dimen.avatar_size_big)))
  291. .build();
  292. conversationAvatarImageView.setController(draweeController);
  293. }
  294. break;
  295. case ROOM_GROUP_CALL:
  296. conversationAvatarImageView.getHierarchy().setPlaceholderImage(DisplayUtils
  297. .getRoundedBitmapDrawableFromVectorDrawableResource(getResources(),
  298. R.drawable.ic_people_group_white_24px));
  299. break;
  300. case ROOM_PUBLIC_CALL:
  301. conversationAvatarImageView.getHierarchy().setPlaceholderImage(DisplayUtils
  302. .getRoundedBitmapDrawableFromVectorDrawableResource(getResources(),
  303. R.drawable.ic_link_white_24px));
  304. break;
  305. default:
  306. break;
  307. }
  308. }
  309. }
  310. }