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
32
33import static eu.siacs.conversations.ui.ConversationFragment.REQUEST_DECRYPT_PGP;
34
35import android.annotation.SuppressLint;
36import android.app.Activity;
37import android.app.Fragment;
38import android.app.FragmentManager;
39import android.app.FragmentTransaction;
40import android.content.ActivityNotFoundException;
41import android.content.Context;
42import android.content.Intent;
43import android.content.pm.PackageManager;
44import android.net.Uri;
45import android.os.Bundle;
46import android.provider.Settings;
47import android.util.Log;
48import android.view.KeyEvent;
49import android.view.Menu;
50import android.view.MenuItem;
51import android.widget.Toast;
52
53import androidx.annotation.IdRes;
54import androidx.annotation.NonNull;
55import androidx.appcompat.app.ActionBar;
56import androidx.appcompat.app.AlertDialog;
57import androidx.databinding.DataBindingUtil;
58
59import org.openintents.openpgp.util.OpenPgpApi;
60
61import java.util.Arrays;
62import java.util.List;
63import java.util.concurrent.atomic.AtomicBoolean;
64
65import eu.siacs.conversations.Config;
66import eu.siacs.conversations.R;
67import eu.siacs.conversations.crypto.OmemoSetting;
68import eu.siacs.conversations.databinding.ActivityConversationsBinding;
69import eu.siacs.conversations.entities.Account;
70import eu.siacs.conversations.entities.Contact;
71import eu.siacs.conversations.entities.Conversation;
72import eu.siacs.conversations.entities.Conversational;
73import eu.siacs.conversations.services.XmppConnectionService;
74import eu.siacs.conversations.ui.interfaces.OnBackendConnected;
75import eu.siacs.conversations.ui.interfaces.OnConversationArchived;
76import eu.siacs.conversations.ui.interfaces.OnConversationRead;
77import eu.siacs.conversations.ui.interfaces.OnConversationSelected;
78import eu.siacs.conversations.ui.interfaces.OnConversationsListItemUpdated;
79import eu.siacs.conversations.ui.util.ActionBarUtil;
80import eu.siacs.conversations.ui.util.ActivityResult;
81import eu.siacs.conversations.ui.util.ConversationMenuConfigurator;
82import eu.siacs.conversations.ui.util.MenuDoubleTabUtil;
83import eu.siacs.conversations.ui.util.PendingItem;
84import eu.siacs.conversations.utils.ExceptionHelper;
85import eu.siacs.conversations.utils.SignupUtils;
86import eu.siacs.conversations.utils.XmppUri;
87import eu.siacs.conversations.xmpp.Jid;
88import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
89
90public class ConversationsActivity extends XmppActivity implements OnConversationSelected, OnConversationArchived, OnConversationsListItemUpdated, OnConversationRead, XmppConnectionService.OnAccountUpdate, XmppConnectionService.OnConversationUpdate, XmppConnectionService.OnRosterUpdate, OnUpdateBlocklist, XmppConnectionService.OnShowErrorToast, XmppConnectionService.OnAffiliationChanged {
91
92 public static final String ACTION_VIEW_CONVERSATION = "eu.siacs.conversations.action.VIEW";
93 public static final String EXTRA_CONVERSATION = "conversationUuid";
94 public static final String EXTRA_DOWNLOAD_UUID = "eu.siacs.conversations.download_uuid";
95 public static final String EXTRA_AS_QUOTE = "eu.siacs.conversations.as_quote";
96 public static final String EXTRA_NICK = "nick";
97 public static final String EXTRA_IS_PRIVATE_MESSAGE = "pm";
98 public static final String EXTRA_DO_NOT_APPEND = "do_not_append";
99 public static final String EXTRA_POST_INIT_ACTION = "post_init_action";
100 public static final String POST_ACTION_RECORD_VOICE = "record_voice";
101 public static final String EXTRA_TYPE = "type";
102 public static final String EXTRA_NODE = "node";
103 public static final String EXTRA_JID = "jid";
104
105 private static final List<String> VIEW_AND_SHARE_ACTIONS = Arrays.asList(
106 ACTION_VIEW_CONVERSATION,
107 Intent.ACTION_SEND,
108 Intent.ACTION_SEND_MULTIPLE
109 );
110
111 public static final int REQUEST_OPEN_MESSAGE = 0x9876;
112 public static final int REQUEST_PLAY_PAUSE = 0x5432;
113
114
115 //secondary fragment (when holding the conversation, must be initialized before refreshing the overview fragment
116 private static final @IdRes
117 int[] FRAGMENT_ID_NOTIFICATION_ORDER = {R.id.secondary_fragment, R.id.main_fragment};
118 private final PendingItem<Intent> pendingViewIntent = new PendingItem<>();
119 private final PendingItem<ActivityResult> postponedActivityResult = new PendingItem<>();
120 private ActivityConversationsBinding binding;
121 private boolean mActivityPaused = true;
122 private final AtomicBoolean mRedirectInProcess = new AtomicBoolean(false);
123 private boolean refreshForNewCaps = false;
124
125 private static boolean isViewOrShareIntent(Intent i) {
126 Log.d(Config.LOGTAG, "action: " + (i == null ? null : i.getAction()));
127 return i != null && VIEW_AND_SHARE_ACTIONS.contains(i.getAction()) && i.hasExtra(EXTRA_CONVERSATION);
128 }
129
130 private static Intent createLauncherIntent(Context context) {
131 final Intent intent = new Intent(context, ConversationsActivity.class);
132 intent.setAction(Intent.ACTION_MAIN);
133 intent.addCategory(Intent.CATEGORY_LAUNCHER);
134 return intent;
135 }
136
137 @Override
138 protected void refreshUiReal() {
139 invalidateOptionsMenu();
140 for (@IdRes int id : FRAGMENT_ID_NOTIFICATION_ORDER) {
141 refreshFragment(id);
142 }
143 refreshForNewCaps = false;
144 }
145
146 @Override
147 void onBackendConnected() {
148 if (performRedirectIfNecessary(true)) {
149 return;
150 }
151 xmppConnectionService.getNotificationService().setIsInForeground(true);
152 final Intent intent = pendingViewIntent.pop();
153 if (intent != null) {
154 if (processViewIntent(intent)) {
155 if (binding.secondaryFragment != null) {
156 notifyFragmentOfBackendConnected(R.id.main_fragment);
157 }
158 invalidateActionBarTitle();
159 return;
160 }
161 }
162 for (@IdRes int id : FRAGMENT_ID_NOTIFICATION_ORDER) {
163 notifyFragmentOfBackendConnected(id);
164 }
165
166 final ActivityResult activityResult = postponedActivityResult.pop();
167 if (activityResult != null) {
168 handleActivityResult(activityResult);
169 }
170
171 invalidateActionBarTitle();
172 if (binding.secondaryFragment != null && ConversationFragment.getConversation(this) == null) {
173 Conversation conversation = ConversationsOverviewFragment.getSuggestion(this);
174 if (conversation != null) {
175 openConversation(conversation, null);
176 }
177 }
178 showDialogsIfMainIsOverview();
179 }
180
181 private boolean performRedirectIfNecessary(boolean noAnimation) {
182 return performRedirectIfNecessary(null, noAnimation);
183 }
184
185 private boolean performRedirectIfNecessary(final Conversation ignore, final boolean noAnimation) {
186 if (xmppConnectionService == null) {
187 return false;
188 }
189 boolean isConversationsListEmpty = xmppConnectionService.isConversationsListEmpty(ignore);
190 if (isConversationsListEmpty && mRedirectInProcess.compareAndSet(false, true)) {
191 final Intent intent = SignupUtils.getRedirectionIntent(this);
192 if (noAnimation) {
193 intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
194 }
195 runOnUiThread(() -> {
196 startActivity(intent);
197 if (noAnimation) {
198 overridePendingTransition(0, 0);
199 }
200 });
201 }
202 return mRedirectInProcess.get();
203 }
204
205 private void showDialogsIfMainIsOverview() {
206 if (xmppConnectionService == null) {
207 return;
208 }
209 final Fragment fragment = getFragmentManager().findFragmentById(R.id.main_fragment);
210 if (fragment instanceof ConversationsOverviewFragment) {
211 if (ExceptionHelper.checkForCrash(this)) {
212 return;
213 }
214 openBatteryOptimizationDialogIfNeeded();
215 }
216 }
217
218 private String getBatteryOptimizationPreferenceKey() {
219 @SuppressLint("HardwareIds") String device = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
220 return "show_battery_optimization" + (device == null ? "" : device);
221 }
222
223 private void setNeverAskForBatteryOptimizationsAgain() {
224 getPreferences().edit().putBoolean(getBatteryOptimizationPreferenceKey(), false).apply();
225 }
226
227 private void openBatteryOptimizationDialogIfNeeded() {
228 if (isOptimizingBattery()
229 && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M
230 && getPreferences().getBoolean(getBatteryOptimizationPreferenceKey(), true)) {
231 AlertDialog.Builder builder = new AlertDialog.Builder(this);
232 builder.setTitle(R.string.battery_optimizations_enabled);
233 builder.setMessage(getString(R.string.battery_optimizations_enabled_dialog, getString(R.string.app_name)));
234 builder.setPositiveButton(R.string.next, (dialog, which) -> {
235 Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
236 Uri uri = Uri.parse("package:" + getPackageName());
237 intent.setData(uri);
238 try {
239 startActivityForResult(intent, REQUEST_BATTERY_OP);
240 } catch (ActivityNotFoundException e) {
241 Toast.makeText(this, R.string.device_does_not_support_battery_op, Toast.LENGTH_SHORT).show();
242 }
243 });
244 builder.setOnDismissListener(dialog -> setNeverAskForBatteryOptimizationsAgain());
245 final AlertDialog dialog = builder.create();
246 dialog.setCanceledOnTouchOutside(false);
247 dialog.show();
248 }
249 }
250
251 private void notifyFragmentOfBackendConnected(@IdRes int id) {
252 final Fragment fragment = getFragmentManager().findFragmentById(id);
253 if (fragment instanceof OnBackendConnected) {
254 ((OnBackendConnected) fragment).onBackendConnected();
255 }
256 }
257
258 private void refreshFragment(@IdRes int id) {
259 final Fragment fragment = getFragmentManager().findFragmentById(id);
260 if (fragment instanceof XmppFragment) {
261 ((XmppFragment) fragment).refresh();
262 if (refreshForNewCaps) ((XmppFragment) fragment).refreshForNewCaps();
263 }
264 }
265
266 private boolean processViewIntent(Intent intent) {
267 final String uuid = intent.getStringExtra(EXTRA_CONVERSATION);
268 final Conversation conversation = uuid != null ? xmppConnectionService.findConversationByUuid(uuid) : null;
269 if (conversation == null) {
270 Log.d(Config.LOGTAG, "unable to view conversation with uuid:" + uuid);
271 return false;
272 }
273 openConversation(conversation, intent.getExtras());
274 return true;
275 }
276
277 @Override
278 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
279 super.onRequestPermissionsResult(requestCode, permissions, grantResults);
280 UriHandlerActivity.onRequestPermissionResult(this, requestCode, grantResults);
281 if (grantResults.length > 0) {
282 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
283 switch (requestCode) {
284 case REQUEST_OPEN_MESSAGE:
285 refreshUiReal();
286 ConversationFragment.openPendingMessage(this);
287 break;
288 case REQUEST_PLAY_PAUSE:
289 ConversationFragment.startStopPending(this);
290 break;
291 }
292 }
293 }
294 }
295
296 @Override
297 public void onActivityResult(int requestCode, int resultCode, final Intent data) {
298 super.onActivityResult(requestCode, resultCode, data);
299 ActivityResult activityResult = ActivityResult.of(requestCode, resultCode, data);
300 if (xmppConnectionService != null) {
301 handleActivityResult(activityResult);
302 } else {
303 this.postponedActivityResult.push(activityResult);
304 }
305 }
306
307 private void handleActivityResult(ActivityResult activityResult) {
308 if (activityResult.resultCode == Activity.RESULT_OK) {
309 handlePositiveActivityResult(activityResult.requestCode, activityResult.data);
310 } else {
311 handleNegativeActivityResult(activityResult.requestCode);
312 }
313 }
314
315 private void handleNegativeActivityResult(int requestCode) {
316 Conversation conversation = ConversationFragment.getConversationReliable(this);
317 switch (requestCode) {
318 case REQUEST_DECRYPT_PGP:
319 if (conversation == null) {
320 break;
321 }
322 conversation.getAccount().getPgpDecryptionService().giveUpCurrentDecryption();
323 break;
324 case REQUEST_BATTERY_OP:
325 setNeverAskForBatteryOptimizationsAgain();
326 break;
327 }
328 }
329
330 private void handlePositiveActivityResult(int requestCode, final Intent data) {
331 Conversation conversation = ConversationFragment.getConversationReliable(this);
332 if (conversation == null) {
333 Log.d(Config.LOGTAG, "conversation not found");
334 return;
335 }
336 switch (requestCode) {
337 case REQUEST_DECRYPT_PGP:
338 conversation.getAccount().getPgpDecryptionService().continueDecryption(data);
339 break;
340 case REQUEST_CHOOSE_PGP_ID:
341 long id = data.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, 0);
342 if (id != 0) {
343 conversation.getAccount().setPgpSignId(id);
344 announcePgp(conversation.getAccount(), null, null, onOpenPGPKeyPublished);
345 } else {
346 choosePgpSignId(conversation.getAccount());
347 }
348 break;
349 case REQUEST_ANNOUNCE_PGP:
350 announcePgp(conversation.getAccount(), conversation, data, onOpenPGPKeyPublished);
351 break;
352 }
353 }
354
355 @Override
356 protected void onCreate(final Bundle savedInstanceState) {
357 super.onCreate(savedInstanceState);
358 ConversationMenuConfigurator.reloadFeatures(this);
359 OmemoSetting.load(this);
360 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_conversations);
361 setSupportActionBar(binding.toolbar);
362 configureActionBar(getSupportActionBar());
363 this.getFragmentManager().addOnBackStackChangedListener(this::invalidateActionBarTitle);
364 this.getFragmentManager().addOnBackStackChangedListener(this::showDialogsIfMainIsOverview);
365 this.initializeFragments();
366 this.invalidateActionBarTitle();
367 final Intent intent;
368 if (savedInstanceState == null) {
369 intent = getIntent();
370 } else {
371 intent = savedInstanceState.getParcelable("intent");
372 }
373 if (isViewOrShareIntent(intent)) {
374 pendingViewIntent.push(intent);
375 setIntent(createLauncherIntent(this));
376 }
377 }
378
379 @Override
380 public boolean onCreateOptionsMenu(Menu menu) {
381 getMenuInflater().inflate(R.menu.activity_conversations, menu);
382 final MenuItem qrCodeScanMenuItem = menu.findItem(R.id.action_scan_qr_code);
383 if (qrCodeScanMenuItem != null) {
384 if (isCameraFeatureAvailable()) {
385 Fragment fragment = getFragmentManager().findFragmentById(R.id.main_fragment);
386 boolean visible = getResources().getBoolean(R.bool.show_qr_code_scan)
387 && fragment instanceof ConversationsOverviewFragment;
388 qrCodeScanMenuItem.setVisible(visible);
389 } else {
390 qrCodeScanMenuItem.setVisible(false);
391 }
392 }
393 return super.onCreateOptionsMenu(menu);
394 }
395
396 @Override
397 public void onConversationSelected(Conversation conversation) {
398 clearPendingViewIntent();
399 if (ConversationFragment.getConversation(this) == conversation) {
400 Log.d(Config.LOGTAG, "ignore onConversationSelected() because conversation is already open");
401 return;
402 }
403 openConversation(conversation, null);
404 }
405
406 public void clearPendingViewIntent() {
407 if (pendingViewIntent.clear()) {
408 Log.e(Config.LOGTAG, "cleared pending view intent");
409 }
410 }
411
412 private void displayToast(final String msg) {
413 runOnUiThread(() -> Toast.makeText(ConversationsActivity.this, msg, Toast.LENGTH_SHORT).show());
414 }
415
416 @Override
417 public void onAffiliationChangedSuccessful(Jid jid) {
418
419 }
420
421 @Override
422 public void onAffiliationChangeFailed(Jid jid, int resId) {
423 displayToast(getString(resId, jid.asBareJid().toString()));
424 }
425
426 private void openConversation(Conversation conversation, Bundle extras) {
427 final FragmentManager fragmentManager = getFragmentManager();
428 executePendingTransactions(fragmentManager);
429 ConversationFragment conversationFragment = (ConversationFragment) fragmentManager.findFragmentById(R.id.secondary_fragment);
430 final boolean mainNeedsRefresh;
431 if (conversationFragment == null) {
432 mainNeedsRefresh = false;
433 final Fragment mainFragment = fragmentManager.findFragmentById(R.id.main_fragment);
434 if (mainFragment instanceof ConversationFragment) {
435 conversationFragment = (ConversationFragment) mainFragment;
436 } else {
437 conversationFragment = new ConversationFragment();
438 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
439 fragmentTransaction.replace(R.id.main_fragment, conversationFragment);
440 fragmentTransaction.addToBackStack(null);
441 try {
442 fragmentTransaction.commit();
443 } catch (IllegalStateException e) {
444 Log.w(Config.LOGTAG, "sate loss while opening conversation", e);
445 //allowing state loss is probably fine since view intents et all are already stored and a click can probably be 'ignored'
446 return;
447 }
448 }
449 } else {
450 mainNeedsRefresh = true;
451 }
452 conversationFragment.reInit(conversation, extras == null ? new Bundle() : extras);
453 if (mainNeedsRefresh) {
454 refreshFragment(R.id.main_fragment);
455 } else {
456 invalidateActionBarTitle();
457 }
458 }
459
460 private static void executePendingTransactions(final FragmentManager fragmentManager) {
461 try {
462 fragmentManager.executePendingTransactions();
463 } catch (final Exception e) {
464 Log.e(Config.LOGTAG,"unable to execute pending fragment transactions");
465 }
466 }
467
468 public boolean onXmppUriClicked(Uri uri) {
469 XmppUri xmppUri = new XmppUri(uri);
470 if (xmppUri.isValidJid() && !xmppUri.hasFingerprints()) {
471 final Conversation conversation = xmppConnectionService.findUniqueConversationByJid(xmppUri);
472 if (conversation != null) {
473 if (xmppUri.isAction("command")) {
474 startCommand(conversation.getAccount(), conversation.getJid(), xmppUri.getParameter("node"));
475 } else {
476 Bundle extras = new Bundle();
477 extras.putString(Intent.EXTRA_TEXT, xmppUri.getBody());
478 if (xmppUri.isAction("message")) extras.putString(EXTRA_POST_INIT_ACTION, "message");
479 openConversation(conversation, extras);
480 }
481 return true;
482 }
483 }
484 return false;
485 }
486
487 @Override
488 public boolean onOptionsItemSelected(MenuItem item) {
489 if (MenuDoubleTabUtil.shouldIgnoreTap()) {
490 return false;
491 }
492 switch (item.getItemId()) {
493 case android.R.id.home:
494 FragmentManager fm = getFragmentManager();
495 Fragment f = fm.getFragments().get(fm.getFragments().size() - 1);
496 if (f != null && f instanceof ConversationFragment) {
497 if (((ConversationFragment) f).onBackPressed()) {
498 return true;
499 }
500 }
501 if (fm.getBackStackEntryCount() > 0) {
502 try {
503 fm.popBackStack();
504 } catch (IllegalStateException e) {
505 Log.w(Config.LOGTAG, "Unable to pop back stack after pressing home button");
506 }
507 return true;
508 }
509 break;
510 case R.id.action_scan_qr_code:
511 UriHandlerActivity.scan(this);
512 return true;
513 case R.id.action_search_all_conversations:
514 startActivity(new Intent(this, SearchActivity.class));
515 return true;
516 case R.id.action_search_this_conversation:
517 final Conversation conversation = ConversationFragment.getConversation(this);
518 if (conversation == null) {
519 return true;
520 }
521 final Intent intent = new Intent(this, SearchActivity.class);
522 intent.putExtra(SearchActivity.EXTRA_CONVERSATION_UUID, conversation.getUuid());
523 startActivity(intent);
524 return true;
525 }
526 return super.onOptionsItemSelected(item);
527 }
528
529 @Override
530 public boolean onKeyDown(final int keyCode, final KeyEvent keyEvent) {
531 if (keyCode == KeyEvent.KEYCODE_DPAD_UP && keyEvent.isCtrlPressed()) {
532 final ConversationFragment conversationFragment = ConversationFragment.get(this);
533 if (conversationFragment != null && conversationFragment.onArrowUpCtrlPressed()) {
534 return true;
535 }
536 }
537 return super.onKeyDown(keyCode, keyEvent);
538 }
539
540 @Override
541 public void onSaveInstanceState(final Bundle savedInstanceState) {
542 final Intent pendingIntent = pendingViewIntent.peek();
543 savedInstanceState.putParcelable("intent", pendingIntent != null ? pendingIntent : getIntent());
544 super.onSaveInstanceState(savedInstanceState);
545 }
546
547 @Override
548 protected void onStart() {
549 super.onStart();
550 final int theme = findTheme();
551 if (this.mTheme != theme) {
552 this.mSkipBackgroundBinding = true;
553 recreate();
554 } else {
555 this.mSkipBackgroundBinding = false;
556 }
557 mRedirectInProcess.set(false);
558 }
559
560 @Override
561 protected void onNewIntent(final Intent intent) {
562 super.onNewIntent(intent);
563 if (isViewOrShareIntent(intent)) {
564 if (xmppConnectionService != null) {
565 clearPendingViewIntent();
566 processViewIntent(intent);
567 } else {
568 pendingViewIntent.push(intent);
569 }
570 }
571 setIntent(createLauncherIntent(this));
572 }
573
574 @Override
575 public void onPause() {
576 this.mActivityPaused = true;
577 super.onPause();
578 }
579
580 @Override
581 public void onResume() {
582 super.onResume();
583 this.mActivityPaused = false;
584 }
585
586 private void initializeFragments() {
587 final FragmentManager fragmentManager = getFragmentManager();
588 FragmentTransaction transaction = fragmentManager.beginTransaction();
589 final Fragment mainFragment = fragmentManager.findFragmentById(R.id.main_fragment);
590 final Fragment secondaryFragment = fragmentManager.findFragmentById(R.id.secondary_fragment);
591 if (mainFragment != null) {
592 if (binding.secondaryFragment != null) {
593 if (mainFragment instanceof ConversationFragment) {
594 getFragmentManager().popBackStack();
595 transaction.remove(mainFragment);
596 transaction.commit();
597 fragmentManager.executePendingTransactions();
598 transaction = fragmentManager.beginTransaction();
599 transaction.replace(R.id.secondary_fragment, mainFragment);
600 transaction.replace(R.id.main_fragment, new ConversationsOverviewFragment());
601 transaction.commit();
602 return;
603 }
604 } else {
605 if (secondaryFragment instanceof ConversationFragment) {
606 transaction.remove(secondaryFragment);
607 transaction.commit();
608 getFragmentManager().executePendingTransactions();
609 transaction = fragmentManager.beginTransaction();
610 transaction.replace(R.id.main_fragment, secondaryFragment);
611 transaction.addToBackStack(null);
612 transaction.commit();
613 return;
614 }
615 }
616 } else {
617 transaction.replace(R.id.main_fragment, new ConversationsOverviewFragment());
618 }
619 if (binding.secondaryFragment != null && secondaryFragment == null) {
620 transaction.replace(R.id.secondary_fragment, new ConversationFragment());
621 }
622 transaction.commit();
623 }
624
625 private void invalidateActionBarTitle() {
626 final ActionBar actionBar = getSupportActionBar();
627 if (actionBar == null) {
628 return;
629 }
630 final FragmentManager fragmentManager = getFragmentManager();
631 final Fragment mainFragment = fragmentManager.findFragmentById(R.id.main_fragment);
632 if (mainFragment instanceof ConversationFragment) {
633 final Conversation conversation = ((ConversationFragment) mainFragment).getConversation();
634 if (conversation != null) {
635 actionBar.setTitle(conversation.getName());
636 actionBar.setDisplayHomeAsUpEnabled(true);
637 ActionBarUtil.setActionBarOnClickListener(
638 binding.toolbar,
639 (v) -> openConversationDetails(conversation)
640 );
641 return;
642 }
643 }
644 actionBar.setTitle(R.string.app_name);
645 actionBar.setDisplayHomeAsUpEnabled(false);
646 ActionBarUtil.resetActionBarOnClickListeners(binding.toolbar);
647 }
648
649 private void openConversationDetails(final Conversation conversation) {
650 if (conversation.getMode() == Conversational.MODE_MULTI) {
651 ConferenceDetailsActivity.open(this, conversation);
652 } else {
653 final Contact contact = conversation.getContact();
654 if (contact.isSelf()) {
655 switchToAccount(conversation.getAccount());
656 } else {
657 switchToContactDetails(contact);
658 }
659 }
660 }
661
662 @Override
663 public void onConversationArchived(Conversation conversation) {
664 if (performRedirectIfNecessary(conversation, false)) {
665 return;
666 }
667 final FragmentManager fragmentManager = getFragmentManager();
668 final Fragment mainFragment = fragmentManager.findFragmentById(R.id.main_fragment);
669 if (mainFragment instanceof ConversationFragment) {
670 try {
671 fragmentManager.popBackStack();
672 } catch (final IllegalStateException e) {
673 Log.w(Config.LOGTAG, "state loss while popping back state after archiving conversation", e);
674 //this usually means activity is no longer active; meaning on the next open we will run through this again
675 }
676 return;
677 }
678 final Fragment secondaryFragment = fragmentManager.findFragmentById(R.id.secondary_fragment);
679 if (secondaryFragment instanceof ConversationFragment) {
680 if (((ConversationFragment) secondaryFragment).getConversation() == conversation) {
681 Conversation suggestion = ConversationsOverviewFragment.getSuggestion(this, conversation);
682 if (suggestion != null) {
683 openConversation(suggestion, null);
684 }
685 }
686 }
687 }
688
689 @Override
690 public void onConversationsListItemUpdated() {
691 Fragment fragment = getFragmentManager().findFragmentById(R.id.main_fragment);
692 if (fragment instanceof ConversationsOverviewFragment) {
693 ((ConversationsOverviewFragment) fragment).refresh();
694 }
695 }
696
697 @Override
698 public void switchToConversation(Conversation conversation) {
699 Log.d(Config.LOGTAG, "override");
700 openConversation(conversation, null);
701 }
702
703 @Override
704 public void onConversationRead(Conversation conversation, String upToUuid) {
705 if (!mActivityPaused && pendingViewIntent.peek() == null) {
706 xmppConnectionService.sendReadMarker(conversation, upToUuid);
707 } else {
708 Log.d(Config.LOGTAG, "ignoring read callback. mActivityPaused=" + mActivityPaused);
709 }
710 }
711
712 @Override
713 public void onAccountUpdate() {
714 this.refreshUi();
715 }
716
717 @Override
718 public void onConversationUpdate(boolean newCaps) {
719 if (performRedirectIfNecessary(false)) {
720 return;
721 }
722 refreshForNewCaps = newCaps;
723 this.refreshUi();
724 }
725
726 @Override
727 public void onRosterUpdate() {
728 refreshForNewCaps = true;
729 this.refreshUi();
730 }
731
732 @Override
733 public void OnUpdateBlocklist(OnUpdateBlocklist.Status status) {
734 this.refreshUi();
735 }
736
737 @Override
738 public void onShowErrorToast(int resId) {
739 runOnUiThread(() -> Toast.makeText(this, resId, Toast.LENGTH_SHORT).show());
740 }
741}