2023-02-04 21:52:26 +01:00
|
|
|
package jp.juggler.util.os
|
|
|
|
|
|
|
|
import android.content.Context
|
2024-01-05 18:18:28 +01:00
|
|
|
import androidx.annotation.DrawableRes
|
2024-01-04 02:40:09 +01:00
|
|
|
import androidx.annotation.StringRes
|
2024-01-05 18:18:28 +01:00
|
|
|
import androidx.core.content.ContextCompat
|
2023-02-04 21:52:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* インストゥルメントテストのContextは
|
|
|
|
* applicationContext がnullを返す。
|
|
|
|
* この場合は元のcontextを補うのがベストだろう。
|
|
|
|
*/
|
|
|
|
val Context.applicationContextSafe: Context
|
2024-01-04 02:40:09 +01:00
|
|
|
get() = try {
|
|
|
|
applicationContext ?: this
|
|
|
|
} catch (ex: Throwable) {
|
|
|
|
// applicationContextへのアクセスは例外を出すことがある
|
|
|
|
this
|
|
|
|
}
|
|
|
|
|
|
|
|
fun Context.error(@StringRes resId: Int, vararg args: Any?): Nothing =
|
|
|
|
error(getString(resId, *args))
|
2024-01-05 18:18:28 +01:00
|
|
|
|
|
|
|
fun Context.resDrawable(@DrawableRes resId: Int) =
|
|
|
|
ContextCompat.getDrawable(this, resId)
|