2016-02-17 01:44:21 +01:00
|
|
|
package eu.kanade.tachiyomi.util
|
|
|
|
|
|
|
|
import android.app.Notification
|
2016-02-23 14:51:18 +01:00
|
|
|
import android.app.NotificationManager
|
2017-01-20 21:18:15 +01:00
|
|
|
import android.content.BroadcastReceiver
|
2016-02-17 01:44:21 +01:00
|
|
|
import android.content.Context
|
2017-01-20 21:18:15 +01:00
|
|
|
import android.content.Intent
|
|
|
|
import android.content.IntentFilter
|
2016-06-05 23:40:27 +02:00
|
|
|
import android.content.pm.PackageManager
|
2017-01-08 18:50:51 +01:00
|
|
|
import android.content.res.Resources
|
2016-11-20 11:20:57 +01:00
|
|
|
import android.net.ConnectivityManager
|
|
|
|
import android.os.PowerManager
|
2016-02-17 01:44:21 +01:00
|
|
|
import android.support.annotation.StringRes
|
|
|
|
import android.support.v4.app.NotificationCompat
|
2016-06-05 23:40:27 +02:00
|
|
|
import android.support.v4.content.ContextCompat
|
2017-01-20 21:18:15 +01:00
|
|
|
import android.support.v4.content.LocalBroadcastManager
|
2016-02-17 01:44:21 +01:00
|
|
|
import android.widget.Toast
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a toast in this context.
|
2017-01-08 18:50:51 +01:00
|
|
|
*
|
2016-02-17 01:44:21 +01:00
|
|
|
* @param resource the text resource.
|
|
|
|
* @param duration the duration of the toast. Defaults to short.
|
|
|
|
*/
|
|
|
|
fun Context.toast(@StringRes resource: Int, duration: Int = Toast.LENGTH_SHORT) {
|
|
|
|
Toast.makeText(this, resource, duration).show()
|
|
|
|
}
|
|
|
|
|
2016-03-13 22:06:32 +01:00
|
|
|
/**
|
|
|
|
* Display a toast in this context.
|
2017-01-08 18:50:51 +01:00
|
|
|
*
|
2016-03-13 22:06:32 +01:00
|
|
|
* @param text the text to display.
|
|
|
|
* @param duration the duration of the toast. Defaults to short.
|
|
|
|
*/
|
|
|
|
fun Context.toast(text: String?, duration: Int = Toast.LENGTH_SHORT) {
|
|
|
|
Toast.makeText(this, text, duration).show()
|
|
|
|
}
|
|
|
|
|
2016-02-17 01:44:21 +01:00
|
|
|
/**
|
|
|
|
* Helper method to create a notification.
|
2017-01-08 18:50:51 +01:00
|
|
|
*
|
2016-02-17 01:44:21 +01:00
|
|
|
* @param func the function that will execute inside the builder.
|
|
|
|
* @return a notification to be displayed or updated.
|
|
|
|
*/
|
|
|
|
inline fun Context.notification(func: NotificationCompat.Builder.() -> Unit): Notification {
|
|
|
|
val builder = NotificationCompat.Builder(this)
|
|
|
|
builder.func()
|
|
|
|
return builder.build()
|
|
|
|
}
|
|
|
|
|
2016-06-05 23:40:27 +02:00
|
|
|
/**
|
|
|
|
* Checks if the give permission is granted.
|
2017-01-08 18:50:51 +01:00
|
|
|
*
|
2016-06-05 23:40:27 +02:00
|
|
|
* @param permission the permission to check.
|
|
|
|
* @return true if it has permissions.
|
|
|
|
*/
|
|
|
|
fun Context.hasPermission(permission: String)
|
|
|
|
= ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
|
|
|
|
|
2017-01-08 18:50:51 +01:00
|
|
|
/**
|
|
|
|
* 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()
|
|
|
|
|
2016-02-23 14:51:18 +01:00
|
|
|
/**
|
|
|
|
* Property to get the notification manager from the context.
|
|
|
|
*/
|
2016-03-12 14:22:40 +01:00
|
|
|
val Context.notificationManager: NotificationManager
|
2016-02-23 14:51:18 +01:00
|
|
|
get() = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
|
|
|
2016-02-17 01:44:21 +01:00
|
|
|
/**
|
2016-11-20 11:20:57 +01:00
|
|
|
* Property to get the connectivity manager from the context.
|
2016-02-17 01:44:21 +01:00
|
|
|
*/
|
2016-11-20 11:20:57 +01:00
|
|
|
val Context.connectivityManager: ConnectivityManager
|
|
|
|
get() = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Property to get the power manager from the context.
|
|
|
|
*/
|
|
|
|
val Context.powerManager: PowerManager
|
2017-01-08 18:50:51 +01:00
|
|
|
get() = getSystemService(Context.POWER_SERVICE) as PowerManager
|
|
|
|
|
2017-01-20 21:18:15 +01:00
|
|
|
/**
|
|
|
|
* Function used to send a local broadcast asynchronous
|
|
|
|
*
|
|
|
|
* @param intent intent that contains broadcast information
|
|
|
|
*/
|
|
|
|
fun Context.sendLocalBroadcast(intent:Intent){
|
|
|
|
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function used to send a local broadcast synchronous
|
|
|
|
*
|
|
|
|
* @param intent intent that contains broadcast information
|
|
|
|
*/
|
|
|
|
fun Context.sendLocalBroadcastSync(intent: Intent) {
|
|
|
|
LocalBroadcastManager.getInstance(this).sendBroadcastSync(intent)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function used to register local broadcast
|
|
|
|
*
|
|
|
|
* @param receiver receiver that gets registered.
|
|
|
|
*/
|
2017-03-26 11:24:39 +02:00
|
|
|
fun Context.registerLocalReceiver(receiver: BroadcastReceiver, filter: IntentFilter) {
|
2017-01-20 21:18:15 +01:00
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function used to unregister local broadcast
|
|
|
|
*
|
|
|
|
* @param receiver receiver that gets unregistered.
|
|
|
|
*/
|
2017-03-26 11:24:39 +02:00
|
|
|
fun Context.unregisterLocalReceiver(receiver: BroadcastReceiver) {
|
2017-01-20 21:18:15 +01:00
|
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver)
|
|
|
|
}
|
|
|
|
|
|
|
|
|