user alarmmanager to reschedule wakeups instead of just sleeping

This commit is contained in:
Daniel Gultsch 2014-03-06 03:30:03 +01:00
parent e4f61da079
commit 2f0b2b865e
6 changed files with 169 additions and 113 deletions

View File

@ -32,11 +32,12 @@ public class Account extends AbstractEntity{
public static final int OPTION_USETLS = 0; public static final int OPTION_USETLS = 0;
public static final int OPTION_DISABLED = 1; public static final int OPTION_DISABLED = 1;
public static final int STATUS_DISABLED = -1; public static final int STATUS_CONNECTING = 0;
public static final int STATUS_OFFLINE = 0; public static final int STATUS_DISABLED = -2;
public static final int STATUS_OFFLINE = -1;
public static final int STATUS_ONLINE = 1; public static final int STATUS_ONLINE = 1;
public static final int STATUS_UNAUTHORIZED = 2; public static final int STATUS_UNAUTHORIZED = 2;
public static final int STATUS_NOINTERNET = 3; public static final int STATUS_NO_INTERNET = 3;
public static final int STATUS_TLS_ERROR = 4; public static final int STATUS_TLS_ERROR = 4;
public static final int STATUS_SERVER_NOT_FOUND = 5; public static final int STATUS_SERVER_NOT_FOUND = 5;
@ -46,7 +47,7 @@ public class Account extends AbstractEntity{
protected int options = 0; protected int options = 0;
protected String rosterVersion; protected String rosterVersion;
protected String resource; protected String resource;
protected int status = 0; protected int status = -1;
protected JSONObject keys = new JSONObject(); protected JSONObject keys = new JSONObject();
protected boolean online = false; protected boolean online = false;

View File

@ -3,14 +3,26 @@ package eu.siacs.conversations.services;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class EventReceiver extends BroadcastReceiver { public class EventReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
Intent mIntentForService = new Intent(context, XmppConnectionService.class); Intent mIntentForService = new Intent(context,
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { 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); context.startService(mIntentForService);
} }

View File

@ -5,6 +5,7 @@ import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.List; import java.util.List;
import java.util.Random;
import org.json.JSONException; import org.json.JSONException;
import org.openintents.openpgp.util.OpenPgpApi; 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.OnStatusChanged;
import eu.siacs.conversations.xmpp.PresencePacket; import eu.siacs.conversations.xmpp.PresencePacket;
import eu.siacs.conversations.xmpp.XmppConnection; import eu.siacs.conversations.xmpp.XmppConnection;
import android.app.AlarmManager;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -53,6 +56,7 @@ import android.os.Binder;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.SystemClock;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.util.Log; import android.util.Log;
@ -71,6 +75,8 @@ public class XmppConnectionService extends Service {
public OnConversationListChangedListener convChangedListener = null; public OnConversationListChangedListener convChangedListener = null;
private OnAccountListChangedListener accountChangedListener = null; private OnAccountListChangedListener accountChangedListener = null;
private Random mRandom = new Random(System.currentTimeMillis());
private ContentObserver contactObserver = new ContentObserver(null) { private ContentObserver contactObserver = new ContentObserver(null) {
@Override @Override
public void onChange(boolean selfChange) { public void onChange(boolean selfChange) {
@ -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 @Override
public int onStartCommand(Intent intent, int flags, int startId) { 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) { 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.getXmppConnection() == null) {
if (!account.isOptionSet(Account.OPTION_DISABLED)) { if ((!account.isOptionSet(Account.OPTION_DISABLED))&&(internet)) {
account.setXmppConnection(this.createConnection(account)); 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( this.pgpServiceConnection = new OpenPgpServiceConnection(
getApplicationContext(), "org.sufficientlysecure.keychain"); getApplicationContext(), "org.sufficientlysecure.keychain");
this.pgpServiceConnection.bindToService(); this.pgpServiceConnection.bindToService();
} }
@Override @Override
@ -396,6 +441,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) { public XmppConnection createConnection(Account account) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
XmppConnection connection = new XmppConnection(account, pm); XmppConnection connection = new XmppConnection(account, pm);
@ -404,8 +460,6 @@ public class XmppConnectionService extends Service {
connection.setOnPresencePacketReceivedListener(this.presenceListener); connection.setOnPresencePacketReceivedListener(this.presenceListener);
connection connection
.setOnUnregisteredIqPacketReceivedListener(this.unknownIqListener); .setOnUnregisteredIqPacketReceivedListener(this.unknownIqListener);
Thread thread = new Thread(connection);
thread.start();
return connection; return connection;
} }

View File

@ -32,7 +32,7 @@ import android.widget.ArrayAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
public class ManageAccountActivity extends XmppActivity implements ActionMode.Callback { public class ManageAccountActivity extends XmppActivity {
public static final int REQUEST_ANNOUNCE_PGP = 0x73731; public static final int REQUEST_ANNOUNCE_PGP = 0x73731;
@ -54,10 +54,6 @@ public class ManageAccountActivity extends XmppActivity implements ActionMode.Ca
@Override @Override
public void run() { public void run() {
if (accountList.size() == 1) {
startActivity(new Intent(getApplicationContext(),
NewConversationActivity.class));
}
accountListViewAdapter.notifyDataSetChanged(); accountListViewAdapter.notifyDataSetChanged();
} }
}); });
@ -113,7 +109,7 @@ public class ManageAccountActivity extends XmppActivity implements ActionMode.Ca
return view; return view;
} }
}; };
final Activity activity = this; final XmppActivity activity = this;
accountListView.setAdapter(this.accountListViewAdapter); accountListView.setAdapter(this.accountListViewAdapter);
accountListView.setOnItemClickListener(new OnItemClickListener() { accountListView.setOnItemClickListener(new OnItemClickListener() {
@ -146,7 +142,76 @@ public class ManageAccountActivity extends XmppActivity implements ActionMode.Ca
accountListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); accountListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
accountListView.setItemChecked(position,true); accountListView.setItemChecked(position,true);
selectedAccountForActionMode = accountList.get(position); selectedAccountForActionMode = accountList.get(position);
actionMode = activity.startActionMode((Callback) activity); actionMode = activity.startActionMode((new ActionMode.Callback() {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
if (selectedAccountForActionMode.isOptionSet(Account.OPTION_DISABLED)) {
menu.findItem(R.id.account_enable).setVisible(true);
menu.findItem(R.id.account_disable).setVisible(false);
} else {
menu.findItem(R.id.account_disable).setVisible(true);
menu.findItem(R.id.account_enable).setVisible(false);
}
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.manageaccounts_context, menu);
return true;
}
@Override
public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
if (item.getItemId()==R.id.account_disable) {
selectedAccountForActionMode.setOption(Account.OPTION_DISABLED, true);
xmppConnectionService.updateAccount(selectedAccountForActionMode);
mode.finish();
} else if (item.getItemId()==R.id.account_enable) {
selectedAccountForActionMode.setOption(Account.OPTION_DISABLED, false);
xmppConnectionService.updateAccount(selectedAccountForActionMode);
mode.finish();
} else if (item.getItemId()==R.id.account_delete) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Are you sure?");
builder.setIconAttribute(android.R.attr.alertDialogIcon);
builder.setMessage("If you delete your account your entire conversation history will be lost");
builder.setPositiveButton("Delete", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
xmppConnectionService.deleteAccount(selectedAccountForActionMode);
selectedAccountForActionMode = null;
mode.finish();
}
});
builder.setNegativeButton("Cancel",null);
builder.create().show();
} else if (item.getItemId()==R.id.announce_pgp) {
if (activity.hasPgp()) {
mode.finish();
try {
xmppConnectionService.generatePgpAnnouncement(selectedAccountForActionMode);
} catch (PgpEngine.UserInputRequiredException e) {
try {
startIntentSenderForResult(e.getPendingIntent().getIntentSender(), REQUEST_ANNOUNCE_PGP, null, 0, 0, 0);
} catch (SendIntentException e1) {
Log.d("gultsch","sending intent failed");
}
}
}
}
return true;
}
}));
return true; return true;
} else { } else {
return false; return false;
@ -210,73 +275,6 @@ public class ManageAccountActivity extends XmppActivity implements ActionMode.Ca
dialog.show(getFragmentManager(), "add_account"); dialog.show(getFragmentManager(), "add_account");
} }
@Override
public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
if (item.getItemId()==R.id.account_disable) {
selectedAccountForActionMode.setOption(Account.OPTION_DISABLED, true);
xmppConnectionService.updateAccount(selectedAccountForActionMode);
mode.finish();
} else if (item.getItemId()==R.id.account_enable) {
selectedAccountForActionMode.setOption(Account.OPTION_DISABLED, false);
xmppConnectionService.updateAccount(selectedAccountForActionMode);
mode.finish();
} else if (item.getItemId()==R.id.account_delete) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Are you sure?");
builder.setIconAttribute(android.R.attr.alertDialogIcon);
builder.setMessage("If you delete your account your entire conversation history will be lost");
builder.setPositiveButton("Delete", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
xmppConnectionService.deleteAccount(selectedAccountForActionMode);
selectedAccountForActionMode = null;
mode.finish();
}
});
builder.setNegativeButton("Cancel",null);
builder.create().show();
} else if (item.getItemId()==R.id.announce_pgp) {
if (this.hasPgp()) {
mode.finish();
try {
xmppConnectionService.generatePgpAnnouncement(selectedAccountForActionMode);
} catch (PgpEngine.UserInputRequiredException e) {
try {
startIntentSenderForResult(e.getPendingIntent().getIntentSender(), REQUEST_ANNOUNCE_PGP, null, 0, 0, 0);
} catch (SendIntentException e1) {
Log.d("gultsch","sending intent failed");
}
}
}
}
return true;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.manageaccounts_context, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
if (selectedAccountForActionMode.isOptionSet(Account.OPTION_DISABLED)) {
menu.findItem(R.id.account_enable).setVisible(true);
menu.findItem(R.id.account_disable).setVisible(false);
} else {
menu.findItem(R.id.account_disable).setVisible(true);
menu.findItem(R.id.account_enable).setVisible(false);
}
return true;
}
@Override @Override
public void onActionModeStarted(ActionMode mode) { public void onActionModeStarted(ActionMode mode) {

View File

@ -8,12 +8,10 @@ import java.net.InetAddress;
import java.util.Random; import java.util.Random;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
public class DNSHelper { public class DNSHelper {
public static Bundle getSRVRecord(String host) { public static Bundle getSRVRecord(String host) throws IOException {
Bundle namePort = new Bundle(); Bundle namePort = new Bundle();
try {
String[] hostParts = host.split("\\."); String[] hostParts = host.split("\\.");
byte[] transId = new byte[2]; byte[] transId = new byte[2];
Random random = new Random(); Random random = new Random();
@ -70,9 +68,6 @@ public class DNSHelper {
} }
builder.replace(0, 1, ""); builder.replace(0, 1, "");
namePort.putString("name",builder.toString()); namePort.putString("name",builder.toString());
} catch (IOException e) {
Log.d("xmppService","gut" + e.getMessage());
}
return namePort; return namePort;
} }

View File

@ -69,6 +69,7 @@ public class XmppConnection implements Runnable {
protected void connect() { protected void connect() {
try { try {
account.setStatus(Account.STATUS_CONNECTING);
Bundle namePort = DNSHelper.getSRVRecord(account.getServer()); Bundle namePort = DNSHelper.getSRVRecord(account.getServer());
String srvRecordServer = namePort.getString("name"); String srvRecordServer = namePort.getString("name");
int srvRecordPort = namePort.getInt("port"); int srvRecordPort = namePort.getInt("port");
@ -103,18 +104,24 @@ public class XmppConnection implements Runnable {
if (statusListener != null) { if (statusListener != null) {
statusListener.onStatusChanged(account); statusListener.onStatusChanged(account);
} }
if (wakeLock.isHeld()) {
wakeLock.release();
}
return; return;
} catch (IOException e) { } catch (IOException e) {
Log.d(LOGTAG, "bla " + e.getMessage());
if (shouldConnect) {
Log.d(LOGTAG, account.getJid() + ": connection lost");
account.setStatus(Account.STATUS_OFFLINE); account.setStatus(Account.STATUS_OFFLINE);
if (statusListener != null) { if (statusListener != null) {
statusListener.onStatusChanged(account); statusListener.onStatusChanged(account);
} }
if (wakeLock.isHeld()) {
wakeLock.release();
} }
return;
} catch (XmlPullParserException e) { } catch (XmlPullParserException e) {
Log.d(LOGTAG, "xml exception " + e.getMessage()); Log.d(LOGTAG, "xml exception " + e.getMessage());
if (wakeLock.isHeld()) {
wakeLock.release();
}
return; return;
} }
@ -122,18 +129,7 @@ public class XmppConnection implements Runnable {
@Override @Override
public void run() { public void run() {
shouldConnect = true;
while (shouldConnect) {
connect(); connect();
try {
if (shouldConnect) {
Thread.sleep(30000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Log.d(LOGTAG, "end run"); Log.d(LOGTAG, "end run");
} }