|
|
|
@ -3,21 +3,28 @@ package com.cloud.diplomaticquarterapp.utils
|
|
|
|
|
import android.app.NotificationChannel
|
|
|
|
|
import android.app.NotificationManager
|
|
|
|
|
import android.app.PendingIntent
|
|
|
|
|
import android.content.ClipData.newIntent
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.Intent
|
|
|
|
|
import android.os.Build
|
|
|
|
|
import android.view.View
|
|
|
|
|
import android.widget.Toast
|
|
|
|
|
import androidx.annotation.Nullable
|
|
|
|
|
import androidx.core.app.NotificationCompat
|
|
|
|
|
import androidx.core.app.TaskStackBuilder
|
|
|
|
|
import com.cloud.diplomaticquarterapp.BuildConfig
|
|
|
|
|
import com.cloud.diplomaticquarterapp.MainActivity
|
|
|
|
|
import com.cloud.diplomaticquarterapp.R
|
|
|
|
|
import com.github.kittinunf.fuel.core.extensions.jsonBody
|
|
|
|
|
import com.github.kittinunf.fuel.httpPost
|
|
|
|
|
import com.google.gson.Gson
|
|
|
|
|
import com.google.gson.reflect.TypeToken
|
|
|
|
|
import io.flutter.plugin.common.MethodChannel
|
|
|
|
|
import org.json.JSONArray
|
|
|
|
|
import org.json.JSONException
|
|
|
|
|
import org.json.JSONObject
|
|
|
|
|
import java.util.*
|
|
|
|
|
import kotlin.concurrent.timerTask
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HMGUtils {
|
|
|
|
|
|
|
|
|
|
companion object{
|
|
|
|
@ -25,34 +32,34 @@ class HMGUtils {
|
|
|
|
|
fun getPlatformChannel():MethodChannel{
|
|
|
|
|
return platformChannel
|
|
|
|
|
}
|
|
|
|
|
fun setPlatformChannel(channel:MethodChannel){
|
|
|
|
|
fun setPlatformChannel(channel: MethodChannel){
|
|
|
|
|
platformChannel = channel
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun timer(delay:Long, repeat:Boolean, tick:(Timer)->Unit) : Timer{
|
|
|
|
|
fun timer(delay: Long, repeat: Boolean, tick: (Timer) -> Unit) : Timer{
|
|
|
|
|
val timer = Timer()
|
|
|
|
|
if(repeat)
|
|
|
|
|
timer.schedule(timerTask {
|
|
|
|
|
tick(timer)
|
|
|
|
|
},delay,delay)
|
|
|
|
|
}, delay, delay)
|
|
|
|
|
else
|
|
|
|
|
timer.schedule(timerTask {
|
|
|
|
|
tick(timer)
|
|
|
|
|
},delay)
|
|
|
|
|
}, delay)
|
|
|
|
|
|
|
|
|
|
return timer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun popMessage(context:MainActivity, message:String){
|
|
|
|
|
fun popMessage(context: MainActivity, message: String){
|
|
|
|
|
context.runOnUiThread {
|
|
|
|
|
Toast.makeText(context,message,Toast.LENGTH_LONG).show()
|
|
|
|
|
Toast.makeText(context, message, Toast.LENGTH_LONG).show()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun popFlutterText(context:MainActivity, key:String){
|
|
|
|
|
fun popFlutterText(context: MainActivity, key: String){
|
|
|
|
|
context.runOnUiThread {
|
|
|
|
|
FlutterText.with(key){
|
|
|
|
|
Toast.makeText(context,it,Toast.LENGTH_LONG).show()
|
|
|
|
|
Toast.makeText(context, it, Toast.LENGTH_LONG).show()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -66,9 +73,8 @@ private fun Timer.schedule(timerTask: TimerTask) {
|
|
|
|
|
}
|
|
|
|
|
private const val NOTIFICATION_CHANNEL_ID = BuildConfig.APPLICATION_ID + ".channel"
|
|
|
|
|
|
|
|
|
|
fun sendNotification(context: Context, message: String) {
|
|
|
|
|
val notificationManager = context
|
|
|
|
|
.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
|
|
|
fun sendNotification(context: Context, title:String, @Nullable subtitle:String?, message:String?) {
|
|
|
|
|
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
|
|
|
|
|
&& notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID) == null) {
|
|
|
|
@ -80,23 +86,63 @@ fun sendNotification(context: Context, message: String) {
|
|
|
|
|
notificationManager.createNotificationChannel(channel)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val intent = Intent(context,MainActivity::class.java)
|
|
|
|
|
val intent = Intent(context, MainActivity::class.java)
|
|
|
|
|
|
|
|
|
|
val stackBuilder = TaskStackBuilder.create(context)
|
|
|
|
|
.addParentStack(MainActivity::class.java)
|
|
|
|
|
.addNextIntent(intent)
|
|
|
|
|
val notificationPendingIntent = stackBuilder
|
|
|
|
|
.getPendingIntent(getUniqueId(), PendingIntent.FLAG_UPDATE_CURRENT)
|
|
|
|
|
val notificationPendingIntent = stackBuilder.getPendingIntent(getUniqueId(), PendingIntent.FLAG_UPDATE_CURRENT)
|
|
|
|
|
|
|
|
|
|
val notification = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
|
|
|
|
|
.setSmallIcon(R.mipmap.ic_launcher)
|
|
|
|
|
.setContentTitle(message)
|
|
|
|
|
.setContentIntent(notificationPendingIntent)
|
|
|
|
|
.setAutoCancel(true)
|
|
|
|
|
.build()
|
|
|
|
|
.setContentTitle(title)
|
|
|
|
|
|
|
|
|
|
subtitle.let { notification.setContentText(it) }
|
|
|
|
|
message.let { notification.setSubText(it) }
|
|
|
|
|
|
|
|
|
|
notificationManager.notify(getUniqueId(), notification.build())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
notificationManager.notify(getUniqueId(), notification)
|
|
|
|
|
private fun getUniqueId() = ((System.currentTimeMillis() % 10000).toInt())
|
|
|
|
|
|
|
|
|
|
fun isJSONValid(jsonString: String?): Boolean {
|
|
|
|
|
try { JSONObject(jsonString) } catch (ex: JSONException) {
|
|
|
|
|
try { JSONArray(jsonString) } catch (ex1: JSONException) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class HTTPResponse<T>(data: T){
|
|
|
|
|
final var data:T = data
|
|
|
|
|
}
|
|
|
|
|
fun <T>httpPost(url: String, body: Map<String, Any?>, onSuccess: (response: HTTPResponse<T>) -> Unit, onError: (error: Exception) -> Unit){
|
|
|
|
|
|
|
|
|
|
val gson = Gson()
|
|
|
|
|
val type = object : TypeToken<T>() {}.type
|
|
|
|
|
val jsonBody = gson.toJson(body)
|
|
|
|
|
url.httpPost()
|
|
|
|
|
.jsonBody(jsonBody, Charsets.UTF_8)
|
|
|
|
|
.timeout(10000)
|
|
|
|
|
.header("Content-Type","application/json")
|
|
|
|
|
.header("Allow","*/*")
|
|
|
|
|
.response { request, response, result ->
|
|
|
|
|
result.fold({ data ->
|
|
|
|
|
val dataString = String(data)
|
|
|
|
|
if(isJSONValid(dataString)){
|
|
|
|
|
val responseData = gson.fromJson<T>(dataString,type)
|
|
|
|
|
onSuccess(HTTPResponse(responseData))
|
|
|
|
|
}else{
|
|
|
|
|
onError(Exception("Invalid response from server (Not a valid JSON)"))
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
onError(it)
|
|
|
|
|
it.localizedMessage
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
private fun getUniqueId() = ((System.currentTimeMillis() % 10000).toInt())
|
|
|
|
|
}
|
|
|
|
|
}
|