1/*
2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30package eu.siacs.conversations.ui;
31
32import android.app.Activity;
33import android.app.AlertDialog;
34import android.app.Fragment;
35import android.content.Intent;
36import android.graphics.Canvas;
37import android.graphics.Paint;
38import android.os.Bundle;
39import android.util.Log;
40import android.view.ContextMenu;
41import android.view.LayoutInflater;
42import android.view.Menu;
43import android.view.MenuInflater;
44import android.view.MenuItem;
45import android.view.View;
46import android.view.ViewGroup;
47import android.widget.AdapterView.AdapterContextMenuInfo;
48import android.widget.Toast;
49
50import androidx.annotation.NonNull;
51import androidx.databinding.DataBindingUtil;
52import androidx.recyclerview.widget.ItemTouchHelper;
53import androidx.recyclerview.widget.LinearLayoutManager;
54import androidx.recyclerview.widget.RecyclerView;
55
56import com.google.android.material.color.MaterialColors;
57import com.google.android.material.dialog.MaterialAlertDialogBuilder;
58import com.google.android.material.snackbar.Snackbar;
59import com.google.common.base.Optional;
60import com.google.common.collect.Collections2;
61
62import java.util.ArrayList;
63import java.util.List;
64import java.util.concurrent.atomic.AtomicReference;
65
66import eu.siacs.conversations.BuildConfig;
67import eu.siacs.conversations.Config;
68import eu.siacs.conversations.R;
69import eu.siacs.conversations.databinding.FragmentConversationsOverviewBinding;
70import eu.siacs.conversations.entities.Account;
71import eu.siacs.conversations.entities.Conversation;
72import eu.siacs.conversations.entities.Conversational;
73import eu.siacs.conversations.services.XmppConnectionService;
74import eu.siacs.conversations.services.QuickConversationsService;
75import eu.siacs.conversations.ui.adapter.ConversationAdapter;
76import eu.siacs.conversations.ui.interfaces.OnConversationArchived;
77import eu.siacs.conversations.ui.interfaces.OnConversationSelected;
78import eu.siacs.conversations.ui.util.MenuDoubleTabUtil;
79import eu.siacs.conversations.ui.util.PendingActionHelper;
80import eu.siacs.conversations.ui.util.PendingItem;
81import eu.siacs.conversations.ui.util.ScrollState;
82import eu.siacs.conversations.utils.AccountUtils;
83import eu.siacs.conversations.utils.EasyOnboardingInvite;
84import eu.siacs.conversations.utils.ThemeHelper;
85import eu.siacs.conversations.xmpp.jingle.OngoingRtpSession;
86
87import static androidx.recyclerview.widget.ItemTouchHelper.LEFT;
88import static androidx.recyclerview.widget.ItemTouchHelper.RIGHT;
89
90public class ConversationsOverviewFragment extends XmppFragment {
91
92 private static final String STATE_SCROLL_POSITION = ConversationsOverviewFragment.class.getName()+".scroll_state";
93
94 private final List<Conversation> conversations = new ArrayList<>();
95 private final PendingItem<Conversation> swipedConversation = new PendingItem<>();
96 private final PendingItem<ScrollState> pendingScrollState = new PendingItem<>();
97 private FragmentConversationsOverviewBinding binding;
98 private ConversationAdapter conversationsAdapter;
99 private XmppActivity activity;
100 private final PendingActionHelper pendingActionHelper = new PendingActionHelper();
101
102 private final ItemTouchHelper.SimpleCallback callback = new ItemTouchHelper.SimpleCallback(0,LEFT|RIGHT) {
103 @Override
104 public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
105 return false;
106 }
107
108 @Override
109 public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder,
110 float dX, float dY, int actionState, boolean isCurrentlyActive) {
111 if (viewHolder instanceof ConversationAdapter.ConversationViewHolder conversationViewHolder) {
112 getDefaultUIUtil().onDraw(c,recyclerView,conversationViewHolder.binding.frame,dX,dY,actionState,isCurrentlyActive);
113 }
114 }
115
116 @Override
117 public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
118 if (viewHolder instanceof ConversationAdapter.ConversationViewHolder conversationViewHolder) {
119 getDefaultUIUtil().clearView(conversationViewHolder.binding.frame);
120 }
121 }
122
123 @Override
124 public float getSwipeEscapeVelocity(final float defaultEscapeVelocity) {
125 return 32 * defaultEscapeVelocity;
126 }
127
128 @Override
129 public void onSwiped(final RecyclerView.ViewHolder viewHolder, final int direction) {
130 pendingActionHelper.execute();
131 int position = viewHolder.getLayoutPosition();
132 try {
133 swipedConversation.push(conversations.get(position));
134 } catch (IndexOutOfBoundsException e) {
135 return;
136 }
137 conversationsAdapter.remove(swipedConversation.peek(), position);
138 activity.xmppConnectionService.markRead(swipedConversation.peek());
139
140 if (position == 0 && conversationsAdapter.getItemCount() == 0) {
141 final Conversation c = swipedConversation.pop();
142 activity.xmppConnectionService.archiveConversation(c);
143 return;
144 }
145 final boolean formerlySelected = ConversationFragment.getConversation(getActivity()) == swipedConversation.peek();
146 if (activity instanceof OnConversationArchived) {
147 ((OnConversationArchived) activity).onConversationArchived(swipedConversation.peek());
148 }
149 final Conversation c = swipedConversation.peek();
150 final int title;
151 if (c.getMode() == Conversational.MODE_MULTI) {
152 if (c.getMucOptions().isPrivateAndNonAnonymous()) {
153 title = R.string.title_undo_swipe_out_group_chat;
154 } else {
155 title = R.string.title_undo_swipe_out_channel;
156 }
157 } else {
158 title = R.string.title_undo_swipe_out_chat;
159 }
160
161 final Snackbar snackbar = Snackbar.make(binding.list, title, 5000)
162 .setAction(R.string.undo, v -> {
163 pendingActionHelper.undo();
164 Conversation conversation = swipedConversation.pop();
165 conversationsAdapter.insert(conversation, position);
166 if (formerlySelected) {
167 if (activity instanceof OnConversationSelected) {
168 ((OnConversationSelected) activity).onConversationSelected(c);
169 }
170 }
171 LinearLayoutManager layoutManager = (LinearLayoutManager) binding.list.getLayoutManager();
172 if (position > layoutManager.findLastVisibleItemPosition()) {
173 binding.list.smoothScrollToPosition(position);
174 }
175 })
176 .addCallback(new Snackbar.Callback() {
177 @Override
178 public void onDismissed(Snackbar transientBottomBar, int event) {
179 switch (event) {
180 case DISMISS_EVENT_SWIPE:
181 case DISMISS_EVENT_TIMEOUT:
182 pendingActionHelper.execute();
183 break;
184 }
185 }
186 });
187
188 pendingActionHelper.push(() -> {
189 if (snackbar.isShownOrQueued()) {
190 snackbar.dismiss();
191 }
192 final Conversation conversation = swipedConversation.pop();
193 if(conversation != null){
194 if (!conversation.isRead(activity.xmppConnectionService) && conversation.getMode() == Conversation.MODE_SINGLE) {
195 return;
196 }
197 activity.xmppConnectionService.archiveConversation(c);
198 }
199 });
200 snackbar.show();
201 }
202 };
203
204 private ItemTouchHelper touchHelper = null;
205
206 public static Conversation getSuggestion(Activity activity) {
207 final Conversation exception;
208 Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
209 if (fragment instanceof ConversationsOverviewFragment) {
210 exception = ((ConversationsOverviewFragment) fragment).swipedConversation.peek();
211 } else {
212 exception = null;
213 }
214 return getSuggestion(activity, exception);
215 }
216
217 public static Conversation getSuggestion(Activity activity, Conversation exception) {
218 Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
219 if (fragment instanceof ConversationsOverviewFragment) {
220 List<Conversation> conversations = ((ConversationsOverviewFragment) fragment).conversations;
221 if (conversations.size() > 0) {
222 Conversation suggestion = conversations.get(0);
223 if (suggestion == exception) {
224 if (conversations.size() > 1) {
225 return conversations.get(1);
226 }
227 } else {
228 return suggestion;
229 }
230 }
231 }
232 return null;
233
234 }
235
236 @Override
237 public void onActivityCreated(Bundle savedInstanceState) {
238 super.onActivityCreated(savedInstanceState);
239 if (savedInstanceState == null) {
240 return;
241 }
242 pendingScrollState.push(savedInstanceState.getParcelable(STATE_SCROLL_POSITION));
243 }
244
245 @Override
246 public void onAttach(Activity activity) {
247 super.onAttach(activity);
248 if (activity instanceof XmppActivity) {
249 this.activity = (XmppActivity) activity;
250 } else {
251 throw new IllegalStateException("Trying to attach fragment to activity that is not an XmppActivity");
252 }
253 }
254 @Override
255 public void onDestroyView() {
256 Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onDestroyView()");
257 super.onDestroyView();
258 this.binding = null;
259 this.conversationsAdapter = null;
260 this.touchHelper = null;
261 }
262 @Override
263 public void onDestroy() {
264 Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onDestroy()");
265 super.onDestroy();
266
267 }
268 @Override
269 public void onPause() {
270 Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onPause()");
271 pendingActionHelper.execute();
272 super.onPause();
273 }
274
275 @Override
276 public void onDetach() {
277 super.onDetach();
278 this.activity = null;
279 }
280
281 @Override
282 public void onCreate(Bundle savedInstanceState) {
283 super.onCreate(savedInstanceState);
284 setHasOptionsMenu(true);
285 }
286
287 @Override
288 public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
289 this.binding = DataBindingUtil.inflate(inflater, R.layout.fragment_conversations_overview, container, false);
290 this.binding.fab.setOnClickListener((view) -> activity.launchStartConversation());
291
292 this.conversationsAdapter = new ConversationAdapter(this.activity, this.conversations);
293 this.conversationsAdapter.setConversationClickListener((view, conversation) -> {
294 if (activity instanceof OnConversationSelected) {
295 ((OnConversationSelected) activity).onConversationSelected(conversation);
296 } else {
297 Log.w(ConversationsOverviewFragment.class.getCanonicalName(), "Activity does not implement OnConversationSelected");
298 }
299 });
300 this.binding.list.setAdapter(this.conversationsAdapter);
301 this.binding.list.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
302 registerForContextMenu(this.binding.list);
303 this.binding.list.addOnScrollListener(ExtendedFabSizeChanger.of(binding.fab));
304 this.touchHelper = new ItemTouchHelper(this.callback);
305 this.touchHelper.attachToRecyclerView(this.binding.list);
306 return binding.getRoot();
307 }
308
309 @Override
310 public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
311 menuInflater.inflate(R.menu.fragment_conversations_overview, menu);
312 AccountUtils.showHideMenuItems(menu);
313 final MenuItem easyOnboardInvite = menu.findItem(R.id.action_easy_invite);
314 easyOnboardInvite.setVisible(EasyOnboardingInvite.anyHasSupport(activity == null ? null : activity.xmppConnectionService));
315 if (activity != null && activity.xmppConnectionService != null && activity.xmppConnectionService.isOnboarding()) {
316 final MenuItem manageAccounts = menu.findItem(R.id.action_accounts);
317 if (manageAccounts != null) manageAccounts.setVisible(false);
318
319 final MenuItem settings = menu.findItem(R.id.action_settings);
320 if (settings != null) settings.setVisible(false);
321 }
322 }
323
324 @Override
325 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
326 activity.getMenuInflater().inflate(R.menu.conversations, menu);
327
328 final MenuItem menuMucDetails = menu.findItem(R.id.action_muc_details);
329 final MenuItem menuContactDetails = menu.findItem(R.id.action_contact_details);
330 final MenuItem menuMute = menu.findItem(R.id.action_mute);
331 final MenuItem menuUnmute = menu.findItem(R.id.action_unmute);
332 final MenuItem menuOngoingCall = menu.findItem(R.id.action_ongoing_call);
333 final MenuItem menuTogglePinned = menu.findItem(R.id.action_toggle_pinned);
334 final MenuItem menuArchiveChat = menu.findItem(R.id.action_archive);
335
336 if (menuInfo == null) return;
337 int pos = ((AdapterContextMenuInfo) menuInfo).position;
338 if (pos < 0) return;
339 Conversation conversation = conversations.get(pos);
340 if (conversation != null) {
341 if (conversation.getMode() == Conversation.MODE_MULTI) {
342 menuContactDetails.setVisible(false);
343 menuMucDetails.setTitle(conversation.getMucOptions().isPrivateAndNonAnonymous() ? R.string.action_muc_details : R.string.channel_details);
344 menuOngoingCall.setVisible(false);
345 menuArchiveChat.setTitle("Leave " + (conversation.getMucOptions().isPrivateAndNonAnonymous() ? "group chat" : "Channel"));
346 } else {
347 final XmppConnectionService service = activity == null ? null : activity.xmppConnectionService;
348 final Optional<OngoingRtpSession> ongoingRtpSession = service == null ? Optional.absent() : service.getJingleConnectionManager().getOngoingRtpConnection(conversation.getContact());
349 if (ongoingRtpSession.isPresent()) {
350 menuOngoingCall.setVisible(true);
351 } else {
352 menuOngoingCall.setVisible(false);
353 }
354 menuContactDetails.setVisible(!conversation.withSelf());
355 menuMucDetails.setVisible(false);
356 menuArchiveChat.setTitle(R.string.action_archive_chat);
357 }
358 if (conversation.isMuted()) {
359 menuMute.setVisible(false);
360 } else {
361 menuUnmute.setVisible(false);
362 }
363 if (conversation.getBooleanAttribute(Conversation.ATTRIBUTE_PINNED_ON_TOP, false)) {
364 menuTogglePinned.setTitle(R.string.remove_from_favorites);
365 } else {
366 menuTogglePinned.setTitle(R.string.add_to_favorites);
367 }
368 }
369 super.onCreateContextMenu(menu, view, menuInfo);
370 }
371
372 @Override
373 public boolean onContextItemSelected(MenuItem item) {
374 final var info = ((AdapterContextMenuInfo) item.getMenuInfo());
375 if (info == null) return false;
376
377 int pos = info.position;
378 if (conversations == null || conversations.size() <= pos || pos < 0) return false;
379
380 Conversation conversation = conversations.get(pos);
381 ConversationFragment fragment = new ConversationFragment();
382 fragment.setHasOptionsMenu(false);
383 fragment.onAttach(activity);
384 fragment.reInit(conversation, null);
385 boolean r = fragment.onOptionsItemSelected(item);
386 refresh();
387 return r;
388 }
389
390 @Override
391 public void onBackendConnected() {
392 refresh();
393 }
394
395 private void setupSwipe() {
396 if (this.touchHelper == null && (activity.xmppConnectionService == null || !activity.xmppConnectionService.isOnboarding())) {
397 this.touchHelper = new ItemTouchHelper(this.callback);
398 this.touchHelper.attachToRecyclerView(this.binding.list);
399 }
400 }
401
402 @Override
403 public void onSaveInstanceState(Bundle bundle) {
404 super.onSaveInstanceState(bundle);
405 ScrollState scrollState = getScrollState();
406 if (scrollState != null) {
407 bundle.putParcelable(STATE_SCROLL_POSITION, scrollState);
408 }
409 }
410
411 private ScrollState getScrollState() {
412 if (this.binding == null) {
413 return null;
414 }
415 LinearLayoutManager layoutManager = (LinearLayoutManager) this.binding.list.getLayoutManager();
416 int position = layoutManager.findFirstVisibleItemPosition();
417 final View view = this.binding.list.getChildAt(0);
418 if (view != null) {
419 return new ScrollState(position,view.getTop());
420 } else {
421 return new ScrollState(position, 0);
422 }
423 }
424
425 @Override
426 public void onStart() {
427 super.onStart();
428 Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onStart()");
429 if (activity.xmppConnectionService != null) {
430 refresh();
431 }
432 }
433
434 @Override
435 public void onResume() {
436 super.onResume();
437 Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onResume()");
438 }
439
440 @Override
441 public boolean onOptionsItemSelected(final MenuItem item) {
442 if (MenuDoubleTabUtil.shouldIgnoreTap()) {
443 return false;
444 }
445 switch (item.getItemId()) {
446 case R.id.action_search:
447 startActivity(new Intent(getActivity(), SearchActivity.class));
448 return true;
449 case R.id.action_easy_invite:
450 selectAccountToStartEasyInvite();
451 return true;
452 }
453 return super.onOptionsItemSelected(item);
454 }
455
456 private void selectAccountToStartEasyInvite() {
457 final List<Account> accounts = EasyOnboardingInvite.getSupportingAccounts(activity.xmppConnectionService);
458 if (accounts.isEmpty()) {
459 //This can technically happen if opening the menu item races with accounts reconnecting or something
460 Toast.makeText(getActivity(),R.string.no_active_accounts_support_this, Toast.LENGTH_LONG).show();
461 } else if (accounts.size() == 1) {
462 openEasyInviteScreen(accounts.get(0));
463 } else {
464 final AtomicReference<Account> selectedAccount = new AtomicReference<>(accounts.get(0));
465 final MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder(activity);
466 alertDialogBuilder.setTitle(R.string.choose_account);
467 final String[] asStrings = Collections2.transform(accounts, a -> a.getJid().asBareJid().toEscapedString()).toArray(new String[0]);
468 alertDialogBuilder.setSingleChoiceItems(asStrings, 0, (dialog, which) -> selectedAccount.set(accounts.get(which)));
469 alertDialogBuilder.setNegativeButton(R.string.cancel, null);
470 alertDialogBuilder.setPositiveButton(R.string.ok, (dialog, which) -> openEasyInviteScreen(selectedAccount.get()));
471 alertDialogBuilder.create().show();
472 }
473 }
474
475 private void openEasyInviteScreen(final Account account) {
476 EasyOnboardingInviteActivity.launch(account, activity);
477 }
478
479 @Override
480 void refresh() {
481 if (this.binding == null || this.activity == null) {
482 Log.d(Config.LOGTAG,"ConversationsOverviewFragment.refresh() skipped updated because view binding or activity was null");
483 return;
484 }
485 this.activity.populateWithOrderedConversations(this.conversations);
486 Conversation removed = this.swipedConversation.peek();
487 if (removed != null) {
488 if (removed.isRead(activity == null ? null : activity.xmppConnectionService)) {
489 this.conversations.remove(removed);
490 } else {
491 pendingActionHelper.execute();
492 }
493 }
494 this.conversationsAdapter.notifyDataSetChanged();
495 ScrollState scrollState = pendingScrollState.pop();
496 if (scrollState != null) {
497 setScrollPosition(scrollState);
498 }
499
500 if (activity.xmppConnectionService != null && activity.xmppConnectionService.isOnboarding()) {
501 binding.fab.setVisibility(View.GONE);
502
503 if (this.conversations.size() == 1) {
504 if (activity instanceof OnConversationSelected) {
505 ((OnConversationSelected) activity).onConversationSelected(this.conversations.get(0));
506 } else {
507 Log.w(ConversationsOverviewFragment.class.getCanonicalName(), "Activity does not implement OnConversationSelected");
508 }
509 }
510 } else {
511 binding.fab.setVisibility(View.VISIBLE);
512 }
513 setupSwipe();
514 }
515
516 private void setScrollPosition(ScrollState scrollPosition) {
517 if (scrollPosition != null) {
518 LinearLayoutManager layoutManager = (LinearLayoutManager) binding.list.getLayoutManager();
519 layoutManager.scrollToPositionWithOffset(scrollPosition.position, scrollPosition.offset);
520 }
521 }
522}