Extract code to linkify commit message into own class

This way the code can be reused.

Change-Id: I62b40fcf212c374d4573891b2731dcd7352dd7d2
Signed-off-by: Edwin Kempin <ekempin@google.com>
diff --git a/app/src/main/java/com/google/reviewit/DetailedChangeFragment.java b/app/src/main/java/com/google/reviewit/DetailedChangeFragment.java
index f8f427a..b1f8a36 100644
--- a/app/src/main/java/com/google/reviewit/DetailedChangeFragment.java
+++ b/app/src/main/java/com/google/reviewit/DetailedChangeFragment.java
@@ -19,19 +19,18 @@
 import android.os.Bundle;
 import android.support.annotation.LayoutRes;
 import android.support.v4.widget.SwipeRefreshLayout;
-import android.text.util.Linkify;
 import android.util.Log;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.MotionEvent;
-import android.widget.TextView;
 
 import com.google.gerrit.extensions.client.ChangeStatus;
 import com.google.reviewit.app.SortActionHandler;
 import com.google.gerrit.extensions.restapi.RestApiException;
 import com.google.reviewit.app.Change;
 import com.google.reviewit.util.ChangeUtil;
+import com.google.reviewit.util.Linkifier;
 import com.google.reviewit.widget.ChangeBox;
 import com.google.reviewit.util.FormatUtil;
 import com.google.reviewit.util.TaskObserver;
@@ -40,8 +39,6 @@
 import com.google.reviewit.widget.FileBox;
 import com.google.reviewit.widget.ZoomHandler;
 
-import java.util.regex.Pattern;
-
 import static com.google.reviewit.util.WidgetUtil.setVisible;
 
 /**
@@ -51,23 +48,6 @@
     OnBackPressedAware, DispatchTouchEventAware {
   private static final String TAG = DetailedChangeFragment.class.getName();
 
-  private static final Pattern PATTERN_CHANGE_ID =
-      Pattern.compile("I[0-9a-f]{5,40}");
-  private static final String PART_LINK = "(?:"
-      + "[a-zA-Z0-9$_+!*'%;:@=?#/~-]"
-      + "|&(?!lt;|gt;)"
-      + "|[.,](?!(?:\\s|$))"
-      + ")";
-  private static final Pattern PATTERN_LINK = Pattern.compile(
-      "https?://"
-      + PART_LINK + "{2,}"
-      + "(?:[(]" + PART_LINK + "*" + "[)])*"
-      + PART_LINK + "*");
-  private static final Pattern PATTERN_EMAIL =
-      Pattern.compile("[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}",
-          Pattern.CASE_INSENSITIVE);
-
-
   private ZoomHandler zoomHandler;
 
   @Override
@@ -101,15 +81,13 @@
             refresh(change);
           }
         });
-
-    tv(R.id.commitMessage).setLinksClickable(true);
   }
 
   private void display(Change change) {
     try {
       ChangeUtil.colorBackground(root, change);
       ((ChangeBox) v(R.id.changeBox)).display(getApp(), change);
-      linkify();
+      (new Linkifier(getApp())).linkifyCommitMessage(tv(R.id.commitMessage));
       displayChangeUrl(change);
       ((ApprovalsView) v(R.id.approvals)).displayApprovals(getApp(),
           change.info, this);
@@ -124,13 +102,6 @@
     }
   }
 
-  private void linkify() {
-    TextView commitMsg = tv(R.id.commitMessage);
-    Linkify.addLinks(commitMsg, PATTERN_CHANGE_ID, getServerUrl() + "#/q/");
-    Linkify.addLinks(commitMsg, PATTERN_LINK, "");
-    Linkify.addLinks(commitMsg, PATTERN_EMAIL, "");
-  }
-
   @Override
   public void dispatchTouchEvent(MotionEvent event) {
     zoomHandler.dispatchTouchEvent(event);
diff --git a/app/src/main/java/com/google/reviewit/util/Linkifier.java b/app/src/main/java/com/google/reviewit/util/Linkifier.java
new file mode 100644
index 0000000..64ad33d
--- /dev/null
+++ b/app/src/main/java/com/google/reviewit/util/Linkifier.java
@@ -0,0 +1,59 @@
+// 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.util;
+
+import android.text.util.Linkify;
+import android.widget.TextView;
+
+import com.google.reviewit.R;
+import com.google.reviewit.app.ReviewItApp;
+
+import java.util.regex.Pattern;
+
+public class Linkifier {
+  private static final Pattern PATTERN_CHANGE_ID =
+      Pattern.compile("I[0-9a-f]{5,40}");
+  private static final String PART_LINK = "(?:"
+      + "[a-zA-Z0-9$_+!*'%;:@=?#/~-]"
+      + "|&(?!lt;|gt;)"
+      + "|[.,](?!(?:\\s|$))"
+      + ")";
+  private static final Pattern PATTERN_LINK = Pattern.compile(
+      "https?://"
+          + PART_LINK + "{2,}"
+          + "(?:[(]" + PART_LINK + "*" + "[)])*"
+          + PART_LINK + "*");
+  private static final Pattern PATTERN_EMAIL =
+      Pattern.compile("[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}",
+          Pattern.CASE_INSENSITIVE);
+
+  private final ReviewItApp app;
+
+  public Linkifier(ReviewItApp app) {
+    this.app = app;
+  }
+
+  public void linkifyCommitMessage(TextView commitMsg) {
+    commitMsg.setLinksClickable(true);
+    Linkify.addLinks(commitMsg, PATTERN_CHANGE_ID, getServerUrl() + "#/q/");
+    Linkify.addLinks(commitMsg, PATTERN_LINK, "");
+    Linkify.addLinks(commitMsg, PATTERN_EMAIL, "");
+  }
+
+  private String getServerUrl() {
+    String serverUrl = app.getConfigManager().getServerConfig().url;
+    return FormatUtil.ensureSlash(serverUrl);
+  }
+}