GCM: Ensure we have a context for preferences

This commit is contained in:
Marvin W 2020-09-09 00:49:05 +02:00
parent f48298b1f6
commit 6ec8332b1e
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A
4 changed files with 9 additions and 9 deletions

View File

@ -235,18 +235,18 @@ public class McsService extends Service implements Handler.Callback {
return null;
}
public synchronized static boolean isConnected() {
public synchronized static boolean isConnected(Context context) {
if (inputStream == null || !inputStream.isAlive() || outputStream == null || !outputStream.isAlive()) {
logd(null, "Connection is not enabled or dead.");
return false;
}
// consider connection to be dead if we did not receive an ack within twice the heartbeat interval
int heartbeatMs = GcmPrefs.get(null).getHeartbeatMsFor(activeNetworkPref, false);
int heartbeatMs = GcmPrefs.get(context).getHeartbeatMsFor(activeNetworkPref, false);
if (heartbeatMs < 0) {
closeAll();
} else if (SystemClock.elapsedRealtime() - lastHeartbeatAckElapsedRealtime > 2 * heartbeatMs) {
logd(null, "No heartbeat for " + (SystemClock.elapsedRealtime() - lastHeartbeatAckElapsedRealtime) / 1000 + " seconds, connection assumed to be dead after " + 2 * heartbeatMs / 1000 + " seconds");
GcmPrefs.get(null).learnTimeout(activeNetworkPref);
GcmPrefs.get(context).learnTimeout(activeNetworkPref);
return false;
}
return true;
@ -649,13 +649,13 @@ public class McsService extends Service implements Handler.Callback {
return true;
case MSG_CONNECT:
logd(this, "Connect initiated, reason: " + msg.obj);
if (!isConnected()) {
if (!isConnected(this)) {
connect();
}
return true;
case MSG_HEARTBEAT:
logd(this, "Heartbeat initiated, reason: " + msg.obj);
if (isConnected()) {
if (isConnected(this)) {
HeartbeatPing.Builder ping = new HeartbeatPing.Builder();
if (inputStream.newStreamIdAvailable()) {
ping.last_stream_id_received(inputStream.getStreamId());
@ -669,7 +669,7 @@ public class McsService extends Service implements Handler.Callback {
return true;
case MSG_ACK:
logd(this, "Ack initiated, reason: " + msg.obj);
if (isConnected()) {
if (isConnected(this)) {
IqStanza.Builder iq = new IqStanza.Builder()
.type(IqStanza.IqType.SET)
.id("")

View File

@ -86,7 +86,7 @@ public class TriggerReceiver extends WakefulBroadcastReceiver {
}
}
if (!McsService.isConnected() || force) {
if (!McsService.isConnected(context) || force) {
Log.d(TAG, "Not connected to GCM but should be, asking the service to start up. Triggered by: " + intent);
startWakefulService(new ForegroundServiceContext(context), new Intent(ACTION_CONNECT, null, context, McsService.class)
.putExtra(EXTRA_REASON, intent));

View File

@ -52,7 +52,7 @@ public class GcmAdvancedFragment extends ResourceSettingsFragment {
updateContent();
if (newValue.equals("-1") && preference.getKey().equals(McsService.activeNetworkPref)) {
McsService.stop(getContext());
} else if (!McsService.isConnected()) {
} else if (!McsService.isConnected(getContext())) {
getContext().sendBroadcast(new Intent(TriggerReceiver.FORCE_TRY_RECONNECT, null, getContext(), TriggerReceiver.class));
}
return true;

View File

@ -38,7 +38,7 @@ class ServiceInfoReceiver : BroadcastReceiver() {
private fun sendInfoResponse(context: Context) {
context.sendOrderedBroadcast(Intent(ACTION_SERVICE_INFO_RESPONSE).apply {
setPackage(context.packageName)
putExtra(EXTRA_SERVICE_INFO, ServiceInfo(GcmPrefs.get(context).toConfiguration(), McsService.isConnected(), McsService.getStartTimestamp()))
putExtra(EXTRA_SERVICE_INFO, ServiceInfo(GcmPrefs.get(context).toConfiguration(), McsService.isConnected(context), McsService.getStartTimestamp()))
}, null)
}