From ea6a008b39b0f669f66f3c8aec08b4deb3ece154 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 31 May 2016 17:20:21 +0200 Subject: execute phone contact changes in singlethreadexecutor --- .../services/XmppConnectionService.java | 97 ++++++++++------------ .../eu/siacs/conversations/utils/PhoneHelper.java | 4 +- .../utils/ReplacingSerialSingleThreadExecutor.java | 14 ++++ .../utils/SerialSingleThreadExecutor.java | 19 ++++- 4 files changed, 79 insertions(+), 55 deletions(-) create mode 100644 src/main/java/eu/siacs/conversations/utils/ReplacingSerialSingleThreadExecutor.java (limited to 'src/main') diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 757e29b5..fa8ac0ed 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -96,6 +96,7 @@ import eu.siacs.conversations.utils.ExceptionHelper; import eu.siacs.conversations.utils.OnPhoneContactsLoadedListener; import eu.siacs.conversations.utils.PRNGFixes; import eu.siacs.conversations.utils.PhoneHelper; +import eu.siacs.conversations.utils.ReplacingSerialSingleThreadExecutor; import eu.siacs.conversations.utils.SerialSingleThreadExecutor; import eu.siacs.conversations.utils.Xmlns; import eu.siacs.conversations.xml.Element; @@ -123,7 +124,7 @@ import eu.siacs.conversations.xmpp.stanzas.MessagePacket; import eu.siacs.conversations.xmpp.stanzas.PresencePacket; import me.leolin.shortcutbadger.ShortcutBadger; -public class XmppConnectionService extends Service implements OnPhoneContactsLoadedListener { +public class XmppConnectionService extends Service { public static final String ACTION_CLEAR_NOTIFICATION = "clear_notification"; public static final String ACTION_DISABLE_FOREGROUND = "disable_foreground"; @@ -135,6 +136,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa public static final String ACTION_GCM_MESSAGE_RECEIVED = "gcm_message_received"; private final SerialSingleThreadExecutor mFileAddingExecutor = new SerialSingleThreadExecutor(); private final SerialSingleThreadExecutor mDatabaseExecutor = new SerialSingleThreadExecutor(); + private ReplacingSerialSingleThreadExecutor mContactMergerExecutor = new ReplacingSerialSingleThreadExecutor(true); private final IBinder mBinder = new XmppConnectionBinder(); private final List conversations = new CopyOnWriteArrayList<>(); private final IqGenerator mIqGenerator = new IqGenerator(this); @@ -350,7 +352,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa private WakeLock wakeLock; private PowerManager pm; private LruCache mBitmapCache; - private Thread mPhoneContactMergerThread; private EventReceiver mEventReceiver = new EventReceiver(); private boolean mRestoredFromDatabase = false; @@ -1152,53 +1153,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa sendIqPacket(account, iqPacket, mDefaultIqHandler); } - public void onPhoneContactsLoaded(final List phoneContacts) { - if (mPhoneContactMergerThread != null) { - mPhoneContactMergerThread.interrupt(); - } - mPhoneContactMergerThread = new Thread(new Runnable() { - @Override - public void run() { - Log.d(Config.LOGTAG, "start merging phone contacts with roster"); - for (Account account : accounts) { - List withSystemAccounts = account.getRoster().getWithSystemAccounts(); - for (Bundle phoneContact : phoneContacts) { - if (Thread.interrupted()) { - Log.d(Config.LOGTAG, "interrupted merging phone contacts"); - return; - } - Jid jid; - try { - jid = Jid.fromString(phoneContact.getString("jid")); - } catch (final InvalidJidException e) { - continue; - } - final Contact contact = account.getRoster().getContact(jid); - String systemAccount = phoneContact.getInt("phoneid") - + "#" - + phoneContact.getString("lookup"); - contact.setSystemAccount(systemAccount); - if (contact.setPhotoUri(phoneContact.getString("photouri"))) { - getAvatarService().clear(contact); - } - contact.setSystemName(phoneContact.getString("displayname")); - withSystemAccounts.remove(contact); - } - for (Contact contact : withSystemAccounts) { - contact.setSystemAccount(null); - contact.setSystemName(null); - if (contact.setPhotoUri(null)) { - getAvatarService().clear(contact); - } - } - } - Log.d(Config.LOGTAG, "finished merging phone contacts"); - updateAccountUi(); - } - }); - mPhoneContactMergerThread.start(); - } - private void restoreFromDatabase() { synchronized (this.conversations) { final Map accountLookupTable = new Hashtable<>(); @@ -1219,7 +1173,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa account.initAccountServices(XmppConnectionService.this); //roster needs to be loaded at this stage } getBitmapCache().evictAll(); - Looper.prepare(); loadPhoneContacts(); Log.d(Config.LOGTAG, "restoring messages"); for (Conversation conversation : conversations) { @@ -1243,9 +1196,47 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } public void loadPhoneContacts() { - PhoneHelper.loadPhoneContacts(getApplicationContext(), - new CopyOnWriteArrayList(), - XmppConnectionService.this); + mContactMergerExecutor.execute(new Runnable() { + @Override + public void run() { + PhoneHelper.loadPhoneContacts(XmppConnectionService.this, new OnPhoneContactsLoadedListener() { + @Override + public void onPhoneContactsLoaded(List phoneContacts) { + Log.d(Config.LOGTAG, "start merging phone contacts with roster"); + for (Account account : accounts) { + List withSystemAccounts = account.getRoster().getWithSystemAccounts(); + for (Bundle phoneContact : phoneContacts) { + Jid jid; + try { + jid = Jid.fromString(phoneContact.getString("jid")); + } catch (final InvalidJidException e) { + continue; + } + final Contact contact = account.getRoster().getContact(jid); + String systemAccount = phoneContact.getInt("phoneid") + + "#" + + phoneContact.getString("lookup"); + contact.setSystemAccount(systemAccount); + if (contact.setPhotoUri(phoneContact.getString("photouri"))) { + getAvatarService().clear(contact); + } + contact.setSystemName(phoneContact.getString("displayname")); + withSystemAccounts.remove(contact); + } + for (Contact contact : withSystemAccounts) { + contact.setSystemAccount(null); + contact.setSystemName(null); + if (contact.setPhotoUri(null)) { + getAvatarService().clear(contact); + } + } + } + Log.d(Config.LOGTAG, "finished merging phone contacts"); + updateAccountUi(); + } + }); + } + }); } public List getConversations() { diff --git a/src/main/java/eu/siacs/conversations/utils/PhoneHelper.java b/src/main/java/eu/siacs/conversations/utils/PhoneHelper.java index 6c1b4bef..e3a24fcf 100644 --- a/src/main/java/eu/siacs/conversations/utils/PhoneHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/PhoneHelper.java @@ -13,12 +13,14 @@ import android.os.Bundle; import android.provider.ContactsContract; import android.provider.ContactsContract.Profile; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.RejectedExecutionException; public class PhoneHelper { - public static void loadPhoneContacts(Context context, final List phoneContacts, final OnPhoneContactsLoadedListener listener) { + public static void loadPhoneContacts(Context context, final OnPhoneContactsLoadedListener listener) { + final List phoneContacts = new ArrayList<>(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { listener.onPhoneContactsLoaded(phoneContacts); diff --git a/src/main/java/eu/siacs/conversations/utils/ReplacingSerialSingleThreadExecutor.java b/src/main/java/eu/siacs/conversations/utils/ReplacingSerialSingleThreadExecutor.java new file mode 100644 index 00000000..dc8a0a82 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/utils/ReplacingSerialSingleThreadExecutor.java @@ -0,0 +1,14 @@ +package eu.siacs.conversations.utils; + +public class ReplacingSerialSingleThreadExecutor extends SerialSingleThreadExecutor { + + public ReplacingSerialSingleThreadExecutor(boolean prepareLooper) { + super(prepareLooper); + } + + @Override + public synchronized void execute(final Runnable r) { + tasks.clear(); + super.execute(r); + } +} diff --git a/src/main/java/eu/siacs/conversations/utils/SerialSingleThreadExecutor.java b/src/main/java/eu/siacs/conversations/utils/SerialSingleThreadExecutor.java index bfb4668d..55489ee3 100644 --- a/src/main/java/eu/siacs/conversations/utils/SerialSingleThreadExecutor.java +++ b/src/main/java/eu/siacs/conversations/utils/SerialSingleThreadExecutor.java @@ -1,5 +1,7 @@ package eu.siacs.conversations.utils; +import android.os.Looper; + import java.util.ArrayDeque; import java.util.Queue; import java.util.concurrent.Executor; @@ -8,9 +10,24 @@ import java.util.concurrent.Executors; public class SerialSingleThreadExecutor implements Executor { final Executor executor = Executors.newSingleThreadExecutor(); - final Queue tasks = new ArrayDeque(); + protected final Queue tasks = new ArrayDeque(); Runnable active; + public SerialSingleThreadExecutor() { + this(false); + } + + public SerialSingleThreadExecutor(boolean prepareLooper) { + if (prepareLooper) { + execute(new Runnable() { + @Override + public void run() { + Looper.prepare(); + } + }); + } + } + public synchronized void execute(final Runnable r) { tasks.offer(new Runnable() { public void run() { -- cgit v1.2.3