Add support for tel: URIs
[zxing.git] / android / src / com / google / zxing / client / android / BarcodeReaderCaptureActivity.java
index 6f7e24f..9599dfe 100644 (file)
@@ -18,6 +18,7 @@ package com.google.zxing.client.android;
 
 import android.app.Activity;
 import android.content.Context;
+import android.content.Intent;
 import android.graphics.PixelFormat;
 import android.os.Bundle;
 import android.os.Handler;
@@ -34,18 +35,18 @@ import com.google.zxing.client.result.ParsedReaderResultType;
 /**
  * The barcode reader activity itself. This is loosely based on the CameraPreview
  * example included in the Android SDK.
- * 
+ *
  * @author dswitkin@google.com (Daniel Switkin)
  * @author Android Team (for CameraPreview example)
  */
 public final class BarcodeReaderCaptureActivity extends Activity {
-  
+
   private CameraManager cameraManager;
   private CameraSurfaceView surfaceView;
   private WorkerThread workerThread;
-  
+
   private static final int ABOUT_ID = Menu.FIRST;
-  
+
   @Override
   public void onCreate(Bundle icicle) {
     super.onCreate(icicle);
@@ -63,8 +64,11 @@ public final class BarcodeReaderCaptureActivity extends Activity {
     workerThread = new WorkerThread(surfaceView, cameraManager, messageHandler);
     workerThread.requestPreviewLoop();
     workerThread.start();
+
+    // TODO re-enable this when issues with Matrix.setPolyToPoly() are resolved
+    //GridSampler.setGridSampler(new AndroidGraphicsGridSampler());
   }
-  
+
   @Override
   protected boolean isFullscreenOpaque() {
     // Our main window is set to translucent, but we know that we will
@@ -103,14 +107,14 @@ public final class BarcodeReaderCaptureActivity extends Activity {
       return super.onKeyDown(keyCode, event);
     }
   }
-  
+
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
-      super.onCreateOptionsMenu(menu);
-      menu.add(0, ABOUT_ID, R.string.menu_about);
-      return true;
+    super.onCreateOptionsMenu(menu);
+    menu.add(0, ABOUT_ID, R.string.menu_about);
+    return true;
   }
-  
+
   @Override
   public boolean onOptionsItemSelected(Menu.Item item) {
     switch (item.getId()) {
@@ -120,31 +124,31 @@ public final class BarcodeReaderCaptureActivity extends Activity {
             context.getString(R.string.msg_about),
             context.getString(R.string.button_ok), null, true, null);
         break;
-      }
-      return super.onOptionsItemSelected(item);
+    }
+    return super.onOptionsItemSelected(item);
   }
-  
-  Handler messageHandler = new Handler() {
+
+  private final Handler messageHandler = new Handler() {
     @Override
     public void handleMessage(Message message) {
       switch (message.what) {
-      case R.id.decoding_succeeded_message:
-        handleDecode((Result)message.obj);
-        break;
-      case R.id.decoding_failed_message:
-        Context context = getApplication();
-        showAlert(context.getString(R.string.title_no_barcode_detected),
-            context.getString(R.string.msg_no_barcode_detected),
-            context.getString(R.string.button_ok), null, true, null);
-        break;
+        case R.id.decoding_succeeded_message:
+          handleDecode((Result) message.obj);
+          break;
+        case R.id.decoding_failed_message:
+          Context context = getApplication();
+          showAlert(context.getString(R.string.title_no_barcode_detected),
+              context.getString(R.string.msg_no_barcode_detected),
+              context.getString(R.string.button_ok), null, true, null);
+          break;
       }
     }
   };
-  
+
   public void restartPreview() {
     workerThread.requestPreviewLoop();
   }
-  
+
   // TODO(dswitkin): These deprecated showAlert calls need to be updated.
   private void handleDecode(Result rawResult) {
     ResultPoint[] points = rawResult.getResultPoints();
@@ -153,9 +157,9 @@ public final class BarcodeReaderCaptureActivity extends Activity {
     }
 
     Context context = getApplication();
-    ParsedReaderResult readerResult = ParsedReaderResult.parseReaderResult(rawResult.getText());
-    Handler handler = new ResultHandler(this, readerResult);
-    if (canBeHandled(readerResult.getType())) {
+    ParsedReaderResult readerResult = parseReaderResult(rawResult);
+    ResultHandler handler = new ResultHandler(this, readerResult);
+    if (handler.getIntent() != null) {
       // Can be handled by some external app; ask if the user wants to
       // proceed first though
       Message yesMessage = handler.obtainMessage(R.string.button_yes);
@@ -172,24 +176,40 @@ public final class BarcodeReaderCaptureActivity extends Activity {
     }
   }
 
-  private static boolean canBeHandled(ParsedReaderResultType type) {
-    return type != ParsedReaderResultType.TEXT;
+  private static ParsedReaderResult parseReaderResult(Result rawResult) {
+    ParsedReaderResult readerResult = ParsedReaderResult.parseReaderResult(rawResult);
+    if (readerResult.getType().equals(ParsedReaderResultType.TEXT)) {
+      String rawText = rawResult.getText();
+      AndroidIntentParsedResult androidResult = AndroidIntentParsedResult.parse(rawText);
+      if (androidResult != null) {
+        Intent intent = androidResult.getIntent();
+        if (!Intent.VIEW_ACTION.equals(intent.getAction())) {
+          // For now, don't take anything that just parses as a View action. A lot
+          // of things are accepted as a View action by default.
+          readerResult = androidResult;          
+        }
+      }
+    }
+    return readerResult;
   }
 
   private static int getDialogTitleID(ParsedReaderResultType type) {
-    if (type == ParsedReaderResultType.ADDRESSBOOK) {
+    if (type.equals(ParsedReaderResultType.ADDRESSBOOK)) {
       return R.string.title_add_contact;
-    } else if (type == ParsedReaderResultType.BOOKMARK) {
+    } else if (type.equals(ParsedReaderResultType.URI) ||
+               type.equals(ParsedReaderResultType.BOOKMARK) ||
+               type.equals(ParsedReaderResultType.URLTO)) {
       return R.string.title_open_url;
-    } else if (type == ParsedReaderResultType.EMAIL || type == ParsedReaderResultType.EMAIL_ADDRESS) {
+    } else if (type.equals(ParsedReaderResultType.EMAIL) ||
+               type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
       return R.string.title_compose_email;
-    } else if (type == ParsedReaderResultType.UPC) {
+    } else if (type.equals(ParsedReaderResultType.UPC)) {
       return R.string.title_lookup_barcode;
-    } else if (type == ParsedReaderResultType.URI) {
-      return R.string.title_open_url;
+    } else if (type.equals(ParsedReaderResultType.TEL)) {
+      return R.string.title_dial;
     } else {
       return R.string.title_barcode_detected;
     }
   }
-  
+
 }
\ No newline at end of file