move some properties fetching functions in string extension

This commit is contained in:
tibbi 2017-10-18 20:27:45 +02:00
parent 8af2a3d606
commit 39eb430896
2 changed files with 56 additions and 40 deletions

View file

@ -12,7 +12,6 @@ import com.simplemobiletools.commons.extensions.*
import kotlinx.android.synthetic.main.dialog_properties.view.*
import kotlinx.android.synthetic.main.property_item.view.*
import java.io.File
import java.text.SimpleDateFormat
import java.util.*
class PropertiesDialog() {
@ -127,15 +126,9 @@ class PropertiesDialog() {
private fun addExifProperties(path: String) {
val exif = ExifInterface(path)
exif.getAttribute(ExifInterface.TAG_DATETIME).let {
if (it?.isNotEmpty() == true) {
try {
val simpleDateFormat = SimpleDateFormat("yyyy:MM:dd kk:mm:ss", Locale.ENGLISH)
val dateTaken = simpleDateFormat.parse(it).time.formatLastModified()
addProperty(R.string.date_taken, dateTaken)
} catch (ignored: Exception) {
}
}
val dateTaken = path.getFileExifDateTaken()
if (dateTaken.isNotEmpty()) {
addProperty(R.string.date_taken, dateTaken)
}
exif.getAttribute(ExifInterface.TAG_MAKE).let {
@ -145,36 +138,8 @@ class PropertiesDialog() {
}
}
var exifString = ""
exif.getAttribute(ExifInterface.TAG_F_NUMBER).let {
if (it?.isNotEmpty() == true) {
val number = it.trimEnd('0').trimEnd('.')
exifString += "F/$number "
}
}
exif.getAttribute(ExifInterface.TAG_FOCAL_LENGTH).let {
if (it?.isNotEmpty() == true) {
val values = it.split('/')
val focalLength = "${Math.round(values[0].toDouble() / values[1].toDouble())}mm"
exifString += "$focalLength "
}
}
exif.getAttribute(ExifInterface.TAG_EXPOSURE_TIME).let {
if (it?.isNotEmpty() == true) {
val exposureSec = (1 / it.toFloat()).toInt()
exifString += "1/${exposureSec}s "
}
}
exif.getAttribute(ExifInterface.TAG_ISO_SPEED_RATINGS).let {
if (it?.isNotEmpty() == true) {
exifString += "ISO-$it"
}
}
if (exifString.trim().isNotEmpty())
val exifString = path.getFileExifProperties()
if (exifString.isNotEmpty())
addProperty(R.string.exif, exifString.trim())
}

View file

@ -1,6 +1,8 @@
package com.simplemobiletools.commons.extensions
import android.content.Context
import android.media.ExifInterface
import java.text.SimpleDateFormat
import java.util.*
fun String.getFilenameFromPath() = substring(lastIndexOf("/") + 1)
@ -37,11 +39,60 @@ fun String.isPng() = endsWith(".png", true)
// fast extension checks, not guaranteed to be accurate
fun String.isVideoFast() = videoExtensions.any { endsWith(it, true) }
fun String.isImageFast() = photoExtensions.any { endsWith(it, true) }
fun String.isAudioFast() = audioExtensions.any { endsWith(it, true) }
fun String.areDigitsOnly() = matches(Regex("[0-9]+"))
fun String.getFileExifProperties(): String {
val exif = ExifInterface(this)
var exifString = ""
exif.getAttribute(ExifInterface.TAG_F_NUMBER).let {
if (it?.isNotEmpty() == true) {
val number = it.trimEnd('0').trimEnd('.')
exifString += "F/$number "
}
}
exif.getAttribute(ExifInterface.TAG_FOCAL_LENGTH).let {
if (it?.isNotEmpty() == true) {
val values = it.split('/')
val focalLength = "${Math.round(values[0].toDouble() / values[1].toDouble())}mm"
exifString += "$focalLength "
}
}
exif.getAttribute(ExifInterface.TAG_EXPOSURE_TIME).let {
if (it?.isNotEmpty() == true) {
val exposureSec = (1 / it.toFloat()).toInt()
exifString += "1/${exposureSec}s "
}
}
exif.getAttribute(ExifInterface.TAG_ISO_SPEED_RATINGS).let {
if (it?.isNotEmpty() == true) {
exifString += "ISO-$it"
}
}
return exifString.trim()
}
fun String.getFileExifDateTaken(): String {
val exif = ExifInterface(this)
exif.getAttribute(ExifInterface.TAG_DATETIME).let {
if (it?.isNotEmpty() == true) {
try {
val simpleDateFormat = SimpleDateFormat("yyyy:MM:dd kk:mm:ss", Locale.ENGLISH)
return simpleDateFormat.parse(it).time.formatLastModified().trim()
} catch (ignored: Exception) {
}
}
}
return ""
}
fun String.getMimeTypeFromPath(): String {
val typesMap = HashMap<String, String>().apply {
put("323", "text/h323")