mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-22 16:07:30 +00:00
Improve locking and performance on asynchronous contact loading.
This commit is contained in:
parent
25f75cb3d2
commit
2204584d8f
@ -21,6 +21,7 @@ import android.database.Cursor;
|
|||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.AbsListView;
|
||||||
import android.widget.CursorAdapter;
|
import android.widget.CursorAdapter;
|
||||||
|
|
||||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||||
@ -38,7 +39,7 @@ import java.util.Set;
|
|||||||
*
|
*
|
||||||
* @author Moxie Marlinspike
|
* @author Moxie Marlinspike
|
||||||
*/
|
*/
|
||||||
public class ConversationListAdapter extends CursorAdapter {
|
public class ConversationListAdapter extends CursorAdapter implements AbsListView.RecyclerListener {
|
||||||
|
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final LayoutInflater inflater;
|
private final LayoutInflater inflater;
|
||||||
@ -114,4 +115,9 @@ public class ConversationListAdapter extends CursorAdapter {
|
|||||||
|
|
||||||
this.notifyDataSetChanged();
|
this.notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMovedToScrapHeap(View view) {
|
||||||
|
((ConversationListItem)view).unbind();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -143,6 +143,7 @@ private void initializeSearch(android.widget.SearchView searchView) {
|
|||||||
this.setListAdapter(new DecryptingConversationListAdapter(getActivity(), null, masterSecret));
|
this.setListAdapter(new DecryptingConversationListAdapter(getActivity(), null, masterSecret));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getListView().setRecyclerListener((ConversationListAdapter)getListAdapter());
|
||||||
getLoaderManager().restartLoader(0, null, this);
|
getLoaderManager().restartLoader(0, null, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,6 +103,7 @@ public class ConversationListItem extends RelativeLayout
|
|||||||
this.count = thread.getCount();
|
this.count = thread.getCount();
|
||||||
this.read = thread.isRead();
|
this.read = thread.isRead();
|
||||||
|
|
||||||
|
this.recipients.addListener(this);
|
||||||
this.fromView.setText(formatFrom(recipients, count, read));
|
this.fromView.setText(formatFrom(recipients, count, read));
|
||||||
|
|
||||||
if (thread.isKeyExchange())
|
if (thread.isKeyExchange())
|
||||||
@ -124,7 +125,10 @@ public class ConversationListItem extends RelativeLayout
|
|||||||
else checkbox.setVisibility(View.GONE);
|
else checkbox.setVisibility(View.GONE);
|
||||||
|
|
||||||
setContactPhoto(this.recipients.getPrimaryRecipient());
|
setContactPhoto(this.recipients.getPrimaryRecipient());
|
||||||
this.recipients.setListener(this);
|
}
|
||||||
|
|
||||||
|
public void unbind() {
|
||||||
|
this.recipients.removeListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void intializeListeners() {
|
private void intializeListeners() {
|
||||||
|
@ -26,7 +26,7 @@ import org.thoughtcrime.securesms.recipients.RecipientProvider.RecipientDetails;
|
|||||||
import org.thoughtcrime.securesms.util.FutureTaskListener;
|
import org.thoughtcrime.securesms.util.FutureTaskListener;
|
||||||
import org.thoughtcrime.securesms.util.ListenableFutureTask;
|
import org.thoughtcrime.securesms.util.ListenableFutureTask;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.HashSet;
|
||||||
|
|
||||||
public class Recipient implements Parcelable {
|
public class Recipient implements Parcelable {
|
||||||
|
|
||||||
@ -40,33 +40,35 @@ public class Recipient implements Parcelable {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private final AtomicReference<String> name = new AtomicReference<String>(null);
|
|
||||||
private final AtomicReference<Bitmap> contactPhoto = new AtomicReference<Bitmap>(null);
|
|
||||||
private final AtomicReference<Uri> contactUri = new AtomicReference<Uri>(null);
|
|
||||||
|
|
||||||
private final String number;
|
private final String number;
|
||||||
|
private final HashSet<RecipientModifiedListener> listeners = new HashSet<RecipientModifiedListener>();
|
||||||
|
|
||||||
private RecipientModifiedListener listener;
|
private String name;
|
||||||
private boolean asynchronousUpdateComplete = false;
|
private Bitmap contactPhoto;
|
||||||
|
private Uri contactUri;
|
||||||
|
|
||||||
public Recipient(String number, Bitmap contactPhoto,
|
public Recipient(String number, Bitmap contactPhoto,
|
||||||
ListenableFutureTask<RecipientDetails> future)
|
ListenableFutureTask<RecipientDetails> future)
|
||||||
{
|
{
|
||||||
this.number = number;
|
this.number = number;
|
||||||
this.contactPhoto.set(contactPhoto);
|
this.contactPhoto = contactPhoto;
|
||||||
|
|
||||||
future.setListener(new FutureTaskListener<RecipientDetails>() {
|
future.setListener(new FutureTaskListener<RecipientDetails>() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(RecipientDetails result) {
|
public void onSuccess(RecipientDetails result) {
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
Recipient.this.name.set(result.name);
|
HashSet<RecipientModifiedListener> localListeners;
|
||||||
Recipient.this.contactUri.set(result.contactUri);
|
|
||||||
Recipient.this.contactPhoto.set(result.avatar);
|
|
||||||
|
|
||||||
synchronized(this) {
|
synchronized (Recipient.this) {
|
||||||
if (listener == null) asynchronousUpdateComplete = true;
|
Recipient.this.name = result.name;
|
||||||
else listener.onModified(Recipient.this);
|
Recipient.this.contactUri = result.contactUri;
|
||||||
|
Recipient.this.contactPhoto = result.avatar;
|
||||||
|
localListeners = (HashSet<RecipientModifiedListener>)listeners.clone();
|
||||||
|
listeners.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (RecipientModifiedListener listener : localListeners)
|
||||||
|
listener.onModified(Recipient.this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,25 +81,24 @@ public class Recipient implements Parcelable {
|
|||||||
|
|
||||||
public Recipient(String name, String number, Uri contactUri, Bitmap contactPhoto) {
|
public Recipient(String name, String number, Uri contactUri, Bitmap contactPhoto) {
|
||||||
this.number = number;
|
this.number = number;
|
||||||
this.contactUri.set(contactUri);
|
this.contactUri = contactUri;
|
||||||
this.name.set(name);
|
this.name = name;
|
||||||
this.contactPhoto.set(contactPhoto);
|
this.contactPhoto = contactPhoto;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Recipient(Parcel in) {
|
public Recipient(Parcel in) {
|
||||||
this.number = in.readString();
|
this.number = in.readString();
|
||||||
|
this.name = in.readString();
|
||||||
this.name.set(in.readString());
|
this.contactUri = (Uri)in.readParcelable(null);
|
||||||
this.contactUri.set((Uri)in.readParcelable(null));
|
this.contactPhoto = (Bitmap)in.readParcelable(null);
|
||||||
this.contactPhoto.set((Bitmap)in.readParcelable(null));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Uri getContactUri() {
|
public synchronized Uri getContactUri() {
|
||||||
return this.contactUri.get();
|
return this.contactUri;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public synchronized String getName() {
|
||||||
return name.get();
|
return this.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getNumber() {
|
public String getNumber() {
|
||||||
@ -121,28 +122,27 @@ public class Recipient implements Parcelable {
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
public synchronized void setListener(RecipientModifiedListener listener) {
|
public synchronized void addListener(RecipientModifiedListener listener) {
|
||||||
this.listener = listener;
|
listeners.add(listener);
|
||||||
if (asynchronousUpdateComplete) {
|
|
||||||
if (listener != null)
|
|
||||||
listener.onModified(this);
|
|
||||||
asynchronousUpdateComplete = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeToParcel(Parcel dest, int flags) {
|
public synchronized void removeListener(RecipientModifiedListener listener) {
|
||||||
|
listeners.remove(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void writeToParcel(Parcel dest, int flags) {
|
||||||
dest.writeString(number);
|
dest.writeString(number);
|
||||||
dest.writeString(name.get());
|
dest.writeString(name);
|
||||||
dest.writeParcelable(contactUri.get(), 0);
|
dest.writeParcelable(contactUri, 0);
|
||||||
dest.writeParcelable(contactPhoto.get(), 0);
|
dest.writeParcelable(contactPhoto, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toShortString() {
|
public synchronized String toShortString() {
|
||||||
return (name.get() == null ? number : name.get());
|
return (name == null ? number : name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitmap getContactPhoto() {
|
public synchronized Bitmap getContactPhoto() {
|
||||||
return contactPhoto.get();
|
return contactPhoto;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static interface RecipientModifiedListener {
|
public static interface RecipientModifiedListener {
|
||||||
|
@ -21,7 +21,6 @@ import android.database.Cursor;
|
|||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Process;
|
|
||||||
import android.provider.ContactsContract;
|
import android.provider.ContactsContract;
|
||||||
import android.provider.ContactsContract.Contacts;
|
import android.provider.ContactsContract.Contacts;
|
||||||
import android.provider.ContactsContract.PhoneLookup;
|
import android.provider.ContactsContract.PhoneLookup;
|
||||||
@ -100,7 +99,7 @@ public class RecipientProvider {
|
|||||||
Callable<RecipientDetails> task = new Callable<RecipientDetails>() {
|
Callable<RecipientDetails> task = new Callable<RecipientDetails>() {
|
||||||
@Override
|
@Override
|
||||||
public RecipientDetails call() throws Exception {
|
public RecipientDetails call() throws Exception {
|
||||||
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
|
// Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
|
||||||
return getRecipientDetails(context, number);
|
return getRecipientDetails(context, number);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -69,9 +69,15 @@ public class Recipients implements Parcelable {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setListener(RecipientModifiedListener listener) {
|
public void addListener(RecipientModifiedListener listener) {
|
||||||
for (Recipient recipient : recipients) {
|
for (Recipient recipient : recipients) {
|
||||||
recipient.setListener(listener);
|
recipient.addListener(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeListener(RecipientModifiedListener listener) {
|
||||||
|
for (Recipient recipient : recipients) {
|
||||||
|
recipient.removeListener(listener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +67,17 @@ public class Util {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static ExecutorService newSingleThreadedLifoExecutor() {
|
public static ExecutorService newSingleThreadedLifoExecutor() {
|
||||||
return new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingLifoQueue<Runnable>());
|
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingLifoQueue<Runnable>());
|
||||||
|
|
||||||
|
executor.execute(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
// Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
|
||||||
|
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return executor;
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static Bitmap loadScaledBitmap(InputStream src, int targetWidth, int targetHeight) {
|
// public static Bitmap loadScaledBitmap(InputStream src, int targetWidth, int targetHeight) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user