mirror of
https://github.com/mihonapp/mihon.git
synced 2025-11-14 04:58:56 +01:00
Merge and remove util classes
This commit is contained in:
@@ -4,6 +4,7 @@ import android.app.Notification
|
||||
import android.app.NotificationManager
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import android.content.res.Resources
|
||||
import android.net.ConnectivityManager
|
||||
import android.os.PowerManager
|
||||
import android.support.annotation.StringRes
|
||||
@@ -13,6 +14,7 @@ import android.widget.Toast
|
||||
|
||||
/**
|
||||
* Display a toast in this context.
|
||||
*
|
||||
* @param resource the text resource.
|
||||
* @param duration the duration of the toast. Defaults to short.
|
||||
*/
|
||||
@@ -22,6 +24,7 @@ fun Context.toast(@StringRes resource: Int, duration: Int = Toast.LENGTH_SHORT)
|
||||
|
||||
/**
|
||||
* Display a toast in this context.
|
||||
*
|
||||
* @param text the text to display.
|
||||
* @param duration the duration of the toast. Defaults to short.
|
||||
*/
|
||||
@@ -31,6 +34,7 @@ fun Context.toast(text: String?, duration: Int = Toast.LENGTH_SHORT) {
|
||||
|
||||
/**
|
||||
* Helper method to create a notification.
|
||||
*
|
||||
* @param func the function that will execute inside the builder.
|
||||
* @return a notification to be displayed or updated.
|
||||
*/
|
||||
@@ -42,12 +46,37 @@ inline fun Context.notification(func: NotificationCompat.Builder.() -> Unit): No
|
||||
|
||||
/**
|
||||
* Checks if the give permission is granted.
|
||||
*
|
||||
* @param permission the permission to check.
|
||||
* @return true if it has permissions.
|
||||
*/
|
||||
fun Context.hasPermission(permission: String)
|
||||
= ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
|
||||
|
||||
/**
|
||||
* Returns the color for the given attribute.
|
||||
*
|
||||
* @param resource the attribute.
|
||||
*/
|
||||
fun Context.getResourceColor(@StringRes resource: Int): Int {
|
||||
val typedArray = obtainStyledAttributes(intArrayOf(resource))
|
||||
val attrValue = typedArray.getColor(0, 0)
|
||||
typedArray.recycle()
|
||||
return attrValue
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts to dp.
|
||||
*/
|
||||
val Int.pxToDp: Int
|
||||
get() = (this / Resources.getSystem().displayMetrics.density).toInt()
|
||||
|
||||
/**
|
||||
* Converts to px.
|
||||
*/
|
||||
val Int.dpToPx: Int
|
||||
get() = (this * Resources.getSystem().displayMetrics.density).toInt()
|
||||
|
||||
/**
|
||||
* Property to get the notification manager from the context.
|
||||
*/
|
||||
@@ -64,4 +93,5 @@ val Context.connectivityManager: ConnectivityManager
|
||||
* Property to get the power manager from the context.
|
||||
*/
|
||||
val Context.powerManager: PowerManager
|
||||
get() = getSystemService(Context.POWER_SERVICE) as PowerManager
|
||||
get() = getSystemService(Context.POWER_SERVICE) as PowerManager
|
||||
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
package eu.kanade.tachiyomi.util
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.net.ConnectivityManager
|
||||
import android.os.BatteryManager
|
||||
|
||||
object DeviceUtil {
|
||||
fun isPowerConnected(context: Context): Boolean {
|
||||
val intent = context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
|
||||
intent?.let {
|
||||
val plugged = it.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)
|
||||
return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fun isNetworkConnected(context: Context): Boolean {
|
||||
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
val activeNetwork = cm.activeNetworkInfo
|
||||
return activeNetwork != null && activeNetwork.isConnectedOrConnecting
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package eu.kanade.tachiyomi.util
|
||||
|
||||
import android.content.res.Resources
|
||||
|
||||
val Int.pxToDp: Int
|
||||
get() = (this / Resources.getSystem().displayMetrics.density).toInt()
|
||||
|
||||
val Int.dpToPx: Int
|
||||
get() = (this * Resources.getSystem().displayMetrics.density).toInt()
|
||||
@@ -12,6 +12,7 @@ import android.widget.ImageView
|
||||
fun ImageView.setVectorCompat(@DrawableRes drawable: Int, tint: Int? = null) {
|
||||
val vector = VectorDrawableCompat.create(resources, drawable, context.theme)
|
||||
if (tint != null) {
|
||||
vector?.mutate()
|
||||
vector?.setTint(tint)
|
||||
}
|
||||
setImageDrawable(vector)
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
package eu.kanade.tachiyomi.util
|
||||
|
||||
import android.content.res.Resources
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.support.annotation.AttrRes
|
||||
import android.support.annotation.StringRes
|
||||
|
||||
fun Resources.Theme.getResourceColor(@StringRes resource: Int): Int {
|
||||
val typedArray = obtainStyledAttributes(intArrayOf(resource))
|
||||
val attrValue = typedArray.getColor(0, 0)
|
||||
typedArray.recycle()
|
||||
return attrValue
|
||||
}
|
||||
|
||||
fun Resources.Theme.getResourceDrawable(@StringRes resource: Int): Drawable {
|
||||
val typedArray = obtainStyledAttributes(intArrayOf(resource))
|
||||
val attrValue = typedArray.getDrawable(0)
|
||||
typedArray.recycle()
|
||||
return attrValue
|
||||
}
|
||||
|
||||
fun Resources.Theme.getResourceId(@AttrRes resource: Int, fallback: Int): Int {
|
||||
val typedArray = obtainStyledAttributes(intArrayOf(resource))
|
||||
val attrValue = typedArray.getResourceId(0, fallback)
|
||||
typedArray.recycle()
|
||||
return attrValue
|
||||
}
|
||||
Reference in New Issue
Block a user