Minor changes from code inspection results
[zxing.git] / android / src / com / google / zxing / client / android / result / ResultHandler.java
index 97e57e1..cc4aac4 100644 (file)
 
 package com.google.zxing.client.android.result;
 
+import com.google.zxing.Result;
 import com.google.zxing.client.android.Contents;
 import com.google.zxing.client.android.Intents;
 import com.google.zxing.client.android.LocaleManager;
 import com.google.zxing.client.android.PreferencesActivity;
 import com.google.zxing.client.android.R;
 import com.google.zxing.client.android.book.SearchBookContentsActivity;
+import com.google.zxing.client.android.wifi.WifiActivity;
 import com.google.zxing.client.result.ParsedResult;
 import com.google.zxing.client.result.ParsedResultType;
+import com.google.zxing.client.result.WifiParsedResult;
 
 import android.app.Activity;
 import android.app.AlertDialog;
@@ -36,6 +39,7 @@ import android.content.pm.PackageManager;
 import android.net.Uri;
 import android.preference.PreferenceManager;
 import android.provider.Contacts;
+import android.view.View;
 
 import java.text.DateFormat;
 import java.text.ParsePosition;
@@ -69,6 +73,8 @@ public abstract class ResultHandler {
 
   private final ParsedResult result;
   private final Activity activity;
+  private final Result rawResult;
+  private final String customProductSearch;
 
   private final DialogInterface.OnClickListener shopperMarketListener =
       new DialogInterface.OnClickListener() {
@@ -79,14 +85,29 @@ public abstract class ResultHandler {
   };
 
   ResultHandler(Activity activity, ParsedResult result) {
+    this(activity, result, null);
+  }
+
+  ResultHandler(Activity activity, ParsedResult result, Result rawResult) {
     this.result = result;
     this.activity = activity;
+    this.rawResult = rawResult;
+    this.customProductSearch = parseCustomSearchURL();
+
+    // Make sure the Shopper button is hidden by default. Without this, scanning a product followed
+    // by a QR Code would leave the button on screen among the QR Code actions.
+    View shopperButton = activity.findViewById(R.id.shopper_button);
+    shopperButton.setVisibility(View.GONE);
   }
 
   ParsedResult getResult() {
     return result;
   }
 
+  boolean hasCustomProductSearch() {
+    return customProductSearch != null;
+  }
+
   /**
    * Indicates how many buttons the derived class wants shown.
    *
@@ -110,6 +131,17 @@ public abstract class ResultHandler {
    */
   public abstract void handleButtonPress(int index);
 
+  /**
+   * The Google Shopper button is special and is not handled by the abstract button methods above.
+   *
+   * @param listener The on click listener to install for this button.
+   */
+  protected void showGoogleShopperButton(View.OnClickListener listener) {
+    View shopperButton = activity.findViewById(R.id.shopper_button);
+    shopperButton.setVisibility(View.VISIBLE);
+    shopperButton.setOnClickListener(listener);
+  }
+
   /**
    * Create a possibly styled string for the contents of the current barcode.
    *
@@ -146,7 +178,7 @@ public abstract class ResultHandler {
    * @param location a text description of the event location
    * @param description a text description of the event itself
    */
-  final void addCalendarEvent(String summary, 
+  final void addCalendarEvent(String summary,
                               String start,
                               String end,
                               String location,
@@ -321,6 +353,15 @@ public abstract class ResultHandler {
     launchIntent(intent);
   }
 
+  final void wifiConnect(WifiParsedResult wifiResult) {
+    Intent intent = new Intent(Intents.WifiConnect.ACTION);
+    intent.setClassName(activity, WifiActivity.class.getName());
+    putExtra(intent, Intents.WifiConnect.SSID, wifiResult.getSsid());
+    putExtra(intent, Intents.WifiConnect.TYPE, wifiResult.getNetworkEncryption());
+    putExtra(intent, Intents.WifiConnect.PASSWORD, wifiResult.getPassword());
+    launchIntent(intent);
+  }
+
   final void openURL(String url) {
     launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
   }
@@ -387,7 +428,7 @@ public abstract class ResultHandler {
     }
   }
 
-  protected String parseCustomSearchURL() {
+  private String parseCustomSearchURL() {
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
     String customProductSearch = prefs.getString(PreferencesActivity.KEY_CUSTOM_PRODUCT_SEARCH, null);
     if (customProductSearch != null && customProductSearch.trim().length() == 0) {
@@ -396,4 +437,12 @@ public abstract class ResultHandler {
     return customProductSearch;
   }
 
-}
+  String fillInCustomSearchURL(String text) {
+    String url = customProductSearch.replace("%s", text);
+    if (rawResult != null) {
+      url = url.replace("%f", rawResult.getBarcodeFormat().toString());
+    }
+    return url;
+  }
+
+}
\ No newline at end of file