ソースを参照

More cleaning up...

Thomas Chef 3 年 前
コミット
a1002d88a5

+ 15 - 24
app/src/main/java/com/flacksta/chef/journeygpstracker/TrackingService.kt

@@ -9,11 +9,9 @@ import android.content.pm.PackageManager
 import android.graphics.Bitmap
 import android.graphics.BitmapFactory
 import android.location.Location
-import android.os.Build
 import android.os.IBinder
 import android.os.Looper
 import android.util.Log
-import androidx.annotation.RequiresApi
 import androidx.core.app.ActivityCompat
 import androidx.core.app.NotificationCompat
 import androidx.core.content.ContextCompat
@@ -21,7 +19,9 @@ import com.flacksta.chef.journeygpstracker.backend.BackendClient
 import com.flacksta.chef.journeygpstracker.database.GpsData
 import com.flacksta.chef.journeygpstracker.database.GpsPosRepository
 import com.google.android.gms.location.*
-import kotlinx.coroutines.*
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
 
 class TrackingService : Service() {
 
@@ -34,17 +34,13 @@ class TrackingService : Service() {
 
     private lateinit var trackApp : JourneyGpsTrackerApplication
 
+    private val mNotificationId = 123
+
     override fun onCreate() {
         super.onCreate()
         Log.i(mTag,"onCreate()")
-        generateForegroundNotification()
         trackApp = JourneyGpsTrackerApplication()
         startForeground(mNotificationId, generateForegroundNotification() )
-
-        // Start background upload functions...
-        BackendClient()
-
-        Log.i(mTag,"Requested foreground service...")
     }
 
     private fun requestLocationUpdates() {
@@ -91,27 +87,22 @@ class TrackingService : Service() {
     }
 
     override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
-        @Suppress("UNUSED_VARIABLE") val retVal: Int = super.onStartCommand(intent, flags, startId)
+        super.onStartCommand(intent, flags, startId)
         Log.i(mTag,"onStartCommand()")
+        startForeground(mNotificationId, generateForegroundNotification() )
         requestLocationUpdates()
+        BackendClient()
         return START_STICKY
     }
 
-    private var iconNotification: Bitmap? = null
-    private var mNotificationManager: NotificationManager? = null
-    private val mNotificationId = 123
-
     private fun generateForegroundNotification() : Notification {
+
+        val mNotificationManager: NotificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
+        val iconNotification: Bitmap? = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
         val intentMainLanding = Intent(this, MainActivity::class.java)
-        val pendingIntent =
-                PendingIntent.getActivity(this, 0, intentMainLanding, PendingIntent.FLAG_IMMUTABLE)
-        iconNotification = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
-        if (mNotificationManager == null) {
-            mNotificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
-        }
+        val pendingIntent = PendingIntent.getActivity(this, 0, intentMainLanding, PendingIntent.FLAG_IMMUTABLE)
 
-        assert(mNotificationManager != null)
-        mNotificationManager?.createNotificationChannelGroup(
+        mNotificationManager.createNotificationChannelGroup(
                 NotificationChannelGroup("chats_group", "Chats")
         )
         val notificationChannel =
@@ -119,7 +110,7 @@ class TrackingService : Service() {
                         NotificationManager.IMPORTANCE_MIN)
         notificationChannel.enableLights(false)
         notificationChannel.lockscreenVisibility = Notification.VISIBILITY_SECRET
-        mNotificationManager?.createNotificationChannel(notificationChannel)
+        mNotificationManager.createNotificationChannel(notificationChannel)
 
         val builder = NotificationCompat.Builder(this, "service_channel")
 
@@ -134,7 +125,7 @@ class TrackingService : Service() {
                 .setOngoing(true)
                 .setForegroundServiceBehavior(FOREGROUND_SERVICE_IMMEDIATE)
         if (iconNotification != null) {
-            builder.setLargeIcon(Bitmap.createScaledBitmap(iconNotification!!, 128, 128, false))
+            builder.setLargeIcon(Bitmap.createScaledBitmap(iconNotification, 128, 128, false))
         }
         builder.color = ContextCompat.getColor(this, R.color.purple_200)
         return builder.build()

+ 1 - 1
app/src/main/java/com/flacksta/chef/journeygpstracker/backend/BackendClient.kt

@@ -30,7 +30,7 @@ class BackendClient {
 
     private fun startTimer() {
         fixedRateTimer(name = "hello-timer",
-                initialDelay = 10000, period = 30000) {
+                initialDelay = 10000, period = 60000) {
 
             val  dao :GpsDataDao = trackApp.database.gpsDataDao()