diff --git a/app/src/main/java/com/rosan/dhizuku/data/settings/model/room/DhizukuRoom.kt b/app/src/main/java/com/rosan/dhizuku/data/settings/model/room/DhizukuRoom.kt index 0ca10b2..6c16d87 100644 --- a/app/src/main/java/com/rosan/dhizuku/data/settings/model/room/DhizukuRoom.kt +++ b/app/src/main/java/com/rosan/dhizuku/data/settings/model/room/DhizukuRoom.kt @@ -1,5 +1,7 @@ package com.rosan.dhizuku.data.settings.model.room +import android.content.Context +import android.os.Build import androidx.room.AutoMigration import androidx.room.Database import androidx.room.Room @@ -19,8 +21,13 @@ import org.koin.core.component.get abstract class DhizukuRoom : RoomDatabase() { companion object : KoinComponent { fun createInstance(): DhizukuRoom { + val context = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + get().createDeviceProtectedStorageContext() + } else { + get() + } return Room.databaseBuilder( - get(), + context, DhizukuRoom::class.java, "dhizuku.db", ).build() diff --git a/app/src/main/java/com/rosan/dhizuku/server/RunningService.kt b/app/src/main/java/com/rosan/dhizuku/server/RunningService.kt index c0214ca..fcd20c9 100644 --- a/app/src/main/java/com/rosan/dhizuku/server/RunningService.kt +++ b/app/src/main/java/com/rosan/dhizuku/server/RunningService.kt @@ -1,13 +1,12 @@ package com.rosan.dhizuku.server -import android.app.Service import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.IntentFilter import android.content.pm.ServiceInfo import android.os.Build -import android.os.IBinder +import androidx.core.app.JobIntentService import androidx.core.app.NotificationChannelCompat import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat @@ -22,13 +21,12 @@ import kotlinx.coroutines.launch import org.koin.core.component.KoinComponent import org.koin.core.component.inject -class RunningService : Service(), KoinComponent { + +class RunningService : JobIntentService(), KoinComponent { companion object { + private const val JOB_ID = 1 fun start(context: Context) { - val intent = Intent(context, RunningService::class.java) - /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - context.startForegroundService(intent) - else */context.startService(intent) + enqueueWork(context, RunningService::class.java, JOB_ID, Intent()) } } @@ -50,14 +48,10 @@ class RunningService : Service(), KoinComponent { } } - override fun onBind(intent: Intent?): IBinder? { - return null - } - override fun onCreate() { super.onCreate() - runForeground() - registerPackageReceiver() + // runForeground() + // registerPackageReceiver() } override fun onDestroy() { @@ -65,6 +59,10 @@ class RunningService : Service(), KoinComponent { super.onDestroy() } + override fun onHandleWork(intent: Intent) { + registerPackageReceiver() + } + private fun registerPackageReceiver() { scope.launch { repo.all() @@ -91,34 +89,4 @@ class RunningService : Service(), KoinComponent { return true } - - private fun runForeground() { - val manager = NotificationManagerCompat.from(this) - val channelName = "running_service_channel" - val channel: NotificationChannelCompat = - NotificationChannelCompat.Builder(channelName, NotificationManagerCompat.IMPORTANCE_MAX) - .setName(getString(R.string.service_channel_name)) - .setVibrationEnabled(false) - .setSound(null, null) - .setShowBadge(false) - .build() - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - manager.createNotificationChannel(channel) - val notificationId = 1 - val notification = NotificationCompat.Builder(this, channel.id) - .setSmallIcon(R.drawable.round_hourglass_empty_black_24) - .setContentTitle(getString(R.string.service_running)) - .setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_DEFERRED) - .setPriority(NotificationCompat.PRIORITY_MIN) - .setAutoCancel(false) - .setOngoing(true) - .build() - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) - startForeground(notificationId, notification) - else startForeground( - notificationId, - notification, - ServiceInfo.FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING - ) - } } \ No newline at end of file