refresh contacts when opening StartConversationsActivity

This commit is contained in:
Daniel Gultsch 2018-10-29 12:00:25 +01:00
parent 3e1d01798c
commit 1bcbd257c3
5 changed files with 42 additions and 2 deletions

View File

@ -48,6 +48,8 @@ public final class Config {
public static final boolean ALLOW_NON_TLS_CONNECTIONS = false; //very dangerous. you should have a good reason to set this to true public static final boolean ALLOW_NON_TLS_CONNECTIONS = false; //very dangerous. you should have a good reason to set this to true
public static final long CONTACT_SYNC_RETRY_INTERVAL = 1000L * 60 * 5;
//Notification settings //Notification settings
public static final boolean HIDE_MESSAGE_TEXT_IN_NOTIFICATION = false; public static final boolean HIDE_MESSAGE_TEXT_IN_NOTIFICATION = false;

View File

@ -67,6 +67,7 @@ public class Account extends AbstractEntity {
protected String password; protected String password;
protected int options = 0; protected int options = 0;
protected State status = State.OFFLINE; protected State status = State.OFFLINE;
private State lastErrorStatus = State.OFFLINE;
protected String resource; protected String resource;
protected String avatar; protected String avatar;
protected String hostname = null; protected String hostname = null;
@ -263,8 +264,15 @@ public class Account extends AbstractEntity {
} }
} }
public State getLastErrorStatus() {
return this.lastErrorStatus;
}
public void setStatus(final State status) { public void setStatus(final State status) {
this.status = status; this.status = status;
if (status.isError) {
this.lastErrorStatus = status;
}
} }
public State getTrueStatus() { public State getTrueStatus() {

View File

@ -890,9 +890,10 @@ public class NotificationService {
cancel(ERROR_NOTIFICATION_ID); cancel(ERROR_NOTIFICATION_ID);
return; return;
} }
final boolean showAllErrors = QuickConversationsService.isConversations();
final List<Account> errors = new ArrayList<>(); final List<Account> errors = new ArrayList<>();
for (final Account account : mXmppConnectionService.getAccounts()) { for (final Account account : mXmppConnectionService.getAccounts()) {
if (account.hasErrorStatus() && account.showErrorNotification()) { if (account.hasErrorStatus() && account.showErrorNotification() && (showAllErrors || account.getLastErrorStatus() == Account.State.UNAUTHORIZED)) {
errors.add(account); errors.add(account);
} }
} }

View File

@ -726,6 +726,7 @@ public class StartConversationActivity extends XmppActivity implements XmppConne
@Override @Override
protected void onBackendConnected() { protected void onBackendConnected() {
xmppConnectionService.getQuickConversationsService().considerSync();
if (mPostponedActivityResult != null) { if (mPostponedActivityResult != null) {
onActivityResult(mPostponedActivityResult.first, RESULT_OK, mPostponedActivityResult.second); onActivityResult(mPostponedActivityResult.first, RESULT_OK, mPostponedActivityResult.second);
this.mPostponedActivityResult = null; this.mPostponedActivityResult = null;

View File

@ -64,6 +64,8 @@ public class QuickConversationsService extends AbstractQuickConversationsService
private final AtomicBoolean mVerificationInProgress = new AtomicBoolean(false); private final AtomicBoolean mVerificationInProgress = new AtomicBoolean(false);
private final AtomicBoolean mVerificationRequestInProgress = new AtomicBoolean(false); private final AtomicBoolean mVerificationRequestInProgress = new AtomicBoolean(false);
private Attempt mLastSyncAttempt = new Attempt(0,0);
QuickConversationsService(XmppConnectionService xmppConnectionService) { QuickConversationsService(XmppConnectionService xmppConnectionService) {
super(xmppConnectionService); super(xmppConnectionService);
} }
@ -299,6 +301,12 @@ public class QuickConversationsService extends AbstractQuickConversationsService
} }
private boolean considerSync(Account account, final Map<String, PhoneNumberContact> contacts) { private boolean considerSync(Account account, final Map<String, PhoneNumberContact> contacts) {
final int hash = contacts.keySet().hashCode();
Log.d(Config.LOGTAG,account.getJid().asBareJid()+": consider sync of "+hash);
if (!mLastSyncAttempt.retry(hash)) {
Log.d(Config.LOGTAG,account.getJid().asBareJid()+": do not attempt sync");
return false;
}
XmppConnection xmppConnection = account.getXmppConnection(); XmppConnection xmppConnection = account.getXmppConnection();
Jid syncServer = xmppConnection == null ? null : xmppConnection.findDiscoItemByFeature(Namespace.SYNCHRONIZATION); Jid syncServer = xmppConnection == null ? null : xmppConnection.findDiscoItemByFeature(Namespace.SYNCHRONIZATION);
if (syncServer == null) { if (syncServer == null) {
@ -313,6 +321,7 @@ public class QuickConversationsService extends AbstractQuickConversationsService
IqPacket query = new IqPacket(IqPacket.TYPE.GET); IqPacket query = new IqPacket(IqPacket.TYPE.GET);
query.setTo(syncServer); query.setTo(syncServer);
query.addChild(new Element("phone-book", Namespace.SYNCHRONIZATION).setChildren(entries)); query.addChild(new Element("phone-book", Namespace.SYNCHRONIZATION).setChildren(entries));
mLastSyncAttempt = Attempt.create(hash);
service.sendIqPacket(account, query, (a, response) -> { service.sendIqPacket(account, query, (a, response) -> {
if (response.getType() == IqPacket.TYPE.RESULT) { if (response.getType() == IqPacket.TYPE.RESULT) {
List<Contact> withSystemAccounts = account.getRoster().getWithSystemAccounts(PhoneNumberContact.class); List<Contact> withSystemAccounts = account.getRoster().getWithSystemAccounts(PhoneNumberContact.class);
@ -334,13 +343,14 @@ public class QuickConversationsService extends AbstractQuickConversationsService
} }
} }
for (Contact contact : withSystemAccounts) { for (Contact contact : withSystemAccounts) {
boolean needsCacheClean = contact.unsetPhoneContact(JabberIdContact.class); final boolean needsCacheClean = contact.unsetPhoneContact(PhoneNumberContact.class);
if (needsCacheClean) { if (needsCacheClean) {
service.getAvatarService().clear(contact); service.getAvatarService().clear(contact);
} }
} }
} }
service.syncRoster(account); service.syncRoster(account);
service.updateRosterUi();
}); });
return true; return true;
} }
@ -354,6 +364,24 @@ public class QuickConversationsService extends AbstractQuickConversationsService
return null; return null;
} }
private static class Attempt {
private final long timestamp;
private int hash;
private Attempt(long timestamp, int hash) {
this.timestamp = timestamp;
this.hash = hash;
}
public static Attempt create(int hash) {
return new Attempt(SystemClock.elapsedRealtime(), hash);
}
public boolean retry(int hash) {
return hash != this.hash || SystemClock.elapsedRealtime() - timestamp >= Config.CONTACT_SYNC_RETRY_INTERVAL;
}
}
public static class Entry { public static class Entry {
private final List<Jid> jids; private final List<Jid> jids;
private final String number; private final String number;