Fix Android M (6.0) Camera Runtime Persmission

This commit is contained in:
Bruno Bierbaumer 2015-12-23 19:59:05 +01:00
parent 260f234d61
commit b12e0036f2
3 changed files with 50 additions and 8 deletions

View file

@ -33,7 +33,7 @@ dependencies {
compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1' compile 'com.android.support:design:23.1.1'
compile 'com.journeyapps:zxing-android-embedded:3.0.3@aar' compile 'com.journeyapps:zxing-android-embedded:3.0.3@aar'
compile 'com.google.zxing:core:3.2.0' compile 'com.google.zxing:core:3.2.1'
compile 'commons-codec:commons-codec:1.5' compile 'commons-codec:commons-codec:1.5'
androidTestCompile 'com.android.support:support-annotations:23.1.1' androidTestCompile 'com.android.support:support-annotations:23.1.1'

View file

@ -1,14 +1,19 @@
package net.bierbaumer.otp_authenticator; package net.bierbaumer.otp_authenticator;
import android.Manifest;
import android.animation.ObjectAnimator; import android.animation.ObjectAnimator;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.ActionMode; import android.view.ActionMode;
@ -38,6 +43,49 @@ public class MainActivity extends AppCompatActivity implements ActionMode.Callb
private Handler handler; private Handler handler;
private Runnable handlerTask; private Runnable handlerTask;
private static final int PERMISSIONS_REQUEST_CAMERA = 42;
private void doScanQRCode(){
new IntentIntegrator(MainActivity.this)
.setCaptureActivity(CaptureActivityAnyOrientation.class)
.setOrientationLocked(false)
.initiateScan();
}
private void scanQRCode(){
// check Android 6 permission
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
doScanQRCode();
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAMERA);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
if(requestCode == PERMISSIONS_REQUEST_CAMERA) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
doScanQRCode();
} else {
Snackbar.make(fab, R.string.msg_camera_permission, Snackbar.LENGTH_LONG).setCallback(new Snackbar.Callback() {
@Override
public void onDismissed(Snackbar snackbar, int event) {
super.onDismissed(snackbar, event);
if (entries.isEmpty()) {
showNoAccount();
}
}
}).show();
}
}
else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private Entry nextSelection = null; private Entry nextSelection = null;
private void showNoAccount(){ private void showNoAccount(){
Snackbar noAccountSnackbar = Snackbar.make(fab, R.string.no_accounts, Snackbar.LENGTH_INDEFINITE) Snackbar noAccountSnackbar = Snackbar.make(fab, R.string.no_accounts, Snackbar.LENGTH_INDEFINITE)
@ -50,13 +98,6 @@ public class MainActivity extends AppCompatActivity implements ActionMode.Callb
noAccountSnackbar.show(); noAccountSnackbar.show();
} }
private void scanQRCode(){
new IntentIntegrator(MainActivity.this)
.setCaptureActivity(CaptureActivityAnyOrientation.class)
.setOrientationLocked(false)
.initiateScan();
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);

View file

@ -16,4 +16,5 @@
<string name="button_remove">Remove</string> <string name="button_remove">Remove</string>
<string name="alert_rename">Rename</string> <string name="alert_rename">Rename</string>
<string name="alert_remove">"Remove "</string> <string name="alert_remove">"Remove "</string>
<string name="msg_camera_permission">Camera permission not granted</string>
</resources> </resources>