Guest User

Untitled

a guest
Nov 22nd, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. import android.content.ComponentName
  2. import android.content.Context
  3. import android.content.CursorLoader
  4. import android.content.pm.PackageManager
  5. import android.net.ConnectivityManager
  6. import android.net.Uri
  7. import android.os.Looper
  8. import android.provider.MediaStore
  9. import android.support.annotation.ColorRes
  10. import android.support.annotation.StringRes
  11. import android.support.v4.app.Fragment
  12. import android.support.v4.content.ContextCompat
  13. import android.widget.Toast
  14.  
  15.  
  16. fun Context.isNetworkConnected(): Boolean {
  17. val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
  18. return cm.activeNetworkInfo?.isConnectedOrConnecting ?: false
  19. }
  20.  
  21. fun Context.toggleAndroidComponent(componentClass: Class<*>, enable: Boolean) {
  22. val componentName = ComponentName(this, componentClass)
  23.  
  24. val newState = if (enable)
  25. PackageManager.COMPONENT_ENABLED_STATE_ENABLED
  26. else
  27. PackageManager.COMPONENT_ENABLED_STATE_DISABLED
  28.  
  29. packageManager.setComponentEnabledSetting(componentName, newState, PackageManager.DONT_KILL_APP)
  30. }
  31.  
  32. /**
  33. * Default short toast
  34. */
  35. fun Context.toast(any: Any, duration: Int = Toast.LENGTH_SHORT){
  36. Toast.makeText(this, any.toString(), duration).show()
  37. }
  38.  
  39. /**
  40. * Default short toast
  41. */
  42. fun Context.toast(@StringRes resString: Int, duration: Int = Toast.LENGTH_SHORT){
  43. toast(getString(resString), duration)
  44. }
  45.  
  46. /**
  47. * Long duration toast
  48. */
  49. fun Context.longToast(any: Any){
  50. toast(any.toString(), Toast.LENGTH_LONG)
  51. }
  52.  
  53. /**
  54. * Long duration toast
  55. */
  56. fun Context.longToast(@StringRes stringRes: Int){
  57. toast(getString(stringRes), Toast.LENGTH_LONG)
  58. }
  59.  
  60. fun Fragment.toast(message: String, duration: Int = Toast.LENGTH_SHORT){
  61. context.toast(message, duration)
  62. }
  63.  
  64. fun Fragment.toast(@StringRes resString: Int, duration: Int = Toast.LENGTH_SHORT){
  65. context.toast(getString(resString), duration)
  66. }
  67. fun Fragment.longToast(@StringRes stringRes: Int){
  68. context.toast(getString(stringRes), Toast.LENGTH_LONG)
  69. }
  70.  
  71.  
  72. fun Context.getPath(contentUri: Uri): String {
  73. val proj = arrayOf(MediaStore.Images.Media.DATA)
  74. val result: String
  75.  
  76. if (Looper.myLooper() == null) {
  77. Looper.prepare()
  78. }
  79. val cursorLoader = CursorLoader(
  80. this,
  81. contentUri, proj, null, null, null)
  82. val cursor = cursorLoader.loadInBackground()
  83.  
  84. result = if (cursor != null) {
  85. val column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
  86. cursor.moveToFirst()
  87. cursor.getString(column_index)
  88. } else {
  89. contentUri.path
  90. }
  91.  
  92. return result
  93. }
  94.  
  95. fun Context.getColorCompat(@ColorRes resId: Int): Int {
  96. return ContextCompat.getColor(this, resId)
  97. }
Add Comment
Please, Sign In to add comment