lets require only a file path, instead of FileDir for getting the properties

This commit is contained in:
tibbi 2016-10-16 21:37:06 +02:00
parent d55df91c6c
commit 48454140f2
4 changed files with 93 additions and 70 deletions

View file

@ -392,7 +392,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
return; return;
final Config config = Config.newInstance(getContext()); final Config config = Config.newInstance(getContext());
new PropertiesDialog(getContext(), item, config.getShowHidden()); new PropertiesDialog(getContext(), item.getPath(), config.getShowHidden());
} }
private void displayRenameDialog() { private void displayRenameDialog() {

View file

@ -8,8 +8,7 @@ import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import com.simplemobiletools.filemanager.Config import com.simplemobiletools.filemanager.Config
import com.simplemobiletools.filemanager.R import com.simplemobiletools.filemanager.R
import com.simplemobiletools.filemanager.extensions.formatSize import com.simplemobiletools.filemanager.extensions.*
import com.simplemobiletools.filepicker.models.FileDirItem
import kotlinx.android.synthetic.main.item_properties.view.* import kotlinx.android.synthetic.main.item_properties.view.*
import kotlinx.android.synthetic.main.property_item.view.* import kotlinx.android.synthetic.main.property_item.view.*
import java.io.File import java.io.File
@ -17,7 +16,6 @@ import java.util.*
class PropertiesDialog() { class PropertiesDialog() {
lateinit var mContext: Context lateinit var mContext: Context
lateinit var mItem: FileDirItem
lateinit var mInflater: LayoutInflater lateinit var mInflater: LayoutInflater
lateinit var mPropertyView: ViewGroup lateinit var mPropertyView: ViewGroup
lateinit var mResources: Resources lateinit var mResources: Resources
@ -25,31 +23,30 @@ class PropertiesDialog() {
private var mCountHiddenItems = false private var mCountHiddenItems = false
private var mFilesCnt = 0 private var mFilesCnt = 0
constructor(context: Context, item: FileDirItem, countHiddenItems: Boolean = false) : this() { constructor(context: Context, path: String, countHiddenItems: Boolean = false) : this() {
mContext = context mContext = context
mItem = item
mCountHiddenItems = countHiddenItems mCountHiddenItems = countHiddenItems
mInflater = LayoutInflater.from(context) mInflater = LayoutInflater.from(context)
mResources = mContext.resources mResources = mContext.resources
val file = File(mItem.path) val file = File(path)
val title = if (mItem.isDirectory) R.string.directory_properties else R.string.file_properties val title = if (file.isDirectory) R.string.directory_properties else R.string.file_properties
mPropertyView = mInflater.inflate(R.layout.item_properties, null) as ViewGroup mPropertyView = mInflater.inflate(R.layout.item_properties, null) as ViewGroup
addProperty(R.string.name, mItem.name) addProperty(R.string.name, file.name)
addProperty(R.string.path, mItem.path) addProperty(R.string.path, path)
addProperty(R.string.size, getItemSize()) addProperty(R.string.size, getItemSize(file))
addProperty(R.string.last_modified, formatLastModified(file.lastModified())) addProperty(R.string.last_modified, formatLastModified(file.lastModified()))
if (mItem.isDirectory) { if (file.isDirectory) {
addProperty(R.string.files_count, mFilesCnt.toString()) addProperty(R.string.files_count, mFilesCnt.toString())
} else if (mItem.isImage()) { } else if (file.isImage()) {
addProperty(R.string.resolution, mItem.getImageResolution()) addProperty(R.string.resolution, file.getImageResolution())
} else if (mItem.isAudio()) { } else if (file.isAudio()) {
addProperty(R.string.duration, mItem.getDuration()) addProperty(R.string.duration, file.getDuration())
} else if (mItem.isVideo()) { } else if (file.isVideo()) {
addProperty(R.string.duration, mItem.getDuration()) addProperty(R.string.duration, file.getDuration())
addProperty(R.string.resolution, mItem.getVideoResolution()) addProperty(R.string.resolution, file.getVideoResolution())
} }
AlertDialog.Builder(context) AlertDialog.Builder(context)
@ -67,13 +64,13 @@ class PropertiesDialog() {
mPropertyView.properties_holder.addView(view) mPropertyView.properties_holder.addView(view)
} }
private fun getItemSize(): String { private fun getItemSize(file: File): String {
if (mItem.isDirectory) { if (file.isDirectory) {
mCountHiddenItems = Config.newInstance(mContext).showHidden mCountHiddenItems = Config.newInstance(mContext).showHidden
return getDirectorySize(File(mItem.path)).formatSize() return getDirectorySize(File(file.path)).formatSize()
} }
return mItem.size.formatSize() return file.length().formatSize()
} }
private fun formatLastModified(ts: Long): String { private fun formatLastModified(ts: Long): String {

View file

@ -0,0 +1,73 @@
package com.simplemobiletools.filemanager.extensions
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.media.MediaMetadataRetriever
import java.io.File
import java.util.*
fun File.isGif() = name.toLowerCase().endsWith(".gif")
fun File.isVideo() = getMimeType().startsWith("video")
fun File.isAudio() = getMimeType().startsWith("audio")
fun File.isImage(): Boolean {
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeFile(path, options)
return options.outWidth != -1 && options.outHeight != -1
}
fun File.getMimeType(): String {
try {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(path)
return retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE)
} catch (ignored: Exception) {
}
return ""
}
fun File.getDuration(): String {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(path)
val time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
val timeInMillisec = java.lang.Long.parseLong(time)
return getFormattedDuration((timeInMillisec / 1000).toInt())
}
fun File.getVideoResolution(): String {
try {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(path)
val width = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)
val height = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)
return "$width x $height"
} catch (ignored: Exception) {
}
return ""
}
fun File.getImageResolution(): String {
val bitmap: Bitmap? = BitmapFactory.decodeFile(path)
if (bitmap == null)
return ""
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()
}

View file

@ -1,9 +1,7 @@
package com.simplemobiletools.filepicker.models package com.simplemobiletools.filepicker.models
import android.graphics.Bitmap
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.media.MediaMetadataRetriever import android.media.MediaMetadataRetriever
import java.util.*
class FileDirItem(val path: String, val name: String, val isDirectory: Boolean, val children: Int, val size: Long) : class FileDirItem(val path: String, val name: String, val isDirectory: Boolean, val children: Int, val size: Long) :
Comparable<FileDirItem> { Comparable<FileDirItem> {
@ -24,7 +22,6 @@ class FileDirItem(val path: String, val name: String, val isDirectory: Boolean,
fun isGif() = name.toLowerCase().endsWith(".gif") fun isGif() = name.toLowerCase().endsWith(".gif")
fun isVideo() = getMimeType().startsWith("video") fun isVideo() = getMimeType().startsWith("video")
fun isAudio() = getMimeType().startsWith("audio")
fun isImage(): Boolean { fun isImage(): Boolean {
val options = BitmapFactory.Options() val options = BitmapFactory.Options()
@ -43,48 +40,4 @@ class FileDirItem(val path: String, val name: String, val isDirectory: Boolean,
} }
return "" return ""
} }
fun getDuration(): String {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(path)
val time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
val timeInMillisec = java.lang.Long.parseLong(time)
return getFormattedDuration((timeInMillisec / 1000).toInt())
}
fun getVideoResolution(): String {
try {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(path)
val width = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)
val height = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)
return "$width x $height"
} catch (ignored: Exception) {
}
return ""
}
fun getImageResolution(): String {
val bitmap: Bitmap? = BitmapFactory.decodeFile(path)
if (bitmap == null)
return ""
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()
}
} }