Skip to content

Commit

Permalink
Fix crash on android >M when clicking on buttons without permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
gillesdubois committed Sep 28, 2017
1 parent 6025de8 commit 9e6baa1
Showing 1 changed file with 46 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,53 +98,49 @@ protected void onCreate(Bundle savedInstanceState) {

/*
* Actions for buttons
* Note : If you add a new button add check permissions function if needed
*/

// Send Inventory
Button sendInventory = (Button) findViewById(R.id.btSendInventory);
sendInventory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setStatus(R.string.title_bt_launch);
spawnTask(true);
if(checkAndRequestPermissions()) {
setStatus(R.string.title_bt_launch);
spawnTask(true);
}
}
});
// Show Inventory
Button showInventory = (Button) findViewById(R.id.btShowInventory);
showInventory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent localIntent = new Intent(getApplicationContext(), OCSShowInventory.class);
startActivity(localIntent);
if(checkAndRequestPermissions()) {
Intent localIntent = new Intent(getApplicationContext(), OCSShowInventory.class);
startActivity(localIntent);
}
}
});
// Save Inventory
Button saveInventory = (Button) findViewById(R.id.btSaveInventory);
saveInventory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
spawnTask(false);
if(checkAndRequestPermissions()) {
spawnTask(false);
}
}
});

// Check permissions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String[] ocsPermissions = new String[]{
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.CAMERA,
android.Manifest.permission.READ_PHONE_STATE};
// Check and request premissions
checkAndRequestPermissions();

List<String> permissionNeeded = new ArrayList<>();
for (String permission:ocsPermissions) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED){
permissionNeeded.add(permission);
}
}
if (!permissionNeeded.isEmpty()) {
requestPermissions(permissionNeeded.toArray(new String[permissionNeeded.size()]), REQUEST_PERMISSION_CODE);
}
}
}



@Override
public void onRequestPermissionsResult(
int requestCode,
Expand Down Expand Up @@ -260,7 +256,35 @@ private void setStatus(String msg) {
status.setText(msg);
}

private void checkAndRequestPermissions() {
private boolean checkAndRequestPermissions() {
// Check permissions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String[] ocsPermissions = new String[]{
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.CAMERA,
android.Manifest.permission.READ_PHONE_STATE};

List<String> permissionNeeded = new ArrayList<>();
for (String permission:ocsPermissions) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED){
permissionNeeded.add(permission);
}
}

if (!permissionNeeded.isEmpty()) {
requestPermissions(permissionNeeded.toArray(new String[permissionNeeded.size()]), REQUEST_PERMISSION_CODE);
}

for (String permission:ocsPermissions) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED){
return false;
}
}

}

return true;

}

}

0 comments on commit 9e6baa1

Please sign in to comment.