aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/services/BaseJobService.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/services/BaseJobService.kt')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/services/BaseJobService.kt59
1 files changed, 59 insertions, 0 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/BaseJobService.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/BaseJobService.kt
new file mode 100644
index 00000000..3cc7deaf
--- /dev/null
+++ b/app/src/main/kotlin/com/pitchedapps/frost/services/BaseJobService.kt
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2018 Allan Wang
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.pitchedapps.frost.services
+
+import android.app.job.JobParameters
+import android.app.job.JobService
+import androidx.annotation.CallSuper
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.Job
+import kotlin.coroutines.CoroutineContext
+
+abstract class BaseJobService : JobService(), CoroutineScope {
+
+ private lateinit var job: Job
+ override val coroutineContext: CoroutineContext
+ get() = Dispatchers.Main + job
+
+ protected val startTime = System.currentTimeMillis()
+
+ /**
+ * Note that if a job plans on running asynchronously, it should return true
+ */
+ @CallSuper
+ override fun onStartJob(params: JobParameters?): Boolean {
+ job = Job()
+ return false
+ }
+
+ @CallSuper
+ override fun onStopJob(params: JobParameters?): Boolean {
+ job.cancel()
+ return false
+ }
+}
+
+/*
+ * Collection of ids for job services.
+ * These should all be unique
+ */
+
+const val NOTIFICATION_JOB_NOW = 6
+const val NOTIFICATION_PERIODIC_JOB = 7
+const val LOCAL_SERVICE_BASE = 110
+const val REQUEST_SERVICE_BASE = 220