Skip to content

Commit

Permalink
Fixed issue with camera permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
erasmospunk committed Apr 21, 2016
1 parent 8736b36 commit 5b21967
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 56 deletions.
2 changes: 2 additions & 0 deletions wallet/src/main/java/com/coinomi/wallet/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public class Constants {
public static final String ARG_URI = "test_wallet";
public static final String ARG_PRIVATE_KEY = "private_key";

public static final int PERMISSIONS_REQUEST_CAMERA = 0;

public static final String WALLET_FILENAME_PROTOBUF = "wallet";
public static final long WALLET_WRITE_DELAY = 5;
public static final TimeUnit WALLET_WRITE_DELAY_UNIT = TimeUnit.SECONDS;
Expand Down
130 changes: 74 additions & 56 deletions wallet/src/main/java/com/coinomi/wallet/ui/ScanActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
*/


import static android.Manifest.permission.CAMERA;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
Expand All @@ -32,10 +35,16 @@
import android.os.HandlerThread;
import android.os.Process;
import android.os.Vibrator;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

import com.coinomi.wallet.Constants;
import com.coinomi.wallet.R;
import com.coinomi.wallet.camera.CameraManager;
import com.google.zxing.BinaryBitmap;
Expand All @@ -57,8 +66,9 @@

/**
* @author Andreas Schildbach
* @author John L. Jegutanis
*/
public final class ScanActivity extends Activity implements SurfaceHolder.Callback
public final class ScanActivity extends FragmentActivity implements SurfaceHolder.Callback
{
public static final String INTENT_EXTRA_RESULT = "result";

Expand All @@ -71,6 +81,7 @@ public final class ScanActivity extends Activity implements SurfaceHolder.Callba
private Vibrator vibrator;
private HandlerThread cameraThread;
private Handler cameraHandler;
private boolean isSurfaceCreated = false;

private static final int DIALOG_CAMERA_PROBLEM = 0;

Expand Down Expand Up @@ -107,33 +118,71 @@ protected void onResume()
final SurfaceView surfaceView = (SurfaceView) findViewById(R.id.scan_activity_preview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


if (!hasCameraPermission()) {
askCameraPermission();
} else {
openCamera();
}

}

@Override
public void surfaceCreated(final SurfaceHolder holder)
protected void onPause()
{
cameraHandler.post(openRunnable);
cameraHandler.post(closeRunnable);
surfaceHolder.removeCallback(this);

super.onPause();
}

private void askCameraPermission() {
if (!hasCameraPermission()) {
ActivityCompat.requestPermissions(this, new String[]{CAMERA},
Constants.PERMISSIONS_REQUEST_CAMERA);
}
}

private boolean hasCameraPermission() {
return ContextCompat.checkSelfPermission(this, CAMERA) == PERMISSION_GRANTED;
}

@Override
public void surfaceDestroyed(final SurfaceHolder holder)
{
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == Constants.PERMISSIONS_REQUEST_CAMERA) {
for (int i = 0; i < permissions.length; i++) {
if (permissions[i].equals(CAMERA) && grantResults[i] == PERMISSION_GRANTED) {
break;
}
}

if (!hasCameraPermission())
showErrorToast();
}
}

@Override
public void surfaceChanged(final SurfaceHolder holder, final int format, final int width, final int height)
public void surfaceCreated(final SurfaceHolder holder)
{
isSurfaceCreated = true;
openCamera();
}

private void openCamera() {
if (isSurfaceCreated && hasCameraPermission()) {
cameraHandler.post(openRunnable);
}
}

@Override
protected void onPause()
public void surfaceDestroyed(final SurfaceHolder holder)
{
cameraHandler.post(closeRunnable);

surfaceHolder.removeCallback(this);
}

super.onPause();
@Override
public void surfaceChanged(final SurfaceHolder holder, final int format, final int width, final int height)
{
}

@Override
Expand Down Expand Up @@ -236,18 +285,27 @@ public void run()
catch (final IOException x)
{
log.info("problem opening camera", x);
//TODO show dialog
// showDialog(DIALOG_CAMERA_PROBLEM);
showErrorToast();
}
catch (final RuntimeException x)
{
log.info("problem opening camera", x);
//TODO show dialog
// showDialog(DIALOG_CAMERA_PROBLEM);
showErrorToast();
}
}
};

private void showErrorToast() {

runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ScanActivity.this, R.string.error_camera, Toast.LENGTH_LONG).show();
finish();
}
});
}

private final Runnable closeRunnable = new Runnable()
{
@Override
Expand Down Expand Up @@ -354,44 +412,4 @@ public void run()
}
}
};


/** TODO
*
* Use the new {@link android.app.DialogFragment} class with
* {@link android.app.FragmentManager} instead
*
*/

// @Override
// protected Dialog onCreateDialog(final int id)
// {
// if (id == DIALOG_CAMERA_PROBLEM)
// {
// final DialogBuilder dialog = DialogBuilder.warn(this, R.string.scan_camera_problem_dialog_title);
// dialog.setMessage(R.string.scan_camera_problem_dialog_message);
// dialog.singleDismissButton(new OnClickListener()
// {
// @Override
// public void onClick(final DialogInterface dialog, final int which)
// {
// finish();
// }
// });
// dialog.setOnCancelListener(new OnCancelListener()
// {
// @Override
// public void onCancel(final DialogInterface dialog)
// {
// finish();
// }
// });
//
// return dialog.create();
// }
// else
// {
// throw new IllegalArgumentException();
// }
// }
}
1 change: 1 addition & 0 deletions wallet/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<!-- Errors -->
<string name="error_generic">Error occurred while performing action</string>
<string name="error_could_not_read_wallet">Could not read wallet</string>
<string name="error_camera">Could not access the device camera</string>
<!-- Generic items -->
<string name="button_ok">OK</string>
<string name="button_next">Next</string>
Expand Down

0 comments on commit 5b21967

Please sign in to comment.