From 6aaca003a9253d3d7568e88c2062b8e8f0694e70 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 16 Oct 2016 17:32:48 +0200 Subject: [PATCH] add a couple more helper functions about files --- .../filemanager/dialogs/PropertiesDialog.kt | 4 ++ .../filepicker/models/FileDirItem.kt | 47 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/PropertiesDialog.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/PropertiesDialog.kt index fc2e324c..681674df 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/PropertiesDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/PropertiesDialog.kt @@ -44,6 +44,10 @@ class PropertiesDialog : DialogFragment() { properties_resolution_label.visibility = View.VISIBLE properties_resolution.visibility = View.VISIBLE properties_resolution.text = mItem.resolution + } else if (mItem.isAudio(context)) { + + } else if (mItem.isVideo(context)) { + } val file = File(mItem.path) diff --git a/library/src/main/kotlin/com/simplemobiletools/filepicker/models/FileDirItem.kt b/library/src/main/kotlin/com/simplemobiletools/filepicker/models/FileDirItem.kt index d3e0c8bb..87ca8f27 100644 --- a/library/src/main/kotlin/com/simplemobiletools/filepicker/models/FileDirItem.kt +++ b/library/src/main/kotlin/com/simplemobiletools/filepicker/models/FileDirItem.kt @@ -1,7 +1,12 @@ package com.simplemobiletools.filepicker.models +import android.content.Context import android.graphics.Bitmap import android.graphics.BitmapFactory +import android.media.MediaMetadataRetriever +import android.net.Uri +import java.io.File +import java.util.* class FileDirItem(val path: String, val name: String, val isDirectory: Boolean, val children: Int, val size: Long) : Comparable { @@ -27,6 +32,33 @@ class FileDirItem(val path: String, val name: String, val isDirectory: Boolean, return options.outWidth != -1 && options.outHeight != -1 } + fun isVideo(context: Context): Boolean { + return getMimeType(context).startsWith("video") + } + + fun isAudio(context: Context): Boolean { + return getMimeType(context).startsWith("audio") + } + + fun getMimeType(context: Context): String { + try { + val retriever = MediaMetadataRetriever() + retriever.setDataSource(context, Uri.fromFile(File(path))) + return retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE) + } catch (ignored: Exception) { + + } + return "" + } + + fun getDuration(context: Context): String { + val retriever = MediaMetadataRetriever() + retriever.setDataSource(context, Uri.fromFile(File(path))) + val time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION) + val timeInMillisec = java.lang.Long.parseLong(time) + return getFormattedDuration((timeInMillisec / 1000).toInt()) + } + val resolution: String get () { val bitmap: Bitmap? = BitmapFactory.decodeFile(path) @@ -35,4 +67,19 @@ class FileDirItem(val path: String, val name: String, val isDirectory: Boolean, return "${bitmap.width} x ${bitmap.height}" } + + private fun getFormattedDuration(duration: Int): String { + val sb = StringBuilder(8) + val hours = duration / (60 * 60) + val minutes = duration % (60 * 60) / 60 + val seconds = duration % (60 * 60) % 60 + + if (duration > 3600) { + sb.append(String.format(Locale.getDefault(), "%02d", hours)).append(":") + } + + sb.append(String.format(Locale.getDefault(), "%02d", minutes)) + sb.append(":").append(String.format(Locale.getDefault(), "%02d", seconds)) + return sb.toString() + } }