added idle ping in 10min intervals

This commit is contained in:
Daniel Gultsch 2016-05-28 14:44:22 +02:00
parent ae7543bbfc
commit bc36f1950f
2 changed files with 24 additions and 8 deletions

View File

@ -50,6 +50,7 @@ public final class Config {
public static final boolean SHOW_DISABLE_FOREGROUND = true; //if set to true the foreground notification has a button to disable it public static final boolean SHOW_DISABLE_FOREGROUND = true; //if set to true the foreground notification has a button to disable it
public static final int PING_MAX_INTERVAL = 300; public static final int PING_MAX_INTERVAL = 300;
public static final int IDLE_PING_INTERVAL = 600; //540 is minimum according to docs;
public static final int PING_MIN_INTERVAL = 30; public static final int PING_MIN_INTERVAL = 30;
public static final int PING_TIMEOUT = 15; public static final int PING_TIMEOUT = 15;
public static final int SOCKET_TIMEOUT = 15; public static final int SOCKET_TIMEOUT = 15;

View File

@ -1,6 +1,7 @@
package eu.siacs.conversations.services; package eu.siacs.conversations.services;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
@ -26,7 +27,6 @@ import android.os.SystemClock;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.security.KeyChain; import android.security.KeyChain;
import android.util.Base64;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.util.LruCache; import android.util.LruCache;
@ -129,6 +129,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
public static final String ACTION_DISABLE_FOREGROUND = "disable_foreground"; public static final String ACTION_DISABLE_FOREGROUND = "disable_foreground";
public static final String ACTION_TRY_AGAIN = "try_again"; public static final String ACTION_TRY_AGAIN = "try_again";
public static final String ACTION_DISABLE_ACCOUNT = "disable_account"; public static final String ACTION_DISABLE_ACCOUNT = "disable_account";
public static final String ACTION_IDLE_PING = "idle_ping";
private static final String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts"; private static final String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts";
public static final String ACTION_GCM_TOKEN_REFRESH = "gcm_token_refresh"; public static final String ACTION_GCM_TOKEN_REFRESH = "gcm_token_refresh";
public static final String ACTION_GCM_MESSAGE_RECEIVED = "gcm_message_received"; public static final String ACTION_GCM_MESSAGE_RECEIVED = "gcm_message_received";
@ -552,8 +553,14 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
case ACTION_GCM_TOKEN_REFRESH: case ACTION_GCM_TOKEN_REFRESH:
refreshAllGcmTokens(); refreshAllGcmTokens();
break; break;
case ACTION_IDLE_PING:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
scheduleNextIdlePing();
}
break;
case ACTION_GCM_MESSAGE_RECEIVED: case ACTION_GCM_MESSAGE_RECEIVED:
Log.d(Config.LOGTAG,"gcm push message arrived in service. extras="+intent.getExtras()); Log.d(Config.LOGTAG,"gcm push message arrived in service. extras="+intent.getExtras());
break;
} }
} }
this.wakeLock.acquire(); this.wakeLock.acquire();
@ -628,7 +635,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
if (pingNow) { if (pingNow) {
for (Account account : pingCandidates) { for (Account account : pingCandidates) {
account.getXmppConnection().sendPing(); account.getXmppConnection().sendPing();
Log.d(Config.LOGTAG, account.getJid().toBareJid() + " send ping"); Log.d(Config.LOGTAG, account.getJid().toBareJid() + " send ping (action="+action+")");
this.scheduleWakeUpCall(Config.PING_TIMEOUT, account.getUuid().hashCode()); this.scheduleWakeUpCall(Config.PING_TIMEOUT, account.getUuid().hashCode());
} }
} }
@ -842,16 +849,24 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
public void scheduleWakeUpCall(int seconds, int requestCode) { public void scheduleWakeUpCall(int seconds, int requestCode) {
final long timeToWake = SystemClock.elapsedRealtime() + (seconds < 0 ? 1 : seconds + 1) * 1000; final long timeToWake = SystemClock.elapsedRealtime() + (seconds < 0 ? 1 : seconds + 1) * 1000;
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Context context = getApplicationContext(); Intent intent = new Intent(this, EventReceiver.class);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, EventReceiver.class);
intent.setAction("ping"); intent.setAction("ping");
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, requestCode, intent, 0); PendingIntent alarmIntent = PendingIntent.getBroadcast(this, requestCode, intent, 0);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeToWake, alarmIntent); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeToWake, alarmIntent);
} }
@TargetApi(Build.VERSION_CODES.M)
private void scheduleNextIdlePing() {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, EventReceiver.class);
intent.setAction(ACTION_IDLE_PING);
alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime()+(Config.IDLE_PING_INTERVAL * 1000),
PendingIntent.getBroadcast(this,0,intent,0)
);
}
public XmppConnection createConnection(final Account account) { public XmppConnection createConnection(final Account account) {
final SharedPreferences sharedPref = getPreferences(); final SharedPreferences sharedPref = getPreferences();
account.setResource(sharedPref.getString("resource", getString(R.string.default_resource)) account.setResource(sharedPref.getString("resource", getString(R.string.default_resource))