Fixed some sporadic crashes.
authordswitkin <dswitkin@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Fri, 11 Dec 2009 17:49:11 +0000 (17:49 +0000)
committerdswitkin <dswitkin@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Fri, 11 Dec 2009 17:49:11 +0000 (17:49 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@1151 59b500cc-1b3d-0410-9834-0bbf25fbcc57

android/res/values/strings.xml
android/src/com/google/zxing/client/android/CaptureActivity.java
android/src/com/google/zxing/client/android/encode/QRCodeEncoder.java

index 534e818..64d7c54 100755 (executable)
@@ -56,6 +56,7 @@
   <string name="menu_history">History</string>
   <string name="menu_share">Share</string>
   <string name="msg_about">Based on the open source ZXing Barcode Library</string>
+  <string name="msg_camera_framework_bug">Sorry, the Android camera encountered a problem. You may need to restart the device.</string>
   <string name="msg_default_contents">Contents</string>
   <string name="msg_default_format">Format</string>
   <string name="msg_default_mms_subject">Hi</string>
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);
index 599e40f..c55481c 100755 (executable)
@@ -159,6 +159,9 @@ final class QRCodeEncoder {
       return false;
     } catch (IOException e) {
       return false;
+    } catch (NullPointerException e) {
+      // In case the uri was not found in the Intent.
+      return false;
     }
     return contents != null && contents.length() > 0;
   }