OsmAnd/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt

107 lines
3.1 KiB
Kotlin
Raw Normal View History

2018-06-07 16:44:43 +02:00
package net.osmand.telegram.utils
import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Configuration
2018-06-14 20:01:10 +02:00
import android.net.Uri
import android.os.Build
2018-06-13 19:14:08 +02:00
import android.support.annotation.AttrRes
import android.support.annotation.ColorInt
import android.support.v4.app.ActivityCompat
2018-06-14 20:01:10 +02:00
import android.support.v4.content.FileProvider
2018-06-13 19:14:08 +02:00
import android.util.TypedValue
import android.util.TypedValue.COMPLEX_UNIT_DIP
import android.view.View
import android.view.inputmethod.InputMethodManager
2018-06-14 20:01:10 +02:00
import java.io.File
object AndroidUtils {
2018-06-13 19:14:08 +02:00
private fun isHardwareKeyboardAvailable(context: Context): Boolean {
return context.resources.configuration.keyboard != Configuration.KEYBOARD_NOKEYS
}
fun softKeyboardDelayed(view: View) {
view.post {
2018-06-17 23:08:29 +02:00
view.requestFocus()
2018-06-13 19:14:08 +02:00
if (!isHardwareKeyboardAvailable(view.context)) {
val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
imm?.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
}
}
fun hideSoftKeyboard(activity: Activity, input: View?) {
val inputMethodManager = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager?
if (inputMethodManager != null) {
if (input != null) {
val windowToken = input.windowToken
if (windowToken != null) {
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
}
}
}
2018-07-06 10:28:26 +02:00
2018-06-13 19:14:08 +02:00
fun isLocationPermissionAvailable(context: Context): Boolean {
return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
}
fun dpToPx(ctx: Context, dp: Float): Int {
val r = ctx.resources
return TypedValue.applyDimension(
COMPLEX_UNIT_DIP,
dp,
r.displayMetrics
).toInt()
}
2018-07-12 15:38:20 +02:00
fun getStatusBarHeight(ctx: Context): Int {
var result = 0
val resourceId = ctx.resources.getIdentifier("status_bar_height", "dimen", "android")
if (resourceId > 0) {
result = ctx.resources.getDimensionPixelSize(resourceId)
}
return result
}
2018-06-13 19:14:08 +02:00
@ColorInt
fun getAttrColor(ctx: Context, @AttrRes attrId: Int, @ColorInt defaultColor: Int = 0): Int {
val ta = ctx.theme.obtainStyledAttributes(intArrayOf(attrId))
val color = ta.getColor(0, defaultColor)
ta.recycle()
return color
}
2018-06-14 20:01:10 +02:00
fun getUriForFile(context: Context, file: File): Uri {
return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
Uri.fromFile(file)
} else {
FileProvider.getUriForFile(context, "net.osmand.telegram.fileprovider", file)
}
}
fun isGooglePlayInstalled(ctx: Context): Boolean {
try {
ctx.packageManager.getPackageInfo("com.android.vending", 0)
} catch (e: PackageManager.NameNotFoundException) {
return false
}
return true
}
fun getPlayMarketLink(ctx: Context, packageName: String): String {
if (isGooglePlayInstalled(ctx)) {
return "market://details?id=$packageName"
}
return "https://play.google.com/store/apps/details?id=$packageName"
}
fun isIntentSafe(ctx: Context, intent: Intent) =
ctx.packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).isNotEmpty()
}