Make sure it's possible un-set custom search URL; sometimes remains as a newline...
[zxing.git] / android / src / com / google / zxing / client / android / result / ISBNResultHandler.java
index b79c794..bf3ec78 100644 (file)
 
 package com.google.zxing.client.android.result;
 
-import android.app.Activity;
 import com.google.zxing.client.android.R;
 import com.google.zxing.client.result.ISBNParsedResult;
 import com.google.zxing.client.result.ParsedResult;
 
-public final class ISBNResultHandler extends ResultHandler {
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.DialogInterface;
 
-  private static final int[] mButtons = {
+/**
+ * Handles books encoded by their ISBN values.
+ *
+ * @author dswitkin@google.com (Daniel Switkin)
+ */
+public final class ISBNResultHandler extends ResultHandler {
+  private static final int[] buttons = {
       R.string.button_product_search,
       R.string.button_book_search,
-      R.string.button_search_book_contents
+      R.string.button_search_book_contents,
+      R.string.button_google_shopper
   };
 
+  private final String customProductSearch;
+
   public ISBNResultHandler(Activity activity, ParsedResult result) {
     super(activity, result);
+    customProductSearch = parseCustomSearchURL();
   }
 
+  @Override
   public int getButtonCount() {
-    return mButtons.length;
+    // Always show four buttons - Shopper and Custom Search are mutually exclusive.
+    return buttons.length;
   }
 
+  @Override
   public int getButtonText(int index) {
-    return mButtons[index];
+    if (index == buttons.length - 1 && customProductSearch != null) {
+      return R.string.button_custom_product_search;
+    }
+    return buttons[index];
   }
 
-  public void handleButtonPress(int index) {
-    ISBNParsedResult isbnResult = (ISBNParsedResult) mResult;
-    switch (index) {
-      case 0:
-        openProductSearch(isbnResult.getISBN());
-        break;
-      case 1:
-        openBookSearch(isbnResult.getISBN());
-        break;
-      case 2:
-        searchBookContents(isbnResult.getISBN());
-        break;
-    }
+  @Override
+  public void handleButtonPress(final int index) {
+    showNotOurResults(index, new AlertDialog.OnClickListener() {
+      public void onClick(DialogInterface dialogInterface, int i) {
+        ISBNParsedResult isbnResult = (ISBNParsedResult) getResult();
+        switch (index) {
+          case 0:
+            openProductSearch(isbnResult.getISBN());
+            break;
+          case 1:
+            openBookSearch(isbnResult.getISBN());
+            break;
+          case 2:
+            searchBookContents(isbnResult.getISBN());
+            break;
+          case 3:
+            if (customProductSearch != null) {
+              String url = customProductSearch.replace("%s", isbnResult.getISBN());
+              openURL(url);
+            } else {
+              openGoogleShopper(isbnResult.getISBN());
+            }
+            break;
+        }
+      }
+    });
   }
 
+  @Override
   public int getDisplayTitle() {
     return R.string.result_isbn;
   }
-
 }