Skip to content

Commit

Permalink
resources: smoother filename extraction (fixes #3509) (#3513)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
rlam20 and dogi authored May 30, 2024
1 parent 08e7721 commit c3233ab
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 35 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 21
targetSdkVersion 34
versionCode 1550
versionName "0.15.50"
versionCode 1543
versionName "0.15.51"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.appcompat.app.AppCompatActivity
import com.opencsv.CSVParserBuilder
import com.opencsv.CSVReaderBuilder
import org.ole.planet.myplanet.databinding.ActivityCsvviewerBinding
import org.ole.planet.myplanet.utilities.FileUtils
import java.io.File
import java.io.FileReader

Expand All @@ -22,15 +23,13 @@ class CSVViewerActivity : AppCompatActivity() {
val csvFileOpenIntent = intent
val fileName = csvFileOpenIntent.getStringExtra("TOUCHED_FILE")
if (!fileName.isNullOrEmpty()) {
val regex = Regex(".+/(.+\\.csv)")
val matchResult = regex.find(fileName)
val nameWithExtension = matchResult?.groupValues?.get(1)
val nameWithoutExtension = nameWithExtension?.substringBeforeLast(".")
activityCsvviewerBinding.csvFileName.text = nameWithoutExtension
activityCsvviewerBinding.csvFileName.text = FileUtils.nameWithoutExtension(fileName)
activityCsvviewerBinding.csvFileName.visibility = View.VISIBLE
} else {
activityCsvviewerBinding.csvFileName.text = "No file selected"
activityCsvviewerBinding.csvFileName.visibility = View.VISIBLE


}

try {
val csvFile: File = if (fileName!!.startsWith("/")) {
File(fileName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import org.ole.planet.myplanet.databinding.ActivityImageViewerBinding
import org.ole.planet.myplanet.utilities.FileUtils
import java.io.File
import java.util.regex.Pattern

Expand All @@ -24,15 +25,14 @@ class ImageViewerActivity : AppCompatActivity() {
val isFullPath = intent.getBooleanExtra("isFullPath", false)
val imageOpenIntent = intent
fileName = imageOpenIntent.getStringExtra("TOUCHED_FILE")
if (fileName != null && fileName?.isNotEmpty() == true) {
val regex = Regex(".+/([^/]+\\.(jpg|jpeg|png|gif|bmp))")

val matchResult = regex.find(fileName ?: "")
val nameWithExtension = matchResult?.groupValues?.get(1)
val nameWithoutExtension = nameWithExtension?.substringBefore(".")
activityImageViewerBinding.imageFileName.text = nameWithoutExtension
if (!fileName.isNullOrEmpty()) {
activityImageViewerBinding.imageFileName.text = FileUtils.nameWithoutExtension(fileName)
activityImageViewerBinding.imageFileName.visibility = View.VISIBLE
} else {
activityImageViewerBinding.imageFileName.text = "No file selected"
activityImageViewerBinding.imageFileName.visibility = View.VISIBLE
}

if (fileName?.matches(".*[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}.*".toRegex()) == true) {
displayCapturedImage()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.databinding.ActivityMarkdownViewerBinding
import org.ole.planet.myplanet.utilities.FileUtils
import org.ole.planet.myplanet.utilities.Markdown.setMarkdownText
import java.io.BufferedReader
import java.io.File
Expand All @@ -27,11 +28,10 @@ class MarkdownViewerActivity : AppCompatActivity() {
fileName = markdownOpenIntent.getStringExtra("TOUCHED_FILE")
if (!fileName.isNullOrEmpty()) {

val regex = Regex(".+/(.+\\.md)")
val matchResult = regex.find(fileName ?: "")
val nameWithExtension = matchResult?.groupValues?.get(1)
val nameWithoutExtension = nameWithExtension?.substringBeforeLast(".")
activityMarkdownViewerBinding.markdownFileName.text = nameWithoutExtension
activityMarkdownViewerBinding.markdownFileName.text = FileUtils.nameWithoutExtension(fileName)
activityMarkdownViewerBinding.markdownFileName.visibility = View.VISIBLE
} else {
activityMarkdownViewerBinding.markdownFileName.text = "No file selected"
activityMarkdownViewerBinding.markdownFileName.visibility = View.VISIBLE
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.ole.planet.myplanet.model.RealmMyLibrary
import org.ole.planet.myplanet.service.AudioRecorderService
import org.ole.planet.myplanet.service.AudioRecorderService.AudioRecordListener
import org.ole.planet.myplanet.ui.resources.AddResourceFragment
import org.ole.planet.myplanet.utilities.FileUtils
import org.ole.planet.myplanet.utilities.IntentUtils.openAudioFile
import org.ole.planet.myplanet.utilities.NotificationUtil.cancellAll
import org.ole.planet.myplanet.utilities.NotificationUtil.create
Expand Down Expand Up @@ -57,12 +58,11 @@ class PDFReaderActivity : AppCompatActivity(), OnPageChangeListener, OnLoadCompl
private fun renderPdfFile() {
val pdfOpenIntent = intent
fileName = pdfOpenIntent.getStringExtra("TOUCHED_FILE")
if (fileName != null && fileName?.isNotEmpty() == true) {
val regex = Regex(".+/(.+\\.pdf)")
val matchResult = regex.find(fileName ?: "")
val nameWithExtension = matchResult?.groupValues?.get(1)
val nameWithoutExtension = nameWithExtension?.substringBeforeLast(".")
activityPdfreaderBinding.pdfFileName.text = nameWithoutExtension
if (!fileName.isNullOrEmpty()) {
activityPdfreaderBinding.pdfFileName.text = FileUtils.nameWithoutExtension(fileName)
activityPdfreaderBinding.pdfFileName.visibility = View.VISIBLE
} else {
activityPdfreaderBinding.pdfFileName.text = "No file selected"
activityPdfreaderBinding.pdfFileName.visibility = View.VISIBLE
}
val file = File(getExternalFilesDir(null), "ole/$fileName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import org.ole.planet.myplanet.databinding.ActivityTextfileViewerBinding
import org.ole.planet.myplanet.utilities.FileUtils
import java.io.BufferedReader
import java.io.File
import java.io.FileReader
Expand All @@ -21,18 +22,15 @@ class TextFileViewerActivity : AppCompatActivity() {
private fun renderTextFile() {
val textFileOpenIntent = intent
fileName = textFileOpenIntent.getStringExtra("TOUCHED_FILE")
if (fileName != null && fileName?.isNotEmpty() == true) {

val regex = Regex(".+/(.+\\.txt)")
val matchResult = regex.find(fileName ?: "")
val nameWithExtension = matchResult?.groupValues?.get(1)
val nameWithoutExtension = nameWithExtension?.substringBeforeLast(".")
activityTextfileViewerBinding.textFileName.text = nameWithoutExtension
if (!fileName.isNullOrEmpty()) {
activityTextfileViewerBinding.textFileName.text = FileUtils.nameWithoutExtension(fileName)
activityTextfileViewerBinding.textFileName.visibility = View.VISIBLE
} else {
activityTextfileViewerBinding.textFileName.text = "No file selected"
activityTextfileViewerBinding.textFileName.visibility = View.VISIBLE
}
renderTextFileThread()
}

private fun renderTextFileThread() {
val openTextFileThread: Thread = object : Thread() {
override fun run() {
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/org/ole/planet/myplanet/utilities/FileUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,18 @@ object FileUtils {
return Pair(totalBytes, availableBytes)
}
}
fun extractFileName(filePath: String?): String?{
if(filePath.isNullOrEmpty()) return null
val regex = Regex(".+/(.+\\.[a-zA-Z0-9]+)")
return regex.find(filePath)?.groupValues?.get(1)
}

fun nameWithoutExtension(fileName: String?): String?{
extractFileName(fileName)
val nameWithExtension = FileUtils.extractFileName(fileName)
val nameWithoutExtension = nameWithExtension?.substringBeforeLast(".")
return nameWithoutExtension
}


}

0 comments on commit c3233ab

Please sign in to comment.