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

139 lines
4.3 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
import android.graphics.Paint
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
import net.osmand.telegram.R
2018-06-14 20:01:10 +02:00
import java.io.File
object AndroidUtils {
2018-07-13 14:53:58 +02:00
private const val PERMISSION_REQUEST_LOCATION = 1
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
}
2018-07-13 14:53:58 +02:00
fun requestLocationPermission(activity: Activity) {
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), PERMISSION_REQUEST_LOCATION)
}
2018-06-13 19:14:08 +02:00
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-07-12 19:40:43 +02:00
fun addStatusBarPadding19v(ctx: Context, view: View) {
if (Build.VERSION.SDK_INT >= 19) {
view.apply {
setPadding(paddingLeft, paddingTop + getStatusBarHeight(ctx), paddingRight, paddingBottom)
}
}
}
fun getPopupMenuWidth(ctx: Context, titles: Collection<String>): Int {
val txtSize = ctx.resources.getDimensionPixelSize(R.dimen.list_item_title_text_size)
val paint = Paint().apply { textSize = txtSize.toFloat() }
val maxTextWidth = titles.map { paint.measureText(it) }.max()
if (maxTextWidth != null) {
val maxItemWidth = maxTextWidth.toInt() + AndroidUtils.dpToPx(ctx, 33f)
val minWidth = AndroidUtils.dpToPx(ctx, 100f)
return maxOf(minWidth, maxItemWidth)
}
return 0
}
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
}
2018-08-08 17:48:16 +02:00
fun getPlayMarketIntent(ctx: Context, packageName: String) =
Intent(Intent.ACTION_VIEW, Uri.parse(AndroidUtils.getPlayMarketLink(ctx, packageName)))
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()
}