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