diff options
author | Andrew Comminos <andrew@comminos.com> | 2017-02-28 12:46:58 +0300 |
---|---|---|
committer | Andrew Comminos <andrew@comminos.com> | 2017-02-28 12:46:58 +0300 |
commit | 4684fadb58112368e1651dbb65847e86fb4d97d8 (patch) | |
tree | fb2cd5ca6a0a84741cef0897cf56a3207464828d | |
parent | 209737511276cde108a12e18013d7eda2e2259b1 (diff) |
Suppress connection notifications when the activity is foreground.
4 files changed, 33 insertions, 12 deletions
diff --git a/app/src/main/java/com/morlunk/mumbleclient/app/PlumbleActivity.java b/app/src/main/java/com/morlunk/mumbleclient/app/PlumbleActivity.java index 4007822..0794ffd 100644 --- a/app/src/main/java/com/morlunk/mumbleclient/app/PlumbleActivity.java +++ b/app/src/main/java/com/morlunk/mumbleclient/app/PlumbleActivity.java @@ -123,6 +123,7 @@ public class PlumbleActivity extends ActionBarActivity implements ListView.OnIte @Override public void onServiceConnected(ComponentName name, IBinder service) { mService = (PlumbleService)((JumbleService.JumbleBinder) service).getService(); + mService.setSuppressNotifications(true); mService.registerObserver(mObserver); mService.clearChatNotifications(); // Clear chat notifications on resume. mDrawerAdapter.notifyDataSetChanged(); @@ -355,6 +356,7 @@ public class PlumbleActivity extends ActionBarActivity implements ListView.OnIte fragment.setServiceBound(false); } mService.unregisterObserver(mObserver); + mService.setSuppressNotifications(false); } unbindService(mConnection); } diff --git a/app/src/main/java/com/morlunk/mumbleclient/channel/ChannelChatFragment.java b/app/src/main/java/com/morlunk/mumbleclient/channel/ChannelChatFragment.java index 1011a04..0a0d613 100644 --- a/app/src/main/java/com/morlunk/mumbleclient/channel/ChannelChatFragment.java +++ b/app/src/main/java/com/morlunk/mumbleclient/channel/ChannelChatFragment.java @@ -43,6 +43,7 @@ import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; import com.morlunk.jumble.IJumbleService; +import com.morlunk.jumble.JumbleService; import com.morlunk.jumble.model.Channel; import com.morlunk.jumble.model.IChannel; import com.morlunk.jumble.model.IMessage; @@ -334,7 +335,8 @@ public class ChannelChatFragment extends JumbleServiceFragment implements ChatTa public void visit(IChatMessage.TextMessage message) { IMessage textMessage = message.getMessage(); String targetMessage = getContext().getString(R.string.unknown); - boolean selfAuthored = textMessage.getActor() == mService.getSession(); + boolean selfAuthored = mService.getConnectionState() == JumbleService.ConnectionState.CONNECTED && + textMessage.getActor() == mService.getSession(); if (textMessage.getTargetChannels() != null && !textMessage.getTargetChannels().isEmpty()) { IChannel currentChannel = (IChannel) textMessage.getTargetChannels().get(0); diff --git a/app/src/main/java/com/morlunk/mumbleclient/service/PlumbleConnectionNotification.java b/app/src/main/java/com/morlunk/mumbleclient/service/PlumbleConnectionNotification.java index 83c522c..9d02719 100644 --- a/app/src/main/java/com/morlunk/mumbleclient/service/PlumbleConnectionNotification.java +++ b/app/src/main/java/com/morlunk/mumbleclient/service/PlumbleConnectionNotification.java @@ -68,11 +68,9 @@ public class PlumbleConnectionNotification { * @param listener An listener for notification actions. * @return A new PlumbleNotification instance. */ - public static PlumbleConnectionNotification showForeground(Service service, String ticker, String contentText, - OnActionListener listener) { - PlumbleConnectionNotification notification = new PlumbleConnectionNotification(service, ticker, contentText, listener); - notification.show(); - return notification; + public static PlumbleConnectionNotification create(Service service, String ticker, String contentText, + OnActionListener listener) { + return new PlumbleConnectionNotification(service, ticker, contentText, listener); } private PlumbleConnectionNotification(Service service, String ticker, String contentText, 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 e892483..598127b 100644 --- a/app/src/main/java/com/morlunk/mumbleclient/service/PlumbleService.java +++ b/app/src/main/java/com/morlunk/mumbleclient/service/PlumbleService.java @@ -79,6 +79,7 @@ public class PlumbleService extends JumbleService implements */ private boolean mErrorShown; private List<IChatMessage> mMessageLog; + private boolean mSuppressNotifications; private TextToSpeech mTTS; private TextToSpeech.OnInitListener mTTSInitListener = new TextToSpeech.OnInitListener() { @@ -115,16 +116,20 @@ public class PlumbleService extends JumbleService implements mReconnectNotification = null; } - mNotification = PlumbleConnectionNotification.showForeground(PlumbleService.this, + mNotification = PlumbleConnectionNotification.create(PlumbleService.this, getString(R.string.plumbleConnecting), getString(R.string.connecting), PlumbleService.this); + if (!mSuppressNotifications) { + mNotification.show(); + } + mErrorShown = false; } @Override public void onConnected() { - if (mNotification != null) { + if (mNotification != null && !mSuppressNotifications) { mNotification.setCustomTicker(getString(R.string.plumbleConnected)); mNotification.setCustomContentText(getString(R.string.connected)); mNotification.setActionsShown(true); @@ -138,7 +143,7 @@ public class PlumbleService extends JumbleService implements mNotification.hide(); mNotification = null; } - if (e != null) { + if (e != null && !mSuppressNotifications) { mReconnectNotification = PlumbleReconnectNotification.show(PlumbleService.this, e.getMessage(), isReconnecting(), @@ -159,7 +164,7 @@ public class PlumbleService extends JumbleService implements public void onUserStateUpdated(IUser user) { if(user.getSession() == getSession()) { mSettings.setMutedAndDeafened(user.isSelfMuted(), user.isSelfDeafened()); // Update settings mute/deafen state - if(mNotification != null) { + if(mNotification != null && !mSuppressNotifications) { String contentText; if (user.isSelfMuted() && user.isSelfDeafened()) contentText = getString(R.string.status_notify_muted_and_deafened); @@ -240,8 +245,7 @@ public class PlumbleService extends JumbleService implements @Override public void onPermissionDenied(String reason) { - if(mSettings.isChatNotifyEnabled() && - mNotification != null) { + if(mNotification != null && !mSuppressNotifications) { mNotification.setCustomTicker(reason); mNotification.show(); } @@ -565,4 +569,19 @@ public class PlumbleService extends JumbleService implements public void clearMessageLog() { mMessageLog.clear(); } + + /** + * Sets whether or not notifications should be suppressed. + * + * It's typically a good idea to do this when the main activity is foreground, so that the user + * is not bombarded with redundant alerts. + * + * <b>Chat notifications are NOT suppressed.</b> They may be if a chat indicator is added in the + * activity itself. For now, the user may disable chat notifications manually. + * + * @param suppressNotifications true if Plumble is to disable notifications. + */ + public void setSuppressNotifications(boolean suppressNotifications) { + mSuppressNotifications = suppressNotifications; + } } |