Guest User

Untitled

a guest
May 16th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. import android.graphics.*
  2. import android.text.TextPaint
  3. import android.text.style.TypefaceSpan
  4. import java.util.*
  5. import java.util.regex.Pattern
  6. import java.io.*
  7. import java.math.BigInteger
  8. import java.security.MessageDigest
  9. import java.security.NoSuchAlgorithmException
  10. import kotlin.experimental.and
  11. import android.text.TextUtils
  12. import android.util.Log
  13. import com.amazonaws.util.Md5Utils
  14.  
  15.  
  16. /**
  17. * Created by armshare on 22/2/2018 AD.
  18. */
  19.  
  20. fun String.isValidEmail(): Boolean {
  21. val expression = "^[\\w\\.]+@([\\w]+\\.)+[A-Z]{2,7}$"
  22. val inputString = this
  23. val pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE)
  24. val matcher = pattern.matcher(inputString)
  25. return matcher.matches()
  26. }
  27.  
  28. fun LinkedHashMap<String,String>.getKeyByIndex(index:Int):String{
  29. return keys.toTypedArray()[index]
  30. }
  31.  
  32. fun LinkedHashMap<String,String>.getValueByIndex(index:Int):String{
  33. return values.toTypedArray()[index]
  34. }
  35.  
  36. fun File.lengthInMegaByte():Long {
  37. return (Math.ceil(((length() / (1024 * 1024) * 10).toDouble())) / 10).toLong()
  38. }
  39.  
  40. fun Long.convertToMegaByte():Long{
  41. return (Math.ceil(((this / (1024 * 1024) * 10).toDouble())) / 10).toLong()
  42.  
  43. }
  44.  
  45. fun File.getAllChild(): List<File>{
  46. val inFiles = ArrayList<File>()
  47. val files = LinkedList<File>()
  48. if (listFiles() != null && listFiles().isNotEmpty()){
  49. files.addAll(listFiles())
  50. while (!files.isEmpty()) {
  51. val file = files.remove()
  52. if (file.isDirectory) {
  53. files.addAll(file.listFiles())
  54. }
  55. else
  56. inFiles.add(file)
  57. }
  58. }
  59.  
  60. return inFiles
  61. }
  62.  
  63. fun Long.suffixScaleNumber(): String {
  64. if (this < 1000) return "" + this
  65. val exp = (Math.log(this.toDouble()) / Math.log(1000.0)).toInt()
  66.  
  67. var string = String.format("%.1f",
  68. this / Math.pow(1000.0, exp.toDouble()))
  69.  
  70. if(string.endsWith(".0"))
  71. string = string.replace(".0" , "")
  72. return string+"KMB"[exp - 1]
  73. }
  74.  
  75.  
  76. fun File.md5Hex():String{
  77. val result = StringBuffer()
  78. val HEX_CHARS = "0123456789ABCDEF".toCharArray()
  79.  
  80. Md5Utils.computeMD5Hash(this)
  81. .forEach {
  82. val octet = it.toInt()
  83. val firstIndex = (octet and 0xF0).ushr(4)
  84. val secondIndex = octet and 0x0F
  85. result.append(HEX_CHARS[firstIndex])
  86. result.append(HEX_CHARS[secondIndex])
  87. }
  88.  
  89. return result.toString()
  90. }
Add Comment
Please, Sign In to add comment