Refactor: Implement expandable commit message as view

The post review screen shows an expandable commit message. We want to
use the same widget on the abandon and restore screens. If the
expandable commit message is implemented as view we can easily reuse
it.

Change-Id: Id13188a520782ff8404e4fcb822befeccffb7229
Signed-off-by: Edwin Kempin <ekempin@google.com>
diff --git a/app/src/main/java/com/google/reviewit/PostReviewFragment.java b/app/src/main/java/com/google/reviewit/PostReviewFragment.java
index 7dc1560..b887e48 100644
--- a/app/src/main/java/com/google/reviewit/PostReviewFragment.java
+++ b/app/src/main/java/com/google/reviewit/PostReviewFragment.java
@@ -26,8 +26,8 @@
 import com.google.reviewit.app.Change;
 import com.google.reviewit.util.ObservableAsynTask;
 import com.google.reviewit.util.TaskObserver;
-import com.google.reviewit.util.WidgetUtil;
 import com.google.reviewit.widget.ApprovalsView;
+import com.google.reviewit.widget.ExpandableCommitMessageView;
 import com.google.reviewit.widget.VoteView;
 import com.urswolfer.gerrit.client.rest.http.HttpStatusException;
 
@@ -75,32 +75,13 @@
 
     setTitle(getString(R.string.detailed_change_title, change.info._number));
     init(change);
-    WidgetUtil.setText(v(R.id.subject), change.info.subject);
     initLabels(change, vote);
     ((ApprovalsView) v(R.id.approvals)).displayApprovals(getApp(),
         change.info, this);
   }
 
   private void init(final Change change) {
-    v(R.id.expandCommitMessage).setOnClickListener(new View.OnClickListener() {
-      @Override
-      public void onClick(View v) {
-        setGone(v(R.id.expandCommitMessage));
-        setVisible(v(R.id.collapseCommitMessage));
-        WidgetUtil.setText(v(R.id.subject),
-            change.currentRevision().commit.message);
-      }
-    });
-
-    v(R.id.collapseCommitMessage).setOnClickListener(
-        new View.OnClickListener() {
-      @Override
-      public void onClick(View v) {
-        setGone(v(R.id.collapseCommitMessage));
-        setVisible(v(R.id.expandCommitMessage));
-        WidgetUtil.setText(v(R.id.subject), change.info.subject);
-      }
-    });
+    ((ExpandableCommitMessageView)v(R.id.commitMessage)).init(change);
 
     v(R.id.postReviewButton).setOnClickListener(new View.OnClickListener() {
       @Override
diff --git a/app/src/main/java/com/google/reviewit/widget/ExpandableCommitMessageView.java b/app/src/main/java/com/google/reviewit/widget/ExpandableCommitMessageView.java
new file mode 100644
index 0000000..08ffca3
--- /dev/null
+++ b/app/src/main/java/com/google/reviewit/widget/ExpandableCommitMessageView.java
@@ -0,0 +1,69 @@
+// 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.util.AttributeSet;
+import android.view.View;
+import android.widget.LinearLayout;
+
+import com.google.reviewit.R;
+import com.google.reviewit.app.Change;
+import com.google.reviewit.util.WidgetUtil;
+
+import static com.google.reviewit.util.WidgetUtil.setGone;
+import static com.google.reviewit.util.WidgetUtil.setVisible;
+
+public class ExpandableCommitMessageView extends LinearLayout {
+  public ExpandableCommitMessageView(Context context) {
+    this(context, null, 0);
+  }
+
+  public ExpandableCommitMessageView(Context context, AttributeSet attrs) {
+    this(context, attrs, 0);
+  }
+
+  public ExpandableCommitMessageView(
+      Context context, AttributeSet attrs, int defStyle) {
+    super(context, attrs, defStyle);
+
+    inflate(context, R.layout.expandable_commit_message, this);
+  }
+
+  public void init(final Change change) {
+    WidgetUtil.setText(findViewById(R.id.subject), change.info.subject);
+
+    findViewById(R.id.expandCommitMessage).setOnClickListener(
+        new View.OnClickListener() {
+          @Override
+          public void onClick(View v) {
+            setGone(findViewById(R.id.expandCommitMessage));
+            setVisible(findViewById(R.id.collapseCommitMessage));
+            WidgetUtil.setText(findViewById(R.id.subject),
+                change.currentRevision().commit.message);
+          }
+    });
+
+    findViewById(R.id.collapseCommitMessage).setOnClickListener(
+        new View.OnClickListener() {
+          @Override
+          public void onClick(View v) {
+            setGone(findViewById(R.id.collapseCommitMessage));
+            setVisible(findViewById(R.id.expandCommitMessage));
+            WidgetUtil.setText(findViewById(R.id.subject), change.info.subject);
+          }
+        });
+  }
+}
diff --git a/app/src/main/res/layout/content_post_review.xml b/app/src/main/res/layout/content_post_review.xml
index 0347ae0..6c2f411 100644
--- a/app/src/main/res/layout/content_post_review.xml
+++ b/app/src/main/res/layout/content_post_review.xml
@@ -32,41 +32,11 @@
 
     <include layout="@layout/progress"/>
 
-    <LinearLayout
+    <com.google.reviewit.widget.ExpandableCommitMessageView
+      android:id="@+id/commitMessage"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
-      android:paddingTop="5dp"
-      android:orientation="horizontal">
-
-      <ImageView
-        android:id="@+id/expandCommitMessage"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_marginTop="-7dp"
-        android:clickable="true"
-        android:src="@drawable/ic_keyboard_arrow_right_white_36dp"/>
-
-      <ImageView
-        android:id="@+id/collapseCommitMessage"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_marginTop="-7dp"
-        android:clickable="true"
-        android:src="@drawable/ic_keyboard_arrow_down_white_36dp"
-        android:visibility="gone"/>
-
-      <TextView
-        android:id="@+id/subject"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:padding="5dp"
-        android:layout_marginBottom="5dp"
-        android:layout_marginRight="36dp"
-        android:layout_weight="1"
-        android:textSize="9sp"
-        android:background="@drawable/grey_page_border"
-        android:textColor="@color/darkGrey"/>
-    </LinearLayout>
+      android:paddingTop="5dp"/>
 
     <LinearLayout
       android:layout_width="match_parent"
diff --git a/app/src/main/res/layout/expandable_commit_message.xml b/app/src/main/res/layout/expandable_commit_message.xml
new file mode 100644
index 0000000..dbc2e07
--- /dev/null
+++ b/app/src/main/res/layout/expandable_commit_message.xml
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!-- 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. -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+  android:layout_width="match_parent"
+  android:layout_height="wrap_content"
+  android:orientation="horizontal">
+
+  <ImageView
+    android:id="@+id/expandCommitMessage"
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    android:layout_marginTop="-7dp"
+    android:clickable="true"
+    android:src="@drawable/ic_keyboard_arrow_right_white_36dp"/>
+
+  <ImageView
+    android:id="@+id/collapseCommitMessage"
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    android:layout_marginTop="-7dp"
+    android:clickable="true"
+    android:src="@drawable/ic_keyboard_arrow_down_white_36dp"
+    android:visibility="gone"/>
+
+  <TextView
+    android:id="@+id/subject"
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    android:padding="5dp"
+    android:layout_marginBottom="5dp"
+    android:layout_marginRight="36dp"
+    android:layout_weight="1"
+    android:textSize="9sp"
+    android:background="@drawable/grey_page_border"
+    android:textColor="@color/darkGrey"/>
+</LinearLayout>