Show Code-Review votes in change list

If a change has several Code-Review votes with different values, icons
for all occurring values are shown in the change list.

Change-Id: Ic6454f974025f5fcb7e7d0be002c7bfebf253242
Signed-off-by: Edwin Kempin <ekempin@google.com>
diff --git a/app/src/main/java/com/google/reviewit/util/LayoutUtil.java b/app/src/main/java/com/google/reviewit/util/LayoutUtil.java
index ff19c94..b3993eb 100644
--- a/app/src/main/java/com/google/reviewit/util/LayoutUtil.java
+++ b/app/src/main/java/com/google/reviewit/util/LayoutUtil.java
@@ -39,6 +39,10 @@
         ViewGroup.LayoutParams.MATCH_PARENT, height);
   }
 
+  public static ViewGroup.LayoutParams fixedLayout(int width, int height) {
+    return new ViewGroup.LayoutParams(width, height);
+  }
+
   public static TableLayout.LayoutParams matchAndWrapTableLayout() {
     return new TableLayout.LayoutParams(
         TableLayout.LayoutParams.MATCH_PARENT,
diff --git a/app/src/main/java/com/google/reviewit/widget/ChangeEntry.java b/app/src/main/java/com/google/reviewit/widget/ChangeEntry.java
index cfd5e37..8226434 100644
--- a/app/src/main/java/com/google/reviewit/widget/ChangeEntry.java
+++ b/app/src/main/java/com/google/reviewit/widget/ChangeEntry.java
@@ -46,5 +46,7 @@
 
     ChangeInfo info = change.info;
     WidgetUtil.setText(findViewById(R.id.subject), info.subject);
+
+    ((CodeReviewVotes)findViewById(R.id.codeReviewVotes)).init(change);
   }
 }
diff --git a/app/src/main/java/com/google/reviewit/widget/CodeReviewVotes.java b/app/src/main/java/com/google/reviewit/widget/CodeReviewVotes.java
new file mode 100644
index 0000000..4cd62ab
--- /dev/null
+++ b/app/src/main/java/com/google/reviewit/widget/CodeReviewVotes.java
@@ -0,0 +1,152 @@
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.reviewit.widget;
+
+import android.content.Context;
+import android.support.annotation.DrawableRes;
+import android.util.AttributeSet;
+import android.widget.ImageView;
+import android.widget.LinearLayout;
+
+import com.google.gerrit.extensions.common.ApprovalInfo;
+import com.google.reviewit.R;
+import com.google.reviewit.app.Change;
+import com.google.reviewit.util.WidgetUtil;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static com.google.reviewit.util.LayoutUtil.fixedLayout;
+import static com.google.reviewit.util.LayoutUtil.matchAndWrapLayout;
+import static com.google.reviewit.util.LayoutUtil.matchLayout;
+import static com.google.reviewit.util.WidgetUtil.setInvisible;
+
+public class CodeReviewVotes extends LinearLayout {
+
+  private final WidgetUtil widgetUtil;
+
+  public CodeReviewVotes(Context context) {
+    this(context, null, 0);
+  }
+
+  public CodeReviewVotes(Context context, AttributeSet attrs) {
+    this(context, attrs, 0);
+  }
+
+  public CodeReviewVotes(Context context, AttributeSet attrs, int defStyle) {
+    super(context, attrs, defStyle);
+    this.widgetUtil = new WidgetUtil(getContext());
+
+    setOrientation(VERTICAL);
+  }
+
+  public void init(Change change) {
+    List<ApprovalInfo> codeReviewApprovals =
+        (change.info.labels != null
+            && change.info.labels.get("Code-Review") != null)
+          ? change.info.labels.get("Code-Review").all : null;
+
+    if (codeReviewApprovals == null) {
+      setLayoutParams(new LinearLayout.LayoutParams(
+          widgetUtil.dpToPx(48), widgetUtil.dpToPx(48)));
+      return;
+    }
+
+    Set<Integer> values = new HashSet<>();
+    for (ApprovalInfo approval : codeReviewApprovals) {
+      values.add(approval.value);
+    }
+
+    values.remove(0);
+
+    if (values.isEmpty()) {
+      ImageView icon = createVoteIcon(0);
+      icon.setLayoutParams(matchLayout());
+      setInvisible(icon);
+      addView(icon);
+      return;
+    }
+
+    if (values.size() == 1) {
+      ImageView icon = createVoteIcon(values.iterator().next());
+      icon.setLayoutParams(matchLayout());
+      addView(icon);
+    } else {
+      LinearLayout plusIcons = new LinearLayout(getContext());
+      plusIcons.setLayoutParams(matchAndWrapLayout());
+      addView(plusIcons);
+
+      ImageView iconPlus2 = createVoteIcon(2);
+      iconPlus2.setLayoutParams(
+          fixedLayout(widgetUtil.dpToPx(24), widgetUtil.dpToPx(24)));
+      if (!values.contains(2)) {
+        setInvisible(iconPlus2);
+      }
+      plusIcons.addView(iconPlus2);
+
+      ImageView iconPlus1 = createVoteIcon(1);
+      iconPlus1.setLayoutParams(
+          fixedLayout(widgetUtil.dpToPx(24), widgetUtil.dpToPx(24)));
+      if (!values.contains(1)) {
+        setInvisible(iconPlus1);
+      }
+      plusIcons.addView(iconPlus1);
+
+      LinearLayout minusIcons = new LinearLayout(getContext());
+      minusIcons.setLayoutParams(matchAndWrapLayout());
+      addView(minusIcons);
+
+      ImageView iconMinus2 = createVoteIcon(-2);
+      iconMinus2.setLayoutParams(
+          fixedLayout(widgetUtil.dpToPx(24), widgetUtil.dpToPx(24)));
+      if (!values.contains(-2)) {
+        setInvisible(iconMinus2);
+      }
+      minusIcons.addView(iconMinus2);
+
+      ImageView iconMinus1 = createVoteIcon(-1);
+      iconMinus1.setLayoutParams(
+          fixedLayout(widgetUtil.dpToPx(24), widgetUtil.dpToPx(24)));
+      if (!values.contains(-1)) {
+        setInvisible(iconMinus1);
+      }
+      minusIcons.addView(iconMinus1);
+    }
+  }
+
+  private ImageView createVoteIcon(int codeReviewVote) {
+    ImageView icon = new ImageView(getContext());
+    @DrawableRes int iconRes;
+    if (codeReviewVote <= -2) {
+      icon.setColorFilter(widgetUtil.color(R.color.votingNegativeSelected));
+      iconRes = R.drawable.ic_sentiment_very_dissatisfied_white_48dp;
+    } else if (codeReviewVote == -1) {
+      icon.setColorFilter(widgetUtil.color(R.color.votingNegative));
+      iconRes = R.drawable.ic_sentiment_dissatisfied_white_48dp;
+    } else if (codeReviewVote == 0) {
+      icon.setColorFilter(widgetUtil.color(R.color.votingNeutral));
+      iconRes = R.drawable.ic_sentiment_neutral_white_48dp;
+    } else if (codeReviewVote == 1) {
+      icon.setColorFilter(widgetUtil.color(R.color.votingPositive));
+      iconRes = R.drawable.ic_sentiment_satisfied_white_48dp;
+    } else {
+      icon.setColorFilter(widgetUtil.color(R.color.votingPositiveSelected));
+      iconRes = R.drawable.ic_sentiment_very_satisfied_white_48dp;
+    }
+    icon.setImageDrawable(widgetUtil.getDrawable(iconRes));
+    return icon;
+  }
+}
diff --git a/app/src/main/res/layout/change_entry.xml b/app/src/main/res/layout/change_entry.xml
index 411470d..745a02f 100644
--- a/app/src/main/res/layout/change_entry.xml
+++ b/app/src/main/res/layout/change_entry.xml
@@ -35,17 +35,34 @@
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>
 
-    <TextView
-      android:id="@+id/subject"
-      android:layout_width="wrap_content"
-      android:layout_height="wrap_content"/>
-
-    <com.google.reviewit.widget.UserView
-      android:id="@+id/owner"
+    <LinearLayout
+      android:orientation="horizontal"
       android:layout_width="match_parent"
-      android:layout_height="wrap_content"
-      app:textSize="10sp"
-      app:avatarSize="15dp"/>
-  </LinearLayout>
+      android:layout_height="wrap_content">
 
+      <LinearLayout
+        android:orientation="vertical"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_weight="1">
+
+        <TextView
+          android:id="@+id/subject"
+          android:layout_width="wrap_content"
+          android:layout_height="wrap_content"/>
+
+        <com.google.reviewit.widget.UserView
+          android:id="@+id/owner"
+          android:layout_width="match_parent"
+          android:layout_height="wrap_content"
+          app:textSize="10sp"
+          app:avatarSize="15dp"/>
+      </LinearLayout>
+
+      <com.google.reviewit.widget.CodeReviewVotes
+        android:id="@+id/codeReviewVotes"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"/>
+    </LinearLayout>
+  </LinearLayout>
 </LinearLayout>
\ No newline at end of file