[UI] Fix writing files on Android 13 and newer.

This commit is contained in:
Kuba Szczodrzyński 2023-03-22 23:15:45 +01:00
parent 27b61adf1d
commit 8bf77817d2
No known key found for this signature in database
GPG Key ID: 43037AC62A600562
6 changed files with 26 additions and 13 deletions

View File

@ -235,6 +235,7 @@ class App : MultiDexApplication(), Configuration.Provider, CoroutineScope {
} }
Signing.getCert(this) Signing.getCert(this)
Utils.initializeStorageDir(this)
launch { launch {
withContext(Dispatchers.Default) { withContext(Dispatchers.Default) {

View File

@ -109,14 +109,12 @@ class GenerateBlockTimetableDialog(
.show() .show()
dialog.getButton(AlertDialog.BUTTON_POSITIVE)?.onClick { dialog.getButton(AlertDialog.BUTTON_POSITIVE)?.onClick {
app.permissionManager.requestStoragePermission(activity, permissionMessage = R.string.permissions_generate_timetable) {
when (b.weekSelectionRadioGroup.checkedRadioButtonId) { when (b.weekSelectionRadioGroup.checkedRadioButtonId) {
R.id.withChangesCurrentWeekRadio -> generateBlockTimetable(weekCurrentStart, weekCurrentEnd) R.id.withChangesCurrentWeekRadio -> generateBlockTimetable(weekCurrentStart, weekCurrentEnd)
R.id.withChangesNextWeekRadio -> generateBlockTimetable(weekNextStart, weekNextEnd) R.id.withChangesNextWeekRadio -> generateBlockTimetable(weekNextStart, weekNextEnd)
R.id.forSelectedWeekRadio -> selectDate() R.id.forSelectedWeekRadio -> selectDate()
} }
} }
}
}} }}
private fun selectDate() { private fun selectDate() {

View File

@ -5,6 +5,7 @@
package pl.szczodrzynski.edziennik.ui.views package pl.szczodrzynski.edziennik.ui.views
import android.content.Context import android.content.Context
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.util.AttributeSet import android.util.AttributeSet
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -50,6 +51,10 @@ class AttachmentsView @JvmOverloads constructor(
val attachmentSizes = arguments.getLongArray("attachmentSizes") val attachmentSizes = arguments.getLongArray("attachmentSizes")
val adapter = AttachmentAdapter(context, onAttachmentClick = { item -> val adapter = AttachmentAdapter(context, onAttachmentClick = { item ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
downloadAttachment(item)
return@AttachmentAdapter
}
app.permissionManager.requestStoragePermission(activity, R.string.permissions_attachment) { app.permissionManager.requestStoragePermission(activity, R.string.permissions_attachment) {
downloadAttachment(item) downloadAttachment(item)
} }
@ -57,6 +62,10 @@ class AttachmentsView @JvmOverloads constructor(
val popupMenu = PopupMenu(chip.context, chip) val popupMenu = PopupMenu(chip.context, chip)
popupMenu.menu.add(0, 1, 0, R.string.messages_attachment_download_again) popupMenu.menu.add(0, 1, 0, R.string.messages_attachment_download_again)
popupMenu.setOnMenuItemClickListener { popupMenu.setOnMenuItemClickListener {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
downloadAttachment(item)
return@setOnMenuItemClickListener true
}
app.permissionManager.requestStoragePermission(activity, R.string.permissions_attachment) { app.permissionManager.requestStoragePermission(activity, R.string.permissions_attachment) {
downloadAttachment(item, forceDownload = true) downloadAttachment(item, forceDownload = true)
} }

View File

@ -774,12 +774,19 @@ public class Utils {
private static File storageDir = null; private static File storageDir = null;
public static File getStorageDir() { public static File getStorageDir() {
if (storageDir != null)
return storageDir; return storageDir;
}
public static void initializeStorageDir(Context context) {
if (storageDir != null)
return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
storageDir = context.getExternalFilesDir(null);
} else {
storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
storageDir = new File(storageDir, "Szkolny.eu"); storageDir = new File(storageDir, "Szkolny.eu");
}
storageDir.mkdirs(); storageDir.mkdirs();
return storageDir;
} }
public static void writeStringToFile(File file, String data) throws IOException { public static void writeStringToFile(File file, String data) throws IOException {

View File

@ -1353,7 +1353,6 @@
<string name="home_archive_close_no_target_text">Child %s does not have a profile on this account in the current school year. Probably this profile has been deleted or the student no longer attends this class.\n\nTo go to the current profile, select a student from the list or log in to their account with the Add student button.</string> <string name="home_archive_close_no_target_text">Child %s does not have a profile on this account in the current school year. Probably this profile has been deleted or the student no longer attends this class.\n\nTo go to the current profile, select a student from the list or log in to their account with the Add student button.</string>
<string name="build_invalid_no_remote_repo">A reference to a remote repository was not found. Make sure you are using the official repository fork and verify your Gradle configuration.</string> <string name="build_invalid_no_remote_repo">A reference to a remote repository was not found. Make sure you are using the official repository fork and verify your Gradle configuration.</string>
<string name="login_mode_mobidziennik_web_guide">"Enter the data you use to log in to the MobiDziennik website. As the server address, you can enter the address of the website where you have MobiDziennik. "</string> <string name="login_mode_mobidziennik_web_guide">"Enter the data you use to log in to the MobiDziennik website. As the server address, you can enter the address of the website where you have MobiDziennik. "</string>
<string name="permissions_generate_timetable">In order to be able to save the generated timetable, you must grant access rights to the device\'s memory.\n\nClick OK to grant permissions.</string>
<string name="login_summary_account_child">(Child)</string> <string name="login_summary_account_child">(Child)</string>
<string name="login_summary_account_parent">(Parent)</string> <string name="login_summary_account_parent">(Parent)</string>
<string name="menu_teachers">Teachers</string> <string name="menu_teachers">Teachers</string>

View File

@ -1416,7 +1416,6 @@
<string name="build_dialog_open_repo">Sprawdź kod</string> <string name="build_dialog_open_repo">Sprawdź kod</string>
<string name="error_no_api_access">Brak dostępu do API</string> <string name="error_no_api_access">Brak dostępu do API</string>
<string name="build_date">Data kompilacji</string> <string name="build_date">Data kompilacji</string>
<string name="permissions_generate_timetable">Aby móc zapisać wygenerowany plan lekcji musisz przyznać uprawnienia dostępu do pamięci urządzenia.\n\nKliknij OK, aby przyznać uprawnienia.</string>
<string name="privacy_policy_dialog_html"><![CDATA[Korzystając z aplikacji potwierdzasz <a href="https://szkolny.eu/privacy-policy">przeczytanie Polityki prywatności</a> i akceptujesz jej postanowienia.<br /><br />Autorzy aplikacji nie biorą odpowiedzialności za korzystanie z aplikacji Szkolny.eu.]]></string> <string name="privacy_policy_dialog_html"><![CDATA[Korzystając z aplikacji potwierdzasz <a href="https://szkolny.eu/privacy-policy">przeczytanie Polityki prywatności</a> i akceptujesz jej postanowienia.<br /><br />Autorzy aplikacji nie biorą odpowiedzialności za korzystanie z aplikacji Szkolny.eu.]]></string>
<string name="login_chooser_version_format">Szkolny.eu v%s\n%s</string> <string name="login_chooser_version_format">Szkolny.eu v%s\n%s</string>
<string name="menu_agenda_config">Ustawienia terminarza</string> <string name="menu_agenda_config">Ustawienia terminarza</string>