diff options
author | Andrew Comminos <andrew@comminos.com> | 2016-03-26 04:33:12 +0300 |
---|---|---|
committer | Andrew Comminos <andrew@comminos.com> | 2016-03-26 04:33:12 +0300 |
commit | 1eea9470d8e85fdb17a9f498b3c2e11b8a876844 (patch) | |
tree | ae851aa0cab70f0978d9b4c265a3eca769fbfa5f | |
parent | f841ab859748909e704ab58596c2d8598cf1e799 (diff) |
Implement new message notifications.
-rw-r--r-- | app/src/main/java/com/morlunk/mumbleclient/service/PlumbleConnectionNotification.java (renamed from app/src/main/java/com/morlunk/mumbleclient/service/PlumbleNotification.java) | 46 | ||||
-rw-r--r-- | app/src/main/java/com/morlunk/mumbleclient/service/PlumbleMessageNotification.java | 102 | ||||
-rw-r--r-- | app/src/main/java/com/morlunk/mumbleclient/service/PlumbleService.java | 23 | ||||
-rw-r--r-- | app/src/main/res/values/strings.xml | 1 |
4 files changed, 126 insertions, 46 deletions
diff --git a/app/src/main/java/com/morlunk/mumbleclient/service/PlumbleNotification.java b/app/src/main/java/com/morlunk/mumbleclient/service/PlumbleConnectionNotification.java index e40829d..e9ca660 100644 --- a/app/src/main/java/com/morlunk/mumbleclient/service/PlumbleNotification.java +++ b/app/src/main/java/com/morlunk/mumbleclient/service/PlumbleConnectionNotification.java @@ -37,7 +37,7 @@ import java.util.List; * Wrapper to create Plumble notifications. * Created by andrew on 08/08/14. */ -public class PlumbleNotification { +public class PlumbleConnectionNotification { private static final int NOTIFICATION_ID = 1; private static final String BROADCAST_MUTE = "b_mute"; private static final String BROADCAST_DEAFEN = "b_deafen"; @@ -45,7 +45,6 @@ public class PlumbleNotification { private Service mService; private OnActionListener mListener; - private List<String> mMessages; private String mCustomTicker; private String mCustomContentText; private boolean mActionsShown; @@ -69,18 +68,17 @@ public class PlumbleNotification { * @param listener An listener for notification actions. * @return A new PlumbleNotification instance. */ - public static PlumbleNotification showForeground(Service service, String ticker, String contentText, + public static PlumbleConnectionNotification showForeground(Service service, String ticker, String contentText, OnActionListener listener) { - PlumbleNotification notification = new PlumbleNotification(service, ticker, contentText, listener); + PlumbleConnectionNotification notification = new PlumbleConnectionNotification(service, ticker, contentText, listener); notification.show(); return notification; } - private PlumbleNotification(Service service, String ticker, String contentText, - OnActionListener listener) { + private PlumbleConnectionNotification(Service service, String ticker, String contentText, + OnActionListener listener) { mService = service; mListener = listener; - mMessages = new ArrayList<String>(); mCustomTicker = ticker; mCustomContentText = contentText; mActionsShown = false; @@ -99,20 +97,6 @@ public class PlumbleNotification { } /** - * Updates the notification with the given message. - * Sets the ticker to the current message as well. - * @param message The message to notify. - */ - public void addMessage(String message) { - mMessages.add(message); - mCustomTicker = message; - } - - public void clearMessages() { - mMessages.clear(); - } - - /** * Shows the notification and registers the notification action button receiver. */ public void show() { @@ -148,11 +132,10 @@ public class PlumbleNotification { */ private Notification createNotification() { NotificationCompat.Builder builder = new NotificationCompat.Builder(mService); - builder.setSmallIcon(R.drawable.ic_stat_notify); - builder.setTicker(mCustomTicker); builder.setContentTitle(mService.getString(R.string.app_name)); builder.setContentText(mCustomContentText); - builder.setPriority(NotificationCompat.PRIORITY_HIGH); + builder.setSmallIcon(R.drawable.ic_stat_notify); + builder.setPriority(NotificationCompat.PRIORITY_MIN); builder.setOngoing(true); if (mActionsShown) { @@ -172,15 +155,6 @@ public class PlumbleNotification { overlayIntent, PendingIntent.FLAG_CANCEL_CURRENT)); } - // Show unread messages - if (mMessages.size() > 0) { - NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); - for (String message : mMessages) { - inboxStyle.addLine(message); - } - builder.setStyle(inboxStyle); - } - Intent channelListIntent = new Intent(mService, PlumbleActivity.class); channelListIntent.putExtra(PlumbleActivity.EXTRA_DRAWER_FRAGMENT, DrawerAdapter.ITEM_SERVER); // FLAG_CANCEL_CURRENT ensures that the extra always gets sent. @@ -193,8 +167,8 @@ public class PlumbleNotification { } public interface OnActionListener { - public void onMuteToggled(); - public void onDeafenToggled(); - public void onOverlayToggled(); + void onMuteToggled(); + void onDeafenToggled(); + void onOverlayToggled(); } } diff --git a/app/src/main/java/com/morlunk/mumbleclient/service/PlumbleMessageNotification.java b/app/src/main/java/com/morlunk/mumbleclient/service/PlumbleMessageNotification.java new file mode 100644 index 0000000..63775b7 --- /dev/null +++ b/app/src/main/java/com/morlunk/mumbleclient/service/PlumbleMessageNotification.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2016 Andrew Comminos <andrew@comminos.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package com.morlunk.mumbleclient.service; + +import android.app.Notification; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.support.v4.app.NotificationCompat; +import android.support.v4.app.NotificationManagerCompat; + +import com.morlunk.jumble.model.Channel; +import com.morlunk.jumble.model.IChannel; +import com.morlunk.jumble.model.IMessage; +import com.morlunk.mumbleclient.R; +import com.morlunk.mumbleclient.app.DrawerAdapter; +import com.morlunk.mumbleclient.app.PlumbleActivity; + +import java.util.ArrayList; +import java.util.List; + +/** + * A notification indicating that new messages have been received. + * Intended to augment the existing {@link PlumbleConnectionNotification} by providing a higher + * priority heads-up display on Android 5.0+ devices, as well as vibration. + * Created by andrew on 25/03/16. + */ +public class PlumbleMessageNotification { + private static final int NOTIFICATION_ID = 2; + private static final long VIBRATION_PATTERN[] = { 0, 100 }; + + private final Context mContext; + private final List<IMessage> mUnreadMessages; + + public PlumbleMessageNotification(Context context) { + mContext = context; + mUnreadMessages = new ArrayList<>(); + } + + /** + * Shows the notification with the provided message. + * If the notification is already shown, append the message to the existing notification. + * @param message The message to notify the user about. + */ + public void show(IMessage message) { + mUnreadMessages.add(message); + + NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); + style.setBigContentTitle(mContext.getString(R.string.notification_unread_many, mUnreadMessages.size())); + for (IMessage m : mUnreadMessages) { + String line = mContext.getString(R.string.notification_message, m.getActorName(), m.getMessage()); + style.addLine(line); + } + + Intent channelListIntent = new Intent(mContext, PlumbleActivity.class); + channelListIntent.putExtra(PlumbleActivity.EXTRA_DRAWER_FRAGMENT, DrawerAdapter.ITEM_SERVER); + // FLAG_CANCEL_CURRENT ensures that the extra always gets sent. + PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, channelListIntent, PendingIntent.FLAG_CANCEL_CURRENT); + + NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext) + .setPriority(NotificationCompat.PRIORITY_HIGH) + .setSmallIcon(R.drawable.ic_stat_notify) + .setContentIntent(pendingIntent) + .setAutoCancel(true) + .setTicker(message.getActorName()) + .setContentTitle(message.getActorName()) + .setContentText(message.getMessage()) + .setVibrate(VIBRATION_PATTERN) + .setStyle(style); + + if (mUnreadMessages.size() > 0) + builder.setNumber(mUnreadMessages.size()); + + final NotificationManagerCompat manager = NotificationManagerCompat.from(mContext); + Notification notification = builder.build(); + manager.notify(NOTIFICATION_ID, notification); + } + + /** + * Dismisses the unread messages notification, marking all messages read. + */ + public void dismiss() { + mUnreadMessages.clear(); + final NotificationManagerCompat manager = NotificationManagerCompat.from(mContext); + manager.cancel(NOTIFICATION_ID); + } +} diff --git a/app/src/main/java/com/morlunk/mumbleclient/service/PlumbleService.java b/app/src/main/java/com/morlunk/mumbleclient/service/PlumbleService.java index 08721a8..e892483 100644 --- a/app/src/main/java/com/morlunk/mumbleclient/service/PlumbleService.java +++ b/app/src/main/java/com/morlunk/mumbleclient/service/PlumbleService.java @@ -19,14 +19,11 @@ package com.morlunk.mumbleclient.service; import android.app.AlertDialog; import android.content.BroadcastReceiver; -import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.media.AudioManager; import android.os.Bundle; -import android.os.IBinder; import android.os.PowerManager; -import android.os.RemoteException; import android.preference.PreferenceManager; import android.speech.tts.TextToSpeech; import android.view.WindowManager; @@ -57,7 +54,7 @@ import java.util.List; */ public class PlumbleService extends JumbleService implements SharedPreferences.OnSharedPreferenceChangeListener, - PlumbleNotification.OnActionListener, + PlumbleConnectionNotification.OnActionListener, PlumbleReconnectNotification.OnActionListener { /** Undocumented constant that permits a proximity-sensing wake lock. */ public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK = 32; @@ -65,7 +62,8 @@ public class PlumbleService extends JumbleService implements public static final int RECONNECT_DELAY = 10000; private Settings mSettings; - private PlumbleNotification mNotification; + private PlumbleConnectionNotification mNotification; + private PlumbleMessageNotification mMessageNotification; private PlumbleReconnectNotification mReconnectNotification; /** Channel view overlay. */ private PlumbleOverlay mChannelOverlay; @@ -117,7 +115,7 @@ public class PlumbleService extends JumbleService implements mReconnectNotification = null; } - mNotification = PlumbleNotification.showForeground(PlumbleService.this, + mNotification = PlumbleConnectionNotification.showForeground(PlumbleService.this, getString(R.string.plumbleConnecting), getString(R.string.connecting), PlumbleService.this); @@ -217,6 +215,11 @@ public class PlumbleService extends JumbleService implements mTTS.speak(formattedTtsMessage, TextToSpeech.QUEUE_ADD, null); } + // TODO: create a customizable notification sieve + if (mSettings.isChatNotifyEnabled()) { + mMessageNotification.show(message); + } + mMessageLog.add(new IChatMessage.TextMessage(message)); } @@ -283,6 +286,7 @@ public class PlumbleService extends JumbleService implements mTalkReceiver = new TalkBroadcastReceiver(this); mMessageLog = new ArrayList<>(); + mMessageNotification = new PlumbleMessageNotification(PlumbleService.this); } @Override @@ -307,6 +311,7 @@ public class PlumbleService extends JumbleService implements unregisterObserver(mObserver); if(mTTS != null) mTTS.shutdown(); mMessageLog = null; + mMessageNotification.dismiss(); super.onDestroy(); } @@ -346,6 +351,7 @@ public class PlumbleService extends JumbleService implements setProximitySensorOn(false); mMessageLog.clear(); + mMessageNotification.dismiss(); } /** @@ -508,10 +514,7 @@ public class PlumbleService extends JumbleService implements } public void clearChatNotifications() { - if (mNotification != null) { - mNotification.clearMessages(); - mNotification.show(); - } + mMessageNotification.dismiss(); } public void markErrorShown() { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8610463..b25ff61 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -259,4 +259,5 @@ <string name="migration_certificate_success">Successfully migrated certificate!</string> <string name="error_writing_to_storage">Error writing to external storage.</string> <string name="export_success">Exported certificate to \'%s\'.</string> + <string name="notification_unread_many">%d new messages</string> </resources> |