From 9e6baa1c6c7eb84a8769aa510e789ce7c84ded18 Mon Sep 17 00:00:00 2001 From: Gilles Dubois Date: Thu, 28 Sep 2017 15:53:03 +0200 Subject: [PATCH] Fix crash on android >M when clicking on buttons without permissions --- .../agent/activity/OCSAgentActivity.java | 68 +++++++++++++------ 1 file changed, 46 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/org/ocs/android/agent/activity/OCSAgentActivity.java b/app/src/main/java/org/ocs/android/agent/activity/OCSAgentActivity.java index 202b981..1df578b 100644 --- a/app/src/main/java/org/ocs/android/agent/activity/OCSAgentActivity.java +++ b/app/src/main/java/org/ocs/android/agent/activity/OCSAgentActivity.java @@ -98,14 +98,18 @@ 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 @@ -113,8 +117,10 @@ public void onClick(View v) { 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 @@ -122,29 +128,19 @@ public void onClick(View v) { 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 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, @@ -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 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; + } }