Add new icon and text to foreground notification

This commit is contained in:
Paul Schaub 2020-07-31 00:13:23 +02:00
parent 9e3718c0a1
commit c0c0de8c2c
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
4 changed files with 218 additions and 5 deletions

View File

@ -51,7 +51,6 @@ public class MercuryImApplication extends Application {
appComponent.inject(this);
setupClientStateIndication();
ServerPingWithAlarmManager.onCreate(this);
Notifications.initializeNotificationChannels(this);

View File

@ -1,6 +1,7 @@
package org.mercury_im.messenger.android.service;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
@ -15,6 +16,21 @@ import org.mercury_im.messenger.R;
import org.mercury_im.messenger.android.MercuryImApplication;
import org.mercury_im.messenger.android.Notifications;
import org.mercury_im.messenger.android.ui.MainActivity;
import org.mercury_im.messenger.core.connection.MercuryConnectionManager;
import org.mercury_im.messenger.core.connection.state.ConnectionPoolState;
import org.mercury_im.messenger.core.connection.state.ConnectionState;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Inject;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
/**
* Started, Bound Service, which is responsible keeping the application alive when the app is not open.
@ -30,6 +46,12 @@ public class MercuryForegroundService extends Service {
public static final String ACTION_START = ACTION + ".START";
public static final String ACTION_STOP = ACTION + ".STOP";
private static final Logger LOGGER = Logger.getLogger(MercuryForegroundService.class.getName());
@Inject
MercuryConnectionManager connectionManager;
CompositeDisposable disposable;
@NonNull
@Override
@ -42,6 +64,14 @@ public class MercuryForegroundService extends Service {
super.onCreate();
MercuryImApplication.getApplication().getAppComponent().inject(this);
beginLifecycleOfPingManager();
disposable = new CompositeDisposable();
disposable.add(connectionManager.observeConnectionPool()
.subscribe(state -> {
Notification notification = buildForegroundNotification(getApplicationContext(), state);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Notifications.FOREGROUND_SERVICE_ID, notification);
}));
LOGGER.log(Level.INFO, "onCreate");
}
/**
@ -54,8 +84,9 @@ public class MercuryForegroundService extends Service {
@Override
public void onDestroy() {
super.onDestroy();
disposable.dispose();
endLifecycleOfPingManager();
LOGGER.log(Level.INFO, "onDestroy");
}
private void endLifecycleOfPingManager() {
@ -87,20 +118,82 @@ public class MercuryForegroundService extends Service {
}
private void startAndDisplayForegroundNotification() {
Notification notification = buildForegroundNotification(getApplicationContext());
Notification notification = buildForegroundNotification(getApplicationContext(),
connectionManager.observeConnectionPool().blockingFirst());
startForeground(Notifications.FOREGROUND_SERVICE_ID, notification);
}
private static Notification buildForegroundNotification(Context context) {
private static Notification buildForegroundNotification(Context context, ConnectionPoolState state) {
Intent startMainActivityIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
startMainActivityIntent, 0);
String notificationText = notificationTextFrom(state);
return new NotificationCompat.Builder(context, Notifications.NOTIFICATION_CHANNEL__FOREGROUND_SERVICE)
.setSmallIcon(R.drawable.ic_send_black_24dp)
.setSmallIcon(R.drawable.ic_mercury_black_24dp)
.setContentTitle("Mercury-IM is running!")
.setContentText(notificationText)
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationText))
.setContentIntent(pendingIntent)
.build();
}
private static String notificationTextFrom(ConnectionPoolState state) {
List<UUID> connecting = new ArrayList<>();
List<UUID> authenticated = new ArrayList<>();
List<UUID> erred = new ArrayList<>();
for (UUID id : state.getConnectionStates().keySet()) {
ConnectionState connectionState = state.getConnectionStates().get(id);
switch (connectionState.getConnectivity()) {
case disconnected:
break;
case connecting:
case connected:
connecting.add(id);
break;
case authenticated:
authenticated.add(id);
break;
case disconnectedOnError:
erred.add(id);
break;
}
}
StringBuilder sb = new StringBuilder();
if (!authenticated.isEmpty()) {
sb.append(authenticated.size())
.append(authenticated.size() == 1 ? " account" : " accounts")
.append(" connected.");
}
if (!connecting.isEmpty()) {
if (!sb.toString().isEmpty()) {
sb.append("\n");
}
sb.append(connecting.size())
.append(authenticated.size() == 1 ? " account" : " accounts")
.append(" connecting.");
}
if (!erred.isEmpty()) {
if (!sb.toString().isEmpty()) {
sb.append("\n");
}
Iterator<UUID> iterator = erred.iterator();
while (iterator.hasNext()) {
UUID id = iterator.next();
sb.append(getAddress(state, id));
if (iterator.hasNext()) sb.append(", ");
}
sb.append(erred.size() == 1 ? " has an error." : " have errors.");
}
return sb.toString();
}
private static String getAddress(ConnectionPoolState state, UUID uuid) {
return state.getConnectionStates().get(uuid).getConnection().getAccount().getAddress();
}
public class Binder extends android.os.Binder {
private final MercuryForegroundService service;

View File

@ -0,0 +1,26 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M11.4119,17.5533m-5.3107,0a5.3107,5.3107 0,1 1,10.6215 0a5.3107,5.3107 0,1 1,-10.6215 0"
android:strokeWidth="1.51735461"
android:fillColor="#000000"
android:strokeColor="#00000000"
android:fillAlpha="1"/>
<path
android:pathData="M7.3696,5.3675m-3.7815,0a3.7815,3.7815 0,1 1,7.5631 0a3.7815,3.7815 0,1 1,-7.5631 0"
android:strokeAlpha="0.99"
android:strokeWidth="1.08043778"
android:fillColor="#000000"
android:strokeColor="#00000000"
android:fillAlpha="0.99"/>
<path
android:pathData="M20.2078,10.0262m-2.3234,0a2.3234,2.3234 0,1 1,4.6469 0a2.3234,2.3234 0,1 1,-4.6469 0"
android:strokeWidth="0.66384262"
android:fillColor="#000000"
android:strokeColor="#00000000"
android:fillAlpha="1"/>
</vector>

95
mercury_black_24dp.svg Normal file
View File

@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:osb="http://www.openswatchbook.org/uri/2009/osb"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="24px"
height="24px"
viewBox="0 0 24 24"
version="1.1"
id="SVGRoot"
inkscape:version="0.92.5 (2060ec1f9f, 2020-04-08)"
sodipodi:docname="mercury_24dp_black.svg"
inkscape:export-filename="/home/vanitas/Programmierung/mercury/Mercury-IM/mercury_24dp_blackpng.png"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96">
<defs
id="defs5069">
<linearGradient
id="linearGradient13546"
osb:paint="solid">
<stop
style="stop-color:#f9f9f9;stop-opacity:1;"
offset="0"
id="stop13544" />
</linearGradient>
</defs>
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="21.333334"
inkscape:cx="12.55039"
inkscape:cy="11.808873"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showgrid="false"
inkscape:window-width="1920"
inkscape:window-height="1016"
inkscape:window-x="0"
inkscape:window-y="27"
inkscape:window-maximized="1"
inkscape:grid-bbox="true">
<inkscape:grid
type="xygrid"
id="grid5625" />
</sodipodi:namedview>
<metadata
id="metadata5072">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Ebene 1"
inkscape:groupmode="layer"
id="layer1">
<circle
style="fill:#000000;fill-opacity:1;stroke:none;stroke-width:1.51735461"
id="path5653"
cx="11.411885"
cy="17.553291"
r="5.3107409"
inkscape:label="blob_big" />
<circle
style="opacity:0.98999999;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1.08043778"
id="path5653-6"
cx="7.3695569"
cy="5.3674693"
r="3.7815323"
inkscape:label="blob_med" />
<circle
style="fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.66384262"
id="path5653-7"
cx="20.207802"
cy="10.026221"
r="2.3234494"
inkscape:label="blob_sml" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.8 KiB