Fixed some sporadic crashes.
[zxing.git] / android / src / com / google / zxing / client / android / CaptureActivity.java
index 0ebc30d..67b24bc 100755 (executable)
@@ -114,7 +114,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
   private String decodeMode;
   private String versionName;
   private HistoryManager historyManager;
-  
+
   private final OnCompletionListener beepListener = new BeepListener();
 
   private final DialogInterface.OnClickListener aboutListener =
@@ -233,7 +233,9 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
         return true;
       } else if ((source == Source.NONE || source == Source.ZXING_LINK) && lastResult != null) {
         resetStatusView();
-        handler.sendEmptyMessage(R.id.restart_preview);
+        if (handler != null) {
+          handler.sendEmptyMessage(R.id.restart_preview);
+        }
         return true;
       }
     } else if (keyCode == KeyEvent.KEYCODE_FOCUS || keyCode == KeyEvent.KEYCODE_CAMERA) {
@@ -546,6 +548,13 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
       CameraManager.get().openDriver(surfaceHolder);
     } catch (IOException ioe) {
       Log.w(TAG, ioe);
+      displayFrameworkBugMessageAndExit();
+      return;
+    } catch (RuntimeException e) {
+      // Barcode Scanner has seen crashes in the wild of this variety:
+      // java.?lang.?RuntimeException: Fail to connect to camera service
+      Log.e(TAG, e.toString());
+      displayFrameworkBugMessageAndExit();
       return;
     }
     if (handler == null) {
@@ -554,6 +563,18 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
     }
   }
 
+  private void displayFrameworkBugMessageAndExit() {
+    AlertDialog.Builder builder = new AlertDialog.Builder(this);
+    builder.setTitle(getString(R.string.app_name));
+    builder.setMessage(getString(R.string.msg_camera_framework_bug));
+    builder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
+      public void onClick(DialogInterface dialogInterface, int i) {
+        finish();
+      }
+    });
+    builder.show();
+  }
+
   private void resetStatusView() {
     resultView.setVisibility(View.GONE);
     statusView.setVisibility(View.VISIBLE);