ContactsController.java 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * @author Marcel Hibbe
  6. * Copyright (C) 2017 Mario Danic <mario@lovelyhq.com>
  7. * Copyright (C) 2022 Marcel Hibbe <dev@mhibbe.de>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. package com.nextcloud.talk.controllers;
  23. import static com.nextcloud.talk.controllers.bottomsheet.ConversationOperationEnum.*;
  24. import android.app.SearchManager;
  25. import android.content.Context;
  26. import android.graphics.PorterDuff;
  27. import android.os.Build;
  28. import android.os.Bundle;
  29. import android.text.InputType;
  30. import android.util.Log;
  31. import android.view.LayoutInflater;
  32. import android.view.Menu;
  33. import android.view.MenuInflater;
  34. import android.view.MenuItem;
  35. import android.view.View;
  36. import android.view.ViewGroup;
  37. import android.view.inputmethod.EditorInfo;
  38. import android.widget.ImageView;
  39. import android.widget.LinearLayout;
  40. import android.widget.RelativeLayout;
  41. import com.bluelinelabs.logansquare.LoganSquare;
  42. import com.nextcloud.talk.R;
  43. import com.nextcloud.talk.adapters.items.GenericTextHeaderItem;
  44. import com.nextcloud.talk.adapters.items.UserItem;
  45. import com.nextcloud.talk.api.NcApi;
  46. import com.nextcloud.talk.application.NextcloudTalkApplication;
  47. import com.nextcloud.talk.controllers.base.BaseController;
  48. import com.nextcloud.talk.events.OpenConversationEvent;
  49. import com.nextcloud.talk.jobs.AddParticipantsToConversation;
  50. import com.nextcloud.talk.models.RetrofitBucket;
  51. import com.nextcloud.talk.models.database.CapabilitiesUtil;
  52. import com.nextcloud.talk.models.database.UserEntity;
  53. import com.nextcloud.talk.models.json.autocomplete.AutocompleteOverall;
  54. import com.nextcloud.talk.models.json.autocomplete.AutocompleteUser;
  55. import com.nextcloud.talk.models.json.conversations.Conversation;
  56. import com.nextcloud.talk.models.json.conversations.RoomOverall;
  57. import com.nextcloud.talk.models.json.converters.EnumActorTypeConverter;
  58. import com.nextcloud.talk.models.json.participants.Participant;
  59. import com.nextcloud.talk.ui.dialog.ContactsBottomDialog;
  60. import com.nextcloud.talk.utils.ApiUtils;
  61. import com.nextcloud.talk.utils.ConductorRemapping;
  62. import com.nextcloud.talk.utils.bundle.BundleKeys;
  63. import com.nextcloud.talk.utils.database.user.UserUtils;
  64. import com.nextcloud.talk.utils.preferences.AppPreferences;
  65. import org.greenrobot.eventbus.EventBus;
  66. import org.greenrobot.eventbus.Subscribe;
  67. import org.greenrobot.eventbus.ThreadMode;
  68. import org.parceler.Parcels;
  69. import java.io.IOException;
  70. import java.util.ArrayList;
  71. import java.util.Collections;
  72. import java.util.HashMap;
  73. import java.util.HashSet;
  74. import java.util.List;
  75. import java.util.Locale;
  76. import java.util.Map;
  77. import java.util.Set;
  78. import javax.inject.Inject;
  79. import androidx.annotation.NonNull;
  80. import androidx.annotation.Nullable;
  81. import androidx.appcompat.widget.SearchView;
  82. import androidx.coordinatorlayout.widget.CoordinatorLayout;
  83. import androidx.core.content.res.ResourcesCompat;
  84. import androidx.core.view.MenuItemCompat;
  85. import androidx.recyclerview.widget.RecyclerView;
  86. import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
  87. import androidx.work.Data;
  88. import androidx.work.OneTimeWorkRequest;
  89. import androidx.work.WorkManager;
  90. import autodagger.AutoInjector;
  91. import butterknife.BindView;
  92. import butterknife.OnClick;
  93. import butterknife.Optional;
  94. import eu.davidea.flexibleadapter.FlexibleAdapter;
  95. import eu.davidea.flexibleadapter.SelectableAdapter;
  96. import eu.davidea.flexibleadapter.common.SmoothScrollLinearLayoutManager;
  97. import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
  98. import io.reactivex.Observer;
  99. import io.reactivex.android.schedulers.AndroidSchedulers;
  100. import io.reactivex.disposables.Disposable;
  101. import io.reactivex.schedulers.Schedulers;
  102. import okhttp3.ResponseBody;
  103. @AutoInjector(NextcloudTalkApplication.class)
  104. public class ContactsController extends BaseController implements SearchView.OnQueryTextListener,
  105. FlexibleAdapter.OnItemClickListener {
  106. public static final String TAG = "ContactsController";
  107. @Nullable
  108. @BindView(R.id.initial_relative_layout)
  109. RelativeLayout initialRelativeLayout;
  110. @Nullable
  111. @BindView(R.id.secondary_relative_layout)
  112. RelativeLayout secondaryRelativeLayout;
  113. @BindView(R.id.loading_content)
  114. LinearLayout loadingContent;
  115. @BindView(R.id.recycler_view)
  116. RecyclerView recyclerView;
  117. @BindView(R.id.swipe_refresh_layout)
  118. SwipeRefreshLayout swipeRefreshLayout;
  119. @BindView(R.id.call_header_layout)
  120. RelativeLayout conversationPrivacyToogleLayout;
  121. @BindView(R.id.joinConversationViaLinkRelativeLayout)
  122. RelativeLayout joinConversationViaLinkLayout;
  123. @BindView(R.id.joinConversationViaLinkImageView)
  124. ImageView joinConversationViaLinkImageView;
  125. @BindView(R.id.public_call_link)
  126. ImageView publicCallLinkImageView;
  127. @BindView(R.id.generic_rv_layout)
  128. CoordinatorLayout genericRvLayout;
  129. @Inject
  130. UserUtils userUtils;
  131. @Inject
  132. EventBus eventBus;
  133. @Inject
  134. AppPreferences appPreferences;
  135. @Inject
  136. NcApi ncApi;
  137. private String credentials;
  138. private UserEntity currentUser;
  139. private Disposable contactsQueryDisposable;
  140. private Disposable cacheQueryDisposable;
  141. private FlexibleAdapter adapter;
  142. private List<AbstractFlexibleItem> contactItems;
  143. private SmoothScrollLinearLayoutManager layoutManager;
  144. private MenuItem searchItem;
  145. private SearchView searchView;
  146. private boolean isNewConversationView;
  147. private boolean isPublicCall;
  148. private HashMap<String, GenericTextHeaderItem> userHeaderItems = new HashMap<>();
  149. private boolean alreadyFetching = false;
  150. private MenuItem doneMenuItem;
  151. private Set<String> selectedUserIds;
  152. private Set<String> selectedGroupIds;
  153. private Set<String> selectedCircleIds;
  154. private Set<String> selectedEmails;
  155. private List<String> existingParticipants;
  156. private boolean isAddingParticipantsView;
  157. private String conversationToken;
  158. private ContactsBottomDialog contactsBottomDialog;
  159. public ContactsController() {
  160. super();
  161. setHasOptionsMenu(true);
  162. }
  163. public ContactsController(Bundle args) {
  164. super(args);
  165. setHasOptionsMenu(true);
  166. if (args.containsKey(BundleKeys.INSTANCE.getKEY_NEW_CONVERSATION())) {
  167. isNewConversationView = true;
  168. existingParticipants = new ArrayList<>();
  169. } else if (args.containsKey(BundleKeys.INSTANCE.getKEY_ADD_PARTICIPANTS())) {
  170. isAddingParticipantsView = true;
  171. conversationToken = args.getString(BundleKeys.INSTANCE.getKEY_TOKEN());
  172. existingParticipants = new ArrayList<>();
  173. if (args.containsKey(BundleKeys.INSTANCE.getKEY_EXISTING_PARTICIPANTS())) {
  174. existingParticipants = args.getStringArrayList(BundleKeys.INSTANCE.getKEY_EXISTING_PARTICIPANTS());
  175. }
  176. }
  177. selectedUserIds = new HashSet<>();
  178. selectedGroupIds = new HashSet<>();
  179. selectedEmails = new HashSet<>();
  180. selectedCircleIds = new HashSet<>();
  181. }
  182. @Override
  183. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  184. return inflater.inflate(R.layout.controller_contacts_rv, container, false);
  185. }
  186. @Override
  187. protected void onAttach(@NonNull View view) {
  188. super.onAttach(view);
  189. eventBus.register(this);
  190. if (isNewConversationView) {
  191. toggleNewCallHeaderVisibility(!isPublicCall);
  192. }
  193. if (isAddingParticipantsView) {
  194. joinConversationViaLinkLayout.setVisibility(View.GONE);
  195. conversationPrivacyToogleLayout.setVisibility(View.GONE);
  196. }
  197. }
  198. @Override
  199. protected void onViewBound(@NonNull View view) {
  200. super.onViewBound(view);
  201. NextcloudTalkApplication.Companion.getSharedApplication().getComponentApplication().inject(this);
  202. currentUser = userUtils.getCurrentUser();
  203. if (currentUser != null) {
  204. credentials = ApiUtils.getCredentials(currentUser.getUsername(), currentUser.getToken());
  205. }
  206. if (adapter == null) {
  207. contactItems = new ArrayList<>();
  208. adapter = new FlexibleAdapter<>(contactItems, getActivity(), false);
  209. if (currentUser != null) {
  210. fetchData(true);
  211. }
  212. }
  213. setupAdapter();
  214. prepareViews();
  215. }
  216. private void setupAdapter() {
  217. adapter.setNotifyChangeOfUnfilteredItems(true)
  218. .setMode(SelectableAdapter.Mode.MULTI);
  219. adapter.setStickyHeaderElevation(5)
  220. .setUnlinkAllItemsOnRemoveHeaders(true)
  221. .setDisplayHeadersAtStartUp(true)
  222. .setStickyHeaders(true);
  223. adapter.addListener(this);
  224. }
  225. private void selectionDone() {
  226. if (!isAddingParticipantsView) {
  227. if (!isPublicCall && (selectedCircleIds.size() + selectedGroupIds.size() + selectedUserIds.size() == 1)) {
  228. String userId;
  229. String sourceType = null;
  230. String roomType = "1";
  231. if (selectedGroupIds.size() == 1) {
  232. roomType = "2";
  233. userId = selectedGroupIds.iterator().next();
  234. } else if (selectedCircleIds.size() == 1) {
  235. roomType = "2";
  236. sourceType = "circles";
  237. userId = selectedCircleIds.iterator().next();
  238. } else {
  239. userId = selectedUserIds.iterator().next();
  240. }
  241. int apiVersion = ApiUtils.getConversationApiVersion(currentUser, new int[]{ApiUtils.APIv4, 1});
  242. RetrofitBucket retrofitBucket = ApiUtils.getRetrofitBucketForCreateRoom(apiVersion,
  243. currentUser.getBaseUrl(),
  244. roomType,
  245. sourceType,
  246. userId,
  247. null);
  248. ncApi.createRoom(credentials,
  249. retrofitBucket.getUrl(), retrofitBucket.getQueryMap())
  250. .subscribeOn(Schedulers.io())
  251. .observeOn(AndroidSchedulers.mainThread())
  252. .subscribe(new Observer<RoomOverall>() {
  253. @Override
  254. public void onSubscribe(Disposable d) {
  255. }
  256. @Override
  257. public void onNext(RoomOverall roomOverall) {
  258. Bundle bundle = new Bundle();
  259. bundle.putParcelable(BundleKeys.INSTANCE.getKEY_USER_ENTITY(), currentUser);
  260. bundle.putString(BundleKeys.INSTANCE.getKEY_ROOM_TOKEN(), roomOverall.getOcs().getData().getToken());
  261. bundle.putString(BundleKeys.INSTANCE.getKEY_ROOM_ID(), roomOverall.getOcs().getData().getRoomId());
  262. // FIXME once APIv2 or later is used only, the createRoom already returns all the data
  263. ncApi.getRoom(credentials,
  264. ApiUtils.getUrlForRoom(apiVersion, currentUser.getBaseUrl(),
  265. roomOverall.getOcs().getData().getToken()))
  266. .subscribeOn(Schedulers.io())
  267. .observeOn(AndroidSchedulers.mainThread())
  268. .subscribe(new Observer<RoomOverall>() {
  269. @Override
  270. public void onSubscribe(Disposable d) {
  271. }
  272. @Override
  273. public void onNext(RoomOverall roomOverall) {
  274. bundle.putParcelable(BundleKeys.INSTANCE.getKEY_ACTIVE_CONVERSATION(),
  275. Parcels.wrap(roomOverall.getOcs().getData()));
  276. ConductorRemapping.INSTANCE.remapChatController(getRouter(), currentUser.getId(),
  277. roomOverall.getOcs().getData().getToken(), bundle, true);
  278. }
  279. @Override
  280. public void onError(Throwable e) {
  281. }
  282. @Override
  283. public void onComplete() {
  284. }
  285. });
  286. }
  287. @Override
  288. public void onError(Throwable e) {
  289. }
  290. @Override
  291. public void onComplete() {
  292. }
  293. });
  294. } else {
  295. Bundle bundle = new Bundle();
  296. Conversation.ConversationType roomType;
  297. if (isPublicCall) {
  298. roomType = Conversation.ConversationType.ROOM_PUBLIC_CALL;
  299. } else {
  300. roomType = Conversation.ConversationType.ROOM_GROUP_CALL;
  301. }
  302. ArrayList<String> userIdsArray = new ArrayList<>(selectedUserIds);
  303. ArrayList<String> groupIdsArray = new ArrayList<>(selectedGroupIds);
  304. ArrayList<String> emailsArray = new ArrayList<>(selectedEmails);
  305. ArrayList<String> circleIdsArray = new ArrayList<>(selectedCircleIds);
  306. bundle.putParcelable(BundleKeys.INSTANCE.getKEY_CONVERSATION_TYPE(), Parcels.wrap(roomType));
  307. bundle.putStringArrayList(BundleKeys.INSTANCE.getKEY_INVITED_PARTICIPANTS(), userIdsArray);
  308. bundle.putStringArrayList(BundleKeys.INSTANCE.getKEY_INVITED_GROUP(), groupIdsArray);
  309. bundle.putStringArrayList(BundleKeys.INSTANCE.getKEY_INVITED_EMAIL(), emailsArray);
  310. bundle.putStringArrayList(BundleKeys.INSTANCE.getKEY_INVITED_CIRCLE(), circleIdsArray);
  311. bundle.putSerializable(BundleKeys.INSTANCE.getKEY_OPERATION_CODE(), OPS_CODE_INVITE_USERS);
  312. prepareAndShowBottomSheetWithBundle(bundle);
  313. }
  314. } else {
  315. String[] userIdsArray = selectedUserIds.toArray(new String[selectedUserIds.size()]);
  316. String[] groupIdsArray = selectedGroupIds.toArray(new String[selectedGroupIds.size()]);
  317. String[] emailsArray = selectedEmails.toArray(new String[selectedEmails.size()]);
  318. String[] circleIdsArray = selectedCircleIds.toArray(new String[selectedCircleIds.size()]);
  319. Data.Builder data = new Data.Builder();
  320. data.putLong(BundleKeys.INSTANCE.getKEY_INTERNAL_USER_ID(), currentUser.getId());
  321. data.putString(BundleKeys.INSTANCE.getKEY_TOKEN(), conversationToken);
  322. data.putStringArray(BundleKeys.INSTANCE.getKEY_SELECTED_USERS(), userIdsArray);
  323. data.putStringArray(BundleKeys.INSTANCE.getKEY_SELECTED_GROUPS(), groupIdsArray);
  324. data.putStringArray(BundleKeys.INSTANCE.getKEY_SELECTED_EMAILS(), emailsArray);
  325. data.putStringArray(BundleKeys.INSTANCE.getKEY_SELECTED_CIRCLES(), circleIdsArray);
  326. OneTimeWorkRequest addParticipantsToConversationWorker =
  327. new OneTimeWorkRequest.Builder(AddParticipantsToConversation.class).setInputData(data.build()).build();
  328. WorkManager.getInstance().enqueue(addParticipantsToConversationWorker);
  329. getRouter().popCurrentController();
  330. }
  331. }
  332. private void initSearchView() {
  333. if (getActivity() != null) {
  334. SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
  335. if (searchItem != null) {
  336. searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
  337. searchView.setMaxWidth(Integer.MAX_VALUE);
  338. searchView.setInputType(InputType.TYPE_TEXT_VARIATION_FILTER);
  339. int imeOptions = EditorInfo.IME_ACTION_DONE | EditorInfo.IME_FLAG_NO_FULLSCREEN;
  340. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && appPreferences.getIsKeyboardIncognito()) {
  341. imeOptions |= EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING;
  342. }
  343. searchView.setImeOptions(imeOptions);
  344. searchView.setQueryHint(getResources().getString(R.string.nc_search));
  345. if (searchManager != null) {
  346. searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
  347. }
  348. searchView.setOnQueryTextListener(this);
  349. }
  350. }
  351. }
  352. @Override
  353. public boolean onOptionsItemSelected(@NonNull MenuItem item) {
  354. int itemId = item.getItemId();
  355. if (itemId == android.R.id.home) {
  356. return getRouter().popCurrentController();
  357. } else if (itemId == R.id.contacts_selection_done) {
  358. selectionDone();
  359. return true;
  360. }
  361. return super.onOptionsItemSelected(item);
  362. }
  363. @Override
  364. public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
  365. super.onCreateOptionsMenu(menu, inflater);
  366. inflater.inflate(R.menu.menu_contacts, menu);
  367. searchItem = menu.findItem(R.id.action_search);
  368. doneMenuItem = menu.findItem(R.id.contacts_selection_done);
  369. initSearchView();
  370. }
  371. @Override
  372. public void onPrepareOptionsMenu(@NonNull Menu menu) {
  373. super.onPrepareOptionsMenu(menu);
  374. checkAndHandleDoneMenuItem();
  375. if (adapter.hasFilter()) {
  376. searchItem.expandActionView();
  377. searchView.setQuery((CharSequence) adapter.getFilter(String.class), false);
  378. }
  379. }
  380. private void fetchData(boolean startFromScratch) {
  381. dispose(null);
  382. alreadyFetching = true;
  383. Set<AutocompleteUser> autocompleteUsersHashSet = new HashSet<>();
  384. userHeaderItems = new HashMap<>();
  385. String query = (String) adapter.getFilter(String.class);
  386. RetrofitBucket retrofitBucket = ApiUtils.getRetrofitBucketForContactsSearchFor14(currentUser.getBaseUrl(), query);
  387. Map<String, Object> modifiedQueryMap = new HashMap<String, Object>(retrofitBucket.getQueryMap());
  388. modifiedQueryMap.put("limit", 50);
  389. if (isAddingParticipantsView) {
  390. modifiedQueryMap.put("itemId", conversationToken);
  391. }
  392. List<String> shareTypesList;
  393. shareTypesList = new ArrayList<>();
  394. // users
  395. shareTypesList.add("0");
  396. if (!isAddingParticipantsView) {
  397. // groups
  398. shareTypesList.add("1");
  399. } else if (CapabilitiesUtil.hasSpreedFeatureCapability(currentUser, "invite-groups-and-mails")) {
  400. // groups
  401. shareTypesList.add("1");
  402. // emails
  403. shareTypesList.add("4");
  404. }
  405. if (CapabilitiesUtil.hasSpreedFeatureCapability(currentUser, "circles-support")) {
  406. // circles
  407. shareTypesList.add("7");
  408. }
  409. modifiedQueryMap.put("shareTypes[]", shareTypesList);
  410. ncApi.getContactsWithSearchParam(
  411. credentials,
  412. retrofitBucket.getUrl(), shareTypesList, modifiedQueryMap)
  413. .subscribeOn(Schedulers.io())
  414. .observeOn(AndroidSchedulers.mainThread())
  415. .retry(3)
  416. .subscribe(new Observer<ResponseBody>() {
  417. @Override
  418. public void onSubscribe(Disposable d) {
  419. contactsQueryDisposable = d;
  420. }
  421. @Override
  422. public void onNext(ResponseBody responseBody) {
  423. if (responseBody != null) {
  424. Participant participant;
  425. List<AbstractFlexibleItem> newUserItemList = new ArrayList<>();
  426. EnumActorTypeConverter actorTypeConverter = new EnumActorTypeConverter();
  427. try {
  428. AutocompleteOverall autocompleteOverall = LoganSquare.parse(
  429. responseBody.string(),
  430. AutocompleteOverall.class);
  431. autocompleteUsersHashSet.addAll(autocompleteOverall.getOcs().getData());
  432. for (AutocompleteUser autocompleteUser : autocompleteUsersHashSet) {
  433. if (!autocompleteUser.getId().equals(currentUser.getUserId())
  434. && !existingParticipants.contains(autocompleteUser.getId())) {
  435. participant = new Participant();
  436. participant.setActorId(autocompleteUser.getId());
  437. participant.setActorType(actorTypeConverter.getFromString(autocompleteUser.getSource()));
  438. participant.setDisplayName(autocompleteUser.getLabel());
  439. participant.setSource(autocompleteUser.getSource());
  440. String headerTitle;
  441. if (participant.getActorType() == Participant.ActorType.GROUPS) {
  442. headerTitle = getResources().getString(R.string.nc_groups);
  443. } else if (participant.getActorType() == Participant.ActorType.CIRCLES) {
  444. headerTitle = getResources().getString(R.string.nc_circles);
  445. } else {
  446. headerTitle =
  447. participant.getDisplayName().substring(0, 1).toUpperCase(Locale.getDefault());
  448. }
  449. GenericTextHeaderItem genericTextHeaderItem;
  450. if (!userHeaderItems.containsKey(headerTitle)) {
  451. genericTextHeaderItem = new GenericTextHeaderItem(headerTitle);
  452. userHeaderItems.put(headerTitle, genericTextHeaderItem);
  453. }
  454. UserItem newContactItem = new UserItem(
  455. participant,
  456. currentUser,
  457. userHeaderItems.get(headerTitle)
  458. );
  459. if (!contactItems.contains(newContactItem)) {
  460. newUserItemList.add(newContactItem);
  461. }
  462. }
  463. }
  464. } catch (IOException ioe) {
  465. Log.e(TAG, "Parsing response body failed while getting contacts", ioe);
  466. }
  467. userHeaderItems = new HashMap<>();
  468. contactItems.addAll(newUserItemList);
  469. Collections.sort(newUserItemList, (o1, o2) -> {
  470. String firstName;
  471. String secondName;
  472. if (o1 instanceof UserItem) {
  473. firstName = ((UserItem) o1).getModel().getDisplayName();
  474. } else {
  475. firstName = ((GenericTextHeaderItem) o1).getModel();
  476. }
  477. if (o2 instanceof UserItem) {
  478. secondName = ((UserItem) o2).getModel().getDisplayName();
  479. } else {
  480. secondName = ((GenericTextHeaderItem) o2).getModel();
  481. }
  482. if (o1 instanceof UserItem && o2 instanceof UserItem) {
  483. String firstSource = ((UserItem) o1).getModel().getSource();
  484. String secondSource = ((UserItem) o2).getModel().getSource();
  485. if (firstSource.equals(secondSource)) {
  486. return firstName.compareToIgnoreCase(secondName);
  487. }
  488. // First users
  489. if ("users".equals(firstSource)) {
  490. return -1;
  491. } else if ("users".equals(secondSource)) {
  492. return 1;
  493. }
  494. // Then groups
  495. if ("groups".equals(firstSource)) {
  496. return -1;
  497. } else if ("groups".equals(secondSource)) {
  498. return 1;
  499. }
  500. // Then circles
  501. if ("circles".equals(firstSource)) {
  502. return -1;
  503. } else if ("circles".equals(secondSource)) {
  504. return 1;
  505. }
  506. // Otherwise fall back to name sorting
  507. return firstName.compareToIgnoreCase(secondName);
  508. }
  509. return firstName.compareToIgnoreCase(secondName);
  510. });
  511. Collections.sort(contactItems, (o1, o2) -> {
  512. String firstName;
  513. String secondName;
  514. if (o1 instanceof UserItem) {
  515. firstName = ((UserItem) o1).getModel().getDisplayName();
  516. } else {
  517. firstName = ((GenericTextHeaderItem) o1).getModel();
  518. }
  519. if (o2 instanceof UserItem) {
  520. secondName = ((UserItem) o2).getModel().getDisplayName();
  521. } else {
  522. secondName = ((GenericTextHeaderItem) o2).getModel();
  523. }
  524. if (o1 instanceof UserItem && o2 instanceof UserItem) {
  525. if ("groups".equals(((UserItem) o1).getModel().getSource()) && "groups".equals(((UserItem) o2).getModel().getSource())) {
  526. return firstName.compareToIgnoreCase(secondName);
  527. } else if ("groups".equals(((UserItem) o1).getModel().getSource())) {
  528. return -1;
  529. } else if ("groups".equals(((UserItem) o2).getModel().getSource())) {
  530. return 1;
  531. }
  532. }
  533. return firstName.compareToIgnoreCase(secondName);
  534. });
  535. if (newUserItemList.size() > 0) {
  536. adapter.updateDataSet(newUserItemList);
  537. } else {
  538. adapter.filterItems();
  539. }
  540. if (swipeRefreshLayout != null) {
  541. swipeRefreshLayout.setRefreshing(false);
  542. }
  543. }
  544. }
  545. @Override
  546. public void onError(Throwable e) {
  547. if (swipeRefreshLayout != null) {
  548. swipeRefreshLayout.setRefreshing(false);
  549. }
  550. dispose(contactsQueryDisposable);
  551. }
  552. @Override
  553. public void onComplete() {
  554. if (swipeRefreshLayout != null) {
  555. swipeRefreshLayout.setRefreshing(false);
  556. }
  557. dispose(contactsQueryDisposable);
  558. alreadyFetching = false;
  559. disengageProgressBar();
  560. }
  561. });
  562. }
  563. private void prepareViews() {
  564. layoutManager = new SmoothScrollLinearLayoutManager(getActivity());
  565. recyclerView.setLayoutManager(layoutManager);
  566. recyclerView.setHasFixedSize(true);
  567. recyclerView.setAdapter(adapter);
  568. swipeRefreshLayout.setOnRefreshListener(() -> fetchData(true));
  569. swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
  570. swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.refresh_spinner_background);
  571. joinConversationViaLinkImageView
  572. .getBackground()
  573. .setColorFilter(ResourcesCompat.getColor(getResources(), R.color.colorBackgroundDarker, null),
  574. PorterDuff.Mode.SRC_IN);
  575. publicCallLinkImageView
  576. .getBackground()
  577. .setColorFilter(ResourcesCompat.getColor(getResources(), R.color.colorPrimary, null),
  578. PorterDuff.Mode.SRC_IN);
  579. disengageProgressBar();
  580. }
  581. private void disengageProgressBar() {
  582. if (!alreadyFetching) {
  583. loadingContent.setVisibility(View.GONE);
  584. genericRvLayout.setVisibility(View.VISIBLE);
  585. if (isNewConversationView) {
  586. conversationPrivacyToogleLayout.setVisibility(View.VISIBLE);
  587. joinConversationViaLinkLayout.setVisibility(View.VISIBLE);
  588. }
  589. }
  590. }
  591. private void dispose(@Nullable Disposable disposable) {
  592. if (disposable != null && !disposable.isDisposed()) {
  593. disposable.dispose();
  594. } else if (disposable == null) {
  595. if (contactsQueryDisposable != null && !contactsQueryDisposable.isDisposed()) {
  596. contactsQueryDisposable.dispose();
  597. contactsQueryDisposable = null;
  598. }
  599. if (cacheQueryDisposable != null && !cacheQueryDisposable.isDisposed()) {
  600. cacheQueryDisposable.dispose();
  601. cacheQueryDisposable = null;
  602. }
  603. }
  604. }
  605. @Override
  606. public void onSaveViewState(@NonNull View view, @NonNull Bundle outState) {
  607. adapter.onSaveInstanceState(outState);
  608. super.onSaveViewState(view, outState);
  609. }
  610. @Override
  611. public void onRestoreViewState(@NonNull View view, @NonNull Bundle savedViewState) {
  612. super.onRestoreViewState(view, savedViewState);
  613. if (adapter != null) {
  614. adapter.onRestoreInstanceState(savedViewState);
  615. }
  616. }
  617. @Override
  618. public void onDestroy() {
  619. super.onDestroy();
  620. dispose(null);
  621. }
  622. @Override
  623. public boolean onQueryTextChange(String newText) {
  624. if (!newText.equals("") && adapter.hasNewFilter(newText)) {
  625. adapter.setFilter(newText);
  626. fetchData(true);
  627. } else if (newText.equals("")) {
  628. adapter.setFilter("");
  629. adapter.updateDataSet(contactItems);
  630. }
  631. if (swipeRefreshLayout != null) {
  632. swipeRefreshLayout.setEnabled(!adapter.hasFilter());
  633. }
  634. return true;
  635. }
  636. @Override
  637. public boolean onQueryTextSubmit(String query) {
  638. return onQueryTextChange(query);
  639. }
  640. private void checkAndHandleDoneMenuItem() {
  641. if (adapter != null && doneMenuItem != null) {
  642. if ((selectedCircleIds.size() + selectedEmails.size() + selectedGroupIds.size() + selectedUserIds.size() > 0) || isPublicCall) {
  643. doneMenuItem.setVisible(true);
  644. } else {
  645. doneMenuItem.setVisible(false);
  646. }
  647. } else if (doneMenuItem != null) {
  648. doneMenuItem.setVisible(false);
  649. }
  650. }
  651. @Override
  652. protected String getTitle() {
  653. if (isAddingParticipantsView) {
  654. return getResources().getString(R.string.nc_add_participants);
  655. } else if (isNewConversationView) {
  656. return getResources().getString(R.string.nc_select_participants);
  657. } else {
  658. return getResources().getString(R.string.nc_app_product_name);
  659. }
  660. }
  661. private void prepareAndShowBottomSheetWithBundle(Bundle bundle) {
  662. // 11: create conversation-enter name for new conversation
  663. // 10: get&join room when enter link
  664. contactsBottomDialog = new ContactsBottomDialog(getActivity(), bundle);
  665. contactsBottomDialog.show();
  666. }
  667. @Subscribe(threadMode = ThreadMode.MAIN)
  668. public void onMessageEvent(OpenConversationEvent openConversationEvent) {
  669. ConductorRemapping.INSTANCE.remapChatController(getRouter(), currentUser.getId(),
  670. openConversationEvent.getConversation().getToken(),
  671. openConversationEvent.getBundle(), true);
  672. if (contactsBottomDialog != null) {
  673. contactsBottomDialog.dismiss();
  674. }
  675. }
  676. @Override
  677. protected void onDetach(@NonNull View view) {
  678. super.onDetach(view);
  679. eventBus.unregister(this);
  680. }
  681. @Override
  682. public boolean onItemClick(View view, int position) {
  683. if (adapter.getItem(position) instanceof UserItem) {
  684. if (!isNewConversationView && !isAddingParticipantsView) {
  685. UserItem userItem = (UserItem) adapter.getItem(position);
  686. String roomType = "1";
  687. if ("groups".equals(userItem.getModel().getSource())) {
  688. roomType = "2";
  689. }
  690. int apiVersion = ApiUtils.getConversationApiVersion(currentUser, new int[]{ApiUtils.APIv4, 1});
  691. RetrofitBucket retrofitBucket = ApiUtils.getRetrofitBucketForCreateRoom(apiVersion,
  692. currentUser.getBaseUrl(),
  693. roomType,
  694. null,
  695. userItem.getModel().getActorId(),
  696. null);
  697. ncApi.createRoom(credentials,
  698. retrofitBucket.getUrl(), retrofitBucket.getQueryMap())
  699. .subscribeOn(Schedulers.io())
  700. .observeOn(AndroidSchedulers.mainThread())
  701. .subscribe(new Observer<RoomOverall>() {
  702. @Override
  703. public void onSubscribe(Disposable d) {
  704. }
  705. @Override
  706. public void onNext(RoomOverall roomOverall) {
  707. if (getActivity() != null) {
  708. Bundle bundle = new Bundle();
  709. bundle.putParcelable(BundleKeys.INSTANCE.getKEY_USER_ENTITY(), currentUser);
  710. bundle.putString(BundleKeys.INSTANCE.getKEY_ROOM_TOKEN(), roomOverall.getOcs().getData().getToken());
  711. bundle.putString(BundleKeys.INSTANCE.getKEY_ROOM_ID(), roomOverall.getOcs().getData().getRoomId());
  712. bundle.putParcelable(BundleKeys.INSTANCE.getKEY_ACTIVE_CONVERSATION(),
  713. Parcels.wrap(roomOverall.getOcs().getData()));
  714. ConductorRemapping.INSTANCE.remapChatController(getRouter(), currentUser.getId(),
  715. roomOverall.getOcs().getData().getToken(), bundle, true);
  716. }
  717. }
  718. @Override
  719. public void onError(Throwable e) {
  720. }
  721. @Override
  722. public void onComplete() {
  723. }
  724. });
  725. } else {
  726. Participant participant = ((UserItem) adapter.getItem(position)).getModel();
  727. participant.setSelected(!participant.isSelected());
  728. if ("groups".equals(participant.getSource())) {
  729. if (participant.isSelected()) {
  730. selectedGroupIds.add(participant.getActorId());
  731. } else {
  732. selectedGroupIds.remove(participant.getActorId());
  733. }
  734. } else if ("emails".equals(participant.getSource())) {
  735. if (participant.isSelected()) {
  736. selectedEmails.add(participant.getActorId());
  737. } else {
  738. selectedEmails.remove(participant.getActorId());
  739. }
  740. } else if ("circles".equals(participant.getSource())) {
  741. if (participant.isSelected()) {
  742. selectedCircleIds.add(participant.getActorId());
  743. } else {
  744. selectedCircleIds.remove(participant.getActorId());
  745. }
  746. } else {
  747. if (participant.isSelected()) {
  748. selectedUserIds.add(participant.getActorId());
  749. } else {
  750. selectedUserIds.remove(participant.getActorId());
  751. }
  752. }
  753. if (CapabilitiesUtil.hasSpreedFeatureCapability(currentUser, "last-room-activity")
  754. && !CapabilitiesUtil.hasSpreedFeatureCapability(currentUser, "invite-groups-and-mails") &&
  755. "groups".equals(((UserItem) adapter.getItem(position)).getModel().getSource()) &&
  756. participant.isSelected() &&
  757. adapter.getSelectedItemCount() > 1) {
  758. List<UserItem> currentItems = adapter.getCurrentItems();
  759. Participant internalParticipant;
  760. for (int i = 0; i < currentItems.size(); i++) {
  761. internalParticipant = currentItems.get(i).getModel();
  762. if (internalParticipant.getActorId().equals(participant.getActorId()) &&
  763. internalParticipant.getActorType() == Participant.ActorType.GROUPS &&
  764. internalParticipant.isSelected()) {
  765. internalParticipant.setSelected(false);
  766. selectedGroupIds.remove(internalParticipant.getActorId());
  767. }
  768. }
  769. }
  770. adapter.notifyDataSetChanged();
  771. checkAndHandleDoneMenuItem();
  772. }
  773. }
  774. return true;
  775. }
  776. @Optional
  777. @OnClick(R.id.joinConversationViaLinkRelativeLayout)
  778. void joinConversationViaLink() {
  779. Bundle bundle = new Bundle();
  780. bundle.putSerializable(BundleKeys.INSTANCE.getKEY_OPERATION_CODE(), OPS_CODE_GET_AND_JOIN_ROOM);
  781. prepareAndShowBottomSheetWithBundle(bundle);
  782. }
  783. @Optional
  784. @OnClick(R.id.call_header_layout)
  785. void toggleCallHeader() {
  786. toggleNewCallHeaderVisibility(isPublicCall);
  787. isPublicCall = !isPublicCall;
  788. if (isPublicCall) {
  789. joinConversationViaLinkLayout.setVisibility(View.GONE);
  790. } else {
  791. joinConversationViaLinkLayout.setVisibility(View.VISIBLE);
  792. }
  793. if (isPublicCall) {
  794. List<AbstractFlexibleItem> currentItems = adapter.getCurrentItems();
  795. Participant internalParticipant;
  796. for (int i = 0; i < currentItems.size(); i++) {
  797. if (currentItems.get(i) instanceof UserItem) {
  798. internalParticipant = ((UserItem) currentItems.get(i)).getModel();
  799. if (internalParticipant.getActorType() == Participant.ActorType.GROUPS &&
  800. internalParticipant.isSelected()) {
  801. internalParticipant.setSelected(false);
  802. selectedGroupIds.remove(internalParticipant.getActorId());
  803. }
  804. }
  805. }
  806. }
  807. for (int i = 0; i < adapter.getItemCount(); i++) {
  808. if (adapter.getItem(i) instanceof UserItem) {
  809. UserItem userItem = (UserItem) adapter.getItem(i);
  810. if ("groups".equals(userItem.getModel().getSource())) {
  811. userItem.setEnabled(!isPublicCall);
  812. }
  813. }
  814. }
  815. checkAndHandleDoneMenuItem();
  816. adapter.notifyDataSetChanged();
  817. }
  818. private void toggleNewCallHeaderVisibility(boolean showInitialLayout) {
  819. if (showInitialLayout) {
  820. if (initialRelativeLayout != null) {
  821. initialRelativeLayout.setVisibility(View.VISIBLE);
  822. }
  823. if (secondaryRelativeLayout != null) {
  824. secondaryRelativeLayout.setVisibility(View.GONE);
  825. }
  826. } else {
  827. if (initialRelativeLayout != null) {
  828. initialRelativeLayout.setVisibility(View.GONE);
  829. }
  830. if (secondaryRelativeLayout != null) {
  831. secondaryRelativeLayout.setVisibility(View.VISIBLE);
  832. }
  833. }
  834. }
  835. }