Fixed the Google Shopper button remaining visible after scanning a product, and bumpe...
[zxing.git] / android / src / com / google / zxing / client / android / result / ResultHandler.java
index 2492f0c..5c88165 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,8 @@ import android.content.pm.PackageManager;
 import android.net.Uri;
 import android.preference.PreferenceManager;
 import android.provider.Contacts;
+import android.view.View;
+import android.widget.Button;
 
 import java.text.DateFormat;
 import java.text.ParsePosition;
@@ -69,6 +74,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 +86,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.
+    Button shopperButton = (Button) 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 +132,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) {
+    Button shopperButton = (Button) 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.
    *
@@ -144,8 +177,13 @@ public abstract class ResultHandler {
    * @param start   The start time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
    * @param end     The end time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
    * @param location a text description of the event location
+   * @param description a text description of the event itself
    */
-  final void addCalendarEvent(String summary, String start, String end, String location) {
+  final void addCalendarEvent(String summary,
+                              String start,
+                              String end,
+                              String location,
+                              String description) {
     Intent intent = new Intent(Intent.ACTION_EDIT);
     intent.setType("vnd.android.cursor.item/event");
     intent.putExtra("beginTime", calculateMilliseconds(start));
@@ -158,6 +196,7 @@ public abstract class ResultHandler {
     intent.putExtra("endTime", calculateMilliseconds(end));
     intent.putExtra("title", summary);
     intent.putExtra("eventLocation", location);
+    intent.putExtra("description", description);
     launchIntent(intent);
   }
 
@@ -315,6 +354,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)));
   }
@@ -381,7 +429,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) {
@@ -390,4 +438,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