From ffbdad7503e5d70d461adb65d7841856ea60c2fd Mon Sep 17 00:00:00 2001 From: kosyak Date: Mon, 2 Sep 2024 01:00:39 +0200 Subject: [PATCH] show participants count/last seen in toolbar indicator --- .../ui/ContactDetailsActivity.java | 2 +- .../ui/ConversationsActivity.java | 41 ++++++++++++++ .../siacs/conversations/utils/UIHelper.java | 56 +++++++++++++++---- src/main/res/values/strings.xml | 13 +++++ 4 files changed, 100 insertions(+), 12 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java b/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java index cc7773ad2..8daffdbbc 100644 --- a/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java @@ -504,7 +504,7 @@ public class ContactDetailsActivity extends OmemoActivity implements OnAccountUp && contact.getLastseen() > 0 && contact.getPresences().allOrNonSupport(Namespace.IDLE)) { binding.detailsLastseen.setVisibility(View.VISIBLE); - binding.detailsLastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.isActive(), contact.getLastseen())); + binding.detailsLastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.isActive(), contact.getLastseen(), false)); } else { binding.detailsLastseen.setVisibility(View.GONE); } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java index b225d8404..5d7762488 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java @@ -42,10 +42,14 @@ import android.app.FragmentTransaction; import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; +import android.preference.PreferenceManager; import android.provider.Settings; import android.util.Log; import android.view.KeyEvent; @@ -95,7 +99,9 @@ import eu.siacs.conversations.ui.util.PendingItem; import eu.siacs.conversations.utils.ExceptionHelper; import eu.siacs.conversations.utils.PhoneNumberUtilWrapper; import eu.siacs.conversations.utils.SignupUtils; +import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.XmppUri; +import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xmpp.Jid; import eu.siacs.conversations.xmpp.OnUpdateBlocklist; import io.michaelrocks.libphonenumber.android.NumberParseException; @@ -135,6 +141,10 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio private boolean mActivityPaused = true; private final AtomicBoolean mRedirectInProcess = new AtomicBoolean(false); + private final Handler handler = new Handler(Looper.getMainLooper()); + private final Runnable refreshTitleRunnable = this::invalidateActionBarTitle; + private boolean showLastSeen = false; + private static boolean isViewOrShareIntent(Intent i) { Log.d(Config.LOGTAG, "action: " + (i == null ? null : i.getAction())); return i != null && VIEW_AND_SHARE_ACTIONS.contains(i.getAction()) && i.hasExtra(EXTRA_CONVERSATION); @@ -660,6 +670,8 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio this.mSkipBackgroundBinding = false; } mRedirectInProcess.set(false); + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); + this.showLastSeen = preferences.getBoolean("last_activity", false); BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation); bottomNavigationView.setSelectedItemId(R.id.chats); @@ -735,6 +747,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio if (actionBar == null) { return; } + final FragmentManager fragmentManager = getFragmentManager(); final Fragment mainFragment = fragmentManager.findFragmentById(R.id.main_fragment); if (mainFragment instanceof ConversationFragment) { @@ -750,10 +763,38 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio binding.toolbar, (v) -> openConversationDetails(conversation) ); + + if (conversation.getMode() == Conversation.MODE_MULTI && conversation.getNextCounterpart() == null) { + int usersCount = conversation.getMucOptions().getUserCount(); + if (usersCount > 0) { + actionBar.setSubtitle(getResources().getQuantityString(R.plurals.x_participants, conversation.getMucOptions().getUserCount(), conversation.getMucOptions().getUserCount())); + } else { + actionBar.setSubtitle(""); + } + + handler.postDelayed(refreshTitleRunnable, 5000L); + } else if (conversation.getMode() == Conversation.MODE_SINGLE) { + Contact contact = conversation.getContact(); + if (showLastSeen + && contact.getLastseen() > 0 + && contact.getPresences().allOrNonSupport(Namespace.IDLE)) { + actionBar.setSubtitle(UIHelper.lastseen(getApplicationContext(), contact.isActive(), contact.getLastseen(), true)); + } else { + actionBar.setSubtitle(""); + handler.removeCallbacks(refreshTitleRunnable); + } + } else { + actionBar.setSubtitle(""); + handler.removeCallbacks(refreshTitleRunnable); + } + return; } } + + handler.removeCallbacks(refreshTitleRunnable); actionBar.setTitle(R.string.app_name); + actionBar.setSubtitle(""); actionBar.setDisplayHomeAsUpEnabled(false); ActionBarUtil.resetActionBarOnClickListeners(binding.toolbar); } diff --git a/src/main/java/eu/siacs/conversations/utils/UIHelper.java b/src/main/java/eu/siacs/conversations/utils/UIHelper.java index ea674c893..f684c5f46 100644 --- a/src/main/java/eu/siacs/conversations/utils/UIHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/UIHelper.java @@ -205,26 +205,60 @@ public class UIHelper { .get(Calendar.DAY_OF_YEAR); } - public static String lastseen(Context context, boolean active, long time) { + public static String lastseen(Context context, boolean active, long time, boolean shortText) { long difference = (System.currentTimeMillis() - time) / 1000; if (active) { - return context.getString(R.string.online_right_now); + if (shortText) { + return context.getString(R.string.online_right_now_short); + } else { + return context.getString(R.string.online_right_now); + } } else if (difference < 60) { - return context.getString(R.string.last_seen_now); + if (shortText) { + return context.getString(R.string.last_seen_now_short); + } else { + return context.getString(R.string.last_seen_now); + } } else if (difference < 60 * 2) { - return context.getString(R.string.last_seen_min); + if (shortText) { + return context.getString(R.string.last_seen_min_short); + } else { + return context.getString(R.string.last_seen_min); + } } else if (difference < 60 * 60) { - return context.getString(R.string.last_seen_mins, Math.round(difference / 60.0)); + if (shortText) { + return context.getString(R.string.last_seen_mins_short, Math.round(difference / 60.0)); + } else { + return context.getString(R.string.last_seen_mins, Math.round(difference / 60.0)); + } } else if (difference < 60 * 60 * 2) { - return context.getString(R.string.last_seen_hour); + if (shortText) { + return context.getString(R.string.last_seen_hour_short); + } else { + return context.getString(R.string.last_seen_hour); + } } else if (difference < 60 * 60 * 24) { - return context.getString(R.string.last_seen_hours, - Math.round(difference / (60.0 * 60.0))); + if (shortText) { + return context.getString(R.string.last_seen_hours_short, + Math.round(difference / (60.0 * 60.0))); + } else { + return context.getString(R.string.last_seen_hours, + Math.round(difference / (60.0 * 60.0))); + } } else if (difference < 60 * 60 * 48) { - return context.getString(R.string.last_seen_day); + if (shortText) { + return context.getString(R.string.last_seen_day_short); + } else { + return context.getString(R.string.last_seen_day); + } } else { - return context.getString(R.string.last_seen_days, - Math.round(difference / (60.0 * 60.0 * 24.0))); + if (shortText) { + return context.getString(R.string.last_seen_days_short, + Math.round(difference / (60.0 * 60.0 * 24.0))); + } else { + return context.getString(R.string.last_seen_days, + Math.round(difference / (60.0 * 60.0 * 24.0))); + } } } diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index a6c021455..92ce2eac0 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -1108,6 +1108,19 @@ Rounded Square Square + + %1$d participant + %1$d participants + + + online + just now + one minute ago + %d minutes ago + one hour ago + %d hours ago + one day ago + %d days ago Clarendon OldMan