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.Menu;
53import android.view.MenuItem;
54import android.widget.Toast;
55
56import org.openintents.openpgp.util.OpenPgpApi;
57
58import java.util.Arrays;
59import java.util.List;
60import java.util.concurrent.atomic.AtomicBoolean;
61
62import eu.siacs.conversations.Config;
63import eu.siacs.conversations.R;
64import eu.siacs.conversations.crypto.OmemoSetting;
65import eu.siacs.conversations.databinding.ActivityConversationsBinding;
66import eu.siacs.conversations.entities.Account;
67import eu.siacs.conversations.entities.Conversation;
68import eu.siacs.conversations.services.XmppConnectionService;
69import eu.siacs.conversations.ui.interfaces.OnBackendConnected;
70import eu.siacs.conversations.ui.interfaces.OnConversationArchived;
71import eu.siacs.conversations.ui.interfaces.OnConversationRead;
72import eu.siacs.conversations.ui.interfaces.OnConversationSelected;
73import eu.siacs.conversations.ui.interfaces.OnConversationsListItemUpdated;
74import eu.siacs.conversations.ui.service.EmojiService;
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.EmojiWrapper;
80import eu.siacs.conversations.utils.ExceptionHelper;
81import eu.siacs.conversations.utils.XmppUri;
82import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
83import rocks.xmpp.addr.Jid;
84
85import static eu.siacs.conversations.ui.ConversationFragment.REQUEST_DECRYPT_PGP;
86
87public class ConversationsActivity extends XmppActivity implements OnConversationSelected, OnConversationArchived, OnConversationsListItemUpdated, OnConversationRead, XmppConnectionService.OnAccountUpdate, XmppConnectionService.OnConversationUpdate, XmppConnectionService.OnRosterUpdate, OnUpdateBlocklist, XmppConnectionService.OnShowErrorToast, XmppConnectionService.OnAffiliationChanged, XmppConnectionService.OnRoleChanged {
88
89 public static final String ACTION_VIEW_CONVERSATION = "eu.siacs.conversations.action.VIEW";
90 public static final String EXTRA_CONVERSATION = "conversationUuid";
91 public static final String EXTRA_DOWNLOAD_UUID = "eu.siacs.conversations.download_uuid";
92 public static final String EXTRA_TEXT = "text";
93 public static final String EXTRA_AS_QUOTE = "as_quote";
94 public static final String EXTRA_NICK = "nick";
95 public static final String EXTRA_IS_PRIVATE_MESSAGE = "pm";
96
97 private static List<String> VIEW_AND_SHARE_ACTIONS = Arrays.asList(
98 ACTION_VIEW_CONVERSATION,
99 Intent.ACTION_SEND,
100 Intent.ACTION_SEND_MULTIPLE
101 );
102
103 public static final int REQUEST_OPEN_MESSAGE = 0x9876;
104 public static final int REQUEST_PLAY_PAUSE = 0x5432;
105
106
107 //secondary fragment (when holding the conversation, must be initialized before refreshing the overview fragment
108 private static final @IdRes
109 int[] FRAGMENT_ID_NOTIFICATION_ORDER = {R.id.secondary_fragment, R.id.main_fragment};
110 private final PendingItem<Intent> pendingViewIntent = new PendingItem<>();
111 private final PendingItem<ActivityResult> postponedActivityResult = new PendingItem<>();
112 private ActivityConversationsBinding binding;
113 private boolean mActivityPaused = true;
114 private AtomicBoolean mRedirectInProcess = new AtomicBoolean(false);
115
116 private static boolean isViewOrShareIntent(Intent i) {
117 Log.d(Config.LOGTAG,"action: "+(i == null ? null : i.getAction()));
118 return i != null && VIEW_AND_SHARE_ACTIONS.contains(i.getAction()) && i.hasExtra(EXTRA_CONVERSATION);
119 }
120
121 private static Intent createLauncherIntent(Context context) {
122 final Intent intent = new Intent(context, ConversationsActivity.class);
123 intent.setAction(Intent.ACTION_MAIN);
124 intent.addCategory(Intent.CATEGORY_LAUNCHER);
125 return intent;
126 }
127
128 @Override
129 protected void refreshUiReal() {
130 for (@IdRes int id : FRAGMENT_ID_NOTIFICATION_ORDER) {
131 refreshFragment(id);
132 }
133 }
134
135 @Override
136 void onBackendConnected() {
137 if (performRedirectIfNecessary(true)) {
138 return;
139 }
140 xmppConnectionService.getNotificationService().setIsInForeground(true);
141 Intent intent = pendingViewIntent.pop();
142 if (intent != null) {
143 if (processViewIntent(intent)) {
144 if (binding.secondaryFragment != null) {
145 notifyFragmentOfBackendConnected(R.id.main_fragment);
146 }
147 invalidateActionBarTitle();
148 return;
149 }
150 }
151 for (@IdRes int id : FRAGMENT_ID_NOTIFICATION_ORDER) {
152 notifyFragmentOfBackendConnected(id);
153 }
154
155 ActivityResult activityResult = postponedActivityResult.pop();
156 if (activityResult != null) {
157 handleActivityResult(activityResult);
158 }
159
160 invalidateActionBarTitle();
161 if (binding.secondaryFragment != null && ConversationFragment.getConversation(this) == null) {
162 Conversation conversation = ConversationsOverviewFragment.getSuggestion(this);
163 if (conversation != null) {
164 openConversation(conversation, null);
165 }
166 }
167 showDialogsIfMainIsOverview();
168 }
169
170 private boolean performRedirectIfNecessary(boolean noAnimation) {
171 return performRedirectIfNecessary(null, noAnimation);
172 }
173
174 private boolean performRedirectIfNecessary(final Conversation ignore, final boolean noAnimation) {
175 if (xmppConnectionService == null) {
176 return false;
177 }
178 boolean isConversationsListEmpty = xmppConnectionService.isConversationsListEmpty(ignore);
179 if (isConversationsListEmpty && mRedirectInProcess.compareAndSet(false, true)) {
180 final Intent intent = getRedirectionIntent(noAnimation);
181 runOnUiThread(() -> {
182 startActivity(intent);
183 if (noAnimation) {
184 overridePendingTransition(0, 0);
185 }
186 });
187 }
188 return mRedirectInProcess.get();
189 }
190
191 private Intent getRedirectionIntent(boolean noAnimation) {
192 Account pendingAccount = xmppConnectionService.getPendingAccount();
193 Intent intent;
194 if (pendingAccount != null) {
195 intent = new Intent(this, EditAccountActivity.class);
196 intent.putExtra("jid", pendingAccount.getJid().asBareJid().toString());
197 } else {
198 if (xmppConnectionService.getAccounts().size() == 0) {
199 if (Config.X509_VERIFICATION) {
200 intent = new Intent(this, ManageAccountActivity.class);
201 } else if (Config.MAGIC_CREATE_DOMAIN != null) {
202 intent = new Intent(this, WelcomeActivity.class);
203 WelcomeActivity.addInviteUri(intent, getIntent());
204 } else {
205 intent = new Intent(this, EditAccountActivity.class);
206 }
207 } else {
208 intent = new Intent(this, StartConversationActivity.class);
209 }
210 }
211 intent.putExtra("init", true);
212 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
213 if (noAnimation) {
214 intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
215 }
216 return intent;
217 }
218
219 private void showDialogsIfMainIsOverview() {
220 if (xmppConnectionService == null) {
221 return;
222 }
223 final Fragment fragment = getFragmentManager().findFragmentById(R.id.main_fragment);
224 if (fragment != null && fragment instanceof ConversationsOverviewFragment) {
225 if (ExceptionHelper.checkForCrash(this)) {
226 return;
227 }
228 openBatteryOptimizationDialogIfNeeded();
229 }
230 }
231
232 private String getBatteryOptimizationPreferenceKey() {
233 @SuppressLint("HardwareIds") String device = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
234 return "show_battery_optimization" + (device == null ? "" : device);
235 }
236
237 private void setNeverAskForBatteryOptimizationsAgain() {
238 getPreferences().edit().putBoolean(getBatteryOptimizationPreferenceKey(), false).apply();
239 }
240
241 private void openBatteryOptimizationDialogIfNeeded() {
242 if (hasAccountWithoutPush()
243 && isOptimizingBattery()
244 && getPreferences().getBoolean(getBatteryOptimizationPreferenceKey(), true)) {
245 AlertDialog.Builder builder = new AlertDialog.Builder(this);
246 builder.setTitle(R.string.battery_optimizations_enabled);
247 builder.setMessage(R.string.battery_optimizations_enabled_dialog);
248 builder.setPositiveButton(R.string.next, (dialog, which) -> {
249 Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
250 Uri uri = Uri.parse("package:" + getPackageName());
251 intent.setData(uri);
252 try {
253 startActivityForResult(intent, REQUEST_BATTERY_OP);
254 } catch (ActivityNotFoundException e) {
255 Toast.makeText(this, R.string.device_does_not_support_battery_op, Toast.LENGTH_SHORT).show();
256 }
257 });
258 builder.setOnDismissListener(dialog -> setNeverAskForBatteryOptimizationsAgain());
259 final AlertDialog dialog = builder.create();
260 dialog.setCanceledOnTouchOutside(false);
261 dialog.show();
262 }
263 }
264
265 private boolean hasAccountWithoutPush() {
266 for (Account account : xmppConnectionService.getAccounts()) {
267 if (account.getStatus() == Account.State.ONLINE && !xmppConnectionService.getPushManagementService().available(account)) {
268 return true;
269 }
270 }
271 return false;
272 }
273
274 private void notifyFragmentOfBackendConnected(@IdRes int id) {
275 final Fragment fragment = getFragmentManager().findFragmentById(id);
276 if (fragment != null && fragment instanceof OnBackendConnected) {
277 ((OnBackendConnected) fragment).onBackendConnected();
278 }
279 }
280
281 private void refreshFragment(@IdRes int id) {
282 final Fragment fragment = getFragmentManager().findFragmentById(id);
283 if (fragment != null && fragment instanceof XmppFragment) {
284 ((XmppFragment) fragment).refresh();
285 }
286 }
287
288 private boolean processViewIntent(Intent intent) {
289 String uuid = intent.getStringExtra(EXTRA_CONVERSATION);
290 Conversation conversation = uuid != null ? xmppConnectionService.findConversationByUuid(uuid) : null;
291 if (conversation == null) {
292 Log.d(Config.LOGTAG, "unable to view conversation with uuid:" + uuid);
293 return false;
294 }
295 openConversation(conversation, intent.getExtras());
296 return true;
297 }
298
299 @Override
300 public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
301 UriHandlerActivity.onRequestPermissionResult(this, requestCode, grantResults);
302 if (grantResults.length > 0) {
303 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
304 switch (requestCode) {
305 case REQUEST_OPEN_MESSAGE:
306 refreshUiReal();
307 ConversationFragment.openPendingMessage(this);
308 break;
309 case REQUEST_PLAY_PAUSE:
310 ConversationFragment.startStopPending(this);
311 break;
312 }
313 }
314 }
315 }
316
317 @Override
318 public void onActivityResult(int requestCode, int resultCode, final Intent data) {
319 super.onActivityResult(requestCode, resultCode, data);
320 ActivityResult activityResult = ActivityResult.of(requestCode, resultCode, data);
321 if (xmppConnectionService != null) {
322 handleActivityResult(activityResult);
323 } else {
324 this.postponedActivityResult.push(activityResult);
325 }
326 }
327
328 private void handleActivityResult(ActivityResult activityResult) {
329 if (activityResult.resultCode == Activity.RESULT_OK) {
330 handlePositiveActivityResult(activityResult.requestCode, activityResult.data);
331 } else {
332 handleNegativeActivityResult(activityResult.requestCode);
333 }
334 }
335
336 private void handleNegativeActivityResult(int requestCode) {
337 Conversation conversation = ConversationFragment.getConversationReliable(this);
338 switch (requestCode) {
339 case REQUEST_DECRYPT_PGP:
340 if (conversation == null) {
341 break;
342 }
343 conversation.getAccount().getPgpDecryptionService().giveUpCurrentDecryption();
344 break;
345 case REQUEST_BATTERY_OP:
346 setNeverAskForBatteryOptimizationsAgain();
347 break;
348 }
349 }
350
351 private void handlePositiveActivityResult(int requestCode, final Intent data) {
352 Conversation conversation = ConversationFragment.getConversationReliable(this);
353 if (conversation == null) {
354 Log.d(Config.LOGTAG, "conversation not found");
355 return;
356 }
357 switch (requestCode) {
358 case REQUEST_DECRYPT_PGP:
359 conversation.getAccount().getPgpDecryptionService().continueDecryption(data);
360 break;
361 case REQUEST_CHOOSE_PGP_ID:
362 long id = data.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, 0);
363 if (id != 0) {
364 conversation.getAccount().setPgpSignId(id);
365 announcePgp(conversation.getAccount(), null, null, onOpenPGPKeyPublished);
366 } else {
367 choosePgpSignId(conversation.getAccount());
368 }
369 break;
370 case REQUEST_ANNOUNCE_PGP:
371 announcePgp(conversation.getAccount(), conversation, data, onOpenPGPKeyPublished);
372 break;
373 }
374 }
375
376 @Override
377 protected void onCreate(final Bundle savedInstanceState) {
378 super.onCreate(savedInstanceState);
379 ConversationMenuConfigurator.reloadFeatures(this);
380 OmemoSetting.load(this);
381 new EmojiService(this).init();
382 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_conversations);
383 setSupportActionBar((Toolbar) binding.toolbar);
384 configureActionBar(getSupportActionBar());
385 this.getFragmentManager().addOnBackStackChangedListener(this::invalidateActionBarTitle);
386 this.getFragmentManager().addOnBackStackChangedListener(this::showDialogsIfMainIsOverview);
387 this.initializeFragments();
388 this.invalidateActionBarTitle();
389 final Intent intent;
390 if (savedInstanceState == null) {
391 intent = getIntent();
392 } else {
393 intent = savedInstanceState.getParcelable("intent");
394 }
395 if (isViewOrShareIntent(intent)) {
396 pendingViewIntent.push(intent);
397 setIntent(createLauncherIntent(this));
398 }
399 }
400
401 @Override
402 public boolean onCreateOptionsMenu(Menu menu) {
403 getMenuInflater().inflate(R.menu.activity_conversations, menu);
404 MenuItem qrCodeScanMenuItem = menu.findItem(R.id.action_scan_qr_code);
405 if (qrCodeScanMenuItem != null) {
406 if (isCameraFeatureAvailable()) {
407 Fragment fragment = getFragmentManager().findFragmentById(R.id.main_fragment);
408 boolean visible = getResources().getBoolean(R.bool.show_qr_code_scan)
409 && fragment != null
410 && fragment instanceof ConversationsOverviewFragment;
411 qrCodeScanMenuItem.setVisible(visible);
412 } else {
413 qrCodeScanMenuItem.setVisible(false);
414 }
415 }
416 return super.onCreateOptionsMenu(menu);
417 }
418
419 @Override
420 public void onConversationSelected(Conversation conversation) {
421 if (ConversationFragment.getConversation(this) == conversation) {
422 Log.d(Config.LOGTAG, "ignore onConversationSelected() because conversation is already open");
423 return;
424 }
425 openConversation(conversation, null);
426 }
427
428 private void displayToast(final String msg) {
429 runOnUiThread(() -> Toast.makeText(ConversationsActivity.this, msg, Toast.LENGTH_SHORT).show());
430 }
431
432 @Override
433 public void onAffiliationChangedSuccessful(Jid jid) {
434
435 }
436
437 @Override
438 public void onAffiliationChangeFailed(Jid jid, int resId) {
439 displayToast(getString(resId, jid.asBareJid().toString()));
440 }
441
442 @Override
443 public void onRoleChangedSuccessful(String nick) {
444
445 }
446
447 @Override
448 public void onRoleChangeFailed(String nick, int resId) {
449 displayToast(getString(resId, nick));
450 }
451
452 private void openConversation(Conversation conversation, Bundle extras) {
453 ConversationFragment conversationFragment = (ConversationFragment) getFragmentManager().findFragmentById(R.id.secondary_fragment);
454 final boolean mainNeedsRefresh;
455 if (conversationFragment == null) {
456 mainNeedsRefresh = false;
457 Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
458 if (mainFragment != null && mainFragment instanceof ConversationFragment) {
459 conversationFragment = (ConversationFragment) mainFragment;
460 } else {
461 conversationFragment = new ConversationFragment();
462 FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
463 fragmentTransaction.replace(R.id.main_fragment, conversationFragment);
464 fragmentTransaction.addToBackStack(null);
465 try {
466 fragmentTransaction.commit();
467 } catch (IllegalStateException e) {
468 Log.w(Config.LOGTAG,"sate loss while opening conversation",e);
469 //allowing state loss is probably fine since view intents et all are already stored and a click can probably be 'ignored'
470 return;
471 }
472 }
473 } else {
474 mainNeedsRefresh = true;
475 }
476 conversationFragment.reInit(conversation, extras == null ? new Bundle() : extras);
477 if (mainNeedsRefresh) {
478 refreshFragment(R.id.main_fragment);
479 } else {
480 invalidateActionBarTitle();
481 }
482 }
483
484 public boolean onXmppUriClicked(Uri uri) {
485 XmppUri xmppUri = new XmppUri(uri);
486 if (xmppUri.isJidValid() && !xmppUri.hasFingerprints()) {
487 final Conversation conversation = xmppConnectionService.findUniqueConversationByJid(xmppUri);
488 if (conversation != null) {
489 openConversation(conversation, null);
490 return true;
491 }
492 }
493 return false;
494 }
495
496 @Override
497 public boolean onOptionsItemSelected(MenuItem item) {
498 if (MenuDoubleTabUtil.shouldIgnoreTap()) {
499 return false;
500 }
501 switch (item.getItemId()) {
502 case android.R.id.home:
503 FragmentManager fm = getFragmentManager();
504 if (fm.getBackStackEntryCount() > 0) {
505 try {
506 fm.popBackStack();
507 } catch (IllegalStateException e) {
508 Log.w(Config.LOGTAG,"Unable to pop back stack after pressing home button");
509 }
510 return true;
511 }
512 break;
513 case R.id.action_scan_qr_code:
514 UriHandlerActivity.scan(this);
515 return true;
516 }
517 return super.onOptionsItemSelected(item);
518 }
519
520 @Override
521 public void onSaveInstanceState(Bundle savedInstanceState) {
522 Intent pendingIntent = pendingViewIntent.peek();
523 savedInstanceState.putParcelable("intent", pendingIntent != null ? pendingIntent : getIntent());
524 super.onSaveInstanceState(savedInstanceState);
525 }
526
527 @Override
528 protected void onStart() {
529 final int theme = findTheme();
530 if (this.mTheme != theme) {
531 this.mSkipBackgroundBinding = true;
532 recreate();
533 } else {
534 this.mSkipBackgroundBinding = false;
535 }
536 mRedirectInProcess.set(false);
537 super.onStart();
538 }
539
540 @Override
541 protected void onNewIntent(final Intent intent) {
542 if (isViewOrShareIntent(intent)) {
543 if (xmppConnectionService != null) {
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 != null && 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 != null && 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 != null && 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 != null && 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 != null && 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}