123 lines
4.1 KiB
Kotlin
123 lines
4.1 KiB
Kotlin
package ru.officialdakari.fmd.services
|
|
|
|
import android.app.Service
|
|
import android.content.BroadcastReceiver
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.os.Build
|
|
import android.os.IBinder
|
|
import android.util.Log
|
|
import android.widget.Toast
|
|
|
|
import ru.officialdakari.fmd.LockActivity
|
|
import ru.officialdakari.fmd.RingActivity
|
|
import ru.officialdakari.fmd.Utils
|
|
|
|
import io.socket.client.IO
|
|
import io.socket.client.Socket
|
|
import io.socket.engineio.client.transports.PollingXHR
|
|
import io.socket.engineio.client.transports.WebSocket
|
|
import ru.officialdakari.fmd.LocationUtils
|
|
|
|
class WebSocketService: Service() {
|
|
var ringIntent: Intent? = null
|
|
var lockIntent: Intent? = null
|
|
var loc: LocationUtils = LocationUtils(this)
|
|
|
|
var mSocket: Socket? = null
|
|
|
|
private fun initIntents(context: Context) {
|
|
ringIntent = Intent(context, RingActivity::class.java).apply {
|
|
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
|
|
addFlags(Intent.FLAG_FROM_BACKGROUND)
|
|
}
|
|
lockIntent = Intent(context, LockActivity::class.java).apply {
|
|
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
|
|
addFlags(Intent.FLAG_FROM_BACKGROUND)
|
|
}
|
|
}
|
|
|
|
private fun startWebsocket(context: Context) {
|
|
try {
|
|
val connData = Utils.getServerData(context)
|
|
Log.w("FMD Service", "Connecting IO")
|
|
if (connData == null) {
|
|
Log.w("FMD Service", "connData == null, returning")
|
|
return
|
|
}
|
|
|
|
val opts = IO.Options()
|
|
opts.transports = arrayOf(WebSocket.NAME, PollingXHR.NAME)
|
|
mSocket = IO.socket(connData.first, opts)
|
|
Log.w("FMD Service", "Connecting to ${connData.first}")
|
|
mSocket!!.on(Socket.EVENT_CONNECT) {
|
|
Log.d("FMD Service", "Connected to server")
|
|
mSocket!!.emit("auth", connData.second, "${Build.BRAND} ${Build.MODEL}")
|
|
}.on(Socket.EVENT_CONNECT_ERROR) { args ->
|
|
Log.e("FMD Service", "Connection Error: ${(args[0] as Throwable).stackTraceToString()}")
|
|
}.on(Socket.EVENT_DISCONNECT) {
|
|
Log.d("FMD Service", "Disconnected from server")
|
|
Utils.sleep(15000) {
|
|
startWebsocket(context)
|
|
}
|
|
}.on("ring") {
|
|
ring(context)
|
|
}.on("lock") {
|
|
val message = it[0] as String
|
|
val phoneNum = it[1] as String
|
|
lock(context, message, phoneNum)
|
|
}.on("locate") {
|
|
loc.getLocation({
|
|
mSocket!!.emit("location", it.latitude, it.longitude)
|
|
})
|
|
}
|
|
|
|
mSocket!!.connect()
|
|
} catch (ex: Throwable) {
|
|
ex.printStackTrace()
|
|
Toast.makeText(this, "Failed to connect to server, see logcat", Toast.LENGTH_LONG).show()
|
|
}
|
|
}
|
|
|
|
private fun ring(context: Context) {
|
|
if (ringIntent != null) {
|
|
context.startActivity(ringIntent)
|
|
}
|
|
}
|
|
|
|
private fun lock(context: Context, message: String, phoneNum: String) {
|
|
if (lockIntent != null) {
|
|
Utils.lockPhoneNum = phoneNum
|
|
Utils.lockMessage = message
|
|
Utils.lockDevice(context)
|
|
Utils.sleep(1000) {
|
|
startActivity(lockIntent)
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun onCreate() {
|
|
super.onCreate()
|
|
if (!loc.isStarted()) loc.start()
|
|
if (lockIntent == null || ringIntent == null) {
|
|
initIntents(this)
|
|
}
|
|
startWebsocket(this)
|
|
}
|
|
|
|
override fun onBind(p0: Intent?): IBinder? {
|
|
return null
|
|
}
|
|
|
|
// override fun onReceive(context: Context, intent: Intent) {
|
|
// if (intent.action == "android.intent.action.LOCKED_BOOT_COMPLETED"
|
|
// || intent.action == "android.intent.action.BOOT_COMPLETED") {
|
|
// if (lockIntent == null || ringIntent == null) {
|
|
// initIntents(context)
|
|
// }
|
|
// startWebsocket(context)
|
|
// }
|
|
// }
|
|
} |