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 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";
private static final String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts";
private ContentObserver contactObserver = new ContentObserver(null) {
@Override
public void onChange(boolean selfChange) {
@ -114,6 +113,56 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}
};
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 OnContactStatusChanged onContactStatusChanged = new OnContactStatusChanged() {
@ -142,7 +191,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
private MessageGenerator mMessageGenerator = new MessageGenerator(this);
private PresenceGenerator mPresenceGenerator = new PresenceGenerator(this);
private List<Account> accounts;
private final List<Conversation> conversations = new CopyOnWriteArrayList<>();
private JingleConnectionManager mJingleConnectionManager = new JingleConnectionManager(
this);
private HttpConnectionManager mHttpConnectionManager = new HttpConnectionManager(
@ -212,7 +260,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
getNotificationService().updateErrorNotification();
}
};
private int accountChangedListenerCount = 0;
private OnRosterUpdate mOnRosterUpdate = null;
private OnUpdateBlocklist mOnUpdateBlocklist = null;
@ -221,62 +268,11 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
private OnMucRosterUpdate mOnMucRosterUpdate = null;
private int mucRosterChangedListenerCount = 0;
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 PgpEngine mPgpEngine = null;
private WakeLock wakeLock;
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 final IqGenerator mIqGenerator = new IqGenerator(this);
private Thread mPhoneContactMergerThread;
public PgpEngine getPgpEngine() {
@ -548,7 +544,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}
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();
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
@ -958,11 +954,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}).start();
}
public interface OnMoreMessagesLoaded {
public void onMoreMessagesLoaded(int count,Conversation conversation);
public void informUser(int r);
}
public List<Account> getAccounts() {
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) {
if (jid == null ) {
if (jid == null) {
return null;
}
for (final Conversation conversation : haystack) {
@ -989,11 +980,11 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
return null;
}
public Conversation findOrCreateConversation(final Account account, final Jid jid,final boolean muc) {
return this.findOrCreateConversation(account,jid,muc,null);
public Conversation findOrCreateConversation(final Account account, final Jid jid, final boolean muc) {
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) {
Conversation conversation = find(account, jid);
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) {
synchronized (this.conversations) {
for (final Conversation conversation : conversations) {
@ -1308,11 +1294,11 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
PresencePacket packet = new PresencePacket();
packet.setFrom(conversation.getAccount().getJid());
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) {
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();
if (sig != null) {
packet.addChild("status").setContent("online");
@ -1417,7 +1403,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
return server;
}
}
for(Account other : getAccounts()) {
for (Account other : getAccounts()) {
if (other != account && other.getXmppConnection() != null) {
server = other.getXmppConnection().getMucServer();
if (server != null) {
@ -1435,12 +1421,12 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
String server = findConferenceServer(account);
if (server == null) {
if (callback != null) {
callback.error(R.string.no_conference_server_found,null);
callback.error(R.string.no_conference_server_found, null);
}
return;
}
String name = new BigInteger(75,getRNG()).toString(32);
Jid jid = Jid.fromParts(name,server,null);
String name = new BigInteger(75, getRNG()).toString(32);
Jid jid = Jid.fromParts(name, server, null);
final Conversation conversation = findOrCreateConversation(account, jid, true);
joinMuc(conversation);
Bundle options = new Bundle();
@ -1451,8 +1437,8 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
pushConferenceConfiguration(conversation, options, new OnConferenceOptionsPushed() {
@Override
public void onPushSucceeded() {
for(Jid invite : jids) {
invite(conversation,invite);
for (Jid invite : jids) {
invite(conversation, invite);
}
if (callback != null) {
callback.success(conversation);
@ -1474,7 +1460,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}
} else {
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);
request.setTo(conversation.getJid().toBareJid());
request.query("http://jabber.org/protocol/muc#owner");
sendIqPacket(conversation.getAccount(),request,new OnIqPacketReceived() {
sendIqPacket(conversation.getAccount(), request, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
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)) {
Bundle options = new Bundle();
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) {
List<Jid> jids = new ArrayList<>();
for(MucOptions.User user : conference.getMucOptions().getUsers()) {
for (MucOptions.User user : conference.getMucOptions().getUsers()) {
if (user.getAffiliation() == before) {
jids.add(user.getJid());
}
@ -1582,14 +1568,9 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
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) {
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() {
@Override
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) {
if ((account.getStatus() == Account.State.ONLINE)
|| (account.getStatus() == Account.State.DISABLED)) {
@ -1929,7 +1905,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}
Thread thread = new Thread(account.getXmppConnection());
thread.start();
scheduleWakeUpCall(Config.CONNECT_TIMEOUT,account.getUuid().hashCode());
scheduleWakeUpCall(Config.CONNECT_TIMEOUT, account.getUuid().hashCode());
} else {
account.getRoster().clearPresences();
account.setXmppConnection(null);
@ -1978,8 +1954,8 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
return false;
} else {
Message message = conversation.findSentMessageWithUuid(uuid);
if (message!=null) {
markMessage(message,status);
if (message != null) {
markMessage(message, status);
return true;
} else {
return false;
@ -2076,11 +2052,11 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
final Message markable = conversation.getLatestMarkableMessage();
this.markRead(conversation);
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();
final Jid to = markable.getCounterpart();
MessagePacket packet = mMessageGenerator.confirm(account, to, markable.getRemoteMsgId());
this.sendMessagePacket(conversation.getAccount(),packet);
this.sendMessagePacket(conversation.getAccount(), packet);
}
updateConversationUi();
}
@ -2176,7 +2152,9 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
return this.mIqGenerator;
}
public IqParser getIqParser() { return this.mIqParser; }
public IqParser getIqParser() {
return this.mIqParser;
}
public JingleConnectionManager getJingleConnectionManager() {
return this.mJingleConnectionManager;
@ -2235,33 +2213,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}).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) {
if (blockable != null && blockable.getBlockedJid() != null) {
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;
}
}
}