2016-11-06 16:35:45 +01:00
|
|
|
package com.simplemobiletools.gallery
|
|
|
|
|
|
|
|
import android.app.Activity
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.content.res.Resources
|
|
|
|
import android.net.Uri
|
|
|
|
import android.os.Build
|
|
|
|
import android.util.DisplayMetrics
|
|
|
|
import android.util.TypedValue
|
2016-11-12 23:27:10 +01:00
|
|
|
import android.view.KeyCharacterMap
|
|
|
|
import android.view.KeyEvent
|
|
|
|
import android.view.ViewConfiguration
|
2016-11-06 16:35:45 +01:00
|
|
|
import android.webkit.MimeTypeMap
|
2016-11-12 12:12:05 +01:00
|
|
|
import com.simplemobiletools.filepicker.extensions.*
|
2016-11-06 16:35:45 +01:00
|
|
|
import com.simplemobiletools.gallery.models.Medium
|
|
|
|
import java.io.File
|
2016-11-12 15:24:40 +01:00
|
|
|
import java.util.*
|
2016-11-06 16:35:45 +01:00
|
|
|
|
|
|
|
class Utils {
|
|
|
|
companion object {
|
2016-11-13 11:17:01 +01:00
|
|
|
fun getFilename(context: Context, path: String): String {
|
|
|
|
val humanized = context.humanizePath(path)
|
|
|
|
return humanized.substring(humanized.lastIndexOf("/") + 1)
|
2016-11-06 16:35:45 +01:00
|
|
|
}
|
|
|
|
|
2016-11-13 11:17:01 +01:00
|
|
|
fun showToast(context: Context, resId: Int) = context.toast(resId)
|
|
|
|
|
2016-11-06 16:35:45 +01:00
|
|
|
fun getActionBarHeight(context: Context, res: Resources): Int {
|
|
|
|
val tv = TypedValue()
|
|
|
|
var height = 0
|
|
|
|
if (context.theme.resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
|
|
|
|
height = TypedValue.complexToDimensionPixelSize(tv.data, res.displayMetrics)
|
|
|
|
}
|
|
|
|
return height
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getStatusBarHeight(res: Resources): Int {
|
|
|
|
val id = res.getIdentifier("status_bar_height", "dimen", "android")
|
|
|
|
return if (id > 0) {
|
|
|
|
res.getDimensionPixelSize(id)
|
|
|
|
} else
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getNavBarHeight(res: Resources): Int {
|
|
|
|
val id = res.getIdentifier("navigation_bar_height", "dimen", "android")
|
|
|
|
return if (id > 0) {
|
|
|
|
res.getDimensionPixelSize(id)
|
|
|
|
} else
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
fun hasNavBar(act: Activity): Boolean {
|
|
|
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
|
|
|
val display = act.windowManager.defaultDisplay
|
|
|
|
|
|
|
|
val realDisplayMetrics = DisplayMetrics()
|
|
|
|
display.getRealMetrics(realDisplayMetrics)
|
|
|
|
|
|
|
|
val realHeight = realDisplayMetrics.heightPixels
|
|
|
|
val realWidth = realDisplayMetrics.widthPixels
|
|
|
|
|
|
|
|
val displayMetrics = DisplayMetrics()
|
|
|
|
display.getMetrics(displayMetrics)
|
|
|
|
|
|
|
|
val displayHeight = displayMetrics.heightPixels
|
|
|
|
val displayWidth = displayMetrics.widthPixels
|
|
|
|
|
|
|
|
realWidth - displayWidth > 0 || realHeight - displayHeight > 0
|
|
|
|
} else {
|
|
|
|
val hasMenuKey = ViewConfiguration.get(act).hasPermanentMenuKey()
|
|
|
|
val hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK)
|
|
|
|
!hasMenuKey && !hasBackKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 23:40:33 +01:00
|
|
|
fun hasStoragePermission(context: Context) = context.hasStoragePermission()
|
2016-11-06 16:35:45 +01:00
|
|
|
|
|
|
|
fun getMimeType(url: String): String {
|
|
|
|
val extension = MimeTypeMap.getFileExtensionFromUrl(url)
|
|
|
|
return if (extension != null) {
|
|
|
|
MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
|
|
|
|
} else
|
|
|
|
""
|
|
|
|
}
|
|
|
|
|
|
|
|
fun shareMedium(medium: Medium, activity: Activity) {
|
|
|
|
val shareTitle = activity.resources.getString(R.string.share_via)
|
|
|
|
val intent = Intent()
|
|
|
|
val file = File(medium.path)
|
|
|
|
val uri = Uri.fromFile(file)
|
|
|
|
intent.action = Intent.ACTION_SEND
|
|
|
|
intent.putExtra(Intent.EXTRA_STREAM, uri)
|
2016-11-12 23:01:57 +01:00
|
|
|
intent.type = medium.getMimeType()
|
2016-11-06 16:35:45 +01:00
|
|
|
activity.startActivity(Intent.createChooser(intent, shareTitle))
|
|
|
|
}
|
|
|
|
|
2016-11-13 11:45:43 +01:00
|
|
|
fun isAStorageRootFolder(context: Context, path: String) = context.isAStorageRootFolder(path)
|
2016-11-13 11:40:12 +01:00
|
|
|
|
2016-11-12 20:34:21 +01:00
|
|
|
fun isPhotoVideo(file: File) = file.isPhotoVideo()
|
|
|
|
|
2016-11-11 23:40:33 +01:00
|
|
|
fun needsStupidWritePermissions(context: Context, path: String) = context.needsStupidWritePermissions(path)
|
2016-11-06 17:10:42 +01:00
|
|
|
|
2016-11-11 23:40:33 +01:00
|
|
|
fun getFileDocument(context: Context, path: String, treeUri: String) = context.getFileDocument(path, treeUri)
|
2016-11-06 19:40:03 +01:00
|
|
|
|
2016-11-12 15:24:40 +01:00
|
|
|
fun scanPath(context: Context, path: String) = context.scanPath(path) {}
|
|
|
|
|
|
|
|
fun scanFiles(context: Context, files: ArrayList<File>) = context.scanFiles(files) {}
|
2016-11-06 16:35:45 +01:00
|
|
|
}
|
|
|
|
}
|