aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/services/XmppConnectionService.java
diff options
context:
space:
mode:
authorDaniel Gultsch <daniel@gultsch.de>2014-03-06 03:30:03 +0100
committerDaniel Gultsch <daniel@gultsch.de>2014-03-06 03:30:03 +0100
commit2f0b2b865e4ec1b34d37120f085a9841ea11d1ed (patch)
tree409219c4699ff385d41ad3767d917a5243becce0 /src/eu/siacs/conversations/services/XmppConnectionService.java
parente4f61da07924e9440cde5b92c4364b1310c64ce5 (diff)
user alarmmanager to reschedule wakeups instead of just sleeping
Diffstat (limited to '')
-rw-r--r--src/eu/siacs/conversations/services/XmppConnectionService.java60
1 files changed, 57 insertions, 3 deletions
diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java
index e973afa9..8c7d64ce 100644
--- a/src/eu/siacs/conversations/services/XmppConnectionService.java
+++ b/src/eu/siacs/conversations/services/XmppConnectionService.java
@@ -5,6 +5,7 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
+import java.util.Random;
import org.json.JSONException;
import org.openintents.openpgp.util.OpenPgpApi;
@@ -42,7 +43,9 @@ import eu.siacs.conversations.xmpp.OnPresencePacketReceived;
import eu.siacs.conversations.xmpp.OnStatusChanged;
import eu.siacs.conversations.xmpp.PresencePacket;
import eu.siacs.conversations.xmpp.XmppConnection;
+import android.app.AlarmManager;
import android.app.NotificationManager;
+import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
@@ -53,6 +56,7 @@ import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager;
+import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.util.Log;
@@ -70,6 +74,8 @@ public class XmppConnectionService extends Service {
public OnConversationListChangedListener convChangedListener = null;
private OnAccountListChangedListener accountChangedListener = null;
+
+ private Random mRandom = new Random(System.currentTimeMillis());
private ContentObserver contactObserver = new ContentObserver(null) {
@Override
@@ -183,6 +189,14 @@ public class XmppConnectionService extends Service {
//
}
}
+ } else if (account.getStatus() == Account.STATUS_OFFLINE) {
+ Log.d(LOGTAG,"onStatusChanged offline");
+ databaseBackend.clearPresences(account);
+ if (!account.isOptionSet(Account.OPTION_DISABLED)) {
+ int timeToReconnect = mRandom.nextInt(50)+10;
+ scheduleWakeupCall(timeToReconnect);
+ }
+
}
}
};
@@ -364,10 +378,39 @@ public class XmppConnectionService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
+ boolean internet;
+ if ((intent!=null)&&(intent.hasExtra("has_internet"))) {
+ if (intent.getExtras().getBoolean("has_internet",true)) {
+ internet = true;
+ } else {
+ internet = false;
+ }
+ } else {
+ internet = true;
+ }
for (Account account : accounts) {
+ if (!internet) {
+ account.setStatus(Account.STATUS_NO_INTERNET);
+ break;
+ } else {
+ if (account.getStatus() == Account.STATUS_NO_INTERNET) {
+ account.setStatus(Account.STATUS_OFFLINE);
+ }
+ }
if (account.getXmppConnection() == null) {
- if (!account.isOptionSet(Account.OPTION_DISABLED)) {
+ if ((!account.isOptionSet(Account.OPTION_DISABLED))&&(internet)) {
account.setXmppConnection(this.createConnection(account));
+ Thread thread = new Thread(account.getXmppConnection());
+ thread.start();
+ }
+ } else {
+ if ((!account.isOptionSet(Account.OPTION_DISABLED))&&(internet)) {
+ if (account.getStatus()==Account.STATUS_OFFLINE) {
+ Thread thread = new Thread(account.getXmppConnection());
+ thread.start();
+ }
+ } else {
+ disconnect(account);
}
}
}
@@ -384,6 +427,8 @@ public class XmppConnectionService extends Service {
this.pgpServiceConnection = new OpenPgpServiceConnection(
getApplicationContext(), "org.sufficientlysecure.keychain");
this.pgpServiceConnection.bindToService();
+
+
}
@Override
@@ -395,6 +440,17 @@ public class XmppConnectionService extends Service {
}
}
}
+
+ protected void scheduleWakeupCall(int seconds) {
+ Context context = getApplicationContext();
+ AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
+ Intent intent = new Intent(context, EventReceiver.class);
+ PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
+ alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
+ SystemClock.elapsedRealtime() +
+ seconds * 1000, alarmIntent);
+ Log.d(LOGTAG,"wake up call scheduled in "+seconds+" seconds");
+ }
public XmppConnection createConnection(Account account) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
@@ -404,8 +460,6 @@ public class XmppConnectionService extends Service {
connection.setOnPresencePacketReceivedListener(this.presenceListener);
connection
.setOnUnregisteredIqPacketReceivedListener(this.unknownIqListener);
- Thread thread = new Thread(connection);
- thread.start();
return connection;
}