Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix storage permission for pdf export #43

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading