From 6c81d74debfa2d8132b0d532f77122ce6639ddb3 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Sun, 25 Feb 2018 20:12:01 -0400 Subject: [PATCH] - Gravatars are loaded in Contact List and Contact Selection List --- app/build.gradle | 2 + .../2.json | 12 ++++- .../agorise/crystalwallet/dao/ContactDao.java | 2 +- .../agorise/crystalwallet/models/Contact.java | 4 +- .../util/CircleTransformation.java | 47 +++++++++++++++++++ .../agorise/crystalwallet/util/MD5Hash.java | 22 +++++++++ .../views/ContactSelectionViewHolder.java | 14 ++++++ .../views/ContactViewHolder.java | 15 ++++++ 8 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/util/CircleTransformation.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/util/MD5Hash.java diff --git a/app/build.gradle b/app/build.gradle index c79b2db..44b82fd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -63,4 +63,6 @@ dependencies { annotationProcessor 'android.arch.lifecycle:compiler:1.0.0' annotationProcessor 'android.arch.persistence.room:compiler:1.0.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' + + compile 'com.squareup.picasso:picasso:2.5.2' } diff --git a/app/schemas/cy.agorise.crystalwallet.dao.CrystalDatabase/2.json b/app/schemas/cy.agorise.crystalwallet.dao.CrystalDatabase/2.json index 3153165..22f45c4 100644 --- a/app/schemas/cy.agorise.crystalwallet.dao.CrystalDatabase/2.json +++ b/app/schemas/cy.agorise.crystalwallet.dao.CrystalDatabase/2.json @@ -2,7 +2,7 @@ "formatVersion": 1, "database": { "version": 2, - "identityHash": "3ec53bc71906b5abcabeaefcd7505bc1", + "identityHash": "22cb2a56b28a9f7088ec98d6a72f9f67", "entities": [ { "tableName": "account_seed", @@ -284,6 +284,14 @@ "name" ], "createSql": "CREATE UNIQUE INDEX `index_contact_name` ON `${TABLE_NAME}` (`name`)" + }, + { + "name": "index_contact_email", + "unique": false, + "columnNames": [ + "email" + ], + "createSql": "CREATE INDEX `index_contact_email` ON `${TABLE_NAME}` (`email`)" } ], "foreignKeys": [] @@ -684,7 +692,7 @@ ], "setupQueries": [ "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", - "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"3ec53bc71906b5abcabeaefcd7505bc1\")" + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"22cb2a56b28a9f7088ec98d6a72f9f67\")" ] } } \ No newline at end of file diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/ContactDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/ContactDao.java index 84cab75..97d1e51 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/ContactDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/ContactDao.java @@ -27,7 +27,7 @@ public interface ContactDao { @Query("SELECT * FROM contact ORDER BY name ASC") LivePagedListProvider contactsByName(); - @Query("SELECT c.* FROM contact c WHERE c.id IN (SELECT DISTINCT(ca.contact_id) FROM contact_address ca WHERE ca.crypto_net == :cryptoNet) ORDER BY name ASC") + @Query("SELECT c.* FROM contact c WHERE c.id IN (SELECT DISTINCT(ca.contact_id) FROM contact_address ca WHERE ca.crypto_net == :cryptoNet) ORDER BY name ASC, email ASC") LivePagedListProvider contactsByNameAndCryptoNet(String cryptoNet); @Query("SELECT * FROM contact WHERE id = :id") diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/Contact.java b/app/src/main/java/cy/agorise/crystalwallet/models/Contact.java index 7700227..be43fb9 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/Contact.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/Contact.java @@ -22,7 +22,7 @@ import cy.agorise.crystalwallet.enums.CryptoNet; */ @Entity(tableName="contact", - indices = {@Index("id"),@Index(value = {"name"}, unique=true)}) + indices = {@Index("id"),@Index(value = {"name"}, unique=true),@Index("email")}) public class Contact { /** @@ -69,7 +69,7 @@ public class Contact { } public String getEmail() { - return mEmail; + return this.mEmail; } public void setEmail(String email) { diff --git a/app/src/main/java/cy/agorise/crystalwallet/util/CircleTransformation.java b/app/src/main/java/cy/agorise/crystalwallet/util/CircleTransformation.java new file mode 100644 index 0000000..e31ce57 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/util/CircleTransformation.java @@ -0,0 +1,47 @@ +package cy.agorise.crystalwallet.util; + +/** + * Created by Henry Varona on 25/2/2018. + */ + +import android.graphics.Bitmap; +import android.graphics.BitmapShader; +import android.graphics.Canvas; +import android.graphics.Paint; + +import com.squareup.picasso.Transformation; + +public class CircleTransformation implements Transformation { + @Override + public Bitmap transform(Bitmap source) { + int size = Math.min(source.getWidth(), source.getHeight()); + + int x = (source.getWidth() - size) / 2; + int y = (source.getHeight() - size) / 2; + + Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size); + if (squaredBitmap != source) { + source.recycle(); + } + + Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig()); + + Canvas canvas = new Canvas(bitmap); + Paint paint = new Paint(); + BitmapShader shader = new BitmapShader(squaredBitmap, + BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); + paint.setShader(shader); + paint.setAntiAlias(true); + + float r = size / 2f; + canvas.drawCircle(r, r, r, paint); + + squaredBitmap.recycle(); + return bitmap; + } + + @Override + public String key() { + return "CircleTransformation"; + } +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/util/MD5Hash.java b/app/src/main/java/cy/agorise/crystalwallet/util/MD5Hash.java new file mode 100644 index 0000000..38e6534 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/util/MD5Hash.java @@ -0,0 +1,22 @@ +package cy.agorise.crystalwallet.util; + +/** + * Created by Henry Varona on 24/2/2018. + */ + +public class MD5Hash { + + static public String hash(String md5) { + try { + java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5"); + byte[] array = md.digest(md5.getBytes()); + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < array.length; ++i) { + sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3)); + } + return sb.toString(); + } catch (java.security.NoSuchAlgorithmException e) { + } + return null; + } +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/ContactSelectionViewHolder.java b/app/src/main/java/cy/agorise/crystalwallet/views/ContactSelectionViewHolder.java index c6ed3f0..797ae02 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/ContactSelectionViewHolder.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/ContactSelectionViewHolder.java @@ -9,10 +9,14 @@ import android.view.View; import android.widget.ImageView; import android.widget.TextView; +import com.squareup.picasso.Picasso; + import cy.agorise.crystalwallet.R; import cy.agorise.crystalwallet.activities.CreateContactActivity; import cy.agorise.crystalwallet.dao.CrystalDatabase; import cy.agorise.crystalwallet.models.Contact; +import cy.agorise.crystalwallet.util.CircleTransformation; +import cy.agorise.crystalwallet.util.MD5Hash; /** * Created by Henry Varona on 2/16/2018. @@ -62,6 +66,16 @@ public class ContactSelectionViewHolder extends RecyclerView.ViewHolder { tvName.setText(contact.getName()); tvLastPaid.setText("Paid: 1 Jan, 2001 01:01"); + if (contact.getEmail() != null){ + String emailHash = MD5Hash.hash(contact.getEmail()); + String gravatarUrl = "http://www.gravatar.com/avatar/" + emailHash + "?s=204&d=404"; + + Picasso.with(this.context) + .load(gravatarUrl) + .transform(new CircleTransformation()) + .into(ivThumbnail); + } + this.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/ContactViewHolder.java b/app/src/main/java/cy/agorise/crystalwallet/views/ContactViewHolder.java index a7cc1b7..376ce11 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/ContactViewHolder.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/ContactViewHolder.java @@ -6,14 +6,19 @@ import android.content.Intent; import android.provider.ContactsContract; import android.support.v7.app.AlertDialog; import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.ThemedSpinnerAdapter; import android.view.View; import android.widget.ImageView; import android.widget.TextView; +import com.squareup.picasso.Picasso; + import cy.agorise.crystalwallet.R; import cy.agorise.crystalwallet.activities.CreateContactActivity; import cy.agorise.crystalwallet.dao.CrystalDatabase; import cy.agorise.crystalwallet.models.Contact; +import cy.agorise.crystalwallet.util.CircleTransformation; +import cy.agorise.crystalwallet.util.MD5Hash; /** * Created by Henry Varona on 1/17/2017. @@ -67,6 +72,16 @@ public class ContactViewHolder extends RecyclerView.ViewHolder { } }); + if (contact.getEmail() != null){ + String emailHash = MD5Hash.hash(contact.getEmail()); + String gravatarUrl = "http://www.gravatar.com/avatar/" + emailHash + "?s=204&d=404"; + + Picasso.with(this.context) + .load(gravatarUrl) + .transform(new CircleTransformation()) + .into(ivThumbnail); + } + this.ivDeleteContact.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {