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.snackbar.Snackbar;
56import com.google.common.base.Optional;
57import com.google.common.collect.Collections2;
58
59import java.util.ArrayList;
60import java.util.List;
61import java.util.concurrent.atomic.AtomicReference;
62
63import eu.siacs.conversations.BuildConfig;
64import eu.siacs.conversations.Config;
65import eu.siacs.conversations.R;
66import eu.siacs.conversations.databinding.FragmentConversationsOverviewBinding;
67import eu.siacs.conversations.entities.Account;
68import eu.siacs.conversations.entities.Conversation;
69import eu.siacs.conversations.entities.Conversational;
70import eu.siacs.conversations.services.XmppConnectionService;
71import eu.siacs.conversations.services.QuickConversationsService;
72import eu.siacs.conversations.ui.adapter.ConversationAdapter;
73import eu.siacs.conversations.ui.interfaces.OnConversationArchived;
74import eu.siacs.conversations.ui.interfaces.OnConversationSelected;
75import eu.siacs.conversations.ui.util.MenuDoubleTabUtil;
76import eu.siacs.conversations.ui.util.PendingActionHelper;
77import eu.siacs.conversations.ui.util.PendingItem;
78import eu.siacs.conversations.ui.util.ScrollState;
79import eu.siacs.conversations.ui.util.StyledAttributes;
80import eu.siacs.conversations.utils.AccountUtils;
81import eu.siacs.conversations.utils.EasyOnboardingInvite;
82import eu.siacs.conversations.utils.ThemeHelper;
83import eu.siacs.conversations.xmpp.jingle.OngoingRtpSession;
84
85import static androidx.recyclerview.widget.ItemTouchHelper.LEFT;
86import static androidx.recyclerview.widget.ItemTouchHelper.RIGHT;
87
88public class ConversationsOverviewFragment extends XmppFragment {
89
90 private static final String STATE_SCROLL_POSITION = ConversationsOverviewFragment.class.getName()+".scroll_state";
91
92 private final List<Conversation> conversations = new ArrayList<>();
93 private final PendingItem<Conversation> swipedConversation = new PendingItem<>();
94 private final PendingItem<ScrollState> pendingScrollState = new PendingItem<>();
95 private FragmentConversationsOverviewBinding binding;
96 private ConversationAdapter conversationsAdapter;
97 private XmppActivity activity;
98 private float mSwipeEscapeVelocity = 0f;
99 private final PendingActionHelper pendingActionHelper = new PendingActionHelper();
100
101 private final ItemTouchHelper.SimpleCallback callback = new ItemTouchHelper.SimpleCallback(0,LEFT|RIGHT) {
102 @Override
103 public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
104 //todo maybe we can manually changing the position of the conversation
105 return false;
106 }
107
108 @Override
109 public float getSwipeEscapeVelocity (float defaultValue) {
110 return mSwipeEscapeVelocity;
111 }
112
113 @Override
114 public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
115 float dX, float dY, int actionState, boolean isCurrentlyActive) {
116 super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
117 if(actionState != ItemTouchHelper.ACTION_STATE_IDLE){
118 Paint paint = new Paint();
119 paint.setColor(StyledAttributes.getColor(activity,R.attr.conversations_overview_background));
120 paint.setStyle(Paint.Style.FILL);
121 c.drawRect(viewHolder.itemView.getLeft(),viewHolder.itemView.getTop()
122 ,viewHolder.itemView.getRight(),viewHolder.itemView.getBottom(), paint);
123 }
124 }
125
126 @Override
127 public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
128 super.clearView(recyclerView, viewHolder);
129 viewHolder.itemView.setAlpha(1f);
130 }
131
132 @Override
133 public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
134 pendingActionHelper.execute();
135 int position = viewHolder.getLayoutPosition();
136 try {
137 swipedConversation.push(conversations.get(position));
138 } catch (IndexOutOfBoundsException e) {
139 return;
140 }
141 conversationsAdapter.remove(swipedConversation.peek(), position);
142 activity.xmppConnectionService.markRead(swipedConversation.peek());
143
144 if (position == 0 && conversationsAdapter.getItemCount() == 0) {
145 final Conversation c = swipedConversation.pop();
146 activity.xmppConnectionService.archiveConversation(c);
147 return;
148 }
149 final boolean formerlySelected = ConversationFragment.getConversation(getActivity()) == swipedConversation.peek();
150 if (activity instanceof OnConversationArchived) {
151 ((OnConversationArchived) activity).onConversationArchived(swipedConversation.peek());
152 }
153 final Conversation c = swipedConversation.peek();
154 final int title;
155 if (c.getMode() == Conversational.MODE_MULTI) {
156 if (c.getMucOptions().isPrivateAndNonAnonymous()) {
157 title = R.string.title_undo_swipe_out_group_chat;
158 } else {
159 title = R.string.title_undo_swipe_out_channel;
160 }
161 } else {
162 title = R.string.title_undo_swipe_out_conversation;
163 }
164
165 final Snackbar snackbar = Snackbar.make(binding.list, title, 5000)
166 .setAction(R.string.undo, v -> {
167 pendingActionHelper.undo();
168 Conversation conversation = swipedConversation.pop();
169 conversationsAdapter.insert(conversation, position);
170 if (formerlySelected) {
171 if (activity instanceof OnConversationSelected) {
172 ((OnConversationSelected) activity).onConversationSelected(c);
173 }
174 }
175 LinearLayoutManager layoutManager = (LinearLayoutManager) binding.list.getLayoutManager();
176 if (position > layoutManager.findLastVisibleItemPosition()) {
177 binding.list.smoothScrollToPosition(position);
178 }
179 })
180 .addCallback(new Snackbar.Callback() {
181 @Override
182 public void onDismissed(Snackbar transientBottomBar, int event) {
183 switch (event) {
184 case DISMISS_EVENT_SWIPE:
185 case DISMISS_EVENT_TIMEOUT:
186 pendingActionHelper.execute();
187 break;
188 }
189 }
190 });
191
192 pendingActionHelper.push(() -> {
193 if (snackbar.isShownOrQueued()) {
194 snackbar.dismiss();
195 }
196 final Conversation conversation = swipedConversation.pop();
197 if(conversation != null){
198 if (!conversation.isRead() && conversation.getMode() == Conversation.MODE_SINGLE) {
199 return;
200 }
201 activity.xmppConnectionService.archiveConversation(c);
202 }
203 });
204
205 ThemeHelper.fix(snackbar);
206 snackbar.show();
207 }
208 };
209
210 private ItemTouchHelper touchHelper = null;
211
212 public static Conversation getSuggestion(Activity activity) {
213 final Conversation exception;
214 Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
215 if (fragment instanceof ConversationsOverviewFragment) {
216 exception = ((ConversationsOverviewFragment) fragment).swipedConversation.peek();
217 } else {
218 exception = null;
219 }
220 return getSuggestion(activity, exception);
221 }
222
223 public static Conversation getSuggestion(Activity activity, Conversation exception) {
224 Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
225 if (fragment instanceof ConversationsOverviewFragment) {
226 List<Conversation> conversations = ((ConversationsOverviewFragment) fragment).conversations;
227 if (conversations.size() > 0) {
228 Conversation suggestion = conversations.get(0);
229 if (suggestion == exception) {
230 if (conversations.size() > 1) {
231 return conversations.get(1);
232 }
233 } else {
234 return suggestion;
235 }
236 }
237 }
238 return null;
239
240 }
241
242 @Override
243 public void onActivityCreated(Bundle savedInstanceState) {
244 super.onActivityCreated(savedInstanceState);
245 if (savedInstanceState == null) {
246 return;
247 }
248 pendingScrollState.push(savedInstanceState.getParcelable(STATE_SCROLL_POSITION));
249 }
250
251 @Override
252 public void onAttach(Activity activity) {
253 super.onAttach(activity);
254 if (activity instanceof XmppActivity) {
255 this.activity = (XmppActivity) activity;
256 } else {
257 throw new IllegalStateException("Trying to attach fragment to activity that is not an XmppActivity");
258 }
259 }
260 @Override
261 public void onDestroyView() {
262 Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onDestroyView()");
263 super.onDestroyView();
264 this.binding = null;
265 this.conversationsAdapter = null;
266 this.touchHelper = null;
267 }
268 @Override
269 public void onDestroy() {
270 Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onDestroy()");
271 super.onDestroy();
272
273 }
274 @Override
275 public void onPause() {
276 Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onPause()");
277 pendingActionHelper.execute();
278 super.onPause();
279 }
280
281 @Override
282 public void onDetach() {
283 super.onDetach();
284 this.activity = null;
285 }
286
287 @Override
288 public void onCreate(Bundle savedInstanceState) {
289 super.onCreate(savedInstanceState);
290 setHasOptionsMenu(true);
291 }
292
293 @Override
294 public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
295 this.mSwipeEscapeVelocity = getResources().getDimension(R.dimen.swipe_escape_velocity);
296 this.binding = DataBindingUtil.inflate(inflater, R.layout.fragment_conversations_overview, container, false);
297 this.binding.fab.setOnClickListener((view) -> StartConversationActivity.launch(getActivity()));
298
299 this.conversationsAdapter = new ConversationAdapter(this.activity, this.conversations);
300 this.conversationsAdapter.setConversationClickListener((view, conversation) -> {
301 if (activity instanceof OnConversationSelected) {
302 ((OnConversationSelected) activity).onConversationSelected(conversation);
303 } else {
304 Log.w(ConversationsOverviewFragment.class.getCanonicalName(), "Activity does not implement OnConversationSelected");
305 }
306 });
307 this.binding.list.setAdapter(this.conversationsAdapter);
308 this.binding.list.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
309 registerForContextMenu(this.binding.list);
310 return binding.getRoot();
311 }
312
313 @Override
314 public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
315 menuInflater.inflate(R.menu.fragment_conversations_overview, menu);
316 AccountUtils.showHideMenuItems(menu);
317 final MenuItem easyOnboardInvite = menu.findItem(R.id.action_easy_invite);
318 easyOnboardInvite.setVisible(EasyOnboardingInvite.anyHasSupport(activity == null ? null : activity.xmppConnectionService));
319 if (activity != null && activity.xmppConnectionService != null && activity.xmppConnectionService.isOnboarding()) {
320 final MenuItem manageAccounts = menu.findItem(R.id.action_accounts);
321 if (manageAccounts != null) manageAccounts.setVisible(false);
322
323 final MenuItem settings = menu.findItem(R.id.action_settings);
324 if (settings != null) settings.setVisible(false);
325 }
326 }
327
328 @Override
329 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
330 activity.getMenuInflater().inflate(R.menu.conversations, menu);
331
332 final MenuItem menuMucDetails = menu.findItem(R.id.action_muc_details);
333 final MenuItem menuContactDetails = menu.findItem(R.id.action_contact_details);
334 final MenuItem menuMute = menu.findItem(R.id.action_mute);
335 final MenuItem menuUnmute = menu.findItem(R.id.action_unmute);
336 final MenuItem menuOngoingCall = menu.findItem(R.id.action_ongoing_call);
337 final MenuItem menuTogglePinned = menu.findItem(R.id.action_toggle_pinned);
338
339 if (menuInfo == null) return;
340 int pos = ((AdapterContextMenuInfo) menuInfo).position;
341 if (pos < 0) return;
342 Conversation conversation = conversations.get(pos);
343 if (conversation != null) {
344 if (conversation.getMode() == Conversation.MODE_MULTI) {
345 menuContactDetails.setVisible(false);
346 menuMucDetails.setTitle(conversation.getMucOptions().isPrivateAndNonAnonymous() ? R.string.action_muc_details : R.string.channel_details);
347 menuOngoingCall.setVisible(false);
348 } else {
349 final XmppConnectionService service = activity == null ? null : activity.xmppConnectionService;
350 final Optional<OngoingRtpSession> ongoingRtpSession = service == null ? Optional.absent() : service.getJingleConnectionManager().getOngoingRtpConnection(conversation.getContact());
351 if (ongoingRtpSession.isPresent()) {
352 menuOngoingCall.setVisible(true);
353 } else {
354 menuOngoingCall.setVisible(false);
355 }
356 menuContactDetails.setVisible(!conversation.withSelf());
357 menuMucDetails.setVisible(false);
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.size() == 0) {
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 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(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 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.xmppConnectionService.populateWithOrderedConversations(this.conversations);
487 Conversation removed = this.swipedConversation.peek();
488 if (removed != null) {
489 if (removed.isRead()) {
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
517 private void setScrollPosition(ScrollState scrollPosition) {
518 if (scrollPosition != null) {
519 LinearLayoutManager layoutManager = (LinearLayoutManager) binding.list.getLayoutManager();
520 layoutManager.scrollToPositionWithOffset(scrollPosition.position, scrollPosition.offset);
521 }
522 }
523}