diff --git a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java index 340cb61371..42234e5146 100644 --- a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java +++ b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java @@ -62,7 +62,7 @@ import org.thoughtcrime.securesms.service.MessageRetrievalService; import org.thoughtcrime.securesms.util.ServiceUtil; import org.thoughtcrime.securesms.util.SpanUtil; import org.thoughtcrime.securesms.util.TextSecurePreferences; -import org.thoughtcrime.securesms.webrtc.CallNotificationManager; +import org.thoughtcrime.securesms.webrtc.CallNotificationBuilder; import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope; import java.util.HashSet; @@ -159,7 +159,7 @@ public class MessageNotifier { if (notification.getId() != SUMMARY_NOTIFICATION_ID && notification.getId() != NotificationBarManager.RED_PHONE_NOTIFICATION && - notification.getId() != CallNotificationManager.WEBRTC_NOTIFICATION && + notification.getId() != CallNotificationBuilder.WEBRTC_NOTIFICATION && notification.getId() != KeyCachingService.SERVICE_RUNNING_ID && notification.getId() != MessageRetrievalService.FOREGROUND_ID) { diff --git a/src/org/thoughtcrime/securesms/service/WebRtcCallService.java b/src/org/thoughtcrime/securesms/service/WebRtcCallService.java index 22efe157f9..e460bd353a 100644 --- a/src/org/thoughtcrime/securesms/service/WebRtcCallService.java +++ b/src/org/thoughtcrime/securesms/service/WebRtcCallService.java @@ -44,7 +44,7 @@ import org.thoughtcrime.securesms.util.ListenableFutureTask; import org.thoughtcrime.securesms.util.ServiceUtil; import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.Util; -import org.thoughtcrime.securesms.webrtc.CallNotificationManager; +import org.thoughtcrime.securesms.webrtc.CallNotificationBuilder; import org.thoughtcrime.securesms.webrtc.PeerConnectionFactoryOptions; import org.thoughtcrime.securesms.webrtc.PeerConnectionWrapper; import org.thoughtcrime.securesms.webrtc.PeerConnectionWrapper.PeerConnectionException; @@ -97,9 +97,9 @@ import java.util.concurrent.TimeUnit; import javax.inject.Inject; -import static org.thoughtcrime.securesms.webrtc.CallNotificationManager.TYPE_ESTABLISHED; -import static org.thoughtcrime.securesms.webrtc.CallNotificationManager.TYPE_INCOMING_RINGING; -import static org.thoughtcrime.securesms.webrtc.CallNotificationManager.TYPE_OUTGOING_RINGING; +import static org.thoughtcrime.securesms.webrtc.CallNotificationBuilder.TYPE_ESTABLISHED; +import static org.thoughtcrime.securesms.webrtc.CallNotificationBuilder.TYPE_INCOMING_RINGING; +import static org.thoughtcrime.securesms.webrtc.CallNotificationBuilder.TYPE_OUTGOING_RINGING; public class WebRtcCallService extends Service implements InjectableType, PeerConnection.Observer, DataChannel.Observer { @@ -326,7 +326,7 @@ public class WebRtcCallService extends Service implements InjectableType, PeerCo lockManager.updatePhoneState(LockManager.PhoneState.IN_CALL); outgoingRinger.playSonar(); - CallNotificationManager.setCallInProgress(this, TYPE_OUTGOING_RINGING, recipient); + setCallInProgressNotification(TYPE_OUTGOING_RINGING, recipient); DatabaseFactory.getSmsDatabase(this).insertOutgoingCall(recipient.getNumber()); timeoutExecutor.schedule(new TimeoutRunnable(this.callId), 2, TimeUnit.MINUTES); @@ -461,7 +461,7 @@ public class WebRtcCallService extends Service implements InjectableType, PeerCo sendMessage(WebRtcViewModel.State.CALL_INCOMING, recipient, localVideoEnabled, remoteVideoEnabled); startCallCardActivity(); incomingRinger.start(); - CallNotificationManager.setCallInProgress(this, TYPE_INCOMING_RINGING, recipient); + setCallInProgressNotification(TYPE_INCOMING_RINGING, recipient); } else if (callState == CallState.STATE_DIALING) { if (this.recipient == null) throw new AssertionError("assert"); @@ -497,7 +497,7 @@ public class WebRtcCallService extends Service implements InjectableType, PeerCo sendMessage(WebRtcViewModel.State.CALL_CONNECTED, recipient, localVideoEnabled, remoteVideoEnabled); - CallNotificationManager.setCallInProgress(this, TYPE_ESTABLISHED, recipient); + setCallInProgressNotification(TYPE_ESTABLISHED, recipient); this.peerConnection.setAudioEnabled(audioEnabled); this.peerConnection.setVideoEnabled(localVideoEnabled); @@ -731,6 +731,11 @@ public class WebRtcCallService extends Service implements InjectableType, PeerCo }); } + private void setCallInProgressNotification(int type, Recipient recipient) { + startForeground(CallNotificationBuilder.WEBRTC_NOTIFICATION, + CallNotificationBuilder.getCallInProgressNotification(this, type, recipient)); + } + private void shutdownAudio() { Log.d(TAG, "reset audio mode and abandon focus"); AudioUtils.resetConfiguration(this); @@ -742,7 +747,7 @@ public class WebRtcCallService extends Service implements InjectableType, PeerCo private synchronized void terminate() { lockManager.updatePhoneState(LockManager.PhoneState.PROCESSING); - CallNotificationManager.setCallEnded(this); + stopForeground(true); incomingRinger.stop(); outgoingRinger.stop(); diff --git a/src/org/thoughtcrime/securesms/webrtc/CallNotificationManager.java b/src/org/thoughtcrime/securesms/webrtc/CallNotificationBuilder.java similarity index 87% rename from src/org/thoughtcrime/securesms/webrtc/CallNotificationManager.java rename to src/org/thoughtcrime/securesms/webrtc/CallNotificationBuilder.java index cd5eb21837..d9ac52eaed 100644 --- a/src/org/thoughtcrime/securesms/webrtc/CallNotificationManager.java +++ b/src/org/thoughtcrime/securesms/webrtc/CallNotificationBuilder.java @@ -1,7 +1,9 @@ package org.thoughtcrime.securesms.webrtc; +import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; +import android.app.Service; import android.content.Context; import android.content.Intent; import android.support.annotation.DrawableRes; @@ -22,7 +24,7 @@ import org.thoughtcrime.securesms.util.ServiceUtil; * */ -public class CallNotificationManager { +public class CallNotificationBuilder { public static final int WEBRTC_NOTIFICATION = 313388; @@ -30,14 +32,7 @@ public class CallNotificationManager { public static final int TYPE_OUTGOING_RINGING = 2; public static final int TYPE_ESTABLISHED = 3; - public static void setCallEnded(Context context) { - NotificationManager notificationManager = ServiceUtil.getNotificationManager(context); - notificationManager.cancel(WEBRTC_NOTIFICATION); - } - - public static void setCallInProgress(Context context, int type, Recipient recipient) { - NotificationManager notificationManager = ServiceUtil.getNotificationManager(context); - + public static Notification getCallInProgressNotification(Context context, int type, Recipient recipient) { Intent contentIntent = new Intent(context, WebRtcCallActivity.class); contentIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); @@ -61,7 +56,7 @@ public class CallNotificationManager { builder.addAction(getServiceNotificationAction(context, WebRtcCallService.ACTION_LOCAL_HANGUP, R.drawable.ic_call_end_grey600_32dp, R.string.NotificationBarManager__end_call)); } - notificationManager.notify(WEBRTC_NOTIFICATION, builder.build()); + return builder.build(); } private static NotificationCompat.Action getServiceNotificationAction(Context context, String action, int iconResId, int titleResId) {