Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Notifications: EventStreamService: Kill a bool (mIsForeground) by usi…
Browse files Browse the repository at this point in the history
…ng the existing machine state

#2130
  • Loading branch information
manuroe committed Apr 11, 2018
1 parent 896a901 commit 51bb4d2
Showing 1 changed file with 72 additions and 61 deletions.
133 changes: 72 additions & 61 deletions vector/src/main/java/im/vector/services/EventStreamService.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,6 @@ public enum ForegroundNotificationState {
*/
private String mIncomingCallId = null;

/**
* true when the service is in foreground ie the GCM registration failed or is disabled.
*/
// @TODO: still required with ForegroundNotificationState?
private boolean mIsForeground = false;

/**
* GCM manager
*/
Expand Down Expand Up @@ -293,7 +287,7 @@ public void run() {
// it seems there are some race conditions with onInitialSyncComplete
if (mForegroundNotificationState == ForegroundNotificationState.INITIAL_SYNCING) {
Log.d(LOG_TAG, "onLiveEventsChunkProcessed : end of init sync");
refreshStatusNotification();
refreshForegroundNotification();
}
}
};
Expand Down Expand Up @@ -509,8 +503,7 @@ public void onDestroy() {
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) &&
(mForegroundNotificationState == ForegroundNotificationState.INITIAL_SYNCING)
&& Matrix.getInstance(getApplicationContext()).getSharedGCMRegistrationManager().hasRegistrationToken()) {
stopForeground(true);
mIsForeground = false;
setForegroundNotificationState(ForegroundNotificationState.NONE, null);
}

Log.d(LOG_TAG, "## onDestroy() : restart it");
Expand Down Expand Up @@ -591,7 +584,7 @@ public void run() {
(new Handler(getMainLooper())).post(new Runnable() {
@Override
public void run() {
refreshStatusNotification();
refreshForegroundNotification();
}
});
}
Expand Down Expand Up @@ -702,7 +695,7 @@ private void start() {
monitorSession(session);
}

refreshStatusNotification();
refreshForegroundNotification();

setServiceState(StreamAction.START);
}
Expand All @@ -722,9 +715,8 @@ public void stopNow() {
private void stop() {
Log.d(LOG_TAG, "## stop(): the service is stopped");

if (mIsForeground) {
stopForeground(true);
}
// stop the foreground service, if any
setForegroundNotificationState(ForegroundNotificationState.NONE, null);

if (mSessions != null) {
for (MXSession session : mSessions) {
Expand Down Expand Up @@ -816,23 +808,25 @@ private void resume() {
setServiceState(StreamAction.START);
}

//================================================================================
// notification management
//================================================================================

/**
* The GCM status has been updated (i.e disabled or enabled).
*/
private void gcmStatusUpdate() {
Log.d(LOG_TAG, "## gcmStatusUpdate");

if (mIsForeground) {
Log.d(LOG_TAG, "## gcmStatusUpdate : gcm status succeeds so stopForeground (" + mForegroundNotificationState + ")");
if (ForegroundNotificationState.NONE != mForegroundNotificationState) {
Log.d(LOG_TAG, "## gcmStatusUpdate : gcm status succeeds. So, stop foreground service (" + mForegroundNotificationState + ")");

if (ForegroundNotificationState.LISTENING_FOR_EVENTS == mForegroundNotificationState) {
stopForeground(true);
mForegroundNotificationState = ForegroundNotificationState.NONE;
mIsForeground = false;
setForegroundNotificationState(ForegroundNotificationState.NONE, null);
}
}

refreshStatusNotification();
refreshForegroundNotification();
}

/**
Expand All @@ -849,8 +843,8 @@ private boolean shouldDisplayListenForEventsNotification() {
* Manages the sticky foreground notification.
* It display the background state of the app ("Listen for events", "synchronising", ...)
*/
public void refreshStatusNotification() {
Log.d(LOG_TAG, "## refreshStatusNotification from state " + mForegroundNotificationState);
public void refreshForegroundNotification() {
Log.d(LOG_TAG, "## refreshForegroundNotification from state " + mForegroundNotificationState);

MXSession session = Matrix.getInstance(getApplicationContext()).getDefaultSession();

Expand All @@ -861,7 +855,7 @@ public void refreshStatusNotification() {

// call in progress notifications
if ((mForegroundNotificationState == ForegroundNotificationState.INCOMING_CALL) || (mForegroundNotificationState == ForegroundNotificationState.CALL_IN_PROGRESS)) {
Log.d(LOG_TAG, "## refreshStatusNotification : does nothing as there is a pending call");
Log.d(LOG_TAG, "## refreshForegroundNotification : does nothing as there is a pending call");
return;
}

Expand All @@ -873,37 +867,59 @@ public void refreshStatusNotification() {
boolean isInitialSyncInProgress = !session.getDataHandler().isInitialSyncComplete() || isStopped() || (mServiceState == StreamAction.CATCHUP);

if (isInitialSyncInProgress) {
Log.d(LOG_TAG, "## refreshStatusNotification : put the service in foreground because of an initial sync " + mForegroundNotificationState);

if (mForegroundNotificationState != ForegroundNotificationState.INITIAL_SYNCING) {
startForeground(NOTIF_ID_FOREGROUND_SERVICE, buildForegroundServiceNotification(getString(R.string.notification_sync_in_progress)));
mForegroundNotificationState = ForegroundNotificationState.INITIAL_SYNCING;
}
mIsForeground = true;
Log.d(LOG_TAG, "## refreshForegroundNotification : put the service in foreground because of an initial sync " + mForegroundNotificationState);
setForegroundNotificationState(ForegroundNotificationState.INITIAL_SYNCING, null);
} else if (shouldDisplayListenForEventsNotification()) {
Log.d(LOG_TAG, "## refreshStatusNotification : put the service in foreground because of GCM registration");
Log.d(LOG_TAG, "## refreshForegroundNotification : put the service in foreground because of GCM registration");
setForegroundNotificationState(ForegroundNotificationState.LISTENING_FOR_EVENTS, null);
} else {
Log.d(LOG_TAG, "## refreshForegroundNotification : put the service in background from state " + mForegroundNotificationState);
setForegroundNotificationState(ForegroundNotificationState.NONE, null);
}
}

if (mForegroundNotificationState != ForegroundNotificationState.LISTENING_FOR_EVENTS) {
startForeground(NOTIF_ID_FOREGROUND_SERVICE, buildForegroundServiceNotification(getString(R.string.notification_listen_for_events)));
mForegroundNotificationState = ForegroundNotificationState.LISTENING_FOR_EVENTS;
}
/**
* Set the new foreground notification state.
* And display the foreground service notification (`NOTIF_ID_FOREGROUND_SERVICE`) if required.
*
* @param foregroundNotificationState the new state
* @param notification an already built notification. Required for `INCOMING_CALL` and `CALL_IN_PROGRESS`
*/
private void setForegroundNotificationState(ForegroundNotificationState foregroundNotificationState, Notification notification) {

mIsForeground = true;
} else {
if ((mForegroundNotificationState == ForegroundNotificationState.LISTENING_FOR_EVENTS) || (mForegroundNotificationState == ForegroundNotificationState.INITIAL_SYNCING)) {
Log.d(LOG_TAG, "## refreshStatusNotification : put the service in background from state " + mForegroundNotificationState);
if (foregroundNotificationState == mForegroundNotificationState) {
return;
}

NotificationManager nm = (NotificationManager) EventStreamService.this.getSystemService(Context.NOTIFICATION_SERVICE);

mForegroundNotificationState = foregroundNotificationState;
switch (mForegroundNotificationState) {
case NONE:
// The foreground/ sticky notification can be removed
nm.cancel(NOTIF_ID_FOREGROUND_SERVICE);
stopForeground(true);
mForegroundNotificationState = ForegroundNotificationState.NONE;
} else {
Log.d(LOG_TAG, "## refreshStatusNotification : nothing to do");
}
mIsForeground = false;
break;
case INITIAL_SYNCING:
notification = buildForegroundServiceNotification(getString(R.string.notification_sync_in_progress));
break;
case LISTENING_FOR_EVENTS:
notification = buildForegroundServiceNotification(getString(R.string.notification_listen_for_events));
break;
case INCOMING_CALL:
case CALL_IN_PROGRESS:
// A prebuilt notification must be passed for changing to these states
if (notification == null) {
throw new IllegalArgumentException("A notification object must be passed for state " + foregroundNotificationState);
}
break;
}
}

//================================================================================
// notification management
//================================================================================
if (notification != null) {
// display the stick foreground notification
startForeground(NOTIF_ID_FOREGROUND_SERVICE, notification);
}
}

/**
* @return the polling thread listener notification
Expand Down Expand Up @@ -1628,11 +1644,7 @@ else if (null == CallsManager.getSharedInstance().getActiveCall()) {
RoomsNotifications.getRoomName(getApplicationContext(), session, room, event),
session.getMyUserId(),
callId);


startForeground(NOTIF_ID_FOREGROUND_SERVICE, notification);
mForegroundNotificationState = ForegroundNotificationState.INCOMING_CALL;
mIsForeground = true;
setForegroundNotificationState(ForegroundNotificationState.INCOMING_CALL, notification);

mIncomingCallId = callId;

Expand All @@ -1657,8 +1669,7 @@ else if (null == CallsManager.getSharedInstance().getActiveCall()) {
public void displayCallInProgressNotification(MXSession session, Room room, String callId) {
if (null != callId) {
Notification notification = NotificationUtils.buildPendingCallNotification(getApplicationContext(), room.getName(session.getCredentials().userId), room.getRoomId(), session.getCredentials().userId, callId);
startForeground(NOTIF_ID_FOREGROUND_SERVICE, notification);
mForegroundNotificationState = ForegroundNotificationState.CALL_IN_PROGRESS;
setForegroundNotificationState(ForegroundNotificationState.CALL_IN_PROGRESS, notification);
mCallIdInProgress = callId;
}
}
Expand All @@ -1669,18 +1680,18 @@ public void displayCallInProgressNotification(MXSession session, Room room, Stri
public void hideCallNotifications() {
NotificationManager nm = (NotificationManager) EventStreamService.this.getSystemService(Context.NOTIFICATION_SERVICE);

// hide the call
if ((ForegroundNotificationState.CALL_IN_PROGRESS == mForegroundNotificationState) || (ForegroundNotificationState.INCOMING_CALL == mForegroundNotificationState)) {
// hide the foreground notification for calls only if we are in these states
if ((ForegroundNotificationState.CALL_IN_PROGRESS == mForegroundNotificationState)
|| (ForegroundNotificationState.INCOMING_CALL == mForegroundNotificationState)) {
if (ForegroundNotificationState.CALL_IN_PROGRESS == mForegroundNotificationState) {
mCallIdInProgress = null;
} else {
mIncomingCallId = null;
}
nm.cancel(NOTIF_ID_FOREGROUND_SERVICE);
stopForeground(true);

mForegroundNotificationState = ForegroundNotificationState.NONE;
refreshStatusNotification();
setForegroundNotificationState(ForegroundNotificationState.NONE, null);
refreshForegroundNotification();
}
}

}

0 comments on commit 51bb4d2

Please sign in to comment.