ConversationsOverviewFragment.java

  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.PopupMenu;
 49import android.widget.Toast;
 50
 51import androidx.annotation.NonNull;
 52import androidx.databinding.DataBindingUtil;
 53import androidx.recyclerview.widget.ItemTouchHelper;
 54import androidx.recyclerview.widget.LinearLayoutManager;
 55import androidx.recyclerview.widget.RecyclerView;
 56
 57import com.google.android.material.color.MaterialColors;
 58import com.google.android.material.dialog.MaterialAlertDialogBuilder;
 59import com.google.android.material.snackbar.Snackbar;
 60import com.google.common.base.Optional;
 61import com.google.common.collect.Collections2;
 62
 63import java.util.ArrayList;
 64import java.util.List;
 65import java.util.concurrent.atomic.AtomicReference;
 66
 67import eu.siacs.conversations.BuildConfig;
 68import eu.siacs.conversations.Config;
 69import eu.siacs.conversations.R;
 70import eu.siacs.conversations.databinding.FragmentConversationsOverviewBinding;
 71import eu.siacs.conversations.entities.Account;
 72import eu.siacs.conversations.entities.Conversation;
 73import eu.siacs.conversations.entities.Conversational;
 74import eu.siacs.conversations.services.XmppConnectionService;
 75import eu.siacs.conversations.services.QuickConversationsService;
 76import eu.siacs.conversations.ui.adapter.ConversationAdapter;
 77import eu.siacs.conversations.ui.interfaces.OnConversationArchived;
 78import eu.siacs.conversations.ui.interfaces.OnConversationSelected;
 79import eu.siacs.conversations.ui.util.MenuDoubleTabUtil;
 80import eu.siacs.conversations.ui.util.PendingActionHelper;
 81import eu.siacs.conversations.ui.util.PendingItem;
 82import eu.siacs.conversations.ui.util.ScrollState;
 83import eu.siacs.conversations.utils.AccountUtils;
 84import eu.siacs.conversations.utils.EasyOnboardingInvite;
 85import eu.siacs.conversations.utils.ThemeHelper;
 86import eu.siacs.conversations.xmpp.jingle.OngoingRtpSession;
 87
 88import static androidx.recyclerview.widget.ItemTouchHelper.LEFT;
 89import static androidx.recyclerview.widget.ItemTouchHelper.RIGHT;
 90
 91public class ConversationsOverviewFragment extends XmppFragment {
 92
 93	private static final String STATE_SCROLL_POSITION = ConversationsOverviewFragment.class.getName()+".scroll_state";
 94
 95	private final List<Conversation> conversations = new ArrayList<>();
 96	private final PendingItem<Conversation> swipedConversation = new PendingItem<>();
 97	private final PendingItem<ScrollState> pendingScrollState = new PendingItem<>();
 98	private FragmentConversationsOverviewBinding binding;
 99	private ConversationAdapter conversationsAdapter;
100	private XmppActivity activity;
101	private final PendingActionHelper pendingActionHelper = new PendingActionHelper();
102
103	private final ItemTouchHelper.SimpleCallback callback = new ItemTouchHelper.SimpleCallback(0,LEFT|RIGHT) {
104		@Override
105		public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
106			return false;
107		}
108
109		@Override
110		public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder,
111								float dX, float dY, int actionState, boolean isCurrentlyActive) {
112			if (viewHolder instanceof ConversationAdapter.ConversationViewHolder conversationViewHolder) {
113				getDefaultUIUtil().onDraw(c,recyclerView,conversationViewHolder.binding.frame,dX,dY,actionState,isCurrentlyActive);
114			}
115		}
116
117		@Override
118		public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
119			if (viewHolder instanceof ConversationAdapter.ConversationViewHolder conversationViewHolder) {
120				getDefaultUIUtil().clearView(conversationViewHolder.binding.frame);
121			}
122		}
123
124		@Override
125		public float getSwipeEscapeVelocity(final float defaultEscapeVelocity) {
126			return 32 * defaultEscapeVelocity;
127		}
128
129		@Override
130		public void onSwiped(final RecyclerView.ViewHolder viewHolder, final int direction) {
131			pendingActionHelper.execute();
132			int position = viewHolder.getLayoutPosition();
133			try {
134				swipedConversation.push(conversations.get(position));
135			} catch (IndexOutOfBoundsException e) {
136				return;
137			}
138			conversationsAdapter.remove(swipedConversation.peek(), position);
139			activity.xmppConnectionService.markRead(swipedConversation.peek());
140
141			if (position == 0 && conversationsAdapter.getItemCount() == 0) {
142				final Conversation c = swipedConversation.pop();
143				activity.xmppConnectionService.archiveConversation(c);
144				return;
145			}
146			final boolean formerlySelected = ConversationFragment.getConversation(getActivity()) == swipedConversation.peek();
147			if (activity instanceof OnConversationArchived) {
148				((OnConversationArchived) activity).onConversationArchived(swipedConversation.peek());
149			}
150			final Conversation c = swipedConversation.peek();
151			final int title;
152			if (c.getMode() == Conversational.MODE_MULTI) {
153				if (c.getMucOptions().isPrivateAndNonAnonymous()) {
154					title = R.string.title_undo_swipe_out_group_chat;
155				} else {
156					title = R.string.title_undo_swipe_out_channel;
157				}
158			} else {
159				title = R.string.title_undo_swipe_out_chat;
160			}
161
162			final Snackbar snackbar = Snackbar.make(binding.list, title, 5000)
163					.setAction(R.string.undo, v -> {
164						pendingActionHelper.undo();
165						Conversation conversation = swipedConversation.pop();
166						conversationsAdapter.insert(conversation, position);
167						if (formerlySelected) {
168							if (activity instanceof OnConversationSelected) {
169								((OnConversationSelected) activity).onConversationSelected(c);
170							}
171						}
172						LinearLayoutManager layoutManager = (LinearLayoutManager) binding.list.getLayoutManager();
173						if (position > layoutManager.findLastVisibleItemPosition()) {
174							binding.list.smoothScrollToPosition(position);
175						}
176					})
177					.addCallback(new Snackbar.Callback() {
178						@Override
179						public void onDismissed(Snackbar transientBottomBar, int event) {
180							switch (event) {
181								case DISMISS_EVENT_SWIPE:
182								case DISMISS_EVENT_TIMEOUT:
183									pendingActionHelper.execute();
184									break;
185							}
186						}
187					});
188
189			pendingActionHelper.push(() -> {
190				if (snackbar.isShownOrQueued()) {
191					snackbar.dismiss();
192				}
193				final Conversation conversation = swipedConversation.pop();
194				if(conversation != null){
195					if (!conversation.isRead(activity.xmppConnectionService) && conversation.getMode() == Conversation.MODE_SINGLE) {
196						return;
197					}
198					activity.xmppConnectionService.archiveConversation(c);
199				}
200			});
201			snackbar.show();
202		}
203	};
204
205	private ItemTouchHelper touchHelper = null;
206
207	public static Conversation getSuggestion(Activity activity) {
208		final Conversation exception;
209		Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
210		if (fragment instanceof ConversationsOverviewFragment) {
211			exception = ((ConversationsOverviewFragment) fragment).swipedConversation.peek();
212		} else {
213			exception = null;
214		}
215		return getSuggestion(activity, exception);
216	}
217
218	public static Conversation getSuggestion(Activity activity, Conversation exception) {
219		Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
220		if (fragment instanceof ConversationsOverviewFragment) {
221			List<Conversation> conversations = ((ConversationsOverviewFragment) fragment).conversations;
222			if (conversations.size() > 0) {
223				Conversation suggestion = conversations.get(0);
224				if (suggestion == exception) {
225					if (conversations.size() > 1) {
226						return conversations.get(1);
227					}
228				} else {
229					return suggestion;
230				}
231			}
232		}
233		return null;
234
235	}
236
237	@Override
238	public void onActivityCreated(Bundle savedInstanceState) {
239		super.onActivityCreated(savedInstanceState);
240		if (savedInstanceState == null) {
241			return;
242		}
243		pendingScrollState.push(savedInstanceState.getParcelable(STATE_SCROLL_POSITION));
244	}
245
246	@Override
247	public void onAttach(Activity activity) {
248		super.onAttach(activity);
249		if (activity instanceof XmppActivity) {
250			this.activity = (XmppActivity) activity;
251		} else {
252			throw new IllegalStateException("Trying to attach fragment to activity that is not an XmppActivity");
253		}
254	}
255	@Override
256	public void onDestroyView() {
257		Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onDestroyView()");
258		super.onDestroyView();
259		this.binding = null;
260		this.conversationsAdapter = null;
261		this.touchHelper = null;
262	}
263	@Override
264	public void onDestroy() {
265		Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onDestroy()");
266		super.onDestroy();
267
268	}
269	@Override
270	public void onPause() {
271		Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onPause()");
272		pendingActionHelper.execute();
273		super.onPause();
274	}
275
276	@Override
277	public void onDetach() {
278		super.onDetach();
279		this.activity = null;
280	}
281
282	@Override
283	public void onCreate(Bundle savedInstanceState) {
284		super.onCreate(savedInstanceState);
285		setHasOptionsMenu(true);
286	}
287
288	@Override
289	public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
290		this.binding = DataBindingUtil.inflate(inflater, R.layout.fragment_conversations_overview, container, false);
291		this.binding.fab.setOnClickListener((view) -> activity.launchStartConversation());
292
293		this.conversationsAdapter = new ConversationAdapter(this.activity, this.conversations);
294		this.conversationsAdapter.setConversationClickListener((view, conversation) -> {
295			if (activity instanceof OnConversationSelected) {
296				((OnConversationSelected) activity).onConversationSelected(conversation);
297			} else {
298				Log.w(ConversationsOverviewFragment.class.getCanonicalName(), "Activity does not implement OnConversationSelected");
299			}
300		});
301		this.binding.list.setAdapter(this.conversationsAdapter);
302		this.binding.list.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
303		registerForContextMenu(this.binding.list);
304		this.binding.list.addOnScrollListener(ExtendedFabSizeChanger.of(binding.fab));
305		this.touchHelper = new ItemTouchHelper(this.callback);
306		this.touchHelper.attachToRecyclerView(this.binding.list);
307		return binding.getRoot();
308	}
309
310	@Override
311	public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
312		menuInflater.inflate(R.menu.fragment_conversations_overview, menu);
313		AccountUtils.showHideMenuItems(menu);
314		final MenuItem easyOnboardInvite = menu.findItem(R.id.action_easy_invite);
315		easyOnboardInvite.setVisible(EasyOnboardingInvite.anyHasSupport(activity == null ? null : activity.xmppConnectionService));
316		if (activity != null && activity.xmppConnectionService != null && activity.xmppConnectionService.isOnboarding()) {
317			final MenuItem manageAccounts = menu.findItem(R.id.action_accounts);
318			if (manageAccounts != null) manageAccounts.setVisible(false);
319
320			final MenuItem settings = menu.findItem(R.id.action_settings);
321			if (settings != null) settings.setVisible(false);
322		}
323	}
324
325	@Override
326	public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
327		activity.getMenuInflater().inflate(R.menu.conversations, menu);
328
329		final MenuItem menuMucDetails = menu.findItem(R.id.action_muc_details);
330		final MenuItem menuContactDetails = menu.findItem(R.id.action_contact_details);
331		final MenuItem menuMute = menu.findItem(R.id.action_mute);
332		final MenuItem menuUnmute = menu.findItem(R.id.action_unmute);
333		final MenuItem menuOngoingCall = menu.findItem(R.id.action_ongoing_call);
334		final MenuItem menuTogglePinned = menu.findItem(R.id.action_toggle_pinned);
335		final MenuItem menuArchiveChat = menu.findItem(R.id.action_archive);
336
337		if (menuInfo == null) return;
338		int pos = ((AdapterContextMenuInfo) menuInfo).position;
339		if (pos < 0) return;
340		Conversation conversation = conversations.get(pos);
341		if (conversation != null) {
342			if (conversation.getMode() == Conversation.MODE_MULTI) {
343				menuContactDetails.setVisible(false);
344				menuMucDetails.setTitle(conversation.getMucOptions().isPrivateAndNonAnonymous() ? R.string.action_muc_details : R.string.channel_details);
345				menuOngoingCall.setVisible(false);
346				menuArchiveChat.setTitle("Leave " + (conversation.getMucOptions().isPrivateAndNonAnonymous() ? "group chat" : "Channel"));
347			} else {
348				final XmppConnectionService service = activity == null ? null : activity.xmppConnectionService;
349				final Optional<OngoingRtpSession> ongoingRtpSession = service == null ? Optional.absent() : service.getJingleConnectionManager().getOngoingRtpConnection(conversation.getContact());
350				if (ongoingRtpSession.isPresent()) {
351					menuOngoingCall.setVisible(true);
352				} else {
353					menuOngoingCall.setVisible(false);
354				}
355				menuContactDetails.setVisible(!conversation.withSelf());
356				menuMucDetails.setVisible(false);
357				menuArchiveChat.setTitle(R.string.action_archive_chat);
358			}
359			if (conversation.isMuted()) {
360				menuMute.setVisible(false);
361			} else {
362				menuUnmute.setVisible(false);
363			}
364			if (conversation.getBooleanAttribute(Conversation.ATTRIBUTE_PINNED_ON_TOP, false)) {
365				menuTogglePinned.setTitle(R.string.remove_from_favorites);
366			} else {
367				menuTogglePinned.setTitle(R.string.add_to_favorites);
368			}
369		}
370		super.onCreateContextMenu(menu, view, menuInfo);
371	}
372
373	@Override
374	public boolean onContextItemSelected(MenuItem item) {
375		final var info = ((AdapterContextMenuInfo) item.getMenuInfo());
376		if (info == null) return false;
377
378		int pos = info.position;
379		if (conversations == null || conversations.size() <= pos || pos < 0) return false;
380
381		Conversation conversation = conversations.get(pos);
382		ConversationFragment fragment = new ConversationFragment();
383		fragment.setHasOptionsMenu(false);
384		fragment.onAttach(activity);
385		fragment.reInit(conversation, null);
386		boolean r = fragment.onOptionsItemSelected(item);
387		refresh();
388		return r;
389	}
390
391	@Override
392	public void onBackendConnected() {
393		refresh();
394	}
395
396	private void setupSwipe() {
397		if (this.touchHelper == null && (activity.xmppConnectionService == null || !activity.xmppConnectionService.isOnboarding())) {
398			this.touchHelper = new ItemTouchHelper(this.callback);
399			this.touchHelper.attachToRecyclerView(this.binding.list);
400		}
401	}
402
403	@Override
404	public void onSaveInstanceState(Bundle bundle) {
405		super.onSaveInstanceState(bundle);
406		ScrollState scrollState = getScrollState();
407		if (scrollState != null) {
408			bundle.putParcelable(STATE_SCROLL_POSITION, scrollState);
409		}
410	}
411
412	private ScrollState getScrollState() {
413		if (this.binding == null) {
414			return null;
415		}
416		LinearLayoutManager layoutManager = (LinearLayoutManager) this.binding.list.getLayoutManager();
417		int position = layoutManager.findFirstVisibleItemPosition();
418		final View view = this.binding.list.getChildAt(0);
419		if (view != null) {
420			return new ScrollState(position,view.getTop());
421		} else {
422			return new ScrollState(position, 0);
423		}
424	}
425
426	@Override
427	public void onStart() {
428		super.onStart();
429		Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onStart()");
430		if (activity.xmppConnectionService != null) {
431			refresh();
432		}
433	}
434
435	@Override
436	public void onResume() {
437		super.onResume();
438		Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onResume()");
439	}
440
441	@Override
442	public boolean onOptionsItemSelected(final MenuItem item) {
443		if (MenuDoubleTabUtil.shouldIgnoreTap()) {
444			return false;
445		}
446		switch (item.getItemId()) {
447			case R.id.action_search:
448				startActivity(new Intent(getActivity(), SearchActivity.class));
449				return true;
450			case R.id.action_easy_invite:
451				selectAccountToStartEasyInvite();
452				return true;
453		}
454		return super.onOptionsItemSelected(item);
455	}
456
457	private void selectAccountToStartEasyInvite() {
458		final List<Account> accounts = EasyOnboardingInvite.getSupportingAccounts(activity.xmppConnectionService);
459		if (accounts.isEmpty()) {
460			//This can technically happen if opening the menu item races with accounts reconnecting or something
461			Toast.makeText(getActivity(),R.string.no_active_accounts_support_this, Toast.LENGTH_LONG).show();
462		} else if (accounts.size() == 1) {
463			openEasyInviteScreen(accounts.get(0));
464		} else {
465			final AtomicReference<Account> selectedAccount = new AtomicReference<>(accounts.get(0));
466			final MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder(activity);
467			alertDialogBuilder.setTitle(R.string.choose_account);
468			final String[] asStrings = Collections2.transform(accounts, a -> a.getJid().asBareJid().toEscapedString()).toArray(new String[0]);
469			alertDialogBuilder.setSingleChoiceItems(asStrings, 0, (dialog, which) -> selectedAccount.set(accounts.get(which)));
470			alertDialogBuilder.setNegativeButton(R.string.cancel, null);
471			alertDialogBuilder.setPositiveButton(R.string.ok, (dialog, which) -> openEasyInviteScreen(selectedAccount.get()));
472			alertDialogBuilder.create().show();
473		}
474	}
475
476	private void openEasyInviteScreen(final Account account) {
477		EasyOnboardingInviteActivity.launch(account, activity);
478	}
479
480	@Override
481	protected void refresh() {
482		if (this.binding == null || this.activity == null) {
483			Log.d(Config.LOGTAG,"ConversationsOverviewFragment.refresh() skipped updated because view binding or activity was null");
484			return;
485		}
486		this.activity.populateWithOrderedConversations(this.conversations);
487		Conversation removed = this.swipedConversation.peek();
488		if (removed != null) {
489			if (removed.isRead(activity == null ? null : activity.xmppConnectionService)) {
490				this.conversations.remove(removed);
491			} else {
492				pendingActionHelper.execute();
493			}
494		}
495		this.conversationsAdapter.notifyDataSetChanged();
496		ScrollState scrollState = pendingScrollState.pop();
497		if (scrollState != null) {
498			setScrollPosition(scrollState);
499		}
500
501		if (activity.xmppConnectionService != null && activity.xmppConnectionService.isOnboarding()) {
502			binding.fab.setVisibility(View.GONE);
503
504			if (this.conversations.size() == 1) {
505				if (activity instanceof OnConversationSelected) {
506					((OnConversationSelected) activity).onConversationSelected(this.conversations.get(0));
507				} else {
508					Log.w(ConversationsOverviewFragment.class.getCanonicalName(), "Activity does not implement OnConversationSelected");
509				}
510			}
511		} else {
512			binding.fab.setVisibility(View.VISIBLE);
513		}
514		setupSwipe();
515
516		if (binding.overviewSnackbar != null) binding.overviewSnackbar.setVisibility(View.GONE);
517		if (activity.xmppConnectionService == null) return;
518		for (final var account : activity.xmppConnectionService.getAccounts()) {
519			if (activity.getPreferences().getBoolean("no_mam_pref_warn:" + account.getUuid(), false)) continue;
520			if (account.mamPrefs() != null && !"always".equals(account.mamPrefs().getAttribute("default"))) {
521				binding.overviewSnackbar.setVisibility(View.VISIBLE);
522				binding.overviewSnackbarMessage.setText("Your account " + account.getJid().asBareJid().toEscapedString() + " does not have archiving fully enabled. This may result in missed messages if you use multiple devices or apps.");
523				binding.overviewSnackbarAction.setOnClickListener((v) -> {
524					final var prefs = account.mamPrefs();
525					prefs.setAttribute("default", "always");
526					activity.xmppConnectionService.pushMamPreferences(account, prefs);
527					refresh();
528				});
529
530				binding.overviewSnackbarAction.setOnLongClickListener((v) -> {
531					PopupMenu popupMenu = new PopupMenu(getActivity(), v);
532					popupMenu.inflate(R.menu.mam_pref_fix);
533					popupMenu.setOnMenuItemClickListener(menuItem -> {
534							switch (menuItem.getItemId()) {
535							case R.id.ignore:
536								final var editor = activity.getPreferences().edit();
537								editor.putBoolean("no_mam_pref_warn:" + account.getUuid(), true).apply();
538								editor.apply();
539								refresh();
540								return true;
541							}
542							return true;
543					});
544					popupMenu.show();
545					return true;
546				});
547				break;
548			}
549		}
550	}
551
552	private void setScrollPosition(ScrollState scrollPosition) {
553		if (scrollPosition != null) {
554			LinearLayoutManager layoutManager = (LinearLayoutManager) binding.list.getLayoutManager();
555			layoutManager.scrollToPositionWithOffset(scrollPosition.position, scrollPosition.offset);
556		}
557	}
558}