@ -9,13 +9,11 @@ import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Color
import android.net.Uri
import android.net.Uri
import android.text.SpannableStringBuilder
import android.util.TypedValue
import android.util.TypedValue
import android.view.View
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.view.inputmethod.InputMethodManager
import android.widget.ImageView
import android.widget.*
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.SearchView
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat
@ -36,216 +34,245 @@ import java.io.File
// Sizes
// Sizes
val Int . dp : Int
val Int . dp : Int
get ( ) = ( this / Resources . getSystem ( ) . displayMetrics . density ) . toInt ( )
get ( ) = ( this / Resources . getSystem ( ) . displayMetrics . density ) . toInt ( )
val Int . px : Int
val Int . px : Int
get ( ) = ( this * Resources . getSystem ( ) . displayMetrics . density ) . toInt ( )
get ( ) = ( this * Resources . getSystem ( ) . displayMetrics . density ) . toInt ( )
val Float . dp : Float
val Float . dp : Float
get ( ) = ( this / Resources . getSystem ( ) . displayMetrics . density )
get ( ) = ( this / Resources . getSystem ( ) . displayMetrics . density )
val Float . px : Float
val Float . px : Float
get ( ) = ( this * Resources . getSystem ( ) . displayMetrics . density )
get ( ) = ( this * Resources . getSystem ( ) . displayMetrics . density )
// Toast
// Toast
fun Activity . toast ( message : String ) {
fun Activity . toast ( message : String ) {
Toast . makeText ( this , message , Toast . LENGTH _SHORT ) . show ( )
Toast . makeText ( this , message , Toast . LENGTH _SHORT ) . show ( )
}
}
fun Fragment . toast ( message : String ) {
fun Fragment . toast ( message : String ) {
Toast . makeText ( requireContext ( ) , message , Toast . LENGTH _SHORT ) . show ( )
Toast . makeText ( requireContext ( ) , message , Toast . LENGTH _SHORT ) . show ( )
}
}
// Open Play Store for rating
// Open Play Store for rating
fun Activity . openPlayStorePage ( ) {
fun Activity . openPlayStorePage ( ) {
val uri = Uri . parse ( " market://details?id= $packageName " )
val uri = Uri . parse ( " market://details?id= $packageName " )
val goToMarket = Intent ( Intent . ACTION _VIEW , uri )
val goToMarket = Intent ( Intent . ACTION _VIEW , uri )
goToMarket . addFlags (
goToMarket . addFlags (
Intent . FLAG _ACTIVITY _NO _HISTORY or Intent . FLAG _ACTIVITY _NEW _DOCUMENT or
Intent . FLAG _ACTIVITY _NO _HISTORY or Intent . FLAG _ACTIVITY _NEW _DOCUMENT or
Intent . FLAG _ACTIVITY _MULTIPLE _TASK
Intent . FLAG _ACTIVITY _MULTIPLE _TASK
)
)
try {
try {
startActivity ( goToMarket )
startActivity ( goToMarket )
} catch ( e : ActivityNotFoundException ) {
} catch ( e : ActivityNotFoundException ) {
startActivity (
startActivity (
Intent (
Intent (
Intent . ACTION _VIEW , Uri . parse (
Intent . ACTION _VIEW , Uri . parse (
" http://play.google.com/store/apps/details?id= $packageName "
" http://play.google.com/store/apps/details?id= $packageName "
)
)
)
)
)
)
}
}
}
}
// Open email for "Contact us"
// Open email for "Contact us"
fun BaseActivity . openContactMail ( subjectStringRes : Int , filePath : String ? = null ) {
fun BaseActivity . openContactMail ( subjectStringRes : Int , filePath : String ? = null ) {
val info =
val info =
" v ${BuildConfig.VERSION_NAME} ( ${BuildConfig.VERSION_CODE} ) - ${AppGuard.isProUser} , Android ${android.os.Build.VERSION.SDK_INT} , ${DeviceUtils.getDeviceName()} "
" v ${BuildConfig.VERSION_NAME} ( ${BuildConfig.VERSION_CODE} ) - ${AppGuard.isProUser} , Android ${android.os.Build.VERSION.SDK_INT} , ${DeviceUtils.getDeviceName()} "
val emailIntent = Intent ( Intent . ACTION _SEND )
val emailIntent = Intent ( Intent . ACTION _SEND )
filePath ?. let {
filePath ?. let {
val databaseFile = File ( it )
val databaseFile = File ( it )
val contentUri = FileProvider . getUriForFile ( this , " net.pokeranalytics.android.fileprovider " , databaseFile )
val contentUri = FileProvider . getUriForFile (
if ( contentUri != null ) {
this ,
emailIntent . addFlags ( Intent . FLAG _GRANT _READ _URI _PERMISSION )
" net.pokeranalytics.android.fileprovider " ,
emailIntent . setDataAndType ( contentUri , contentResolver . getType ( contentUri ) )
databaseFile
emailIntent . putExtra ( Intent . EXTRA _STREAM , contentUri )
)
}
if ( contentUri != null ) {
}
emailIntent . addFlags ( Intent . FLAG _GRANT _READ _URI _PERMISSION )
emailIntent . setDataAndType ( contentUri , contentResolver . getType ( contentUri ) )
emailIntent . type = " message/rfc822 "
emailIntent . putExtra ( Intent . EXTRA _STREAM , contentUri )
emailIntent . putExtra ( Intent . EXTRA _SUBJECT , getString ( subjectStringRes ) )
}
emailIntent . putExtra ( Intent . EXTRA _TEXT , " \n \n $info " )
}
emailIntent . putExtra ( Intent . EXTRA _EMAIL , arrayOf ( URL . SUPPORT _EMAIL . value ) )
emailIntent . type = " message/rfc822 "
startActivity ( Intent . createChooser ( emailIntent , getString ( R . string . contact ) ) )
emailIntent . putExtra ( Intent . EXTRA _SUBJECT , getString ( subjectStringRes ) )
emailIntent . putExtra ( Intent . EXTRA _TEXT , " \n \n $info " )
emailIntent . putExtra ( Intent . EXTRA _EMAIL , arrayOf ( URL . SUPPORT _EMAIL . value ) )
startActivity ( Intent . createChooser ( emailIntent , getString ( R . string . contact ) ) )
}
}
// Open custom tab
// Open custom tab
fun Context . openUrl ( url : String ) {
fun Context . openUrl ( url : String ) {
val browserIntent = Intent ( Intent . ACTION _VIEW , Uri . parse ( url ) )
val browserIntent = Intent ( Intent . ACTION _VIEW , Uri . parse ( url ) )
ContextCompat . startActivity ( this , browserIntent , null )
ContextCompat . startActivity ( this , browserIntent , null )
}
}
// Open custom tab
// Open custom tab
fun Context . areYouSure ( title : Int ? = null , message : Int ? = null , positiveTitle : Int ? = null , proceed : ( ) -> Unit ) {
fun Context . areYouSure (
title : Int ? = null ,
val builder : android . app . AlertDialog . Builder = android . app . AlertDialog . Builder ( this )
message : Int ? = null ,
positiveTitle : Int ? = null ,
val messageResource = message ?: R . string . are _you _sure _you _want _to _do _this
proceed : ( ) -> Unit
builder . setMessage ( messageResource )
) {
title ?. let { builder . setTitle ( it ) }
val builder : android . app . AlertDialog . Builder = android . app . AlertDialog . Builder ( this )
val positiveButtonTitle = positiveTitle ?: R . string . yes
val messageResource = message ?: R . string . are _you _sure _you _want _to _do _this
builder . setPositiveButton ( positiveButtonTitle ) { _ , _ ->
builder . setMessage ( messageResource )
proceed ( )
}
builder . setNegativeButton ( R . string . cancel ) { _ , _ ->
// nothing
}
// builder.setItems(
title ?. let { builder . setTitle ( it ) }
// arrayOf<CharSequence>(
// getString(R.string.yes),
// getString(R.string.cancel)
// )
// ) { _, index ->
// // The 'which' argument contains the index position
// // of the selected item
// when (index) {
// 0 -> proceed()
// 1 -> {} // nothing
// }
// }
builder . create ( ) . show ( )
val positiveButtonTitle = positiveTitle ?: R . string . yes
builder . setPositiveButton ( positiveButtonTitle ) { _ , _ ->
proceed ( )
}
builder . setNegativeButton ( R . string . cancel ) { _ , _ ->
// nothing
}
builder . create ( ) . show ( )
}
}
// Display Alert Dialog
// Display Alert Dialog
fun Activity . showAlertDialog ( title : Int ? = null , message : Int ? = null ) {
fun Activity . showAlertDialog ( title : Int ? = null , message : Int ? = null ) {
showAlertDialog ( this , title , message )
showAlertDialog ( this , title , message )
}
}
fun Fragment . showAlertDialog ( title : Int ? = null , messageResId : Int ? = null , message : String ? = null ) {
fun Fragment . showAlertDialog (
context ?. let {
title : Int ? = null ,
showAlertDialog ( it , title , messageResId , message )
messageResId : Int ? = null ,
}
message : String ? = null
) {
context ?. let {
showAlertDialog ( it , title , messageResId , message )
}
}
fun showEditTextAlertDialog (
context : Context , inputType : Int , title : Int ? = null , messageResId : Int ? = null , message : String ? = null ,
editTextText : String ? = null , positiveAction : ( ( String ) -> Unit ) ? = null
) {
val builder = AlertDialog . Builder ( context )
title ?. let {
builder . setTitle ( title )
}
messageResId ?. let {
builder . setMessage ( messageResId )
}
message ?. let {
builder . setMessage ( it )
}
val editText = EditText ( context )
editTextText ?. let {
editText . text = SpannableStringBuilder ( it )
}
editText . setTextColor ( ContextCompat . getColor ( context , R . color . white ) )
editText . inputType = inputType
builder . setView ( editText )
builder . setPositiveButton ( net . pokeranalytics . android . R . string . ok ) { _ , _ ->
positiveAction ?. invoke ( editText . text . toString ( ) )
}
builder . show ( )
}
}
/ * *
/ * *
* Create and show an alert dialog
* Create and show an alert dialog
* /
* /
fun showAlertDialog (
fun showAlertDialog (
context : Context , title : Int ? = null , messageResId : Int ? = null , message : String ? = null , cancelButtonTitle : Int ? = null , showCancelButton : Boolean = false ,
context : Context , title : Int ? = null , messageResId : Int ? = null , message : String ? = null ,
positiveAction : ( ( ) -> Unit ) ? = null , negativeAction : ( ( ) -> Unit ) ? = null
cancelButtonTitle : Int ? = null , showCancelButton : Boolean = false ,
positiveAction : ( ( ) -> Unit ) ? = null , negativeAction : ( ( ) -> Unit ) ? = null
) {
) {
val builder = AlertDialog . Builder ( context )
val builder = AlertDialog . Builder ( context )
title ?. let {
title ?. let {
builder . setTitle ( title )
builder . setTitle ( title )
}
}
messageResId ?. let {
messageResId ?. let {
builder . setMessage ( messageResId )
builder . setMessage ( messageResId )
}
}
message ?. let {
message ?. let {
builder . setMessage ( it )
builder . setMessage ( it )
}
}
builder . setPositiveButton ( net . pokeranalytics . android . R . string . ok ) { _ , _ ->
builder . setPositiveButton ( net . pokeranalytics . android . R . string . ok ) { _ , _ ->
positiveAction ?. invoke ( )
positiveAction ?. invoke ( )
}
}
if ( cancelButtonTitle != null ) {
if ( cancelButtonTitle != null ) {
builder . setNegativeButton ( cancelButtonTitle ) { _ , _ ->
builder . setNegativeButton ( cancelButtonTitle ) { _ , _ ->
negativeAction ?. invoke ( )
negativeAction ?. invoke ( )
}
}
} else if ( showCancelButton ) {
} else if ( showCancelButton ) {
builder . setNegativeButton ( R . string . cancel ) { _ , _ ->
builder . setNegativeButton ( R . string . cancel ) { _ , _ ->
negativeAction ?. invoke ( )
negativeAction ?. invoke ( )
}
}
}
}
builder . show ( )
builder . show ( )
}
}
fun TextView . setTextFormat ( textFormat : TextFormat , context : Context ) {
fun TextView . setTextFormat ( textFormat : TextFormat , context : Context ) {
this . setTextColor ( textFormat . getColor ( context ) )
this . setTextColor ( textFormat . getColor ( context ) )
this . text = textFormat . text
this . text = textFormat . text
}
}
fun View . hideWithAnimation ( ) {
fun View . hideWithAnimation ( ) {
isVisible = true
isVisible = true
animate ( ) . cancel ( )
animate ( ) . cancel ( )
animate ( ) . alpha ( 0f ) . withEndAction { isVisible = false } . start ( )
animate ( ) . alpha ( 0f ) . withEndAction { isVisible = false } . start ( )
}
}
fun View . showWithAnimation ( ) {
fun View . showWithAnimation ( ) {
isVisible = true
isVisible = true
animate ( ) . cancel ( )
animate ( ) . cancel ( )
animate ( ) . alpha ( 1f ) . start ( )
animate ( ) . alpha ( 1f ) . start ( )
}
}
fun View . addCircleRipple ( ) = with ( TypedValue ( ) ) {
fun View . addCircleRipple ( ) = with ( TypedValue ( ) ) {
context . theme . resolveAttribute ( android . R . attr . selectableItemBackgroundBorderless , this , true )
context . theme . resolveAttribute ( android . R . attr . selectableItemBackgroundBorderless , this , true )
setBackgroundResource ( resourceId )
setBackgroundResource ( resourceId )
}
}
fun SearchView . removeMargins ( ) {
fun SearchView . removeMargins ( ) {
val searchEditFrame = findViewById < LinearLayout ? > ( R . id . search _edit _frame )
val searchEditFrame = findViewById < LinearLayout ? > ( R . id . search _edit _frame )
val layoutParams = searchEditFrame ?. layoutParams as LinearLayout . LayoutParams ?
val layoutParams = searchEditFrame ?. layoutParams as LinearLayout . LayoutParams ?
layoutParams ?. leftMargin = 0
layoutParams ?. leftMargin = 0
layoutParams ?. rightMargin = 0
layoutParams ?. rightMargin = 0
searchEditFrame ?. layoutParams = layoutParams
searchEditFrame ?. layoutParams = layoutParams
}
}
fun View . toByteArray ( ) : ByteArray {
fun View . toByteArray ( ) : ByteArray {
return this . convertToBitmap ( ) . toByteArray ( )
return this . convertToBitmap ( ) . toByteArray ( )
}
}
fun View . convertToBitmap ( ) : Bitmap {
fun View . convertToBitmap ( ) : Bitmap {
val b = Bitmap . createBitmap ( this . width , this . height , Bitmap . Config . ARGB _8888 )
val b = Bitmap . createBitmap ( this . width , this . height , Bitmap . Config . ARGB _8888 )
val canvas = Canvas ( b )
val canvas = Canvas ( b )
val background = this . background
val background = this . background
this . background ?. let {
this . background ?. let {
background . draw ( canvas )
background . draw ( canvas )
} ?: run {
} ?: run {
canvas . drawColor ( Color . WHITE )
canvas . drawColor ( Color . WHITE )
}
}
this . draw ( canvas )
this . draw ( canvas )
return b
return b
}
}
fun ImageView . toByteArray ( ) : ByteArray {
fun ImageView . toByteArray ( ) : ByteArray {
return this . drawable . toBitmap ( ) . toByteArray ( )
return this . drawable . toBitmap ( ) . toByteArray ( )
}
}
fun Bitmap . toByteArray ( ) : ByteArray {
fun Bitmap . toByteArray ( ) : ByteArray {
val baos = ByteArrayOutputStream ( )
val baos = ByteArrayOutputStream ( )
this . compress ( Bitmap . CompressFormat . PNG , 100 , baos )
this . compress ( Bitmap . CompressFormat . PNG , 100 , baos )
return baos . toByteArray ( )
return baos . toByteArray ( )
}
}
fun Context . hideKeyboard ( v : View ) {
fun Context . hideKeyboard ( v : View ) {
val imm = v . context . getSystemService ( InputMethodManager :: class . java )
val imm = v . context . getSystemService ( InputMethodManager :: class . java )
imm ?. hideSoftInputFromWindow ( v . windowToken , 0 )
imm ?. hideSoftInputFromWindow ( v . windowToken , 0 )
}
}
//fun Context.showKeyboard(view: View) {
//fun Context.showKeyboard(view: View) {