Added actions to notifications (this required a slight detour)

This commit is contained in:
2016-10-20 12:53:35 +02:00
parent b3dd53a5df
commit 6a8648ca28
15 changed files with 405 additions and 165 deletions

View File

@ -27,21 +27,26 @@ import java.util.TimerTask;
import ch.dissem.apps.abit.MainActivity;
import ch.dissem.apps.abit.R;
import ch.dissem.apps.abit.service.BitmessageIntentService;
import ch.dissem.apps.abit.service.BitmessageService;
import ch.dissem.bitmessage.utils.Property;
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
import static ch.dissem.apps.abit.MainActivity.updateNodeSwitch;
/**
* Shows the network status (as long as the client is connected as a full node)
*/
public class NetworkNotification extends AbstractNotification {
public static final int ONGOING_NOTIFICATION_ID = 2;
public static final int NETWORK_NOTIFICATION_ID = 2;
private NotificationCompat.Builder builder;
private final NotificationCompat.Builder builder;
private Timer timer;
public NetworkNotification(Context ctx) {
super(ctx);
Intent showMessageIntent = new Intent(ctx, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 1, showMessageIntent, 0);
Intent showAppIntent = new Intent(ctx, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 1, showAppIntent, 0);
builder = new NotificationCompat.Builder(ctx);
builder.setSmallIcon(R.drawable.ic_notification_full_node)
.setContentTitle(ctx.getString(R.string.bitmessage_full_node))
@ -58,10 +63,7 @@ public class NetworkNotification extends AbstractNotification {
Property connections = BitmessageService.getStatus().getProperty("network", "connections");
if (!running) {
builder.setContentText(ctx.getString(R.string.connection_info_disconnected));
MainActivity mainActivity = MainActivity.getInstance();
if (mainActivity != null) {
mainActivity.updateNodeSwitch();
}
updateNodeSwitch();
} else if (connections.getProperties().length == 0) {
builder.setContentText(ctx.getString(R.string.connection_info_pending));
} else {
@ -80,6 +82,19 @@ public class NetworkNotification extends AbstractNotification {
}
builder.setContentText(info);
}
builder.mActions.clear();
Intent intent = new Intent(ctx, BitmessageIntentService.class);
if (running) {
intent.putExtra(BitmessageIntentService.EXTRA_SHUTDOWN_NODE, true);
builder.addAction(R.drawable.ic_notification_node_stop,
ctx.getString(R.string.full_node_stop),
PendingIntent.getService(ctx, 0, intent, FLAG_UPDATE_CURRENT));
} else {
intent.putExtra(BitmessageIntentService.EXTRA_STARTUP_NODE, true);
builder.addAction(R.drawable.ic_notification_node_start,
ctx.getString(R.string.full_node_restart),
PendingIntent.getService(ctx, 1, intent, FLAG_UPDATE_CURRENT));
}
notification = builder.build();
return running;
}
@ -88,7 +103,8 @@ public class NetworkNotification extends AbstractNotification {
public void show() {
super.show();
new Timer().schedule(new TimerTask() {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (!update()) {
@ -99,14 +115,28 @@ public class NetworkNotification extends AbstractNotification {
}, 10_000, 10_000);
}
public void showShutdown() {
if (timer != null) {
timer.cancel();
}
update();
super.show();
}
@Override
protected int getNotificationId() {
return ONGOING_NOTIFICATION_ID;
return NETWORK_NOTIFICATION_ID;
}
public void connecting() {
builder.setOngoing(true);
builder.setContentText(ctx.getString(R.string.connection_info_pending));
Intent intent = new Intent(ctx, BitmessageIntentService.class);
intent.putExtra(BitmessageIntentService.EXTRA_SHUTDOWN_NODE, true);
builder.mActions.clear();
builder.addAction(R.drawable.ic_notification_node_stop,
ctx.getString(R.string.full_node_stop),
PendingIntent.getService(ctx, 0, intent, FLAG_UPDATE_CURRENT));
notification = builder.build();
}
}

View File

@ -31,8 +31,13 @@ import java.util.Collection;
import ch.dissem.apps.abit.Identicon;
import ch.dissem.apps.abit.MainActivity;
import ch.dissem.apps.abit.R;
import ch.dissem.apps.abit.service.BitmessageIntentService;
import ch.dissem.bitmessage.entity.Plaintext;
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
import static ch.dissem.apps.abit.MainActivity.EXTRA_REPLY_TO_MESSAGE;
import static ch.dissem.apps.abit.MainActivity.EXTRA_SHOW_MESSAGE;
import static ch.dissem.apps.abit.service.BitmessageIntentService.EXTRA_DELETE_MESSAGE;
import static ch.dissem.apps.abit.util.Drawables.toBitmap;
public class NewMessageNotification extends AbstractNotification {
@ -46,51 +51,59 @@ public class NewMessageNotification extends AbstractNotification {
public NewMessageNotification singleNotification(Plaintext plaintext) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
Spannable bigText = new SpannableString(plaintext.getSubject() + "\n" + plaintext.getText
());
());
bigText.setSpan(SPAN_EMPHASIS, 0, plaintext.getSubject().length(), Spanned
.SPAN_INCLUSIVE_EXCLUSIVE);
.SPAN_INCLUSIVE_EXCLUSIVE);
builder.setSmallIcon(R.drawable.ic_notification_new_message)
.setLargeIcon(toBitmap(new Identicon(plaintext.getFrom()), 192))
.setContentTitle(plaintext.getFrom().toString())
.setContentText(plaintext.getSubject())
.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText))
.setContentInfo("Info");
.setLargeIcon(toBitmap(new Identicon(plaintext.getFrom()), 192))
.setContentTitle(plaintext.getFrom().toString())
.setContentText(plaintext.getSubject())
.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText))
.setContentInfo("Info");
Intent showMessageIntent = new Intent(ctx, MainActivity.class);
showMessageIntent.putExtra(MainActivity.EXTRA_SHOW_MESSAGE, plaintext);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, showMessageIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
// TODO: add proper intents to reply/delete
builder.addAction(R.drawable.ic_action_reply, ctx.getString(R.string.reply), pendingIntent);
builder.setContentIntent(
createActivityIntent(EXTRA_SHOW_MESSAGE, plaintext));
builder.addAction(R.drawable.ic_action_reply, ctx.getString(R.string.reply),
createActivityIntent(EXTRA_REPLY_TO_MESSAGE, plaintext));
builder.addAction(R.drawable.ic_action_delete, ctx.getString(R.string.delete),
pendingIntent);
createServiceIntent(ctx, EXTRA_DELETE_MESSAGE, plaintext));
notification = builder.build();
return this;
}
private PendingIntent createActivityIntent(String action, Plaintext message) {
Intent intent = new Intent(ctx, MainActivity.class);
intent.putExtra(action, message);
return PendingIntent.getActivity(ctx, action.hashCode(), intent, FLAG_UPDATE_CURRENT);
}
private PendingIntent createServiceIntent(Context ctx, String action, Plaintext message) {
Intent intent = new Intent(ctx, BitmessageIntentService.class);
intent.putExtra(action, message);
return PendingIntent.getService(ctx, action.hashCode(), intent, FLAG_UPDATE_CURRENT);
}
/**
* @param unacknowledged will be accessed from different threads, so make sure wherever it's
* accessed it will be in a <code>synchronized(unacknowledged)
* {}</code> block
*/
public NewMessageNotification multiNotification(Collection<Plaintext> unacknowledged, int
numberOfUnacknowledgedMessages) {
numberOfUnacknowledgedMessages) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
builder.setSmallIcon(R.drawable.ic_notification_new_message)
.setContentTitle(ctx.getString(R.string.n_new_messages, unacknowledged.size()))
.setContentText(ctx.getString(R.string.app_name));
.setContentTitle(ctx.getString(R.string.n_new_messages, unacknowledged.size()))
.setContentText(ctx.getString(R.string.app_name));
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
//noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (unacknowledged) {
inboxStyle.setBigContentTitle(ctx.getString(R.string.n_new_messages,
numberOfUnacknowledgedMessages));
numberOfUnacknowledgedMessages));
for (Plaintext msg : unacknowledged) {
Spannable sb = new SpannableString(msg.getFrom() + " " + msg.getSubject());
sb.setSpan(SPAN_EMPHASIS, 0, String.valueOf(msg.getFrom()).length(), Spannable
.SPAN_INCLUSIVE_EXCLUSIVE);
.SPAN_INCLUSIVE_EXCLUSIVE);
inboxStyle.addLine(sb);
}
}