PulseCatchService.kt 1.86 KB
Newer Older
nk2's avatar
nk2 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
package ga.nk2ishere.dev.musclecar.features.pulse

import android.app.Notification
import android.app.Service
import android.content.Intent
import android.os.IBinder
import ga.nk2ishere.dev.musclecar.R.mipmap.ic_launcher
import android.app.PendingIntent
import android.app.NotificationManager
import android.support.v4.app.NotificationCompat
import ga.nk2ishere.dev.musclecar.R
import ga.nk2ishere.dev.musclecar.common.libs.applySchedulers
import ga.nk2ishere.dev.musclecar.common.libs.random
import ga.nk2ishere.dev.musclecar.common.libs.sendNotification
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import org.jetbrains.anko.notificationManager
import java.lang.Thread.sleep
import kotlin.concurrent.thread


/**
 * Created by nk2 on 06/02/2018.
 */
class PulseCatchService : Service() {
    companion object {
        val observer: Subject<Int> = PublishSubject.create()
        val DEFAULT_NOTIFICATION_ID = 101
        var started = false
            private set
    }
    val worker = thread(false) {
        //Task
        while (started) {
            //TODO BLUETOOTH SEX
            observer.onNext((-1000..1000).random())
            sleep(500)
        }
    }

    override fun onCreate() {
        super.onCreate()
        started = true
        worker.start()
        observer.applySchedulers()
    }

    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        sendNotification(DEFAULT_NOTIFICATION_ID, "Ticker", "MuscleCar", "Linstening")
        return Service.START_REDELIVER_INTENT
    }

    override fun onBind(intent: Intent): IBinder? {
        return null
    }

    override fun onDestroy() {
        super.onDestroy()
        started = false
        worker.interrupt()
        notificationManager.cancel(DEFAULT_NOTIFICATION_ID)
        stopSelf()
    }
}