From 35c59fdb39ef0447d7919544d6fb8907b8024d02 Mon Sep 17 00:00:00 2001
From: str4d <str4d@mail.i2p>
Date: Tue, 5 Jan 2016 07:19:10 +0000
Subject: [PATCH] Migrate AuthorView to XML, add identicon

---
 briar-android/res/layout/author_view.xml      | 42 +++++++++++
 .../android/forum/ForumActivity.java          |  6 +-
 .../android/forum/ForumAdapter.java           |  3 +-
 .../android/forum/ReadForumPostActivity.java  | 15 ++--
 .../briarproject/android/util/AuthorView.java | 72 +++++++++++--------
 5 files changed, 102 insertions(+), 36 deletions(-)
 create mode 100644 briar-android/res/layout/author_view.xml

diff --git a/briar-android/res/layout/author_view.xml b/briar-android/res/layout/author_view.xml
new file mode 100644
index 0000000000..a3e473dc78
--- /dev/null
+++ b/briar-android/res/layout/author_view.xml
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout
+	xmlns:android="http://schemas.android.com/apk/res/android"
+	android:layout_width="match_parent"
+	android:layout_height="match_parent">
+
+	<im.delight.android.identicons.SymmetricIdenticon
+		android:id="@+id/identiconView"
+		android:layout_width="@dimen/listitem_picture_size"
+		android:layout_height="@dimen/listitem_picture_size"
+		android:layout_centerVertical="true"
+		android:layout_marginLeft="@dimen/listitem_horizontal_margin"
+		android:layout_marginStart="@dimen/listitem_horizontal_margin"/>
+
+	<ImageView
+		android:id="@+id/statusView"
+		android:layout_width="wrap_content"
+		android:layout_height="wrap_content"
+		android:layout_alignParentEnd="true"
+		android:layout_alignParentRight="true"
+		android:layout_centerVertical="true"
+		android:src="@drawable/identity_anonymous"/>
+
+	<TextView
+		android:id="@+id/nameView"
+		android:layout_width="wrap_content"
+		android:layout_height="wrap_content"
+		android:layout_alignParentLeft="true"
+		android:layout_alignParentStart="true"
+		android:layout_centerVertical="true"
+		android:layout_marginEnd="@dimen/margin_small"
+		android:layout_marginLeft="@dimen/listitem_text_left_margin"
+		android:layout_marginRight="@dimen/margin_small"
+		android:layout_marginStart="@dimen/listitem_text_left_margin"
+		android:layout_toLeftOf="@id/statusView"
+		android:layout_toStartOf="@id/statusView"
+		android:ellipsize="end"
+		android:singleLine="true"
+		android:text="@string/anonymous"
+		android:textSize="@dimen/text_size_medium"/>
+
+</RelativeLayout>
\ No newline at end of file
diff --git a/briar-android/src/org/briarproject/android/forum/ForumActivity.java b/briar-android/src/org/briarproject/android/forum/ForumActivity.java
index 7f725d40d2..a67ebd12e7 100644
--- a/briar-android/src/org/briarproject/android/forum/ForumActivity.java
+++ b/briar-android/src/org/briarproject/android/forum/ForumActivity.java
@@ -18,6 +18,7 @@ import org.briarproject.android.util.ElasticHorizontalSpace;
 import org.briarproject.android.util.HorizontalBorder;
 import org.briarproject.android.util.ListLoadingProgressBar;
 import org.briarproject.api.android.AndroidNotificationManager;
+import org.briarproject.api.android.ReferenceManager;
 import org.briarproject.api.db.DbException;
 import org.briarproject.api.db.NoSuchMessageException;
 import org.briarproject.api.db.NoSuchSubscriptionException;
@@ -70,6 +71,8 @@ public class ForumActivity extends BriarActivity implements EventListener,
 	private ListLoadingProgressBar loading = null;
 	private ImageButton composeButton = null, shareButton = null;
 
+	@Inject private ReferenceManager referenceManager;
+
 	// Fields that are accessed from background threads must be volatile
 	@Inject private volatile ForumManager forumManager;
 	@Inject private volatile EventBus eventBus;
@@ -366,7 +369,8 @@ public class ForumActivity extends BriarActivity implements EventListener,
 		i.putExtra("briar.FORUM_NAME", forum.getName());
 		i.putExtra("briar.MESSAGE_ID", header.getId().getBytes());
 		Author author = header.getAuthor();
-		if (author != null) i.putExtra("briar.AUTHOR_NAME", author.getName());
+		if (author != null) i.putExtra("briar.AUTHOR_HANDLE",
+				referenceManager.putReference(author, Author.class));
 		i.putExtra("briar.AUTHOR_STATUS", header.getAuthorStatus().name());
 		i.putExtra("briar.CONTENT_TYPE", header.getContentType());
 		i.putExtra("briar.TIMESTAMP", header.getTimestamp());
diff --git a/briar-android/src/org/briarproject/android/forum/ForumAdapter.java b/briar-android/src/org/briarproject/android/forum/ForumAdapter.java
index ac12114474..30ff7d0bf8 100644
--- a/briar-android/src/org/briarproject/android/forum/ForumAdapter.java
+++ b/briar-android/src/org/briarproject/android/forum/ForumAdapter.java
@@ -55,8 +55,7 @@ class ForumAdapter extends ArrayAdapter<ForumItem> {
 		AuthorView authorView = new AuthorView(ctx);
 		authorView.setLayoutParams(WRAP_WRAP_1);
 		Author author = header.getAuthor();
-		if (author == null) authorView.init(null, header.getAuthorStatus());
-		else authorView.init(author.getName(), header.getAuthorStatus());
+		authorView.init(author, header.getAuthorStatus());
 		headerLayout.addView(authorView);
 
 		TextView date = new TextView(ctx);
diff --git a/briar-android/src/org/briarproject/android/forum/ReadForumPostActivity.java b/briar-android/src/org/briarproject/android/forum/ReadForumPostActivity.java
index 706cb31fe0..5ffc8aaa89 100644
--- a/briar-android/src/org/briarproject/android/forum/ReadForumPostActivity.java
+++ b/briar-android/src/org/briarproject/android/forum/ReadForumPostActivity.java
@@ -17,6 +17,7 @@ import org.briarproject.android.util.AuthorView;
 import org.briarproject.android.util.ElasticHorizontalSpace;
 import org.briarproject.android.util.HorizontalBorder;
 import org.briarproject.android.util.LayoutUtils;
+import org.briarproject.api.android.ReferenceManager;
 import org.briarproject.api.db.DbException;
 import org.briarproject.api.db.NoSuchMessageException;
 import org.briarproject.api.forum.ForumManager;
@@ -56,6 +57,8 @@ implements OnClickListener {
 	private TextView content = null;
 	private int position = -1;
 
+	@Inject private ReferenceManager referenceManager;
+
 	// Fields that are accessed from background threads must be volatile
 	@Inject private volatile ForumManager forumManager;
 	private volatile MessageId messageId = null;
@@ -82,7 +85,9 @@ implements OnClickListener {
 		if (minTimestamp == -1) throw new IllegalStateException();
 		position = i.getIntExtra("briar.POSITION", -1);
 		if (position == -1) throw new IllegalStateException();
-		String authorName = i.getStringExtra("briar.AUTHOR_NAME");
+		long authorHandle = i.getLongExtra("briar.AUTHOR_HANDLE", -1);
+		if (authorHandle == -1) throw new IllegalStateException();
+		Author author = referenceManager.removeReference(authorHandle, Author.class);
 		String s = i.getStringExtra("briar.AUTHOR_STATUS");
 		if (s == null) throw new IllegalStateException();
 		Author.Status authorStatus = Author.Status.valueOf(s);
@@ -102,10 +107,10 @@ implements OnClickListener {
 		header.setOrientation(HORIZONTAL);
 		header.setGravity(CENTER_VERTICAL);
 
-		AuthorView author = new AuthorView(this);
-		author.setLayoutParams(WRAP_WRAP_1);
-		author.init(authorName, authorStatus);
-		header.addView(author);
+		AuthorView authorView = new AuthorView(this);
+		authorView.setLayoutParams(WRAP_WRAP_1);
+		authorView.init(author, authorStatus);
+		header.addView(authorView);
 
 		int pad = LayoutUtils.getPadding(this);
 
diff --git a/briar-android/src/org/briarproject/android/util/AuthorView.java b/briar-android/src/org/briarproject/android/util/AuthorView.java
index 152a801b91..166e8d63ba 100644
--- a/briar-android/src/org/briarproject/android/util/AuthorView.java
+++ b/briar-android/src/org/briarproject/android/util/AuthorView.java
@@ -1,42 +1,62 @@
 package org.briarproject.android.util;
 
 import android.content.Context;
+import android.util.AttributeSet;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.widget.FrameLayout;
 import android.widget.ImageView;
-import android.widget.RelativeLayout;
 import android.widget.TextView;
 
 import org.briarproject.R;
 import org.briarproject.api.identity.Author;
 
-import static android.text.TextUtils.TruncateAt.END;
+import im.delight.android.identicons.IdenticonView;
 
-public class AuthorView extends RelativeLayout {
+public class AuthorView extends FrameLayout {
+
+	private IdenticonView identiconView;
+	private TextView nameView;
+	private ImageView statusView;
 
 	public AuthorView(Context ctx) {
 		super(ctx);
+
+		initViews();
+	}
+
+	public AuthorView(Context context, AttributeSet attrs) {
+		super(context, attrs);
+
+		initViews();
+	}
+
+	public AuthorView(Context context, AttributeSet attrs,
+							 int defStyle) {
+		super(context, attrs, defStyle);
+
+		initViews();
 	}
 
-	public void init(String name, Author.Status status) {
-		Context ctx = getContext();
-		int pad = LayoutUtils.getPadding(ctx);
-
-		TextView nameView = new TextView(ctx);
-		nameView.setId(1);
-		nameView.setTextSize(18);
-		nameView.setSingleLine();
-		nameView.setEllipsize(END);
-		nameView.setPadding(pad, pad, pad, pad);
-		if (name == null) nameView.setText(R.string.anonymous);
-		else nameView.setText(name);
-		LayoutParams leftOf = CommonLayoutParams.relative();
-		leftOf.addRule(ALIGN_PARENT_LEFT);
-		leftOf.addRule(CENTER_VERTICAL);
-		leftOf.addRule(LEFT_OF, 2);
-		addView(nameView, leftOf);
-
-		ImageView statusView = new ImageView(ctx);
-		statusView.setId(2);
-		statusView.setPadding(0, pad, pad, pad);
+	private void initViews() {
+		if (isInEditMode())
+			return;
+
+		View v = LayoutInflater.from(getContext()).inflate(
+				R.layout.author_view, this, true);
+
+		identiconView = (IdenticonView) v.findViewById(R.id.identiconView);
+		nameView = (TextView) v.findViewById(R.id.nameView);
+		statusView = (ImageView) v.findViewById(R.id.statusView);
+	}
+
+	public void init(Author author, Author.Status status) {
+		if (author == null) nameView.setText(R.string.anonymous);
+		else {
+			identiconView.show(author.getId().getBytes());
+			nameView.setText(author.getName());
+		}
+
 		switch(status) {
 		case ANONYMOUS:
 			statusView.setImageResource(R.drawable.identity_anonymous);
@@ -51,9 +71,5 @@ public class AuthorView extends RelativeLayout {
 			statusView.setImageResource(R.drawable.identity_verified);
 			break;
 		}
-		LayoutParams right = CommonLayoutParams.relative();
-		right.addRule(ALIGN_PARENT_RIGHT);
-		right.addRule(CENTER_VERTICAL);
-		addView(statusView, right);
 	}
 }
-- 
GitLab