Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
fix: show error message when file cant be written (see #190)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Sep 8, 2023
1 parent 81dccc5 commit 3a313ef
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bnyro.recorder.services

import android.widget.Toast
import com.bnyro.recorder.App
import com.bnyro.recorder.R
import com.bnyro.recorder.enums.AudioChannels
Expand Down Expand Up @@ -41,6 +42,12 @@ class AudioRecorderService : RecorderService() {
outputFile = (application as App).fileRepository.getOutputFile(
audioFormat.extension
)
if (outputFile == null) {
Toast.makeText(this@AudioRecorderService, R.string.cant_access_selected_folder, Toast.LENGTH_LONG).show()
onDestroy()
return
}

fileDescriptor = contentResolver.openFileDescriptor(outputFile!!.uri, "w")
setOutputFile(fileDescriptor?.fileDescriptor)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.media.AudioFormat
import android.media.AudioRecord
import android.media.MediaRecorder
import android.os.Build
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.documentfile.provider.DocumentFile
import com.bnyro.recorder.App
Expand Down Expand Up @@ -112,9 +113,15 @@ class LosslessRecorderService : RecorderService() {
val inputStream = contentResolver.openInputStream(outputFile?.uri ?: return) ?: return
val outputStream = (application as App).fileRepository
.getOutputFile(FILE_NAME_EXTENSION_WAV)
.let {
contentResolver.openOutputStream(it.uri) ?: return
?.let {
contentResolver.openOutputStream(it.uri)
}

if (outputStream == null) {
Toast.makeText(this, R.string.cant_access_selected_folder, Toast.LENGTH_LONG).show()
return
}

pcmConverter?.convertToWave(inputStream, outputStream, BUFFER_SIZE_IN_BYTES)
outputFile?.delete()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.os.Build
import android.util.DisplayMetrics
import android.util.Log
import android.view.Display
import android.widget.Toast
import androidx.activity.result.ActivityResult
import com.bnyro.recorder.App
import com.bnyro.recorder.R
Expand Down Expand Up @@ -115,8 +116,13 @@ class ScreenRecorderService : RecorderService() {
null
)

outputFile = (application as App).fileRepository
.getOutputFile(videoFormat.extension)
outputFile = (application as App).fileRepository.getOutputFile(videoFormat.extension)
if (outputFile == null) {
Toast.makeText(this@ScreenRecorderService, R.string.cant_access_selected_folder, Toast.LENGTH_LONG).show()
onDestroy()
return
}

fileDescriptor = contentResolver.openFileDescriptor(outputFile!!.uri, "w")
setOutputFile(fileDescriptor?.fileDescriptor)

Expand Down
37 changes: 17 additions & 20 deletions app/src/main/java/com/bnyro/recorder/util/FileRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package com.bnyro.recorder.util
import android.annotation.SuppressLint
import android.content.Context
import android.media.MediaMetadataRetriever
import android.net.Uri
import android.util.Log
import androidx.core.net.toUri
import androidx.documentfile.provider.DocumentFile
import com.bnyro.recorder.enums.RecorderType
import com.bnyro.recorder.enums.SortOrder
Expand All @@ -18,7 +19,7 @@ interface FileRepository {
suspend fun getAudioRecordingItems(sortOrder: SortOrder): List<RecordingItemData>
suspend fun deleteFiles(files: List<DocumentFile>)
suspend fun deleteAllFiles()
fun getOutputFile(extension: String, prefix: String = ""): DocumentFile
fun getOutputFile(extension: String, prefix: String = ""): DocumentFile?
fun getOutputDir(): DocumentFile
}

Expand All @@ -39,10 +40,7 @@ class FileRepositoryImpl(val context: Context) : FileRepository {
getVideoFiles().sortedBy(sortOrder).map {
val thumbnail =
MediaMetadataRetriever().apply {
setDataSource(
context,
it.uri
)
setDataSource(context, it.uri)
}.frameAtTime
RecordingItemData(it, RecorderType.VIDEO, thumbnail)
}
Expand Down Expand Up @@ -73,7 +71,7 @@ class FileRepositoryImpl(val context: Context) : FileRepository {
}
}

override fun getOutputFile(extension: String, prefix: String): DocumentFile {
override fun getOutputFile(extension: String, prefix: String): DocumentFile? {
val currentTimeMillis = Calendar.getInstance().time
val currentDateTime = dateTimeFormat.format(currentTimeMillis)
val currentDate = currentDateTime.split("_").first()
Expand All @@ -87,22 +85,26 @@ class FileRepositoryImpl(val context: Context) : FileRepository {
.replace("%t", currentTime)
.replace("%m", currentTimeMillis.time.toString())
.replace("%s", currentTimeMillis.time.div(1000).toString())

val recordingFile = getOutputDir().createFile("audio/*", "$prefix$fileName.$extension")
return recordingFile!!

val outputDir = getOutputDir()
if (!outputDir.exists() || !outputDir.canRead() || !outputDir.canWrite()) return null

Log.e("out", Preferences.prefs.getString(Preferences.targetFolderKey, "").toString())

return outputDir
.createFile("audio/*", "$prefix$fileName.$extension")
}

override fun getOutputDir(): DocumentFile {
val prefDir = Preferences.prefs.getString(Preferences.targetFolderKey, "")
val audioDir = when {
return when {
prefDir.isNullOrBlank() -> {
val dir = context.getExternalFilesDir(null) ?: context.filesDir
DocumentFile.fromFile(dir)
}

else -> DocumentFile.fromTreeUri(context, Uri.parse(prefDir))
else -> DocumentFile.fromTreeUri(context, prefDir.toUri())!!
}
return audioDir!!
}

companion object {
Expand All @@ -117,12 +119,7 @@ fun List<DocumentFile>.sortedBy(sortOrder: SortOrder): List<DocumentFile> {
SortOrder.DEFAULT -> this
SortOrder.ALPHABETIC -> sortedBy { it.name }
SortOrder.ALPHABETIC_REV -> sortedByDescending { it.name }
SortOrder.SIZE_REV -> sortedBy {
it.length()
}

SortOrder.SIZE -> sortedByDescending {
it.length()
}
SortOrder.SIZE_REV -> sortedBy { it.length() }
SortOrder.SIZE -> sortedByDescending { it.length() }
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/recorder/util/MediaTrimmer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MediaTrimmer {
extension = extension ?: "mp4",
prefix = "Trim_"
)
val pfd = context.contentResolver.openFileDescriptor(outputFile.uri, "w")!!
val pfd = context.contentResolver.openFileDescriptor(outputFile!!.uri, "w")!!
trimMediaFile(inputFilePath, pfd, startMs, endMs)
}
}
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/com/bnyro/recorder/util/PickFolderContract.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class PickFolderContract : ActivityResultContract<Uri?, Uri?>() {
this.context = context

val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
flags = Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
flags = Intent.FLAG_GRANT_WRITE_URI_PERMISSION or
Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && input != null) {
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, input)
Expand All @@ -32,10 +34,8 @@ class PickFolderContract : ActivityResultContract<Uri?, Uri?>() {

override fun parseResult(resultCode: Int, intent: Intent?): Uri? {
return intent.takeIf { resultCode == Activity.RESULT_OK }?.data?.also {
context.contentResolver.takePersistableUriPermission(
it,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
context.contentResolver.takePersistableUriPermission(it, flags)
}
}
}
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<string name="screen_recorder_annotation">Screen Record Annotation</string>
<string name="screen_recorder_annotation_desc">Show annotation tool during screen recording</string>
<string name="amoled_dark">Amoled Dark</string>
<!-- Other info -->
<string name="default_sort">Default</string>
<string name="audio_visualizer_timestamps">Audio visualizer timestamps</string>
<string name="audio_visualizer_timestamps_description">Show timestamps in the audio visualizer timeline.</string>
Expand All @@ -77,6 +78,7 @@
<string name="start_timestamp">Start:</string>
<string name="select_trim_range">Select Trim Range</string>
<string name="trim_failed">Trim Failed</string>
<string name="trimming">Trimming...</string>
<string name="trimming">Trimming</string>
<string name="trim_successful">Trim Successful</string>
<string name="cant_access_selected_folder">Can\'t access selected folder!</string>
</resources>

0 comments on commit 3a313ef

Please sign in to comment.