Fixed avatar stutter

Refs #61
This commit is contained in:
M66B
2018-09-08 17:04:10 +00:00
parent ef44555115
commit 765e2ba725
6 changed files with 1005 additions and 43 deletions

View File

@@ -34,6 +34,7 @@ import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
@@ -49,6 +50,8 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.mail.internet.InternetAddress;
@@ -69,6 +72,8 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
private boolean avatars;
private boolean debug;
private ExecutorService executor = Executors.newCachedThreadPool(Helper.backgroundThreadFactory);
private DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.LONG);
enum ViewType {UNIFIED, FOLDER, THREAD, SEARCH}
@@ -135,52 +140,42 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
private void bindTo(final TupleMessageEx message) {
pbLoading.setVisibility(View.GONE);
ivAvatar.setVisibility(View.GONE);
if (message.avatar != null) {
ContentResolver resolver = context.getContentResolver();
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(resolver, Uri.parse(message.avatar));
ivAvatar.setImageDrawable(Drawable.createFromStream(is, "avatar"));
}
ivAvatar.setVisibility(message.avatar == null ? View.GONE : View.VISIBLE);
if (avatars && message.from != null && message.from.length > 0) {
itemView.setHasTransientState(true);
Bundle args = new Bundle();
args.putSerializable("from", message.from[0]);
new SimpleTask<Drawable>() {
final long id = message.id;
final String email = ((InternetAddress) message.from[0]).getAddress();
executor.submit(new Runnable() {
@Override
protected Drawable onLoad(Context context, Bundle args) {
Cursor cursor = null;
public void run() {
try {
InternetAddress from = (InternetAddress) args.getSerializable("from");
ContentResolver resolver = context.getContentResolver();
cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Photo.CONTACT_ID},
ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
new String[]{from.getAddress()}, null);
if (cursor.moveToNext()) {
int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
long contactId = cursor.getLong(colContactId);
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
return Drawable.createFromStream(is, from.getPersonal());
Cursor cursor = null;
try {
ContentResolver resolver = context.getContentResolver();
cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Photo.CONTACT_ID},
ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
new String[]{email}, null);
if (cursor.moveToNext()) {
int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
long contactId = cursor.getLong(colContactId);
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
DB.getInstance(context).message().setMessageAvatar(id, uri.toString());
}
} finally {
if (cursor != null)
cursor.close();
}
} finally {
if (cursor != null)
cursor.close();
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}
return null;
}
@Override
protected void onLoaded(Bundle args, Drawable photo) {
if (photo != null) {
ivAvatar.setImageDrawable(photo);
ivAvatar.setVisibility(View.VISIBLE);
}
itemView.setHasTransientState(false);
}
@Override
protected void onException(Bundle args, Throwable ex) {
itemView.setHasTransientState(false);
}
}.load(context, owner, args);
});
}
ivFlagged.setVisibility(message.ui_flagged ? View.VISIBLE : View.GONE);
@@ -343,7 +338,7 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
this.avatars = (prefs.getBoolean("avatars", false) &&
this.avatars = (prefs.getBoolean("avatars", true) &&
ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED);
this.debug = prefs.getBoolean("debug", false);
}

View File

@@ -45,7 +45,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase;
// https://developer.android.com/topic/libraries/architecture/room.html
@Database(
version = 12,
version = 13,
entities = {
EntityIdentity.class,
EntityAccount.class,
@@ -199,6 +199,13 @@ public abstract class DB extends RoomDatabase {
db.execSQL("ALTER TABLE `message` ADD COLUMN `ui_flagged` INTEGER NOT NULL DEFAULT 0");
}
})
.addMigrations(new Migration(12, 13) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `message` ADD COLUMN `avatar` TEXT");
}
})
.build();
}

View File

@@ -177,6 +177,12 @@ public interface DaoMessage {
@Query("UPDATE message SET headers = :headers WHERE id = :id")
int setMessageHeaders(long id, String headers);
@Query("UPDATE message SET avatar = :avatar WHERE id = :id")
int setMessageAvatar(long id, String avatar);
@Query("UPDATE message SET avatar = NULL")
void clearMessageAvatars();
@Query("DELETE FROM message WHERE id = :id")
int deleteMessage(long id);

View File

@@ -82,6 +82,7 @@ public class EntityMessage implements Serializable {
public String references;
public String inreplyto;
public String thread; // compose = null
public String avatar; // URI
public Address[] from;
public Address[] to;
public Address[] cc;
@@ -186,6 +187,7 @@ public class EntityMessage implements Serializable {
(this.references == null ? other.references == null : this.references.equals(other.references)) &&
(this.inreplyto == null ? other.inreplyto == null : this.inreplyto.equals(other.inreplyto)) &&
(this.thread == null ? other.thread == null : this.thread.equals(other.thread)) &&
(this.avatar == null ? other.avatar == null : this.avatar.equals(other.avatar)) &&
equal(this.from, other.from) &&
equal(this.to, other.to) &&
equal(this.cc, other.cc) &&

View File

@@ -23,6 +23,7 @@ import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -84,11 +85,19 @@ public class FragmentOptions extends FragmentEx {
}
});
cbAvatars.setChecked(prefs.getBoolean("avatars", false));
cbAvatars.setChecked(prefs.getBoolean("avatars", true));
cbAvatars.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("avatars", checked).apply();
if (!checked)
new Thread(new Runnable() {
@Override
public void run() {
Log.i(Helper.TAG, "Clearing avatars");
DB.getInstance(getContext()).message().clearMessageAvatars();
}
}).start();
}
});