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