account for rounding errors when rescheduling wake up

This commit is contained in:
iNPUTmice 2015-01-19 11:17:27 +01:00
parent fc2e458053
commit 7af588c8b3
1 changed files with 153 additions and 150 deletions

View File

@ -100,9 +100,8 @@ import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
public class XmppConnectionService extends Service implements OnPhoneContactsLoadedListener { public class XmppConnectionService extends Service implements OnPhoneContactsLoadedListener {
public static final String ACTION_CLEAR_NOTIFICATION = "clear_notification"; public static final String ACTION_CLEAR_NOTIFICATION = "clear_notification";
private static final String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts";
public static final String ACTION_DISABLE_FOREGROUND = "disable_foreground"; public static final String ACTION_DISABLE_FOREGROUND = "disable_foreground";
private static final String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts";
private ContentObserver contactObserver = new ContentObserver(null) { private ContentObserver contactObserver = new ContentObserver(null) {
@Override @Override
public void onChange(boolean selfChange) { public void onChange(boolean selfChange) {
@ -114,6 +113,56 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
}; };
private final IBinder mBinder = new XmppConnectionBinder(); private final IBinder mBinder = new XmppConnectionBinder();
private final List<Conversation> conversations = new CopyOnWriteArrayList<>();
private final FileObserver fileObserver = new FileObserver(
FileBackend.getConversationsImageDirectory()) {
@Override
public void onEvent(int event, String path) {
if (event == FileObserver.DELETE) {
markFileDeleted(path.split("\\.")[0]);
}
}
};
private final OnJinglePacketReceived jingleListener = new OnJinglePacketReceived() {
@Override
public void onJinglePacketReceived(Account account, JinglePacket packet) {
mJingleConnectionManager.deliverPacket(account, packet);
}
};
private final OnBindListener mOnBindListener = new OnBindListener() {
@Override
public void onBind(final Account account) {
account.getRoster().clearPresences();
account.pendingConferenceJoins.clear();
account.pendingConferenceLeaves.clear();
fetchRosterFromServer(account);
fetchBookmarks(account);
sendPresencePacket(account, mPresenceGenerator.sendPresence(account));
connectMultiModeConversations(account);
updateConversationUi();
}
};
private final OnMessageAcknowledged mOnMessageAcknowledgedListener = new OnMessageAcknowledged() {
@Override
public void onMessageAcknowledged(Account account, String uuid) {
for (final Conversation conversation : getConversations()) {
if (conversation.getAccount() == account) {
Message message = conversation.findUnsentMessageWithUuid(uuid);
if (message != null) {
markMessage(message, Message.STATUS_SEND);
if (conversation.setLastMessageTransmitted(System.currentTimeMillis())) {
databaseBackend.updateConversation(conversation);
}
}
}
}
}
};
private final IqGenerator mIqGenerator = new IqGenerator(this);
public DatabaseBackend databaseBackend; public DatabaseBackend databaseBackend;
public OnContactStatusChanged onContactStatusChanged = new OnContactStatusChanged() { public OnContactStatusChanged onContactStatusChanged = new OnContactStatusChanged() {
@ -142,7 +191,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
private MessageGenerator mMessageGenerator = new MessageGenerator(this); private MessageGenerator mMessageGenerator = new MessageGenerator(this);
private PresenceGenerator mPresenceGenerator = new PresenceGenerator(this); private PresenceGenerator mPresenceGenerator = new PresenceGenerator(this);
private List<Account> accounts; private List<Account> accounts;
private final List<Conversation> conversations = new CopyOnWriteArrayList<>();
private JingleConnectionManager mJingleConnectionManager = new JingleConnectionManager( private JingleConnectionManager mJingleConnectionManager = new JingleConnectionManager(
this); this);
private HttpConnectionManager mHttpConnectionManager = new HttpConnectionManager( private HttpConnectionManager mHttpConnectionManager = new HttpConnectionManager(
@ -212,7 +260,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
getNotificationService().updateErrorNotification(); getNotificationService().updateErrorNotification();
} }
}; };
private int accountChangedListenerCount = 0; private int accountChangedListenerCount = 0;
private OnRosterUpdate mOnRosterUpdate = null; private OnRosterUpdate mOnRosterUpdate = null;
private OnUpdateBlocklist mOnUpdateBlocklist = null; private OnUpdateBlocklist mOnUpdateBlocklist = null;
@ -221,62 +268,11 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
private OnMucRosterUpdate mOnMucRosterUpdate = null; private OnMucRosterUpdate mOnMucRosterUpdate = null;
private int mucRosterChangedListenerCount = 0; private int mucRosterChangedListenerCount = 0;
private SecureRandom mRandom; private SecureRandom mRandom;
private final FileObserver fileObserver = new FileObserver(
FileBackend.getConversationsImageDirectory()) {
@Override
public void onEvent(int event, String path) {
if (event == FileObserver.DELETE) {
markFileDeleted(path.split("\\.")[0]);
}
}
};
private final OnJinglePacketReceived jingleListener = new OnJinglePacketReceived() {
@Override
public void onJinglePacketReceived(Account account, JinglePacket packet) {
mJingleConnectionManager.deliverPacket(account, packet);
}
};
private OpenPgpServiceConnection pgpServiceConnection; private OpenPgpServiceConnection pgpServiceConnection;
private PgpEngine mPgpEngine = null; private PgpEngine mPgpEngine = null;
private WakeLock wakeLock; private WakeLock wakeLock;
private PowerManager pm; private PowerManager pm;
private final OnBindListener mOnBindListener = new OnBindListener() {
@Override
public void onBind(final Account account) {
account.getRoster().clearPresences();
account.pendingConferenceJoins.clear();
account.pendingConferenceLeaves.clear();
fetchRosterFromServer(account);
fetchBookmarks(account);
sendPresencePacket(account,mPresenceGenerator.sendPresence(account));
connectMultiModeConversations(account);
updateConversationUi();
}
};
private final OnMessageAcknowledged mOnMessageAcknowledgedListener = new OnMessageAcknowledged() {
@Override
public void onMessageAcknowledged(Account account, String uuid) {
for (final Conversation conversation : getConversations()) {
if (conversation.getAccount() == account) {
Message message = conversation.findUnsentMessageWithUuid(uuid);
if (message != null) {
markMessage(message, Message.STATUS_SEND);
if (conversation.setLastMessageTransmitted(System.currentTimeMillis())) {
databaseBackend.updateConversation(conversation);
}
}
}
}
}
};
private LruCache<String, Bitmap> mBitmapCache; private LruCache<String, Bitmap> mBitmapCache;
private final IqGenerator mIqGenerator = new IqGenerator(this);
private Thread mPhoneContactMergerThread; private Thread mPhoneContactMergerThread;
public PgpEngine getPgpEngine() { public PgpEngine getPgpEngine() {
@ -548,7 +544,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
protected void scheduleWakeUpCall(int seconds, int requestCode) { protected void scheduleWakeUpCall(int seconds, int requestCode) {
final long timeToWake = SystemClock.elapsedRealtime() + seconds * 1000; final long timeToWake = SystemClock.elapsedRealtime() + (seconds + 1) * 1000;
Context context = getApplicationContext(); Context context = getApplicationContext();
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
@ -958,11 +954,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}).start(); }).start();
} }
public interface OnMoreMessagesLoaded {
public void onMoreMessagesLoaded(int count,Conversation conversation);
public void informUser(int r);
}
public List<Account> getAccounts() { public List<Account> getAccounts() {
return this.accounts; return this.accounts;
} }
@ -977,7 +968,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
public Conversation find(final Iterable<Conversation> haystack, final Account account, final Jid jid) { public Conversation find(final Iterable<Conversation> haystack, final Account account, final Jid jid) {
if (jid == null ) { if (jid == null) {
return null; return null;
} }
for (final Conversation conversation : haystack) { for (final Conversation conversation : haystack) {
@ -989,11 +980,11 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
return null; return null;
} }
public Conversation findOrCreateConversation(final Account account, final Jid jid,final boolean muc) { public Conversation findOrCreateConversation(final Account account, final Jid jid, final boolean muc) {
return this.findOrCreateConversation(account,jid,muc,null); return this.findOrCreateConversation(account, jid, muc, null);
} }
public Conversation findOrCreateConversation(final Account account, final Jid jid,final boolean muc, final MessageArchiveService.Query query) { public Conversation findOrCreateConversation(final Account account, final Jid jid, final boolean muc, final MessageArchiveService.Query query) {
synchronized (this.conversations) { synchronized (this.conversations) {
Conversation conversation = find(account, jid); Conversation conversation = find(account, jid);
if (conversation != null) { if (conversation != null) {
@ -1097,11 +1088,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}); });
} }
public interface OnAccountPasswordChanged {
public void onPasswordChangeSucceeded();
public void onPasswordChangeFailed();
}
public void deleteAccount(final Account account) { public void deleteAccount(final Account account) {
synchronized (this.conversations) { synchronized (this.conversations) {
for (final Conversation conversation : conversations) { for (final Conversation conversation : conversations) {
@ -1308,11 +1294,11 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
PresencePacket packet = new PresencePacket(); PresencePacket packet = new PresencePacket();
packet.setFrom(conversation.getAccount().getJid()); packet.setFrom(conversation.getAccount().getJid());
packet.setTo(joinJid); packet.setTo(joinJid);
Element x = packet.addChild("x","http://jabber.org/protocol/muc"); Element x = packet.addChild("x", "http://jabber.org/protocol/muc");
if (conversation.getMucOptions().getPassword() != null) { if (conversation.getMucOptions().getPassword() != null) {
x.addChild("password").setContent(conversation.getMucOptions().getPassword()); x.addChild("password").setContent(conversation.getMucOptions().getPassword());
} }
x.addChild("history").setAttribute("since",PresenceGenerator.getTimestamp(conversation.getLastMessageTransmitted())); x.addChild("history").setAttribute("since", PresenceGenerator.getTimestamp(conversation.getLastMessageTransmitted()));
String sig = account.getPgpSignature(); String sig = account.getPgpSignature();
if (sig != null) { if (sig != null) {
packet.addChild("status").setContent("online"); packet.addChild("status").setContent("online");
@ -1417,7 +1403,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
return server; return server;
} }
} }
for(Account other : getAccounts()) { for (Account other : getAccounts()) {
if (other != account && other.getXmppConnection() != null) { if (other != account && other.getXmppConnection() != null) {
server = other.getXmppConnection().getMucServer(); server = other.getXmppConnection().getMucServer();
if (server != null) { if (server != null) {
@ -1435,12 +1421,12 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
String server = findConferenceServer(account); String server = findConferenceServer(account);
if (server == null) { if (server == null) {
if (callback != null) { if (callback != null) {
callback.error(R.string.no_conference_server_found,null); callback.error(R.string.no_conference_server_found, null);
} }
return; return;
} }
String name = new BigInteger(75,getRNG()).toString(32); String name = new BigInteger(75, getRNG()).toString(32);
Jid jid = Jid.fromParts(name,server,null); Jid jid = Jid.fromParts(name, server, null);
final Conversation conversation = findOrCreateConversation(account, jid, true); final Conversation conversation = findOrCreateConversation(account, jid, true);
joinMuc(conversation); joinMuc(conversation);
Bundle options = new Bundle(); Bundle options = new Bundle();
@ -1451,8 +1437,8 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
pushConferenceConfiguration(conversation, options, new OnConferenceOptionsPushed() { pushConferenceConfiguration(conversation, options, new OnConferenceOptionsPushed() {
@Override @Override
public void onPushSucceeded() { public void onPushSucceeded() {
for(Jid invite : jids) { for (Jid invite : jids) {
invite(conversation,invite); invite(conversation, invite);
} }
if (callback != null) { if (callback != null) {
callback.success(conversation); callback.success(conversation);
@ -1474,7 +1460,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
} else { } else {
if (callback != null) { if (callback != null) {
callback.error(R.string.not_connected_try_again,null); callback.error(R.string.not_connected_try_again, null);
} }
} }
} }
@ -1503,11 +1489,11 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}); });
} }
public void pushConferenceConfiguration(final Conversation conversation,final Bundle options, final OnConferenceOptionsPushed callback) { public void pushConferenceConfiguration(final Conversation conversation, final Bundle options, final OnConferenceOptionsPushed callback) {
IqPacket request = new IqPacket(IqPacket.TYPE.GET); IqPacket request = new IqPacket(IqPacket.TYPE.GET);
request.setTo(conversation.getJid().toBareJid()); request.setTo(conversation.getJid().toBareJid());
request.query("http://jabber.org/protocol/muc#owner"); request.query("http://jabber.org/protocol/muc#owner");
sendIqPacket(conversation.getAccount(),request,new OnIqPacketReceived() { sendIqPacket(conversation.getAccount(), request, new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() != IqPacket.TYPE.ERROR) { if (packet.getType() != IqPacket.TYPE.ERROR) {
@ -1552,7 +1538,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
if (!mucOptions.persistent() && self.getAffiliation().ranks(MucOptions.Affiliation.OWNER)) { if (!mucOptions.persistent() && self.getAffiliation().ranks(MucOptions.Affiliation.OWNER)) {
Bundle options = new Bundle(); Bundle options = new Bundle();
options.putString("muc#roomconfig_persistentroom", "1"); options.putString("muc#roomconfig_persistentroom", "1");
this.pushConferenceConfiguration(conference,options,null); this.pushConferenceConfiguration(conference, options, null);
} }
} }
@ -1573,7 +1559,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
public void changeAffiliationsInConference(final Conversation conference, MucOptions.Affiliation before, MucOptions.Affiliation after) { public void changeAffiliationsInConference(final Conversation conference, MucOptions.Affiliation before, MucOptions.Affiliation after) {
List<Jid> jids = new ArrayList<>(); List<Jid> jids = new ArrayList<>();
for(MucOptions.User user : conference.getMucOptions().getUsers()) { for (MucOptions.User user : conference.getMucOptions().getUsers()) {
if (user.getAffiliation() == before) { if (user.getAffiliation() == before) {
jids.add(user.getJid()); jids.add(user.getJid());
} }
@ -1582,14 +1568,9 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
sendIqPacket(conference.getAccount(), request, null); sendIqPacket(conference.getAccount(), request, null);
} }
public interface OnAffiliationChanged {
public void onAffiliationChangedSuccessful(Jid jid);
public void onAffiliationChangeFailed(Jid jid, int resId);
}
public void changeRoleInConference(final Conversation conference, final String nick, MucOptions.Role role, final OnRoleChanged callback) { public void changeRoleInConference(final Conversation conference, final String nick, MucOptions.Role role, final OnRoleChanged callback) {
IqPacket request = this.mIqGenerator.changeRole(conference, nick, role.toString()); IqPacket request = this.mIqGenerator.changeRole(conference, nick, role.toString());
Log.d(Config.LOGTAG,request.toString()); Log.d(Config.LOGTAG, request.toString());
sendIqPacket(conference.getAccount(), request, new OnIqPacketReceived() { sendIqPacket(conference.getAccount(), request, new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
@ -1603,11 +1584,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}); });
} }
public interface OnRoleChanged{
public void onRoleChangedSuccessful(String nick);
public void onRoleChangeFailed(String nick, int resid);
}
public void disconnect(Account account, boolean force) { public void disconnect(Account account, boolean force) {
if ((account.getStatus() == Account.State.ONLINE) if ((account.getStatus() == Account.State.ONLINE)
|| (account.getStatus() == Account.State.DISABLED)) { || (account.getStatus() == Account.State.DISABLED)) {
@ -1929,7 +1905,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
Thread thread = new Thread(account.getXmppConnection()); Thread thread = new Thread(account.getXmppConnection());
thread.start(); thread.start();
scheduleWakeUpCall(Config.CONNECT_TIMEOUT,account.getUuid().hashCode()); scheduleWakeUpCall(Config.CONNECT_TIMEOUT, account.getUuid().hashCode());
} else { } else {
account.getRoster().clearPresences(); account.getRoster().clearPresences();
account.setXmppConnection(null); account.setXmppConnection(null);
@ -1978,8 +1954,8 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
return false; return false;
} else { } else {
Message message = conversation.findSentMessageWithUuid(uuid); Message message = conversation.findSentMessageWithUuid(uuid);
if (message!=null) { if (message != null) {
markMessage(message,status); markMessage(message, status);
return true; return true;
} else { } else {
return false; return false;
@ -2076,11 +2052,11 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
final Message markable = conversation.getLatestMarkableMessage(); final Message markable = conversation.getLatestMarkableMessage();
this.markRead(conversation); this.markRead(conversation);
if (confirmMessages() && markable != null && markable.getRemoteMsgId() != null) { if (confirmMessages() && markable != null && markable.getRemoteMsgId() != null) {
Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid()+ ": sending read marker to " + markable.getCounterpart().toString()); Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() + ": sending read marker to " + markable.getCounterpart().toString());
Account account = conversation.getAccount(); Account account = conversation.getAccount();
final Jid to = markable.getCounterpart(); final Jid to = markable.getCounterpart();
MessagePacket packet = mMessageGenerator.confirm(account, to, markable.getRemoteMsgId()); MessagePacket packet = mMessageGenerator.confirm(account, to, markable.getRemoteMsgId());
this.sendMessagePacket(conversation.getAccount(),packet); this.sendMessagePacket(conversation.getAccount(), packet);
} }
updateConversationUi(); updateConversationUi();
} }
@ -2176,7 +2152,9 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
return this.mIqGenerator; return this.mIqGenerator;
} }
public IqParser getIqParser() { return this.mIqParser; } public IqParser getIqParser() {
return this.mIqParser;
}
public JingleConnectionManager getJingleConnectionManager() { public JingleConnectionManager getJingleConnectionManager() {
return this.mJingleConnectionManager; return this.mJingleConnectionManager;
@ -2235,33 +2213,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}).start(); }).start();
} }
public interface OnConversationUpdate {
public void onConversationUpdate();
}
public interface OnAccountUpdate {
public void onAccountUpdate();
}
public interface OnRosterUpdate {
public void onRosterUpdate();
}
public interface OnMucRosterUpdate {
public void onMucRosterUpdate();
}
public interface OnConferenceOptionsPushed {
public void onPushSucceeded();
public void onPushFailed();
}
public class XmppConnectionBinder extends Binder {
public XmppConnectionService getService() {
return XmppConnectionService.this;
}
}
public void sendBlockRequest(final Blockable blockable) { public void sendBlockRequest(final Blockable blockable) {
if (blockable != null && blockable.getBlockedJid() != null) { if (blockable != null && blockable.getBlockedJid() != null) {
final Jid jid = blockable.getBlockedJid(); final Jid jid = blockable.getBlockedJid();
@ -2292,4 +2243,56 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}); });
} }
} }
public interface OnMoreMessagesLoaded {
public void onMoreMessagesLoaded(int count, Conversation conversation);
public void informUser(int r);
}
public interface OnAccountPasswordChanged {
public void onPasswordChangeSucceeded();
public void onPasswordChangeFailed();
}
public interface OnAffiliationChanged {
public void onAffiliationChangedSuccessful(Jid jid);
public void onAffiliationChangeFailed(Jid jid, int resId);
}
public interface OnRoleChanged {
public void onRoleChangedSuccessful(String nick);
public void onRoleChangeFailed(String nick, int resid);
}
public interface OnConversationUpdate {
public void onConversationUpdate();
}
public interface OnAccountUpdate {
public void onAccountUpdate();
}
public interface OnRosterUpdate {
public void onRosterUpdate();
}
public interface OnMucRosterUpdate {
public void onMucRosterUpdate();
}
public interface OnConferenceOptionsPushed {
public void onPushSucceeded();
public void onPushFailed();
}
public class XmppConnectionBinder extends Binder {
public XmppConnectionService getService() {
return XmppConnectionService.this;
}
}
} }