From 81f9f47f24ef8403c080ed703601c7a472355540 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 3 Sep 2018 16:11:14 +0300 Subject: [PATCH] Added permissions to Uploads --- .../activities/upload/UploadActivity.java | 46 +++++++++---- .../gr/thmmy/mthmmy/base/BaseActivity.java | 64 +++++++------------ 2 files changed, 59 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java index e161edf5..26ab4222 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java @@ -4,10 +4,12 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.design.widget.FloatingActionButton; import android.support.v7.content.res.AppCompatResources; import android.support.v7.preference.PreferenceManager; @@ -63,9 +65,14 @@ public class UploadActivity extends BaseActivity { /** * Request codes used in activities for result (AFR) calls */ - private static final int AFR_REQUEST_CODE_CHOOSE_FILE = 8; - private static final int AFR_REQUEST_CODE_CAMERA = 4; - private static final int AFR_REQUEST_CODE_FIELDS_BUILDER = 74; + private static final int AFR_REQUEST_CODE_CHOOSE_FILE = 8; //Arbitrary, application specific + private static final int AFR_REQUEST_CODE_CAMERA = 4; //Arbitrary, application specific + private static final int AFR_REQUEST_CODE_FIELDS_BUILDER = 74; //Arbitrary, application specific + + /** + * Request code to gain read/write permission + */ + private static final int UPLOAD_REQUEST_CODE = 42; //Arbitrary, application specific private ArrayList uploadRootCategories = new ArrayList<>(); private ParseUploadPageTask parseUploadPageTask; @@ -214,14 +221,10 @@ public class UploadActivity extends BaseActivity { Drawable takePhotoDrawable = AppCompatResources.getDrawable(this, R.drawable.ic_photo_camera_white_24dp); takePhotoButton.setCompoundDrawablesRelativeWithIntrinsicBounds(takePhotoDrawable, null, null, null); takePhotoButton.setOnClickListener(v -> { - // Create the File where the photo should go - photoFile = TakePhoto.createImageFile(this); - - // Continue only if the File was successfully created - if (photoFile != null) { - startActivityForResult(TakePhoto.getIntent(this, photoFile), - AFR_REQUEST_CODE_CAMERA); - } + if(checkPerms()) + takePhoto(); + else + requestPerms(UPLOAD_REQUEST_CODE); }); FloatingActionButton uploadFAB = findViewById(R.id.upload_fab); @@ -408,6 +411,27 @@ public class UploadActivity extends BaseActivity { } } + @Override + public void onRequestPermissionsResult(int permsRequestCode, @NonNull String[] permissions + , @NonNull int[] grantResults) { + switch (permsRequestCode) { + case UPLOAD_REQUEST_CODE: + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) + takePhoto(); + break; + } + } + + // Should only be called after making sure permissions are granted + private void takePhoto(){ + // Create the File where the photo should go + photoFile = TakePhoto.createImageFile(this); + + // Continue only if the File was successfully created + if (photoFile != null) + startActivityForResult(TakePhoto.getIntent(this, photoFile), AFR_REQUEST_CODE_CAMERA); + } + private class CustomOnItemSelectedListener implements AdapterView.OnItemSelectedListener { private ArrayList parentCategories, childCategories; diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java index df678e29..67c2322b 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -632,10 +632,10 @@ public abstract class BaseActivity extends AppCompatActivity { //-------------------------------------------BOOKMARKS END------------------------------------------ //-------PERMS--------- - private static final int PERMISSIONS_REQUEST_CODE = 69; + private static final int DOWNLOAD_REQUEST_CODE = 69; //Arbitrary, application specific //True if permissions are OK - private boolean checkPerms() { + protected boolean checkPerms() { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) { String[] PERMISSIONS_STORAGE = { Manifest.permission.READ_EXTERNAL_STORAGE, @@ -648,13 +648,13 @@ public abstract class BaseActivity extends AppCompatActivity { } //Display popup for user to grant permission - private void requestPerms() { //Runtime permissions request for devices with API >= 23 + protected void requestPerms(int code) { //Runtime permissions request for devices with API >= 23 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) { String[] PERMISSIONS_STORAGE = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; - requestPermissions(PERMISSIONS_STORAGE, PERMISSIONS_REQUEST_CODE); + requestPermissions(PERMISSIONS_STORAGE, code); } } @@ -663,8 +663,9 @@ public abstract class BaseActivity extends AppCompatActivity { public void onRequestPermissionsResult(int permsRequestCode, @NonNull String[] permissions , @NonNull int[] grantResults) { switch (permsRequestCode) { - case PERMISSIONS_REQUEST_CODE: - downloadFile(); + case DOWNLOAD_REQUEST_CODE: + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) + prepareDownload(tempThmmyFile); break; } } @@ -678,16 +679,10 @@ public abstract class BaseActivity extends AppCompatActivity { prepareDownload(thmmyFile); else { tempThmmyFile = thmmyFile; - requestPerms(); + requestPerms(DOWNLOAD_REQUEST_CODE); } } - //Uses temp file - called after permission grant - private void downloadFile() { - if (checkPerms()) - prepareDownload(tempThmmyFile); - } - private void prepareDownload(ThmmyFile thmmyFile) { String fileName = thmmyFile.getFilename(); if (FileUtils.fileNameExists(fileName)) @@ -705,36 +700,25 @@ public abstract class BaseActivity extends AppCompatActivity { Button cancelButton = view.findViewById(R.id.cancel); Button openButton = view.findViewById(R.id.open); Button downloadButton = view.findViewById(R.id.download); - cancelButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - dialog.dismiss(); + cancelButton.setOnClickListener(v -> dialog.dismiss()); + openButton.setOnClickListener(v -> { + dialog.dismiss(); + try { + String fileName = thmmyFile.getFilename(); + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION); + Uri fileUri = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", new File(SAVE_DIR, fileName)); + intent.setDataAndType(fileUri, getMimeType(fileName)); + BaseActivity.this.startActivity(intent); + } catch (Exception e) { + Timber.e(e, "Couldn't open downloaded file..."); + Toast.makeText(getBaseContext(), "Couldn't open file...", Toast.LENGTH_SHORT).show(); } - }); - openButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - dialog.dismiss(); - try { - String fileName = thmmyFile.getFilename(); - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION); - Uri fileUri = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", new File(SAVE_DIR, fileName)); - intent.setDataAndType(fileUri, getMimeType(fileName)); - BaseActivity.this.startActivity(intent); - } catch (Exception e) { - Timber.e(e, "Couldn't open downloaded file..."); - Toast.makeText(getBaseContext(), "Couldn't open file...", Toast.LENGTH_SHORT).show(); - } - } }); - downloadButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - dialog.dismiss(); - DownloadHelper.enqueueDownload(thmmyFile); - } + downloadButton.setOnClickListener(v -> { + dialog.dismiss(); + DownloadHelper.enqueueDownload(thmmyFile); }); dialog.show(); }