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