Use resources for foreground notification
This commit is contained in:
parent
c0c0de8c2c
commit
3a7b17649a
|
@ -6,6 +6,7 @@ import android.app.PendingIntent;
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.res.Resources;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
@ -128,18 +129,19 @@ public class MercuryForegroundService extends Service {
|
||||||
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
|
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
|
||||||
startMainActivityIntent, 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)
|
return new NotificationCompat.Builder(context, Notifications.NOTIFICATION_CHANNEL__FOREGROUND_SERVICE)
|
||||||
.setSmallIcon(R.drawable.ic_mercury_black_24dp)
|
.setSmallIcon(R.drawable.ic_mercury_black_24dp)
|
||||||
.setContentTitle("Mercury-IM is running!")
|
.setContentTitle(resources.getString(R.string.title_foreground_service))
|
||||||
.setContentText(notificationText)
|
.setContentText(notificationText)
|
||||||
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationText))
|
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationText))
|
||||||
.setContentIntent(pendingIntent)
|
.setContentIntent(pendingIntent)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String notificationTextFrom(ConnectionPoolState state) {
|
private static String notificationTextFrom(ConnectionPoolState state, Resources resources) {
|
||||||
List<UUID> connecting = new ArrayList<>();
|
List<UUID> connecting = new ArrayList<>();
|
||||||
List<UUID> authenticated = new ArrayList<>();
|
List<UUID> authenticated = new ArrayList<>();
|
||||||
List<UUID> erred = new ArrayList<>();
|
List<UUID> erred = new ArrayList<>();
|
||||||
|
@ -163,29 +165,29 @@ public class MercuryForegroundService extends Service {
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
if (!authenticated.isEmpty()) {
|
if (!authenticated.isEmpty()) {
|
||||||
sb.append(authenticated.size())
|
sb.append(resources.getQuantityString(R.plurals.foreground_service_accounts_connected,
|
||||||
.append(authenticated.size() == 1 ? " account" : " accounts")
|
authenticated.size(), authenticated.size()));
|
||||||
.append(" connected.");
|
|
||||||
}
|
}
|
||||||
if (!connecting.isEmpty()) {
|
if (!connecting.isEmpty()) {
|
||||||
if (!sb.toString().isEmpty()) {
|
if (!authenticated.isEmpty()) {
|
||||||
sb.append("\n");
|
sb.append("\n");
|
||||||
}
|
}
|
||||||
sb.append(connecting.size())
|
sb.append(resources.getQuantityString(R.plurals.foreground_service_accounts_connecting,
|
||||||
.append(authenticated.size() == 1 ? " account" : " accounts")
|
connecting.size(), connecting.size()));
|
||||||
.append(" connecting.");
|
|
||||||
}
|
}
|
||||||
if (!erred.isEmpty()) {
|
if (!erred.isEmpty()) {
|
||||||
if (!sb.toString().isEmpty()) {
|
if (!authenticated.isEmpty() || !connecting.isEmpty()) {
|
||||||
sb.append("\n");
|
sb.append("\n");
|
||||||
}
|
}
|
||||||
Iterator<UUID> iterator = erred.iterator();
|
Iterator<UUID> iterator = erred.iterator();
|
||||||
|
StringBuilder accounts = new StringBuilder();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
UUID id = iterator.next();
|
UUID id = iterator.next();
|
||||||
sb.append(getAddress(state, id));
|
accounts.append(getAddress(state, id));
|
||||||
if (iterator.hasNext()) sb.append(", ");
|
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();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,22 @@
|
||||||
<string name="channel_description_foreground">Service that keeps the app running</string>
|
<string name="channel_description_foreground">Service that keeps the app running</string>
|
||||||
<string name="channel_description_message">Notifications about incoming messages</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 -->
|
<!-- TODO: DELETE BELOW STUFF -->
|
||||||
<!-- Example General settings -->
|
<!-- Example General settings -->
|
||||||
|
|
Loading…
Reference in New Issue