diff options
Diffstat (limited to 'src/eu/siacs/conversations/services')
-rw-r--r-- | src/eu/siacs/conversations/services/EventReceiver.java | 26 | ||||
-rw-r--r-- | src/eu/siacs/conversations/services/XmppConnectionService.java | 60 |
2 files changed, 76 insertions, 10 deletions
diff --git a/src/eu/siacs/conversations/services/EventReceiver.java b/src/eu/siacs/conversations/services/EventReceiver.java index 41e31114..66208bca 100644 --- a/src/eu/siacs/conversations/services/EventReceiver.java +++ b/src/eu/siacs/conversations/services/EventReceiver.java @@ -3,15 +3,27 @@ package eu.siacs.conversations.services; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; public class EventReceiver extends BroadcastReceiver { @Override - public void onReceive(Context context, Intent intent) { - Intent mIntentForService = new Intent(context, XmppConnectionService.class); - if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { - - } + public void onReceive(Context context, Intent intent) { + Intent mIntentForService = new Intent(context, + XmppConnectionService.class); + if ((intent.getAction() != null) + && (intent.getAction() + .equals("android.intent.action.BOOT_COMPLETED"))) { + + } + ConnectivityManager cm = (ConnectivityManager) context + .getSystemService(Context.CONNECTIVITY_SERVICE); + + NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); + boolean isConnected = activeNetwork != null + && activeNetwork.isConnected(); + mIntentForService.putExtra("has_internet", isConnected); context.startService(mIntentForService); - } - + } + } 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; } |