Skip to content

Commit

Permalink
Merge pull request #43 from SecUSo/fix-storage-permissions
Browse files Browse the repository at this point in the history
Fix storage permission for pdf export
  • Loading branch information
udenr authored Apr 8, 2024
2 parents adebc13 + 8048aa5 commit 524a072
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
8 changes: 4 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />

<supports-screens
android:anyDensity="true"
android:largeScreens="true"
Expand Down Expand Up @@ -135,8 +139,4 @@

</application>

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ License, or (at your option) any later version.
import android.widget.DatePicker;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
Expand Down Expand Up @@ -74,7 +75,7 @@ public class ExportPDFActivity extends AppCompatActivity {
private TextInputLayout startDateWrapper;
private TextInputLayout endDateWrapper;

private SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.US);
private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.US);
private Date startDate;
private Date endDate;

Expand Down Expand Up @@ -134,7 +135,7 @@ public void onClick(View view) {
case R.id.btn_export:
LiveData<File> fileLive = exportAsPDF();
final ExportPDFActivity activity = this;
fileLive.observe(this, new Observer<File>() {
fileLive.observe(this, new Observer<>() {
@Override
public void onChanged(File file) {
if (file != null) {
Expand Down Expand Up @@ -196,7 +197,7 @@ public void onDateSet(DatePicker datePicker, int year, int month, int day) {
}

private LiveData<File> exportAsPDF() {
final MutableLiveData<File>[] file = new MutableLiveData[]{new MutableLiveData<>()};
final MutableLiveData<File> file = new MutableLiveData<>();
if (startDate == null) {
startDateWrapper.setError(getString(R.string.start_date_error));
} else if (endDate == null) {
Expand All @@ -206,25 +207,22 @@ private LiveData<File> exportAsPDF() {
diaryEntriesLive.observe(this, diaryEntryInterfaces -> {
long userID = new PrefManager(this).getUserID();
if (userID == AbstractPersistentObject.INVALID_OBJECT_ID) {
file[0].setValue(exportAsPDF(new PdfCreator(this, startDate, endDate, diaryEntryInterfaces, new User()).createPdfDocument()));
file.setValue(exportAsPDF(new PdfCreator(this, startDate, endDate, diaryEntryInterfaces, new User()).createPdfDocument()));
} else {
LiveData<UserInterface> userLive = database.getUserByID(userID);
userLive.observe(this, userInterface -> {
file[0].setValue(exportAsPDF(new PdfCreator(this, startDate, endDate, diaryEntryInterfaces, userInterface).createPdfDocument()));
file.setValue(exportAsPDF(new PdfCreator(this, startDate, endDate, diaryEntryInterfaces, userInterface).createPdfDocument()));
});
}
});
}
return file[0];
return file;
}

private File exportAsPDF(PdfDocument doc) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(ExportPDFActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(ExportPDFActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
return null;
}
}
Expand All @@ -237,7 +235,7 @@ private File exportAsPDF(PdfDocument doc) {
}
}

SimpleDateFormat s = new SimpleDateFormat("ddMMyyyy");
SimpleDateFormat s = new SimpleDateFormat("ddMMyyyy", Locale.getDefault());
String filename = s.format(startDate) + "-" + s.format(endDate);
File file = new File(directory, filename + ".pdf");
if (file.exists()) {
Expand Down Expand Up @@ -277,17 +275,15 @@ public void onChanged(File file) {
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, getString(R.string.permission_write_granted), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, getString(R.string.permission_write_denied), Toast.LENGTH_LONG).show();
}
return;
if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, getString(R.string.permission_write_granted), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, getString(R.string.permission_write_denied), Toast.LENGTH_LONG).show();
}
return;
}
}

Expand Down

0 comments on commit 524a072

Please sign in to comment.