Skip to content

Commit

Permalink
Force inactive state if service not held (fix #787)
Browse files Browse the repository at this point in the history
This fix will make the message center force inactivate state on login
if the message center reference counter is zero.

Signed-off-by: Daniele Ricci <[email protected]>
  • Loading branch information
daniele-athome committed Nov 18, 2016
1 parent 8e83026 commit e5e262b
Showing 1 changed file with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public class MessageCenterService extends Service implements ConnectionHelperLis
/** Server push sender id. This is static so the {@link IPushListener} can see it. */
static String sPushSenderId;
/** Push registration id. */
private String mPushRegistrationId;
String mPushRegistrationId;
/** Flag marking a currently ongoing push registration cycle (unregister/register) */
boolean mPushRegistrationCycle;

Expand All @@ -358,7 +358,7 @@ public class MessageCenterService extends Service implements ConnectionHelperLis
/** Cached last used server. */
EndpointServer mServer;
/** The connection helper instance. */
private XMPPConnectionHelper mHelper;
XMPPConnectionHelper mHelper;
/** The connection instance. */
KontalkConnection mConnection;
/** My username (account name). */
Expand Down Expand Up @@ -404,9 +404,9 @@ static final class IdleConnectionHandler extends Handler implements IdleHandler
private final static int INACTIVE_TIME = 30000;

/** A reference to the message center. */
private WeakReference<MessageCenterService> s;
WeakReference<MessageCenterService> s;
/** Reference counter. */
private int mRefCount;
int mRefCount;

public IdleConnectionHandler(MessageCenterService service, int refCount, Looper looper) {
super(looper);
Expand Down Expand Up @@ -547,7 +547,7 @@ public void quit() {
}

/** Aborts any idle message because we are using the service or quitting. */
private void abortIdle() {
void abortIdle() {
Looper.myQueue().removeIdleHandler(IdleConnectionHandler.this);
removeMessages(MSG_IDLE);
removeMessages(MSG_INACTIVE);
Expand All @@ -556,11 +556,12 @@ private void abortIdle() {
service.cancelIdleAlarm();
}

public void queueInactiveIfNeeded() {
public void forceInactiveIfNeeded() {
post(new Runnable() {
public void run() {
if (mRefCount <= 0 && !hasMessages(MSG_INACTIVE)) {
queueInactive();
MessageCenterService service = s.get();
if (service != null && mRefCount <= 0 && !service.isInactive()) {
forceInactive();
}
}
});
Expand All @@ -574,7 +575,7 @@ public void forceInactive() {
}
}

private void queueInactive() {
void queueInactive() {
// send inactive state message only if connected
MessageCenterService service = s.get();
if (service != null && service.isConnected()) {
Expand Down Expand Up @@ -1523,8 +1524,8 @@ public void authenticated(XMPPConnection connection, boolean resumed) {
// we can now release any pending push notification
Preferences.setLastPushNotification(this, -1);

// queue inactive message if needed
mIdleHandler.queueInactiveIfNeeded();
// force inactive state if needed
mIdleHandler.forceInactiveIfNeeded();

// update alarm manager
AndroidAdaptiveServerPingManager
Expand Down Expand Up @@ -1582,7 +1583,7 @@ private void discovery() {
sendPacket(items);
}

private synchronized void active(boolean available) {
synchronized void active(boolean available) {
final XMPPConnection connection = mConnection;
if (connection != null) {
cancelIdleAlarm();
Expand All @@ -1606,7 +1607,7 @@ private synchronized void active(boolean available) {
}
}

private synchronized void inactive() {
synchronized void inactive() {
final XMPPConnection connection = mConnection;
if (connection != null) {
if (!mInactive) {
Expand All @@ -1628,11 +1629,11 @@ private synchronized void inactive() {
}
}

private boolean isInactive() {
boolean isInactive() {
return mInactive;
}

private boolean fastReply() {
boolean fastReply() {
if (!isConnected()) return false;

try {
Expand All @@ -1644,7 +1645,7 @@ private boolean fastReply() {
}
}

private long getLastReceivedStanza() {
long getLastReceivedStanza() {
return mConnection != null ? mConnection.getLastStanzaReceived() : 0;
}

Expand Down Expand Up @@ -3101,7 +3102,7 @@ public static String getPushSenderId() {
return sPushSenderId;
}

private void setWakeupAlarm() {
void setWakeupAlarm() {
long delay = Preferences.getWakeupTimeMillis(this,
MIN_WAKEUP_TIME);

Expand All @@ -3127,7 +3128,7 @@ private void ensureIdleAlarm() {
}
}

private void cancelIdleAlarm() {
void cancelIdleAlarm() {
ensureIdleAlarm();
mAlarmManager.cancel(mIdleIntent);
}
Expand Down

0 comments on commit e5e262b

Please sign in to comment.