refactored phone contacts merger

This commit is contained in:
iNPUTmice 2014-12-13 13:52:57 +01:00
parent 2b958e51df
commit 2368ba518d

View file

@ -97,7 +97,7 @@ import eu.siacs.conversations.xmpp.stanzas.IqPacket;
import eu.siacs.conversations.xmpp.stanzas.MessagePacket; import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
import eu.siacs.conversations.xmpp.stanzas.PresencePacket; import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
public class XmppConnectionService extends Service { public class XmppConnectionService extends Service implements OnPhoneContactsLoadedListener {
public static String ACTION_CLEAR_NOTIFICATION = "clear_notification"; public static String ACTION_CLEAR_NOTIFICATION = "clear_notification";
private static String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts"; private static String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts";
@ -276,6 +276,7 @@ public class XmppConnectionService extends Service {
}; };
private LruCache<String, Bitmap> mBitmapCache; private LruCache<String, Bitmap> mBitmapCache;
private IqGenerator mIqGenerator = new IqGenerator(this); private IqGenerator mIqGenerator = new IqGenerator(this);
private Thread mPhoneContactMergerThread;
public PgpEngine getPgpEngine() { public PgpEngine getPgpEngine() {
if (pgpServiceConnection.isBound()) { if (pgpServiceConnection.isBound()) {
@ -384,7 +385,7 @@ public class XmppConnectionService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null && intent.getAction() != null) { if (intent != null && intent.getAction() != null) {
if (intent.getAction().equals(ACTION_MERGE_PHONE_CONTACTS)) { if (intent.getAction().equals(ACTION_MERGE_PHONE_CONTACTS)) {
mergePhoneContactsWithRoster(); PhoneHelper.loadPhoneContacts(getApplicationContext(), this);
return START_STICKY; return START_STICKY;
} else if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) { } else if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
logoutAndSave(); logoutAndSave();
@ -496,7 +497,7 @@ public class XmppConnectionService extends Service {
this.databaseBackend.readRoster(account.getRoster()); this.databaseBackend.readRoster(account.getRoster());
} }
initConversations(); initConversations();
this.mergePhoneContactsWithRoster(); PhoneHelper.loadPhoneContacts(getApplicationContext(),this);
getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactObserver); getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactObserver);
this.fileObserver.startWatching(); this.fileObserver.startWatching();
@ -839,39 +840,43 @@ public class XmppConnectionService extends Service {
sendIqPacket(account, iqPacket, null); sendIqPacket(account, iqPacket, null);
} }
private void mergePhoneContactsWithRoster() { public void onPhoneContactsLoaded(final List<Bundle> phoneContacts) {
PhoneHelper.loadPhoneContacts(getApplicationContext(), if (mPhoneContactMergerThread != null) {
new OnPhoneContactsLoadedListener() { mPhoneContactMergerThread.interrupt();
@Override }
public void onPhoneContactsLoaded(List<Bundle> phoneContacts) { mPhoneContactMergerThread = new Thread(new Runnable() {
for (Account account : accounts) { @Override
account.getRoster().clearSystemAccounts(); public void run() {
Log.d(Config.LOGTAG,"start merging phone contacts with roster");
for (Account account : accounts) {
account.getRoster().clearSystemAccounts();
for (Bundle phoneContact : phoneContacts) {
if (Thread.interrupted()) {
Log.d(Config.LOGTAG,"interrupted merging phone contacts");
return;
} }
for (Bundle phoneContact : phoneContacts) { Jid jid;
for (Account account : accounts) { try {
Jid jid; jid = Jid.fromString(phoneContact.getString("jid"));
try { } catch (final InvalidJidException e) {
jid = Jid.fromString(phoneContact.getString("jid")); break;
} catch (final InvalidJidException e) {
// TODO: Warn if contact import fails here?
break;
}
final Contact contact = account.getRoster()
.getContact(jid);
String systemAccount = phoneContact
.getInt("phoneid")
+ "#"
+ phoneContact.getString("lookup");
contact.setSystemAccount(systemAccount);
contact.setPhotoUri(phoneContact
.getString("photouri"));
contact.setSystemName(phoneContact
.getString("displayname"));
getAvatarService().clear(contact);
}
} }
final Contact contact = account.getRoster()
.getContact(jid);
String systemAccount = phoneContact.getInt("phoneid")
+ "#"
+ phoneContact.getString("lookup");
contact.setSystemAccount(systemAccount);
contact.setPhotoUri(phoneContact.getString("photouri"));
getAvatarService().clear(contact);
contact.setSystemName(phoneContact.getString("displayname"));
} }
}); }
Log.d(Config.LOGTAG,"finished merging phone contacts");
updateAccountUi();
}
});
mPhoneContactMergerThread.start();
} }
private void initConversations() { private void initConversations() {