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.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
335		if (menuInfo == null) return;
336		int pos = ((AdapterContextMenuInfo) menuInfo).position;
337		if (pos < 0) return;
338		Conversation conversation = conversations.get(pos);
339		if (conversation != null) {
340			if (conversation.getMode() == Conversation.MODE_MULTI) {
341				menuContactDetails.setVisible(false);
342				menuMucDetails.setTitle(conversation.getMucOptions().isPrivateAndNonAnonymous() ? R.string.action_muc_details : R.string.channel_details);
343				menuOngoingCall.setVisible(false);
344			} else {
345				final XmppConnectionService service = activity == null ? null : activity.xmppConnectionService;
346				final Optional<OngoingRtpSession> ongoingRtpSession = service == null ? Optional.absent() : service.getJingleConnectionManager().getOngoingRtpConnection(conversation.getContact());
347				if (ongoingRtpSession.isPresent()) {
348					menuOngoingCall.setVisible(true);
349				} else {
350					menuOngoingCall.setVisible(false);
351				}
352				menuContactDetails.setVisible(!conversation.withSelf());
353				menuMucDetails.setVisible(false);
354			}
355			if (conversation.isMuted()) {
356				menuMute.setVisible(false);
357			} else {
358				menuUnmute.setVisible(false);
359			}
360			if (conversation.getBooleanAttribute(Conversation.ATTRIBUTE_PINNED_ON_TOP, false)) {
361				menuTogglePinned.setTitle(R.string.remove_from_favorites);
362			} else {
363				menuTogglePinned.setTitle(R.string.add_to_favorites);
364			}
365		}
366		super.onCreateContextMenu(menu, view, menuInfo);
367	}
368
369	@Override
370	public boolean onContextItemSelected(MenuItem item) {
371		final var info = ((AdapterContextMenuInfo) item.getMenuInfo());
372		if (info == null) return false;
373
374		int pos = info.position;
375		if (conversations == null || conversations.size() <= pos || pos < 0) return false;
376
377		Conversation conversation = conversations.get(pos);
378		ConversationFragment fragment = new ConversationFragment();
379		fragment.setHasOptionsMenu(false);
380		fragment.onAttach(activity);
381		fragment.reInit(conversation, null);
382		boolean r = fragment.onOptionsItemSelected(item);
383		refresh();
384		return r;
385	}
386
387	@Override
388	public void onBackendConnected() {
389		refresh();
390	}
391
392	private void setupSwipe() {
393		if (this.touchHelper == null && (activity.xmppConnectionService == null || !activity.xmppConnectionService.isOnboarding())) {
394			this.touchHelper = new ItemTouchHelper(this.callback);
395			this.touchHelper.attachToRecyclerView(this.binding.list);
396		}
397	}
398
399	@Override
400	public void onSaveInstanceState(Bundle bundle) {
401		super.onSaveInstanceState(bundle);
402		ScrollState scrollState = getScrollState();
403		if (scrollState != null) {
404			bundle.putParcelable(STATE_SCROLL_POSITION, scrollState);
405		}
406	}
407
408	private ScrollState getScrollState() {
409		if (this.binding == null) {
410			return null;
411		}
412		LinearLayoutManager layoutManager = (LinearLayoutManager) this.binding.list.getLayoutManager();
413		int position = layoutManager.findFirstVisibleItemPosition();
414		final View view = this.binding.list.getChildAt(0);
415		if (view != null) {
416			return new ScrollState(position,view.getTop());
417		} else {
418			return new ScrollState(position, 0);
419		}
420	}
421
422	@Override
423	public void onStart() {
424		super.onStart();
425		Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onStart()");
426		if (activity.xmppConnectionService != null) {
427			refresh();
428		}
429	}
430
431	@Override
432	public void onResume() {
433		super.onResume();
434		Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onResume()");
435	}
436
437	@Override
438	public boolean onOptionsItemSelected(final MenuItem item) {
439		if (MenuDoubleTabUtil.shouldIgnoreTap()) {
440			return false;
441		}
442		switch (item.getItemId()) {
443			case R.id.action_search:
444				startActivity(new Intent(getActivity(), SearchActivity.class));
445				return true;
446			case R.id.action_easy_invite:
447				selectAccountToStartEasyInvite();
448				return true;
449		}
450		return super.onOptionsItemSelected(item);
451	}
452
453	private void selectAccountToStartEasyInvite() {
454		final List<Account> accounts = EasyOnboardingInvite.getSupportingAccounts(activity.xmppConnectionService);
455		if (accounts.isEmpty()) {
456			//This can technically happen if opening the menu item races with accounts reconnecting or something
457			Toast.makeText(getActivity(),R.string.no_active_accounts_support_this, Toast.LENGTH_LONG).show();
458		} else if (accounts.size() == 1) {
459			openEasyInviteScreen(accounts.get(0));
460		} else {
461			final AtomicReference<Account> selectedAccount = new AtomicReference<>(accounts.get(0));
462			final MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder(activity);
463			alertDialogBuilder.setTitle(R.string.choose_account);
464			final String[] asStrings = Collections2.transform(accounts, a -> a.getJid().asBareJid().toEscapedString()).toArray(new String[0]);
465			alertDialogBuilder.setSingleChoiceItems(asStrings, 0, (dialog, which) -> selectedAccount.set(accounts.get(which)));
466			alertDialogBuilder.setNegativeButton(R.string.cancel, null);
467			alertDialogBuilder.setPositiveButton(R.string.ok, (dialog, which) -> openEasyInviteScreen(selectedAccount.get()));
468			alertDialogBuilder.create().show();
469		}
470	}
471
472	private void openEasyInviteScreen(final Account account) {
473		EasyOnboardingInviteActivity.launch(account, activity);
474	}
475
476	@Override
477	void refresh() {
478		if (this.binding == null || this.activity == null) {
479			Log.d(Config.LOGTAG,"ConversationsOverviewFragment.refresh() skipped updated because view binding or activity was null");
480			return;
481		}
482		this.activity.populateWithOrderedConversations(this.conversations);
483		Conversation removed = this.swipedConversation.peek();
484		if (removed != null) {
485			if (removed.isRead(activity == null ? null : activity.xmppConnectionService)) {
486				this.conversations.remove(removed);
487			} else {
488				pendingActionHelper.execute();
489			}
490		}
491		this.conversationsAdapter.notifyDataSetChanged();
492		ScrollState scrollState = pendingScrollState.pop();
493		if (scrollState != null) {
494			setScrollPosition(scrollState);
495		}
496
497		if (activity.xmppConnectionService != null && activity.xmppConnectionService.isOnboarding()) {
498			binding.fab.setVisibility(View.GONE);
499
500			if (this.conversations.size() == 1) {
501				if (activity instanceof OnConversationSelected) {
502					((OnConversationSelected) activity).onConversationSelected(this.conversations.get(0));
503				} else {
504					Log.w(ConversationsOverviewFragment.class.getCanonicalName(), "Activity does not implement OnConversationSelected");
505				}
506			}
507		} else {
508			binding.fab.setVisibility(View.VISIBLE);
509		}
510		setupSwipe();
511	}
512
513	private void setScrollPosition(ScrollState scrollPosition) {
514		if (scrollPosition != null) {
515			LinearLayoutManager layoutManager = (LinearLayoutManager) binding.list.getLayoutManager();
516			layoutManager.scrollToPositionWithOffset(scrollPosition.position, scrollPosition.offset);
517		}
518	}
519}