ContactsController.java 44 KB

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