Browse Source

Added permissions to Uploads

uploads
Ezerous 6 years ago
parent
commit
81f9f47f24
No known key found for this signature in database GPG Key ID: 262B2954BBA319E3
  1. 46
      app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java
  2. 38
      app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java

46
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.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v7.content.res.AppCompatResources; import android.support.v7.content.res.AppCompatResources;
import android.support.v7.preference.PreferenceManager; import android.support.v7.preference.PreferenceManager;
@ -63,9 +65,14 @@ public class UploadActivity extends BaseActivity {
/** /**
* Request codes used in activities for result (AFR) calls * 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_CHOOSE_FILE = 8; //Arbitrary, application specific
private static final int AFR_REQUEST_CODE_CAMERA = 4; private static final int AFR_REQUEST_CODE_CAMERA = 4; //Arbitrary, application specific
private static final int AFR_REQUEST_CODE_FIELDS_BUILDER = 74; 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<UploadCategory> uploadRootCategories = new ArrayList<>(); private ArrayList<UploadCategory> uploadRootCategories = new ArrayList<>();
private ParseUploadPageTask parseUploadPageTask; private ParseUploadPageTask parseUploadPageTask;
@ -214,14 +221,10 @@ public class UploadActivity extends BaseActivity {
Drawable takePhotoDrawable = AppCompatResources.getDrawable(this, R.drawable.ic_photo_camera_white_24dp); Drawable takePhotoDrawable = AppCompatResources.getDrawable(this, R.drawable.ic_photo_camera_white_24dp);
takePhotoButton.setCompoundDrawablesRelativeWithIntrinsicBounds(takePhotoDrawable, null, null, null); takePhotoButton.setCompoundDrawablesRelativeWithIntrinsicBounds(takePhotoDrawable, null, null, null);
takePhotoButton.setOnClickListener(v -> { takePhotoButton.setOnClickListener(v -> {
// Create the File where the photo should go if(checkPerms())
photoFile = TakePhoto.createImageFile(this); takePhoto();
else
// Continue only if the File was successfully created requestPerms(UPLOAD_REQUEST_CODE);
if (photoFile != null) {
startActivityForResult(TakePhoto.getIntent(this, photoFile),
AFR_REQUEST_CODE_CAMERA);
}
}); });
FloatingActionButton uploadFAB = findViewById(R.id.upload_fab); 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 class CustomOnItemSelectedListener implements AdapterView.OnItemSelectedListener {
private ArrayList<UploadCategory> parentCategories, childCategories; private ArrayList<UploadCategory> parentCategories, childCategories;

38
app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java

@ -632,10 +632,10 @@ public abstract class BaseActivity extends AppCompatActivity {
//-------------------------------------------BOOKMARKS END------------------------------------------ //-------------------------------------------BOOKMARKS END------------------------------------------
//-------PERMS--------- //-------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 //True if permissions are OK
private boolean checkPerms() { protected boolean checkPerms() {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
String[] PERMISSIONS_STORAGE = { String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,
@ -648,13 +648,13 @@ public abstract class BaseActivity extends AppCompatActivity {
} }
//Display popup for user to grant permission //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) { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
String[] PERMISSIONS_STORAGE = { String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_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 public void onRequestPermissionsResult(int permsRequestCode, @NonNull String[] permissions
, @NonNull int[] grantResults) { , @NonNull int[] grantResults) {
switch (permsRequestCode) { switch (permsRequestCode) {
case PERMISSIONS_REQUEST_CODE: case DOWNLOAD_REQUEST_CODE:
downloadFile(); if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
prepareDownload(tempThmmyFile);
break; break;
} }
} }
@ -678,16 +679,10 @@ public abstract class BaseActivity extends AppCompatActivity {
prepareDownload(thmmyFile); prepareDownload(thmmyFile);
else { else {
tempThmmyFile = thmmyFile; 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) { private void prepareDownload(ThmmyFile thmmyFile) {
String fileName = thmmyFile.getFilename(); String fileName = thmmyFile.getFilename();
if (FileUtils.fileNameExists(fileName)) if (FileUtils.fileNameExists(fileName))
@ -705,15 +700,8 @@ public abstract class BaseActivity extends AppCompatActivity {
Button cancelButton = view.findViewById(R.id.cancel); Button cancelButton = view.findViewById(R.id.cancel);
Button openButton = view.findViewById(R.id.open); Button openButton = view.findViewById(R.id.open);
Button downloadButton = view.findViewById(R.id.download); Button downloadButton = view.findViewById(R.id.download);
cancelButton.setOnClickListener(new View.OnClickListener() { cancelButton.setOnClickListener(v -> dialog.dismiss());
@Override openButton.setOnClickListener(v -> {
public void onClick(View v) {
dialog.dismiss();
}
});
openButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss(); dialog.dismiss();
try { try {
String fileName = thmmyFile.getFilename(); String fileName = thmmyFile.getFilename();
@ -727,14 +715,10 @@ public abstract class BaseActivity extends AppCompatActivity {
Toast.makeText(getBaseContext(), "Couldn't open file...", Toast.LENGTH_SHORT).show(); Toast.makeText(getBaseContext(), "Couldn't open file...", Toast.LENGTH_SHORT).show();
} }
}
}); });
downloadButton.setOnClickListener(new View.OnClickListener() { downloadButton.setOnClickListener(v -> {
@Override
public void onClick(View v) {
dialog.dismiss(); dialog.dismiss();
DownloadHelper.enqueueDownload(thmmyFile); DownloadHelper.enqueueDownload(thmmyFile);
}
}); });
dialog.show(); dialog.show();
} }

Loading…
Cancel
Save