library / org.akop.kotx.ext.app / android.content.Context

Extensions for android.content.Context

appVersionCode

fun Context.appVersionCode(): Int

Returns the app version code, or -1 if it can't be determined.

appVersionName

fun Context.appVersionName(): String?

Returns the app version string, or null if it can't be determined.

broadcastLocal

fun Context.broadcastLocal(action: String, block: Intent.() -> Unit = {}): Unit

Sends a local broadcast by creating a new Intent with specified action, calling optional block with the instance before calling LocalBroadcastManager.sendBroadcast.

fun Context.broadcastLocal(intent: Intent): Unit

Sends a local broadcast by calling LocalBroadcastManager.sendBroadcast with specified intent.

connectivityManager

val Context.connectivityManager: ConnectivityManager

Returns ConnectivityManager instance by calling Context.getSystemService.

defaultSharedPreferences

val Context.defaultSharedPreferences: SharedPreferences

Returns default SharedPreferences instance by calling PreferenceManager.getDefaultSharedPreferences.

getVectorDrawable

fun Context.getVectorDrawable(resId: Int): Drawable?

Helper method call to AppCompatResources.getDrawable - returns a drawable with specific resource id. Despite the name, the function this method calls will also load non-vector drawables. Will also work on pre-vector versions of the platform.

inputMethodManager

val Context.inputMethodManager: InputMethodManager

Returns InputMethodManager instance by calling Context.getSystemService.

isConnectedToNetwork

val Context.isConnectedToNetwork: Boolean

Returns true if the device is connected to at least one network. Note that this doesn't necessarily indicate internet access.

localBroadcastManager

val Context.localBroadcastManager: LocalBroadcastManager

Returns a LocalBroadcastManager instance by calling LocalBroadcastManager.getInstance.

notificationManager

val Context.notificationManager: NotificationManager

Returns NotificationManager instance by calling Context.getSystemService.

resolveIntAttribute

fun Context.resolveIntAttribute(resId: Int, resolveRefs: Boolean = true): Int

Resolves an attribute with specified resource id, returning its integer value if successful. Throws an exception if attribute is invalid.

resolveIntAttributeSafe

fun Context.resolveIntAttributeSafe(resId: Int, resolveRefs: Boolean = true): Int?

Safe variant of Context.resolveIntAttribute. Resolves an attribute with specified resource id, returning its integer value if successful, null otherwise.

searchManager

val Context.searchManager: SearchManager

Returns SearchManager instance by calling Context.getSystemService.

showAlertDialog

fun Context.showAlertDialog(message: String? = null, title: String? = null, block: AlertDialogBuilder.() -> Unit = {}): Dialog

Displays an alert dialog by creating a new AlertDialogBuilder using the title and message provided, then passing it to block with the builder as receiver.

fun Context.showAlertDialog(messageResId: Int, titleResId: Int = 0, block: AlertDialogBuilder.() -> Unit = {}): Dialog

Displays an alert dialog by creating a new AlertDialogBuilder using the title and message string resource ids provided, then passing it to block with the builder as receiver.

showToast

fun Context.showToast(messageResId: Int, duration: Int = TOAST_DEFAULT_DURATION): Unit

Creates and displays a new Toast with specified message.

fun Context.showToast(message: String?, duration: Int = TOAST_DEFAULT_DURATION): Unit

Creates and displays a new Toast with specified message.

startActivity

fun <T : Activity> Context.startActivity(block: Intent.() -> Unit = {}): Unit

Starts a new activity by creating a new Intent, calling block with the new instance, then passing the intent to Context.startActivity.

withStyledAttributes

fun Context.withStyledAttributes(attrs: IntArray, set: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0, block: TypedArray.() -> Unit): Unit

Obtains TypedArray by calling Resources.Theme.obtainStyledAttributes with supplied parameters, then passing the array to the supplied block, before recycling it.