-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContextX.kt
94 lines (76 loc) · 3.13 KB
/
ContextX.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
@file:Suppress("unused")
package com.rstech.camerascanner.pdfscanner.camscanner.easy.scan.camscanner.model
import android.annotation.SuppressLint
import android.app.Activity
import android.content.ContentResolver
import android.content.Context
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.net.Uri
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.TextView
import androidx.annotation.*
import androidx.core.content.ContextCompat
import com.rstech.camerascanner.pdfscanner.camscanner.easy.scan.camscanner.R
internal val Context.layoutInflater: LayoutInflater
get() = LayoutInflater.from(this)
internal val Context.inputMethodManager
get() = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
internal fun Context.getDrawableCompat(@DrawableRes drawable: Int) =
ContextCompat.getDrawable(this, drawable)
internal fun Context.getColorCompat(@ColorRes color: Int) = ContextCompat.getColor(this, color)
internal fun ViewGroup.inflate(@LayoutRes layoutRes: Int, attachToRoot: Boolean = false): View {
return context.layoutInflater.inflate(layoutRes, this, attachToRoot)
}
internal fun TextView.setTextColorRes(@ColorRes color: Int) =
setTextColor(context.getColorCompat(color))
///hide soft keyboard
fun Activity.hideKeyboard() {
hideKeyboard(currentFocus ?: View(this))
}
fun Context.hideKeyboard(view: View) {
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
///getAppIcon of any app installed with just package name and context
///your app must declare the target app in queries or have query all packages permission
fun Context.getAppIcon(packageName: String?): Drawable? {
return packageName?.let {
try {
packageManager.getApplicationIcon(packageName)
} catch (e: Exception) {
null
}
}
}
///get color Int From attr name (example: R.attr.colorPrimary)
@ColorInt
fun Context.themeColor(@AttrRes attrRes: Int): Int = TypedValue()
.apply { theme.resolveAttribute(attrRes, this, true) }
.data
///get Uri of any resource
internal fun Context.getResourceUri(@AnyRes resourceId: Int): Uri = Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(packageName)
.path(resourceId.toString())
.build()
///get resource id by name
@SuppressLint("DiscouragedApi")
internal fun Context.resIdByName(resIdName: String?, resType: String): Int {
resIdName?.let {
return resources.getIdentifier(it, resType, packageName)
}
throw Resources.NotFoundException()
}
///get drawable by id
@SuppressLint("DiscouragedApi")
internal fun Context.drawableIdByName(resIdName: String?): Int {
return if (resIdName != null) {
resources.getIdentifier(resIdName, "drawable", packageName)
} else
R.drawable.ic_launcher_foreground
}