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