Minor changes from code inspection results
[zxing.git] / android / src / com / google / zxing / client / android / result / ResultHandler.java
index c2a367d..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.SearchBookContentsActivity;
+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;
+import android.app.SearchManager;
 import android.content.ActivityNotFoundException;
+import android.content.DialogInterface;
 import android.content.Intent;
+import android.content.SharedPreferences;
+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;
@@ -52,14 +62,50 @@ public abstract class ResultHandler {
   private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
   private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
 
+  private static final String GOOGLE_SHOPPER_PACKAGE = "com.google.android.apps.shopper";
+  private static final String GOOGLE_SHOPPER_ACTIVITY = GOOGLE_SHOPPER_PACKAGE +
+      ".results.SearchResultsActivity";
+  private static final String MARKET_URI_PREFIX = "market://search?q=pname:";
+  private static final String MARKET_REFERRER_SUFFIX =
+      "&referrer=utm_source%3Dbarcodescanner%26utm_medium%3Dapps%26utm_campaign%3Dscan";
+
   public static final int MAX_BUTTON_COUNT = 4;
 
-  protected final ParsedResult result;
+  private final ParsedResult result;
   private final Activity activity;
+  private final Result rawResult;
+  private final String customProductSearch;
+
+  private final DialogInterface.OnClickListener shopperMarketListener =
+      new DialogInterface.OnClickListener() {
+    public void onClick(DialogInterface dialogInterface, int which) {
+      launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(MARKET_URI_PREFIX +
+          GOOGLE_SHOPPER_PACKAGE + MARKET_REFERRER_SUFFIX)));
+    }
+  };
 
-  protected ResultHandler(Activity activity, ParsedResult result) {
+  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;
   }
 
   /**
@@ -85,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.
    *
@@ -118,16 +175,27 @@ public abstract class ResultHandler {
    * @param summary A description of the event
    * @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
    */
-  public final void addCalendarEvent(String summary, String start, String end) {
+  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));
     if (start.length() == 8) {
       intent.putExtra("allDay", true);
     }
+    if (end == null) {
+      end = start;
+    }
     intent.putExtra("endTime", calculateMilliseconds(end));
     intent.putExtra("title", summary);
+    intent.putExtra("eventLocation", location);
+    intent.putExtra("description", description);
     launchIntent(intent);
   }
 
@@ -155,7 +223,7 @@ public abstract class ResultHandler {
     }
   }
 
-  public final void addContact(String[] names, String[] phoneNumbers, String[] emails, String note,
+  final void addContact(String[] names, String[] phoneNumbers, String[] emails, String note,
                          String address, String org, String title) {
 
     // Only use the first name in the array, if present.
@@ -180,33 +248,36 @@ public abstract class ResultHandler {
     launchIntent(intent);
   }
 
-  public final void shareByEmail(String contents) {
-    sendEmailFromUri("mailto:", activity.getString(R.string.msg_share_subject_line), contents);
+  final void shareByEmail(String contents) {
+    sendEmailFromUri("mailto:", null, activity.getString(R.string.msg_share_subject_line), contents);
   }
 
-  public final void sendEmail(String address, String subject, String body) {
-    sendEmailFromUri("mailto:" + address, subject, body);
+  final void sendEmail(String address, String subject, String body) {
+    sendEmailFromUri("mailto:" + address, address, subject, body);
   }
 
   // Use public Intent fields rather than private GMail app fields to specify subject and body.
-  public final void sendEmailFromUri(String uri, String subject, String body) {
+  final void sendEmailFromUri(String uri, String email, String subject, String body) {
     Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse(uri));
+    if (email != null) {
+      intent.putExtra(Intent.EXTRA_EMAIL, new String[] {email});
+    }
     putExtra(intent, Intent.EXTRA_SUBJECT, subject);
     putExtra(intent, Intent.EXTRA_TEXT, body);
     intent.setType("text/plain");
     launchIntent(intent);
   }
 
-  public final void shareBySMS(String contents) {
+  final void shareBySMS(String contents) {
     sendSMSFromUri("smsto:", activity.getString(R.string.msg_share_subject_line) + ":\n" +
         contents);
   }
 
-  public final void sendSMS(String phoneNumber, String body) {
+  final void sendSMS(String phoneNumber, String body) {
     sendSMSFromUri("smsto:" + phoneNumber, body);
   }
 
-  public final void sendSMSFromUri(String uri, String body) {
+  final void sendSMSFromUri(String uri, String body) {
     Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
     putExtra(intent, "sms_body", body);
     // Exit the app once the SMS is sent
@@ -214,11 +285,11 @@ public abstract class ResultHandler {
     launchIntent(intent);
   }
 
-  public final void sendMMS(String phoneNumber, String subject, String body) {
+  final void sendMMS(String phoneNumber, String subject, String body) {
     sendMMSFromUri("mmsto:" + phoneNumber, subject, body);
   }
 
-  public final void sendMMSFromUri(String uri, String subject, String body) {
+  final void sendMMSFromUri(String uri, String subject, String body) {
     Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
     // The Messaging app needs to see a valid subject or else it will treat this an an SMS.
     if (subject == null || subject.length() == 0) {
@@ -231,15 +302,15 @@ public abstract class ResultHandler {
     launchIntent(intent);
   }
 
-  public final void dialPhone(String phoneNumber) {
+  final void dialPhone(String phoneNumber) {
     launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)));
   }
 
-  public final void dialPhoneFromUri(String uri) {
+  final void dialPhoneFromUri(String uri) {
     launchIntent(new Intent(Intent.ACTION_DIAL, Uri.parse(uri)));
   }
 
-  public final void openMap(String geoURI) {
+  final void openMap(String geoURI) {
     launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(geoURI)));
   }
 
@@ -249,7 +320,7 @@ public abstract class ResultHandler {
    * @param address The address to find
    * @param title An optional title, e.g. the name of the business at this address
    */
-  public final void searchMap(String address, String title) {
+  final void searchMap(String address, String title) {
     String query = address;
     if (title != null && title.length() > 0) {
       query = query + " (" + title + ')';
@@ -257,49 +328,79 @@ public abstract class ResultHandler {
     launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + Uri.encode(query))));
   }
 
-  public final void getDirections(double latitude, double longitude) {
+  final void getDirections(double latitude, double longitude) {
     launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google." +
         LocaleManager.getCountryTLD() + "/maps?f=d&daddr=" + latitude + ',' + longitude)));
   }
 
   // Uses the mobile-specific version of Product Search, which is formatted for small screens.
-  public final void openProductSearch(String upc) {
+  final void openProductSearch(String upc) {
     Uri uri = Uri.parse("http://www.google." + LocaleManager.getProductSearchCountryTLD() +
         "/m/products?q=" + upc + "&source=zxing");
     launchIntent(new Intent(Intent.ACTION_VIEW, uri));
   }
 
-  public final void openBookSearch(String isbn) {
+  final void openBookSearch(String isbn) {
     Uri uri = Uri.parse("http://books.google." + LocaleManager.getBookSearchCountryTLD() +
         "/books?vid=isbn" + isbn);
     launchIntent(new Intent(Intent.ACTION_VIEW, uri));
   }
 
-  public final void searchBookContents(String isbn) {
+  final void searchBookContents(String isbn) {
     Intent intent = new Intent(Intents.SearchBookContents.ACTION);
     intent.setClassName(activity, SearchBookContentsActivity.class.getName());
     putExtra(intent, Intents.SearchBookContents.ISBN, isbn);
     launchIntent(intent);
   }
 
-  public final void openURL(String url) {
+  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)));
   }
 
-  public final void webSearch(String query) {
+  final void webSearch(String query) {
     Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
     intent.putExtra("query", query);
     launchIntent(intent);
   }
 
-  private void launchIntent(Intent intent) {
+  final void openGoogleShopper(String query) {
+    try {
+      activity.getPackageManager().getPackageInfo(GOOGLE_SHOPPER_PACKAGE, 0);
+      // If we didn't throw, Shopper is installed, so launch it.
+      Intent intent = new Intent(Intent.ACTION_SEARCH);
+      intent.setClassName(GOOGLE_SHOPPER_PACKAGE, GOOGLE_SHOPPER_ACTIVITY);
+      intent.putExtra(SearchManager.QUERY, query);
+      activity.startActivity(intent);
+    } catch (PackageManager.NameNotFoundException e) {
+      // Otherwise offer to install it from Market.
+      AlertDialog.Builder builder = new AlertDialog.Builder(activity);
+      builder.setTitle(R.string.msg_google_shopper_missing);
+      builder.setMessage(R.string.msg_install_google_shopper);
+      builder.setIcon(R.drawable.shopper_icon);
+      builder.setPositiveButton(R.string.button_ok, shopperMarketListener);
+      builder.setNegativeButton(R.string.button_cancel, null);
+      builder.show();
+    }
+  }
+
+  void launchIntent(Intent intent) {
     if (intent != null) {
+      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
       try {
         activity.startActivity(intent);
       } catch (ActivityNotFoundException e) {
         AlertDialog.Builder builder = new AlertDialog.Builder(activity);
-        builder.setTitle(activity.getString(R.string.app_name));
-        builder.setMessage(activity.getString(R.string.msg_intent_failed));
+        builder.setTitle(R.string.app_name);
+        builder.setMessage(R.string.msg_intent_failed);
         builder.setPositiveButton(R.string.button_ok, null);
         builder.show();
       }
@@ -311,4 +412,37 @@ public abstract class ResultHandler {
       intent.putExtra(key, value);
     }
   }
-}
+
+  protected void showNotOurResults(int index, AlertDialog.OnClickListener proceedListener) {
+    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
+    if (prefs.getBoolean(PreferencesActivity.KEY_NOT_OUR_RESULTS_SHOWN, false)) {
+      // already seen it, just proceed
+      proceedListener.onClick(null, index);
+    } else {
+      // note the user has seen it
+      prefs.edit().putBoolean(PreferencesActivity.KEY_NOT_OUR_RESULTS_SHOWN, true).commit();
+      AlertDialog.Builder builder = new AlertDialog.Builder(activity);
+      builder.setMessage(R.string.msg_not_our_results);
+      builder.setPositiveButton(R.string.button_ok, proceedListener);
+      builder.show();
+    }
+  }
+
+  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) {
+      return null;
+    }
+    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