Skip to content

Commit

Permalink
Adaptive ping timeouts per connection type (fix #383)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniele Ricci <[email protected]>
  • Loading branch information
daniele-athome committed Apr 11, 2015
1 parent 0baea7e commit 6169fd8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.kontalk.service;

import org.kontalk.Kontalk;
import org.kontalk.service.msgcenter.AdaptiveServerPingManager;
import org.kontalk.service.msgcenter.MessageCenterService;
import org.kontalk.util.Preferences;

Expand Down Expand Up @@ -60,7 +61,10 @@ public void onReceive(Context context, Intent intent) {
}
else {
Log.w(TAG, "background data enabled!");
// start message center
serviceAction = ACTION_START;
// notify ping manager that connection type has changed
AdaptiveServerPingManager.onConnected();
}
}

Expand All @@ -82,6 +86,8 @@ else if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
case CONNECTED:
// test connection or reconnect
serviceAction = ACTION_TEST;
// notify ping manager that connection type has changed
AdaptiveServerPingManager.onConnected();
break;
case SUSPENDED:
Log.v(TAG, "suspending network traffic");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ public static void onCreate(Context context) {
context.registerReceiver(ALARM_BROADCAST_RECEIVER, new IntentFilter(PING_ALARM_ACTION));
sAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
sPendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(PING_ALARM_ACTION), 0);
onConnected();
}

public static void onConnected() {
// setup first alarm using last value from preference
setupAlarmManager(Preferences.getPingAlarmInterval(sContext, AlarmManager.INTERVAL_HALF_HOUR));
}
Expand All @@ -190,8 +194,6 @@ private static void setupAlarmManager(long intervalMillis) {
if (sPendingIntent != null && sIntervalMillis != intervalMillis) {
sAlarmManager.cancel(sPendingIntent);
sIntervalMillis = intervalMillis;
// save value to preference for later retrieval
Preferences.setPingAlarmInterval(sIntervalMillis);

// do not go beyond 30 minutes...
if (sIntervalMillis > AlarmManager.INTERVAL_HALF_HOUR) {
Expand All @@ -202,6 +204,9 @@ else if (sIntervalMillis < MIN_ALARM_INTERVAL) {
sIntervalMillis = MIN_ALARM_INTERVAL;
}

// save value to preference for later retrieval
Preferences.setPingAlarmInterval(sContext, sIntervalMillis);

LOGGER.log(Level.WARNING, "Setting alarm for next ping to " + sIntervalMillis + " ms");
sAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + sIntervalMillis,
Expand Down
12 changes: 8 additions & 4 deletions app/src/main/java/org/kontalk/util/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -468,12 +468,16 @@ public static boolean setRosterVersion(String version) {
}

public static long getPingAlarmInterval(Context context, long defaultValue) {
return getLong(context, "ping_alarm_interval", defaultValue);
String networkType = SystemUtils.getCurrentNetwork(context);
return (networkType != null) ?
getLong(context, "ping_alarm_interval_" + networkType, defaultValue) :
defaultValue;
}

public static boolean setPingAlarmInterval(long intervalMillis) {
return sPreferences.edit()
.putLong("ping_alarm_interval", intervalMillis)
public static boolean setPingAlarmInterval(Context context, long intervalMillis) {
String networkType = SystemUtils.getCurrentNetwork(context);
return networkType != null && sPreferences.edit()
.putLong("ping_alarm_interval_" + networkType, intervalMillis)
.commit();
}

Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/org/kontalk/util/SystemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.Display;
import android.view.WindowManager;

Expand Down Expand Up @@ -79,4 +81,13 @@ public static Point getDisplaySize(Context context) {
return displaySize;
}

/** Returns the type name of the current network, or null. */
public static String getCurrentNetwork(Context context) {
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo info = connMgr.getActiveNetworkInfo();
return info != null ? info.getTypeName() : null;
}

}

0 comments on commit 6169fd8

Please sign in to comment.