Use resources for foreground notification

This commit is contained in:
Paul Schaub 2020-07-31 00:29:11 +02:00
parent c0c0de8c2c
commit 3a7b17649a
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
2 changed files with 32 additions and 14 deletions

View File

@ -6,6 +6,7 @@ import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.IBinder;
import androidx.annotation.NonNull;
@ -128,18 +129,19 @@ public class MercuryForegroundService extends Service {
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
startMainActivityIntent, 0);
String notificationText = notificationTextFrom(state);
Resources resources = context.getResources();
String notificationText = notificationTextFrom(state, resources);
return new NotificationCompat.Builder(context, Notifications.NOTIFICATION_CHANNEL__FOREGROUND_SERVICE)
.setSmallIcon(R.drawable.ic_mercury_black_24dp)
.setContentTitle("Mercury-IM is running!")
.setContentTitle(resources.getString(R.string.title_foreground_service))
.setContentText(notificationText)
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationText))
.setContentIntent(pendingIntent)
.build();
}
private static String notificationTextFrom(ConnectionPoolState state) {
private static String notificationTextFrom(ConnectionPoolState state, Resources resources) {
List<UUID> connecting = new ArrayList<>();
List<UUID> authenticated = new ArrayList<>();
List<UUID> erred = new ArrayList<>();
@ -163,29 +165,29 @@ public class MercuryForegroundService extends Service {
StringBuilder sb = new StringBuilder();
if (!authenticated.isEmpty()) {
sb.append(authenticated.size())
.append(authenticated.size() == 1 ? " account" : " accounts")
.append(" connected.");
sb.append(resources.getQuantityString(R.plurals.foreground_service_accounts_connected,
authenticated.size(), authenticated.size()));
}
if (!connecting.isEmpty()) {
if (!sb.toString().isEmpty()) {
if (!authenticated.isEmpty()) {
sb.append("\n");
}
sb.append(connecting.size())
.append(authenticated.size() == 1 ? " account" : " accounts")
.append(" connecting.");
sb.append(resources.getQuantityString(R.plurals.foreground_service_accounts_connecting,
connecting.size(), connecting.size()));
}
if (!erred.isEmpty()) {
if (!sb.toString().isEmpty()) {
if (!authenticated.isEmpty() || !connecting.isEmpty()) {
sb.append("\n");
}
Iterator<UUID> iterator = erred.iterator();
StringBuilder accounts = new StringBuilder();
while (iterator.hasNext()) {
UUID id = iterator.next();
sb.append(getAddress(state, id));
if (iterator.hasNext()) sb.append(", ");
accounts.append(getAddress(state, id));
if (iterator.hasNext()) accounts.append(", ");
}
sb.append(erred.size() == 1 ? " has an error." : " have errors.");
sb.append(resources.getQuantityString(R.plurals.foreground_service_accounts_have_errors,
erred.size(), accounts));
}
return sb.toString();
}

View File

@ -30,6 +30,22 @@
<string name="channel_description_foreground">Service that keeps the app running</string>
<string name="channel_description_message">Notifications about incoming messages</string>
<string name="title_foreground_service">Mercury-IM is running!</string>
<plurals name="foreground_service_accounts_connected">
<item quantity="zero">No account connected</item>
<item quantity="one">%d account connected</item>
<item quantity="other">%d accounts connected</item>
</plurals>
<plurals name="foreground_service_accounts_connecting">
<item quantity="zero">No account connecting</item>
<item quantity="one">%d account connecting</item>
<item quantity="other">%d accounts connecting</item>
</plurals>
<plurals name="foreground_service_accounts_have_errors">
<item quantity="one">%s has an error</item>
<item quantity="other">%s have errors</item>
</plurals>
<!-- TODO: DELETE BELOW STUFF -->
<!-- Example General settings -->