Merged revisions 321,327,330,332,334,342-343,352-353,355-358,361-363,365,372 via...
[zxing.git] / android / src / com / google / zxing / client / android / ResultHandler.java
index cfe4df6..6f9a3ab 100755 (executable)
 package com.google.zxing.client.android;
 
 import android.content.Intent;
-import android.net.ContentURI;
-import android.os.Handler;
-import android.os.Message;
+import android.net.Uri;
 import android.provider.Contacts;
-import com.google.zxing.client.result.AddressBookAUParsedResult;
-import com.google.zxing.client.result.AddressBookDoCoMoParsedResult;
-import com.google.zxing.client.result.BookmarkDoCoMoParsedResult;
-import com.google.zxing.client.result.EmailAddressParsedResult;
-import com.google.zxing.client.result.EmailDoCoMoParsedResult;
-import com.google.zxing.client.result.GeoParsedResult;
-import com.google.zxing.client.result.ParsedReaderResult;
-import com.google.zxing.client.result.ParsedReaderResultType;
-import com.google.zxing.client.result.TelParsedResult;
-import com.google.zxing.client.result.UPCParsedResult;
-import com.google.zxing.client.result.URIParsedResult;
-import com.google.zxing.client.result.URLTOParsedResult;
-
-import java.net.URISyntaxException;
+import android.util.Log;
+import android.view.View;
+import android.widget.Button;
+import com.google.zxing.client.result.*;
 
 /**
  * Handles the result of barcode decoding in the context of the Android platform,
- * by dispatching the proper intents and so on.
+ * by dispatching the proper intents to open other activities like GMail, Maps, etc.
  *
  * @author srowen@google.com (Sean Owen)
  * @author dswitkin@google.com (Daniel Switkin)
  */
-final class ResultHandler extends Handler {
+final class ResultHandler implements Button.OnClickListener {
+
+  private static final String TAG = "ResultHandler";
 
   private final Intent intent;
   private final BarcodeReaderCaptureActivity captureActivity;
@@ -74,68 +64,39 @@ final class ResultHandler extends Handler {
       putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
     } else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
       // For now, we can only open the browser, and not actually add a bookmark
-      try {
-        intent = new Intent(Intent.VIEW_ACTION, new ContentURI(((BookmarkDoCoMoParsedResult) result).getURI()));
-      } catch (URISyntaxException e) {
-      }
+      intent = new Intent(Intent.VIEW_ACTION, Uri.parse(((BookmarkDoCoMoParsedResult) result).getURI()));
     } else if (type.equals(ParsedReaderResultType.URLTO)) {
-      try {
-        intent = new Intent(Intent.VIEW_ACTION, new ContentURI(((URLTOParsedResult) result).getURI()));
-      } catch (URISyntaxException e) {
-      }
+      intent = new Intent(Intent.VIEW_ACTION, Uri.parse(((URLTOParsedResult) result).getURI()));
     } else if (type.equals(ParsedReaderResultType.EMAIL)) {
       EmailDoCoMoParsedResult emailResult = (EmailDoCoMoParsedResult) result;
-      try {
-        intent = new Intent(Intent.SENDTO_ACTION, new ContentURI(emailResult.getTo()));
-      } catch (URISyntaxException e) {
-      }
+      intent = new Intent(Intent.SENDTO_ACTION, Uri.parse(emailResult.getTo()));
       putExtra(intent, "subject", emailResult.getSubject());
       putExtra(intent, "body", emailResult.getBody());
     } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
       EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
-      try {
-        intent = new Intent(Intent.SENDTO_ACTION, new ContentURI(emailResult.getEmailAddress()));
-      } catch (URISyntaxException e) {
-      }
+      intent = new Intent(Intent.SENDTO_ACTION, Uri.parse("mailto:" + emailResult.getEmailAddress()));
     } else if (type.equals(ParsedReaderResultType.TEL)) {
       TelParsedResult telResult = (TelParsedResult) result;
-      try {
-        intent = new Intent(Intent.DIAL_ACTION, new ContentURI("tel:" + telResult.getNumber()));
-      } catch (URISyntaxException e) {
-      }
+      intent = new Intent(Intent.DIAL_ACTION, Uri.parse("tel:" + telResult.getNumber()));
     } else if (type.equals(ParsedReaderResultType.GEO)) {
       GeoParsedResult geoResult = (GeoParsedResult) result;
-      try {
-        intent = new Intent(Intent.VIEW_ACTION, new ContentURI(geoResult.getGeoURI()));
-      } catch (URISyntaxException e) {
-      }
+      intent = new Intent(Intent.VIEW_ACTION, Uri.parse(geoResult.getGeoURI()));
     } else if (type.equals(ParsedReaderResultType.UPC)) {
       UPCParsedResult upcResult = (UPCParsedResult) result;
-      try {
-        ContentURI uri = new ContentURI("http://www.upcdatabase.com/item.asp?upc=" + upcResult.getUPC());
-        intent = new Intent(Intent.VIEW_ACTION, uri);
-      } catch (URISyntaxException e) {
-      }
+      Uri uri = Uri.parse("http://www.upcdatabase.com/item.asp?upc=" + upcResult.getUPC());
+      intent = new Intent(Intent.VIEW_ACTION, uri);
     } else if (type.equals(ParsedReaderResultType.URI)) {
       URIParsedResult uriResult = (URIParsedResult) result;
-      try {
-        intent = new Intent(Intent.VIEW_ACTION, new ContentURI(uriResult.getURI()));
-      } catch (URISyntaxException e) {
-      }
+      intent = new Intent(Intent.VIEW_ACTION, Uri.parse(uriResult.getURI()));
     } else if (type.equals(ParsedReaderResultType.ANDROID_INTENT)) {
       intent = ((AndroidIntentParsedResult) result).getIntent();
     }
     return intent;
   }
 
-  @Override
-  public void handleMessage(Message message) {
-    if (message.what == R.string.button_yes) {
-      if (intent != null) {
-        captureActivity.startActivity(intent);
-      }
-    } else {
-      captureActivity.restartPreview();
+  public void onClick(View view) {
+    if (intent != null) {
+      captureActivity.startActivity(intent);
     }
   }